The two hosts
A host is the program that runs the cycle. There are two, and they share everything except
how data enters and leaves. Both are built on the same PolicyRunner, which loads the
input sources, the policies and the safety checks (and the node’s executors; the controller
loads its interface executors itself) and owns the stores, so a policy is configured the same
way for either.
Standalone node
policy_node is an ordinary ROS 2 lifecycle node. It does not need ros2_control.
Every input must come from a topic. Configuration fails, listing the missing entries, if a policy needs something no input source provides.
A timer runs one policy step per tick, at
update_rate(the policy rate, typically 50 Hz).Commands leave as topics. The node sends references; something else closes the joint loop at a high rate: a ros2_control
pid_controller, or the robot’s own driver.The safety checks run on every tick, before the step, and the
max_ageof the terms after it. The node does not own the hardware: a fault with a latched reaction, a failed step or a stale input stops the commands until the node is reactivated or its safety is reset (~/reset_safety); neutral commands keep the policy running (Safety behaviour).
Use it when the robot has no ros2_control, or when the policy should live outside the control process.
ros2_control controller
PolicyController is a ros2_control controller. It runs inside the controller manager,
in the control loop.
Inputs. Every entry that no input source provides is claimed as the state interface of the same name. Inputs can therefore all be state interfaces, all be topics, or a mix (for example joints and IMU from interfaces, the velocity command from a topic). Activation fails while a state interface reads NaN (its driver does not write it;
check_on_activation, default on); after that, a NaN goes through and fails the step.Outputs. Interface executors turn the commands into command interface values. They can compute:
PdEffortExecutorruns a joint PD law and writes efforts, so no other controller is needed.ReferenceInterfaceExecutoronly copies references into the interfaces of a chained controller.Two rates in one loop. The state interfaces, the kinematics, the safety checks and the executors run on every update; the policy and the topic sources only every
update_rate / policy_rateupdates.Hold until ready. From activation, or a
~/reset_safetythat clears a stop, until the first step returns commands, the executors hold the robot (write_initial()) instead of following the command store, which may still hold the previous activation’s references.Safety. The safety checks run on every update, before the policy. While a reaction is latched the policy is neither stepped nor followed: the executors write their fault reaction on every update (
holdanddamponly where the executor supports them; the safe values are zero effort forPdEffortExecutor, unchanged interfaces forReferenceInterfaceExecutor), or the controller returns an error so that the controller manager deactivates it and activates its fallback controllers (Safety behaviour). State interfaces that cannot be read for more thanmax_failed_readsupdates in a row also write the safe values and return an error.Configuration errors. A topic executor (e.g.
MultiDofCommandExecutor) is refused,policy_ratemust divideupdate_rate, andasync_inferencewithis_asyncis refused. So are a state entry an executor needs on every update (e.g. PD feedback) that an input source provides, a command interface claimed by two executors, and a fault reaction in the safety configuration that an executor cannot apply.
Control rate and policy rate
A policy is trained at a low rate, typically 50 Hz. A joint PD loop wants a high rate, typically 1 kHz. The controller runs both in the same loop:
update_rate: 1000 policy_rate: 50 -> one policy step every 20 updates
update 1 2 3 ... 20 21 22 ... 40 41
policy S S S
executors x x x ... x x x ... x x
Between two policy steps the position references stay the same, while the PD law keeps using the newest joint positions and velocities on every update.
Where inference runs
Running a neural network takes time, and that time varies. Three arrangements are possible.
Setting |
What happens |
Use it for |
|---|---|---|
default (synchronous) |
The policy step runs inside |
Small models, or a controller whose |
|
Only the policy step runs in its own real-time thread. |
A PD law at a high rate with a policy at a low rate. Recommended in that case. |
|
The standard ros2_control option: the whole |
A reference executor feeding a chained controller. Not for a PD law. |
With async_inference a policy step is spread over several updates:
A fault does not wait for the inference thread either: from the update in which it is
latched, the executors write its reaction, and a step still running is ignored. A step that
failed, or whose term inputs are stale, is a fault in the update that collects it, and its
commands are not taken. With policy_rate 0 there is a tick on every update, so a step’s
result is collected at the earliest in the next update.
The step works on a snapshot of the state store taken at the policy tick, so it sees one
consistent instant even though the control loop keeps updating the live store. If a policy
tick finds the previous step still running, that tick is skipped and a warning is logged:
the model is slower than policy_rate. A tick can also be skipped, rarely, when the thread
happens to hold its lock at that instant (the control loop never waits for it); the next tick
steps again.
Choosing a host
Situation |
Host |
|---|---|
The robot runs ros2_control and exposes joint effort interfaces |
Controller with |
The robot runs ros2_control and already has a joint impedance controller |
Controller with |
The robot has no ros2_control |
Node; its driver must close the joint loop |