How to Run a ROS2 Node Executable Generated by Coder?

I have a simple function that publishes to a ROS2 topic and I need this publisher to run as a separate process outside of Matlab. Right now I also have a little function that subscribes to that topic to receive the messages to test if the publisher is running. I used Coder to 'build and run' the publisher per this tutorial. When I run my subscriber function, it received and plots the published data correctly confirming the process for the publisher is running.
My question is this: How do can I run the publisher executable without Matlab now that I have it built? After running codegen, I get the file structure shown in the attached image. I tried running the ros2publisherexample executable via the terminal as I have done in the past with coder generated executables by calling ros2publisherexample but I get the following output in the terminal window:
libc++abi: terminating with uncaught exception of type rclcpp::exceptions::RCLError: failed to initialize rcl init options: failed to find shared library 'rmw_fastrtps_cpp', at /System/Volumes/Data/mathworks/devel/bat/B3p2/build/3p/derived/maci64/ros2/src/ros2/rmw_implementation/rmw_implementation/src/functions.cpp:75, at /System/Volumes/Data/mathworks/devel/bat/B3p2/build/3p/derived/maci64/ros2/src/ros2/rcl/rcl/src/rcl/init_options.c:61
All of the examples I see on the Matlab documentation use 'build and run' in coder config (cfg.Hardware.BuildAction) and don't show how to run the exectuable as a standalone.
Thanks.

 Accepted Answer

Hi Michael,
The short answer is that you cannot run executable as a standalone from the terminal. You always need a ROS environment ( kind of shell script that you usually run in ROS ) to run the ROS node. This environment is created and used by MATLAB when you created an executable with the Build and Run option and Deploy To is set to Locla Host.
Here is what you can try without MATLAB Installation
• Install ROS 2 Foxy on native mac machine
• Go to ROS2 colcon workspace
• Copy src folder generated from the MATLAB.
• Do colcon_make
• Then launch the node.
Hope this helps. Let me know if you have any questions.
Regards,
Jagadeesh K

10 Comments

