ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2 cannot find custom message C++

asked 2023-05-09 22:43:32 -0500

omgitsmichael gravatar image

Hey! I'm pretty novice when it comes to ROS/ROS2 and CMake stuff so I have been following the guides that ROS2 has provided for when creating packages/nodes/custom messages/etc. Currently I'm following the guide for creating a custom message and when I include it to the include file the custom message cannot be found with the error as follows:

fatal error: messages/msg/AttitudeEstimation.hpp: No such file or directory in line 11 | #include "messages/msg/AttitudeEstimation.hpp

I know this seems to be a pretty similar issue to https://answers.ros.org/question/306568/ros2-custom-messages-not-found/ but their fix doesn't seem to work for me (I am also running with C++ instead of python. Unsure if this makes a difference).

The layout for my workspace I am currently working with is as follows:

/GimbalROS
    /controls
        CMakeLists.txt
        package.xml
        /src
            /nodes
                control_node.cpp
        /include
            /nodes
                control_node.h
    /messages
        CMakeLists.txt
        package.xml
        /msg
            Quaternion.msg
            BodyRates.msg
            AttitudeEstimation.msg

My messages CmakeLists.txt file:

cmake_minimum_required(VERSION 3.8)
project(messages)

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(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  # Estimation messages
  "msg/Quaternion.msg"
  "msg/BodyRates.msg"
  "msg/AttitudeEstimation.msg"
)

ament_export_dependencies(rosidl_default_runtime)

ament_package()

My messages package.xml file:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" 
 schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>messages</name>
  <version>0.0.0</version>
  <description>Gimbal message topics</description>
  <maintainer email="my email">michael</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <buildtool_depend>rosidl_default_generators</buildtool_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

My controls CMakeLists.txt file:

cmake_minimum_required(VERSION 3.8)
project(controls)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(messages REQUIRED)

add_executable(control_node src/nodes/control_node.cpp)

ament_target_dependencies(control_node rclcpp std_msgs messages)

target_include_directories(control_node PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

target_compile_features(control_node PUBLIC c_std_99 cxx_std_17)  # Require C99 and C++17

install(TARGETS control_node
  DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  set(ament_cmake_copyright_FOUND TRUE)
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

and my controls package.xml file:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" 
 schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>controls</name>
  <version>0.0.0</version>
  <description>Gimbal attitude control package</description>
  <maintainer email="my email">michael</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <depend>rclcpp</depend>
  <depend>std_msgs</depend>  <!--remove this line later-->
  <depend>messages</depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

After running colcon build --packages-select messages I then ran source install/setup.bash. After sourcing the setup file I tested to see if my message would pop up in the command tool line ros2 interface list | grep messages. This outputs:

messages/msg/AttitudeEstimation
messages/msg/BodyRates
messages/msg/Quaternion

using ros2 interface show messages/msg ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-10 00:52:52 -0500

omgitsmichael gravatar image

Found out the answer myself. Apparently the msg files get renamed when they turn into hpp files. The correct location for the file was "messages/msg/attitude_estimation.hpp". I don't recall seeing this information on the ROS2 website, but it should maybe be updated to add it/highlight it a bit clearer!

edit flag offensive delete link more

Question Tools

Stats

Asked: 2023-05-09 22:43:32 -0500

Seen: 490 times

Last updated: May 09 '23