home / skills / zeeshan080 / ai-native-robotics / 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-patternsReview the files below or copy the command above to add this skill to your agents.
---
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.
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.
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.
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.