Skip to content

Commit a53c1d0

Browse files
authored
Add README for each example (#1199)
This PR adds comprehensive README documentation for each example directory, providing users with clear instructions, purposes, and code snippets for running and understanding the examples. - Introduce README files for all example categories (topics, services, rosidl, rate, parameter, lifecycle, graph, actions) - Detail purpose, functionality, features, run commands, and expected outputs for each example - Include key concepts, troubleshooting tips, and advanced patterns in the documentation Fix: #1198
1 parent d175ce4 commit a53c1d0

File tree

8 files changed

+2611
-0
lines changed

8 files changed

+2611
-0
lines changed

example/actions/README.md

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
# ROS 2 Actions Examples
2+
3+
This directory contains examples demonstrating ROS 2 action-based communication using rclnodejs. Actions provide a more complex communication pattern than topics or services, designed for long-running tasks that require feedback and can be canceled.
4+
5+
## Overview
6+
7+
ROS 2 actions are built on top of topics and services to provide:
8+
9+
- **Goal**: A request to perform some long-running task
10+
- **Feedback**: Periodic updates on progress during task execution
11+
- **Result**: The final outcome when the task completes
12+
- **Cancellation**: Ability to stop the task before completion
13+
14+
Actions are ideal for:
15+
16+
- Navigation tasks (moving a robot to a location)
17+
- Long-running computations
18+
- Tasks that need progress updates
19+
- Operations that might need to be canceled
20+
21+
## Action Examples
22+
23+
### Action Client Examples
24+
25+
The `action_client/` directory contains examples of nodes that send goals to action servers:
26+
27+
#### 1. Basic Action Client (`action-client-example.js`)
28+
29+
**Purpose**: Demonstrates basic action client functionality with the Fibonacci action.
30+
31+
- **Action Type**: `test_msgs/action/Fibonacci`
32+
- **Action Name**: `fibonacci`
33+
- **Functionality**:
34+
- Sends a goal to compute Fibonacci sequence up to order 10
35+
- Receives and logs feedback during execution
36+
- Waits for final result and logs success/failure
37+
- Automatically shuts down after completion
38+
- **Features**:
39+
- Server availability checking with `waitForServer()`
40+
- Goal acceptance verification
41+
- Feedback handling during execution
42+
- Result processing with status checking
43+
- **Run Command**: `node action_client/action-client-example.js`
44+
45+
#### 2. Action Client with Cancellation (`action-client-cancel-example.js`)
46+
47+
**Purpose**: Demonstrates how to cancel an action goal during execution.
48+
49+
- **Action Type**: `test_msgs/action/Fibonacci`
50+
- **Action Name**: `fibonacci`
51+
- **Functionality**:
52+
- Sends a goal to compute Fibonacci sequence up to order 10
53+
- Receives feedback for 2 seconds
54+
- Automatically cancels the goal after timer expires
55+
- Logs cancellation success/failure
56+
- **Features**:
57+
- Timer-based cancellation mechanism
58+
- Goal cancellation with `cancelGoal()`
59+
- Cancellation response verification
60+
- Cleanup and shutdown handling
61+
- **Run Command**: `node action_client/action-client-cancel-example.js`
62+
63+
### Action Server Examples
64+
65+
The `action_server/` directory contains examples of nodes that provide action services:
66+
67+
#### 1. Basic Action Server (`action-server-example.js`)
68+
69+
**Purpose**: Demonstrates basic action server implementation for computing Fibonacci sequences.
70+
71+
- **Action Type**: `test_msgs/action/Fibonacci`
72+
- **Action Name**: `fibonacci`
73+
- **Functionality**:
74+
- Accepts goals to compute Fibonacci sequences
75+
- Publishes feedback with incremental sequence updates
76+
- Returns complete sequence as result
77+
- Supports goal cancellation during execution
78+
- **Features**:
79+
- Goal acceptance callback (`goalCallback`)
80+
- Execution callback with feedback publishing
81+
- Cancellation handling (`cancelCallback`)
82+
- Progress updates every second
83+
- **Run Command**: `node action_server/action-server-example.js`
84+
85+
#### 2. Deferred Execution Server (`action-server-defer-example.js`)
86+
87+
**Purpose**: Shows how to defer goal execution using timers and handle accepted callbacks.
88+
89+
- **Action Type**: `test_msgs/action/Fibonacci`
90+
- **Action Name**: `fibonacci`
91+
- **Functionality**:
92+
- Accepts goals immediately but defers execution for 3 seconds
93+
- Uses timer to delay goal execution start
94+
- Demonstrates deferred execution pattern
95+
- Same Fibonacci computation as basic server
96+
- **Features**:
97+
- Goal acceptance with deferred execution
98+
- Handle accepted callback (`handleAcceptedCallback`)
99+
- Timer-based execution control
100+
- Manual goal execution triggering
101+
- **Run Command**: `node action_server/action-server-defer-example.js`
102+
103+
#### 3. Single Goal Server (`action-server-single-goal-example.js`)
104+
105+
**Purpose**: Demonstrates a server that only allows one active goal at a time.
106+
107+
- **Action Type**: `test_msgs/action/Fibonacci`
108+
- **Action Name**: `fibonacci`
109+
- **Functionality**:
110+
- Accepts new goals but aborts any currently active goal
111+
- Ensures only one goal executes at a time
112+
- Tracks active goal state
113+
- Same Fibonacci computation with single-goal constraint
114+
- **Features**:
115+
- Single goal enforcement
116+
- Automatic abortion of previous goals
117+
- Goal state tracking (`isActive`)
118+
- Handle accepted callback for goal management
119+
- **Run Command**: `node action_server/action-server-single-goal-example.js`
120+
121+
## How to Run the Examples
122+
123+
### Prerequisites
124+
125+
1. Ensure ROS 2 is installed and sourced
126+
2. Navigate to the `example/actions` directory
127+
128+
### Running Complete Action Examples
129+
130+
#### Basic Action Communication
131+
132+
1. **Start the Action Server**: In one terminal, run:
133+
134+
```bash
135+
cd example/actions
136+
node action_server/action-server-example.js
137+
```
138+
139+
You should see:
140+
141+
```
142+
[INFO] [action_server_example_node]: Action server started
143+
```
144+
145+
2. **Start the Action Client**: In another terminal, run:
146+
147+
```bash
148+
cd example/actions
149+
node action_client/action-client-example.js
150+
```
151+
152+
3. **Expected Output**:
153+
154+
**Action Server Terminal**:
155+
156+
```
157+
[INFO] [action_server_example_node]: Received goal request
158+
[INFO] [action_server_example_node]: Executing goal...
159+
[INFO] [action_server_example_node]: Publishing feedback: 0,1
160+
[INFO] [action_server_example_node]: Publishing feedback: 0,1,1
161+
[INFO] [action_server_example_node]: Publishing feedback: 0,1,1,2
162+
...
163+
[INFO] [action_server_example_node]: Returning result: 0,1,1,2,3,5,8,13,21,34,55
164+
```
165+
166+
**Action Client Terminal**:
167+
168+
```
169+
[INFO] [action_client_example_node]: Waiting for action server...
170+
[INFO] [action_client_example_node]: Sending goal request...
171+
[INFO] [action_client_example_node]: Goal accepted
172+
[INFO] [action_client_example_node]: Received feedback: 0,1
173+
[INFO] [action_client_example_node]: Received feedback: 0,1,1
174+
...
175+
[INFO] [action_client_example_node]: Goal succeeded with result: 0,1,1,2,3,5,8,13,21,34,55
176+
```
177+
178+
#### Action Cancellation Example
179+
180+
1. **Start Server**: Run any action server example
181+
2. **Start Cancellation Client**:
182+
```bash
183+
node action_client/action-client-cancel-example.js
184+
```
185+
3. **Expected Behavior**: Client sends goal, receives feedback for 2 seconds, then cancels
186+
187+
#### Specialized Server Examples
188+
189+
- **Deferred Execution**: Use `action-server-defer-example.js` to see 3-second execution delay
190+
- **Single Goal**: Use `action-server-single-goal-example.js` to test goal abortion behavior
191+
192+
## Action Components Explained
193+
194+
### Action Message Structure
195+
196+
The `test_msgs/action/Fibonacci` action consists of:
197+
198+
```
199+
# Goal
200+
int32 order
201+
---
202+
# Result
203+
int32[] sequence
204+
---
205+
# Feedback
206+
int32[] sequence
207+
```
208+
209+
### Key Concepts Demonstrated
210+
211+
#### Action Client Concepts
212+
213+
- **Goal Sending**: Using `sendGoal()` with feedback callbacks
214+
- **Server Discovery**: Waiting for action servers with `waitForServer()`
215+
- **Goal Status**: Checking acceptance and completion status
216+
- **Feedback Handling**: Processing incremental updates during execution
217+
- **Result Processing**: Handling final results and status
218+
- **Goal Cancellation**: Canceling active goals with `cancelGoal()`
219+
220+
#### Action Server Concepts
221+
222+
- **Goal Callbacks**: Accepting or rejecting incoming goals
223+
- **Execution Callbacks**: Implementing the actual action logic
224+
- **Feedback Publishing**: Sending progress updates to clients
225+
- **Result Handling**: Returning final results upon completion
226+
- **Cancellation Support**: Responding to cancellation requests
227+
- **Goal State Management**: Tracking active goals and their status
228+
229+
#### Advanced Patterns
230+
231+
- **Deferred Execution**: Accepting goals but delaying execution start
232+
- **Single Goal Servers**: Limiting concurrent goal execution
233+
- **Goal Abortion**: Terminating active goals when new ones arrive
234+
- **Timer Integration**: Using ROS timers for delayed operations
235+
236+
## Programming Patterns
237+
238+
### Class-Based Architecture
239+
240+
All examples use ES6 classes to encapsulate action functionality:
241+
242+
- Clean separation of client/server logic
243+
- Proper callback binding with `bind(this)`
244+
- State management through instance variables
245+
246+
### Asynchronous Operations
247+
248+
- **Async/Await**: Modern JavaScript patterns for goal handling
249+
- **Promises**: Integration with ROS action lifecycle
250+
- **Callbacks**: Feedback and result processing
251+
252+
### Resource Management
253+
254+
- **Timer Cleanup**: Proper timer cancellation
255+
- **Goal Tracking**: Maintaining references to active goals
256+
- **Shutdown Handling**: Clean node shutdown after completion
257+
258+
## Troubleshooting
259+
260+
### Common Issues
261+
262+
1. **Action Server Not Available**:
263+
264+
- Ensure action server is running before starting client
265+
- Check that both use the same action name (`fibonacci`)
266+
- Verify action type matches (`test_msgs/action/Fibonacci`)
267+
268+
2. **Goal Not Accepted**:
269+
270+
- Check server's `goalCallback` return value
271+
- Verify goal message structure is correct
272+
- Ensure server is properly initialized
273+
274+
3. **Missing Feedback**:
275+
276+
- Confirm feedback callback is properly bound
277+
- Check server's `publishFeedback()` calls
278+
- Verify feedback message structure
279+
280+
4. **Cancellation Issues**:
281+
- Ensure server implements `cancelCallback`
282+
- Check `isCancelRequested` in execution loop
283+
- Verify proper `goalHandle.canceled()` calls
284+
285+
### Debugging Tips
286+
287+
- Use `ros2 action list` to see available actions
288+
- Use `ros2 action info <action_name>` to check action details
289+
- Use `ros2 action send_goal <action_name> <action_type> <goal>` to test from command line
290+
- Monitor action topics: `/_action/status`, `/_action/feedback`, `/_action/result`
291+
292+
## Notes
293+
294+
- All examples use the Fibonacci sequence computation as a representative long-running task
295+
- Action servers run continuously until manually terminated (Ctrl+C)
296+
- Action clients typically complete one goal cycle then exit
297+
- Goals are processed with 1-second intervals to demonstrate feedback clearly
298+
- Cancellation examples use timers to simulate real-world cancellation scenarios
299+
- Single goal servers demonstrate resource management for concurrent requests

0 commit comments

Comments
 (0)