Ask Your Question
0

How to pass a vector of objects to another node via service request? [SOLVED]

asked 2020-03-20 16:07:13 -0500

Maahir Gupta gravatar image

updated 2020-03-25 09:22:46 -0500

I need to use a service request to get a vector of objects stored in a container node in my package.

My first instinct was simply trying to provide a vector of objects in the srv request field, then instead of a vector, providing 1 object, then an object in a msg file included in the srv file, none of which worked. I tried a vector of primitive types alone which worked but not what I needed. So my understanding is that ROS only allows primitive types to be communicated through msg or srv files? Can someone confirm this?

edit retag flag offensive close merge delete

Comments

Could I ask you to post your last edit as an answer and then accept your own answer?

That would provide a much clearer indication this question was answered, and would make it much easier to find the answer (it's currently burried in the question itself).

gvdhoorn gravatar image gvdhoorn  ( 2020-03-24 03:39:06 -0500 )edit

And please:

I modified the post to show the solution

do not do this.

Please revert your question text to show the original question. Then post the solution as an actual answer.

gvdhoorn gravatar image gvdhoorn  ( 2020-03-24 03:40:30 -0500 )edit

Done now, sorry about that.

Maahir Gupta gravatar image Maahir Gupta  ( 2020-03-25 09:23:42 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-03-25 09:18:25 -0500

Maahir Gupta gravatar image

[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 ...
(more)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-03-20 16:06:08 -0500

Seen: 23 times

Last updated: Mar 25