Message and interface: what the data looks like
A topic is a "pipe", and message is what flows in the pipe. Each topic only carries a fixed type message — /cmd_vel is always Twist, /scan is always LaserScan. Knowing how to read message types means knowing what a node needs and what it produces, before even reading its code.
After this lesson you will
- Distinguish 3 types of interfaces: msg (topic), srv (service), action
- Use
ros2 interface showandros2 topic infoto manually look up the structure of any message - Understand
header(stamp+frame_id) — two fields that decide the data "when measured, according to which coordinate system" - Immediately recognize about 10 types of messages that every autonomous robot uses
3 interface types
| Type | Definition file | Used for | Structure |
|---|---|---|---|
| msg | .msg | Topic (previous lesson) | A block of data |
| srv | .srv | Service (next lesson) | Request --- Response |
| action | .action | Action — long-running task with progress reports and cancellation (Module 12) | Goal --- Result --- Feedback |
The full name has 3 parts: package/type/Name, for example geometry_msgs/msg/Twist, std_srvs/srv/Empty, nav2_msgs/action/NavigateToPose.
Self-check the structure
ros2 interface show geometry_msgs/msg/Twist
Vector3 linear
float64 x
float64 y
float64 z
Vector3 angular
float64 x
float64 y
float64 z
Message in message: Twist includes 2 Vector3, each Vector3 includes 3 real numbers. Frequently used search commands:
ros2 topic type /cmd_vel # What type does this topic contain?
ros2 topic info /scan --verbose # type + who publishes + who subscribes + QoS
ros2 interface list | grep nav_msgs # every interface in a package
ros2 interface show sensor_msgs/msg/LaserScan
header: when to measure, in what frame
Most sensor and position messages start with std_msgs/msg/Header:
builtin_interfaces/Time stamp # the time the data was MEASURED (not the time it was sent)
string frame_id # coordinate system of data, eg "lidar_link", "odom", "map"
frame_id: point 2 m from robot — where is 2 m from?/scanwithframe_id: lidar_linkmeans the distance measured from the LiDAR center. To know where that point is on the map, TF2 (Module 2) changes fromlidar_linktomap.stamp: the robot is moving; its position changes every millisecond. TF2 usesstampto look up transform at the exact time of measurement. Wrong stamps (e.g. off-clock, or using real time in simulation — seeuse_sim_timein Module 4) cause the classic "extrapolation into the future/past" error.
type with additional header usually has suffix Stamped: Pose → PoseStamped, Twist → TwistStamped.
~10 message types of every autonomous robot
| Typical topic | type | What to carry |
|---|---|---|
/cmd_vel | geometry_msgs/msg/Twist | Velocity command (m/s, rad/s) |
/odom | nav_msgs/msg/Odometry | Pose + velocity from odometry, with covariance |
/scan | sensor_msgs/msg/LaserScan | Array of LiDAR distances by angle |
/imu | sensor_msgs/msg/Imu | Orientation, angular velocity, acceleration |
/joint_states | sensor_msgs/msg/JointState | Angle/speed of each joint (wheel) |
/tf, /tf_static | tf2_msgs/msg/TFMessage | Transforms between frame |
/map | nav_msgs/msg/OccupancyGrid | Occupancy grid map |
/goal_pose | geometry_msgs/msg/PoseStamped | Goal pose (RViz "Nav2 Goal") |
/initialpose, /amcl_pose | geometry_msgs/msg/PoseWithCovarianceStamped | Pose with uncertainty |
/plan | nav_msgs/msg/Path | path calculated by planner |
Publisher and subscribers on the same topic must have the same type. Sending TwistStamped to a node waiting for Twist gives no error — the data simply doesn't arrive. The exact use_stamped_vel issue is documented in diff_drive_controller.yaml of Atlas (Module 5). If you encounter "node not receiving data", check ros2 topic info -v first.
Custom message
When there is no suitable type, create the file .msg in a separate package (usually named *_interfaces), for example BatteryStatus.msg:
std_msgs/Header header
float32 voltage
float32 percentage
bool charging
The interface package must be built with ament_cmake + rosidl_generate_interfaces(...) in CMakeLists.txt (even if the node using it is written in Python). Advice: use the standard type before defining it yourself — the standard type is already understood by RViz, rosbag and all other tools.
Self-check
How do msg, srv, and action differ?
msg: a block of data, used for topic. srv: request/response pair, used for service. action: goal/result/feedback, used for long-running tasks with progress reports and cancellation.
How do I know what type the topic /scan contains and what fields it has?
ros2 topic type /scan (or ros2 topic info /scan) to get the type, then ros2 interface show sensor_msgs/msg/LaserScan to see the fields.
What are header.stamp and header.frame_id used for?
stamp: the time the data was measured — TF2 is used to look up the transform at that time. frame_id: coordinate system of data — shows where the measurement comes from.
Node A publishes TwistStamped to /cmd_vel, node B subscribes to Twist. Are there any errors that appear?
There is usually no obvious error — B simply did not receive the data. You must check the type yourself with ros2 topic info -v.