|
1 | 1 | # Actions |
2 | 2 |
|
3 | | -Actions provide a mechanism for long-running tasks in ROS 2, with the ability to: |
| 3 | +**Actions enable long-running tasks with progress feedback and cancellation support, perfect for operations that take seconds or minutes to complete.** Unlike services that return immediately, actions provide streaming feedback while executing complex workflows. |
4 | 4 |
|
5 | | -- Send goals to an action server |
6 | | -- Receive feedback during execution |
7 | | -- Cancel goals in progress |
8 | | -- Get final results |
| 5 | +```admonish tip |
| 6 | +Use actions for robot navigation, trajectory execution, or any operation where you need progress updates and the ability to cancel mid-execution. Use services for quick request-response operations. |
| 7 | +``` |
9 | 8 |
|
10 | | -## Overview |
| 9 | +## Action Lifecycle |
11 | 10 |
|
12 | | -Actions are useful for tasks that: |
| 11 | +```mermaid |
| 12 | +stateDiagram-v2 |
| 13 | + [*] --> Idle |
| 14 | + Idle --> Accepted: Send Goal |
| 15 | + Accepted --> Executing: Start Processing |
| 16 | + Executing --> Executing: Send Feedback |
| 17 | + Executing --> Succeeded: Complete |
| 18 | + Executing --> Canceled: Cancel Request |
| 19 | + Executing --> Aborted: Error Occurs |
| 20 | + Succeeded --> [*] |
| 21 | + Canceled --> [*] |
| 22 | + Aborted --> [*] |
| 23 | +``` |
13 | 24 |
|
14 | | -- Take a long time to complete |
15 | | -- Need to provide progress updates |
16 | | -- May need to be cancelled |
| 25 | +## Components |
17 | 26 |
|
18 | | -Common examples include: |
| 27 | +| Component | Type | Purpose | |
| 28 | +|-----------|------|---------| |
| 29 | +| **Goal** | Input | Defines the desired outcome | |
| 30 | +| **Feedback** | Stream | Progress updates during execution | |
| 31 | +| **Result** | Output | Final outcome when complete | |
| 32 | +| **Status** | State | Current execution state | |
19 | 33 |
|
20 | | -- Navigation to a goal |
21 | | -- Gripper control |
22 | | -- Long computations |
| 34 | +## Communication Pattern |
23 | 35 |
|
24 | | -## Components |
| 36 | +```mermaid |
| 37 | +sequenceDiagram |
| 38 | + participant C as Client |
| 39 | + participant S as Server |
| 40 | +
|
| 41 | + C->>S: Send Goal |
| 42 | + S->>C: Goal Accepted |
| 43 | + loop During Execution |
| 44 | + S->>C: Feedback Update |
| 45 | + end |
| 46 | + alt Success |
| 47 | + S->>C: Result (Success) |
| 48 | + else Canceled |
| 49 | + C->>S: Cancel Request |
| 50 | + S->>C: Result (Canceled) |
| 51 | + else Error |
| 52 | + S->>C: Result (Aborted) |
| 53 | + end |
| 54 | +``` |
| 55 | + |
| 56 | +## Use Cases |
| 57 | + |
| 58 | +**Robot Navigation:** |
| 59 | + |
| 60 | +- Goal: Target position and orientation |
| 61 | +- Feedback: Current position, distance remaining, obstacles detected |
| 62 | +- Result: Final position, success/failure reason |
| 63 | + |
| 64 | +**Gripper Control:** |
| 65 | + |
| 66 | +- Goal: Desired grip force and position |
| 67 | +- Feedback: Current force, contact detection |
| 68 | +- Result: Grip achieved, object secured |
| 69 | + |
| 70 | +**Long Computations:** |
| 71 | + |
| 72 | +- Goal: Computation parameters |
| 73 | +- Feedback: Progress percentage, intermediate results |
| 74 | +- Result: Final computed value, execution time |
| 75 | + |
| 76 | +```admonish info |
| 77 | +Actions excel when operations take more than a few seconds and users need visibility into progress. For sub-second operations, prefer services for simplicity. |
| 78 | +``` |
| 79 | + |
| 80 | +## Implementation Status |
| 81 | + |
| 82 | +```admonish note |
| 83 | +Action support in ros-z is under active development. The core infrastructure is in place, and examples are available in the repository. Check the latest API documentation for implementation details. |
| 84 | +``` |
| 85 | + |
| 86 | +## Example Patterns |
| 87 | + |
| 88 | +**Action Server:** |
| 89 | + |
| 90 | +```rust,ignore |
| 91 | +let action_server = node |
| 92 | + .create_action_server::<Fibonacci>("/fibonacci") |
| 93 | + .build()?; |
| 94 | +
|
| 95 | +loop { |
| 96 | + let goal = action_server.accept_goal()?; |
| 97 | +
|
| 98 | + // Send periodic feedback |
| 99 | + for i in 0..goal.order { |
| 100 | + action_server.send_feedback(FeedbackMsg { |
| 101 | + current: i, |
| 102 | + sequence: compute_partial(i) |
| 103 | + })?; |
| 104 | + } |
| 105 | +
|
| 106 | + // Send final result |
| 107 | + action_server.send_result(ResultMsg { |
| 108 | + sequence: compute_final(goal.order) |
| 109 | + })?; |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +**Action Client:** |
| 114 | + |
| 115 | +```rust,ignore |
| 116 | +let action_client = node |
| 117 | + .create_action_client::<Fibonacci>("/fibonacci") |
| 118 | + .build()?; |
| 119 | +
|
| 120 | +let goal_handle = action_client.send_goal(GoalMsg { |
| 121 | + order: 10 |
| 122 | +}).await?; |
| 123 | +
|
| 124 | +while let Some(feedback) = goal_handle.feedback().await { |
| 125 | + println!("Progress: {}", feedback.current); |
| 126 | +} |
25 | 127 |
|
26 | | -An action consists of: |
| 128 | +let result = goal_handle.get_result().await?; |
| 129 | +println!("Final: {:?}", result.sequence); |
| 130 | +``` |
27 | 131 |
|
28 | | -1. **Goal**: The desired outcome |
29 | | -2. **Feedback**: Progress updates during execution |
30 | | -3. **Result**: The final outcome |
| 132 | +```admonish warning |
| 133 | +Always implement timeout mechanisms for action clients. Long-running actions can fail or hang, and clients need graceful degradation strategies. |
| 134 | +``` |
31 | 135 |
|
32 | | -## Implementation |
| 136 | +## Comparison with Other Patterns |
33 | 137 |
|
34 | | -Action support in ros-z is under active development. Check the examples directory and API documentation for the latest implementation details. |
| 138 | +| Pattern | Duration | Feedback | Cancellation | Use Case | |
| 139 | +|---------|----------|----------|--------------|----------| |
| 140 | +| **Pub-Sub** | Continuous | No | N/A | Sensor data streaming | |
| 141 | +| **Service** | < 1 second | No | No | Quick queries | |
| 142 | +| **Action** | Seconds to minutes | Yes | Yes | Long-running tasks | |
35 | 143 |
|
36 | | -## Example Use Cases |
| 144 | +## Resources |
37 | 145 |
|
38 | | -- Robot navigation with progress updates |
39 | | -- Trajectory execution with real-time feedback |
40 | | -- Complex multi-step operations |
| 146 | +- **[ROS 2 Actions Documentation](https://docs.ros.org/en/rolling/Tutorials/Intermediate/Writing-an-Action-Server-Client/Py.html)** - Official ROS 2 action guide |
| 147 | +- **[ros-z Examples](https://github.com/ZettaScaleLabs/ros-z/tree/main/ros-z/examples)** - Working action implementations |
| 148 | +- **[Services](./services.md)** - Simpler request-response pattern |
41 | 149 |
|
42 | | -For concrete examples, see the ros-z examples directory. |
| 150 | +**Action implementation is evolving. Check the ros-z repository for the latest examples and API updates.** |
0 commit comments