Ask Your Question

Move a certain distance (geometry_msg:Twist)

asked 2015-03-16 04:33:04 -0600

dylankc gravatar image

Currently this is the code that I have, and I am able to get the robot moving by editing the linear x or angular z values (speed).

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

int main(int argc, char **argv)
ros::init(argc, argv, "move_pub");
ros::NodeHandle n;
ros::Publisher movement_pub = n.advertise<geometry_msgs::Twist>("mobile_base/commands/velocity",1); 
ros::Rate rate(10);

while (ros::ok()) 
    geometry_msgs::Twist move;
    //velocity controls
    move.linear.x = -0.1; //speed value m/s
    move.angular.z = 0;

return 0;

Is there a time control in ROS? Whereby I can do something like while time <= 5s, publish linear x velocity of 0.2m/s. Thus, covering 1 meter. I am new to ROS and it is my first week into it. Im working on the TurtleBot 2.

edit retag flag offensive close merge delete



Also see robot pose slightly off when publishing to /cmd_vel for some discussion on the feasibility of what you are trying to achieve.

gvdhoorn gravatar image gvdhoorn  ( 2015-03-16 06:44:31 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-03-16 05:40:25 -0600

dornhege gravatar image

updated 2015-03-16 05:40:40 -0600

There is ros::Time and ros::Duration, which can be used in an intuitive way. ros::Time::now() gives you the current time. You probably want something like:

ros::Time start = ros::Time::now()
while(ros::Time::now() - start < ros::Duration(5.0))
edit flag offensive delete link more


Thank you dornhege for teaching me about the ros::Time & ros::Duration function. However, i've taken a look at "" and realized that my method is prone to inaccuracy!

I'll try publishing to /odom.

dylankc gravatar image dylankc  ( 2015-03-16 20:20:06 -0600 )edit

I appreciate your help very much. Thank you

dylankc gravatar image dylankc  ( 2015-03-16 20:20:33 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2015-03-16 04:33:04 -0600

Seen: 1,951 times

Last updated: Mar 16 '15