Ask Your Question
0

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 2020-07-27 10:14:48 -0600

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):
            rospy.loginfo("turn")
            turn_cmd.angular.z = radians(angle) 
            new_angle = (angle + new_angle) % 360
            self.cmd_vel.publish(turn_cmd)
            r.sleep()
         # code to pause  
        for x in range(0,100):
            rospy.loginfo("stop")
            turn_cmd.angular.z = radians(0)
            self.cmd_vel.publish(turn_cmd)

            r.sleep()

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

1 Answer

Sort by ยป oldest newest most voted
0

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

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

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

Seen: 87 times

Last updated: Aug 14 '20