@Jagadeesh Konakalla This helps. I will try this today and report back. Can I ask if it matters if I install ROS 2 Galactic? The Foxy documentation suggests it is an older version and points you to Galactic.
See the top of the page here: https://docs.ros.org/en/foxy/
@Jagadeesh Konakalla This question/answer says that only Foxy is will work with Matlab, but if I am trying to get these to run independent of Matlab I don't think it should matter.
You should be able to use with any distribution. R2022a ROS Toolbox is upgraded to Foxy. Let me know if you face any issues. We can help you out.
Thanks,
Jagadeesh K.
@Jagadeesh Konakalla I am currently working through some issues getting ROS installed. I will try your suggestion above as soon as I have it installed and then will accept the answer.
Quick question: Are the steps you outline above documented anywhere on the Matlab docs? It might be obvious to people who are already familiar with ROS, but for newbies like me it isn't clear. It might be a good idea for Matlab to put a document together on how to get Matlab generated code running on an existing ROS network outside of the Matlab development environment. Just a thought :)
@Jagadeesh Konakalla I was able to try to do the 'colcon build' (which is what I assumed you meant by 'colcon_make' above) but ran into an error during the build. I am pasting the output below. Does it matter that I did the src build on macOS and I am trying the colcon build on Ubuntu?
dasl@dasl-Precision-T5610:~/sambashare/matlab_ws$ colcon build
Starting >>> ros2publisherexample
Starting >>> ros2subscriberexample
--- stderr: ros2publisherexample
/home/dasl/sambashare/matlab_ws/src/ros2publisherexample/src/main.cpp: In function void threadFunction():
/home/dasl/sambashare/matlab_ws/src/ros2publisherexample/src/main.cpp:21:33: warning: catching polymorphic type ‘class std::runtime_error’ by value [-Wcatch-value=]
21 | } catch (std::runtime_error e) {
| ^
---
Finished <<< ros2publisherexample [17.5s]
--- stderr: ros2subscriberexample
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/main.cpp: In function void threadFunction():
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/main.cpp:21:33: warning: catching polymorphic type ‘class std::runtime_error’ by value [-Wcatch-value=]
21 | } catch (std::runtime_error e) {
| ^
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/ros2subscriber.cpp: In member function ‘void coder::ros2subscriber::receive() const’:
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/ros2subscriber.cpp:118:56: warning: rclcpp::Duration::Duration(rcl_duration_value_t)is deprecated: Use Duration::from_nanoseconds instead or std::chrono_literals. For example:rclcpp::Duration::from_nanoseconds(int64_variable);rclcpp::Duration(0ns); [-Wdeprecated-declarations]
118 | tStop = tClock->now() + rclcpp::Duration(tTimeoutNsec);
| ^
In file included from /opt/ros/galactic/include/rclcpp/qos.hpp:20,
from /opt/ros/galactic/include/rclcpp/node_interfaces/node_graph_interface.hpp:31,
from /opt/ros/galactic/include/rclcpp/client.hpp:34,
from /opt/ros/galactic/include/rclcpp/callback_group.hpp:23,
from /opt/ros/galactic/include/rclcpp/any_executable.hpp:20,
from /opt/ros/galactic/include/rclcpp/memory_strategy.hpp:25,
from /opt/ros/galactic/include/rclcpp/memory_strategies.hpp:18,
from /opt/ros/galactic/include/rclcpp/executor_options.hpp:20,
from /opt/ros/galactic/include/rclcpp/executor.hpp:36,
from /opt/ros/galactic/include/rclcpp/executors/multi_threaded_executor.hpp:26,
from /opt/ros/galactic/include/rclcpp/executors.hpp:21,
from /opt/ros/galactic/include/rclcpp/rclcpp.hpp:156,
from /home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/mlros2_sub.h:9,
from /home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/ros2subscriber.h:17,
from /home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/ros2subscriber.cpp:12:
/opt/ros/galactic/include/rclcpp/duration.hpp:46:12: note: declared here
46 | explicit Duration(rcl_duration_value_t nanoseconds);
| ^~~~~~~~
In file included from /home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/ros2subscriber.h:17,
from /home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/ros2subscriber.cpp:12:
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/mlros2_sub.h: In instantiation of ‘MATLABROS2Subscriber<MsgType, StructType>::MATLABROS2Subscriber(StructType*, std::function<void()>) [with MsgType = geometry_msgs::msg::PoseStamped_<std::allocator<void> >; StructType = geometry_msgs_PoseStampedStruct_T]:
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/src/ros2subscriber.cpp:100:54: required from here
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/mlros2_sub.h:22:17: warning: MATLABROS2Subscriber<geometry_msgs::msg::PoseStamped_<std::allocator<void> >, geometry_msgs_PoseStampedStruct_T>::structPtr_will be initialized after [-Wreorder]
22 | StructType* structPtr_;
| ^~~~~~~~~~
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/mlros2_sub.h:21:31: warning: std::function<void()> MATLABROS2Subscriber<geometry_msgs::msg::PoseStamped_<std::allocator<void> >, geometry_msgs_PoseStampedStruct_T>::MATLABCallback_[-Wreorder]
21 | std::function<void(void)> MATLABCallback_;
| ^~~~~~~~~~~~~~~
/home/dasl/sambashare/matlab_ws/src/ros2subscriberexample/include/ros2subscriberexample/mlros2_sub.h:29:5: warning: when initialized here [-Wreorder]
29 | MATLABROS2Subscriber(StructType* structPtr, std::function<void(void)> callback)
| ^~~~~~~~~~~~~~~~~~~~
---
Finished <<< ros2subscriberexample [21.6s]
Summary: 2 packages finished [23.1s]
2 packages had stderr output: ros2publisherexample ros2subscriberexample
Hi Michael,
Let me try to understand your setup again.
Looks like your host machine is Mac and you would like to deploy/run the node in ROS distribution available in Ubuntu.
For this use case, you can use the Deploy To option in the coder configuration as Remote Device. See the example Generate a Standalone ROS Node from MATLAB - MATLAB & Simulink (mathworks.com). This is for ROS1 but should be applicable ROS2 also.
cfg = coder.config('exe');
cfg.Hardware = coder.hardware('Robot Operating System 2 (ROS 2)');
cfg.Hardware.BuildAction = 'Build and run';
cfg.Hardware.RemoteDeviceAddress = '172.18.250.139';
cfg.Hardware.RemoteDeviceUsername = 'user';
cfg.Hardware.RemoteDevicePassword = 'password';
cfg.Hardware.DeployTo = 'Remote Device';
cfg.Hardware.ROS2Folder = '/opt/ros/foxy';
cfg.Hardware.ROS2Workspace = '~/ros2_ws';
codegen robotROSFeedbackControlCodegen -args {} -config cfg
In the above work flow, MATLAB is running on your host machine (in your case Mac machine) and ROS node is deployed to the Ubuntu mahcine where ROS is available.
You can also login to Ubuntu machine and go to ros2 workspace and build the MATLAB generated CMAKE package.
Let me know if this helps.
Thanks,
Jagadeesh K.
This is exactly what I needed to do. I ran those commands, went over to my Linux maching, ran 'colcon build', sourced the generated ros files, and then ran the node. It all worked! You've been a huge help here. Thanks so much!
-Michael
@Jagadeesh Konakalla Sorry to bug you again about this issue. Although I am able to deploy my ROS2 node on my Linux machine, I am still getting errors during the 'colcon build' step related to the 'warning: catching polymorphic type' as shown in my previous comments. This may be completely unrelated to my original question and if so, please let me know and I can generate a different question on Matlab Answers.
I have tired building via the remote device (cfg.Hardware.DeployTo = 'Remote Device') from my Mac per your most recent comment and seen this error. I also tried building locally (cfg.Hardware.DeployTo = 'Localhost'), and then copied over the generated /src folder to my Linux machine where I then tried 'colcon build' -- got the same error. What is odd is that the node seems to run fine after I do '. install/setup.bash' on my Linux machine. I have confirmed this with a simple subscriber that reads and plots the published messages.
I'd like to avoid this error during the build if possible. I am attaching a zip of my workspace so that you can see the error (see /matlab_ws/log/build_2022-05-27_18-45-01/ros2publisherexample/stderr.log), other info from the build, as well as all the coder generated source files.
Hi Michael,
What you see is a warning and not an error. I see that build is successful and that is you are able to launch the node. For now, this issue is not a blocker for you.
We will fix this warning in future releases
Thanks,
Great. Thanks.
-Michael

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!