home / skills / zeeshan080 / ai-native-robotics / ros2-patterns

ros2-patterns skill

/.claude/skills/ros2-patterns

This skill helps you implement ROS 2 node patterns with templates, error handling, and clear docstrings for robust robotics development.

npx playbooks add skill zeeshan080/ai-native-robotics --skill ros2-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
1.3 KB
---
name: ros2-patterns
description: ROS 2 node templates and message types for robotics development. Use when creating ROS 2 nodes, understanding message types, or writing publisher/subscriber patterns.
allowed-tools: Read, Bash
---

# ROS 2 Patterns

## Instructions

When creating ROS 2 code:

1. Use the provided templates as starting points
2. Follow ROS 2 Humble conventions
3. Include proper error handling
4. Add docstrings and comments

## Common Patterns

| Pattern | Template | Use Case |
|---------|----------|----------|
| Publisher | publisher.py | Sending data to topics |
| Subscriber | subscriber.py | Receiving data from topics |
| Service Server | (extend publisher) | Request/response pattern |
| Action Server | (advanced) | Long-running tasks |

## Node Structure

```python
import rclpy
from rclpy.node import Node

class MyNode(Node):
    def __init__(self):
        super().__init__('node_name')
        # Initialize publishers, subscribers, timers

    def callback(self, msg):
        # Handle incoming messages
        pass

def main(args=None):
    rclpy.init(args=args)
    node = MyNode()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
```

## Reference

See [messages.md](messages.md) for common message types.
See [templates/](templates/) for complete templates.

Overview

This skill provides ROS 2 node templates and common message type patterns for building reliable robotics software. It accelerates development by offering publisher, subscriber, service, and action scaffolds that follow ROS 2 Humble conventions. The content emphasizes error handling, documentation, and extendable Python examples.

How this skill works

The skill supplies ready-to-use Python templates for common ROS 2 patterns: publishers, subscribers, service servers, and action servers. It outlines node structure, initialization, spinning, and clean shutdown, and points to message type guidance for selecting appropriate ROS messages. Developers copy a template, adapt callbacks and topics, and add error handling and docstrings to match their application.

When to use it

  • Starting a new ROS 2 node or package
  • Implementing publisher/subscriber communication quickly
  • Adding request/response behavior with service servers
  • Creating long-running tasks with action servers
  • Learning ROS 2 Humble node lifecycle and conventions

Best practices

  • Begin from the provided templates to ensure consistent structure and naming
  • Follow ROS 2 Humble conventions for node names, QoS, and topic design
  • Always add try/except blocks around I/O or hardware access and log meaningful errors
  • Include clear docstrings and inline comments for callbacks and message flows
  • Use appropriate QoS settings for reliability and latency requirements
  • Destroy nodes and call rclpy.shutdown() to ensure graceful termination

Example use cases

  • A sensor node publishing range or IMU data at a fixed rate using the publisher template
  • A perception component subscribing to sensor topics and publishing detected objects
  • A control interface exposing commands via a service server for single-shot requests
  • A motion planner running as an action server to manage goal preemption and feedback
  • Educational examples demonstrating message type choices and callback patterns

FAQ

Can I modify templates to add custom message types?

Yes. Replace the example message imports with your custom message packages and update the topic payload fields accordingly. Ensure the message package is listed as a dependency.

Do templates handle node shutdown correctly?

Templates show proper node.destroy_node() and rclpy.shutdown() usage. Add cleanup for hardware or threads inside try/finally blocks if your node opens resources.