Using a custom message in C++ (ROS2 foxy)

asked 2021-11-28 18:15:33 -0600

j1432 gravatar image

I have a project that I am working on where I am (so far) communicating with a microcontroller with one node and sending commands to that node from another node. I started out just using a string to send everything because it was the fastest way to get going but now I am trying to mend my ways and do things properly. It seems that the proper way is to create my own messages, even if I am just sending a single byte that might otherwise be covered by something like std_msgs::int32. That is, of course, unless I am sending data where there is already an established message type for that exact thing.

Anyway, I have followed along with a couple of online tutorials and both of them have allowed me to make a custom message type that I can then publish with using the command line. I can also receive them using the command line. So far so good. Now I want to incorporate this into my programs. I'm getting stuck there though. My understanding is that the build process of the message project should generate a .hpp file that I can use form my own code. It appears that this is how the std_msgs are generated. If I go into /opt/ros/foxy/include/std_msgs/msg/int16.hpp, it has "Generated from rosidl_generator_cpp/resource/idl.hpp.em" in it.

Given that I can publish and subscribe from the command line, I feel like I am 95% of the way through this but am missing something simple.

I am working on a Raspberry Pi 4B using Ubuntu 20.04 server and ROS2 foxy. I am not getting any errors when running colcon build.

If anyone could provide help, I would appreciate it.

Below is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

# Default to C99

# Default to C++14

  add_compile_options(-Wall -Wextra -Wpedantic)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)   # This was added because of the --dependencies when creating the package
find_package(std_msgs REQUIRED) # This was added because of the --dependencies when creating the package
find_package(builtin_interfaces REQUIRED) # ADDED
find_package(rosidl_default_generators REQUIRED) # ADDED

  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)

rosidl_generate_interfaces(${PROJECT_NAME}_msg "msg/SampleMessage.msg" DEPENDENCIES builtin_interfaces LIBRARY_NAME ${PROJECT_NAME})


Below is my package.xml:

<?xml version="1.0"?>
<?xml-model href="" schematypens=""?>
<package format="3">
  <description>TODO: Package description</description>
  <maintainer email="">ubuntu< ...
edit retag flag offensive close merge delete


Interesting question but it’s not clear what you want to resolve. How do you know your custom message is not working?

osilva gravatar image osilva  ( 2021-11-29 20:57:42 -0600 )edit

I have some C++ code running where I can either use a string or a float32_multi_array as my message types. In my C++ code, I have to include their headers:

#include "std_msgs/msg/float32_multi_array.hpp"
#include "std_msgs/msg/string.hpp"

I can't find any header file that is being generated for SampleMessage and trying to ignore that and #include what I think should be there for it or just omitting a #include for it results in compilation errors.

j1432 gravatar image j1432  ( 2021-11-29 21:50:36 -0600 )edit

Thank you for the clarification

osilva gravatar image osilva  ( 2021-11-29 21:53:57 -0600 )edit

Please check this tutorial:

Looks you need to set your message in CMakelists.txt as well

osilva gravatar image osilva  ( 2021-11-29 22:05:45 -0600 )edit

I might be misunderstanding the last comment. The tutorial shows what is basically my last line split into two sections:



I just tried compiling with things like that. It compiled fine but I still don't have the .hpp file anywhere that I can find.

j1432 gravatar image j1432  ( 2021-12-01 20:20:39 -0600 )edit