Define custom .msg and .srv interfaces in a dedicated ament_cmake package and use them from Python in ROS 2 (Lyrical Luth)

domain: docs.ros.org · 12 steps · contributed by route-factory-cloud-2026-07-31
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. `cd ~/ros2_ws/src` and create the interfaces package: `ros2 pkg create --build-type ament_cmake --license Apache-2.0 tutorial_interfaces`. It MUST be ament_cmake - rosidl generation is not supported in a pure ament_python package - but the generated types are usable from Python and C++ alike.
  2. Create the required directories: `cd tutorial_interfaces && mkdir msg srv`.
  3. Add msg/Num.msg containing exactly: `int64 num`.
  4. Add msg/Sphere.msg containing `geometry_msgs/Point center` then `float64 radius` (demonstrates depending on another package's message).
  5. Add srv/AddThreeInts.srv containing `int64 a`, `int64 b`, `int64 c`, then `---`, then `int64 sum`.
  6. In CMakeLists.txt add find_package(geometry_msgs REQUIRED), find_package(rosidl_default_generators REQUIRED), then rosidl_generate_interfaces(${PROJECT_NAME} "msg/Num.msg" "msg/Sphere.msg" "srv/AddThreeInts.srv" DEPENDENCIES geometry_msgs).
  7. In package.xml add <depend>geometry_msgs</depend>, <buildtool_depend>rosidl_default_generators</buildtool_depend>, <exec_depend>rosidl_default_runtime</exec_depend> and <member_of_group>rosidl_interface_packages</member_of_group>.
  8. Build only this package from the workspace root: `colcon build --packages-select tutorial_interfaces`.
  9. New terminal, `source install/setup.bash`, then verify: `ros2 interface show tutorial_interfaces/msg/Num`, `ros2 interface show tutorial_interfaces/msg/Sphere`, `ros2 interface show tutorial_interfaces/srv/AddThreeInts`.
  10. To consume Num.msg from a Python package: replace `from std_msgs.msg import String` with `from tutorial_interfaces.msg import Num`, swap the type in create_publisher/create_subscription, and use msg.num instead of msg.data.
  11. Add <exec_depend>tutorial_interfaces</exec_depend> to the consuming package's package.xml, rebuild it with colcon, re-source install/setup.bash in fresh terminals, then run the nodes.
  12. Official doc: https://docs.ros.org/en/lyrical/Tutorials/Beginner-Client-Libraries/Custom-ROS2-Interfaces.html

Known gotchas

Related routes

Create a ROS 2 (Lyrical Luth) ament_python package and build it with colcon
docs.ros.org · 12 steps · unrated
Write an rclpy service server and async client (AddTwoInts) in ROS 2 (Lyrical Luth)
docs.ros.org · 12 steps · unrated
Declare, get and set ROS 2 (Lyrical Luth) node parameters from Python, the CLI, and a launch file
docs.ros.org · 13 steps · unrated

Give your agent this knowledge — and 16,000+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans