@hackbuild/humanoid 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2,3 +2,5 @@ export { MUJOCO_LICENCE, MUJOCO_URL, MUJOCO_VERSION, HumanoidError, loadMujoco,
2
2
  export { fetchModel, filesFrom, referencedFiles, type ModelFiles } from "./files.js";
3
3
  export { Humanoid, type Pose } from "./sim.js";
4
4
  export { G1 } from "./g1.js";
5
+ export { G1_FOLLOW, type FollowMap } from "./follow.js";
6
+ export { restPoses } from "./rest.js";
package/dist/index.js CHANGED
@@ -2,3 +2,5 @@ export { MUJOCO_LICENCE, MUJOCO_URL, MUJOCO_VERSION, HumanoidError, loadMujoco }
2
2
  export { fetchModel, filesFrom, referencedFiles } from "./files.js";
3
3
  export { Humanoid } from "./sim.js";
4
4
  export { G1 } from "./g1.js";
5
+ export { G1_FOLLOW } from "./follow.js";
6
+ export { restPoses } from "./rest.js";
package/dist/rest.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { MjModel } from "./runtime.js";
2
+ import type { Pose } from "./sim.js";
3
+ type Q = [number, number, number, number];
4
+ type V = [number, number, number];
5
+ /** Quaternion product a b, both [w, x, y, z]. */
6
+ export declare function qmul(a: Readonly<Q>, b: Readonly<Q>): Q;
7
+ /** v turned by the unit quaternion q ([w, x, y, z]). */
8
+ export declare function qrotate(q: Readonly<Q>, v: Readonly<V>): V;
9
+ /**
10
+ * Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root
11
+ * where the model places it), from the chain of body offsets. Body 0 is the world.
12
+ */
13
+ export declare function restPoses(model: Pick<MjModel, "nbody" | "body_parentid" | "body_pos" | "body_quat">): Pose[];
14
+ export {};
package/dist/rest.js ADDED
@@ -0,0 +1,31 @@
1
+ /** Quaternion product a b, both [w, x, y, z]. */
2
+ export function qmul(a, b) {
3
+ return [
4
+ a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3],
5
+ a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2],
6
+ a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1],
7
+ a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0],
8
+ ];
9
+ }
10
+ /** v turned by the unit quaternion q ([w, x, y, z]). */
11
+ export function qrotate(q, v) {
12
+ const [w, x, y, z] = q;
13
+ // t = 2 (q.xyz cross v); v + w t + q.xyz cross t
14
+ const tx = 2 * (y * v[2] - z * v[1]), ty = 2 * (z * v[0] - x * v[2]), tz = 2 * (x * v[1] - y * v[0]);
15
+ return [v[0] + w * tx + (y * tz - z * ty), v[1] + w * ty + (z * tx - x * tz), v[2] + w * tz + (x * ty - y * tx)];
16
+ }
17
+ /**
18
+ * Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root
19
+ * where the model places it), from the chain of body offsets. Body 0 is the world.
20
+ */
21
+ export function restPoses(model) {
22
+ const out = [{ pos: [0, 0, 0], quat: [1, 0, 0, 0] }];
23
+ for (let i = 1; i < model.nbody; i++) {
24
+ const parent = out[model.body_parentid[i]]; // parents come before their children
25
+ const p = model.body_pos, q = model.body_quat;
26
+ const local = [q[i * 4], q[i * 4 + 1], q[i * 4 + 2], q[i * 4 + 3]];
27
+ const off = qrotate(parent.quat, [p[i * 3], p[i * 3 + 1], p[i * 3 + 2]]);
28
+ out.push({ pos: [parent.pos[0] + off[0], parent.pos[1] + off[1], parent.pos[2] + off[2]], quat: qmul(parent.quat, local) });
29
+ }
30
+ return out;
31
+ }
package/dist/runtime.d.ts CHANGED
@@ -29,6 +29,12 @@ export interface MjModel {
29
29
  jnt_qposadr: Int32Array;
30
30
  jnt_dofadr: Int32Array;
31
31
  jnt_bodyid: Int32Array;
32
+ /** Each body's parent body; the world is body 0 and its own parent. */
33
+ body_parentid: Int32Array;
34
+ /** Each body's position in its parent's frame at qpos0, 3 per body. */
35
+ body_pos: Float64Array;
36
+ /** Each body's orientation in its parent's frame at qpos0, [w, x, y, z] per body. */
37
+ body_quat: Float64Array;
32
38
  actuator_trnid: Int32Array;
33
39
  /** What each actuator moves: 0 a joint. */
34
40
  actuator_trntype: Int32Array;
@@ -0,0 +1,36 @@
1
+ /** Absolute joint angles in the model's joint order (`G1.joints`) at `t` seconds since the motion started. */
2
+ export type Motion = (t: number) => number[];
3
+ /** Frames the encoder reads, policy order: angles and velocities. */
4
+ export interface ReferenceFrame {
5
+ q: number[];
6
+ dq: number[];
7
+ }
8
+ /**
9
+ * The encoder's ten reference frames for a motion at time `t`: angles at t, t + step, and so on,
10
+ * with velocities by central difference (one sided at 0), both in policy order.
11
+ */
12
+ export declare function referenceFrames(motion: Motion, t: number, step?: number, count?: number): ReferenceFrame[];
13
+ /** Options for {@link squat}. */
14
+ export interface SquatOptions {
15
+ /** Seconds for one squat, down and up. Default 3. */
16
+ period?: number;
17
+ /** How far down, 0 to 1 of the crouch the policy holds (hips -0.7, knees 1.3, ankles -0.6 rad). Default 1. */
18
+ depth?: number;
19
+ }
20
+ /** Squats from the standing angles to the crouch and back, again and again. */
21
+ export declare function squat(o?: SquatOptions): Motion;
22
+ /** Options for {@link wave}. */
23
+ export interface WaveOptions {
24
+ /** Which arm. Default "right". */
25
+ side?: "left" | "right";
26
+ /** Waves a second. Default 1. */
27
+ hz?: number;
28
+ /** Elbow swing either way, radians, at most 1 (inside the elbow's range). Default 0.6; the G1 under the policy swings about 70 percent of it (measured). */
29
+ swing?: number;
30
+ }
31
+ /**
32
+ * Raises one arm out to the side with the forearm up over 0.8 s, then waves it by the elbow.
33
+ * On the G1 a negative shoulder roll takes the right arm out, a negative shoulder yaw then turns
34
+ * its forearm up, and the elbow tips the forearm out and in; the left arm mirrors roll and yaw.
35
+ */
36
+ export declare function wave(o?: WaveOptions): Motion;
@@ -0,0 +1,64 @@
1
+ // Moving references for the GEAR-SONIC driver: a motion is absolute joint
2
+ // angles over time in the G1's joint order, and the encoder reads its next
3
+ // second as ten frames 0.1 s apart with their velocities. Plain functions,
4
+ // no runtime; SonicDriver.play feeds them.
5
+ import { G1 } from "./g1.js";
6
+ import { HumanoidError } from "./runtime.js";
7
+ import { SONIC_JOINTS, toPolicyOrder } from "./sonic-tables.js";
8
+ const positive = (name, v) => {
9
+ if (!(Number.isFinite(v) && v > 0))
10
+ throw new HumanoidError(`${name} must be a positive number of seconds or waves; it was ${v}.`);
11
+ return v;
12
+ };
13
+ const at = (name) => G1.joints.indexOf(name);
14
+ /** Smoothstep from 0 to 1 over `seconds`, so a motion starts from the standing angles without a jump. */
15
+ const ease = (t, seconds) => {
16
+ const x = Math.min(1, Math.max(0, t / seconds));
17
+ return x * x * (3 - 2 * x);
18
+ };
19
+ /**
20
+ * The encoder's ten reference frames for a motion at time `t`: angles at t, t + step, and so on,
21
+ * with velocities by central difference (one sided at 0), both in policy order.
22
+ */
23
+ export function referenceFrames(motion, t, step = 0.1, count = 10) {
24
+ const h = 0.01;
25
+ return Array.from({ length: count }, (_, f) => {
26
+ const tf = t + step * f;
27
+ const lo = Math.max(0, tf - h), hi = tf + h;
28
+ const a = motion(hi), b = motion(lo);
29
+ return { q: toPolicyOrder(motion(tf)), dq: toPolicyOrder(a.map((v, k) => (v - b[k]) / (hi - lo))) };
30
+ });
31
+ }
32
+ /** Squats from the standing angles to the crouch and back, again and again. */
33
+ export function squat(o = {}) {
34
+ const period = positive("period", o.period ?? 3), depth = Math.min(1, Math.max(0, o.depth ?? 1));
35
+ const low = [["hip_pitch", -0.7], ["knee", 1.3], ["ankle_pitch", -0.6]];
36
+ return (t) => {
37
+ const s = depth * (1 - Math.cos((2 * Math.PI * t) / period)) / 2;
38
+ const q = SONIC_JOINTS.defaults.slice();
39
+ for (const side of ["left", "right"])
40
+ for (const [joint, v] of low) {
41
+ const j = at(`${side}_${joint}_joint`);
42
+ q[j] = q[j] + s * (v - q[j]);
43
+ }
44
+ return q;
45
+ };
46
+ }
47
+ /**
48
+ * Raises one arm out to the side with the forearm up over 0.8 s, then waves it by the elbow.
49
+ * On the G1 a negative shoulder roll takes the right arm out, a negative shoulder yaw then turns
50
+ * its forearm up, and the elbow tips the forearm out and in; the left arm mirrors roll and yaw.
51
+ */
52
+ export function wave(o = {}) {
53
+ const s = o.side ?? "right", sign = s === "right" ? -1 : 1;
54
+ // the deploy model's elbow stops at -1.0472 rad
55
+ const hz = positive("hz", o.hz ?? 1), swing = Math.min(1, Math.max(0, o.swing ?? 0.6));
56
+ const roll = at(`${s}_shoulder_roll_joint`), yaw = at(`${s}_shoulder_yaw_joint`), elbow = at(`${s}_elbow_joint`);
57
+ return (t) => {
58
+ const e = ease(t, 0.8), q = SONIC_JOINTS.defaults.slice();
59
+ q[roll] = q[roll] + e * (sign * 1.3 - q[roll]);
60
+ q[yaw] = q[yaw] + e * (sign * 1.5 - q[yaw]);
61
+ q[elbow] = q[elbow] + e * (swing * Math.sin(2 * Math.PI * hz * Math.max(0, t - 0.8)) - q[elbow]);
62
+ return q;
63
+ };
64
+ }
@@ -0,0 +1,41 @@
1
+ type Q = [number, number, number, number];
2
+ /** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
3
+ export declare function intoBody(q: Q, v: [number, number, number]): [number, number, number];
4
+ /**
5
+ * The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
6
+ * the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
7
+ * reference, read row by row.
8
+ */
9
+ export declare function anchorSix(base: Q): number[];
10
+ /**
11
+ * The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
12
+ * angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
13
+ * stay zero. `ref` holds 10 frames, or 1 repeated.
14
+ */
15
+ export declare function encoderInput(ref: {
16
+ q: ArrayLike<number>;
17
+ dq?: ArrayLike<number>;
18
+ }[], anchor: number[]): Float32Array;
19
+ /** One control tick of robot state, in the policy's order. */
20
+ export interface SonicState {
21
+ angularVelocity: number[];
22
+ /** Joint angles minus the default angles. */
23
+ q: number[];
24
+ dq: number[];
25
+ gravity: number[];
26
+ }
27
+ /** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
28
+ export declare class SonicHistory {
29
+ private av;
30
+ private q;
31
+ private dq;
32
+ private act;
33
+ private g;
34
+ /** Fills all ten ticks with one state and zero actions. */
35
+ prime(s: SonicState): void;
36
+ /** Adds a tick's state, with the actions the last tick produced. */
37
+ add(s: SonicState, lastAction: ArrayLike<number>): void;
38
+ /** The decoder's 994 inputs: the token, then each history oldest first. */
39
+ decoderInput(token: ArrayLike<number>): Float32Array;
40
+ }
41
+ export {};
@@ -0,0 +1,90 @@
1
+ // GEAR-SONIC's observations: gravity in the body frame, the anchor without
2
+ // heading, the encoder's g1 mode input and the decoder's ten tick history.
3
+ // Pure functions over arrays.
4
+ /** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
5
+ export function intoBody(q, v) {
6
+ const [w, x, y, z] = q;
7
+ const r = [
8
+ [1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)],
9
+ [2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)],
10
+ [2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)],
11
+ ];
12
+ return [0, 1, 2].map((i) => r[0][i] * v[0] + r[1][i] * v[1] + r[2][i] * v[2]);
13
+ }
14
+ /**
15
+ * The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
16
+ * the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
17
+ * reference, read row by row.
18
+ */
19
+ export function anchorSix(base) {
20
+ const [w, x, y, z] = base;
21
+ const n = Math.max(Math.hypot(w, z), 1e-9);
22
+ const hw = w / n, hz = z / n;
23
+ // base without heading: conj(h) * q; the relative rotation is its conjugate
24
+ const rw = hw * w + hz * z, rx = -(hw * x + hz * y), ry = -(hw * y - hz * x), rz = -(hw * z - hz * w);
25
+ const r00 = 1 - 2 * (ry * ry + rz * rz), r01 = 2 * (rx * ry - rw * rz);
26
+ const r10 = 2 * (rx * ry + rw * rz), r11 = 1 - 2 * (rx * rx + rz * rz);
27
+ const r20 = 2 * (rx * rz - rw * ry), r21 = 2 * (ry * rz + rw * rx);
28
+ return [r00, r01, r10, r11, r20, r21];
29
+ }
30
+ /**
31
+ * The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
32
+ * angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
33
+ * stay zero. `ref` holds 10 frames, or 1 repeated.
34
+ */
35
+ export function encoderInput(ref, anchor) {
36
+ const e = new Float32Array(1762);
37
+ const frame = (f) => ref[Math.min(f, ref.length - 1)];
38
+ let o = 4; // mode id 0, three zeros
39
+ for (let f = 0; f < 10; f++)
40
+ for (let k = 0; k < 29; k++)
41
+ e[o++] = frame(f).q[k];
42
+ for (let f = 0; f < 10; f++)
43
+ for (let k = 0; k < 29; k++)
44
+ e[o++] = frame(f).dq?.[k] ?? 0;
45
+ o += 10 + 1 + 6; // root height windows and the single anchor belong to other modes
46
+ for (let f = 0; f < 10; f++)
47
+ for (let k = 0; k < 6; k++)
48
+ e[o++] = anchor[k];
49
+ return e;
50
+ }
51
+ /** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
52
+ export class SonicHistory {
53
+ av = [];
54
+ q = [];
55
+ dq = [];
56
+ act = [];
57
+ g = [];
58
+ /** Fills all ten ticks with one state and zero actions. */
59
+ prime(s) {
60
+ for (const k of ["av", "q", "dq", "act", "g"])
61
+ this[k] = [];
62
+ for (let i = 0; i < 10; i++)
63
+ this.add(s, new Array(29).fill(0));
64
+ }
65
+ /** Adds a tick's state, with the actions the last tick produced. */
66
+ add(s, lastAction) {
67
+ const keep = (a, v) => {
68
+ a.push(v);
69
+ if (a.length > 10)
70
+ a.shift();
71
+ };
72
+ keep(this.av, s.angularVelocity);
73
+ keep(this.q, s.q);
74
+ keep(this.dq, s.dq);
75
+ keep(this.act, Array.from(lastAction));
76
+ keep(this.g, s.gravity);
77
+ }
78
+ /** The decoder's 994 inputs: the token, then each history oldest first. */
79
+ decoderInput(token) {
80
+ const d = new Float32Array(994);
81
+ let p = 0;
82
+ for (let k = 0; k < 64; k++)
83
+ d[p++] = token[k];
84
+ for (const h of [this.av, this.q, this.dq, this.act, this.g])
85
+ for (const tick of h)
86
+ for (const v of tick)
87
+ d[p++] = v;
88
+ return d;
89
+ }
90
+ }
@@ -0,0 +1,38 @@
1
+ /** The policy files and their terms. */
2
+ export declare const SONIC: {
3
+ readonly name: "gear-sonic, the default g1 controller";
4
+ /** Hugging Face revision the URLs pin. */
5
+ readonly revision: "6733128a3d8a523b1418b06bca3cdf61c8b0987f";
6
+ readonly encoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_encoder.onnx";
7
+ readonly decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx";
8
+ /** Encoder and decoder together, in bytes (vendor: Hugging Face's sizes). */
9
+ readonly bytes: number;
10
+ readonly hz: 50;
11
+ readonly licence: "gear-sonic weights by nvidia. licensed by nvidia corporation under the nvidia open model license (https://huggingface.co/nvidia/GEAR-SONIC/blob/main/LICENSE). they load at run time and are not part of this library.";
12
+ };
13
+ /** The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction. */
14
+ export declare const G1_SONIC: {
15
+ readonly name: "unitree g1, 29 joints, the gear-sonic deploy model";
16
+ /** The scene, at GR00T-WholeBodyControl commit 2a5e6a1; it includes the robot file beside it. */
17
+ readonly url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml";
18
+ /** The meshes are in Git LFS; pass this as `assetBase` to `fetchModel`. */
19
+ readonly assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/";
20
+ readonly licence: "g1 deploy model from nvidia's gr00t-wholebodycontrol (apache-2.0), meshes copyright unitree robotics (bsd-3-clause). they load at run time and are not part of this library.";
21
+ };
22
+ /** The deployment's joint tables, each in the model's joint order (`G1.joints`). */
23
+ export declare const SONIC_JOINTS: {
24
+ /** Standing angles, radians. */
25
+ defaults: number[];
26
+ /** Position gain, N m per rad: armature times (10 Hz in rad/s) squared (computed). */
27
+ kp: number[];
28
+ /** Velocity gain, N m s per rad: 2 times damping ratio 2 times armature times omega (computed). */
29
+ kd: number[];
30
+ /** Torque limit, N m. */
31
+ effort: (5 | 25 | 88 | 139)[];
32
+ /** Radians per unit of action: 0.25 times effort over stiffness (computed). */
33
+ scale: number[];
34
+ /** The policy's (IsaacLab) index of each joint. */
35
+ policyIndex: number[];
36
+ };
37
+ /** A model order array (29) in the policy's order. */
38
+ export declare function toPolicyOrder(v: ArrayLike<number>): number[];
@@ -0,0 +1,60 @@
1
+ // GEAR-SONIC's fixed tables: the pinned policy files, the deploy G1, and the
2
+ // deployment's per joint constants (gains from motor armature, action scale,
3
+ // default angles, policy order). Data only.
4
+ import { G1 } from "./g1.js";
5
+ /** The policy files and their terms. */
6
+ export const SONIC = {
7
+ name: "gear-sonic, the default g1 controller",
8
+ /** Hugging Face revision the URLs pin. */
9
+ revision: "6733128a3d8a523b1418b06bca3cdf61c8b0987f",
10
+ encoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_encoder.onnx",
11
+ decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx",
12
+ /** Encoder and decoder together, in bytes (vendor: Hugging Face's sizes). */
13
+ bytes: 50_100_513 + 40_900_688,
14
+ hz: 50,
15
+ licence: "gear-sonic weights by nvidia. licensed by nvidia corporation under the nvidia open model license (https://huggingface.co/nvidia/GEAR-SONIC/blob/main/LICENSE). they load at run time and are not part of this library.",
16
+ };
17
+ /** The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction. */
18
+ export const G1_SONIC = {
19
+ name: "unitree g1, 29 joints, the gear-sonic deploy model",
20
+ /** The scene, at GR00T-WholeBodyControl commit 2a5e6a1; it includes the robot file beside it. */
21
+ url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml",
22
+ /** The meshes are in Git LFS; pass this as `assetBase` to `fetchModel`. */
23
+ assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/",
24
+ licence: "g1 deploy model from nvidia's gr00t-wholebodycontrol (apache-2.0), meshes copyright unitree robotics (bsd-3-clause). they load at run time and are not part of this library.",
25
+ };
26
+ // motor constants of the deployment parameters: armature (kg m2), effort limit (N m)
27
+ const MOTORS = { m22: [0.025101925, 139], m14: [0.01017752, 88], m5020: [0.003609725, 25], m4010: [0.00425, 5] };
28
+ // each joint's motor, in the model's order (G1.joints)
29
+ const MOTOR_OF = [
30
+ "m22", "m22", "m14", "m22", "m5020", "m5020", "m22", "m22", "m14", "m22", "m5020", "m5020",
31
+ "m14", "m5020", "m5020",
32
+ "m5020", "m5020", "m5020", "m5020", "m5020", "m4010", "m4010",
33
+ "m5020", "m5020", "m5020", "m5020", "m5020", "m4010", "m4010",
34
+ ];
35
+ // ankles and waist roll and pitch run at twice their motor's gains
36
+ const DOUBLED = new Set([4, 5, 10, 11, 13, 14]);
37
+ const OMEGA = 10 * 2 * Math.PI;
38
+ const ZETA = 2;
39
+ /** The deployment's joint tables, each in the model's joint order (`G1.joints`). */
40
+ export const SONIC_JOINTS = {
41
+ /** Standing angles, radians. */
42
+ defaults: [-0.312, 0, 0, 0.669, -0.363, 0, -0.312, 0, 0, 0.669, -0.363, 0, 0, 0, 0, 0.2, 0.2, 0, 0.6, 0, 0, 0, 0.2, -0.2, 0, 0.6, 0, 0, 0],
43
+ /** Position gain, N m per rad: armature times (10 Hz in rad/s) squared (computed). */
44
+ kp: MOTOR_OF.map((m, j) => MOTORS[m][0] * OMEGA * OMEGA * (DOUBLED.has(j) ? 2 : 1)),
45
+ /** Velocity gain, N m s per rad: 2 times damping ratio 2 times armature times omega (computed). */
46
+ kd: MOTOR_OF.map((m, j) => 2 * ZETA * MOTORS[m][0] * OMEGA * (DOUBLED.has(j) ? 2 : 1)),
47
+ /** Torque limit, N m. */
48
+ effort: MOTOR_OF.map((m) => MOTORS[m][1]),
49
+ /** Radians per unit of action: 0.25 times effort over stiffness (computed). */
50
+ scale: MOTOR_OF.map((m) => (0.25 * MOTORS[m][1]) / (MOTORS[m][0] * OMEGA * OMEGA)),
51
+ /** The policy's (IsaacLab) index of each joint. */
52
+ policyIndex: [0, 3, 6, 9, 13, 17, 1, 4, 7, 10, 14, 18, 2, 5, 8, 11, 15, 19, 21, 23, 25, 27, 12, 16, 20, 22, 24, 26, 28],
53
+ };
54
+ /** A model order array (29) in the policy's order. */
55
+ export function toPolicyOrder(v) {
56
+ const out = new Array(29).fill(0);
57
+ for (let j = 0; j < 29; j++)
58
+ out[SONIC_JOINTS.policyIndex[j]] = v[j];
59
+ return out;
60
+ }
package/dist/sonic.d.ts CHANGED
@@ -15,84 +15,11 @@
15
15
  * run time; onnxruntime-web is an optional peer, passed in or loaded from a URL.
