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

How do I run commands for a specific amount of time?

asked 2013-09-19 22:28:56 -0500

Barbas gravatar image

updated 2013-12-12 20:09:28 -0500

tfoote gravatar image

Hello people I'm just starting out in ros and what I want to do is a simple movement for a simulated robot.

I'm controlling the robot through a differential drive, so I want to have the robot move forward for a while then change direction etc.

I'm using a subscriber to get the data from the encoders and a publisher to send values to the drive.

I'm currently using a while(ros::ok()){...spinOnce()} loop that makes the callback calls for the subscriber.That means that if I call my move_forward() function here it will just keep getting called until I stop the program.

My question is: how do I run my move_forward(), and turn() functions for specific amounts of time, while still getting the data from my encoders at every tick?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-20 02:06:45 -0500

SL Remy gravatar image

There are many possible ways this could be achieved. Since you haven't specified what type of simulated robot you're using, I will assume that you're using Stage.

It sounds as if you don't want to use a loop, you just want to perform move_forward() and turn() one or more times... so there should be no reason to include that code in the while loop. An if (ros::ok()) might be in order though, just in case.

With stageros if you issue a twist message, the simulator will execute that command, and then timeout if no new communication is received. Otherwise, the robot will keep moving. For example, if the timeout is 2 seconds long, and no new twist messages are received in less than 2 seconds after the last message was received, and that last message was move with Vx=V an all other velocities == 0, then the robot would have moved 2*V meters forward (with a little error, of course).

In each of the movement methods you can include rostime references the API is here... sleepUntil seems useful. I presume that you've also checked the wiki before asking.

Finally, you haven't said what you're doing with the encoder values.. if you're not using them to determine when to stop moving (since you seem to want to use time), then they can be read/collected in a different node.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-09-19 22:28:56 -0500

Seen: 1,335 times

Last updated: Sep 20 '13