Shutdownhook function in sphero project

Hi,
I could not figure out that why you have used global variables ctrl_c, twist_object and pub of shutdownhook() function in publisher node because they are not used, are they? Can you please elaborate why are they defined inside this function? Thanks in advance.

def shutdownhook():
    # works better than the rospy.is_shut_down()
    global ctrl_c
    global twist_object
    global pub
    
    rospy.loginfo("shutdown time!")
    
    ctrl_c = True
    cmd_publisher_object.move_robot(direction="stop")

Hi @enderayhan,

When you use the global keyword within a function, it means you intend to modify the referenced global variable within the function. Normally, within a function you can read a global variable but can’t modify it.

You can see in this case that ctrl_c was modified:

ctrl_c = True

It seems that the other two were not used - it happens :slight_smile: - sometimes you define a variable and end up not using it. Please just ignore them; we’ll remove them in the next update.

2 Likes