Ask Your Question
1

ROSLAUNCH ON MATLAB

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

AnandGeorge gravatar image

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

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 imagegvdhoorn ( 2017-02-18 06:29:36 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
4

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

wonwon0 gravatar image

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

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 imagemanuelmelvin ( 2019-07-07 23:01:58 -0500 )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 imageYew Hon Ng ( 2019-09-10 03:45:21 -0500 )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: 2017-02-18 04:03:00 -0500

Seen: 915 times

Last updated: May 24 '18