Main Content

Add Custom ROS Message Definitions to MATLAB

A set of standard messages are included in the default ROS message catalog of ROS toolbox. However, you can create your own ROS custom messages and use them in MATLAB® and Simulink® with ROS networks to transmit information. For this, rosgenmsg function is used. For more information on this, refer to ROS Custom Message Support and rosgenmsg.

The kortex_driver ROS package from Kinova Robotics contains several custom ROS messages and services. In order to control the Gen 3 robot using ROS, these message definitions need to be added to the MATLAB ROS message catalog. One option to do this is to use rosgenmsg. As mentioned in the ROS System Requirements, generating custom messages for ROS, you must build the ROS packages. This requires you to have Python, CMake, and a C++ compiler for your platform.

To make the process of adding custom ROS message definitions to MATLAB easy, the definitions for Kinova Gen 3 robot are included in the support package and the hardware setup process consists of a step to register these pre-generated message definitions with the MATLAB. This workflow is ideal for the use-cases where the custom messages and services from kortex_driver package are not modified.

Note

Generating custom ROS message definitions using rosgenmsg on Windows OS has limitation on maximum number of custom messages. Hence, only limited message and service definitions are included in the support package. These definitions are sufficient to perform the hardware setup successfully and to run all the examples included in the support package (for details, see Default Set of Messages for Windows).

If you are planning to use any custom message which is not part of the default messages for Windows, then refer following instructions to generate custom message definitions for the addition ROS messages.

The custom ROS message definitions have to be regenerated in these cases:

  • If the ROS messages from Kinova Robotics are modified and are used to communicate with and control the robot Or

  • A new ROS package is being created, which is dependent on the ROS packages supplied by Kinova Robotics, to communicate with and control the robot. Or

  • A new message definition for windows needs to be added to the existing set of definitions supplied with the support package.

Generate Custom ROS Message Definitions

To generate Custom ROS message definitions:

  1. Navigate to the custom messages folder by executing the following command in the MATLAB command window.

    fullfile(codertarget.robotmanipulator.internal.getSpPkgRootDir,'resources','ROSMsgs')
  2. Based on the operating system platform, there should be a folder with a name starting with custommsg in the resources folder. Remove custommsg folder from the MATLAB path and save the current path by executing savepath command in the MATLAB command window.

  3. Restart the MATLAB.

  4. Verify that output of the MATLAB command rosmsg list does not contain any ROS messages from the kortex_driver group.

  5. Refer the ROS Custom Message Support to know more about the requirement to generate custom ROS message definitions using rosgenmsg. Also refer to ROS System Requirements to know more about the prerequisite additional software.

  6. As mentioned in the ROS Custom Message Support, rosgenmsg expects custom message files (.msg files) present under the folder msg and custom service files (.srv files) under the folder srv. However the default folder structure of the ROS package kortex_driver contains several sub folders inside the msg and srv folder. Hence before proceeding further, manually remove all the subfolders inside the msg and srv folder.

  7. Generating custom ROS message definitions using rosgenmsg on Windows OS has limitation on maximum number of custom messages. Hence, limit the number of custom messages and services to a minimum required. If you want to just add few ROS messages to the set of custom message / service definitions, then ensure that you first copy .msg and .srv files for the default message set and then additional files for the ROS messages which you want to add.

  8. Navigate to the folder which contains the custom ROS package and execute rosgenmsg on MATLAB command window.

  9. Wait for the process to complete and once it is done execute the mentioned commands to modify MATLAB path, clear classes and rehash toolboxcache.

  10. Check the output of MATLAB command rosmsg list for the messages from the group kortex_driver or whatever the name of your custom ROS package is.