Move a certain distance (geometry_msg:Twist)

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;
movement_pub.publish(move);

ros::spinOnce();
rate.sleep();
}
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 close merge delete

3

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

Sort by » oldest newest most voted 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))
...
more

Thank you dornhege for teaching me about the ros::Time & ros::Duration function. However, i've taken a look at "http://answers.ros.org/question/204973/robot-pose-slightly-off-when-publishing-to-cmd_vel/" and realized that my method is prone to inaccuracy!

I'll try publishing to /odom.

I appreciate your help very much. Thank you