Best way to shutdown ROS when used with MATLAB Compiler

4 visualizzazioni (ultimi 30 giorni)

I have a basic question regarding how to use the robotics.ros library in an application that is compiled using MATLAB Compiler.

The overall structure of my application looks like this:

rosinit(...)            % Connect to an external ROS master.
pub = rospublisher(...) % Declare publisher to be used in while loop.
while 1                 % Run this loop until user press Ctrl+C
  ...
  send(pub,msg)         % Publish data in while loop.
end
rosshutdown             % NEVER REACHED

As I have tried to illustrate in the above code, I'm entering an infinite loop where data is simply published until the user closes the application using Ctrl+C. This means that the rosshutdown command is never reached resulting in the node staying alive.

My question is: What is the best way to shutdown ROS in this scenario?

If the node is not shutdown properly, I receive the following error message:

 A node called /some_node has already registered with the ROS master 
 http://localhost:11311. Try creating a node with a different name.
 robotics:ros:node:NodeNameExists

I have tried to register an onCleanup function, but that does not seem to be supported by the MATLAB Compiler.

Risposte (1)

Cam Salzberger
Cam Salzberger il 22 Mar 2019
Hello Andreas,
Instead of an infinite loop, an alternative design pattern you could try is to run the publishing within a timer callback. This would push the computation and publishing to the background, and allow the rest of the program to continue running. Then you can simply put in a pause statement so that the main program continues to idle until you hit Enter. Something like:
function myMainFunction
rosinit(...)
pub = rospublisher(...);
t = timer;
t.Period = 0.1; % Pick appropriate publish rate
t.TimerFcn = @myTimerCallback;
start(t)
pause
stop(t)
delete(t)
rosshutdown
end
function myTimerCallback(~, ~)
...
send(pub, msg)
end
-Cam

Categorie

Scopri di più su Services and Actions in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by