I have a datatype called alpha which is based on float64. Now I want to create a custom ROS2 message which has this datatype instead of adding float64. Is there a way to do this?

My code would look like:

alpha number;
msg.data = number;

I want my msg type to be:

alpha data

2 Answers 2


Custom ROS 2 messages are usually created using the basic message types which are supported by ROS 2. I don't think you can directly use your Alpha datatype to create a custom ROS 2 message.

However, you can use the basic datatypes which are provided to you by ROS 2 to recreate your Alpha datatype and then use that to create the custom datatype you want.


For example, you can create a ROS 2 package with a directory structure similar to the one below.

└── alpha_msgs
    ├── CMakeLists.txt
    ├── include
    ├── msg
    │   ├── Alpha.msg
    │   └── AlphaArray.msg
    ├── package.xml
    └── src

Let's say you want to create an Alpha array datatype called AlphaArray. You can first define your alpha datatype from scratch.

# Alpha.msg
float64 direction
float64 magnitude

Then you would use the custom Alpha datatype to create another custom array datatype such as AlphaArray.

# AlphaArray.msg
Alpha[] data    # An array of Alpha.msg types

Then it's just a matter of specifying how your package should be built in the CMakeLists.txt and adding some dependencies to the package.xml file. The CMakeLists.txt would look something like this.

cmake_minimum_required(VERSION 3.8)

# some ROS 2 CMakeLists.txt template code

# find important dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rosidl_default_generators REQUIRED)

# find dependencies needed for your datatype
find_package(std_msgs REQUIRED)

# you can use this to get all msg files if you have many

# generate code for message types
    DEPENDENCIES std_msgs

# generate code for message types by manually
# specifying all the message types
# rosidl_generate_interfaces(${PROJECT_NAME}
#    msg/Alpha.msg
#    msg/AlphaArray.msg
#    DEPENDENCIES std_msgs
# )

# rest of ROS 2 CMakeLists.txt template code


And your package.xml would usually include the following lines.




For more information, you can look at the px4_msgs GitHub repository. It has a lot of custom message examples.

I hope this helps. If I have made any mistakes or if you have any questions, let me know.

  • $\begingroup$ Thanks for the detail information. Yeah I have a configuration which is the reason for me to use the custom datatype in ROS2 message. I will figure out if there is something. Thanks. $\endgroup$
    – Rahul
    Commented Mar 22 at 12:30
  1. Create custom message(alpha.msg) with the data type as needed.
float32 alpha_message
  1. Create custom message(beta.msg) where you want to import
<package_name>/alpha beta_message
  • $\begingroup$ Please use code formatting , not block-quote formatting $\endgroup$ Commented Mar 20 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.