How to include generated message .h file into C++ (ROS 2)?
I am new to ROS 2, coming from ROS, currently using crystal to try and learn the basics.
I want to just simply create a custom message and use it in the minimal publisher/subscriber example from here, instead of the default string message used there. So far, I have put a message file LNumM.msg
with the content:
int64 num
into the msg folder of the publisher package. Using the below CMakeLists.txt
and package.xml
, the package compiles with colcon build
.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(examples_rclcpp_minimal_publisher)
# 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")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
set(msg_files
"msg/LNumM.msg"
)
rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
DEPENDENCIES std_msgs
)
add_executable(publisher_lambda lambda.cpp)
ament_target_dependencies(publisher_lambda rclcpp std_msgs)
add_executable(publisher_member_function member_function.cpp)
ament_target_dependencies(publisher_member_function rclcpp std_msgs)
add_executable(publisher_not_composable not_composable.cpp)
ament_target_dependencies(publisher_not_composable rclcpp std_msgs)
install(TARGETS
publisher_lambda
publisher_member_function
publisher_not_composable
DESTINATION lib/${PROJECT_NAME}
)
ament_package()
package.xml:
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>examples_rclcpp_minimal_subscriber</name>
<version>0.6.1</version>
<description>Examples of minimal subscribers</description>
<maintainer email="jacob@openrobotics.org">Jacob Perron</maintainer>
<license>Apache License 2.0</license>
<author>Mikael Arguedas</author>
<author>Morgan Quigley</author>
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<build_depend>rclcpp</build_depend>
<build_depend>std_msgs</build_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<exec_depend>rosidl_default_runtime</exec_depend>
<exec_depend>rclcpp</exec_depend>
<exec_depend>std_msgs</exec_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
However, I don't know how to now include the message into the C++ code of the publisher (or subscriber) to actually publish and receive messages of this type. In ROS, the generated C++ header is placed in the devel
folder, and I can simply include it via (adapting a similar ROS 1 example):
#include "beginner_tutorials/LNumM.h"
In the install folder of my colcon workspace, I couldn't find any file called LNumM.h
, although there are many files with similar names such as LNumM_.h
. I know that ROS 2 uses the IDL to encode messages, and there are also several generated idl files in the install folder. But I would expect to get a generated C++ header file as well by using rosidl_generate_interfaces
. I have tried some random things like:
#include "examples_rclcpp_minimal_subscriber/msg/dds_opensplice/LNumM_.h"
But they did not work (and I didn't actually expect them to). Maybe someone can push me in the right direction?