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

Move a certain distance (geometry_msg:Twist)

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

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 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

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

dornhege gravatar image

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

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 -0500 )edit

I appreciate your help very much. Thank you

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

Question Tools


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

Seen: 2,489 times

Last updated: Mar 16 '15