Stopping go1 with publisher

Hello guys, I made a node to let de go1 walk in a cricle, however after I kill the node with ctrl-c the robot keeps walking circles. Before I run my node I use roslaunch go1_bringup bringup.launch so my node can publish messages into the cmd_vel topic. this is my code:

#!/usr/bin/env python3 
import rospy
from geometry_msgs.msg import Twist

def clean_shutdown():
    #Stop robot when shutting down
    msg.linear.x = 0.0
    msg.angular.z = 0.0
    pub.publish(msg)

    print(pub)
    print ("Go1 shutting down")

if __name__== "__main__":
    rospy.init_node("walk_circle")
    rospy.loginfo("walk_circle node has started.")

    pub = rospy.Publisher("cmd_vel", Twist, queue_size=10)
    rate = rospy.Rate(2)
    msg = Twist()

    while not rospy.is_shutdown():
        msg.linear.x = 0.3
        msg.angular.z = 1.5
        pub.publish(msg)
        rate.sleep()

    rospy.loginfo("while loop exit")
    rospy.signal_shutdown("Shutdown requested.")
    clean_shutdown()

The clean_shutdown function does actually run when I press ctrl+c, but the updated node doesn’t seem to be being published. What I’m trying to do is make the go1 stop when killing the “walk_circle” node and in the future preferably with a command ofcourse.

Hi @Kenneth-vd-H,

Please try putting the clean shutdown, before exiting the ROS node.

1 Like

Thanks for the reply

I put the clean_shutdown function call between the rospy.loginfo and rospy.signal_shutdown, but that doesn’t seem to work either.

@Kenneth-vd-H Please find the modified code below:

#!/usr/bin/env python3 
import rospy
from geometry_msgs.msg import Twist
msg = Twist() # Making it global to be accessible

def clean_shutdown():
    msg.linear.x = 0.0
    msg.angular.z = 0.0
    pub.publish(msg)

    print(pub)
    print ("Go1 shutting down")

if __name__== "__main__":
    rospy.init_node("walk_circle")
    rospy.on_shutdown(clean_shutdown) # Registering callback which will be called when a shutdown event is received. 
    rospy.loginfo("walk_circle node has started.")

    pub = rospy.Publisher("cmd_vel", Twist, queue_size=10)
    rate = rospy.Rate(2)

    while not rospy.is_shutdown():
        msg.linear.x = 0.3
        msg.angular.z = 1.5
        pub.publish(msg)
        rate.sleep()

    rospy.loginfo("while loop exit")
    rospy.signal_shutdown("Shutdown requested.")
    clean_shutdown()
2 Likes

Yup, that’s it thanks