Skip to main content
ClaudeWave
Skill200 repo starsupdated 4d ago

ROS2 Service & Action

ROS2 Service and Action implementation with Clean Architecture (Python & C++)

Install in Claude Code
Copy
git clone --depth 1 https://github.com/harunkurtdev/ros2-claude-code-template /tmp/ros2-service-action && cp -r /tmp/ros2-service-action/.claude/skills/ros2_service_action ~/.claude/skills/ros2-service-action
Then start a new Claude Code session; the skill loads automatically.

SKILL.md

# ROS2 Service & Action Skill

This skill provides a guide for implementing ROS2 Services and Actions adhering to Clean Architecture principles.

## Service Implementation

### 1. Service Definition (.srv)

```
# srv/SetRobotMode.srv
# Request
string mode           # "idle", "active", "emergency"
bool force_change     # Force mode change

---

# Response
bool success
string message
string previous_mode
```

### 2. Infrastructure Layer - Service Server (Python)

See previous Python example.

### 3. Infrastructure Layer - Service Server (C++)

```cpp
// infrastructure/ros2/services/robot_mode_service.hpp
#pragma once

#include <rclcpp/rclcpp.hpp>
#include "robot_interfaces/srv/set_robot_mode.hpp"
#include "domain/use_cases/set_robot_mode.hpp"

namespace infrastructure::ros2::services {

class RobotModeServiceNode : public rclcpp::Node {
public:
    explicit RobotModeServiceNode(
        std::shared_ptr<domain::use_cases::SetRobotModeUseCase> use_case,
        const rclcpp::NodeOptions& options = rclcpp::NodeOptions());

private:
    void handle_set_mode(
        const std::shared_ptr<robot_interfaces::srv::SetRobotMode::Request> request,
        std::shared_ptr<robot_interfaces::srv::SetRobotMode::Response> response);

    std::shared_ptr<domain::use_cases::SetRobotModeUseCase> use_case_;
    rclcpp::Service<robot_interfaces::srv::SetRobotMode>::SharedPtr service_;
};

} // namespace

// infrastructure/ros2/services/robot_mode_service.cpp
#include "infrastructure/ros2/services/robot_mode_service.hpp"

namespace infrastructure::ros2::services {

RobotModeServiceNode::RobotModeServiceNode(
    std::shared_ptr<domain::use_cases::SetRobotModeUseCase> use_case,
    const rclcpp::NodeOptions& options)
    : Node("robot_mode_service", options), use_case_(use_case) {

    using namespace std::placeholders;
    service_ = this->create_service<robot_interfaces::srv::SetRobotMode>(
        "/robot/set_mode",
        std::bind(&RobotModeServiceNode::handle_set_mode, this, _1, _2)
    );
}

void RobotModeServiceNode::handle_set_mode(
    const std::shared_ptr<robot_interfaces::srv::SetRobotMode::Request> request,
    std::shared_ptr<robot_interfaces::srv::SetRobotMode::Response> response) {

    // Conversion from message to domain object would go here
    // ...

    // Execute use case
    // auto result = use_case_->execute(...);

    // Map result to response
    // response->success = result.success;
}

} // namespace
```

### 4. Service Client (C++)

```cpp
// infrastructure/ros2/clients/robot_mode_client.hpp
#pragma once
#include <rclcpp/rclcpp.hpp>
#include "robot_interfaces/srv/set_robot_mode.hpp"

namespace infrastructure::ros2::clients {

class RobotModeClient {
public:
    explicit RobotModeClient(rclcpp::Node::SharedPtr node);

    std::future<std::shared_ptr<robot_interfaces::srv::SetRobotMode::Response>>
    set_mode_async(const std::string& mode, bool force = false);

private:
    rclcpp::Node::SharedPtr node_;
    rclcpp::Client<robot_interfaces::srv::SetRobotMode>::SharedPtr client_;
};

} // namespace
```

## Action Implementation

### 1. Action Definition (.action)

```
# action/NavigateToPoint.action
# Goal
geometry_msgs/Point target_point
float32 max_velocity
bool avoid_obstacles

---

# Result
bool success
string message
float32 total_distance
float32 total_time

---

# Feedback
geometry_msgs/Point current_position
float32 distance_remaining
float32 estimated_time
string status
```

### 2. Action Server (C++)