16
16
  */
17
17
  import type { Humanoid } from "./sim.js";
18
- /** The policy files and their terms. */
19
- export declare const SONIC: {
20
- readonly name: "gear-sonic, the default g1 controller";
21
- /** Hugging Face revision the URLs pin. */
22
- readonly revision: "6733128a3d8a523b1418b06bca3cdf61c8b0987f";
23
- readonly encoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_encoder.onnx";
24
- readonly decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx";
25
- /** Encoder and decoder together, in bytes (vendor: Hugging Face's sizes). */
26
- readonly bytes: number;
27
- readonly hz: 50;
28
- readonly licence: "gear-sonic weights by nvidia. licensed by nvidia corporation under the nvidia open model license (https://huggingface.co/nvidia/GEAR-SONIC/blob/main/LICENSE). they load at run time and are not part of this library.";
29
- };
30
- /** The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction. */
31
- export declare const G1_SONIC: {
32
- readonly name: "unitree g1, 29 joints, the gear-sonic deploy model";
33
- /** The scene, at GR00T-WholeBodyControl commit 2a5e6a1; it includes the robot file beside it. */
34
- readonly url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml";
35
- /** The meshes are in Git LFS; pass this as `assetBase` to `fetchModel`. */
36
- readonly assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/";
37
- readonly licence: "g1 deploy model from nvidia's gr00t-wholebodycontrol (apache-2.0), meshes copyright unitree robotics (bsd-3-clause). they load at run time and are not part of this library.";
38
- };
39
- /** The deployment's joint tables, each in the model's joint order (`G1.joints`). */
40
- export declare const SONIC_JOINTS: {
41
- /** Standing angles, radians. */
42
- defaults: number[];
43
- /** Position gain, N m per rad: armature times (10 Hz in rad/s) squared (computed). */
44
- kp: number[];
45
- /** Velocity gain, N m s per rad: 2 times damping ratio 2 times armature times omega (computed). */
46
- kd: number[];
47
- /** Torque limit, N m. */
48
- effort: (5 | 25 | 88 | 139)[];
49
- /** Radians per unit of action: 0.25 times effort over stiffness (computed). */
50
- scale: number[];
51
- /** The policy's (IsaacLab) index of each joint. */
52
- policyIndex: number[];
53
- };
54
- /** A model order array (29) in the policy's order. */
55
- export declare function toPolicyOrder(v: ArrayLike<number>): number[];
56
- type Q = [number, number, number, number];
57
- /** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
58
- export declare function intoBody(q: Q, v: [number, number, number]): [number, number, number];
59
- /**
60
- * The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
61
- * the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
62
- * reference, read row by row.
63
- */
64
- export declare function anchorSix(base: Q): number[];
65
- /**
66
- * The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
67
- * angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
68
- * stay zero. `ref` holds 10 frames, or 1 repeated.
69
- */
70
- export declare function encoderInput(ref: {
71
- q: ArrayLike<number>;
72
- dq?: ArrayLike<number>;
73
- }[], anchor: number[]): Float32Array;
74
- /** One control tick of robot state, in the policy's order. */
75
- export interface SonicState {
76
- angularVelocity: number[];
77
- /** Joint angles minus the default angles. */
78
- q: number[];
79
- dq: number[];
80
- gravity: number[];
81
- }
82
- /** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
83
- export declare class SonicHistory {
84
- private av;
85
- private q;
86
- private dq;
87
- private act;
88
- private g;
89
- /** Fills all ten ticks with one state and zero actions. */
90
- prime(s: SonicState): void;
91
- /** Adds a tick's state, with the actions the last tick produced. */
92
- add(s: SonicState, lastAction: ArrayLike<number>): void;
93
- /** The decoder's 994 inputs: the token, then each history oldest first. */
94
- decoderInput(token: ArrayLike<number>): Float32Array;
95
- }
18
+ import { SonicHistory, type SonicState } from "./sonic-obs.js";
19
+ import { type Motion } from "./sonic-motion.js";
20
+ export { G1_SONIC, SONIC, SONIC_JOINTS, toPolicyOrder } from "./sonic-tables.js";
21
+ export { SonicHistory, anchorSix, encoderInput, intoBody, type SonicState } from "./sonic-obs.js";
22
+ export { referenceFrames, squat, wave, type Motion, type ReferenceFrame, type SquatOptions, type WaveOptions } from "./sonic-motion.js";
96
23
  /** The parts of onnxruntime-web this library calls. */
