1
我有10个机器人在阶段ROS,并且想要订阅位置/base_pose_ground_truth
的主题。这意味着我必须订阅robot0_base_pose_ground_truth
,robot1_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个不同的位置对应每个机器人。我不知道如何将每个机器人的位置放在相应的内存中(例如数组或矢量)。通过将机器人索引作为参数传递给回调函数并尝试使用它将位置主题分配给数组,可能会有可能。
如果有人帮我解决这个问题,我会很感激。