```cpp
// infrastructure/ros2/actions/navigation_action_server.hpp
#pragma once

#include <rclcpp/rclcpp.hpp>
#include <rclcpp_action/rclcpp_action.hpp>
#include "robot_interfaces/action/navigate_to_point.hpp"
#include "domain/use_cases/navigate_to_point.hpp"

namespace infrastructure::ros2::actions {

class NavigationActionServer : public rclcpp::Node {
public:
    using NavigateToPoint = robot_interfaces::action::NavigateToPoint;
    using GoalHandle = rclcpp_action::ServerGoalHandle<NavigateToPoint>;

    explicit NavigationActionServer(
        std::shared_ptr<domain::use_cases::NavigateToPointUseCase> use_case,
        const rclcpp::NodeOptions& options = rclcpp::NodeOptions());

private:
    rclcpp_action::GoalResponse handle_goal(
        const rclcpp_action::GoalUUID& uuid,
        std::shared_ptr<const NavigateToPoint::Goal> goal);

    rclcpp_action::CancelResponse handle_cancel(
        const std::shared_ptr<GoalHandle> goal_handle);

    void handle_accepted(const std::shared_ptr<GoalHandle> goal_handle);
    void execute(const std::shared_ptr<GoalHandle> goal_handle);

    std::shared_ptr<domain::use_cases::NavigateToPointUseCase> use_case_;
    rclcpp_action::Server<NavigateToPoint>::SharedPtr action_server_;
};

} // namespace
```

### 3. Action Client (C++)

```cpp
// infrastructure/ros2/clients/navigation_client.hpp
#pragma once

#include <rclcpp/rclcpp.hpp>
#include <rclcpp_action/rclcpp_action.hpp>
#include "robot_interfaces/action/navigate_to_point.hpp"

namespace infrastructure::ros2::clients {

class NavigationClient {
public:
    using NavigateToPoint = robot_interfaces::action::NavigateToPoint;
    using GoalHandle = rclcpp_action::ClientGoalHandle<NavigateToPoint>;

    explicit NavigationClient(rclcpp::Node::SharedPtr node);

    std::shared_future<std::shared_ptr<GoalHandle>> navigate_to(
        double x, double y, double z,
        std::function<void(const NavigateToPoint::Feedback&)> feedback_cb);

private:
    rclcpp::Node::SharedPtr node_;
    rclcpp_action::Client<NavigateToPoint>::SharedPtr client_;
};

} // namespace
```
behaviortree-reviewerSubagent

Use proactively before opening a PR that adds or changes BehaviorTree.CPP nodes or BehaviorTree.ROS2 wrappers (RosActionNode/RosServiceNode/RosTopicPub/SubNode, TreeExecutionServer). Reviews a diff against BT.CPP v4 conventions — node base-class choice, non-blocking ticks, ports/blackboard typing, factory/plugin registration, XML v4, and the ROS 2 wrapper contract. Returns a punch list with file:line anchors, not a rewrite.

clean-arch-architectSubagent

Use when a design decision touches Clean Architecture boundaries in a ROS 2 project — which layer a new behaviour belongs to, whether a port belongs in domain or application, whether a new node should be lifecycle-managed, whether to compose nodes or split packages. Returns an architectural recommendation with trade-offs, not implementation.

ecs-architectSubagent

Use when a design decision touches the gz-sim ECS — where new state should live, which system phase should write it, how to avoid coupling, whether to add a component vs. a member variable, whether a new system should be split or merged with an existing one. Returns an architectural recommendation with trade-offs, not implementation.

gz-style-reviewerSubagent

Use proactively before opening any gz-sim PR. Reviews a diff against the project's C++17 style, ECS conventions, plugin registration patterns, CMake structure, test placement, Migration.md / Changelog.md expectations, and pre-commit configuration. Returns a punch list, not a rewrite.

ros2-controllers-reviewerSubagent

Use proactively before opening a PR that adds or changes a ros2_control controller, broadcaster, or hardware component (incl. URDF <ros2_control> bringup). Reviews a diff against ros2_controllers / ros2_control_demos conventions — controller & hardware lifecycle, command/state interface configuration, real-time safety of update()/read()/write(), generate_parameter_library usage, pluginlib registration, chainable-controller correctness, URDF wiring, and tests. Returns a punch list with file:line anchors, not a rewrite.

ros2-style-reviewerSubagent

Use proactively before opening any ROS 2 / Nav 2 PR. Reviews a diff against this template's Clean Architecture, ROS 2 communication, lifecycle, testing, and Nav 2 plugin conventions. Returns a punch list with file:line anchors, not a rewrite.

vda5050-reviewerSubagent

Use proactively before opening a PR that touches a VDA 5050 connector / fleet bridge. Reviews a diff against VDA 5050 v3.0.0 protocol compliance (topics, QoS, header rules, base/horizon, action state machine, schema validation) and the template's Clean Architecture for the MQTT↔Nav 2 bridge. Returns a punch list with file:line anchors, not a rewrite.

buildSlash Command

Build the colcon workspace (optionally a single package) and report the outcome.