4 Real-Robot Sim2Real Deployment

This is the final stop of the full X2 locomotion pipeline. This section explains how to safely progress on the real robot from the env_check self-check step by step to RL walking, and describes the state machine (FSM), safety mechanisms, and model fallback path. The deployment script source is extra/mc-rl/x2_deploy_pkg/deploy_x2.py.

Unless stated otherwise, all commands below are run from the code root directory (the root of the SDK main repo).

4.1 Overview

  • Deployment model: extra/mc-rl/x2_deploy_pkg/models/policy.onnx (the in-repo default model; once synced to the robot it lives at models/policy.onnx).

  • Deployment script: extra/mc-rl/x2_deploy_pkg/deploy_x2.py (a Python implementation based on aimdk_msgs and ROS2).

  • Frequencies: 50 Hz policy inference (POLICY_DT=0.02) / 200 Hz control output (CONTROL_DT=0.005; the control loop runs one inference every int(POLICY_DT/CONTROL_DT)=4 ticks).

Warning

The local source path is extra/mc-rl/x2_deploy_pkg/deploy_x2.py; after syncing to the robot you run the deployed copy deploy_x2.py. Below, <robot-ip> / <robot-user> are placeholders — replace them with the actual robot IP and login name. The robot-side copy is a deployed copy (not git); local changes only take effect after re-syncing to the robot via extra/mc-rl/x2_deploy_pkg/deploy_to_board.sh (see “One-Click Deployment to the Robot” below).

4.2 Preparation Before Going On-Robot

Source the Environment

source /opt/ros/humble/setup.bash
source <aimdk-workspace>/install/setup.bash

<aimdk-workspace>/install/setup.bash provides aimdk_msgs (JointStateArray / JointCommandArray / Imu). If not sourced, extra/mc-rl/x2_deploy_pkg/deploy_x2.py will report aimdk_msgs not found at startup and exit.

One-Click Deployment to the Robot

Run the one-click deploy script on the host machine (code root directory); it syncs the deployment files to the robot, verifies the model md5 in both directions, and automatically runs extra/mc-rl/x2_deploy_pkg/env_check.sh (the synced deployed copy) on the robot side:

extra/mc-rl/x2_deploy_pkg/deploy_to_board.sh <robot-user> <robot-ip> [board-dir]   # board directory defaults to x2_deploy

extra/mc-rl/x2_deploy_pkg/env_check.sh (named env_check.sh once synced to the robot) checks the following items: Python, ROS2, aimdk_msgs, rclpy and sensor_msgs, onnxruntime, numpy, PyYAML, models/policy.onnx, the deployment script and config files (extra/mc-rl/x2_deploy_pkg/deploy_x2.py, the gain source of truth extra/mc-rl/x2_config/joint_gains.py / joint_gains.yaml, the shared layer extra/mc-rl/x2_config/shared/policy_defs.py / shared/keyboard.py, the deploy layer extra/mc-rl/x2_config/deploy/deploy_config.py / deploy/rc_control_proxy.py), and the /aima/hal topic list; only after all checks pass can you proceed to the next step.

Note

extra/mc-rl/x2_deploy_pkg/env_check.sh occasionally falsely reports waist/state as missing, because of the ros2 topic list 5-second timeout compounded by DDS discovery latency. waist/state is actually published normally at 500 Hz, and re-running usually recovers it.

Tip

Manual deployment (fallback): if you skip the one-click script, you can manually scp extra/mc-rl/x2_deploy_pkg/deploy_x2.py, extra/mc-rl/x2_deploy_pkg/env_check.sh, the entire extra/mc-rl/x2_config/ (the gains joint_gains.{py,yaml} plus the shared/ and deploy/ subdirectories — all required), and extra/mc-rl/x2_deploy_pkg/models/policy.onnx to the robot’s ~/<board-dir>/ (put the model in its models/ subdirectory); on the robot side, after source /opt/ros/humble/setup.bash and the AimDK workspace, run cd ~/<board-dir> && bash env_check.sh; and run md5sum models/policy.onnx on both the host and the robot to confirm they match, avoiding accidental use of an old model.

Disabling the Robot’s Built-in MC

