Robot not stopping at shutdown as specified in rospy.on_shutdown


I implemented a similar shutdown function based on this in a python script without a class, as expected in exercise 3.1. (unit 3 topics). I mean it is not expected to shutdown the robot but I was curious about how to do it. However, when pressing ctr+C the robot will not stop:

#! /usr/bin/env python

import rospy                               # Import the Python library for ROS

from geometry_msgs.msg import Twist             # Import the Int32 message from the std_msgs package

def clean_shutdown(self):

    ''' Stop robot when shutting down '''

    rospy.loginfo("System is shutting down. Stopping robot...")

    vel.linear.x = 0

    vel.angular.z = 0


rospy.init_node('vel_publisher')         # Initiate a Node named 'topic_publisher'

pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)    

                                           # Create a Publisher object, that will publish on the /counter topic

                                           # messages of type Int32

rate = rospy.Rate(1)                       # Set a publish rate of 2 Hz

vel = Twist()                          # Create a var of type Int32

vel.linear.x = 0.5                             # Initialize 'count' variable

vel.angular.z = 0.5

while not rospy.is_shutdown():             # Create a loop that will go until someone stops the program execution

  pub.publish(vel)                       # Publish the message within the 'count' variable 


Am I missing something?

Hi @cpumargarcia,

Yes, you’re missing this line in your clean_shutdown function; you’re not publishing the stop message.


By the way, welcome to the Community!

Hi @bayodesegun, I appreciate the quick reply. I also had to remove the self argument from clean_shutdown because the function is not used within a class.

I so happy I found this course, thanks for the warm welcome!

1 Like