Inferensys

Glossary

ROS Message (.msg)

A ROS Message is a strictly typed data structure, defined in a .msg file, that is used for serialization and deserialization when publishing to topics or calling services.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
DATA STRUCTURE

What is ROS Message (.msg)?

A ROS Message is the fundamental unit of data exchange in the Robot Operating System (ROS), providing strict typing and serialization for inter-process communication.

A ROS Message is a strictly typed data structure, defined in a .msg file, that is used for serialization and deserialization when publishing to ROS Topics or calling ROS Services. Each .msg file defines a compound type by listing its fields—such as standard primitives (e.g., string, int32, float64) or other message types—enabling nodes to exchange structured data with a shared, language-agnostic schema. This definition is compiled by the ROS build tools into language-specific code (e.g., C++ classes, Python modules) that handles the binary conversion for network transmission.

The .msg file format enforces data contract reliability across a distributed system, ensuring that publishers and subscribers agree on the structure and semantics of the information being passed. Common patterns include wrapping sensor readings (like sensor_msgs/Image for camera data or geometry_msgs/Twist for velocity commands) or composing complex state information. This strict typing is central to ROS's middleware function, allowing for tooling like rosbag for recording and the rostopic echo command for introspection, while providing the deterministic foundation necessary for real-time robotic control systems.

DATA STRUCTURE

Core Characteristics of ROS Messages

A ROS Message (.msg) is the fundamental unit of data exchange in the Robot Operating System. It is a strictly typed, serializable data structure defined in a simple text file.

01

Definition and Serialization

A ROS Message is a strictly typed data structure defined in a .msg file. Its primary purpose is serialization—converting complex data into a flat byte stream for transmission over a network or storage in a ROS Bag. The .msg file serves as a contract between publishing and subscribing nodes, ensuring data integrity. For example, a common sensor message like sensor_msgs/Image serializes header information, image height/width, encoding, and raw pixel data into a single byte array for efficient transport.

02

Primitive and Built-in Types

ROS Messages are composed of primitive field types that map directly to programming language primitives. The core built-in types include:

  • Numeric types: int8, uint32, float64
  • Boolean: bool
  • String: string
  • Time/Duration: time, duration
  • Raw byte array: byte, char These types ensure predictable memory layout and cross-language compatibility. For instance, a float64 in a .msg file will deserialize to a double in C++ and a float in Python.
03

Arrays, Bounded Arrays, and Constants

Messages support complex field modifiers for flexible data structuring.

  • Unbounded Arrays: Defined with [] (e.g., float32[] ranges), these are variable-length sequences.
  • Bounded Arrays: Defined with a fixed size [N] (e.g., float64[3] position), which serializes to a known, fixed block of bytes, improving memory predictability.
  • Constants: Allow defining immutable values within the message (e.g., uint8 SUCCESS=0). These constants are compiled into the generated code and are accessible to nodes using the message type.
04

Message Composition and Nested Types

Messages can be composed of other message types, enabling hierarchical and complex data structures. This is a key mechanism for code reuse and standardization.

  • Field Composition: A message can have a field of another message type (e.g., geometry_msgs/Pose pose).
  • Standard Message Definitions: The ROS ecosystem provides common package libraries (like std_msgs, geometry_msgs, sensor_msgs) with widely adopted message definitions. For example, sensor_msgs/LaserScan composes a std_msgs/Header, float arrays for ranges and intensities, and scan parameters.
05

Code Generation and Language Support

The .msg file is not used directly in code. Instead, the ROS build toolchain (colcon) uses a message generator to produce language-specific source code.

  • C++: Generates a header and source file defining a class with getter/setter methods for each field.
  • Python: Generates a Python module with a class definition.
  • Other Languages: Support for Rust, Java, etc., via additional generators. This process ensures that a node written in C++ can seamlessly exchange a Twist message with a node written in Python, as both use the same binary on-the-wire format.
06

Relationship to Topics, Services, and Actions

Messages are the payload for ROS's core communication mechanisms.

  • Topics: Use messages for asynchronous, one-way streaming (publish/subscribe).
  • Services: Use a pair of messages: one for the request and one for the response, defined in a .srv file.
  • Actions: Use three message types defined in an .action file: a Goal, a Result, and periodic Feedback. This unified typing system means a geometry_msgs/Twist message can be published to a /cmd_vel topic for robot control, or used as part of a service call, with identical serialization.
DATA STRUCTURE

How ROS Messages Work

A ROS Message is the fundamental unit of data exchange in the Robot Operating System, enabling deterministic communication between nodes.

A ROS Message is a strictly typed data structure, defined in a .msg file, that is used for serialization and deserialization when publishing to topics or calling services. Each message is composed of primitive fields (e.g., int32, float64, string) or other nested message types, forming a contract that ensures data integrity across the distributed system. The .msg definition is compiled into language-specific code (e.g., C++, Python) by the ROS build tools, providing developers with type-safe classes for constructing and parsing message data.

