ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

Shutdown a node from another node in ROS CPP

asked 2018-06-14 00:44:42 -0600

ravijoshi gravatar image

I am looking for a way to shut down a node from another node.

from subprocess import call
call(["rosnode", "kill", "my_node"], shell=True)

The code above works fine with ROS Python. I am looking for the same in ROS CPP.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2018-06-14 01:00:30 -0600

mgruhler gravatar image

updated 2018-06-14 01:54:22 -0600

Instead of doing a System call, I'd rather use a ROS Service which then Triggers a shutdown of the node in question. This is way more portable than hard-coding a node Name that can easily be changed using a roslaunch tag.


EDIT

Create a ServiceServer in the node you want to shut down that triggers ros::shutdown and stops the node. Ideally, you wouldn't do the shutdown in the Service "callback" itself, but rather set some flag that shuts down the node after some short time. This will allow the Service to return a Response.

Thus, your calling node requires a ServiceClient with whatever Service type you see fit. I'd suggest std_srvs/Trigger. But std_srvs/Empty would do as well.

edit flag offensive delete link more

Comments

I like this suggestion. Just want to know, what do you mean by use a ROS Service? Do you want me to create a ROS service with empty request data? Can you please elaborate a bit more?

ravijoshi gravatar image ravijoshi  ( 2018-06-14 01:42:00 -0600 )edit

see edit above

mgruhler gravatar image mgruhler  ( 2018-06-14 01:54:26 -0600 )edit

Interesting. set some flag that shuts down the node after some short time This seems tricky. How much is the short time?

ravijoshi gravatar image ravijoshi  ( 2018-06-14 01:58:03 -0600 )edit

Do you have a while in the main?. Make the exit condition ros::ok() && shutdown_flag. Then just flip shutdown_flag in the callback.

srinskit gravatar image srinskit  ( 2018-06-14 02:49:48 -0600 )edit

Since my service server node is in Python and service client is in C++. I created a sample code in Python. Here is the link.

ravijoshi gravatar image ravijoshi  ( 2018-06-14 08:18:39 -0600 )edit

So solved?

srinskit gravatar image srinskit  ( 2018-06-14 10:12:46 -0600 )edit

I think so. I haven't checked the code yet since I am out of the town. thanks again for the help.

ravijoshi gravatar image ravijoshi  ( 2018-06-14 10:29:37 -0600 )edit
1

answered 2018-06-14 01:06:59 -0600

srinskit gravatar image

updated 2018-06-14 01:21:24 -0600

You can use system.

system(". PATH_TO_setup.sh ; rosnode kill sub");

or if you want to use bash

system("bash -c 'source PATH_TO_setup.bash ; rosnode kill sub'");

replace PATH_TO_setup with your ROS source file like /opt/ros/melodic/setup.bash

edit flag offensive delete link more

Comments

Need to test it. I used system but didn't provide PATH_TO_setup.sh. It should work now!

ravijoshi gravatar image ravijoshi  ( 2018-06-14 01:43:25 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-06-14 00:44:42 -0600

Seen: 2,384 times

Last updated: Jun 14 '18