ROS2 Diagnostics
This ROS2 Diagnostics skill provides a Clean Architecture implementation for monitoring component health across Python and C++ nodes using the diagnostic_updater package. Use it when building ROS2 systems that require standardized health reporting, frequency monitoring, and diagnostic aggregation across distributed components. The skill demonstrates domain entity definitions for health states, infrastructure layers that bridge ROS2 diagnostics tools, and patterns for registering custom health checks and frequency monitors.
git clone --depth 1 https://github.com/harunkurtdev/ros2-claude-code-template /tmp/ros2-diagnostics && cp -r /tmp/ros2-diagnostics/.claude/skills/ros2_diagnostics ~/.claude/skills/ros2-diagnosticsSKILL.md
# ROS2 Diagnostics Skill
This skill demonstrates how to integrate standard ROS2 diagnostics tools for health monitoring within a Clean Architecture.
## Domain Layer
```python
# domain/entities/health.py
class HealthLevel(Enum):
OK = 0
WARN = 1
ERROR = 2
STALE = 3
@dataclass
class ComponentHealth:
name: str
level: HealthLevel
message: str
values: dict
```
## Infrastructure Layer (Python)
See previous Python example using `diagnostic_updater`.
## Infrastructure Layer (C++)
Using `diagnostic_updater` package in C++.
```cpp
// infrastructure/ros2/diagnostics/diagnostics_manager.hpp
#pragma once
#include <rclcpp/rclcpp.hpp>
#include <diagnostic_updater/diagnostic_updater.hpp>
#include "domain/interfaces/diagnostics_port.hpp"
namespace infrastructure::ros2::diagnostics {
class DiagnosticsManager {
public:
explicit DiagnosticsManager(rclcpp::Node::SharedPtr node)
: node_(node), updater_(node) {
updater_.setHardwareID(node->get_name());
}
void register_monitor(const std::string& name,
std::function<void(diagnostic_updater::DiagnosticStatusWrapper&)> callback) {
updater_.add(name, callback);
}
// Example callback wrapper for domain entities
void check_component(diagnostic_updater::DiagnosticStatusWrapper& stat) {
// Retrieve health from domain service
// auto health = domain_service_->get_health();
// stat.summary(health.level, health.message);
// stat.add("temp", health.value);
}
private:
rclcpp::Node::SharedPtr node_;
diagnostic_updater::Updater updater_;
};
} // namespace
```
### Frequency Status Example (C++)
```cpp
// infrastructure/ros2/diagnostics/frequency_monitor.hpp
#include <diagnostic_updater/publisher.hpp> // For TopicDiagnostic
class FrequencyMonitor {
public:
FrequencyMonitor(diagnostic_updater::Updater& updater,
const std::string& topic_name,
double min_freq, double max_freq) {
diagnostic_updater::FrequencyStatusParam freq_param(&min_freq, &max_freq, 0.1, 10);
monitor_ = std::make_unique<diagnostic_updater::HeaderlessTopicDiagnostic>(
topic_name, updater, freq_param);
}
void tick() {
monitor_->tick();
}
private:
std::unique_ptr<diagnostic_updater::HeaderlessTopicDiagnostic> monitor_;
};
```
## Use Case Integration
```cpp
// application/services/motor_controller.cpp
void MotorController::check_temp(diagnostic_updater::DiagnosticStatusWrapper& stat) {
double temp = read_temp();
if (temp > 80.0) {
stat.summary(diagnostic_msgs::msg::DiagnosticStatus::ERROR, "Overheating");
} else {
stat.summary(diagnostic_msgs::msg::DiagnosticStatus::OK, "Normal");
}
stat.add("temp", temp);
}
```
## Best Practices
1. **Hardware ID**: Always set a unique Hardware ID in the updater.
2. **Frequency Monitoring**: Use `TopicDiagnostic` to monitor publication rates.
3. **Meaningful Messages**: Provide descriptive error messages.
4. **Standard Levels**: Stick to OK, WARN, ERROR, STALE semantics.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).
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.
Build the colcon workspace (optionally a single package) and report the outcome.
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.
Author a new skill under .claude/skills/<name>/SKILL.md with valid frontmatter, following the template conventions, and index it in CLAUDE.md + README.md.
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.
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).
Configure and build gz-sim from source using either CMake/Ninja directly or a colcon workspace. Trigger when the user asks to build, recompile, configure, or set up the gz-sim binary tree.