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

Duration calculation and control in ROS - furthermore why does this script keep running even after pressing Ctrl-C?

asked 2020-07-27 09:59:08 -0600

lxg gravatar image

updated 2022-08-07 09:05:08 -0600

lucasw gravatar image

Hi everyone, I have written some code to make the turtlebot turn around. The code is working. What I want to know is how fast the turtlebot is running and how I can control it. Forexample, how can I ensure that the turtlebot turns 5 degrees in one minute? Last part of the question. After pressing Ctrl-C, the turtlebot stops but the script keeps running. Why? and how can I stop that?

this post does not really help.

went through this post .Does that mean that the while loop below runs 5 times a second regardless of the values I put in the for loops? Or does it mean ROS tries its best to make sure that the loop runs 5 times a second to the best of my machine's ability? Thank you very much.

# 5 HZ
    angle = 5
    r = rospy.Rate(5);
    while not rospy.is_shutdown():
        # code to turn
        for x in range(0,100):
            turn_cmd.angular.z = radians(angle) 
            new_angle = (angle + new_angle) % 360
         # code to pause  
        for x in range(0,100):
            turn_cmd.angular.z = radians(0)


def shutdown(self):
    # stop turtlebot
    rospy.loginfo("Stop turning")
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-08-14 05:13:57 -0600

lxg gravatar image

The rospy.Rate convenience class makes a best effort to maintain the loop running at the specified frequency by considering the execution time of the loop since the last successful r.sleep(). This means, in my case: and assuming I had one r.sleep() call, as long as the code execution time within the loop does not exceed 1/5 seconds, rospy.Rate will make sure the loop runs at 5Hz. The script's continued execution has nothing to do with it. But for those who are curious, it was because when pressing Ctrl-C: KeyboardInterrupt will be handled differently than in normal Python scripts when using rospy. I had to manually shutdown the script. For details check this answer

edit flag offensive delete link more

Question Tools



Asked: 2020-07-27 09:59:08 -0600

Seen: 200 times

Last updated: Aug 14 '20