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.
What does this mean? What's happening (output, errors, etc.)?
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.
I have edited the post