# MagicLab Z1 Deploy [中文说明](./README_zh.md) This repository runs low-level deployment for the MagicLab Z1 robot using: - an ONNX locomotion policy - an ONNX odometry model - the `magicbot-z1` SDK - either SSH keyboard input or a supported gamepad The main entrypoint is [deploy_real_z1.py](/home/sean/ws_small/magiclab_deploy/deploy_real_z1.py). Runtime parameters are loaded from [configs/z1_12dof.yaml](/home/sean/ws_small/magiclab_deploy/configs/z1_12dof.yaml). ## Project Layout - [deploy_real_z1.py](/home/sean/ws_small/magiclab_deploy/deploy_real_z1.py): robot connection, state machine, policy inference, command publishing - [config_z1.py](/home/sean/ws_small/magiclab_deploy/config_z1.py): YAML config loader - [configs/z1_12dof.yaml](/home/sean/ws_small/magiclab_deploy/configs/z1_12dof.yaml): deployment config for the 12-DoF setup - [gamepad_reader_btp.py](/home/sean/ws_small/magiclab_deploy/gamepad_reader_btp.py): gamepad input reader - [run_nx.sh](/home/sean/ws_small/magiclab_deploy/run_nx.sh): helper script for running on Jetson / aarch64 - [magiclab_deploy.yaml](/home/sean/ws_small/magiclab_deploy/magiclab_deploy.yaml): Conda environment file ## Joint Index Order When tuning `kps`, `kds`, `extra_kd`, `default_angles`, and similar arrays, the array index is the actual joint order used by the controller. The controller sends and reads joints by group, with the following order: | Group | Array fields | Index range | Notes | | ----- | ---------------------------------------------------------------------------------- | ----------- | --------------------------------------------------------------------------- | | Legs | `leg_mode`, `kps`, `kds`, `extra_kd`, `rec_leg_kp`, `rec_leg_kd`, `default_angles` | `0 ~ 11` | 12 leg joints; all leg-related parameters map one-to-one in this order. | | Arms | `upper_arm_mode`, `upper_arm_kp`, `upper_arm_kd`, `default_arm_angles` | `0 ~ 9` | 10 upper-body arm joints; use this order when looking up arm PD parameters. | | Waist | `upper_waist_mode`, `upper_waist_kp`, `upper_waist_kd`, `default_waist_angles` | `0` | 1 waist joint. | | Head | `upper_head_mode`, `upper_head_kp`, `upper_head_kd`, `default_head_angles` | `0 ~ 1` | 2 head joints. | For quick lookup, you can treat the config arrays as joint index tables. For example: - `default_angles[3]` is the default angle of the 4th leg joint - `kps[3]`, `kds[3]`, and `extra_kd[3]` refer to the same leg joint - `default_arm_angles[0]` is the default angle of the 1st arm joint - `default_head_angles[1]` is the default angle of the 2nd head joint ## Requirements You need: - Python 3.10 - Conda - `magicbot-z1` SDK installed locally - the ONNX policy and odometry model files referenced by the config - a robot network connection that matches the local IP used in the code The controller currently hardcodes: - local IP in [deploy_real_z1.py](/home/sean/ws_small/magiclab_deploy/deploy_real_z1.py:437): `192.168.54.111` Adjust it before running if your deployment machine uses a different network setup. ## Environment Setup Create the Conda environment from [magiclab_deploy.yaml](/home/sean/ws_small/magiclab_deploy/magiclab_deploy.yaml): ```bash conda env create -f magiclab_deploy.yaml conda activate deploy_walk ``` Install PyTorch separately if it is not already available in the environment. The exact wheel depends on your platform and CUDA version. Typical example: ```bash pip install torch torchvision torchaudio ``` ## SDK Environment Variables Before launching the controller, set the SDK library paths. For x86_64: ```bash export PYTHONPATH=/path/to/magicbot-z1_sdk-1.2.x/lib/x86_64${PYTHONPATH:+:$PYTHONPATH} export LD_LIBRARY_PATH=/path/to/magicbot-z1_sdk-1.2.x/lib/x86_64${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} ``` For Jetson / aarch64: ```bash export PYTHONPATH=/path/to/magicbot-z1_sdk-1.2.x/lib/aarch64${PYTHONPATH:+:$PYTHONPATH} export LD_LIBRARY_PATH=/path/to/magicbot-z1_sdk-1.2.x/lib/aarch64${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} ``` If your system needs a specific `libstdc++`, you may also need: ```bash export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6${LD_PRELOAD:+:$LD_PRELOAD} ``` ## Models The deployment config currently points to: - `policy/z1_12dof/policy_78_1211_14000.onnx` - `policy/z1_12dof/odom_78_1211_14000.onnx` If you change model filenames or directories, update [configs/z1_12dof.yaml](/home/sean/ws_small/magiclab_deploy/configs/z1_12dof.yaml:1). ## Run Run directly from the repository root: ```bash python deploy_real_z1.py --config z1_12dof.yaml ``` Or use the helper script on aarch64: ```bash bash run_nx.sh ``` `run_nx.sh` assumes: - Conda is installed in `$HOME/miniconda3` - the environment name is `magiclab_deploy` - the SDK is located under `$HOME/ws_small/magicbot-z1_sdk-1.2.x/lib/aarch64` Those assumptions do not match [magiclab_deploy.yaml](/home/sean/ws_small/magiclab_deploy/magiclab_deploy.yaml), which creates an environment named `z1_deploy`. Update either the environment name or the script before using it. ## Control Flow The controller runs through these states: 1. `ZERO_TORQUE` 2. `DEFAULT_POSITION` 3. `ACTIVE_CONTROL` 4. `DAMPING` Startup flow: 1. Connect to the robot and switch to low-level mode. 2. Wait for IMU and joint-state messages. 3. Stay in zero-torque mode until the start command is received. 4. Interpolate to the default pose. 5. Wait for the active-control command. 6. Run policy inference and publish joint commands at runtime. ## Input Devices The code prefers SSH keyboard control when stdin is a TTY. Otherwise it falls back to the gamepad reader. ### SSH Keyboard When running in a foreground SSH terminal, these keys are available: - `W/S`: forward / backward - `A/D`: left / right - `Q/E`: yaw left / right - `Z`: reset commanded velocity to zero - `J`: trigger start, move from zero torque to default pose - `K`: trigger active control - `X`: stop active control loop - `Space`: emergency stop If the terminal is left in a bad state after an abnormal exit: ```bash stty sane ``` ### Gamepad Gamepad input is implemented in [gamepad_reader_btp.py](/home/sean/ws_small/magiclab_deploy/gamepad_reader_btp.py). Behavior: - left stick: planar velocity - right stick axis: yaw - `LB + RB`: emergency stop - ABXY buttons are forwarded into the controller state machine You can test the reader separately with: ```bash python gamepad_reader_btp.py ``` ## Configuration The main config file is [configs/z1_12dof.yaml](/home/sean/ws_small/magiclab_deploy/configs/z1_12dof.yaml). Key parameter groups are summarized below: | Group | Fields | What they control | | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | Model paths | `policy_path`, `odom_path` | File paths for the locomotion policy ONNX model and the odometry ONNX model. The program fails at startup if either file is missing. | | Timing | `simulation_dt`, `control_decimation`, `cycle_time` | `simulation_dt` is the control-loop period. `control_decimation` determines how many low-level cycles occur between two policy / odometry inference steps. `cycle_time` controls the gait phase period used when gait-phase features are enabled. | | Leg gains | `leg_mode`, `kps`, `kds`, `extra_kd`, `rec_leg_kp`, `rec_leg_kd` | `kps` and `kds` are the gains used during active policy control. `extra_kd` is sent as an additional damping term for each leg joint to enter ankle_torque control mode. `rec_leg_kp` and `rec_leg_kd` are used when moving to and holding the default pose. | | Default pose | `default_angles`, `default_arm_angles`, `default_waist_angles`, `default_head_angles` | Reference joint targets used for startup interpolation, default-pose holding, and as the leg action baseline. Policy actions are added on top of `default_angles`. | | Observation dimensions | `num_actions`, `num_odom_output`, `num_obs`, `num_odom_obs`, `his_obs`, `his_odom_obs` | Define the tensor shapes expected by the ONNX models. `num_obs` and `num_odom_obs` are per-frame dimensions, while `his_obs` and `his_odom_obs` define how many frames are stacked into the history buffers. | | Scaling | `lin_vel_scale`, `ang_vel_scale`, `rpy_scale`, `dof_pos_scale`, `dof_vel_scale`, `action_scale`, `cmd_scale`, `controller_scale` | These values scale raw robot state, commands, and policy outputs before they are fed into the models or converted back into target commands. Wrong values here typically cause poor tracking or unstable behavior. | | Command limits | `xy_yaw_vel_max`, `xy_yaw_vel_min`, `xy_yaw_acc_max`, `xy_yaw_acc_min`, `xy_yaw_weight`, `xy_yaw_priority` | Apply velocity and acceleration limiting to user commands before they reach the policy. The priority and weight terms define how x / y / yaw commands are reduced when multiple axes compete for the limit budget. | | Gait-phase features | `observation_use_gait_phase` | Enables adding gait phase sine / cosine features to the policy observation. This must match the policy model input format. | | Default-pose transition | `interp_duration`, `recovery_gain_ramp`, `recovery_gain_ramp_start`, `recovery_gain_ramp_end`, `recovery_kp_scale` | Control how the robot moves from its current pose into the default pose. `interp_duration` sets the transition length, while the recovery-gain parameters shape how fast joint gains ramp up during that process. | | Initial command | `cmd_init` | The initial command vector `[vx, vy, yaw]` used when the controller starts or resets internal state. | The config filename passed to `--config` is resolved relative to the `configs/` directory. For example: ```bash python deploy_real_z1.py --config z1_12dof.yaml ``` ## Notes on Timing Current values in the default config: - `simulation_dt: 0.002` - `control_decimation: 5` That means: - command publishing runs every 0.002 s - policy / odometry inference runs every 5 control steps, or 50 Hz ## Safety Notes - The program switches the robot into low-level control mode. - `Space` on keyboard or `LB + RB` on the gamepad triggers emergency stop behavior. - On shutdown, the controller enters damping / zero-torque handling before disconnecting. - Validate gains, default angles, and command limits on your hardware before real deployment. ## Troubleshooting ### ONNX file not found Check the model paths in [configs/z1_12dof.yaml](/home/sean/ws_small/magiclab_deploy/configs/z1_12dof.yaml:1). ### SDK import or shared library error Check: - `PYTHONPATH` - `LD_LIBRARY_PATH` - `LD_PRELOAD` if your system requires it ### No keyboard input over SSH Keyboard control only works when stdin is an interactive TTY. If not, the code will fall back to gamepad input. ### Robot does not connect Check: - the robot network connection - the hardcoded local IP in [deploy_real_z1.py](/home/sean/ws_small/magiclab_deploy/deploy_real_z1.py:437) - whether the SDK and controller level setup match your robot firmware