ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I FOUND A WAY!
posting the answer here, behold, it may look ugly!
The answer lies with the command system(command) in matlab. With that, you can run any command that you'd run in a terminal.
The first thing one would do is to simply run:
system('roslaunch xyz.launch');
But sadly it's not that simple!
Since matlab exports it's own little world of LD_LIBRARY_PATH, you have the over write the LD_LIBRARY_PATH when you want to execute a command that uses the LD_LIBRARY_PATH that is normally set in your environment outside of matlab. If you want to know what it is, run "echo $LD_LIBRARY_PATH" in a command line.
for me the output was: "/home/XXXX/catkin_ws/devel/lib:/opt/ros/kinetic/lib:/opt/ros/kinetic/lib/x86_64-linux-gnu". I will refer to it with the name LD_path in the examples bellow.
So to launch the launch file you have to run:
system(['export LD_LIBRARY_PATH="LD_path";' 'roslaunch xyz.launch']);
The thing is that it will stall the execution of any script you put it in since the command returns only after you terminate it...
To be able to continue the script we need to use the "&" character at the end of the command like the following example.
system(['export LD_LIBRARY_PATH="LD_path";' 'roslaunch xyz.launch &']);
Now the program runs in background, but you wont be able to terminate it upon then end of a script or function.
To be able to do so, we add the "echo $!" string at the end of the command. This returns the status and PID of the process. We then can kill the process by issuing the kill command. Example bellow:
[status,cmdout] = system(['export LD_LIBRARY_PATH="LD_path";' 'roslaunch xyz.launch & echo $!']);
system('kill', cmdout);
If you want to make sure the process is killed after a "Ctrl-C" input, the onCleanup() matlab function is pretty useful. It creates an object that executes the function passed in parameter when the object is deleted.
finishup = onCleanup(@() myCleanupFun(cmdout));
"myCleanupFun(cmdout))" being a function I wrote as follows:
function myCleanupFun(cmdout))
system('kill',cmdout)
end
In the case of a script file, since the variables are not deleted upon it'S termination, you have to call it inside a function. All the variables being local to the function workspace, they are now deleted after the function is stopped thus deleting the "finishup" variable and killing the process.
2 | No.2 Revision |
I FOUND A WAY!
posting the answer here, behold, it may look ugly!
The answer lies with the command system(command) in matlab. With that, you can run any command that you'd run in a terminal.
The first thing one would do is to simply run:
system('roslaunch xyz.launch');
But sadly it's not that simple!
Since matlab exports it's own little world of LD_LIBRARY_PATH, you have the over write the LD_LIBRARY_PATH when you want to execute a command that uses the LD_LIBRARY_PATH that is normally set in your environment outside of matlab. If you want to know what it is, run "echo $LD_LIBRARY_PATH" in a command line.
for me the output was: "/home/XXXX/catkin_ws/devel/lib:/opt/ros/kinetic/lib:/opt/ros/kinetic/lib/x86_64-linux-gnu". I will refer to it with the name LD_path in the examples bellow.
So to launch the launch file you have to run:
system(['export LD_LIBRARY_PATH="LD_path";' 'roslaunch xyz.launch']);
The thing is that it will stall the execution of any script you put it in since the command returns only after you terminate it...
To be able to continue the script we need to use the "&" character at the end of the command like the following example.
system(['export LD_LIBRARY_PATH="LD_path";' 'roslaunch xyz.launch &']);
Now the program runs in background, but you wont be able to terminate it upon then end of a script or function.
To be able to do so, we add the "echo $!" string at the end of the command. This returns the status and PID of the process. We then can kill the process by issuing the kill command. Example bellow:
[status,cmdout] = system(['export LD_LIBRARY_PATH="LD_path";' 'roslaunch xyz.launch & echo $!']);
system('kill', cmdout);
If you want to make sure the process is killed after a "Ctrl-C" input, the onCleanup() matlab function is pretty useful. It creates an object that executes the function passed in parameter when the object is deleted.
finishup = onCleanup(@() myCleanupFun(cmdout));
"myCleanupFun(cmdout))" being a function I wrote as follows:
function myCleanupFun(cmdout))
system('kill',cmdout)
end
In the case of a script file, since the variables are not deleted upon it'S its termination, you have to call it inside a function. All the variables being local to the function workspace, they are now deleted after the function is stopped thus deleting the "finishup" variable and killing the process.