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.