Stopping a ROS based Bot and also stop the motors

1,160 views
Skip to first unread message

Mark Johnston

unread,
Feb 27, 2017, 2:47:36 AM2/27/17
to HomeBrew Robotics Club

Normally in dev I hit Ctrl-C in the window that ran main ros launch.    That stops things in a 'linux sort of hard way'.

I wish to stop my motors at that point too generally.

Realizing i could design some system to post on a ros topic to have all processes stop or perhaps at least the motor one I am curious if this I suspect very common concern is indeed addressed.    As a linux dude minimum keystrokes is always top of list and ctrl-C could be counted as 1.5 keys or so.

The first thought is the normal Ctrl-C signal trap in my ROS process that sits on top of the motors (that is what I call my  base_control node) then stop the motors then do some exit from that process.  This I can handle but I'm wondering if ROS got this all worked out driven from a Ctrl-C or not?

If I don't hear a nice other way I'll just fart around with the typical and hopefully most normal linux signal trap for Ctrl-C and try to exit the process after I stop motors but that is 'semi ugly IMHO' what with 'the beauty of ROS' it may have been covered.

does roslaunch send some grace signal to the nodes then nuke um some time delay later or is it just 'petal to the metal'   kill actually done by linux and no ROS trap between?


Thanks,
Mark

Chris Albertson

unread,
Feb 27, 2017, 5:16:24 AM2/27/17
to hbrob...@googlegroups.com
Does your process have a "while not rospy.is_shutdown()" loop in it?
If so could you stop the motor after that loop ends?
> --
> You received this message because you are subscribed to the Google Groups
> "HomeBrew Robotics Club" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to hbrobotics+...@googlegroups.com.
> To post to this group, send email to hbrob...@googlegroups.com.
> Visit this group at https://groups.google.com/group/hbrobotics.
> For more options, visit https://groups.google.com/d/optout.



--

Chris Albertson
Redondo Beach, California

Michael Ferguson

unread,
Feb 27, 2017, 10:42:11 AM2/27/17
to hbrob...@googlegroups.com
In many cases this is implemented through a timeout. Since command topics are being frequently sent (often 100hz or higher), both the driver that receives cmd_vel and the firmware typically implement timeouts of maybe 100-200 milliseconds, and if commands are not received in that time, the robot stops.

-Fergs

--
You received this message because you are subscribed to the Google Groups "HomeBrew Robotics Club" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hbrobotics+unsubscribe@googlegroups.com.

Steve " 'dillo" Okay

unread,
Feb 27, 2017, 10:46:56 AM2/27/17
to HomeBrew Robotics Club


On Sunday, February 26, 2017 at 11:47:36 PM UTC-8, Mark Johnston wrote:

Normally in dev I hit Ctrl-C in the window that ran main ros launch.    That stops things in a 'linux sort of hard way'.

I wish to stop my motors at that point too generally.

Realizing i could design some system to post on a ros topic to have all processes stop or perhaps at least the motor one I am curious if this I suspect very common concern is indeed addressed.    As a linux dude minimum keystrokes is always top of list and ctrl-C could be counted as 1.5 keys or so.

The first thought is the normal Ctrl-C signal trap in my ROS process that sits on top of the motors (that is what I call my  base_control node) then stop the motors then do some exit from that process.  This I can handle but I'm wondering if ROS got this all worked out driven from a Ctrl-C or not?

If I don't hear a nice other way I'll just fart around with the typical and hopefully most normal linux signal trap for Ctrl-C and try to exit the process after I stop motors but that is 'semi ugly IMHO' what with 'the beauty of ROS' it may have been covered.

If you are publishing Twist messages to your robot to get it to move forward, you can stop it by sending and empty Twist msg, like so:
Python example:
      t=Twist()
      drive_the_bot.pub(t) 

Where drive_the_bot is a ROS Publisher that publishes to the cmd_vel or cmd_vel_mux topic. 
This will stop the robot rather abruptly, but without quitting your program.

You could also apply the same code snippet in your shutdown method: 

   def shutdown():
      t = Twist()
      drive_the_bot.pub(t)
      rospy.loginfo("Okay, I think I will stop here")
 
Basically, whatever you're doing to move your robot, do that, but with the X and Y linear velocity set to 0. 

HTH,
'dillo

Mark Johnston

unread,
Feb 28, 2017, 5:44:34 AM2/28/17
to HomeBrew Robotics Club

Thanks Fergs and Steve  (two guys with real robot jobs!)  

I took these replies to mean that there was no built in ROS way to gracefully shutdown so did the typical  old-skewl trap of SIGINT which I found my nodes will get likely because I am doing Ctrl-C on roslaunch and it sends SIGiNT to the nodes it has started as perhaps child processes I should think.

Also I don't have some separate controller that could stop by itself when the stream of cmd_vel commands stop so that is not an option here.  maybe roboclaw can do that so I will have to check on that.

