ROS commands from c++ applications
Hi
I am fairly new to ROS. I am trying to run the ROS commands like roscore, rosrun and so on from external c++ applications. I am able to run roscore
command but when I try to run multiple commands like, roscore
then rosrun
then other commands
it doesn't work. So, is it possible to achieve this?
For example:
working code:
char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
execvp(argsUwsim[0], argsUwsim);
Not working code:
char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
char* argsUwsimSecond[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/rosrun uwsim uwsim", NULL};
execvp(argsUwsimSecond[0], argsUwsimSecond);
"not working code" only starts the roscore
and then do nothing. I have tried to use c++ fork() method to create child process for running multiple commands but it still doesn't work or I am not running it right.
here is the code with fork():
char* argsMaster[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/rosrun uwsim uwsim", NULL};
pid_t pid = fork();
if(pid == 0){
execvp(argsUwsim[0], argsUwsim);
} else if(pid > 0){
execvp(argsMaster[0], argsMaster);
}
here, the roscore
starts and stays there, nothing happens for the rosrun
command. If I stop the roscore
with ctrl + c
,whole program stops.
Asked by touhid on 2018-05-02 10:00:58 UTC
Answers
This works for me. (I used a node of my own instead of uwsim
)
There are two caveats:
- When you kill the second node, the roscore
receives a shutdown request (it doesn't actually shutdown). This is expected, since the second node is a child of the core.
- For some reason I can't shutdown the roscore
with ctrl+C.
#include <unistd.h>
#include <ostream>
int main()
{
char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
char* argsUwsimSecond[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/rosrun uwsim uwsim", NULL};
pid_t pid = fork();
if (pid < 0)
{
std::cerr << "fork failed";
return 1;
}
if (pid > 0) // The parent
{
execvp(argsUwsim[0], argsUwsim);
}
else // The child
{
execvp(argsUwsimSecond[0], argsUwsimSecond);
}
}
Asked by mohsen on 2018-05-02 14:07:42 UTC
Comments
Thanks for your help. But, I am actually trying to accomplish the running of multiple ROS commands. When, I run your code, it only started the roscore
and stayed there and when I stopped roscore
whole program stopped, nothing happened for the child's command.
Asked by touhid on 2018-05-02 19:01:25 UTC
Another thing happened, if child's command i.e. rosrun
executes first, then it shows error about not finding the roscore
running That is it, nothing happens after that. But if I give ctrl + c
command several times, rosrun
starts to run but stops after few seconds.
Asked by touhid on 2018-05-02 19:07:04 UTC
The child process runs in the background. You can verify that using rosnode list
.
Asked by mohsen on 2018-05-02 23:26:25 UTC
Would it not be far easier to do this with a bash script and/or launch file instead? In our lab we use this combination for starting up very complex ROS systems with a single command.
Asked by PeteBlackerThe3rd on 2018-05-03 06:33:59 UTC
Agreed....
Asked by mohsen on 2018-05-03 06:44:40 UTC
Thanks PeteBlackerThe3rd and Mohsen. I was finally able to achieve the things with the help of bash script. I followed the link - Running roscore and Launching ROS nodes as Background Process.
Asked by touhid on 2018-05-08 05:30:19 UTC
Glad you got it working!
Asked by PeteBlackerThe3rd on 2018-05-08 06:32:23 UTC
Comments
What does this mean? What's happening (output, errors, etc.)?
Asked by jayess on 2018-05-02 11:14:47 UTC
Can you please show all your code including the fork statement. It's probable that execution of roscore is blocking any further commands. You can test this by manually killing the roscore from a different terminal or the system monitor and seeing if the next command suddenly gets executed.
Asked by PeteBlackerThe3rd on 2018-05-02 13:08:16 UTC
I have edited the post
Asked by touhid on 2018-05-02 13:26:41 UTC