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

g_main_loop_new

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

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);   
ros::spin();
t.join();

But just main_loop seams to be runing.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

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

mwenger gravatar image

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

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);
    ros::spinOnce();

  // sleep for a while to avoid maxing out CPU
   std::this_thread::sleep_for(std::chrono::milliseconds(20));
}

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
0

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

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

Stats

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

Seen: 888 times

Last updated: Oct 30 '19