ROS 2 Lyrical Luth · Lesson 08 of 86
Message types: standard and custom
std_msgs, sensor_msgs, geometry_msgs, writing .msg files and building a custom interface package.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain message types: standard and custom in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Every topic has a message type that defines its fields. Use the standard ones first: std_msgs (numbers, strings), geometry_msgs (Pose, Twist), sensor_msgs (LaserScan, Image, Imu). Look up a type with ros2 interface show.
When no standard type fits, define your own in a .msg file inside a dedicated interface package built with rosidl_default_generators. Keep interfaces in their own package so every other package can depend on them without circular dependencies.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the message types: standard and custom example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
ros2 interface show geometry_msgs/msg/Twist
# Print the fields of a standard message (linear and angular velocity).
ros2 pkg create --build-type ament_cmake robot_interfaces
# Create a package dedicated to interface definitions.
mkdir robot_interfaces/msg
# Message files live in msg/.
printf 'builtin_interfaces/Time stamp\nuint8 mode\nuint16 fault_code\n' > robot_interfaces/msg/RobotStatus.msg
# Define RobotStatus with a timestamp, a mode and a fault code.
# In CMakeLists.txt: find_package(rosidl_default_generators REQUIRED)
# Generator dependency for C++ and Python code.
# rosidl_generate_interfaces(${PROJECT_NAME} "msg/RobotStatus.msg" DEPENDENCIES builtin_interfaces)
# Generate the language bindings for the file.
ros2 interface show robot_interfaces/msg/RobotStatus
# After building, confirm the new type exists.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Messages between nodes
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Message types: standard and custom. Expected result: uint8 mode uint16 fault_code
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
# robot_interfaces/msg/RobotStatus.msg
builtin_interfaces/Time stamp
# TODO: add a uint8 field named mode and a uint16 field named fault_codeExpected output
uint8 mode
uint16 fault_codeStuck? Show the full solution (try it yourself first)
# robot_interfaces/msg/RobotStatus.msg
builtin_interfaces/Time stamp
uint8 mode
uint16 fault_codeExpected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Create robot_interfaces with a RobotStatus message (stamp, mode, fault_code). Publish it from C++ and echo it with the CLI. Then add a field, rebuild, and note which packages had to rebuild.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.