总体上分为一下步骤:
1 用 ros::CallbackQueue定义需要的回调队列;
ros::CallbackQueue state_callback_queue;
2 将回调队列初始化进subscriber中。例如下面所示:
ros::SubscribeOptions ops=ros::SubscribeOptions::create<bhand_controller::State>( "/bhand_node/state", 1, state_callback, ros::VoidPtr(), &state_callback_queue ) ros::Subscriber listen_state=bhand_state.subscribe(ops);
3 定义ros::AsyncSpinner,并初始化回调队列,第一个参数代表几个进程,但是目前并不知道有什么特殊用处。
ros::AsyncSpinner state_spinner(1,&state_callback_queue);
4 启动调用。
state_spinner.start();
备注:
(1) 注意subscriber的缓存队列的大小,会影响回调结果,这与ROS的回调机制相关,ROS在发生回调是会将所有队列中的消息分别送往回调函数中,因此如果队列未被及时更新,可能出现多次重复响应,因此建议设置为1,或者慎重考虑下运行频率相关。
(2)可通过在回调函数中加标志位的方式实时停止回调。下面在回调函数中使得init_state_flag置1,break跳出while或for的循环.
while(ros::ok()) { if(init_state_flag == 1) { state_spinner.stop(); break; } state_spinner.start(); }
(3)不要把初始化的定义语句,诸如ros::Subscriber listen_state=bhand_state.subscribe(ops);放在没有ros.sleep的while(ros::ok()),否则会不能触发消息回调,而且编译不会提示任何问题。
5 该部分完整的实例如下;
ros::CallbackQueue state_callback_queue; void state_callback(const bhand_controller::State::ConstPtr& msg) { ....... init_state_flag=1;//也可在需要位置置1 } int main(int argc,char **argv) { ros::init(argc,argv,"example_srv_bhand"); //monitor the state of the hand. ros::NodeHandle bhand_state; ros::SubscribeOptions ops=ros::SubscribeOptions::create<bhand_controller::State>( "/bhand_node/state", 1, state_callback, ros::VoidPtr(), &state_callback_queue ); ros::Subscriber listen_state=bhand_state.subscribe(ops); ros::AsyncSpinner state_spinner(1,&state_callback_queue); while(ros::ok()) { if(init_state_flag == 1) { state_spinner.stop(); break; } state_spinner.start(); } }