Commit c8034270 authored by Giuseppe Carboni's avatar Giuseppe Carboni
Browse files

Updated SRTMinorServoLibrary

parent 37bcad5e
Loading
Loading
Loading
Loading
+42 −10
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
 */

#include <boost/python.hpp>
#include <Python.h>
#include "SRTMinorServoCommandLibrary.h"


@@ -21,20 +22,47 @@ namespace MinorServo
    class PySRTMinorServoCommandLibrary : public SRTMinorServoCommandLibrary
    {
    public:
        /**
         * Builds the command used to ask the general status of the minor servos or, eventually, a single servo
         * @param servo_id the ID string of the eventual single servo to retrieve the status. Send no servo_id argument to retrieve the general status of the system
         * @return the composed message
         */
        static boost::python::object status(const std::string servo_id = "");

        /**
         * Builds the command used to configure the telescope for a specific focal path
         * @param configuration the desired focal path to command to the minor servo systems
         * @return the composed message
         */
        static boost::python::object setup(const std::string& configuration);

        /*
         * Builds the command used to stow a single servo system to a given stow position
         * @param servo_id the ID string of the single servo to be stowed
         * @param stow_position the position to which the servo have to stow to
         * @return the composed message
         */
        static boost::python::object stow(const std::string& servo_id, unsigned int stow_position = 1);

        /*
         * Builds the command used to stop a single servo system
         * @param servo_id the ID string of the single servo to be stopped
         * @return the composed message
         */
        static boost::python::object stop(const std::string& servo_id);

        /*
         * Builds the command used to move a single servo to a given set of coordinates
         * This is an overload of the original SRTMinorServoCommandLibrary::preset function
         * An overload was needed in order to handle the coordinates parameter as a Python list instead of a C++ std::vector
         * @param servo_id the ID of the single servo to be moved
         * @param coordinates a Python list containing the N coordinates to be sent to the servo
         * @return the composed message
         */
        static std::string preset(std::string servo_id, boost::python::list& coordinates);
        static boost::python::object preset(const std::string& servo_id, const boost::python::list& coordinates);

        /*
         * Builds the command used to provide a single tracking set of coordinates to a single servo
         * This is an overload of the original SRTMinorServoCommandLibrary::programTrack function
         * An overload was needed in order to handle the coordinates parameter as a Python list instead of a C++ std::vector
         * @param servo_id the ID of the single servo to send the command to
         * @param trajectory_id the ID number of the trajectory the given set of coordinates belongs to
         * @param point_id the ID number of the given set of coordinates inside the trajectory
@@ -42,34 +70,38 @@ namespace MinorServo
         * @param start_time only mandatory for the first point in the trajectory, a double representing the UNIX epoch of the starting instant of the trajectory
         * @return the composed message
         */
        static std::string programTrack(std::string servo_id, unsigned long trajectory_id, unsigned long point_id, boost::python::list& coordinates, double start_time=-1);
        static boost::python::object programTrack(const std::string& servo_id, const unsigned long& trajectory_id, const unsigned long& point_id, const boost::python::list& coordinates, double start_time=-1);

        /*
         * Builds the command used to provide a set of offsets to a given servo
         * This is an overload of the original SRTMinorServoCommandLibrary::offset function
         * An overload was needed in order to handle the coordinates parameter as a Python list instead of a C++ std::vector
         * @param servo_id the ID of the single servo to be moved
         * @param coordinates a Python list containing the N coordinates to be sent to the servo
         * @return the composed message
         */
        static std::string offset(std::string servo_id, boost::python::list& coordinates);
        static boost::python::object offset(const std::string& servo_id, const boost::python::list& coordinates);

        /*
         * Parses the received answer by splitting it and synamically populating a std::map
         * This is an overload of the original SRTMinorServoCommandLibrary::parseAnswer function
         * An overload was needed in order to replace the original std::map object with a boost::python::dict
         * @param answer the string containing the answer received from the VBrain proxy
         * @return a Python dictionary containing the answer splitted into keys and values. The keys are always strings, the values can either be int, double or strings.
         */
        static boost::python::dict parseAnswer(std::string answer);

        static boost::python::dict parseAnswer(const std::string& answer);
    private:
        /*
         * Converts the given Python list into a C++ std::vector object
         * @param py_list the given Python list to be converted
         * @return the composed C++ std::vector containing doubles
         */
        static std::vector<double> pylist2cppvector(boost::python::list& py_list);
        static std::vector<double> pylist2cppvector(const boost::python::list& py_list);

        /**
         * Converts the given std::string to a Python bytestring
         * @param command a reference to the given command string
         * @return the bytestring containing the given command string
         */
        static boost::python::object stringToBytes(const std::string& command);
    };
}

