by Drew Owens
Open-source 15-DoF XL330 biped goose robot with Spine molecule PCB, 3D-printed chassis, and Isaac Lab to MuJoCo sim-to-real RL stack
main
Drew Owens
Publish Microgoose Rev A full-stack hardware, CAD, datasheet, and 7-stage RL stack
e843249
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
#!/usr/bin/env python3
"""Stage 7 Policy Arena Multi-Command Benchmark Suite for Microgoose.
Evaluates rl/checkpoints/policy.onnx across 4 standardized 10-second suites
(8 randomized + nominal trials each = 32 rollouts, 16,000 control steps):
1. stand_still: vx = 0.00 m/s, wz = 0.00 rad/s, stand_flag = 1.0
2. walk_forward_015: vx = 0.15 m/s, wz = 0.00 rad/s, stand_flag = 0.0
3. yaw_turn_pos04: vx = 0.10 m/s, wz = +0.40 rad/s, stand_flag = 0.0
4. push_recovery: vx = 0.15 m/s with bidirectional pushes at t=3.0s and t=6.5s
Writes rl/arena/arena_scorecard.json.
"""
from __future__ import annotations
import json
import math
from pathlib import Path
import sys
from typing import Any, Dict, List
import numpy as np
import onnxruntime as ort
ROOT_DIR = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT_DIR / "rl"))
from microgoose_mj_env import ( # noqa: E402
CONTROL_DT,
MAX_EPISODE_STEPS,
OBS_DIM,
MicrogooseMjEnv,
)
ONNX_PATH = ROOT_DIR / "rl" / "checkpoints" / "policy.onnx"
SCORECARD_PATH = ROOT_DIR / "rl" / "arena" / "arena_scorecard.json"
def run_suite(
sess: ort.InferenceSession,
suite_name: str,
vx_cmd: float,
vy_cmd: float,
wz_cmd: float,
stand_flag: float,
pushes: List[tuple[int, float, float]],
n_trials: int = 8,
) -> Dict[str, Any]:
survivals = 0
ep_lengths: List[int] = []
vx_maes: List[float] = []
wz_maes: List[float] = []
max_tilts: List[float] = []
displacements: List[float] = []
yaw_totals: List[float] = []
min_vbuses: List[float] = []
for trial in range(n_trials):
use_dr = trial >= 2 # First 2 trials nominal, remaining 6 full domain randomization
env = MicrogooseMjEnv(
seed=5000 + hash(suite_name) % 997 + trial * 17,
domain_rand=use_dr,
obs_noise=use_dr,
enable_pushes=False,
max_episode_steps=MAX_EPISODE_STEPS,
)
env.set_command(vx=vx_cmd, vy=vy_cmd, wz=wz_cmd, stand_flag=stand_flag)
obs = env.reset(fixed_stand_pose=(not use_dr))
start_xy = env.data.qpos[0:2].copy()
vx_series = []
wz_series = []
tilts = []
vbuses = []
cum_yaw = 0.0
fell = False
push_map = {step_idx: (dvx, dvy) for step_idx, dvx, dvy in pushes}
for step in range(MAX_EPISODE_STEPS):
if step in push_map:
dvx, dvy = push_map[step]
env.apply_planar_push(dvx, dvy)
act = sess.run(["actions"], {"obs": obs.reshape(1, OBS_DIM).astype(np.float32)})[0][0]
obs, _, term, _, info = env.step(act)
if step >= 35:
vx_series.append(info["vx_body"])
wz_series.append(info["wz_body"])
tilts.append(info["tilt_rad"])
vbuses.append(info["v_bus"])
cum_yaw += info["wz_body"] * CONTROL_DT
if term:
fell = True
ep_lengths.append(step + 1)
break
else:
survivals += 1
ep_lengths.append(MAX_EPISODE_STEPS)
disp = float(np.linalg.norm(env.data.qpos[0:2] - start_xy))
displacements.append(disp)
yaw_totals.append(cum_yaw)
# Measure gait-cycle-averaged (28-step = 0.56s = 1.8Hz stride period) tracking error
win = 28
if len(vx_series) >= win:
kernel = np.ones(win, dtype=np.float64) / win
vx_cyc = np.convolve(np.asarray(vx_series), kernel, mode="valid")
wz_cyc = np.convolve(np.asarray(wz_series), kernel, mode="valid")
vx_maes.append(float(np.mean(np.abs(vx_cyc - vx_cmd))))
wz_maes.append(float(np.mean(np.abs(wz_cyc - wz_cmd))))
else:
vx_maes.append(0.2)
wz_maes.append(0.4)
max_tilts.append(float(np.max(tilts)))
min_vbuses.append(float(np.min(vbuses)))
survival_rate = survivals / float(n_trials)
mean_vx_mae = float(np.mean(vx_maes))
mean_wz_mae = float(np.mean(wz_maes))
mean_max_tilt = float(np.mean(max_tilts))
# Suite score out of 100
score = (
55.0 * survival_rate
+ 25.0 * max(0.0, 1.0 - mean_vx_mae / 0.12)
+ 12.0 * max(0.0, 1.0 - mean_wz_mae / 0.35)
+ 8.0 * max(0.0, 1.0 - mean_max_tilt / 0.45)
)
return {
"suite": suite_name,
"num_trials": n_trials,
"command": {"vx_mps": vx_cmd, "vy_mps": vy_cmd, "wz_rad_s": wz_cmd, "stand_flag": stand_flag},
"survival_rate": round(survival_rate, 4),
"mean_episode_steps": round(float(np.mean(ep_lengths)), 2),
"vx_mae_mps": round(mean_vx_mae, 4),
"wz_mae_rad_s": round(mean_wz_mae, 4),
"mean_planar_displacement_m": round(float(np.mean(displacements)), 4),
"mean_cumulative_yaw_rad": round(float(np.mean(yaw_totals)), 4),
"mean_peak_tilt_rad": round(mean_max_tilt, 4),
"worst_peak_tilt_rad": round(float(np.max(max_tilts)), 4),
"min_spine_5v_bus_v": round(float(np.min(min_vbuses)), 3),
"suite_score_100": round(score, 2),
"passed": bool(survival_rate >= 0.875 and score >= 75.0),
}
def main() -> None:
if not ONNX_PATH.exists():
raise FileNotFoundError(f"Missing policy.onnx at {ONNX_PATH}")
sess = ort.InferenceSession(str(ONNX_PATH), providers=["CPUExecutionProvider"])
suites = [
run_suite(
sess,
suite_name="stand_still",
vx_cmd=0.0,
vy_cmd=0.0,
wz_cmd=0.0,
stand_flag=1.0,
pushes=[],
n_trials=8,
),
run_suite(
sess,
suite_name="walk_forward_015",
vx_cmd=0.15,
vy_cmd=0.0,
wz_cmd=0.0,
stand_flag=0.0,
pushes=[],
n_trials=8,
),
run_suite(
sess,
suite_name="yaw_turn_pos04",
vx_cmd=0.10,
vy_cmd=0.0,
wz_cmd=0.40,
stand_flag=0.0,
pushes=[],
n_trials=8,
),
run_suite(
sess,
suite_name="push_recovery",
vx_cmd=0.15,
vy_cmd=0.0,
wz_cmd=0.0,
stand_flag=0.0,
pushes=[(150, 0.0, 0.20), (325, 0.08, -0.18)],
n_trials=8,
),
]
overall_score = float(np.mean([s["suite_score_100"] for s in suites]))
overall_survival = float(np.mean([s["survival_rate"] for s in suites]))
all_passed = all(s["passed"] for s in suites)
scorecard = {
"schema": "microgoose-arena-scorecard/v1",
"policy_onnx": str(ONNX_PATH),
"total_rollouts": sum(s["num_trials"] for s in suites),
"overall_survival_rate": round(overall_survival, 4),
"composite_arena_score_100": round(overall_score, 2),
"all_suites_passed": all_passed,
"suites": {s["suite"]: s for s in suites},
}
SCORECARD_PATH.parent.mkdir(parents=True, exist_ok=True)
with open(SCORECARD_PATH, "w", encoding="utf-8") as f:
json.dump(scorecard, f, indent=2)
print(f"Saved Arena scorecard -> {SCORECARD_PATH}")
print(
f"Composite Arena Score: {overall_score:.2f}/100 | "
f"Overall Survival: {overall_survival*100:.1f}% | All Passed: {all_passed}"
)
for s in suites:
print(
f" - {s['suite']:18s}: score={s['suite_score_100']:5.1f} "
f"surv={s['survival_rate']*100:5.1f}% vx_mae={s['vx_mae_mps']:.3f} "
f"wz_mae={s['wz_mae_rad_s']:.3f} disp={s['mean_planar_displacement_m']:.2f}m"
)
if __name__ == "__main__":
main()