97
24
  export interface Ort {
98
25
  InferenceSession: {
@@ -150,8 +77,11 @@ export declare class Sonic {
150
77
  export declare class SonicDriver {
151
78
  private sonic;
152
79
  readonly sim: Humanoid;
153
- /** The reference pose the policy tracks, absolute angles in the model's joint order. Default the standing angles. */
154
- reference: number[];
80
+ private still;
81
+ private motion;
82
+ private clock;
83
+ /** Bumped by play, so a tick that awaited inference across a restart does not move the new clock. */
84
+ private plays;
155
85
  /** Milliseconds the last tick's inference took (measured). */
156
86
  inferenceMs: number;
157
87
  private adr;
@@ -165,7 +95,25 @@ export declare class SonicDriver {
165
95
  /** Bumped by reset, so a tick that awaited inference across a reset drops its result. */
166
96
  private generation;
167
97
  constructor(sonic: Sonic, sim: Humanoid);
168
- /** The robot back at the standing angles, upright at the origin, and the history primed. */
98
+ /**
99
+ * The reference pose the policy tracks, absolute angles in the model's joint order: the standing
100
+ * angles by default, or where a playing motion is now. Setting it ends a motion.
101
+ */
102
+ get reference(): number[];
103
+ set reference(pose: number[]);
104
+ /**
105
+ * Plays a moving reference from its start: each tick the encoder reads its next second as ten
106
+ * frames 0.1 s apart, with velocities. On the G1 a squat tracks within 2.8 degrees on average and a
107
+ * wave's elbow swings about 70 percent of its range (measured). Setting `reference` or `stop` ends it.
108
+ */
109
+ play(motion: Motion): void;
110
+ /** Ends a motion, holding the pose it had reached. */
111
+ stop(): void;
112
+ /** Whether a motion is playing. */
113
+ get playing(): boolean;
114
+ /** Seconds into the playing motion, 0 when none plays. */
115
+ get motionTime(): number;
116
+ /** The robot back at the standing angles, upright at the origin, and the history primed. A playing motion ends there, the reference back at the standing angles; a still reference is kept. */
169
117
  reset(): void;
170
118
  /** The robot's state this tick, in the policy's order. */
171
119
  state(): SonicState;
@@ -177,4 +125,3 @@ export declare class SonicDriver {
177
125
  */
178
126
  advance(seconds: number): Promise<number>;
179
127
  }
180
- export {};