Policies and switching
Several policies can be loaded at once, for example one that walks and one that plays a jump
motion. Exactly one is active at any time. The others stay loaded and ready, so switching
never loads a model; a switch only activates the policy’s stages (activate(), which for
mjlab also sends the new gains), logs a line and publishes ~/active_policies.
policies: [loco, imitation] # the first one is the default
loco:
preprocessor: policy_framework_mjlab/MjlabPreprocessor
postprocessor: policy_framework_mjlab/MjlabPostprocessor
model_path: /path/to/loco
imitation:
preprocessor: policy_framework_mjlab/MjlabPreprocessor
postprocessor: policy_framework_mjlab/MjlabPostprocessor
model_path: /path/to/jump
max_steps: 147 # the length of the motion; then back to 'loco'
interruptible: false # cannot be switched away from while it plays
Switching
A switch is requested with the ~/switch_policy service of the host, from any thread. It
is applied at the beginning of the next step, so one step never mixes two policies.
Two per-policy parameters shape this:
max_stepsAfter this many steps the pipeline returns by itself to the policy it interrupted (the default, first, policy after an activation or a safety reset); switching from one such motion to another keeps the original one to return to. Use it for a motion of a known length.
interruptibleWhen false, switch requests are rejected while the policy is active. Use it for a motion that must not be cut short, such as a jump.
A third one, disabled_safety_checks, skips some safety checks while the policy is active,
for instance a joint velocity bound during a jump; see Checks per policy.
What is loaded and what runs
The ~/list_policies service (policy_framework_msgs/srv/ListPolicies) returns the
loaded policies in the order of policies, the first being the default, with their stage
plugins, max_steps and interruptible.
The ~/active_policies topic (policy_framework_msgs/msg/ActivePolicies) holds the
policies being run. It is an array so that it can list several policies running at once;
today it holds exactly one. It is published from the step when the active policy changes
(a switch, the end of a max_steps motion, a reactivation of the host, a safety reset after
a latched stop) — only when the active policy really changes — first at the first step after
the host is configured. It is latched
(transient local, depth 1): a subscriber with a transient local QoS gets the current value
when it joins, and the value
stays while no step runs (host inactive, latched fault).
ros2 service call /policy_controller/list_policies policy_framework_msgs/srv/ListPolicies
ros2 topic echo --qos-durability transient_local /policy_controller/active_policies
What a policy remembers
A policy has memory: the outputs of its previous step, and the history of its observation terms. What happens to that memory at each event matters, because a model reacts badly to a past it never saw in training.
Event |
Effect on the policy’s memory |
|---|---|
The host is activated, or its safety is reset after a latched stop |
Every policy is marked to be cleared, and the default policy becomes active. Each policy is cleared when it next becomes active: previous outputs set to zero, history forgotten. The first step after a clear fills the whole history with the current values, as training does after an environment reset. |
The policy becomes active |
It is activated ( |
Another policy is active |
It observes every step, before the active policy’s step, with its own previous outputs: its history keeps following the robot, without running the model. An mjlab policy starts observing only once it has run since it was last cleared: before that it has no history to keep current. |
The third row is the important one. While the jump plays, the walking policy keeps recording what the robot does. When the jump ends, the walking policy resumes with a history of the last moments of the jump, which is the real recent past, instead of a history frozen from before the jump.
Gains follow the policy
Different policies are usually trained with different joint gains. Each mjlab policy carries its gains in the model file, and they are applied when the policy becomes active. See mjlab policies for the two ways they reach the joint loop.