+9 −9
Original line number Diff line number Diff line
@@ -32,14 +32,14 @@ namespace MinorServo
         * @param servo_id the ID string of the eventual single servo to retrieve the status. Send no servo_id argument to retrieve the general status of the system
         * @return the composed message
         */
        static std::string status(std::string servo_id = "");
        static std::string status(const std::string servo_id = "");

        /**
         * Builds the command used to configure the telescope for a specific focal path
         * @param configuration the desired focal path to command to the minor servo systems
         * @return the composed message
         */
        static std::string setup(std::string configuration);
        static std::string setup(const std::string& configuration);

        /*
         * Builds the command used to stow a single servo system to a given stow position
@@ -47,14 +47,14 @@ namespace MinorServo
         * @param stow_position the position to which the servo have to stow to
         * @return the composed message
         */
        static std::string stow(std::string servo_id, unsigned int stow_position = 1);
        static std::string stow(const std::string& servo_id, const unsigned int stow_position = 1);

        /*
         * Builds the command used to stop a single servo system
         * @param servo_id the ID string of the single servo to be stopped
         * @return the composed message
         */
        static std::string stop(std::string servo_id);
        static std::string stop(const std::string& servo_id);

        /*
         * Builds the command used to move a single servo to a given set of virtual coordinates
@@ -62,7 +62,7 @@ namespace MinorServo
         * @param coordinates a vector containing the N coordinates to be sent to the servo
         * @return the composed message
         */
        static std::string preset(std::string servo_id, std::vector<double> coordinates);
        static std::string preset(const std::string& servo_id, const std::vector<double>& coordinates);

        /*
         * Builds the command used to provide a single tracking point of virtual coordinates to a single servo
@@ -73,7 +73,7 @@ namespace MinorServo
         * @param start_time only mandatory for the first point in the trajectory, a double representing the UNIX Epoch of the starting instant of the trajectory
         * @return the composed message
         */
        static std::string programTrack(std::string servo_id, unsigned long trajectory_id, unsigned long point_id, std::vector<double> coordinates, double start_time = 0);
        static std::string programTrack(const std::string& servo_id, const unsigned long& trajectory_id, const unsigned long& point_id, const std::vector<double>& coordinates, const double start_time = 0);

        /*
         * Builds the command used to send a set of virtual offsets to a single servo
@@ -81,14 +81,14 @@ namespace MinorServo
         * @param coordinates a vector containing the N offsets to be added the servo coordinates
         * @return the composed message
         */
        static std::string offset(std::string servo_id, std::vector<double> coordinates);
        static std::string offset(const std::string& servo_id, const std::vector<double>& coordinates);

        /*
         * Parses the received answer by splitting it and dynamically populating a SRTMinorServoAnswerMap object
         * @param answer the string containing the answer received from the VBrain proxy
         * @param original_answer the string containing the answer received from the VBrain proxy
         * @return a SRTMinorServoAnswerMap dictionary containing the answer splitted into keys and values. The keys are always std::string, the values can either be long, double or std::string.
         */
        static SRTMinorServoAnswerMap parseAnswer(std::string answer);
        static SRTMinorServoAnswerMap parseAnswer(const std::string& original_answer);
    };
}