Before RL deployment you must disable the robot’s built-in MC (Motion Controller); otherwise two controllers sending joint commands at once will conflict.

# First query the mc state
ssh <robot-user>@<robot-ip> "aima em get-app mc"

Handle two cases based on the returned state:

get-app mc returns

Action

Running

Directly run ssh <robot-user>@<robot-ip> "aima em stop-app mc" to disable it

Idle

First run ssh <robot-user>@<robot-ip> "aima em start-app mc && sleep 2 && aima em stop-app mc"

Warning

After boot, mc may be in the Idle state: joint state is then published only once, latched via TRANSIENT_LOCAL, and not continuously, which freezes the RL observation (deploy_x2.py hangs at sensor-ready). So when Idle, first start-app to bring up the publisher, sleep 2 to let it publish stably, then stop-app to release control — after which joint state keeps being published at 500 Hz.

4.3 Step-by-Step Deployment (Step 0–3)

After completing “One-Click Deployment to the Robot”, ssh onto the board, enter ~/<board-dir>, and execute in order, confirming each step is fine before moving to the next. extra/mc-rl/x2_deploy_pkg/deploy_x2.py itself contains the full DAMPING → JOINT_DEFAULT → STANDUP → RL_ACTIVE state machine (see “The FSM and Safety Mechanisms of deploy_x2.py” below); no separate damping / standing test scripts are needed.

Step

Command

Expected behavior / notes

Step 0

extra/mc-rl/x2_deploy_pkg/deploy_to_board.sh <robot-user> <robot-ip> (run on the host)

Syncs the deployment files, md5 matches both ways, and all env_check.sh checks pass on the robot side (see “One-Click Deployment to the Robot”)

Step 0.5

ssh <robot-user>@<robot-ip> "aima em get-app mc", then disable mc according to its state

If Running, run stop-app mc; if Idle, run start-app mc && sleep 2 && stop-app mc (see “Disabling the Robot’s Built-in MC”)

Step 1

(ssh onto the board, cd ~/<board-dir>) python3 deploy_x2.py

After the self-check reaches sensors ready, it enters DAMPING (joints go soft); press Enter twice to advance through JOINT_DEFAULT → STANDUP (gain_ratio ramps from 0 to 1, standing steadily with no impact) → automatically enters RL_ACTIVE, standing still in place at cmd_vx=0 (self-supporting once the hoist is released, recovers from mild disturbance, |a| steady-state about 3.5–4.0)

Step 2

(in the same session) press W to walk

The first W press jumps from 0 to initial_cmd_vx=0.2; afterward W/S accelerate/decelerate by cmd_vx_step=0.1

Wrap-up

ssh <robot-user>@<robot-ip> "aima em start-app mc"

Start the robot’s built-in MC and restore the normal operating state

Before starting, also confirm that no leftover deploy_x2 processes remain:

ps aux | grep deploy_x2

If any remain, terminate them first (see the “leftover background deploy_x2 process” entry in “Troubleshooting” below).

4.4 The FSM and Safety Mechanisms of deploy_x2.py

The Six-State FSM

extra/mc-rl/x2_deploy_pkg/deploy_x2.py uses a three-stage transition FSM (the State enum):

WAITING → DAMPING → (Enter/Space) → JOINT_DEFAULT → (Enter/Space) → STANDUP → (1.5s) → RL_ACTIVE
                                                                                          │
                          ESTOP (tilt>limit auto) ←──────────────────────────────────────┘

State

Meaning

Entry condition

WAITING

Waits for the IMU and leg/waist/arm joint state to be ready

The state at startup

DAMPING

Pure damping on all joints (kp=0, kd=damping_kd); joints go soft

Entered automatically after sensors ready

JOINT_DEFAULT

Soft-PD half-squat preparation, 1.5s smoothstep interpolation

Press Enter/Space under DAMPING

STANDUP

Hard PD (training KP/KD) stands up to the RL default pose, 1.5s cosine interpolation

Press Enter/Space after JOINT_DEFAULT interpolation completes

RL_ACTIVE

50 Hz policy-inference walking; on entry cmd_vx=0 to stand steady

Entered automatically after STANDUP interpolation completes 1.5s

