ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

NodeHandler in multiple threads

asked 2016-05-24 17:12:37 -0600

asdfgf gravatar image

updated 2016-05-25 03:20:20 -0600

gvdhoorn gravatar image

In my case, I'm having multiple threads to subscribe to different topics. I'm not able to receive the data from any. I'm assuming its because I'm using the same node handler for all threads. But I did read and learn that each node can have only one handler, can I get some clarification on this ?

I am using boost threads. A code snippet explaining would be of great help as well.


Edit: Thank you. I had followed this link. Below is my code snippet.

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
 ROS_INFO("I heard: [%s]", msg->data.c_str());
}

void thread1(ros::NodeHandle &n)
{

    ros::Subscriber sub1 = n.subscribe("msg1", 1000, chatterCallback);
}

void thread2(ros::NodeHandle &n)
{

    ros::Subscriber sub2 = n.subscribe("msg2", 1000, chatterCallback);
}

int main(int argc, char **argv)
{

 ros::init(argc, argv, "example");

 ros::NodeHandle n;

 boost::thread get_speedData_thread(&thread1, n);

 boost::thread get_encoderData_thread(&thread2, n);

 ros::MultiThreadedSpinner spinner(2);
 spinner.spin();


 get_speedData_thread.join();   
 get_encoderData_thread.join();

 return 0;
}

I'm publishing msg1 and msg2 from other nodes. And I'm using Multithreaded spinner. My callback function is just a display function. Can anyone tell me where is the error. Message is not getting displayed.

Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-05-25 00:42:07 -0600

kmhallen gravatar image

updated 2016-05-25 13:04:05 -0600

ROS can handle the threads for you.
http://wiki.ros.org/roscpp/Overview/C...

When a subscriber goes out of scope, the subscription is canceled. See the fix below. This will service the callbacks with two threads.

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
 ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
 ros::init(argc, argv, "example");

 ros::NodeHandle n;
 ros::Subscriber sub1 = n.subscribe("msg1", 1000, chatterCallback);
 ros::Subscriber sub2 = n.subscribe("msg2", 1000, chatterCallback);

 ros::MultiThreadedSpinner spinner(2);
 spinner.spin();

 return 0;
}
edit flag offensive delete link more

Comments

I tried this. I have updated my question.

asdfgf gravatar image asdfgf  ( 2016-05-25 11:15:22 -0600 )edit

Thank you. It worked. But the threads are running very slowly even though my ros::Rate is set to 0.001. Can I know how can I make it fast enough to publish and subscribe in real time ?

It is taking about two minutes to subscribe on one topic.

Pardon me, I'm a newbie in this.

asdfgf gravatar image asdfgf  ( 2016-05-26 15:50:49 -0600 )edit

If you have a different question, please start a new question.

kmhallen gravatar image kmhallen  ( 2016-05-30 12:30:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-24 17:12:37 -0600

Seen: 1,452 times

Last updated: May 25 '16