ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hi,
I also had the same doubts. After you asked the question, I decided to find out.
Here is some working code that should make things very clear.
If someone can assist me in making this a tutorial, it will be great.
#include <ros/ros.h>
#include <ros/callback_queue.h>
#include <ros/callback_queue_interface.h>
#include <std_msgs/Empty.h>
void callback_1(const std_msgs::EmptyConstPtr& _msg)
{
ROS_INFO("Called callback_1\n");
}
void callback_2(const std_msgs::EmptyConstPtr& _msg)
{
ROS_INFO("Called callback_2\n");
}
int main(int argn, char* args[])
{
ros::init(argn, args, "callback_q_subscriber");
ros::NodeHandle nh_1;
ros::NodeHandle nh_2;
ros::CallbackQueue queue_1, queue_2;
nh_1.setCallbackQueue(&queue_1);
nh_2.setCallbackQueue(&queue_2);
ros::Subscriber s_1 = nh_1.subscribe("/topic_1", 1, callback_1);
ros::Subscriber s_2 = nh_2.subscribe("/topic_2", 1, callback_2);
pthread_t id_1, id_2;
int i = 20;
while(i--)
{
queue_1.callOne(ros::WallDuration(1.0)); // can also be callAvailable()
}
i = 20;
while(i--)
{
queue_2.callOne(ros::WallDuration(1.0)); // can also be callAvailable()
}
ROS_INFO("Hurray!");
return 0;
}
STEP 1:
Create a catkin package called callback_queue_test or what have you.
catkin_create_pkg callback_queue_test roscpp std_msgs
STEP 2:
a. Open favorite editor and paste the code inside it. Save it as simple.cpp.
b. Edit CMakeLists.txt to add an executable called simple_node. Add the target_link_libraries() for it.
STEP 3:
Open 4 terminals. Run the following in each in the same order.
a. roscore
b. rostopic pub -r 10 /topic_1 std_msgs/Empty
c. rostopic pub -r 10 /topic_2 std_msgs/Empty
d. rosrun callback_queue_test simple_node
That's it.
Have a nice day!