Creating Custom ROS2 message types for simulink

Hello everyone,
i am currently trying to subscribe a topic with the ROS2 subscribe block. The problem is, the message type of the topic gets not detectet when i choose the topic. See pic 1.
I already tried to select message types from the list "ros2 msg list". How can i add my own message type to the list? Or: What i have to do, that the /sensor_state topic sees the correct type?
My Matlab Version is 2022a. ROS Toolbox Version is 1.5. ROS2 Version is foxy. I allready tried ros2message(msgtype), but i dont know where i have to use the freshly created message.
i would much appreciate your answers.

 Accepted Answer

For the peeps out there with similar problems:
Solution that worked for me (Matlab 2022a, Ros Toolbox version 1.5, ROS2 Foxy):
  1. Installed Ros2 on Windows (https://docs.ros.org/en/foxy/Installation/Windows-Install-Binary.html)
  2. Then created a new package (https://docs.ros.org/en/foxy/Tutorials/Custom-ROS2-Interfaces.html)
  3. Created a msg folder in the new package, as descriped here: (https://de.mathworks.com/help/ros/ref/ros2genmsg.html)
  4. pasted my massages into the new folder (https://github.com/ROBOTIS-GIT/turtlebot3_msgs/tree/master/msg)
  5. used the script:
% path to the custom packages:
msgpath = fullfile('C:\','dev','ros2_foxy','custom');
% read the msgs from the packages in the path:
ros2genmsg(msgpath)

More Answers (1)

Hello Andre,
When running "ros2 msg list", are you able to see the custom message you are trying to use? If not, you would need to follow this documentation page to generate the custom message using ros2genmsg.
After generating the custom message, you should be able to create a message in MATLAB with ros2message(msgType), where msgType is the message type of your custom message.
If you are able to create such message in MATLAB, you can then go back to the ROS2 Subscribe block, choose "Specify your own" as the "Topic source", and select the newly created custom message in the pop-up window for "Message type".
Hope this helps,
Josh

5 Comments

Hello Josh,
i tried to get the function ros2genmsg running, but i always get error messages. So far i did the following steps:
  1. I downloaded the three messages from https://github.com/ROBOTIS-GIT/turtlebot3_msgs/tree/master/msg and placed it in a custom folder. E.g: C:\Program Files\MATLAB\R2022a\toolbox\ros\custom\turtlebot3_foxy\msg.
  2. installed python 3.8 and set pyenv too ,but because i got error messages i changed to 3.9.
  3. after that i had to install visual c++ build tools 14.0+
  4. then the same error described in https://de.mathworks.com/matlabcentral/answers/1461834-why-do-i-get-the-error-could-not-find-cmake-in-your-system-when-using-rosgenmsg.
  5. after that i installed MinGW over the Matlab App Explorer: https://de.mathworks.com/matlabcentral/fileexchange/52848-matlab-support-for-mingw-w64-c-c-compiler
  6. Now, when i run the following code as administrator:
msgpath = fullfile('C:\','Program Files','MATLAB','R2022a','toolbox','ros','custom');
% C:\Program Files\MATLAB\R2022a\toolbox\ros\custom\turtlebot3_foxy\msg
ros2genmsg(msgpath)
% the folder \custom\turtlebot3_foxy\msg were created by me!
Now i get the following error:
Identifying message files in folder 'C:/Program Files/MATLAB/R2022a/toolbox/ros/custom'..Done.
Validating message files in folder 'C:/Program Files/MATLAB/R2022a/toolbox/ros/custom'..Done.
[0/1] Generating MATLAB interfaces for custom message packages... 0%Error using ros.internal.ROSProjectBuilder
Current compiler MinGW64 Compiler (C++) is not supported for ROS build. To choose a compiler, run 'mex -setup cpp'.
Error in ros.ros2.internal.ColconBuilder (line 25)
h@ros.internal.ROSProjectBuilder(varargin{:});
Error in ros2genmsg (line 143)
builder = ros.ros2.internal.ColconBuilder(genDir, pkgInfos{iPkg});
Error in turtlebot3_custom_msg (line 5)
ros2genmsg(msgpath)
Do you know which Compiler is supported by Matlab AND ROS?
And the second problem i noticed: i upgraded Matlab from 2021b to 2022a and ROS Toolbox from 1.4 to 1.5 because foxy was not supported in the prior versions. The requirements (https://de.mathworks.com/help/ros/gs/ros-system-requirements.html) of the ROS2 toolbox says that it needs python 3.7, but this python version is not supportet by Matlab 2022a.... (https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/python-compatibility.pdf)
Thanks for your help.
Hi Andre,
I think you are very close to get it work now!
Unfortunately, MinGW64 is not supported in ROS Toolbox. If you are working with Foxy, please consider to use Visual Studio 2019 instead. This page listed out all system requirements for ROS Toolbox. As for python version, I think 3.9 is the correct version.
Best,
Josh
With Visual Studio 2019 the error is gone. But now i get an other error:
Specified message package turtlebot3_foxy does not exist.
By now, i only have a folder structure:
C:\Program Files\MATLAB\R2022a\toolbox\ros\custom\turtlebot3_foxy\msg
in the folder msg are all Massage files (.msg).
I tried to install a new turtlebot3_foxy package, described in this guide. But the cmd Window cant find the command "colcon" for the command "colcon build --symlink-install --merge-install".
Is it possible to create packages via matlab? Or is there another way to implement the custom massages instead of ros2massage()?
Hi Andre,
Have you tried to walk through the example on this documentation page? If you haven't, I would highly recommand to try this out. This should clarify questions related to folder structure and proper path. The custom message need to follow the same pattern as shown in the example.
Given the one-line error message you posted, I suspect the input path to ros2genmsg might be incorrect. Please feel free to reach out to MathWorks Technical Support team if you need help walking through this.
ros2genmsg is the only way to generate custom messages for ROS 2. ros2message will only create ROS 2 message structure successfully after you use ros2genmsg to generate custom messages. There is no such function as "ros2massage" though.
Thanks,
Josh
Hello,
yes i alrady walk through the linked example. guess i have to install ROS2 Foxy with a custom created package with colcon with my desired msg files just to import them..
thanks for your help

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 30 Mar 2022

Edited:

on 20 Apr 2022

Community Treasure Hunt

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

Start Hunting!