2014-12-31 28 views
1

我有10个机器人在阶段ROS,并且想要订阅位置/base_pose_ground_truth的主题。这意味着我必须订阅robot0_base_pose_ground_truthrobot1_base_pose_ground_truth,....ROS:订阅相同的回调和相同的主题(与不同的索引)分配给对应变量

来首先想到的解决办法是:

for(int i=0; i<RobotNumber; i++) {     
    char str[30] = "/robot_"; 
    char index[4]; 
    sprintf(index, "%d", i); 
    strcat(str, index); 
    strcat(str, "/base_pose_ground_truth"); 
    GoalPos_sub[i] = Node.subscribe(str, 100, GetPos_callback);   
} 

和回调可能是:

void GetPos_callback(const nav_msgs::Odometry::ConstPtr& msg) { 
    Goalpose[0] = msg->pose.pose.position.x; 
    Goalpose[1] = msg->pose.pose.position.y; 
} 

但这不会给我10个不同的位置对应每个机器人。我不知道如何将每个机器人的位置放在相应的内存中(例如数组或矢量)。通过将机器人索引作为参数传递给回调函数并尝试使用它将位置主题分配给数组,可能会有可能。

如果有人帮我解决这个问题,我会很感激。

回答

0

您已经是非常接近的解决方案:你确实可以添加机器人指数作为参数传递给回调,改变Goalpose是一个数组:

void GetPos_callback(const nav_msgs::Odometry::ConstPtr& msg, int index) { 
    Goalpose[index][0] = msg->pose.pose.position.x; 
    Goalpose[index][1] = msg->pose.pose.position.y; 
} 

你必须改变的订阅命令位做这个工作。要将索引传递给回调,必须使用boost::bind

GoalPos_sub[i] = Node.subscribe(str, 100, boost::bind(GetPos_callback, _1, i)); 
相关问题