# Create our custom message

There are files in the code section, you might need to download those file onto your robot when needed.

# Create our own custom msg to save bandwidth

before we create our msg file, we need to know the format and where to store it. According to the ROS document, the msg file should be stored in the msg directory of a package, and srv files are stored in the srv directory.

msgs are just simple text files with a field type and field name per line. The field types you can use are:

  • int8, int16, int32, int64 (plus uint*)
  • float32, float64
  • string
  • time, duration
  • other msg files
  • variable-length array[] and fixed-length array[C]

Additionally, there is a special type in ROS: Header. The Header contains a timestamp and coordinate frame information that are commonly used in ROS.

In our case, the header file is already given; since we are all using the same robot.

--- Link to the header file ---

We are going to create different header files that should be in the msg directory of your project. (Probably named beginner_tutorials)

You can choose the name of those header file. However, one example will be:

touch Control_msgs.msg
1

After that, you can copy and past the content into the file. One example will be:

# Command message
float32 left_vel
float32 right_vel
float32 shoulder_vel
float32 elbow_vel
1
2
3
4
5

Do the same thing for all four header files.

# edit the package.xml

Open package.xml, and make sure these two lines are in it and uncommented:

<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
1
2

# edit the CMakeLists.txt

# Do not just add this to your CMakeLists.txt, 
# modify the existing text to 
# add message_generation before the closing parenthesis
find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation
)
1
2
3
4
5
6
7
8
9

After that, you need to make sure you export the message runtime dependency.

catkin_package(
  ...
  CATKIN_DEPENDS message_runtime ...
  ...)
1
2
3
4

Finally, we need to find the following block of code and add our message into it.

add_message_files(
  FILES
  Control_msgs.msg
  Imu_msgs.msg
  ...
)
1
2
3
4
5
6

In the end, un-comment the following part:

generate_messages(
   DEPENDENCIES
   std_msgs 
)
1
2
3
4

You don't need to add your previous message into this block.

# test if our custom message works

go back to your catkin workspace folder, do a

catkin_make
1

TIP

remember to source your bash/zsh file again (might not needed, but I had to do it)

source devel/setup.bash
# or source devel/setup.zsh 
# if you are using zsh
1
2
3

now we can run a simple rosmsg command to see if our custom message is working or not.

rosmsg show beginner_tutorials/Control_msgs
1

and the output should be:

float32 left_vel
float32 right_vel
float32 shoulder_vel
float32 elbow_vel
1
2
3
4

We are done with our custom messages