"""Pack SONIC v1.1 encoder / decoder vectors.""" from __future__ import annotations from collections import deque import numpy as np from .constants import ( DECODER_DIM, DEFAULT_ANGLES, ENCODER_DIM, ENCODER_OFF, FUTURE_FRAMES, G1_STEP, HIST_FRAMES, LOWER_BODY_ISAAC_IDX, MODE_IDS, NUM_JOINTS, TOKEN_DIM, WRIST_ISAAC_IDX, ) from .math_np import heading_rel_6d class History: """Oldest-first proprio history at control rate (10 frames, step 1).""" def __init__(self) -> None: self.ang_vel = deque(maxlen=HIST_FRAMES) self.q_rel = deque(maxlen=HIST_FRAMES) self.dq = deque(maxlen=HIST_FRAMES) self.action = deque(maxlen=HIST_FRAMES) self.gravity = deque(maxlen=HIST_FRAMES) def push( self, ang_vel: np.ndarray, q_rel: np.ndarray, dq: np.ndarray, action: np.ndarray, gravity: np.ndarray, ) -> None: self.ang_vel.append(np.asarray(ang_vel, dtype=np.float32).reshape(3)) self.q_rel.append(np.asarray(q_rel, dtype=np.float32).reshape(NUM_JOINTS)) self.dq.append(np.asarray(dq, dtype=np.float32).reshape(NUM_JOINTS)) self.action.append(np.asarray(action, dtype=np.float32).reshape(NUM_JOINTS)) self.gravity.append(np.asarray(gravity, dtype=np.float32).reshape(3)) def fill(self, **kwargs) -> None: for _ in range(HIST_FRAMES): self.push(**kwargs) def stack(self, buf: deque) -> np.ndarray: while len(buf) < HIST_FRAMES: buf.appendleft(np.zeros_like(buf[0]) if buf else np.zeros(1, dtype=np.float32)) return np.concatenate(list(buf), axis=0) def _future_idx(t: int, n: int, n_frames: int, step: int) -> np.ndarray: idx = t + np.arange(n_frames) * step return np.clip(idx, 0, n - 1) def pack_encoder( *, mode: str, t: int, joint_pos: np.ndarray, joint_vel: np.ndarray, ref_quat_wxyz: np.ndarray, robot_quat_wxyz: np.ndarray, vr_local_pos: np.ndarray | None, vr_local_quat: np.ndarray | None, ) -> np.ndarray: """Build the 1751-D encoder input. ``joint_pos`` / ``joint_vel`` are (T, 29) in **Isaac Lab** joint order at 50 Hz. ``vr_local_pos`` (T, 3, 3) and ``vr_local_quat`` (T, 3, 4 wxyz) are already expressed in the **reference motion pelvis** frame, matching ``vr_3point_local_target``: "local here means relative to the reference motion root, not the robot root". Teleop only fills ``encoder_modes.teleop.required_observations``; other encoder slots stay zero (GEAR-SONIC deploy). """ enc = np.zeros(ENCODER_DIM, dtype=np.float32) mode_id = MODE_IDS[mode] s, _e = ENCODER_OFF["encoder_mode_4"] enc[s] = float(mode_id) n = joint_pos.shape[0] g1_idx = _future_idx(t, n, FUTURE_FRAMES, G1_STEP) smpl_idx = _future_idx(t, n, FUTURE_FRAMES, 1) fill_g1 = mode == "g1" fill_teleop = mode == "teleop" fill_smpl = mode == "smpl" if fill_g1: a, b = ENCODER_OFF["motion_joint_positions_10frame_step5"] enc[a:b] = joint_pos[g1_idx].reshape(-1) a, b = ENCODER_OFF["motion_joint_velocities_10frame_step5"] enc[a:b] = joint_vel[g1_idx].reshape(-1) ori_mf = np.stack( [heading_rel_6d(robot_quat_wxyz, ref_quat_wxyz[i]) for i in g1_idx], axis=0, ).reshape(-1) a, b = ENCODER_OFF["motion_anchor_orientation_heading_10frame_step5"] enc[a:b] = ori_mf if fill_teleop: a, b = ENCODER_OFF["motion_anchor_orientation_heading"] enc[a:b] = heading_rel_6d(robot_quat_wxyz, ref_quat_wxyz[t]) a, b = ENCODER_OFF["motion_joint_positions_lowerbody_10frame_step5"] enc[a:b] = joint_pos[g1_idx][:, LOWER_BODY_ISAAC_IDX].reshape(-1) a, b = ENCODER_OFF["motion_joint_velocities_lowerbody_10frame_step5"] enc[a:b] = joint_vel[g1_idx][:, LOWER_BODY_ISAAC_IDX].reshape(-1) if fill_smpl: a, b = ENCODER_OFF["motion_joint_positions_wrists_10frame_step1"] enc[a:b] = joint_pos[smpl_idx][:, WRIST_ISAAC_IDX].reshape(-1) if fill_teleop and vr_local_pos is not None: a, b = ENCODER_OFF["vr_3point_local_target"] enc[a:b] = np.asarray(vr_local_pos[t], dtype=np.float64).reshape(-1) if fill_teleop and vr_local_quat is not None: a, b = ENCODER_OFF["vr_3point_local_orn_target"] enc[a:b] = np.asarray(vr_local_quat[t], dtype=np.float64).reshape(-1) return enc def pack_decoder(token: np.ndarray, hist: History) -> np.ndarray: dec = np.zeros(DECODER_DIM, dtype=np.float32) dec[:TOKEN_DIM] = np.asarray(token, dtype=np.float32).reshape(TOKEN_DIM) # PolicyCfg term order: base_ang_vel, joint_pos, joint_vel, actions, gravity_dir. i = TOKEN_DIM for buf in (hist.ang_vel, hist.q_rel, hist.dq, hist.action, hist.gravity): stacked = hist.stack(buf) dec[i : i + stacked.size] = stacked i += stacked.size if i != DECODER_DIM: raise RuntimeError(f"decoder pack {i} != {DECODER_DIM}") return dec def default_q() -> np.ndarray: return np.asarray(DEFAULT_ANGLES, dtype=np.float64)