Create Custom Messages from ROS 2 Package
Use custom messages to extend the set of message types currently supported in ROS 2. Custom messages are messages that you define. If you are sending and receiving supported message types, you do not need to use custom messages. To see a list of supported message types, enter ros2 msg list
in the MATLAB® Command Window. For more information about supported ROS 2 messages, see Work with Basic ROS 2 Messages.
If this if your first time working with ROS 2 custom messages, see ROS Toolbox System Requirements.
ROS 2 custom messages are specified in ROS 2 package folders that contain a folder named msg
. The msg
folder contains all your custom message type definitions. For example, the example_b_msgs
package in the custom
folder, has this folder and file structure.
The package contains the custom message type Standalone.msg
. MATLAB uses these files to generate the necessary files for using the custom messages contained in the package.
In this example, you create ROS 2 custom messages in MATLAB. You must have a ROS 2 package that contains the required msg
file.
After ensuring that your custom message package is correct, you specify the path to the parent folder and call ros2genmsg
with the specified path. The following example provided three messages example_package_a,
example_package_b
, and example_package_c
that have dependencies. This example also illustrates that you can use a folder containing multiple messages and generate them all at the same time.
Open a new MATLAB session and create a custom message folder in a local folder.
folderPath = fullfile(pwd,"custom"); copyfile("example_*_msgs",folderPath);
Specify the folder path for custom message files and use ros2genmsg
to create custom messages.
ros2genmsg(folderPath)
Identifying message files in folder 'C:/Work/custom'.Done. Removing previous version of Python virtual environment.Done. Creating a Python virtual environment.Done. Adding required Python packages to virtual environment.Done. Copying include folders.Done. Copying libraries.Done. Validating message files in folder 'C:/Work/custom'.Done. [3/3] Generating MATLAB interfaces for custom message packages... Done. Running colcon build in folder 'C:/Work/custom/matlab_msg_gen/win64'. Build in progress. This may take several minutes... Build succeeded.build log
Call ros2 msg list
to verify creation of new custom messages.
ros2 msg list
action_msgs/CancelGoalRequest action_msgs/GoalInfo action_msgs/GoalStatusArray actionlib_msgs/GoalID actionlib_msgs/GoalStatus builtin_interfaces/Duration builtin_interfaces/Time composition_interfaces/ListNodesRequest composition_interfaces/ListNodesResponse diagnostic_msgs/AddDiagnosticsRequest diagnostic_msgs/AddDiagnosticsResponse example_a_msgs/DependsOnB example_b_msgs/Standalone example_interfaces/AddTwoIntsRequest example_interfaces/AddTwoIntsResponse example_interfaces/Bool example_interfaces/Byte...
You can now use the above created custom message as the standard messages. For more information on sending and receiving messages, see Exchange Data with ROS 2 Publishers and Subscribers.
Create a publisher to use example_b_msgs/Standalone
message.
node = ros2node("/node_1"); pub = ros2publisher(node,"/example_topic","example_b_msgs/Standalone");
Create a subscriber on the same topic.
sub = ros2subscriber(node,"/example_topic");
Create a message and send the message.
custom_msg = ros2message("example_b_msgs/Standalone"); custom_msg.int_property = uint32(12); custom_msg.string_property='This is ROS 2 custom message example'; send(pub,custom_msg); pause(3) % Allow a few seconds for the message to arrive
Use LatestMessage
field to know the recent message received by the subscriber.
sub.LatestMessage
ans = struct with fields:
MessageType: 'example_b_msgs/Standalone'
int_property: 12
string_property: 'This is ROS 2 custom message example'
Remove the created ROS objects.
clear node pub sub
Replacing Definitions of Built-In Messages with Custom Definitions
MATLAB provides a lot of built-in ROS 2 message types. You can replace the definitions of those message types with new definitions using the same custom message creation workflow detailed above. When you are replacing the definitions of a built-in message package, you must ensure that the custom message package folder contains new definitions (.msg
files) for all the message types in the corresponding built-in message package.
See Also
ros2genmsg
| msgList
| ros2publisher
| ros2subscriber
| ros2node