Data flow
This page follows the data through one policy step and explains the two data types every piece exchanges: the stores and the tensors.
The stores
A store (DataStore) is a flat table. Each row, called an entry, has:
a name, for example
leg_left_1_joint/position;a value, one floating point number;
a stamp, the time the value was produced, in seconds;
a received flag, false until the entry is written for the first time.
There are two stores: the state store for inputs and the command store for outputs.
Names follow the ros2_control convention <prefix>/<interface>:
Entry |
What it is |
|---|---|
|
A joint position (state), or a joint position reference (command) |
|
One component of the IMU orientation quaternion |
|
The forward velocity the user asks for |
|
The proportional gain a policy wants for that joint (command) |
Using these names has a practical consequence. A topic source writes the same entry names
that the equivalent state interfaces have. So whether a joint position comes from the
/joint_states topic or from a state interface is a matter of configuration only: the
policy reads leg_left_1_joint/position in both cases.
Entries are created once, while the plugins configure themselves: every plugin registers
the entries it reads or writes and keeps their index. During the control cycle the plugins
only use those indices. Nothing is looked up by name and nothing is allocated, which keeps
the cycle fast and predictable. The exceptions are rare events: a log line and the
~/active_policies message when the active policy changes, and the reason text of a failing
check.
The tensors
Between the three stages of a policy the data is no longer named numbers but tensors: the
blocks of float32 values the model takes and returns, keyed by the model’s input and
output names (TensorMap). A typical policy has one input tensor obs and one output
tensor actions.
One policy step
Read inputs. Topic sources copy their latest message into the state store. In the controller, the state interfaces are copied too.
Kinematics. When enabled, the robot model is updated from the joint entries (Kinematics).
Safety checks. The monitor, each input source’s
max_ageand theSafetyCheckplugins look at the state before the policy acts on it; a failing one applies its reaction (Safety behaviour). While a reaction other than neutral commands is latched, the policy is not stepped: the node sends nothing, the controller’s executors apply the reaction.Preprocessor. First, a pending policy switch is applied, the active policy is cleared or activated if needed, and every other policy observes the state to keep its history current (Policies and switching). Then the active preprocessor reads the state store and fills the model’s input tensors. This is where the observation is built: each observation term computes its current value, and the history of past values is kept. If an input has never been received, the preprocessor says so and the step is skipped: the policy waits for data.
Inference. Runs the model. It is the only stage that depends on the model format; the framework ships
OnnxInference(ONNX Runtime, on the CPU).Postprocessor. Reads the output tensors and writes named commands: typically a position reference per joint, computed as
scale * action + offset. A stage that fails (or throws), or a non-finite model output, is a fault (on_policy_failure).Input ages. The terms with their own subscription and a
max_ageare checked: if one stopped receiving, the step’s commands are not applied and its reaction is latched.Executors. Send the command store to the robot.
In the controller only the topic sources and steps 4 to 7 wait for a policy tick; the state
interfaces, the kinematics, the safety checks and the executors run on every update
(The two hosts). With async_inference, steps 4 to 6 run in the inference thread, and
step 7 runs in the update that collects their result.
The dashed arrow is the feedback many policies need: the preprocessor also receives the outputs of the previous step of the same policy. That is how the “last action” observation term works, and how a motion policy feeds its own predicted reference back to itself.
Timestamps
Every entry carries the time its data was produced. The stamps travel with the data:
Data |
Its stamp |
|---|---|
Entry written by a topic source |
The message header stamp, or the time it is read when the message has none (a Twist) |
Entry read from a state interface |
The node clock when the update read it (not the update’s |
User command term (derived from |
The message header stamp, or its receive time when the message has none (a Twist), so that its input age is how long ago it arrived; the neutral command is stamped when it is written |
Observation of a step |
The oldest stamp among its inputs. Inputs fed back from the previous outputs do not
count, nor do user commands (the terms derived from |
Command entry |
The stamp of the observation it was computed from |
This makes two questions answerable at any time: how old was the data the policy just
acted on? and how long did it take from sensor to command? The controller publishes the
first as observation_age_ms, the age of each input of the active policy as
input_age_ms.<policy>.<input>, and the duration of each step as step_time_us (with
its stages as preprocessor_time_us, inference_time_us and postprocessor_time_us)
and of each item of the update (e.g. update_time_us, executors_time_us) on its own
topics, /<controller>/introspection_data/{names,values,full}.
Introspection
PolicyController publishes its timings and input ages with pal_statistics, the library behind ros2_control’s
introspection,
but on its own topic instead of the controller manager’s shared
/controller_manager/introspection_data. On every update it publishes:
Name |
Meaning |
|---|---|
|
Total duration of the last policy step: its three stages plus the work around them (policy switch, inactive policies observing), so at least their sum |
|
Duration of each stage of that step; 0 for a stage that did not run (e.g. inference while the preprocessor waits for data) |
|
Age of the observation of that step when its commands were applied |
|
Age of each input of the active policy; 0 for the inputs of inactive policies |
|
Duration of the whole last update, the policy step included when synchronous (with
|
|
Duration of each item of that update: reading the state interfaces, reading the input
sources (policy ticks only), updating the kinematics, the safety checks, and the
executors writing the command interfaces (including a fault reaction); 0 for an item
that did not run: the input sources between policy ticks, and the sources, kinematics
and safety checks after the state interfaces could not be read for more than
|
The registry also adds its own topic_stats.* entries, e.g. how many asynchronous
publications were attempted and failed.
These values are published on three topics, under the controller’s namespace (e.g.
/policy_controller):
~/introspection_data/full: names and values in one message, for the command line (ros2 topic echo /policy_controller/introspection_data/full) or custom tools.~/introspection_data/names: the names, published again only when they change (e.g. on reconfiguration). Latched.~/introspection_data/values: the values only, on every update.
Prefer names and values for plotting and recording: they carry far less data than
full at the update rate.
Visualising with PlotJuggler
PlotJuggler plots these values live or from a bag:
Start it with
ros2 run plotjuggler plotjuggler.Under Streaming, choose ROS2 Topic Subscriber and press Start. Or load a bag recorded with
ros2 bag record /policy_controller/introspection_data/names /policy_controller/introspection_data/values.Select
/policy_controller/introspection_data/namesand/policy_controller/introspection_data/values.Drag the values of interest from the Timeseries List onto a plot.
Input ages of the active policy loco_v1734. The state inputs (joint positions and
velocities, IMU) are read from state interfaces in the same update, so they are 0. Here
actions, fed back from the previous step’s outputs, are about 20 ms old.
command is a user command read by a term derived from CommandTerm (such as
TwistCommandTerm): its age grows until the next message arrives, then drops back (a
command is held until its own timeout). Read from a TwistSource instead, as the
Kangaroo configuration now does, it is stamped when the source is read, so its age stays
near 0. The inputs of the inactive imitation_v30 stay at 0.
Durations of the preprocessor, inference and postprocessor, and of the whole step
(step_time_us). Here inference takes most of the step, and its spikes show directly
in the step time. Compare step_time_us with the policy period to check the step fits.