بنقرة واحدة
ros2-messaging-patterns
ROS2 messaging patterns and best practices with Clean Architecture (Python & C++)
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
ROS2 messaging patterns and best practices with Clean Architecture (Python & C++)
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Author a new asset for this .claude/ template — a rule, skill, slash command, or sub-agent — following the project's conventions and wiring it into the CLAUDE.md / README.md indexes. Trigger when the user wants to add or extend a command, skill, agent, or rule (make the template itself extensible).
Bootstrap a complete ROS 2 colcon workspace from scratch — directory layout, .gitignore, top-level README, this .claude/ config, an interfaces package and a first Clean Architecture package, and a bringup package. Trigger when the user asks to create a new workspace / start a new ROS 2 project from zero.
Scaffold a Behavior Tree leaf node — plain BehaviorTree.CPP (SyncActionNode / StatefulActionNode / ConditionNode) or a BehaviorTree.ROS2 wrapper (RosActionNode / RosServiceNode / RosTopicPubNode / RosTopicSubNode) — with ports, factory/plugin registration, and XML v4 usage. Trigger when the user asks to write a behavior-tree node (not Nav 2-specific).
Scaffold a ros2_control hardware component (SystemInterface / ActuatorInterface / SensorInterface) and its bringup — the plugin (on_init/on_configure/on_activate, RT-safe read()/write()), URDF <ros2_control> wiring, controllers.yaml + launch, pluginlib export. Trigger when the user asks to integrate hardware or bring up a robot under ros2_control.
Scaffold or extend a ros2_control controller or broadcaster (ControllerInterface / ChainableControllerInterface) — base-class choice, command/state interface configuration, lifecycle, real-time-safe update(), generate_parameter_library, pluginlib export, tests. Trigger when the user asks to write a ros2_control controller or broadcaster.
Bridge a VDA 5050 v3.0.0 fleet-control interface (MQTT/JSON) onto Nav 2 under Clean Architecture — domain entities, MQTT/Nav 2 adapters behind ports, order→NavigateThroughPoses mapping, state aggregation, action handlers. Trigger when the user asks to build or extend a VDA 5050 connector / fleet bridge.
| name | ROS2 Messaging Patterns |
| description | ROS2 messaging patterns and best practices with Clean Architecture (Python & C++) |
This skill covers ROS2 messaging patterns adhering to Clean Architecture principles, protecting the domain layer from ROS2 dependencies.
See previous Python example.
// infrastructure/ros2/publishers/state_publisher.hpp
#pragma once
#include <rclcpp/rclcpp.hpp>
#include "domain/entities/robot_state.hpp"
#include "robot_interfaces/msg/robot_state.hpp"
namespace infrastructure::ros2::publishers {
template<typename T>
class IStatePublisher {
public:
virtual void publish(const T& state) = 0;
virtual ~IStatePublisher() = default;
};
class RobotStatePublisher : public IStatePublisher<domain::entities::RobotState> {
public:
RobotStatePublisher(rclcpp::Node::SharedPtr node, const std::string& topic, const rclcpp::QoS& qos)
: node_(node) {
publisher_ = node_->create_publisher<robot_interfaces::msg::RobotState>(topic, qos);
}
void publish(const domain::entities::RobotState& state) override {
robot_interfaces::msg::RobotState msg;
msg.mode = static_cast<int>(state.mode);
// ... mapping ...
publisher_->publish(msg);
}
private:
rclcpp::Node::SharedPtr node_;
rclcpp::Publisher<robot_interfaces::msg::RobotState>::SharedPtr publisher_;
};
} // namespace
// infrastructure/ros2/subscribers/base_subscriber.hpp
#pragma once
#include <rclcpp/rclcpp.hpp>
#include <functional>
namespace infrastructure::ros2::subscribers {
template<typename MsgT, typename EntityT>
class BaseSubscriber {
public:
BaseSubscriber(rclcpp::Node::SharedPtr node,
const std::string& topic,
const rclcpp::QoS& qos,
std::function<void(const EntityT&)> callback)
: callback_(callback) {
subscription_ = node->create_subscription<MsgT>(
topic, qos,
[this](const typename MsgT::SharedPtr msg) {
this->handle_message(msg);
}
);
}
virtual ~BaseSubscriber() = default;
protected:
virtual EntityT convert_to_entity(const typename MsgT::SharedPtr msg) = 0;
private:
void handle_message(const typename MsgT::SharedPtr msg) {
auto entity = convert_to_entity(msg);
callback_(entity);
}
rclcpp::Subscription<MsgT>::SharedPtr subscription_;
std::function<void(const EntityT&)> callback_;
};
} // namespace
Using message_filters in C++.
// infrastructure/ros2/sync/time_sync.hpp
#pragma once
#include <rclcpp/rclcpp.hpp>
#include <message_filters/subscriber.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <message_filters/synchronizer.h>
#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/camera_info.hpp>
namespace infrastructure::ros2::sync {
class CameraSync {
public:
CameraSync(rclcpp::Node::SharedPtr node) {
image_sub_.subscribe(node, "image_raw");
info_sub_.subscribe(node, "camera_info");
sync_ = std::make_shared<Sync>(MySyncPolicy(10), image_sub_, info_sub_);
sync_->registerCallback(&CameraSync::callback, this);
}
private:
void callback(const sensor_msgs::msg::Image::ConstSharedPtr& image,
const sensor_msgs::msg::CameraInfo::ConstSharedPtr& info) {
// Handle synchronized messages
}
message_filters::Subscriber<sensor_msgs::msg::Image> image_sub_;
message_filters::Subscriber<sensor_msgs::msg::CameraInfo> info_sub_;
typedef message_filters::sync_policies::ApproximateTime<
sensor_msgs::msg::Image, sensor_msgs::msg::CameraInfo> MySyncPolicy;
typedef message_filters::Synchronizer<MySyncPolicy> Sync;
std::shared_ptr<Sync> sync_;
};
} // namespace
// infrastructure/ros2/events/event_bus.hpp
#pragma once
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
#include <nlohmann/json.hpp> // External dependency
class EventBus {
public:
EventBus(rclcpp::Node::SharedPtr node) {
pub_ = node->create_publisher<std_msgs::msg::String>("/events/all", 10);
// ... subscriber setup ...
}
void emit(const std::string& type, const nlohmann::json& payload) {
std_msgs::msg::String msg;
nlohmann::json data;
data["type"] = type;
data["payload"] = payload;
msg.data = data.dump();
pub_->publish(msg);
}
private:
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_;
};
std::mutex for shared resources in C++ callbacks.