Lab: a managed node waits for its cue
Needs first: Parameters and launch files
Status: this chapter runs on
nros-board-mps2-an385-freertos: Verified in emulator (daily). From the board support registry; Board Support Levels explains the words.
Most nodes start work the moment their image boots. Some should not: a motor controller that must wait until its supervisor has checked the system, a sensor that must not publish until it is calibrated, a node a test wants to start at a known moment. ROS 2 gives such a node a lifecycle (REP-2002): a small state machine that something outside the node drives, and that decides when the node may do its work. A node with one is called a managed node.
nano-ros runs the same state machine, under the node’s own name, with the
same five services and the same ros2 lifecycle commands as stock ROS 2. In
this lab you make the talker of the Tutorial a managed node, boot it, and
watch it stay silent until you activate it from the host. You then switch
it off again, try a transition the state machine refuses, and finally let
it boot straight to work.
At the end, ros2 lifecycle get /sensor_node answers active [3] and the
listener prints I heard: again, but only after you said so.
The lab starts from the workspace of chapter 7, Parameters and launch
files: sensor_node publishes on /sensor/reading every 500 ms, and
monitor_node listens. Run every step from the root of sensor_ws, in a
shell where ROS 2 is sourced. It takes about half an hour, most of it
reading. Nothing in it changes your nodes’ code: the lifecycle is a fact of
the system, written in the launch file and in system.toml.
The four states
A managed node is always in one of four primary states:
| State | What the node does | How it gets there |
|---|---|---|
unconfigured | Exists, but holds nothing it needs to work | Boot, or cleanup from inactive |
inactive | Configured: it could work, but does not | configure from unconfigured, or deactivate from active |
active | Works | activate from inactive |
finalized | Done; it never leaves | shutdown from any of the three above |
The words in the last column are transitions. Something outside the node asks for one, the node runs the matching step (in this lab, a default step that always succeeds), and the machine moves on. A transition that does not start from the node’s current state is refused.
What “works” means is the part nano-ros enforces for you: a managed node’s
publishers send only while the node is active. In every other state,
publish returns success and the message is dropped. Its subscriptions,
timers, services and its log are not gated: they work in every state, so
the node can keep listening while it waits.
Mark the talker managed
A node is managed when the system says so. Change the talker’s <node> in
the launch file to <lifecycle_node>. The tree marks the two files this lab
changes with *:
sensor_ws/
src/
sensor_node_bringup/
launch/
system.launch.xml *
system.toml *
src/sensor_node_bringup/launch/system.launch.xml<?xml version="1.0"?>
<launch>
<arg name="topic" default="/sensor/reading"/>
<lifecycle_node pkg="sensor_node" exec="sensor_node" name="sensor_node">
<param name="period_ms" value="500"/>
<remap from="/reading" to="$(var topic)"/>
</lifecycle_node>
<node pkg="monitor_node" exec="monitor_node" name="monitor_node">
<remap from="/reading" to="$(var topic)"/>
</node>
<node pkg="server_node" exec="server_node" name="server_node"/>
<node pkg="client_node" exec="client_node" name="client_node"/>
<node pkg="action_server_node" exec="action_server_node" name="action_server_node"/>
<node pkg="action_client_node" exec="action_client_node" name="action_client_node"/>
</launch>
Only the talker’s element changed. monitor_node stays a plain node: it
has no state machine and listens from boot, as an rclcpp::Node would.
By default a managed node is brought to active as the image boots, so
nothing would look different. Tell the image to leave it where it starts.
Add this to the end of the bringup’s system.toml:
src/sensor_node_bringup/system.toml
[lifecycle]
autostart = "none"
autostart names the state every managed node of the image boots to:
"none" leaves them unconfigured, "inactive" configures them, and
"active" (the default) configures and activates them.
Boot it, and watch nothing happen
source /opt/ros/humble/setup.bash
nros run
Publishing every 500 ms
while its node is not active
The talker’s timer still fires every 500 ms: timers are not gated. But its first publish prints one line, and every message after it is dropped:
publishing on `/sensor/reading` while its node is not active: the message is dropped, and so is every message until the node is activated
The listener prints nothing, because nothing reaches it. Leave nros run
running, and open a second shell in sensor_ws for the rest of the lab.
Ask the node where it is
source /opt/ros/humble/setup.bash
export RMW_IMPLEMENTATION=rmw_zenoh_cpp
ros2 lifecycle nodes --no-daemon --spin-time 3
ros2 lifecycle get /sensor_node --no-daemon --spin-time 3
/sensor_node
unconfigured [1]
--no-daemon --spin-time 3 asks the board directly, giving discovery
three seconds, as chapter 7 did for ros2 topic list.
ros2 lifecycle nodes lists exactly the image’s managed nodes: the talker,
under its own name. The listener and the service and action nodes are not
there, because they have no state machine. The number after each state is
its REP-2002 id.
Each managed node answers five services under its own name:
/sensor_node/get_state, /sensor_node/change_state,
/sensor_node/get_available_states, /sensor_node/get_available_transitions
and /sensor_node/get_transition_graph. The ros2 lifecycle verbs call
them; a supervisor node of your own can call them too.
Configure it
ros2 lifecycle set /sensor_node configure --no-daemon --spin-time 3
ros2 lifecycle get /sensor_node --no-daemon --spin-time 3
Transitioning successful
inactive [2]
The node is configured, and still silent: inactive is not active. The
listener’s console still prints nothing.
Activate it
ros2 lifecycle set /sensor_node activate --no-daemon --spin-time 3
ros2 lifecycle get /sensor_node --no-daemon --spin-time 3
Transitioning successful
active [3]
The talker’s publisher opened as the node activated, and its next tick went out. The first shell shows the listener again:
I heard: reading …
The count goes on from wherever the talker’s counter had reached: its timer ran the whole time, and only the sending was held back. That is the difference between a gated publisher and a stopped node.
Deactivate it, and try a transition that is not allowed
ros2 lifecycle set /sensor_node deactivate --no-daemon --spin-time 3
ros2 lifecycle get /sensor_node --no-daemon --spin-time 3
Transitioning successful
inactive [2]
The listener stops printing. Now ask for a transition that does not start
from inactive:
ros2 lifecycle set /sensor_node deactivate --no-daemon --spin-time 3
Unknown transition requested
The machine refuses it and stays where it is, as stock ROS 2 does. To see what it would accept from here:
ros2 lifecycle list /sensor_node --no-daemon --spin-time 3
activate [3]
cleanup [2]
shutdown [6]
Activate it once more if you like: it sends again from the next tick, and deactivating it stops it again, as often as you ask.
Who drives a managed node
In this lab you drove the node by hand, from a shell. In a running system
something else does, and the node cannot tell the difference: every
ros2 lifecycle set is a call to the node’s change_state service, with
the transition’s id in the request. Whatever can call a ROS 2 service can
drive the node:
- A supervisor node on the host or on another board. It configures the
nodes it is responsible for, checks what it must (a sensor answers, a
limit switch is closed), activates them, and deactivates them when
something goes wrong. Nav2’s
lifecycle_manageris a well-known example of such a node. - A test, which wants the node to start at a known moment, and to stop again before the next case.
- An operator, with the commands you used.
The node’s own image never drives it. That is the point of the lifecycle:
the decision to start work is taken outside the node, by whatever knows
whether the system is ready. A node that should simply work from boot needs
no lifecycle at all, and stays a plain <node>.
Change it
Stop nros run with Ctrl-C. Two changes to try, one at a time:
- Boot straight to work. Set
autostart = "active"insystem.toml, or delete the[lifecycle]table, since"active"is the default. Run again:ros2 lifecycle get /sensor_nodeanswersactive [3]at once, and the listener hears from the first tick. The node is still managed, so a supervisor can still deactivate it. - Make the listener managed too. Change
monitor_node’s element to<lifecycle_node>as well.ros2 lifecycle nodeslists both, and each has its own state: activating one leaves the other where it was. Its subscription keeps working while it isinactive, because subscriptions are not gated. Only what it publishes would be.
autostart is one value for the whole image today: every managed node of
it boots to the same state. Give a node that should differ its transitions
after boot, with ros2 lifecycle set or a supervisor of your own.
If it doesn’t work
ros2 lifecycle nodesprints nothing. The host shell has noRMW_IMPLEMENTATION=rmw_zenoh_cpp, ornros runis not running. The router thatnros runstarted is the only way the host reaches the board.- The talker publishes from boot. The launch file still says
<node>for it, or the[lifecycle]table is missing or says"active". Rebuild happens on the nextnros run; nothing else needs cleaning. - The build refuses the launch file. Two managed nodes in one image ask
for two
autostartvalues in their contracts. Give the image one value.
What just happened
- A launch file’s
<lifecycle_node>made one node managed, with no change to its code: the generated entry gave it the REP-2002 state machine and its five services, under its own name. [lifecycle] autostartchose where the image’s managed nodes start.- The node’s publisher sent only while the node was
active. Its timer, and the plain listener beside it, ran the whole time. - The host drove the node with stock
ros2 lifecyclecommands, which got the same answers, and the same refusal, as from a stock ROS 2 node.
Next
- Run managed nodes: which nodes are managed, what each state gates, and a node whose own code makes it managed.
- Talk to ROS 2, if you have not yet run a stock ROS 2 node beside the board.
Design background
- RFC-0128: per-node lifecycle: one machine per managed node, what is gated, and why