File size: 5,183 Bytes
3f4bb1d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | """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)
|