How to stop robot in code

Hi,
this is a general question. Is there a way that can make a node stop within coding instead of giving ctrl+c with terminal from outside?
Jialei Li

If I understood correctly, you wanted to stop the robot, so in order to do that there’s no need to stop the node. You just need to publish a Twist message with 0 velocities in linear and angular variables.

In any case, if I didn’t solve your issue, would you mind to be a bit more precise about what you want to do?

In addition to what @ACTISA said, you can also stop the robot whenever ROS is shutting down. Have a look at this post for how to do that.

In general, to stop a robot in your code you just need to create a publisher to the applicable “velocity” topic and publish a stop message as required in your code.

Hi @ACTISA ,
thanks for your advice. But I apologize that I actually mean how to stop “node” with code :sweat_smile:(sorry about the misleading in title). For example, is there any codes to function exactly the same with ctrl+c from terminal?
Jialei Li

Hi @bayodesegun ,
thanks for that. I made a mistake in title…I actually wonder how to stop a “node” with code :sweat_smile:
Jialei Li

Hello @MeineLiebeAxt , it’s fine :smile:
In order to stop the node you could use the method signal_shutdown() from rospy library, here is an example of its ussage:

rospy.init_node("node_name")

# do some stuff

if <condition>:
    rospy.signal_shutdown('<reason_for_shutdown>')

If I’m not mistaken, I think you could also use a function that when called, it prompts the ctrl_c for you, I think you can check it out on the Lesson where they speak about OOP (I’m not 100% sure of this).

Also, remember that you can kill the node using /kill <node_name> from the terminal.
I hope I have helped you!

1 Like

Hi,
it works! Thanks a lot for the advice!
Jialei Li

Hello!
I’m glad you solved your issue!
Feel free to ask again if you have any doubt!
Have a nice day.
Ángel