In this example, you go through the procedure for creating ROS custom messages in
MATLAB®. It assumes you have already gone
through the installation process shown in Install Robotics System Toolbox Add-ons. Also, you must have
a ROS package that contains the required
package.xml files. The correct file contents and folder structure are
described in Custom Message Contents. This folder structure follows the standard ROS package conventions. Therefore, if you
have any existing packages, they should match this structure.
It is recommended you start this procedure after opening a new MATLAB session to ensure that there are no lingering changes to MATLAB preferences from previous work. After ensuring that your custom message package is
correct, note the folder path location. Then, call
rosgenmsg with the specified path and follow the steps output in the command
window. The following example has three messages,
C, that have dependencies on each other. This example also illustrates
that you can use a folder containing multiple messages and generate them all at the same
To set up custom messages in MATLAB:
Open MATLAB in a new session
Place your custom message folder in a location and note the folder path. In this
example, a location of example packages is provided and copied to
userFolder location. Make sure that the
directory exists prior to running this code.
examplePackages = fullfile(fileparts(which('rosgenmsg')), 'examples', 'packages'); userFolder = 'c:\MATLAB\custom_msgs'; copyfile(examplePackages, userFolder)
Specify the folder path of the custom messages.
folderpath = userFolder;
(Optional) If you have an existing catkin workspace (
can specify the path to its
src folder instead. However, this workspace
might contain a large number of packages and message generation will be run for all of
folderpath = fullfile('catkin_ws','src');
Specify the folder path for custom message files and call
to create custom messages for MATLAB.
Checking subfolder "A" for custom messages. Checking subfolder "B" for custom messages. Checking subfolder "C" for custom messages. Building custom message files for the following packages: A B C Generating MATLAB classes for message packages in C:\MATLAB\custom_msgs\matlab_gen\jar Loading file A-1.0.jar. Generating MATLAB code for A/DependsOnB message type. Generating MATLAB code for B/Standalone message type. Loading file B-1.0.jar. Loading file C-1.0.jar. Generating MATLAB code for C/DependsOnB message type. To use the custom messages, follow these steps: 1. Edit javaclasspath.txt, add the following file locations as new lines, and save the file: C:\MATLAB\custom_msgs\matlab_gen\jar\A-1.0.jar C:\MATLAB\custom_msgs\matlab_gen\jar\B-1.0.jar C:\MATLAB\custom_msgs\matlab_gen\jar\C-1.0.jar 2. Add the custom message folder to the MATLAB path by executing: addpath('C:\MATLAB\custom_msgs\matlab_gen\msggen') savepath 3. Restart MATLAB and verify that you can use the custom messages. Type "rosmsg list" and ensure that the output contains the generated custom message types.
If you see the following warning
Objects of *** class exist - not clearing java
Try either calling
rosgenmsg at the beginning of your MATLAB session or make sure that no Java objects are created with any startup
Then, follow steps 1–3 from the output of
javaclasspath.txt link to open the file in the Editor. Copy
and paste the different jar file locations as new lines in the file. If this file does not
exist, you will be prompted to create it. Click Yes and then copy and
paste the file locations into the file.
javaclasspath.txt looks like this after adding lines. Other paths
may also already exist in this file.
Add the given files to the MATLAB path by running
the command window. You can either highlight the commands shown and press
F9 or copy and paste it into the MATLAB Command Window.
Restart MATLAB for the path changes to be applied. You can then use the custom messages like
any other ROS messages supported in Robotics System
Toolbox™. Verify these changes by either calling
rosmsg list and
search for your message types, or use
rosmessage to create a new
custommsg = rosmessage('B/Standalone')
custommsg = ROS Standalone message with properties: MessageType: 'B/Standalone' IntProperty: 0 StringPropert: '' Use showdetails to show the contents of the message
This final verification shows that you have performed the custom message generation process correctly. You can now send and receive these messages over a ROS network using MATLAB and Simulink®. The new custom messages can be used like normal message types. You should see them create objects specific to their message type and be displayed in your workspace.
custommsg = rosmessage('B/Standalone'); custommsg2 = rosmessage('A/DependsOnB');
Custom messages can also be used with the ROS Simulink blocks.