In my case a ROS node called base_control  does the motor control and is commanded to do stuff by my main node often in high level ways like 'drive 0.2 meters or rotate 1.2 radians and so on.
cmd_vel is also supported by this node. base_control publishes back status on it's current operational goal and does other stuff like odom.  The issue was the base_control node (or somebody) needs to stop roboclaw on a ctrl-c in the roslaunch window and that was my question if ROS had some other way except the hard SIGINT way.

Anyway, base_control talks to roboclaw using a modified serial packet driver of mine and so I now just have base_control have a sig handler that if i get SIGINT I tell roboclaw to stop. (thus:  OldSkewl)

It's easy enough but non-elegant.    problem solved, just not with some ROS specific mechanism which I was curious may exist.

I could envision something like that but we have to step outside of ros to the 'real OS' to deal with this issue.   No problemo, Done.

Thanks,
Mark

Ralph Gnauck

unread,
Feb 28, 2017, 11:38:01 AM2/28/17
to hbrob...@googlegroups.com
Mark,

The normal way this is done is by monitoring ros::ok() in your main loop.

ROS nodes automatically trap Ctl-C and will shut down cleanly by having ros::ok() return false. Also happens when launch gets the Ctl-C - it will cause ros::ok() to return false as well for all nodes in the launch file.


Most nodes do something like: (assuming a C++ implementations, there is a similar API for Python etc).


 int  main(...)
{

     while(ros::ok())
    {
             <do stuff>
             ros::spinOnce();  // all callbacks are executed here
     }

    <clean up here, could send 0 commands to a motor controller etc>

}


When the node or launch receives a Ctl-C this loop will exit and send a stop command to the motors before exiting.


Of course there are other ways like using ros::Rate object to sleep and set a timed iteration rate for this loop as well but it is possible to do stuff after the loop exits to shutdown cleanly.

Make sure none of your callbacks does anything that would cause it to block indefinitely or the spinOnce() will not return and the loop will not exit properly.

Ralph




From: Mark Johnston <mjst...@gmail.com>
To: HomeBrew Robotics Club <hbrob...@googlegroups.com>
Sent: Tuesday, February 28, 2017 2:44 AM
Subject: [HBRobotics] Re: Stopping a ROS based Bot and also stop the motors

--
You received this message because you are subscribed to the Google Groups "HomeBrew Robotics Club" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hbrobotics+...@googlegroups.com.

Chris Albertson

unread,
Feb 28, 2017, 12:00:45 PM2/28/17
to hbrob...@googlegroups.com
Is this not the same as "

while not rospy.is_shutdown()
do_normal_stuff()
do_clean_up and park motors()

So I guess I asking of "ok()' is just that same as not is_shutdown().
I figure the is_shutdown is more descriptive

Ralph Gnauck

unread,
Feb 28, 2017, 12:10:06 PM2/28/17
to hbrob...@googlegroups.com
is_shutdown() is the python api, ok() is the c++, there may be others in both languages, ill need to look it up.

But yes your python snippet is effectively the same thing.

Ralph


From: Chris Albertson <alberts...@gmail.com>
To: hbrob...@googlegroups.com
Sent: Tuesday, February 28, 2017 9:00 AM
Subject: Re: [HBRobotics] Re: Stopping a ROS based Bot and also stop the motors
> email to hbrobotics+unsub...@googlegroups.com.

> To post to this group, send email to hbrob...@googlegroups.com.
> Visit this group at https://groups.google.com/group/hbrobotics.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "HomeBrew Robotics Club" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to hbrobotics+unsub...@googlegroups.com.

> To post to this group, send email to hbrob...@googlegroups.com.
> Visit this group at https://groups.google.com/group/hbrobotics.
> For more options, visit https://groups.google.com/d/optout.



--

Chris Albertson
Redondo Beach, California

--
You received this message because you are subscribed to the Google Groups "HomeBrew Robotics Club" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hbrobotics+unsub...@googlegroups.com.

Mark Johnston

unread,
Feb 28, 2017, 2:31:00 PM2/28/17
to HomeBrew Robotics Club
Thanks Ralph,
I have the while ok going on and your answer was what I was seeking.   

If I simply drop out of the while ros::ok()  all would be fine.   I was overthinking the ctrl-c situation.

Mark


Ralph Gnauck

unread,
Feb 28, 2017, 4:29:52 PM2/28/17
to hbrob...@googlegroups.com
Just one extra thing, don't relay on any other ROS functions/other nodes still running after the main loop exists - most other nodes may have already shutdown by then, but your serial link to your motor controller etc will still work.

The formal docs for all this are here:



Ralph


From: Mark Johnston <mjst...@gmail.com>
To: HomeBrew Robotics Club <hbrob...@googlegroups.com>
Sent: Tuesday, February 28, 2017 11:30 AM

Subject: [HBRobotics] Re: Stopping a ROS based Bot and also stop the motors
--
You received this message because you are subscribed to the Google Groups "HomeBrew Robotics Club" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hbrobotics+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages