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

Catch ctrl-C and Set value

asked 2020-12-02 01:46:59 -0500

yanaing gravatar image

I am developing a robot and I wrote some programs for that robot. In the robot's motor driving node, I subscribed the topic from /cmd_vel topic. What the current issue is that when the motor is running and if I terminate the motor driving node with ctrl-C, the motor is still running.

Is there any way to put the motor values with 0 before terminating the program? I am using ROS melodic with python as the following.

#!/usr/bin/env python
import ........

class main():
    def __init__(self):
         rospy.init_node("robot", anonymous=False)
         rospy.Subscriber(..........)
         self.pub=rospy.Publisher(..........)

         rospy.Timer(rospy.Duration(0.2), self.main_callback)
         while not rospy.is_shutdown():
              rospy.sleep(0.2)
              print("-----------------")

     def main_callback(self, event):
           ......................
           motor_1 = 50
           ....end.....

if __name__="__main__":
          try: main()
          except rospy.ROSInterruptException: pass

As with this example, some way to set the value motor_1=0 before terminating the node.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2020-12-02 03:06:34 -0500

mgruhler gravatar image

updated 2020-12-02 03:07:23 -0500

You could register a hook with rospy.on_shutdown() as described here, that sets the motor values to zero. Note that, as described, messages aren't guaranteed to be published.

If you need more fancy handling, have a look at the Advanced Example on the same page.

Also, note that if your node crashes for some reason, this will not be called.

edit flag offensive delete link more

Comments

Thanks solved my problem. Normal class destructor __del__() is called but it seems that rospy is already shutdown by that point so you can't publish anything. Meanwhile your method rospy.on_shutdown(self.my_function) calls my_function() before rospy is shutdown and it's able to publish

Combinacijus gravatar image Combinacijus  ( 2023-01-20 14:40:55 -0500 )edit
0

answered 2020-12-03 03:45:47 -0500

yanaing gravatar image

Thanks... It work.! I finalize my motor node as follwoing.

#!/usr/bin/env python
import ........

class main():
    def __init__(self):
         rospy.init_node("robot", anonymous=False)
         rospy.Subscriber(..........)
         self.pub=rospy.Publisher(..........)

         rospy.Timer(rospy.Duration(0.2), self.main_callback)
         while not rospy.is_shutdown():
              rospy.sleep(0.2)
              print("-----------------")

         rospy.on_shutdown(self.shutdown)

     def shutdown(self):
         motor_1=0

     def main_callback(self, event):
           ......................
           motor_1  =  50
           ....end.....

if __name__="__main__":
          try: main()
          except rospy.ROSInterruptException: pass
edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-12-02 01:46:59 -0500

Seen: 1,980 times

Last updated: Dec 03 '20