Main Content

ROS Custom Message Support

Custom Message Overview

Custom messages are user-defined messages that you can be used to extend the set of message types currently supported inROS Toolbox。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, callrosmsg listin the MATLAB®Command Window.

Custom message creation requires ROS packages, which are detailed in the ROS Wiki atPackages。After ensuring that you have valid ROS packages for custom messages, callrosgenmsgwith the file path to the location containing the custom message package folders to generate the necessary MATLAB code to use custom messages. For an example on how to generate a ROS custom message in MATLAB, seeCreate Custom Messages from ROS Package

If this is your first time using ROS custom messages, checkROS System Requirements

Custom Message Contents

ROS custom messages are specified in ROS package folders that containsmsg,srv, andactiondirectories.

Note

At any time, there should only be one custom messages folder on the MATLAB path. This folder can contain multiple packages. It is recommended that you keep them all in one unique folder.

Themsgfolder contains all your custom message type definitions. You should also add all custom service type definitions to thesrvfolder and add all custom action type definitions to theactionfolder. For example, the packagecustom_robot_msgshas this folder and file structure.

The package contains one custom message type inRobotTopic.msg, one custom service type inRobotService.srv, and one custom action type inRobotAction.action。MATLAB uses these files to generate the necessary files for using the custom messages contained in the package. For more information on creatingmsgandsrvfiles, seeCreating a ROS msg and srvandDefining Custom Messageson the ROS Wiki. The syntax of these files is described on the pages specific tomsgandsrv。For more information about ROS actions, seeROS Actions Overview

Note

  • You must have write access to the custom messages folder.

  • At any time, there should only be one custom messages folder on the MATLAB path. This folder can contain multiple packages. It is recommended that you keep them all in one unique folder.

Property Naming From Message Fields

When ROS message definitions are converted to MATLAB, the field names are converted to properties for the message object. Object properties always begin with a capital letter and do not contain underscores. The field names are modified to fit this naming convention. The first letter and the first letter after underscores are capitalized with underscores removed. For example, thesensor_msgs/Imagemessage has these fields in ROS:

header height width encoding is_bigendian step data

The converted MATLAB properties are:

Header Height Width Encoding IsBigendian Step Data

This is also reflected when using ROS messages in Simulink®。ROS message buses use the same properties names as MATLAB.

Custom Message Creation Workflow

Once you have your custom message structure set up as described in the previous section, you can create the code needed to use these custom messages. First, you callrosgenmsgwith your known path to the custom message files to create MATLAB code.

Therosgenmsgfunction takes your custom message files (。msg,。srv, and。action) and converts each message type to working MATLAB code. Therosgenmsgfunction looks for。msgfiles in themsgfolder, for。srvfiles in thesrvfolder, and for。actionfiles in theactionfolder. This code is a group of classes that define the message properties when you create new custom messages. The function then creates the required MATLAB M-files for the different message classes.

After therosgenmsgfunction creates these files, you must add the class files to the MATLAB path. These steps are given as prompts in the MATLAB Command Window.

  1. Add location of class files to MATLAB path: Useaddpathto add new locations of files with the .m extension to the MATLAB path and usesavepathto save these changes.

  2. Refresh all message class definitions, which requires clearing the workspace:

    clearclassesrehashtoolboxcache

  3. Verify messages are available: Userosmsglistor therosmessagefunction to check that the new custom messages are available.

For an example of this procedure, seeCreate Custom Messages from ROS Package。这个示例使用自定义消息文件示例create custom messages in MATLAB.

You need to complete this procedure only once for a specific set of custom messages. After that, you can use the new custom messages like any other ROS message in MATLAB and take advantage of the full ROS functionality thatROS Toolboxprovides. Repeat this generation procedure when you would like to update or create new message types.

You must maintain the MATLAB path that contain the files directories. Make sure that the MATLAB path has only one folder at a time that contains custom message artifacts.

Code Generation with Custom messages

Custom message, service, and action types can be used with ROS Simulink blocks for generating C++ code for a standalone ROS node. The generated code (。tgzarchive) will include Simulink definitions for the custom messages, but it will not include the ROS custom message packages. When the generated code is built in the destination Linux System, it expects the custom message packages to be available in the catkin workspace or on theROS_PACKAGE_PATH。Ensure that you either install or copy the custom message package to your Linux system before building the generated code.

See Also

|

Related Topics