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

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)
project(jack_test)

# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
endif()

# 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

if(BUILD_TESTING)
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
# 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)
ament_lint_auto_find_test_dependencies()
endif()

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

ament_package()


Below is my package.xml:

<?xml version="1.0"?>
<package format="3">
<name>jack_test</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="fakeemail@nowhere.com">ubuntu< ...
edit retag 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?

( 2021-11-29 20:57:42 -0500 )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.

( 2021-11-29 21:50:36 -0500 )edit

Thank you for the clarification

( 2021-11-29 21:53:57 -0500 )edit

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

( 2021-11-29 22:05:45 -0500 )edit

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

set(msg_files
"msg/SampleMessage.msg"
)

rosidl_generate_interfaces(${PROJECT_NAME}${msg_files}
)


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.

( 2021-12-01 20:20:39 -0500 )edit