Create Custom message in ROS
Our objective is create a custom message called samplemsg with two fields id and name. You can check the available message types by below command
rosmsg list
Now we generate a custom message called samplemsg. Follow these steps:
- Navigate to your ROS package and create a msg folder
- create an empty document in to the msg folder name it as samplemsg with extension .msg
- Open the package in any editor and add the attributes that you want in your custom message.
Here I add id and name attributes with data type int32 and string. To check more details about these primitive data type check in below link.
After updating the message next need to add dependencies in both package.xml and CMakeList.txt
- Open package.xml file and add build depend message_generation and execution depend message_runtime
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
These two lines are important to generate messages in ROS.
- Open CMakeList.txt file and add message_generation in find_package section.
- Uncomment the add_message_files section and add our message file.
It’s responsible for adding the file in ros eco system.
- Add message_runtime dependency in catkin_package section
- Make sure the std_msgs is activated in generate_message
Now all the required configurations are done in package.xml and CMakeList.txt files and ready to generate the message.
- Compile the package in catkin_ws using the command catkin_make
We can see that our custom message samplemsg is generated successfully.
- Check our custom message is correctly generated or not using below command
rosmsg show samplemsg