ESTOP

Fall e-stop; sends damping

Triggered automatically when the tilt angle exceeds the threshold during RL_ACTIVE

On entering RL_ACTIVE, velocity_cmd is forced to zero (velocity_cmd[0]=0.0 in _enter_rl_active) — following the “stand steady before walking” principle, the robot only walks after the user explicitly presses W.

Keyboard Control

Key

Action

Notes

Enter / Space

Advance the FSM: DAMPING→JOINT_DEFAULT; after JOINT_DEFAULT interpolation completes →STANDUP

Requires two Enter presses

W / S

Forward / backward ±cmd_vx_step (0.1)

The first W press jumps from 0 to initial_cmd_vx (0.2); clamped to [cmd_vx_min=-0.3, cmd_vx_max=0.5]

A / D

Strafe left / right ±cmd_vy_step (0.1)

Clamped to [-0.3, 0.3]

Q / E

Turn left / right ±cmd_vyaw_step (0.1)

Clamped to [-0.2, 0.2]

Space (within RL_ACTIVE)

Zero all velocities (stand steady in place)

Clears vx/vy/vyaw all to zero

Backspace

Switch to DAMPING from any RL / transition state (emergency stop)

Press this before a fall to preserve the log

Esc

Exit the program under DAMPING/ESTOP/WAITING

Esc has no effect during RL_ACTIVE; press Backspace first

Ctrl+C

Sends 5 frames of damping and exits from any state

Safe exit

?

Print the full key-binding help

Safety Mechanisms

Mechanism

Value / implementation

Description

DAMPING_KD

4.0 (recommended: too large, e.g. 5.0, tends to buzz under load; too small, e.g. 3.0, causes impact during transitions)

Can be overridden with --damping_kd

GRAVITY_PITCH_LIMIT

1.0 (projected-gravity component threshold)

Fall backstop

Action clip

±5.0 (np.clip(output[0], -5.0, 5.0))

Hard clamp on output actions

Action delta clip

ACTION_DELTA_LIMIT=0.5 (clamps the increment between consecutive target positions)

Prevents single-step jumps

Damping start

After sensors ready, enter DAMPING first rather than sending positions directly

FSM design

Cosine-interpolated standing

STANDUP uses 0.5*(1-cos(πp)) smoothing, over 1.5s

Impact-free standing

Ctrl+C safe exit

Sends 5 frames of damping in finally before shutdown

Safe exit

GRAVITY_PITCH_LIMIT uses a component of the unit gravity vector (equal to sin(tilt angle)) as a proxy: during normal walking this component is below 0.15 and will not false-trigger. The threshold 1.0 corresponds to the component reaching its maximum (about 90°, nearly flat) before triggering, so it almost never auto-e-stops for a fall from a standing posture and serves only as an extreme backstop; if you want an e-stop at a smaller tilt, reduce this value (e.g. 0.72 ≈ 46°, 0.5 ≈ 30°). The deploy-side thresholds GRAVITY_PITCH_LIMIT, ACTION_DELTA_LIMIT, and CONTROL_DT are defined in extra/mc-rl/x2_config/deploy/deploy_config.py (the sim2sim-side counterparts are in extra/mc-rl/x2_config/sim/sim_config.py).

Warning

KP/KD are simulation values: the training KP/KD come from the single source of truth extra/mc-rl/x2_config/joint_gains.yaml (loaded via extra/mc-rl/x2_config/joint_gains.py; a change in one place takes effect across training / sim2sim / deployment). RL_ACTIVE and STANDUP use its rl_gains (DEPLOY_KP / DEPLOY_KD), while JOINT_DEFAULT uses standup_gains. rl_gains (model X2 T2.5) matches training joint by joint; these gains have not been calibrated on the real robot and are simulation values, so the real robot’s actual response may differ — once real-robot system-identification values are obtained, just edit extra/mc-rl/x2_config/joint_gains.yaml.

RC Handle Control (Optional)

Besides the keyboard, you can also drive RL walking with the robot’s factory RC handle. Add --enable_rc_proxy when starting deployment:

python3 deploy_x2.py --enable_rc_proxy

