Ask Your Question
1

ROS commands from c++ applications

asked 2018-05-02 10:00:58 -0600

touhid gravatar image

updated 2018-05-02 13:26:13 -0600

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.

edit retag flag offensive close merge delete

Comments

it doesn't work

What does this mean? What's happening (output, errors, etc.)?

jayess gravatar imagejayess ( 2018-05-02 11:14:47 -0600 )edit
1

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.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-05-02 13:08:16 -0600 )edit

I have edited the post

touhid gravatar imagetouhid ( 2018-05-02 13:26:41 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-05-02 14:07:42 -0600

mohsen gravatar image

updated 2018-05-02 14:20:56 -0600

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);
}

}
edit flag offensive delete link more

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.

touhid gravatar imagetouhid ( 2018-05-02 19:01:25 -0600 )edit

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.

touhid gravatar imagetouhid ( 2018-05-02 19:07:04 -0600 )edit

The child process runs in the background. You can verify that using rosnode list.

mohsen gravatar imagemohsen ( 2018-05-02 23:26:25 -0600 )edit

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.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-05-03 06:33:59 -0600 )edit

Agreed....

mohsen gravatar imagemohsen ( 2018-05-03 06:44:40 -0600 )edit
1

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.

touhid gravatar imagetouhid ( 2018-05-08 05:30:19 -0600 )edit

Glad you got it working!

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-05-08 06:32:23 -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-05-02 10:00:58 -0600

Seen: 286 times

Last updated: May 02 '18