Skip to content

Commit 4e22b07

Browse files
committed
Adds simply rclc lifecycle tutorial
* Shows how to set up an rclc lifecycle node, transition through its lifecycle states, and assign callbacks to lifecycle transitions micro-ROS#179 Signed-off-by: Arne Nordmann <arne.nordmann@de.bosch.com>
1 parent fae93f0 commit 4e22b07

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

  • _docs/tutorials/core/programming_rcl_rclc

_docs/tutorials/core/programming_rcl_rclc/index.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ In this tutorial, you'll learn the basics of the micro-ROS C API. The major conc
99
* [Publishers and subscriptions](#pub_sub)
1010
* [Services](#services)
1111
* [Timers](#timers)
12+
* [Lifecycle](#lifecycle)
1213
* [Rclc Executor](#rclc_executor)
1314

1415
## <a name="node"/>Creating a Node
@@ -212,6 +213,89 @@ if (rc != RCL_RET_OK) {
212213
}
213214
```
214215

216+
## <a name="lifecycle"/>Lifecycle
217+
218+
The rclc lifecycle package provides convenience functions in C to bundle an rclc node with the ROS 2 Node Lifecycle state machine, similar to the [rclcpp Lifecycle Node](https://github.com/ros2/rclcpp/blob/master/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp) for C++.
219+
220+
This tutorial show-cases how to set up an rclc lifecycle node, transition through its lifecycle states, and assign callbacks to lifecycle transitions.
221+
222+
### Initialization
223+
224+
Creation of a lifecycle node as a bundle of an rcl node and the rcl Node Lifecycle state machine.
225+
226+
```C
227+
#include "rclc_lifecycle/rclc_lifecycle.h"
228+
229+
rcl_allocator_t allocator = rcl_get_default_allocator();
230+
rclc_support_t support;
231+
rcl_ret_t rc;
232+
233+
// create rcl node
234+
rc = rclc_support_init(&support, argc, argv, &allocator);
235+
rcl_node_t my_node = rcl_get_zero_initialized_node();
236+
rc = rclc_node_init_default(&my_node, "lifecycle_node", "rclc", &support);
237+
238+
// rcl state machine
239+
rcl_lifecycle_state_machine_t state_machine_ =
240+
rcl_lifecycle_get_zero_initialized_state_machine();
241+
...
242+
243+
// create the lifecycle node
244+
rclc_lifecycle_node_t lifecycle_node;
245+
rcl_ret_t rc = rclc_make_node_a_lifecycle_node(
246+
&lifecycle_node,
247+
&my_node,
248+
&state_machine_,
249+
&allocator);
250+
```
251+
252+
Optionally create hooks for lifecycle state changes.
253+
254+
```C
255+
// declare callback
256+
rcl_ret_t my_on_configure() {
257+
printf(" >>> lifecycle_node: on_configure() callback called.\n");
258+
return RCL_RET_OK;
259+
}
260+
...
261+
262+
// register callbacks
263+
rclc_lifecycle_register_on_configure(&lifecycle_node, &my_on_configure);
264+
```
265+
266+
### Running
267+
268+
Change states of the lifecycle node, e.g.
269+
270+
```C
271+
bool publish_transition = true;
272+
rc += rclc_lifecycle_change_state(
273+
&lifecycle_node,
274+
lifecycle_msgs__msg__Transition__TRANSITION_CONFIGURE,
275+
publish_transition);
276+
rc += rclc_lifecycle_change_state(
277+
&lifecycle_node,
278+
lifecycle_msgs__msg__Transition__TRANSITION_ACTIVATE,
279+
publish_transition);
280+
...
281+
```
282+
283+
Except for error processing transitions, transitions are usually triggered from outside, e.g., by ROS 2 services.
284+
285+
### Cleaning Up
286+
287+
To clean everything up, simply do
288+
289+
```C
290+
rc += rcl_lifecycle_node_fini(&lifecycle_node, &allocator);
291+
```
292+
293+
### Example and Limitations
294+
295+
An example, how to use the RCLC Lifecycle Node is given in the file `lifecycle_node.c` in the [rclc_examples](https://github.com/micro-ROS/rclc/tree/master/rclc_examples) package.
296+
297+
The state machine publishes state changes, however, lifecycle services are not yet exposed via ROS 2 services (tbd).
298+
215299
## <a name="rclc_executor"/>rclc Executor
216300

217301
The rclc Executor provides a C API to manage the execution of subscription and timer callbacks, similar to the [rclcpp Executor](https://github.com/ros2/rclcpp/blob/master/rclcpp/include/rclcpp/executor.hpp) for C++. The rclc Executor is optimized for resource-constrained devices and provides additional features that allow the manual implementation of deterministic schedules with bounded end-to-end latencies.

0 commit comments

Comments
 (0)