Send Twist message on Ctrl.+C
I'm working with ROS Melodic and Gazebo 9.9.0 on an Ubuntu 18.04.2 LTS.
I want to send a "stop" message to the robot when user kills a node.
To handle Ctrl.+C event, I use this:
/**
* Override the default ros sigint handler to stop the robot when user press
* Ctrl.+C.
*/
void sigintHandler(int sig)
{
// All the default sigint handler does is call shutdown()
ros::shutdown();
}
But this function doesn't allow to pass more parameters (I need to pass topic publisher, ros::Publisher cmd_vel_pub, to send the "stop" command).
I have declared this publisher on main function:
int main(int argc, char **argv)
{
ros::init(argc, argv, "astar_controller");
ros::NodeHandle n;
// Override the default ros sigint handler.
// This must be set after the first NodeHandle is created.
std::signal(SIGINT, sigintHandler);
/**
* Topic where we are going to publish speed commands.
*/
ros::Publisher cmd_vel_pub = n.advertise<geometry_msgs::Twist>(cmdTopic, 1000);
// More code...
}
What is the best option to get access to cmd_vel_pub
?
I've thought to declared as global, but I don't think is a good option.
Not an answer, but: are you trying to send an "all-zeros"
Twist
message to a mobile base driver?Does that driver have a built-in command time-out?
The idea is to send an "all-zeros" Twist message but, I don't have and I don't know what is a built-in command time-out.
A proper mobile base driver should have an internal timer running that automatically resets its outputs to "all-zeros" (or whatever makes sense for the particular robot) whenever it doesn't receive a new command (ie: a
Twist
) within a certain amount of time.Having such a timeout is a simple precautionary measure that prevents "runaway robots", as the driver will command the robot to a stand-still when it doesn't receive a new command in time.
Thanks. How can I do that?
Add a timer and in the callback check that
current_time - time_of_last_cmd <= timeout
, then override the "current command" to whatever would be appropriate if that is not true (ie: a timeout has occured).In the
cmd_vel
subscription callback, updatetime_of_last_cmd
to the time the cmd was received.