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 atmodels/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 everyint(POLICY_DT/CONTROL_DT)=4ticks).
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:
|
Action |
|---|---|
|
Directly run |
|
First run |
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 |
|
Syncs the deployment files, md5 matches both ways, and all |
Step 0.5 |
|
If |
Step 1 |
(ssh onto the board, |
After the self-check reaches sensors ready, it enters DAMPING (joints go soft); press Enter twice to advance through JOINT_DEFAULT → STANDUP ( |
Step 2 |
(in the same session) press W to walk |
The first W press jumps from 0 to |
Wrap-up |
|
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 |
|---|---|---|
|
Waits for the IMU and leg/waist/arm joint state to be ready |
The state at startup |
|
Pure damping on all joints (kp=0, kd= |
Entered automatically after sensors ready |
|
Soft-PD half-squat preparation, 1.5s smoothstep interpolation |
Press Enter/Space under DAMPING |
|
Hard PD (training KP/KD) stands up to the RL default pose, 1.5s cosine interpolation |
Press Enter/Space after JOINT_DEFAULT interpolation completes |
|
50 Hz policy-inference walking; on entry cmd_vx=0 to stand steady |
Entered automatically after STANDUP interpolation completes 1.5s |
|
Fall e-stop; sends damping |
Triggered automatically when the tilt angle exceeds the threshold during RL_ACTIVE |
On entering RL_ACTIVE,
velocity_cmdis forced to zero (velocity_cmd[0]=0.0in_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 |
|---|---|---|
|
Advance the FSM: DAMPING→JOINT_DEFAULT; after JOINT_DEFAULT interpolation completes →STANDUP |
Requires two Enter presses |
|
Forward / backward ± |
The first W press jumps from 0 to |
|
Strafe left / right ± |
Clamped to [-0.3, 0.3] |
|
Turn left / right ± |
Clamped to [-0.2, 0.2] |
|
Zero all velocities (stand steady in place) |
Clears vx/vy/vyaw all to zero |
|
Switch to DAMPING from any RL / transition state (emergency stop) |
Press this before a fall to preserve the log |
|
Exit the program under DAMPING/ESTOP/WAITING |
Esc has no effect during RL_ACTIVE; press Backspace first |
|
Sends 5 frames of damping and exits from any state |
Safe exit |
|
Print the full key-binding help |
Safety Mechanisms
Mechanism |
Value / implementation |
Description |
|---|---|---|
|
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 |
|
1.0 (projected-gravity component threshold) |
Fall backstop |
Action clip |
|
Hard clamp on output actions |
Action delta clip |
|
Prevents single-step jumps |
Damping start |
After sensors ready, enter DAMPING first rather than sending positions directly |
FSM design |
Cosine-interpolated standing |
STANDUP uses |
Impact-free standing |
Ctrl+C safe exit |
Sends 5 frames of damping in |
Safe exit |
GRAVITY_PITCH_LIMITuses 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 thresholdsGRAVITY_PITCH_LIMIT,ACTION_DELTA_LIMIT, andCONTROL_DTare defined inextra/mc-rl/x2_config/deploy/deploy_config.py(the sim2sim-side counterparts are inextra/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 |
|---|---|
|
Enter JOINT_DEFAULT (half-squat preparation) |
|
Enter STANDUP (stand up, then automatically enter RL_ACTIVE) |
|
Switch back to DAMPING (damping / emergency stop) |
Argument |
Default |
Description |
|---|---|---|
|
Off |
Enable RC handle control |
|
|
Handle velocity-command topic |
|
|
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 |
|---|---|---|---|
|
|
Subscribe |
Joint state (position / velocity) |
|
|
Publish |
Joint command (position / stiffness / damping) |
|
|
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 |
Model mismatch before going on-robot |
The md5 of |
Deploy with |
A leftover background deploy_x2 process causes conflicting vibration |
The previous |
Before starting, run |
|
Centralized DDS discovery problem |
Use an rclpy script to send/receive topics directly (e.g. |
The full pipeline is now complete.