Messages enable the publish-subscribe and request-response communication models central to ROS. When a node publishes a message to a topic, it is serialized into a standard byte stream, transmitted, and then deserialized by subscribing nodes. This mechanism allows for loose coupling between system components. For complex or long-running tasks, the ROS Action protocol builds upon basic messages, using dedicated goal, feedback, and result message types to manage stateful interactions between clients and servers.

DATA STRUCTURES

Common ROS Message Examples

ROS Messages are the fundamental data packets for communication. These examples illustrate the most common, standardized message types used across perception, control, and state estimation.

01

geometry_msgs/Twist

A message for expressing velocity in free space, broken into linear and angular components. It is the primary command message for mobile robot base control.

  • Linear: A 3D vector (x, y, z) for velocity in meters per second. For a differential-drive robot, typically only linear.x is used for forward/backward motion.
  • Angular: A 3D vector (x, y, z) for rotational velocity in radians per second. For a differential-drive robot, typically only angular.z is used for turning (yaw).

Example: Commanding a TurtleBot3 to move forward at 0.2 m/s while turning left at 0.5 rad/s would set {linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.5}}.

02

sensor_msgs/Image

The standard message for transmitting raw or compressed image data from cameras. It contains critical metadata alongside the pixel array.

  • Header: Includes a timestamp and frame ID for synchronization and transformation (tf).
  • Height/Width: Image dimensions in pixels.
  • Encoding: A string defining the pixel format (e.g., rgb8, bgr8, mono8).
  • is_bigendian: Byte order of the data.
  • step: Full row length in bytes (width * num_channels * bytes_per_channel).
  • data: The actual pixel array as a sequence of bytes.

This message is consumed by vision nodes for tasks like object detection, visual SLAM, and semantic segmentation.

03

nav_msgs/Odometry

A message that estimates the position and velocity of a robot in a coordinate frame (often odom or map). It is a core output of localization systems.

  • Header: Timestamp and child frame ID (typically the robot's base frame, e.g., base_link).
  • Pose: Contains the estimated position (pose.pose.position) and orientation (pose.pose.orientation as a quaternion) along with a covariance matrix (pose.covariance).
  • Twist: Contains the estimated linear and angular velocity (twist.twist) with its own covariance matrix (twist.covariance).

Related Topic: Often published on the /odom topic and used by the navigation stack for feedback control.

04

sensor_msgs/LaserScan

A single scan from a planar range-finder (e.g., a 2D LiDAR). It represents distance measurements at fixed angular intervals in a plane.

  • angle_min/max: The start and end angles of the scan (in radians).
  • angle_increment: The angular distance between measurements.
  • time_increment: Time between measurements (if scanning is sequential).
  • scan_time: Time between complete scans.
  • range_min/max: Valid range limits for the sensor.
  • ranges: An array of measured distances. Values outside [range_min, range_max] are considered invalid.
  • intensities: Optional array of reflection intensity values.

This is a primary input for obstacle avoidance, SLAM, and 2D mapping algorithms.

05

geometry_msgs/PoseStamped

A Pose (position and orientation) with a Header, allowing it to be associated with a specific coordinate frame and timestamp. It is used for specifying goals and publishing localized poses.

  • Header: Contains stamp (ros::Time) and frame_id (string, e.g., map).
  • Pose: A geometry_msgs/Pose object containing:
    • position: A Point (x, y, z coordinates).
    • orientation: A Quaternion (x, y, z, w components).

Common Use Cases:

  • Sending a navigation goal to move_base.
  • Publishing the estimated robot pose from a localization filter.
  • Defining waypoints in a task plan.
06

std_msgs/Header

Not a standalone message, but a critical sub-message included in almost all sensor and state messages. It provides essential metadata for data synchronization and transformation.

  • seq: A consecutively increasing sequence ID (deprecated in ROS 2 in favor of timestamps).
  • stamp: A ros::Time or builtin_interfaces/Time object representing the acquisition time of the data. This is crucial for sensor fusion.
  • frame_id: A string identifying the coordinate frame in which the data is expressed (e.g., base_link, camera_color_optical_frame). This is essential for the TF2 transform library to correctly interpret the data's spatial context.

Understanding the Header is fundamental to building robust, temporally consistent robotic systems.

ROS MESSAGE (.MSG)

Frequently Asked Questions

A ROS Message is the fundamental data structure for communication in the Robot Operating System. These frequently asked questions cover its definition, usage, and role in building robust robotic software.

A ROS Message is a strictly typed data structure, defined in a .msg file, that is used for serialization and deserialization when publishing to topics or calling services. It works by providing a common language for nodes to exchange information. A .msg file defines the data fields and their types (e.g., string, int32, float64[]). The ROS client libraries (like rclcpp for C++ or rclpy for Python) use these definitions to automatically generate source code for message classes. These classes include methods to serialize the data into a compact byte stream for network transmission and to deserialize incoming byte streams back into structured objects that the node's logic can process. This mechanism ensures data consistency and interoperability between nodes written in different programming languages.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.