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

How to spawn multiple nodes in same C++ process

asked 2011-08-25 03:29:17 -0500

nitschej gravatar image

updated 2014-01-28 17:10:17 -0500

ngrennan gravatar image

Hello there,

I want to spawn multiple nodes within the same C++ process. To this end I used boost/thread. All seems to work, except that always one of the nodes is not showing up in 'rosnode list'. This is the code that runs the threads and creates the nodes:

static boost::mutex mutex;
static boost::once_flag cv_thread_flag = BOOST_ONCE_INIT;

void worker0(int argc, char* argv[]) {
    mutex.lock();
    ros::init(argc, argv, "optical_flow_visualization0");
    optical_flow_analyser::visualize::of_visualize vis("asdf", 0);
    mutex.unlock();
    ros::spin();
}

void worker1(int argc, char* argv[]) {
    mutex.lock();
    ros::init(argc, argv, "optical_flow_visualization1");
    optical_flow_analyser::visualize::of_visualize vis("asdf", 1);
    mutex.unlock();
    ros::spin();
}

int main(int argc, char* argv[]) {
    boost::call_once(cv_thread_flag, &cv::startWindowThread);
    boost::thread worker_thread_0(worker0, argc, argv);
    boost::thread worker_thread_1(worker1, argc, argv);
    worker_thread_0.join();
    worker_thread_1.join();
    return 0;
}

Sample output of 'rosnode list':

% rosnode list
/gazebo
/optical_flow_visualization1
(...)

The internal workings of the node consist of subscribing to some image_transport topics and displaying them with cv::imshow() in the callback function. As stated above this does indeed work as it should, but the fact that only one of the nodes is found by rosnode tells me that I might be doing something wrong here.

So I wonder: Is this a sane way of spawning multiple nodes? If not: how should I do it instead?

The background of this is that I want to process multiple camera streams from Gazebo, and I'd like to have it so, that I can flexibly spawn processing nodes, depending on how many camera streams there are.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
5

answered 2011-08-25 04:21:54 -0500

joq gravatar image

I am surprised this seemed to work at all. I would not expect a single process to provide more than one node.

I recommend implementing each thread as a nodelet. They are well-supported and the nodelet manager can dynamically load additional threads for the process when needed.

Many camera drivers provide device nodelets, allowing you to display shared copies of the images published. The image pipeline also provides nodelets for similar reasons.

edit flag offensive delete link more
0

answered 2011-08-29 04:39:21 -0500

embeddedheaven gravatar image

I wrote an article "How to get Start with Nodelet" with example source code, you can take a look.

Get Start with Nodelet

edit flag offensive delete link more
1

answered 2011-08-25 03:53:22 -0500

dornhege gravatar image

updated 2011-08-25 03:57:02 -0500

I think you don't need multiple nodes for your application. The main thing ros::init does is set the name and register the remappings.

You just need multiple topic descriptions and that works without a problem (you can remap topic names independent of the node name if you want).

edit flag offensive delete link more

Comments

I have now looked a bit into remapping. If I get it right it will help me to load the same node with different configurations, but only from the command line or a launch file. Combining this with nodelets will let me do the same from within a C++ process. So I will probably go for a combination.
nitschej gravatar image nitschej  ( 2011-08-25 21:49:09 -0500 )edit

Question Tools

Stats

Asked: 2011-08-25 03:29:17 -0500

Seen: 4,271 times

Last updated: Aug 29 '11