What is a ROS2 message? Standardized data structures
Atlasbot · 16/8/2026 · 11 min read
A topic does not transmit "generic data" — it transmits exactly a fixed message type, explicitly defined before compilation. This is why publishers written in C++ and subscribers written in Python still understand each other's data correctly: both use the same message definition, just in different code generation languages.
Key concepts
Message is declared in file .msg — very simple syntax, each line is data_type field_name:
# geometry_msgs/Twist.msg
Vector3 linear
Vector3 angular
# geometry_msgs/Vector3.msg
float64 x
float64 y
float64 z
Messages can be nested — Twist contains two Vector3 inside, each Vector3 has 3 real number fields. Available basic types: int32, float64, string, bool, bool[] (array)... enough to assemble most data structures the robot needs.
Message is compiled into a real class
When building a package, ROS2's code generation tool automatically reads files .msg and and generates the corresponding class in each language (Python, C++) — no need to manually write struct/class, and no fear of different definitions because both sides are generated from the same .msg source.
In short: File
.msgis a "contract" that describes the data shape — publishers and subscribers, no matter what language they are written in, must comply with that contract, ensuring they never misunderstand each other's data structure.
How it works
Twist.msg (definition)
↓ compile at build time (rosidl)
Python: geometry_msgs.msg.Twist
C++: geometry_msgs::msg::Twist
↓
Used in code like a normal class/struct
Create and fill a message in Python:
from geometry_msgs.msg import Twist
msg = Twist()
msg.linear.x = 0.2 # Access nested fields like object properties
msg.angular.z = 0.1
publisher.publish(msg)
View the full structure of any type of message (including messages from other people's packages) without having to open file .msg manually:
ros2 interface show geometry_msgs/msg/Twist
Technical details
Formulas
Message size when transmitted over the network is estimated to be the total size of each field, plus packaging overhead (CDR, see section Register/Bit/Byte):
Message size ≈ Σ(size of each field) + CDR overhead
Example: Twist (2 × Vector3, each Vector3 = 3 × float64)
= 2 × (3 × 8 byte) + overhead ≈ 48 byte + ~20 byte overhead ≈ ~68 byte
This number directly explains the "size of 1 message" part used in the bandwidth formula in the Topic article — the more nested fields a message has, the more bandwidth it will consume when publishing at a high frequency.
Parameters
Byte size of primitive data types in .msg — using the right type just enough helps save bandwidth when designing custom messages:
| Type | Size |
|---|---|
int8 / uint8 / bool | 1 byte |
int16 / uint16 | 2 byte |
int32 / uint32 / float32 | 4 byte |
int64 / uint64 / float64 | 8 byte |
string | Variable length (4 bytes stored length + actual content) |
Register / Bit / Byte
When a ROS2 message is sent over the network, it is not transmitted as a language object (Python object, C++ struct) directly — it is serialized into a byte sequence according to the CDR (Common Data Representation) standard, the data encoding standard specified by DDS. Exactly like the padding in struct C mentioned in the embedded programming section, CDR also applies the rule memory alignment (alignment) — a field float64 (8 bytes) in the message always starts at an offset divisible by 8 in the serialized byte string, additional padding bytes can be inserted if the previous field is not aligned enough, completely similar to the learned struct padding principle.
Practical applications
When designing custom messages for a specific robot, it often makes more sense to combine closely related fields into 1 message instead of separating them into many separate topics — for example, 1 message BatteryStatus.msg includes voltage, current, percentage, temperature to publish in 1 topic, instead of 4 separate topics for each value — helping the recipient always have all 4 The value is synchronized at the same time (same message), avoid the situation of reading voltage at this time and current at another time and then calculating the error due to the time difference between 2 readings from 2 separate topics.
Practical example
$ ros2 interface show geometry_msgs/msg/Twist
Output:
# This expresses velocity in free space broken into its linear and angular parts.
Vector3 linear
float64 x
float64 y
float64 z
Vector3 angular
float64 x
float64 y
float64 z
$ ros2 topic type /cmd_vel
Output:
geometry_msgs/msg/Twist
ros2 interface show is the fastest way to look up the full structure of any message (including multi-level nested messages) without having to find and open the original .msg file in the source code — useful when working with someone else's unfamiliar package.
Common mistakes
- Do not rebuild after editing file
.msg— Python/C++ code using the old message class (generated before editing) can still run but lacks/excess fields compared to the new definition — alwayscolcon buildre-package containing messages after each edit.msg. - Name a field with the same language-specific keyword — For example, naming a field
classorimport(Python keyword) causes code generation errors or syntax conflicts when used in Python code. - Choosing a number type that is too narrow for the actual value range — similar to the number overflow error mentioned in the embedded programming section, using
int8for values that may exceed 127 (eg: battery percentage calculated in the wrong unit) causes a silent number overflow. - Circular dependency between 2 custom message packages — package A defines message using styles from package B, package B uses styles from package A —
colconcannot determine valid build order, needs refactoring to remove circular dependency.
Conclusion
Messages are "contracts" that define the shape of data exchanged between nodes, automatically generated into classes for each language from a single .msg source — ensuring consistency regardless of programming language. Understanding how CDR serializes messages into bytes helps to accurately explain the bandwidth costs calculated in the Topic article. The next article in this category goes deeper into the Service — request-response mechanism, and how it is implemented from the Message + Topic below.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.