2014-12-31 31 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)); 
相關問題