main
Drew Owens Publish Microgoose Rev A full-stack hardware, CAD, datasheet, and 7-stage RL stack e843249
"""Isaac Lab 3.0 ManagerBasedRLEnvCfg for the 15-DoF Microgoose biped."""

from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Tuple

from .randomizations import DOMAIN_RAND_SPEC, MicrogooseDomainRandSpec

try:
    from isaaclab.utils import configclass
except ImportError:  # Fallback decorator when inspected outside Isaac Sim Python
    def configclass(cls):  # type: ignore
        return dataclass(cls)


ASSET_DIR = Path(__file__).resolve().parents[1] / "assets"
URDF_PATH = str(ASSET_DIR / "microgoose.urdf")
MJCF_PATH = str(ASSET_DIR / "microgoose.xml")

LOCOMOTION_JOINT_NAMES: Tuple[str, ...] = (
    "left_hip_yaw",
    "left_hip_roll",
    "left_hip_pitch",
    "left_knee_pitch",
    "left_ankle_pitch",
    "neck_pitch",
    "head_pitch",
    "head_yaw",
    "head_roll",
    "right_hip_yaw",
    "right_hip_roll",
    "right_hip_pitch",
    "right_knee_pitch",
    "right_ankle_pitch",
)

AUXILIARY_JOINT_NAMES: Tuple[str, ...] = ("beak_pitch",)

DEFAULT_LOCOMOTION_RAD: Tuple[float, ...] = (
    0.0,
    0.0,
    -0.35,
    0.70,
    -0.35,
    0.20,
    -0.20,
    0.0,
    0.0,
    0.0,
    0.0,
    -0.35,
    0.70,
    -0.35,
)

OBSERVATION_SLICES: Tuple[Tuple[str, int, int], ...] = (
    ("base_ang_vel", 0, 3),
    ("projected_gravity", 3, 6),
    ("velocity_commands_xy_yaw", 6, 9),
    ("joint_pos_rel_14", 9, 23),
    ("joint_vel_14", 23, 37),
    ("last_actions_14", 37, 51),
    ("gait_phase_cos_sin", 51, 55),
    ("head_pitch_yaw_roll_cmd_and_stand_flag", 55, 59),
    ("foot_contact_estimate_lr", 59, 61),
)


@configclass
class MicrogooseActuatorCfg:
    """Fitted Rhoban/bam XL330-M288-T actuator configuration."""

    joint_names_expr: Tuple[str, ...] = LOCOMOTION_JOINT_NAMES
    armature: float = 0.0003443
    stiffness: float = 4.4774
    damping: float = 0.11523
    friction: float = 0.0240
    viscous_damping: float = 0.00673
    backlash_deadzone_rad: float = 0.004448
    effort_limit_sim: float = 0.42
    velocity_limit_sim: float = 11.205
    comm_delay_steps: int = 1


@configclass
class MicrogooseCommandsCfg:
    """Velocity and expressive head posture command ranges."""

    resampling_time_range: Tuple[float, float] = (4.0, 7.0)
    stand_still_prob: float = 0.15
    lin_vel_x_range: Tuple[float, float] = (0.0, 0.24)
    lin_vel_y_range: Tuple[float, float] = (-0.06, 0.06)
    ang_vel_z_range: Tuple[float, float] = (-0.55, 0.55)
    head_pitch_range: Tuple[float, float] = (-0.20, 0.20)
    head_yaw_range: Tuple[float, float] = (-0.35, 0.35)
    head_roll_range: Tuple[float, float] = (-0.15, 0.15)


@configclass
class MicrogooseActionsCfg:
    """14-DoF joint position residual targets around default standing posture."""

    joint_names: Tuple[str, ...] = LOCOMOTION_JOINT_NAMES
    action_dim: int = 14
    scale: float = 0.25
    clip: float = 2.0
    use_default_offset: bool = True
    beak_joint_name: str = "beak_pitch"
    beak_default_rad: float = 0.05


@configclass
class MicrogooseObservationsCfg:
    """61-dimensional proprioceptive + command + gait clock observation contract."""

    observation_dim: int = 61
    slices: Tuple[Tuple[str, int, int], ...] = OBSERVATION_SLICES
    ang_vel_noise_std: float = 0.03
    gravity_noise_std: float = 0.015
    joint_pos_noise_std: float = 0.008
    joint_vel_noise_std: float = 0.06


@configclass
class MicrogooseRewardsCfg:
    """Weights for velocity tracking, biped gait clock, clearance, and smoothness."""

    track_lin_vel_xy_weight: float = 2.8
    track_ang_vel_z_weight: float = 1.4
    biped_phase_contact_weight: float = 1.2
    swing_foot_clearance_weight: float = 1.1
    kinematic_gait_ref_weight: float = 1.6
    upright_orientation_weight: float = 1.2
    base_height_target_m: float = 0.135
    base_height_weight: float = 0.8
    head_tracking_weight: float = 0.5
    action_rate_penalty: float = -0.015
    torque_l2_penalty: float = -0.008
    joint_vel_l2_penalty: float = -0.0006
    alive_bonus: float = 0.5


@configclass
class MicrogooseTerminationsCfg:
    """Episode termination thresholds."""

    episode_length_s: float = 10.0
    min_torso_height_m: float = 0.075
    max_tilt_rad: float = 0.65


@configclass
class MicrogooseEnvCfg:
    """Full Isaac Lab 3.0 ManagerBasedRLEnvCfg for Microgoose locomotion."""

    urdf_path: str = URDF_PATH
    mjcf_path: str = MJCF_PATH
    num_envs: int = 4096
    env_spacing: float = 1.2
    physics_dt: float = 0.002
    decimation: int = 10
    control_hz: int = 50
    gait_frequency_hz: float = 1.8
    init_base_pos: Tuple[float, float, float] = (0.0, 0.0, 0.135)
    default_joint_pos: Dict[str, float] = field(
        default_factory=lambda: dict(zip(LOCOMOTION_JOINT_NAMES, DEFAULT_LOCOMOTION_RAD))
    )
    actuator: MicrogooseActuatorCfg = field(default_factory=MicrogooseActuatorCfg)
    commands: MicrogooseCommandsCfg = field(default_factory=MicrogooseCommandsCfg)
    actions: MicrogooseActionsCfg = field(default_factory=MicrogooseActionsCfg)
    observations: MicrogooseObservationsCfg = field(default_factory=MicrogooseObservationsCfg)
    rewards: MicrogooseRewardsCfg = field(default_factory=MicrogooseRewardsCfg)
    terminations: MicrogooseTerminationsCfg = field(default_factory=MicrogooseTerminationsCfg)
    domain_rand: MicrogooseDomainRandSpec = field(default_factory=lambda: DOMAIN_RAND_SPEC)


@configclass
class MicrogooseEnvCfg_PLAY(MicrogooseEnvCfg):
    """Evaluation / interactive play variant with compact scene count."""

    num_envs: int = 32