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


asked 2016-06-07 16:04:04 -0500

I have an object that uses glib (g_main_loop_new) for its callbacks that i want to use to build a ROS node. How does the two loops spin and g_main_loop_run run at the same time?.

until now I have try this:

GMainLoop* main_loop = NULL;
main_loop = g_main_loop_new (NULL, FALSE);
thread t(g_main_loop_run, main_loop);   

But just main_loop seams to be runing.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-10-01 13:44:59 -0500

mwenger gravatar image

updated 2019-10-30 10:58:55 -0500

I realize this was asked a while back, but in case anyone is still struggling with a similar issue, something like the following is working for me...

glib_mainloop = g_main_loop_new (NULL, false);
glib_context = g_main_context_default();

while (ros::ok())
    g_main_context_iteration(glib_context, false);

  // sleep for a while to avoid maxing out CPU

g_main_loop_unref (glib_mainloop);

The idea here is that for each loop of the while statement, the glib main loop is iterated once, followed by a single interation of the ROS loop.

edit flag offensive delete link more

answered 2016-06-08 05:27:43 -0500

gvdhoorn gravatar image

Use ros::spinonce() in your GMainLoop.

See roscpp/Overview/Callbacks and Spinning.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-06-07 16:04:04 -0500

Seen: 770 times

Last updated: Oct 30 '19