ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

ROSLAUNCH ON MATLAB

asked 2017-02-18 04:03:00 -0600

AnandGeorge gravatar image

updated 2017-02-18 04:08:03 -0600

Hello all,

I want to start a node using roslaunch on MATLAB. The node is defined on the remote system and I've initialized global node on MATLAB with ROS master on the remote system. How to accomplish this?

Thanks in advance!

edit retag flag offensive close merge delete

Comments

I'm not sure, but I don't think the robotics toolbox (I assume you're using that) has any support for roslaunch.

If you can start your node / script in Matlab from the command line, then you could use a simple bash script that roslaunch can start.

gvdhoorn gravatar image gvdhoorn  ( 2017-02-18 06:29:36 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2018-05-22 13:49:11 -0600

wonwon0 gravatar image

updated 2018-05-24 11:03:21 -0600

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 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.

edit flag offensive delete link more

Comments

Thanks for the solution. It worked for me. But I have problem with terminating/stopping the launched file. Can you please help me by explaining more, how to stop/terminate the launched node from MATLAB itself? I also tried " system("pkill roslaunch") ", but it kills all the nodes launched.

manuelmelvin gravatar image manuelmelvin  ( 2019-07-07 23:01:58 -0600 )edit

The same method works for rosrun as well, and I assume it works for pretty much all ROS commands. I used it for dynamic_reconfigure and it works like wonder. Thanks!

Yew Hon Ng gravatar image Yew Hon Ng  ( 2019-09-10 03:45:21 -0600 )edit

Sorry for the delay... @manuelmelvin If you have a matlab script and want to follow the solution i gave to terminate the process (roslaunch xyz.launch), you have ot call you script from a function to ensure the variable "finishup" is deleted (thus triggering the execution of the "mycleanupFun()" function).

To do so, you create a function that does:

fucntion my_script_launcher()
my_scipt_that_launches_the_launch_file;
end

If inside your script you call the finishup = onCleanup(@() myCleanupFun(cmdout)); line, once the script finishes it will stop only the xyz.launch execution, not all nodes.

wonwon0 gravatar image wonwon0  ( 2019-11-01 08:07:33 -0600 )edit

Hello, does your matlab installed on ubuntu or windows? I try your method in windows, but it seems doesnot work. Look forward to your reply. Thank you!!

man469 gravatar image man469  ( 2022-11-06 01:47:19 -0600 )edit

hello, this answer was made for linux systems. I suspect it can be adapted for windows but i am not sure.

wonwon0 gravatar image wonwon0  ( 2022-11-07 12:07:11 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-02-18 04:03:00 -0600

Seen: 2,435 times

Last updated: May 24 '18