Bird
Raised Fist0
ROSframework~5 mins

Standard message types (std_msgs, geometry_msgs) in ROS

Choose your learning style10 modes available
Introduction

Standard message types help robots talk to each other using common formats. They make sharing data like numbers, text, or positions easy and clear.

Sending simple data like numbers or text between robot parts.
Sharing robot position or movement information in a common way.
Communicating sensor data like distances or angles.
Exchanging commands or status updates using standard formats.
Building robot programs that work well with others using common messages.
Syntax
ROS
std_msgs/msg/String
std_msgs/msg/Int32
geometry_msgs/msg/Point
geometry_msgs/msg/Twist

Message types are organized in packages like std_msgs for simple data and geometry_msgs for spatial data.

Each message type has fields you fill with data before sending.

Examples
This message holds text data in the data field.
ROS
std_msgs/msg/String
string data
This message holds a 32-bit integer number in the data field.
ROS
std_msgs/msg/Int32
int32 data
This message holds a 3D point with x, y, and z coordinates.
ROS
geometry_msgs/msg/Point
float64 x
float64 y
float64 z
This message holds velocity data: linear and angular parts, each with x, y, and z components.
ROS
geometry_msgs/msg/Twist
geometry_msgs/Vector3 linear
geometry_msgs/Vector3 angular
Sample Program

This example shows a simple ROS2 node that publishes a std_msgs/String message every second. It sends text like "Hello ROS 0", "Hello ROS 1", and so on.

ROS
# Python ROS2 example publishing a string message
import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class MinimalPublisher(Node):
    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(String, 'topic', 10)
        timer_period = 1  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = String()
        msg.data = f'Hello ROS {self.i}'
        self.publisher_.publish(msg)
        self.get_logger().info(f'Publishing: "{msg.data}"')
        self.i += 1


def main(args=None):
    rclpy.init(args=args)
    minimal_publisher = MinimalPublisher()
    rclpy.spin(minimal_publisher)
    minimal_publisher.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
OutputSuccess
Important Notes

Always use the correct message type for your data to avoid errors.

Standard messages make it easier to connect different robot parts or programs.

Use ROS tools like ros2 topic echo to see messages live in your terminal.

Summary

Standard message types are like shared languages for robots.

std_msgs covers simple data like text and numbers.

geometry_msgs covers spatial data like points and velocities.

Practice

(1/5)
1. Which ROS standard message type would you use to send a simple integer value?
easy
A. std_msgs/Int32
B. geometry_msgs/Point
C. std_msgs/String
D. geometry_msgs/Twist

Solution

  1. Step 1: Understand message categories

    std_msgs contains simple data types like integers and strings, while geometry_msgs contains spatial data.
  2. Step 2: Identify the correct message type for an integer

    std_msgs/Int32 is designed to hold a 32-bit integer value.
  3. Final Answer:

    std_msgs/Int32 -> Option A
  4. Quick Check:

    Integer data = std_msgs/Int32 [OK]
Hint: Use std_msgs for simple data like numbers or text [OK]
Common Mistakes:
  • Confusing geometry_msgs with std_msgs for simple data
  • Choosing a spatial message type for integer data
  • Using std_msgs/String for numeric values
2. Which of the following is the correct way to declare a ROS message of type geometry_msgs/Twist in Python?
easy
A. from std_msgs.msg import Twist msg = Twist()
B. import geometry_msgs msg = geometry_msgs.Twist()
C. from geometry_msgs.msg import Twist msg = Twist()
D. msg = geometry_msgs.msg.Twist()

Solution

  1. Step 1: Import the correct message module

    ROS messages are imported from the msg submodule of the package, so from geometry_msgs.msg import Twist is correct.
  2. Step 2: Instantiate the message

    Creating a message instance is done by calling the class: msg = Twist().
  3. Final Answer:

    from geometry_msgs.msg import Twist msg = Twist() -> Option C
  4. Quick Check:

    Correct import and instantiation = from geometry_msgs.msg import Twist msg = Twist() [OK]
Hint: Always import messages from the .msg submodule [OK]
Common Mistakes:
  • Importing from wrong package like std_msgs for Twist
  • Trying to instantiate without importing
  • Using incorrect import syntax
3. Given the following Python code snippet in a ROS node, what will be the printed output?
from std_msgs.msg import String
msg = String()
msg.data = "Hello ROS"
print(msg.data)
medium
A. String(data='Hello ROS')
B. Hello ROS
C. msg.data
D. Error: msg has no attribute data

Solution

  1. Step 1: Assign string to message data field

    The String message has a field called data which holds the string value "Hello ROS".
  2. Step 2: Print the data field

    Printing msg.data outputs the string value directly, so it prints "Hello ROS".
  3. Final Answer:

    Hello ROS -> Option B
  4. Quick Check:

    Printing msg.data shows the string content [OK]
Hint: Print the .data field to see the actual message content [OK]
Common Mistakes:
  • Printing the whole message object instead of .data
  • Expecting a formatted string instead of raw data
  • Confusing attribute names
4. What is wrong with this ROS Python code snippet that tries to create a geometry_msgs/Point message?
import geometry_msgs
point = geometry_msgs.Point()
point.x = 1.0
point.y = 2.0
point.z = 3.0
medium
A. Point message does not have x, y, z fields
B. The code is correct and will run without errors
C. Cannot assign float values to Point fields
D. Missing import from geometry_msgs.msg

Solution

  1. Step 1: Check import statement

    The code imports the whole package geometry_msgs, but message classes are inside the msg submodule, so from geometry_msgs.msg import Point is needed.
  2. Step 2: Understand message instantiation

    Without the correct import, geometry_msgs.Point() will cause an error because Point is not directly accessible.
  3. Final Answer:

    Missing import from geometry_msgs.msg -> Option D
  4. Quick Check:

    Import messages from .msg submodule [OK]
Hint: Always import messages from geometry_msgs.msg, not geometry_msgs [OK]
Common Mistakes:
  • Importing the package instead of the msg submodule
  • Assuming message fields are missing
  • Incorrectly thinking float assignment is invalid
5. You want to publish a robot's velocity using geometry_msgs/Twist. Which fields must you set to represent a forward linear velocity of 1.5 m/s and no rotation?
hard
A. msg.linear.x = 1.5, msg.angular.z = 0.0
B. msg.linear.y = 1.5, msg.angular.x = 0.0
C. msg.linear.z = 1.5, msg.angular.y = 0.0
D. msg.linear.x = 0.0, msg.angular.z = 1.5

Solution

  1. Step 1: Understand geometry_msgs/Twist structure

    The Twist message has two vectors: linear and angular, each with x, y, and z components.
  2. Step 2: Set forward linear velocity and zero rotation

    Forward motion is along the x axis, so linear.x = 1.5. No rotation means all angular components are zero; setting angular.z = 0.0 is sufficient here.
  3. Final Answer:

    msg.linear.x = 1.5, msg.angular.z = 0.0 -> Option A
  4. Quick Check:

    Forward velocity on linear.x, zero rotation on angular.z [OK]
Hint: Forward velocity is linear.x; rotation is angular.z [OK]
Common Mistakes:
  • Setting velocity on wrong axis (y or z)
  • Confusing linear and angular fields
  • Assigning rotation value to linear components
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard