-
Notifications
You must be signed in to change notification settings - Fork 44
Add WBC-AGILE end-to-end velocity policy for G1 #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b0e8c11
Add WBC-AGILE end-to-end velocity policy for G1
lgulich 72e8d09
Move model download to build script, remove runtime download
lgulich 10aca04
fix: remove dead code and add stability tests for G1 AGILE policy
lgulich 0d340cc
fix: address self-review findings for AGILE policy
lgulich 9c1cd61
Wire AGILE WBC policy into environment action term and embodiment
lgulich 9906c2a
Download AGILE model at runtime instead of requiring a manual script
lgulich 3e507b8
Rewrite AGILE test as simulation integration test
lgulich 7f4e021
Address PR review comments on AGILE policy
lgulich f9e197a
Fix AGILE test: use ground plane and add warmup period
lgulich 596bfeb
refactor: use S3/retrieve_file_path for AGILE ONNX model loading
lgulich d3dc499
Use GitHub URL for AGILE model instead of S3
lgulich 282f66f
Extract shared OnnxInferenceSession for homie and agile
lgulich 9ee29d2
Pin AGILE model URL to v1.2 release tag
lgulich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
isaaclab_arena_g1/g1_whole_body_controller/wbc_policy/config/g1_agile.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Copyright (c) 2025-2026, The Isaac Lab Arena Project Developers (https://github.com/isaac-sim/IsaacLab-Arena/blob/main/CONTRIBUTORS.md). | ||
| # All rights reserved. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # Joint ordering for ONNX model inputs (29 body joints in agile order). | ||
| # Must match the element_names from unitree_g1_velocity_e2e.yaml. | ||
| onnx_input_joint_names: | ||
| - left_hip_pitch_joint | ||
| - right_hip_pitch_joint | ||
| - waist_yaw_joint | ||
| - left_hip_roll_joint | ||
| - right_hip_roll_joint | ||
| - waist_roll_joint | ||
| - left_hip_yaw_joint | ||
| - right_hip_yaw_joint | ||
| - waist_pitch_joint | ||
| - left_knee_joint | ||
| - right_knee_joint | ||
| - left_shoulder_pitch_joint | ||
| - right_shoulder_pitch_joint | ||
| - left_ankle_pitch_joint | ||
| - right_ankle_pitch_joint | ||
| - left_shoulder_roll_joint | ||
| - right_shoulder_roll_joint | ||
| - left_ankle_roll_joint | ||
| - right_ankle_roll_joint | ||
| - left_shoulder_yaw_joint | ||
| - right_shoulder_yaw_joint | ||
| - left_elbow_joint | ||
| - right_elbow_joint | ||
| - left_wrist_roll_joint | ||
| - right_wrist_roll_joint | ||
| - left_wrist_pitch_joint | ||
| - right_wrist_pitch_joint | ||
| - left_wrist_yaw_joint | ||
| - right_wrist_yaw_joint | ||
|
|
||
| # Joint ordering for ONNX model outputs (14 controlled joints in agile output order). | ||
| # Must match the action_joint_pos element_names from unitree_g1_velocity_e2e.yaml. | ||
| controlled_joint_names: | ||
| - left_hip_pitch_joint | ||
| - right_hip_pitch_joint | ||
| - left_hip_roll_joint | ||
| - right_hip_roll_joint | ||
| - waist_roll_joint | ||
| - left_hip_yaw_joint | ||
| - right_hip_yaw_joint | ||
| - waist_pitch_joint | ||
| - left_knee_joint | ||
| - right_knee_joint | ||
| - left_ankle_pitch_joint | ||
| - right_ankle_pitch_joint | ||
| - left_ankle_roll_joint | ||
| - right_ankle_roll_joint | ||
|
|
||
| num_actions: 14 | ||
| num_body_joints: 29 | ||
|
|
||
| # Initial commands | ||
| cmd_init: [0.0, 0.0, 0.0] |
159 changes: 159 additions & 0 deletions
159
isaaclab_arena_g1/g1_whole_body_controller/wbc_policy/policy/g1_agile_policy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # Copyright (c) 2025-2026, The Isaac Lab Arena Project Developers (https://github.com/isaac-sim/IsaacLab-Arena/blob/main/CONTRIBUTORS.md). | ||
| # All rights reserved. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import numpy as np | ||
| import pathlib | ||
| import torch | ||
| from typing import Any | ||
|
|
||
| from isaaclab.utils.assets import retrieve_file_path | ||
|
|
||
| from isaaclab_arena_g1.g1_env.robot_model import RobotModel | ||
| from isaaclab_arena_g1.g1_whole_body_controller.wbc_policy.policy.base import WBCPolicy | ||
| from isaaclab_arena_g1.g1_whole_body_controller.wbc_policy.utils.homie_utils import load_config | ||
| from isaaclab_arena_g1.g1_whole_body_controller.wbc_policy.utils.onnx_utils import OnnxInferenceSession | ||
|
|
||
| # ONNX feedback state keys and their per-environment shapes (excluding batch dim). | ||
| _STATE_KEYS_AND_SHAPES: dict[str, tuple[int, ...]] = { | ||
| "last_actions": (14,), | ||
| "base_ang_vel_history": (5, 3), | ||
| "projected_gravity_history": (5, 3), | ||
| "velocity_commands_history": (5, 3), | ||
| "controlled_joint_pos_history": (5, 14), | ||
| "controlled_joint_vel_history": (5, 14), | ||
| "actions_history": (5, 14), | ||
| } | ||
|
|
||
|
|
||
| class G1AgilePolicy(WBCPolicy): | ||
| """G1 robot policy using the WBC-AGILE end-to-end neural network. | ||
|
|
||
| This policy uses a single ONNX model that takes raw sensor inputs and | ||
| manages observation history internally via feedback connections. The model | ||
| outputs target joint positions along with per-joint Kp/Kd gains for 14 | ||
| controlled joints (legs + waist_roll + waist_pitch). | ||
| """ | ||
|
|
||
| def __init__(self, robot_model: RobotModel, config_path: str, model_path: str, num_envs: int = 1): | ||
| """Initialize G1AgilePolicy. | ||
|
|
||
| Args: | ||
| robot_model: Robot model containing joint ordering info. | ||
| config_path: Path to policy YAML configuration file (relative to wbc_policy dir). | ||
| model_path: Path to the ONNX model file. Can be an S3/Nucleus URL | ||
| (resolved and cached by retrieve_file_path) or a local path. | ||
| num_envs: Number of environments. | ||
| """ | ||
| parent_dir = pathlib.Path(__file__).parent.parent | ||
| self.config = load_config(str(parent_dir / config_path)) | ||
| self.robot_model = robot_model | ||
| self.num_envs = num_envs | ||
|
|
||
| # Resolve model path via OV asset API (handles S3 download + local caching). | ||
| # Same pattern as G1HomiePolicyV2: retrieve_file_path returns a local path | ||
| # (absolute for S3 cache, relative for local files), then join with parent_dir. | ||
| model_local_path = retrieve_file_path(model_path, force_download=True) | ||
| model_full_path = parent_dir / model_local_path | ||
| self.session = OnnxInferenceSession(str(model_full_path)) | ||
|
|
||
| # Build joint index mappings between WBC order and agile ONNX order | ||
| self._build_joint_mappings() | ||
|
|
||
| # Initialize state | ||
| self._init_state() | ||
|
|
||
| def _build_joint_mappings(self): | ||
| """Build index mappings between WBC joint order and agile ONNX joint order.""" | ||
| wbc_order = self.robot_model.wbc_g1_joints_order # {joint_name: wbc_index} | ||
|
|
||
| # Mapping for ONNX input: indices into the WBC-ordered observation to select | ||
| # the 29 body joints in the order the ONNX model expects. | ||
| onnx_input_names = self.config["onnx_input_joint_names"] | ||
| self.wbc_to_agile_input = np.array([wbc_order[name] for name in onnx_input_names]) | ||
|
|
||
| # Mapping for ONNX output: for each of the 14 agile output joints, the | ||
| # position in the 15-element lower_body array to write to. | ||
| controlled_names = self.config["controlled_joint_names"] | ||
| lower_body_indices = self.robot_model.get_joint_group_indices("lower_body") | ||
| self.agile_idx_to_lower_body_idx = np.array( | ||
| [lower_body_indices.index(wbc_order[name]) for name in controlled_names] | ||
| ) | ||
|
|
||
| self.num_lower_body = len(lower_body_indices) | ||
|
|
||
| def _init_state(self): | ||
| """Initialize all per-environment state variables.""" | ||
| self.observation = None | ||
| self.cmd = np.tile(self.config["cmd_init"], (self.num_envs, 1)) | ||
|
|
||
| # Batched ONNX feedback state: each array has shape (num_envs, ...). | ||
| self.state = { | ||
| key: np.zeros((self.num_envs, *shape), dtype=np.float32) for key, shape in _STATE_KEYS_AND_SHAPES.items() | ||
| } | ||
|
|
||
| def reset(self, env_ids: torch.Tensor): | ||
| """Reset the policy state for the given environment ids. | ||
|
|
||
| Args: | ||
| env_ids: The environment ids to reset. | ||
| """ | ||
| idx = env_ids.cpu().numpy() | ||
| for key, shape in _STATE_KEYS_AND_SHAPES.items(): | ||
| self.state[key][idx] = np.zeros(shape, dtype=np.float32) | ||
| self.cmd[idx] = self.config["cmd_init"] | ||
|
|
||
| def set_observation(self, observation: dict[str, Any]): | ||
| """Store the current observation for the next get_action call. | ||
|
|
||
| Args: | ||
| observation: Dictionary containing robot state from prepare_observations(). | ||
| """ | ||
| self.observation = observation | ||
|
|
||
| def set_goal(self, goal: dict[str, Any]): | ||
| """Set the goal for the policy. | ||
|
|
||
| Args: | ||
| goal: Dictionary containing goals. Supported keys: | ||
| - "navigate_cmd": velocity command array of shape (num_envs, 3) | ||
| """ | ||
| if "navigate_cmd" in goal: | ||
| self.cmd = goal["navigate_cmd"] | ||
|
|
||
| def get_action(self, time: float | None = None) -> dict[str, Any]: | ||
| """Compute and return the next action based on current observation. | ||
|
|
||
| Returns: | ||
| Dictionary with "body_action" key containing joint position targets | ||
| of shape (num_envs, num_lower_body_joints). | ||
| """ | ||
| if self.observation is None: | ||
| raise ValueError("No observation set. Call set_observation() first.") | ||
|
|
||
| obs = self.observation | ||
|
|
||
| # Build batched ONNX inputs (all envs at once) | ||
| ort_inputs = { | ||
| "root_link_quat_w": obs["floating_base_pose"][:, 3:7].astype(np.float32), | ||
|
lgulich marked this conversation as resolved.
|
||
| "root_ang_vel_b": obs["floating_base_vel"][:, 3:6].astype(np.float32), | ||
| "velocity_commands": self.cmd.astype(np.float32), | ||
| "joint_pos": obs["q"][:, self.wbc_to_agile_input].astype(np.float32), | ||
| "joint_vel": obs["dq"][:, self.wbc_to_agile_input].astype(np.float32), | ||
| **{key: self.state[key] for key in _STATE_KEYS_AND_SHAPES}, | ||
| } | ||
|
|
||
| # Run batched inference | ||
| result = self.session.run(ort_inputs) | ||
|
|
||
| # Update feedback state for next step | ||
| for key in _STATE_KEYS_AND_SHAPES: | ||
| self.state[key] = result[f"{key}_out"] | ||
|
|
||
| # Map 14 agile output joints to the 15-joint lower_body array. | ||
| # waist_yaw (not controlled by agile) stays at 0.0. | ||
| body_action = np.zeros((self.num_envs, self.num_lower_body), dtype=np.float32) | ||
| body_action[:, self.agile_idx_to_lower_body_idx] = result["action_joint_pos"] | ||
|
|
||
| return {"body_action": body_action} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.