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

Revision history [back]

The way your code is now, the nose is initializing itself, creating a publishers object, sending a single Twist message on the topic then it's stays in an infinite loop inside spin until you kill the node.

The reason your not receiving this Twist message is that there is not enough time between creating the publisher and sending the message on it for any listener node to register with this new publisher. If you add a one second sleep after creating the publisher it should solve this problem.

However even then you're only publishing a single message which is not a great way on controlling a robot. Ideally you want to setup a while loop set to run at a specific frequency using a ros::Rate object probably around 100 hz. Your movement functions would then update the current velocity but let the loop actually publish the messages.

This way the robot is getting regular updates for its speed.

Hope this helps.

The way your code is now, the nose is initializing itself, creating a publishers object, sending a single Twist message on the topic then it's stays in an infinite loop inside spin until you kill the node.

The reason your not receiving this Twist message is that there is not enough time between creating the publisher and sending the message on it for any listener node to register with this new publisher. If you add a one second sleep after creating the publisher it should solve this problem.

However even then you're only publishing a single message which is not a great way on controlling a robot. Ideally you want to setup a while loop set to run at a specific frequency using a ros::Rate object probably around 100 hz. Your movement functions would then update the current velocity but let the loop actually publish the messages.

This way the robot is getting regular updates for its speed.

Hope this helps.

Edit:

The question is what's triggering the calls to moveForward, stop, etc. The while loop is similar to the one you've already got, except you want to use spinOnce instead of spin and add the Duration object.

The movement functions would then change the velocity of a Twist object which is a part of your object, not a local variation to a function. These movement functions wouldn't publish the message, they just update it.

Then inside the main loop you can measure the current angle of the robot and set the appropriate rotation speed for example or check for key presses from the user.

If you're not familiar with it already, I highly recommend reading up on event based programming. This is the approach used by ROS for handling tasks, this means you'll never have a long loop in a function only a single main loop and short functions which a triggered from within this loop.