@@ -60,7 +60,7 @@ Before running this demo, ensure you have:
60601 . ** Ensure ROS2 is sourced** :
6161
6262 ``` bash
63- source /opt/ros/humble /setup.bash # or your ROS2 distribution
63+ source /opt/ros/lyrical /setup.bash # or your ROS2 distribution
6464 ```
6565
66662 . ** Build the main rclnodejs package** (if not already done):
@@ -79,8 +79,13 @@ Before running this demo, ensure you have:
7979
80804 . ** Install dependencies** :
8181
82+ This demo depends on the ** local rclnodejs** in this repository
83+ (` "rclnodejs": "file:../../.." ` ). Install with ` --ignore-scripts ` so npm
84+ links the local package without trying to rebuild its native addon (the
85+ prebuilt binary in the repo is reused):
86+
8287 ``` bash
83- npm install
88+ npm install --ignore-scripts
8489 ```
8590
86915 . ** Build the TypeScript code** :
@@ -194,29 +199,52 @@ You can also test the action server using ROS2 command-line tools:
194199
195200## Understanding the Code
196201
202+ Both the server and client show the ** two equivalent, fully type-safe ways**
203+ to identify an action type:
204+
205+ - ** String name** : pass the type string, e.g. ` 'test_msgs/action/Fibonacci' ` .
206+ - ** Action class** : obtain the constructor with ` rclnodejs.require(...) ` and
207+ pass it directly. TypeScript then infers the goal, feedback and result types
208+ from the constructor, so no explicit ` any ` annotations are needed.
209+
210+ This demo uses the ** action class** form. The constructor is loaded once at
211+ module scope and reused for the type annotations via ` typeof Fibonacci ` :
212+
213+ ``` typescript
214+ const Fibonacci = rclnodejs .require (' test_msgs/action/Fibonacci' );
215+ ```
216+
197217### Action Server (` server.ts ` )
198218
199- The action server implements three main callbacks:
219+ The action server implements three main callbacks, all typed from
220+ ` typeof Fibonacci ` :
200221
2012221 . ** Goal Callback** : Decides whether to accept or reject incoming goals
202223
203224 ``` typescript
204- goalCallback (goalHandle : any ): rclnodejs .GoalResponse {
205- // Validate the goal and return ACCEPT or REJECT
225+ goalCallback (
226+ goal : rclnodejs .ActionGoal < typeof Fibonacci >
227+ ): rclnodejs .GoalResponse {
228+ // Validate the goal (goal.order is typed) and return ACCEPT or REJECT
206229 }
207230 ```
208231
2092322 . ** Execute Callback** : Performs the actual work (Fibonacci calculation)
210233
211234 ``` typescript
212- async executeCallback (goalHandle : any ): Promise < any > {
213- // Calculate Fibonacci sequence and provide feedback
235+ async executeCallback (
236+ goalHandle : rclnodejs .ServerGoalHandle < typeof Fibonacci >
237+ ): Promise < rclnodejs .ActionResult < typeof Fibonacci >> {
238+ // Calculate the sequence and publish typed feedback
214239 }
215240 ```
216241
2172423 . ** Cancel Callback** : Handles goal cancellation requests
243+
218244 ``` typescript
219- cancelCallback (goalHandle : any ): rclnodejs .CancelResponse {
245+ cancelCallback (
246+ goalHandle : rclnodejs .ServerGoalHandle < typeof Fibonacci > | undefined
247+ ): rclnodejs .CancelResponse {
220248 // Return ACCEPT to allow cancellation
221249 }
222250 ```
@@ -226,14 +254,23 @@ The action server implements three main callbacks:
226254The action client:
227255
2282561 . Waits for the action server to be available
229- 2 . Creates and sends a goal
230- 3 . Handles feedback during execution
257+ 2 . Creates and sends a goal ( ` new Fibonacci.Goal() ` )
258+ 3 . Handles typed feedback during execution
2312594 . Processes the final result
232260
233261``` typescript
262+ const goal = new Fibonacci .Goal ();
263+ goal .order = FIBONACCI_ORDER ;
264+
234265const goalHandle = await this .actionClient .sendGoal (goal , (feedback ) =>
235266 this .feedbackCallback (feedback )
236267);
268+
269+ private feedbackCallback (
270+ feedback : rclnodejs .ActionFeedback < typeof Fibonacci >
271+ ): void {
272+ // feedback.sequence is typed
273+ }
237274```
238275
239276## Customization
@@ -263,7 +300,7 @@ You can modify the demo to:
263300
2643014 . ** ROS2 environment not sourced** :
265302 ``` bash
266- source /opt/ros/humble /setup.bash # or your ROS2 distribution
303+ source /opt/ros/lyrical /setup.bash # or your ROS2 distribution
267304 ```
268305
269306### Debugging
0 commit comments