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

How to structure code inside a node

asked 2016-04-11 13:29:32 -0500

Vendra gravatar image

updated 2016-04-11 14:48:48 -0500

Hi, I'm learning ROS and I was wondering how should I write the code inside my nodes.

Let's take this example: The node should control the movement of a robot (publishing a velocity command) but at the same time check received messages from a sensor.

I also need to set the velocity gradually I can easily achieve this using a loop. But this avoid my node from calling the ros::Spin() and so the callback updating my global variables (or class members) is never called, right?

So I think I am supposed to make the code inside an infinite loop which calls ros::spinOnce() and write the code to control the robot in such a way that it doesn't block the loop?

I'm not sure I have been very clear, but I hope someone can point me in the right direction, thanks.

EDIT = I thought about something that could be smart in my case (mainly for readibility reasons) The switch/case. (I need to perform multiple action and I can't execute the next until i finished the previous or the sensor detect something. See:

        case 1:
            if (object detected) //global variable updated by the sensor callback
        case 2:
            //do something else, for example turn around and change path
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-04-11 14:24:11 -0500

I think I get what you are asking, but if you could edit your question to be more clear that would be nice. It sounds like you want a node that checks a sensor, increases the robot's velocity, and then publishes that velocity. I would use 2 separate functions for this. You need a callback function to read the sensor data, and a second function to increment the velocity and publish it. Then in your main function have something like


That's a really basic idea of how to do this. More complex systems require other things like using multiple threads with synchronous or asynchronous spinners. Check out the callbacks page on the wiki.

edit flag offensive delete link more



The only thing I would add to this is a ros::Rate object to the loop, to make sure that each iteration runs in the correct amount of time. Otherwise it will spin as fast as possible, running at an undefined rate and using 100% CPU.

ahendrix gravatar image ahendrix  ( 2016-04-11 14:26:35 -0500 )edit

Okay, basically that is how I shaped my code. The thing is: when I call the inc_vel() function and I'm running the code inside it, I am not able to check the sensor, right? That is my problem. I'll edit the question and try to be more clear anyway

Vendra gravatar image Vendra  ( 2016-04-11 14:32:37 -0500 )edit

I think you could call the callback function from inside the inc_vel function, but I am not positive. What sensor is it you are checking, encoders?

Icehawk101 gravatar image Icehawk101  ( 2016-04-11 14:50:17 -0500 )edit

thanks @ahendrix for the tip, I thought about that multiple times but never really found a reason to put it unless strictly neccessary. This was helpful

Vendra gravatar image Vendra  ( 2016-04-11 14:50:51 -0500 )edit

@Icehawk101 I'm checking an infrared line sensor so I can tolerate little delay between sensor readings. What I do care about is my code periodically checks that sensor.

Vendra gravatar image Vendra  ( 2016-04-11 14:52:32 -0500 )edit

Question Tools

1 follower


Asked: 2016-04-11 13:29:32 -0500

Seen: 225 times

Last updated: Apr 11 '16