+1 −1
Original line number Diff line number Diff line
@@ -98,7 +98,7 @@ PY_SCRIPTS_L =
PY_MODULES         =
PY_MODULES_L       =

PY_PACKAGES        = #SRTMinorServoCommandLibrary
PY_PACKAGES        = SRTMinorServoCommandLibrary
PY_PACKAGES_L      =
pppppp_MODULES	   =

+39 −14
Original line number Diff line number Diff line
@@ -2,32 +2,42 @@

using namespace MinorServo;

std::string PySRTMinorServoCommandLibrary::preset(std::string servo_id, boost::python::list& coordinates)
boost::python::object PySRTMinorServoCommandLibrary::status(const std::string servo_id)
{
    return SRTMinorServoCommandLibrary::preset(servo_id, PySRTMinorServoCommandLibrary::pylist2cppvector(coordinates));
    return stringToBytes(SRTMinorServoCommandLibrary::status(servo_id));
}

std::string PySRTMinorServoCommandLibrary::programTrack(std::string servo_id, unsigned long trajectory_id, unsigned long point_id, boost::python::list& coordinates, double start_time)
boost::python::object PySRTMinorServoCommandLibrary::setup(const std::string& configuration)
{
    return SRTMinorServoCommandLibrary::programTrack(servo_id, trajectory_id, point_id, PySRTMinorServoCommandLibrary::pylist2cppvector(coordinates), start_time);
    return stringToBytes(SRTMinorServoCommandLibrary::setup(configuration));
}

std::string PySRTMinorServoCommandLibrary::offset(std::string servo_id, boost::python::list& coordinates)
boost::python::object PySRTMinorServoCommandLibrary::stow(const std::string& servo_id, unsigned int stow_position)
{
    return SRTMinorServoCommandLibrary::offset(servo_id, PySRTMinorServoCommandLibrary::pylist2cppvector(coordinates));
    return stringToBytes(SRTMinorServoCommandLibrary::stow(servo_id, stow_position));
}

std::vector<double> PySRTMinorServoCommandLibrary::pylist2cppvector(boost::python::list& py_list)
boost::python::object PySRTMinorServoCommandLibrary::stop(const std::string& servo_id)
{
    std::vector<double> cpp_vector;
    for(unsigned int i = 0; i < len(py_list); i++)
    return stringToBytes(SRTMinorServoCommandLibrary::stop(servo_id));
}

boost::python::object PySRTMinorServoCommandLibrary::preset(const std::string& servo_id, const boost::python::list& coordinates)
{
        cpp_vector.push_back(boost::python::extract<double>(py_list[i]));
    return stringToBytes(SRTMinorServoCommandLibrary::preset(servo_id, pylist2cppvector(coordinates)));
}
    return cpp_vector;

boost::python::object PySRTMinorServoCommandLibrary::programTrack(const std::string& servo_id, const unsigned long& trajectory_id, const unsigned long& point_id, const boost::python::list& coordinates, double start_time)
{
    return stringToBytes(SRTMinorServoCommandLibrary::programTrack(servo_id, trajectory_id, point_id, pylist2cppvector(coordinates), start_time));
}

boost::python::object PySRTMinorServoCommandLibrary::offset(const std::string& servo_id, const boost::python::list& coordinates)
{
    return stringToBytes(SRTMinorServoCommandLibrary::offset(servo_id, pylist2cppvector(coordinates)));
}

