Create Custom Messages from ROS Package
In this example, you go through the procedure for creating ROS custom messages in
MATLAB®. You must have a ROS package that contains the required
action 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.
To ensure you have the proper third-party software, see ROS Toolbox System Requirements.
After ensuring that your custom message package is correct, note the folder path location.
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 time.
To set up custom messages in MATLAB:
Open MATLAB in a new session
Place your custom messages in a location and note the folder path. We recommend you put all your custom message definitions in a single packages folder.
folderpath = 'c:\MATLAB\custom_msgs\packages';
(Optional) If you have an existing catkin workspace (
catkin_ws), you can specify the path to its
srcfolder instead. However, this workspace might contain a large number of packages and message generation will be run for all of them.
folderpath = fullfile('catkin_ws','src');
Specify the folder path that contains the custom message packages and call the
rosgenmsgfunction to create custom messages for MATLAB.
Then, follow steps from the output of
Add the given files to the MATLAB path by running
savepathin the command window.
Refresh all message class definitions, which requires clearing the workspace:
You can then use the custom messages like any other ROS messages supported in ROS Toolbox. Verify these changes by either calling
rosmsg listand search for your message types, or use
rosmessageto create a new message.
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.