Once enabled, the handle’s sticks replace the keyboard WASD+QE and drive cmd_vx / cmd_vy / cmd_vyaw directly.

The handle can also advance the FSM state transitions (other transitions can still use the keyboard, see “The Six-State FSM” and “Keyboard Control” above). The currently supported button combos:

Handle button combo

Action

L2 + X

Enter JOINT_DEFAULT (half-squat preparation)

L2 + ↑ (D-pad up)

Enter STANDUP (stand up, then automatically enter RL_ACTIVE)

L1 + R1 + Create button

Switch back to DAMPING (damping / emergency stop)

Argument

Default

Description

--enable_rc_proxy

Off

Enable RC handle control

--rc_velocity_topic

/aima/mc/locomotion/velocity

Handle velocity-command topic

--rc_timeout

0.5

Handle velocity timeout (seconds); if no command is received before it elapses, the RL velocity is zeroed

Before use, the robot’s built-in MC must be stopped and the factory RC must still be running (see “Disabling the Robot’s Built-in MC” and “Step-by-Step Deployment (Step 0–3)” above).

4.5 ROS2 Topic Interface

extra/mc-rl/x2_deploy_pkg/deploy_x2.py sends and receives aimdk topics directly via rclpy (it does not use ros2 service call, which is unresponsive on X2).

Topic

Type

Direction

Description

/aima/hal/joint/{leg,waist,arm,head}/state

JointStateArray

Subscribe

Joint state (position / velocity)

/aima/hal/joint/{leg,waist,arm,head}/command

JointCommandArray

Publish

Joint command (position / stiffness / damping)

/aima/hal/imu/torso/state

sensor_msgs/Imu

Subscribe

base IMU (orientation quaternion and angular velocity)

QoS: state subscriptions use BEST_EFFORT, command publishing uses RELIABLE. _sensors_ready() only requires the leg, waist, and arm joint states plus the IMU to be ready (head is not required).

Warning

The torso IMU used in training is actually located at the hip / pelvis — its naming is counterintuitive. IMU_TOPIC = "/aima/hal/imu/torso/state" is the IMU that training is aligned to, and the deployment side uses it directly as the base frame; it should not be changed to a chest topic. This has been cross-verified against the HAL docs, the official deploy source, and the MJCF.

4.6 Real-Robot Validation Status

All velocity dimensions have passed real-robot validation: |a| matches the sim2sim expectation, no ESTOP was triggered, and there was no trembling.

Real-robot RL walking footage (under hoist protection; the bundled model validated across all velocity dimensions).

Dimension

Validated range

Result

cmd_vx (forward / backward)

−0.3 ~ 0.5 m/s throughout

✅ Stable, including standing still and high speed

cmd_vy (A/D lateral)

±0.3 m/s

✅ Stable

cmd_vyaw (Q/E turning)

±0.2 rad/s

✅ Stable

Composite command (vx + vyaw)

Combinations within the above ranges

✅ Stable

Speed switching (continuous key-based speed changes)

Within the above ranges

✅ Stable

Long-duration operation

Continuous operation

✅ Stable

4.7 Troubleshooting

Problem

Symptom

Solution

waist/state depends on mc initialization

When mc is Idle, joint state publishes only one latched frame, the RL observation freezes, and it hangs at sensor-ready

Run start-app mc && sleep 2 && stop-app mc (see “Disabling the Robot’s Built-in MC” above)

Model mismatch before going on-robot

The md5 of policy.onnx on the robot differs from the host

Deploy with extra/mc-rl/x2_deploy_pkg/deploy_to_board.sh; the script verifies md5 in both directions (see “One-Click Deployment to the Robot” above)

A leftover background deploy_x2 process causes conflicting vibration

The previous deploy_x2.py did not exit cleanly; the leftover process and the new node send commands simultaneously, and the robot vibrates

Before starting, run ps aux | grep deploy_x2 and terminate any leftover processes

ros2 service call is unresponsive on X2

Centralized DDS discovery problem

Use an rclpy script to send/receive topics directly (e.g. extra/mc-rl/x2_deploy_pkg/deploy_x2.py) instead of relying on the ros2 service call command line

The full pipeline is now complete.