boost::python::dict PySRTMinorServoCommandLibrary::parseAnswer(std::string answer)
boost::python::dict PySRTMinorServoCommandLibrary::parseAnswer(const std::string& answer)
{
    auto args = SRTMinorServoCommandLibrary::parseAnswer(answer);

@@ -42,3 +52,18 @@ boost::python::dict PySRTMinorServoCommandLibrary::parseAnswer(std::string answe

    return dictionary;
}

std::vector<double> PySRTMinorServoCommandLibrary::pylist2cppvector(const boost::python::list& py_list)
{
    std::vector<double> cpp_vector;
    for(unsigned int i = 0; i < len(py_list); i++)
    {
        cpp_vector.push_back(boost::python::extract<double>(py_list[i]));
    }
    return cpp_vector;
}

boost::python::object PySRTMinorServoCommandLibrary::stringToBytes(const std::string& command)
{
    return boost::python::object(boost::python::handle<>(PyBytes_FromString(command.c_str())));
}
+9 −8
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@

using namespace MinorServo;

std::string SRTMinorServoCommandLibrary::status(std::string servo_id)
std::string SRTMinorServoCommandLibrary::status(const std::string servo_id)
{
    std::stringstream command;
    command << "STATUS";
@@ -23,28 +23,28 @@ std::string SRTMinorServoCommandLibrary::status(std::string servo_id)
    return command.str();
}

std::string SRTMinorServoCommandLibrary::setup(std::string configuration)
std::string SRTMinorServoCommandLibrary::setup(const std::string& configuration)
{
    std::stringstream command;
    command << "SETUP=" << configuration << CLOSER;
    return command.str();
}

std::string SRTMinorServoCommandLibrary::stow(std::string servo_id, unsigned int stow_position)
std::string SRTMinorServoCommandLibrary::stow(const std::string& servo_id, const unsigned int stow_position)
{
    std::stringstream command;
    command << "STOW=" << servo_id << "," << stow_position << CLOSER;
    return command.str();
}

std::string SRTMinorServoCommandLibrary::stop(std::string servo_id)
std::string SRTMinorServoCommandLibrary::stop(const std::string& servo_id)
{
    std::stringstream command;
    command << "STOP=" << servo_id << CLOSER;
    return command.str();
}

std::string SRTMinorServoCommandLibrary::preset(std::string servo_id, std::vector<double> coordinates)
std::string SRTMinorServoCommandLibrary::preset(const std::string& servo_id, const std::vector<double>& coordinates)
{
    std::stringstream command;
    command << "PRESET=" << servo_id;
@@ -56,7 +56,7 @@ std::string SRTMinorServoCommandLibrary::preset(std::string servo_id, std::vecto
    return command.str();
}

std::string SRTMinorServoCommandLibrary::programTrack(std::string servo_id, unsigned long trajectory_id, unsigned long point_id, std::vector<double> coordinates, double start_time)
std::string SRTMinorServoCommandLibrary::programTrack(const std::string& servo_id, const unsigned long& trajectory_id, const unsigned long& point_id, const std::vector<double>& coordinates, const double start_time)
{
    std::stringstream command;
    command << "PROGRAMTRACK=" << servo_id << "," << trajectory_id << "," << point_id << ",";
@@ -77,7 +77,7 @@ std::string SRTMinorServoCommandLibrary::programTrack(std::string servo_id, unsi
    return command.str();
}

std::string SRTMinorServoCommandLibrary::offset(std::string servo_id, std::vector<double> coordinates)
std::string SRTMinorServoCommandLibrary::offset(const std::string& servo_id, const std::vector<double>& coordinates)
{
    std::stringstream command;
    command << "OFFSET=" << servo_id;
@@ -89,9 +89,10 @@ std::string SRTMinorServoCommandLibrary::offset(std::string servo_id, std::vecto
    return command.str();
}

SRTMinorServoAnswerMap SRTMinorServoCommandLibrary::parseAnswer(std::string answer)
SRTMinorServoAnswerMap SRTMinorServoCommandLibrary::parseAnswer(const std::string& original_answer)
{
    // First thing first, standardize the separators and remove the newline/carriage return characters
    std::string answer(original_answer);
    std::replace(answer.begin(), answer.end(), ':', '=');
    std::replace(answer.begin(), answer.end(), '|', ',');
    answer.erase(std::remove(answer.begin(), answer.end(), '\n'), answer.end());
Loading