How to create custom message types in ROS 2

How to create custom message types in ROS 2
Image: Intellect (generated)

In short: Learn to define your own data structures for robot communication. Creating custom interfaces allows you to pass specific information, like contact details, between different parts of a robotic system.

In ROS 2, custom interfaces allow you to define the specific data structures used for communication. By creating a custom message file, you specify the fields—such as strings for names or integers for phone types—that your nodes will exchange.

When defining these messages within the same package as your code, you must use specific CMake commands to link your executable to the generated C++ types. This ensures the compiler can recognize your custom message format during the build process.

For example, you can create a message named AddressBook.msg containing fields for a first name, last name, phone number, and a phone type constant.

Steps

  1. Create a new package named more_interfaces using the ament_cmake build type.
  2. Create a directory named msg inside your new package.
  3. Create a file named AddressBook.msg in the msg directory and define your data fields.
  4. Add the rosidl_default_generators and rosidl_default_runtime dependencies to your package.xml.
  5. Add the necessary CMake code to your CMakeLists.txt to find rclcpp and link your executable.
  6. Add the rosidl_get_typesupport_target command to your CMakeLists.txt to link the types from the same package.
  7. Build the package from the workspace root using the colcon build command.
  8. Source the workspace and run your node to use the new message.

Based on: ROS 2 Documentation — Implementing custom interfaces — tutorial · Licence: CC BY 4.0 · View source →

Opens the Intellect+ feed with this card first. Swipe up for the next one.

Sources

To cite this card, link to https://intellectplus.co.uk/app/technology-how-to-create-custom-message-types-in-ros-2.

How we source, check and correct cards: Editorial standards.

General information, not professional advice. Spotted an error? Flag the card or see corrections and complaints.

Intellect Plus is a visual educational discovery platform for exploring fascinating facts and ideas. Brain Food, Not Brain Rot. What is Intellect Plus?

Related

Finding something worth knowing…