ros_package_msgs conventions

Folder Structure

We define custom messages, services and actions in a separate package, with a package name ending on _msgs, to avoid build problems. This package has the following folder structure:

ros_package/
|   CMakeLists.txt
|   package.xml
|
└───srv/
|   |   custom_service_definition.srv
|
└───msg/
└───action/
  • The structure is very similar to a normal ROS package.

  • There is a srv, msg and/or action directory for custom service/message/action definitions.

CMakeLists.txt

Also the CMakeLists.txt file is very similar. This shows an example where only custom services are generated, but the procedure for custom messages or actions is very similar:

 1# SPDX-FileCopyrightText: Alliander N. V.
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4
 5cmake_minimum_required(VERSION 3.5)
 6project(ros_package)
 7
 8# CMake dependencies:
 9find_package(ament_cmake REQUIRED)
10find_package(rosidl_default_generators REQUIRED)
11
12# Other dependencies:
13find_package(geometry_msgs REQUIRED)
14find_package(vision_msgs REQUIRED)
15
16# Service definitions:
17file(GLOB SRVS CONFIGURE_DEPENDS srv/*.srv*)
18
19foreach(file IN LISTS SRVS)
20  string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" file_relative ${file})
21  list(APPEND SRVS_STRIPPED ${file_relative})
22endforeach()
23
24rosidl_generate_interfaces(${PROJECT_NAME}
25  ${SRVS_STRIPPED}
26  DEPENDENCIES geometry_msgs vision_msgs
27)
28
29# Default test:
30if(BUILD_TESTING)
31  find_package(ament_lint_auto REQUIRED)
32  ament_lint_auto_find_test_dependencies()
33endif()
34
35ament_package()

10:
The rosidl_default_generators dependency is now required, to generated the custom messages.

16-22:
We use CMake’s GLOB method to automatically obtain all the srv files.

24-27:
We generate the custom service and link the dependencies (if any).

package.xml

To generate custom messages successfully, we also need to specify the following dependencies in the package.xml file:

  <buildtool_depend>rosidl_default_generators</buildtool_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>