mjlab policies
policy_framework_mjlab runs policies trained with mjlab and exported to ONNX. The export
stores, next to the network, metadata describing how it must be used: the joints, the
observation terms, the action scale, the gains. The two stages of this package read that
metadata, so a policy needs almost no configuration: point model_path at the file, or at
a directory holding it (its first .onnx file by name is used).
Building the observation
The metadata lists the observation terms in order (observation_names). The preprocessor
creates one term object per name. Each step, every term writes its current value; the
preprocessor keeps the history and lays everything out in the input tensor.
Three things are worth knowing about this layout:
Term-major. The tensor holds all the frames of the first term, then all the frames of the second term, and so on. Within a term the oldest frame comes first.
Back-fill. On the first step after a clear, the whole history of each term is filled with its current value. mjlab does the same after an environment reset.
History length. For mjlab exports it is the same for every term, deduced from the size of the input. pal_mjlab exports give one length per term.
Observation terms
Term |
Value |
|---|---|
|
Angular velocity measured by the IMU |
|
Linear acceleration measured by the IMU |
|
Direction of gravity in the IMU frame, from the IMU orientation |
|
Direction of gravity in the base frame (pal_mjlab, see below) |
|
Linear velocity from an odometry source |
|
Joint positions minus their default positions |
|
Joint velocities |
|
The raw action of the previous step |
|
The velocity the user asks for: forward, sideways, turning |
|
The joint positions and velocities the policy itself predicted at the previous step; the measured joint state on the first step after it becomes active |
Any term can be replaced, and new ones added, without touching the preprocessor: set
<policy>.terms.<name>.type to an ObservationTerm plugin, e.g. to read the velocity
command straight from its topic:
loco:
terms:
command:
type: policy_framework_mjlab/TwistCommandTerm
topic: /cmd_vel
timeout: 1.0
A topic term subscribes once per policy. When many policies take the same command, use one
TwistSource on the host instead (sources: [cmd_vel]) and leave the built-in command
term: every policy reads the shared cmd_vel/* entries from a single subscription. The
source name must match the preprocessor’s velocity_command_name (default cmd_vel).
The Kangaroo configuration does this.
Extra inputs
Every model input other than the observation (observation_input) is filled by an
ExtraInput plugin: <policy>.extra_inputs.<input>.type when set, else the built-in one of
that name. An input with neither fails the configuration, unless
<policy>.unknown_extra_inputs is ignore (it then stays at zero).
Input |
Built-in |
Value |
|---|---|---|
|
|
The steps since the policy became active |
Shipped plugins, usable for any input through <policy>.extra_inputs.<input>.type:
Plugin |
Value |
|---|---|
|
The steps since the policy became active |
|
|
DoubleParameterInput fits any scalar input. Its value is taken when the policy becomes
active, so a running motion keeps it and a change at runtime applies to the next one:
imitation_v30:
extra_inputs:
<input>:
type: policy_framework_mjlab/DoubleParameterInput
ros2 param set /policy_node imitation_v30.extra_inputs.<input>.value 0.45
pal_mjlab exports
pal_mjlab adds metadata that the stages recognise:
Metadata |
Meaning |
|---|---|
|
The joints the policy observes, when it does not observe all of them |
|
The joints the policy drives, and the offset of each action |
|
The size of each term, checked against what the terms produce |
|
The history length of each term |
|
The link the policy calls its base |
|
The default of a |
The last one matters because the IMU is not always mounted on the base. With
goal_root_body set, the terms base_ang_vel and base_projected_gravity describe
that link: the IMU measurement is moved to it with the Kinematics. This needs
kinematics.enabled and the parameter imu_frame (the URDF link of the IMU).
base_projected_gravity cannot be used without goal_root_body.
From actions to commands
The postprocessor writes, for each joint the policy drives (action_joint_names, or the
metadata joints minus excluded_joints):
<joint>/<position_interface> = action_scale * action + offset
<joint>/<velocity_interface> = 0 (not written when velocity_interface is empty)
position_interface and velocity_interface default to position and velocity.
Joints listed in uncommanded_joints get nothing written, so that no executor claims
them: see Joints the robot does not have. They may also have no action: joints with metadata gains
(held by their PD in training) that the policy observes but does not drive, e.g. the arms of
a legs-only policy. The metadata gains are then one per non-excluded joint, and the action
count tells the two cases apart (as many actions as non-excluded joints: the uncommanded
joints have one; as many as the others: they have none):
loco:
uncommanded_joints: [arm_left_1_joint, arm_left_2_joint, arm_left_3_joint, arm_left_4_joint,
arm_right_1_joint, arm_right_2_joint, arm_right_3_joint,
arm_right_4_joint]
These are references for a joint PD loop. The gains of that loop are part of the policy
(joint_stiffness, joint_damping in the metadata) and reach it in one of two ways:
gains_as_commandsThe gains travel with the references, as command entries. A switch of policy changes the gains in the same step as the references. They are also known before the first step, which lets the executor hold the robot (see Safety behaviour).
gains_controllerThe gains are set as parameters of a separate
pid_controllereach time the policy becomes active. They are sent from a 0.5 s timer, which waits for that controller’s parameter services; only the latest request is sent, and a rejected parameter is logged, not retried. Until they arrive, the new references run with the previous gains.
Joints the robot does not have
A policy trained on a robot with more joints can run on one without them when those joints can
be held fixed. For example, a Kangaroo policy trained with the pelvis (pelvis_1_joint,
pelvis_2_joint) runs on a Kangaroo without one, with the pelvis held at zero:
sources: [cmd_vel, fixed_pelvis]
fixed_pelvis:
type: policy_framework/ConstantSource
entries: [pelvis_1_joint/position, pelvis_1_joint/velocity,
pelvis_2_joint/position, pelvis_2_joint/velocity]
values: [0.0, 0.0, 0.0, 0.0]
loco:
postprocessor: policy_framework_mjlab/MjlabPostprocessor
uncommanded_joints: [pelvis_1_joint, pelvis_2_joint]
Both halves are needed:
- The state the policy observes
ConstantSourcewrites the fixed values on every cycle, stamped now, so they are never stale. Like any input source, its entries are not claimed as state interfaces by the controller, so the missing joints are never asked of the robot.- The commands the policy produces
The policy still outputs an action for each of these joints, as in training (its
actionsobservation keeps them), butuncommanded_jointsmakes the postprocessor ignore it: no<joint>/position,<joint>/velocity, stiffness or damping entries, and nogains_controllerparameters. With no command entry, no executor claims or sends anything for these joints, in the node or the controller. Each one must be a joint the policy drives, so a typo fails the configuration.
A joint fed by ConstantSource but left out of uncommanded_joints also fails the
configuration: PdEffortExecutor needs real feedback for its PD law and refuses a joint
whose state an input source provides, instead of driving it blind.
The Kangaroo example
kangaroo_mjlab.launch.py runs walking policies (loco_<version>) and motion policies
(imitation_<version>), one per version given in loco_models and imitation_models.
The first loco version is the default policy, active at start. After its
imitation_frames, an imitation returns to the policy it interrupted (the loco version that
was running), or to the first loco version after a restart.
The policies can be listed in a YAML file passed as config instead, each naming its
model as <task>/<version>, with any policy name and per-policy settings on top of
config/kangaroo/policies.yaml; see config/kangaroo/examples. The models are looked up
in models_dir, by default models/simple of pal_rl_policies.
It runs in three modes:
|
What runs |
|---|---|
|
|
|
The same with |
|
|
The settings of each kind of policy are written once, in config/kangaroo/policies.yaml, and
shared by the three modes; the launch copies them into one policy per version. Apart from
the host-specific file, the controller modes differ in two ways: the launch sets
gains_controller to empty and gains_as_commands to true on every policy, and loads
the real robot’s safety checks (config/kangaroo/safety.yaml) unless
use_sim_time:=true. Node mode has no safety checks yet. The README
has the commands.