Main Content

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 msg, srv, and 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. Then, call rosgenmsg with the specified path and follow the steps output in the command window. The following example has three messages, A, B, and 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 src folder 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 rosgenmsg function to create custom messages for MATLAB.

    rosgenmsg('c:\MATLAB\custom_msgs')
  • Then, follow steps from the output of rosgenmsg.

  1. Add the given files to the MATLAB path by running addpath and savepath in the command window.

    addpath('C:\MATLAB\custom_msgs\packages\matlab_msg_gen_ros1\msggen')
    savepath
  2. Refresh all message class definitions, which requires clearing the workspace:

    clear classes
    rehash toolboxcache

  3. You can then use the custom messages like any other ROS messages supported in ROS Toolbox. Verify these changes by either calling rosmsg list and search for your message types, or use rosmessage to 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');

Custommsg and custommsg2 as viewed in MATLAB command window and workspace.

Custom messages can also be used with the ROS Simulink blocks.

A Blank Message block in a Simulink model configured to create a blank message of custom message type.

See Also

|

Related Topics