Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

[SOLVED] After tampering about and trying many many things, I found that my other more 'elegant solutions' some of which include arrays of msg files, msg files inside msg files to imitate objects seemed to be quite buggy, so here is a less elegant but full proof sol.

SOL: Basically unpack the object into its attributes in the server node, send it to the client node via service request & multiple vectors. Each vector corresponds to an attribute of the object from the original server nodes class and the indices of the vectors all correspond to the i'th object. Eg. all index 1's of all vectors together make up object1. The client node then builds the objects back and adds them to a proprietary vector of objects to the client node.

Note: Could also be done with pointers to minimize the movement of entire arrays. Note: Always remember to modify your CMakeLists! - got me a few times hah

###### CLIENT.CPP #######

#include "sensor_fusion/new_srv.h"
#include "ros/ros.h"
#include "sensor_fusion/new_srv.h"
#include <cstdlib>

class tempClass{
    public:
    int intMember;
    double dubMember;

    // constructor
    tempClass(int temp, double temp2) :intMember(temp), dubMember(temp2){}        
};

class container{
    public:
    std::vector<tempClass> vec;
};

bool add(sensor_fusion::new_srv::Request &req, sensor_fusion::new_srv::Response &res){

    container cont;

    tempClass obj(6, 6.3);
    tempClass obj2(7, 7.3);
    tempClass obj3(8, 8.3);

    cont.vec.push_back(obj);
    cont.vec.push_back(obj2);
    cont.vec.push_back(obj3);

    // index refers to specific objectNum in the original vector
    for(int i = 0; i < cont.vec.size(); i++){
        res.obj_id.push_back(cont.vec[i].intMember);
        res.obj_dx.push_back(cont.vec[i].dubMember);

        //res.obj_lane.push_back(cont.vec[i].intMember);
        // res.obj_vx.push_back(cont.vec[i].intMember);
        // res.obj_dy.push_back(cont.vec[i].intMember);
        // res.obj_ax.push_back(cont.vec[i].intMember);
        // res.obj_path.push_back(cont.vec[i].intMember);
        // res.obj_vy.push_back(cont.vec[i].intMember);
        // res.obj_timestamp.push_back(cont.vec[i].intMember);
        // res.object_track_num.push_back(cont.vec[i].intMember);
    }
    return true;
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "server");
    ros::NodeHandle n;

    ros::ServiceServer service = n.advertiseService("service_topic", add);

    ROS_INFO("Waiting for Sr.Client.");
    ros::spin();

    return 0;
}




###### SERVER.CPP ######



#include "ros/ros.h"
#include "sensor_fusion/new_srv.h"
#include "sensor_fusion/dummy_msg.h"

    class tempClass{
        public:
        int intMember;
        double dubMember;

        tempClass(int temp, double temp2) :intMember(temp), dubMember(temp2){}        
    };

    class container{
        public:
        std::vector<tempClass> vec;
    };

    bool add(sensor_fusion::new_srv::Request &req, sensor_fusion::new_srv::Response &res){

        container cont;

        tempClass obj(6, 6.3);
        tempClass obj2(7, 7.3);
        tempClass obj3(8, 8.3);

        cont.vec.push_back(obj);
        cont.vec.push_back(obj2);
        cont.vec.push_back(obj3);

        // index refers to specific objectNum in the original vector
        for(int i = 0; i < cont.vec.size(); i++){
            res.obj_id.push_back(cont.vec[i].intMember);
            res.obj_dx.push_back(cont.vec[i].dubMember);

            //res.obj_lane.push_back(cont.vec[i].intMember);
            // res.obj_vx.push_back(cont.vec[i].intMember);
            // res.obj_dy.push_back(cont.vec[i].intMember);
            // res.obj_ax.push_back(cont.vec[i].intMember);
            // res.obj_path.push_back(cont.vec[i].intMember);
            // res.obj_vy.push_back(cont.vec[i].intMember);
            // res.obj_timestamp.push_back(cont.vec[i].intMember);
            // res.object_track_num.push_back(cont.vec[i].intMember);
        }
        return true;
    }

    int main(int argc, char **argv) {
        ros::init(argc, argv, "server");
        ros::NodeHandle n;

        ros::ServiceServer service = n.advertiseService("service_topic", add);

        ROS_INFO("Waiting for Sr.Client.");
        ros::spin();

        return 0;
    }




###### SRV FILE ######



---
uint8[] obj_id
float64[] obj_dx

// uint8[] obj_lane
// float64[] obj_vx
// float64[] obj_dy
// float64[] obj_ax
// bool obj_path
// float64[] obj_vy
// float64[] obj_timestamp
// uint8[] obj_count