📖 Scenario: You are building a simple ROS (Robot Operating System) package that needs to send custom data between nodes. To do this, you must define a custom message type that includes specific fields.
🎯 Goal: Create a custom ROS message definition file with exact fields, then use it in a ROS node to publish messages of that type.
📋 What You'll Learn
Create a ROS message file named MyCustomMsg.msg with specified fields
Add a configuration variable for the message queue size
Write a ROS publisher node that publishes messages of type MyCustomMsg
Complete the ROS node setup with proper initialization and publishing
💡 Why This Matters
🌍 Real World
Custom messages let ROS nodes communicate complex data tailored to your robot's needs, like sensor readings or commands.
💼 Career
Defining and using custom messages is essential for robotics software engineers working with ROS to build modular and scalable robot applications.
Progress0 / 4 steps
1
Create the custom message file
Create a ROS message file named MyCustomMsg.msg with these exact fields and types: int32 id string name float64 value
ROS
Hint
This file defines the structure of your custom message. Each line specifies a field type and name.
2
Add a queue size configuration variable
In your ROS publisher node script, create a variable called queue_size and set it to 10.
ROS
Hint
This variable controls how many messages can be buffered before sending.
3
Write the ROS publisher node core logic
Write code to create a ROS publisher named pub that publishes MyCustomMsg messages on the topic custom_topic with the queue_size variable. Use the ROS Python client library conventions.
ROS
Hint
Use rospy.Publisher with the topic name, message type, and queue size.
4
Complete the ROS node with publishing logic
Add code to create a MyCustomMsg message instance named msg, set its fields to id=1, name='test', and value=3.14, then publish it using pub.publish(msg).
ROS
Hint
Create the message object, set each field exactly, then call publish.
Practice
(1/5)
1. What is the main purpose of defining a custom message in ROS?
easy
A. To send specific data types tailored to your robot's needs
B. To create new robot hardware components
C. To write Python scripts for robot control
D. To install ROS packages automatically
Solution
Step 1: Understand the role of custom messages
Custom messages allow you to define exactly what data your robot nodes exchange.
Step 2: Compare with other options
Options B, C, and D relate to hardware, scripting, or package management, not message definition.
Final Answer:
To send specific data types tailored to your robot's needs -> Option A
Quick Check:
Custom messages = tailored data [OK]
Hint: Custom messages define data, not hardware or scripts [OK]
Common Mistakes:
Confusing message definition with hardware setup
Thinking custom messages are scripts
Assuming messages install packages
2. Which of the following is the correct syntax to define a field named speed of type float32 in a ROS .msg file?
easy
A. speed float32
B. float32 speed
C. float speed32
D. float speed
Solution
Step 1: Recall ROS message field syntax
In ROS .msg files, the type comes first, then the field name, e.g., float32 speed.
Step 2: Check each option
float32 speed matches the correct syntax. Options B, C, and D have incorrect order or wrong types.
Final Answer:
float32 speed -> Option B
Quick Check:
Type before name in .msg files [OK]
Hint: Type first, then field name in .msg files [OK]
Common Mistakes:
Swapping field name and type order
Using incorrect type names
Omitting the type or name
3. Given this custom message definition in RobotStatus.msg:
What will be the default value of is_active when a new message is created in a ROS node?
medium
A. false
B. 1
C. null
D. true
Solution
Step 1: Understand default values in ROS messages
Boolean fields default to false when a message is created without explicit initialization.
Step 2: Evaluate options
false is false, which matches the default. Options A and B represent true values, and C is invalid for bool.
Final Answer:
false -> Option A
Quick Check:
Boolean default = false [OK]
Hint: Boolean fields default to false unless set [OK]
Common Mistakes:
Assuming booleans default to true
Confusing null with default values
Using numeric 1 instead of boolean true
4. You wrote this in your SensorData.msg file:
int32 temperature
float64 humidity
string location
But when building your ROS package, you get an error. What is the likely cause?
medium
A. Using float64 instead of float32 is invalid in ROS messages
B. Field names cannot be lowercase
C. Missing blank line at the end of the file
D. No error; the message definition is correct
Solution
Step 1: Check ROS message type validity
ROS supports float64 as a valid type, so Using float64 instead of float32 is invalid in ROS messages is incorrect.
Step 2: Verify field naming and formatting
Lowercase field names are allowed, and no blank line is required at the end.
Final Answer:
No error; the message definition is correct -> Option D
Quick Check:
Valid types and names mean no error [OK]
Hint: ROS supports float64 and lowercase names [OK]
Common Mistakes:
Thinking float64 is invalid
Assuming field names must be uppercase
Expecting blank line at file end
5. You want to define a custom message RobotCommand.msg that includes a nested message Header from the standard ROS messages and a custom field command of type string. Which is the correct way to define this?
hard
A. Header header
string command
B. header Header
command string
C. std_msgs/Header header
string command
D. Header: std_msgs/Header
string command
Solution
Step 1: Understand nested message syntax in ROS
To use a message from another package, you must prefix the type with the package name, e.g., std_msgs/Header.
Step 2: Check each option
std_msgs/Header header
string command correctly uses std_msgs/Header header. Header header
string command misses the package prefix, C reverses type and name, D uses invalid syntax.
Final Answer:
std_msgs/Header header
string command -> Option C
Quick Check:
Use package/type for nested messages [OK]
Hint: Prefix nested types with package name [OK]
Common Mistakes:
Omitting package name for nested messages
Swapping field name and type
Using colon syntax incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.