@hackbuild/humanoid 0.3.0 → 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/README.md +10 -4
- package/dist/humanoid.iife.js +224 -117
- package/dist/sonic-motion.d.ts +36 -0
- package/dist/sonic-motion.js +64 -0
- package/dist/sonic-obs.d.ts +41 -0
- package/dist/sonic-obs.js +90 -0
- package/dist/sonic-tables.d.ts +38 -0
- package/dist/sonic-tables.js +60 -0
- package/dist/sonic.d.ts +29 -82
- package/dist/sonic.js +53 -147
- package/how.ts +1 -0
- package/llms.md +58 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,6 +71,8 @@ An MJCF names other files relative to its compiler's directories: `meshdir` or `
|
|
|
71
71
|
|
|
72
72
|
GEAR-SONIC turns a reference motion into a token (the encoder) and the token plus ten ticks of the robot's state into 29 joint actions (the decoder), 50 times a second; `SonicDriver` turns actions into joint targets (default angle plus action times the joint's action scale) and a PD loop into motor torques at every timestep. It needs the deploy model, `G1_SONIC`, whose joints are on motors; the menagerie G1's position actuators do not take it. `reference` is the pose it tracks, absolute angles in the model's joint order: the standing angles by default, or a crouch, or an arm raised. The code is written from NVIDIA's published deployment description and parameters; the details it leaves open (joint positions relative to the defaults, the reference absolute, histories oldest first, rotations read row by row, the anchor as the base's tilt against an upright reference) were settled by trial on the deploy model, where only that combination stands.
|
|
73
73
|
|
|
74
|
+
A reference can move: `play(motion)` takes a function from seconds to absolute angles, and each tick the encoder reads its next second as ten frames 0.1 s apart with their velocities; setting `reference` or calling `stop()` ends it, holding where it got to. `squat()` and `wave()` are two such motions for the G1, and `referenceFrames` shows what the encoder reads. Fed this way the reference arrives with no lag. A squat tracks closely and survives a shove; a wave's elbow covers most of its range, and a raised arm sags, since the arm joints are soft. The numbers are under What is verified.
|
|
75
|
+
|
|
74
76
|
### the robot
|
|
75
77
|
|
|
76
78
|
`Humanoid.open` compiles the files in MuJoCo's virtual file system. `targets` is the actuators' control array, a live view: for a position actuator it is the joint's target angle. `moveTo` eases the targets it names in over simulated time and leaves the others alone; a sudden jump can topple a robot that a ramp of half a second leaves standing. After writing `qpos` by hand, call `forward()` before reading poses; after `dispose()` the data's arrays must not be read (`disposed` says so). `step(seconds)` runs whole timesteps and carries the remainder, at most 0.1 s a call, so a slow frame does not run away. Poses come back in MuJoCo's frame: z up, meters, quaternions w x y z.
|
|
@@ -109,7 +111,9 @@ person.update(dt);
|
|
|
109
111
|
| [`G1_FOLLOW`](../../docs/api/humanoid/index.md#g1_follow) | the G1's bodies mapped to humanoid bones, and the segments posed to its zero pose |
|
|
110
112
|
| [`restPoses`](../../docs/api/humanoid/index.md#restposes) | every body's world pose at qpos0, from the model alone |
|
|
111
113
|
| [`Sonic`](../../docs/api/humanoid/sonic.md#sonic) | load the GEAR-SONIC policy, from `@hackbuild/humanoid/sonic` |
|
|
112
|
-
| [`SonicDriver`](../../docs/api/humanoid/sonic.md#sonicdriver) | run it on a G1: advance, reference, reset |
|
|
114
|
+
| [`SonicDriver`](../../docs/api/humanoid/sonic.md#sonicdriver) | run it on a G1: advance, reference, play, stop, reset |
|
|
115
|
+
| [`squat`](../../docs/api/humanoid/sonic.md#squat), [`wave`](../../docs/api/humanoid/sonic.md#wave) | moving references for the G1, from `@hackbuild/humanoid/sonic` |
|
|
116
|
+
| [`referenceFrames`](../../docs/api/humanoid/sonic.md#referenceframes) | the ten frames the encoder reads from a motion at a time |
|
|
113
117
|
| [`G1_SONIC`](../../docs/api/humanoid/sonic.md#g1_sonic) | the G1 deploy model the policy runs on: url, asset host, licence |
|
|
114
118
|
| [`SONIC_JOINTS`](../../docs/api/humanoid/sonic.md#sonic_joints) | standing angles, gains, torque limits, action scale and policy order per joint |
|
|
115
119
|
| [`HumanoidError`](../../docs/api/humanoid/index.md#humanoiderror) | a model or runtime this package cannot open, with the reason |
|
|
@@ -152,7 +156,7 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
152
156
|
|
|
153
157
|
| check | result |
|
|
154
158
|
|---|---|
|
|
155
|
-
| `npx vitest run packages/humanoid` |
|
|
159
|
+
| `npx vitest run packages/humanoid` | 32 of 32 tests pass: squat and wave start at the standing angles, move only their joints and stay continuous; reference frames 0.1 s apart with velocities, one sided at 0; a playing motion feeding the encoder its next second, advancing a tick a tick, held by stop and ended by a new reference; play again, stop and reset while a tick awaits inference; a bad period or rate refused, the swing inside the elbow's range; the 7 below; rest poses equal MuJoCo's at qpos0 through turned body frames; a T pose body posed to the robot's zero pose, then every follower segment pointing as the robot's does after hip, knee, shoulder, elbow, wrist, ankle and waist turns and a moved, turned pelvis, the hips, spine, a foot and a hand turned by their robot bodies' turns, the hips travelling at the body's scale, travel off, reset, missing bones and bodies listed, nothing after dispose; unnamed parts named by kind and index, a tendon actuator kept out of the joints, an ease leaving other targets and a controller's output alone, a NaN frame, quoted and commented references and cube faces, a mesh folder reached with `..`, a view after dispose; and for the policy the gains and action scale from motor armature, the policy joint order, gravity in the body frame, the anchor ignoring heading, the encoder and decoder layouts, ten ticks oldest first, a driver on a 29 joint model with a stand in runtime, the refusal of a robot without the G1's joints or with them on servos, and a reset or dispose landing while a tick awaits inference (counted) |
|
|
156
160
|
| the 7 runtime and view tests | 7 of 7 tests pass on MuJoCo 4.0.1 in node: references and compiler directories, a dropped folder with an include and a missing mesh, compiling through the virtual files, names, uneven frames stepped at the timestep, a fall under gravity, a keyframe's pose and controls, an actuator reaching its target, an eased move, hold, a push, the compile error naming missing files, the three.js view's meshes and their motion (counted) |
|
|
157
161
|
| the G1 from MuJoCo Menagerie in headless Chrome on an Apple M1 | MuJoCo loads in 206 ms, the G1's 36 files (19.7 MB) arrive in 886 ms with none missing, it compiles with 29 actuated joints and draws 35 meshes; on its stand keyframe the pelvis holds 0.792 m for 5 s; 0.084 ms per step (measured) |
|
|
158
162
|
| the demo page's own check, headless, MuJoCo from jsDelivr | loads in 362 ms, the G1's files in 528 ms; it stands at 0.792 m and eases into a crouch at 0.671 m at 59.9 frames a second (measured) |
|
|
@@ -160,11 +164,13 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
160
164
|
| the demo page's policy check, headless Chrome | MuJoCo in 332 ms, the deploy G1 in 527 ms, the policy in 3,368 ms; it stands at 0.766 m and is at 0.766 m 4 s after a 1 m/s shove, at 59.9 frames a second, 5.7 ms a tick (measured) |
|
|
161
165
|
| `BodyFollow` on both real G1 models in node | after a crouch every follower segment points within 0.00 degrees of the robot's; the G1's thigh and upper arm segments start at the roll joint while their bones copy the yaw link a few centimetres away, so a hip yaw of 0.6 rad leaves the thigh 2.75 degrees off and a shoulder yaw of 1.0 rad the upper arm 1.85 degrees off (measured, research probe and review) |
|
|
162
166
|
| `BodyFollow` with the dog simulator's person on the docs page, headless Chrome | the person stands beside the G1 and crouches, raises an arm, steps after a shove under the policy and falls with it on its actuators (frame strips looked at; the hips drop 0.918 to 0.774 m in the crouch, measured) |
|
|
167
|
+
| moving references on the deploy G1 in node, real weights | ten frames 0.1 s apart reach the robot with no lag, a pose changed each tick 0.08 s late, and spacings from 0.02 to 0.2 s track alike; squat 2.8 degrees mean error over all joints, knee 34 of 36 degrees; an elbow asked for 29 degrees of bend in a raised arm holds about 14; wave elbow 45 to 49 of 69 degrees, hand sway 14 cm; a 1 m/s shove mid squat caught; four ways to fill the frames and four spacings compared (research probes sonic-motion-trial and sonic-motion-run, measured) |
|
|
168
|
+
| squat and wave on the docs page with the policy, headless Chrome | frame strips looked at: the robot squats over 3 s and waves beside its head, and the person follows both |
|
|
163
169
|
| crouches and shoves on the G1 in node | eased over 0.5 s or more, crouches from hips -0.2 to -0.6 radians all hold (pelvis 0.778 to 0.671 m); the same targets set at once topple it at two depths of five; a 0.3 m/s shove topples it (measured) |
|
|
164
170
|
|
|
165
171
|
### Not verified by anything
|
|
166
172
|
|
|
167
|
-
-
|
|
173
|
+
- Moving references are procedural (squat, wave): no recorded human motion has been converted to the G1's joints, no walking command or the 774 MB planner runs, and the low latency and v1.1 variants are untried.
|
|
168
174
|
- On its actuators alone (no policy) the robot holds targets but does not balance; a shove topples it.
|
|
169
175
|
- A follower copies rotations, so a body with other proportions keeps its own limb lengths: its feet may slide or sink a little against the robot's; no foot contact is enforced. Only the dog simulator's GLB person has been looked at, no VRM.
|
|
170
176
|
- Robots other than the G1 and the test model are untried, and so are textures in MJCF.
|
|
@@ -175,7 +181,7 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
175
181
|
| file | gzip (measured) | ceiling |
|
|
176
182
|
|---|---|---|
|
|
177
183
|
| `dist/index.js` (main entry, imports external) | 7,502 bytes | 7,877 bytes |
|
|
178
|
-
| `dist/humanoid.iife.js` (single file build) |
|
|
184
|
+
| `dist/humanoid.iife.js` (single file build) | 17,011 bytes | 17,861 bytes |
|
|
179
185
|
|
|
180
186
|
Measured 2026-11-30 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
181
187
|
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
package/dist/humanoid.iife.js
CHANGED
|
@@ -822,23 +822,113 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
822
822
|
this.meshes = [];
|
|
823
823
|
}
|
|
824
824
|
};
|
|
825
|
-
// src/sonic.ts
|
|
825
|
+
// src/sonic-obs.ts
|
|
826
|
+
/** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
|
|
827
|
+
function intoBody(q, v) {
|
|
828
|
+
const [w, x, y, z] = q;
|
|
829
|
+
const r = [
|
|
830
|
+
[
|
|
831
|
+
1 - 2 * (y * y + z * z),
|
|
832
|
+
2 * (x * y - w * z),
|
|
833
|
+
2 * (x * z + w * y)
|
|
834
|
+
],
|
|
835
|
+
[
|
|
836
|
+
2 * (x * y + w * z),
|
|
837
|
+
1 - 2 * (x * x + z * z),
|
|
838
|
+
2 * (y * z - w * x)
|
|
839
|
+
],
|
|
840
|
+
[
|
|
841
|
+
2 * (x * z - w * y),
|
|
842
|
+
2 * (y * z + w * x),
|
|
843
|
+
1 - 2 * (x * x + y * y)
|
|
844
|
+
]
|
|
845
|
+
];
|
|
846
|
+
return [
|
|
847
|
+
0,
|
|
848
|
+
1,
|
|
849
|
+
2
|
|
850
|
+
].map((i) => r[0][i] * v[0] + r[1][i] * v[1] + r[2][i] * v[2]);
|
|
851
|
+
}
|
|
826
852
|
/**
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
*
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
853
|
+
* The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
|
|
854
|
+
* the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
|
|
855
|
+
* reference, read row by row.
|
|
856
|
+
*/
|
|
857
|
+
function anchorSix(base) {
|
|
858
|
+
const [w, x, y, z] = base;
|
|
859
|
+
const n = Math.max(Math.hypot(w, z), 1e-9);
|
|
860
|
+
const hw = w / n, hz = z / n;
|
|
861
|
+
const rw = hw * w + hz * z, rx = -(hw * x + hz * y), ry = -(hw * y - hz * x), rz = -(hw * z - hz * w);
|
|
862
|
+
return [
|
|
863
|
+
1 - 2 * (ry * ry + rz * rz),
|
|
864
|
+
2 * (rx * ry - rw * rz),
|
|
865
|
+
2 * (rx * ry + rw * rz),
|
|
866
|
+
1 - 2 * (rx * rx + rz * rz),
|
|
867
|
+
2 * (rx * rz - rw * ry),
|
|
868
|
+
2 * (ry * rz + rw * rx)
|
|
869
|
+
];
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
|
|
873
|
+
* angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
|
|
874
|
+
* stay zero. `ref` holds 10 frames, or 1 repeated.
|
|
841
875
|
*/
|
|
876
|
+
function encoderInput(ref, anchor) {
|
|
877
|
+
const e = /* @__PURE__ */ new Float32Array(1762);
|
|
878
|
+
const frame = (f) => ref[Math.min(f, ref.length - 1)];
|
|
879
|
+
let o = 4;
|
|
880
|
+
for (let f = 0; f < 10; f++) for (let k = 0; k < 29; k++) e[o++] = frame(f).q[k];
|
|
881
|
+
for (let f = 0; f < 10; f++) for (let k = 0; k < 29; k++) e[o++] = frame(f).dq?.[k] ?? 0;
|
|
882
|
+
o += 17;
|
|
883
|
+
for (let f = 0; f < 10; f++) for (let k = 0; k < 6; k++) e[o++] = anchor[k];
|
|
884
|
+
return e;
|
|
885
|
+
}
|
|
886
|
+
/** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
|
|
887
|
+
var SonicHistory = class {
|
|
888
|
+
av = [];
|
|
889
|
+
q = [];
|
|
890
|
+
dq = [];
|
|
891
|
+
act = [];
|
|
892
|
+
g = [];
|
|
893
|
+
/** Fills all ten ticks with one state and zero actions. */
|
|
894
|
+
prime(s) {
|
|
895
|
+
for (const k of [
|
|
896
|
+
"av",
|
|
897
|
+
"q",
|
|
898
|
+
"dq",
|
|
899
|
+
"act",
|
|
900
|
+
"g"
|
|
901
|
+
]) this[k] = [];
|
|
902
|
+
for (let i = 0; i < 10; i++) this.add(s, new Array(29).fill(0));
|
|
903
|
+
}
|
|
904
|
+
/** Adds a tick's state, with the actions the last tick produced. */
|
|
905
|
+
add(s, lastAction) {
|
|
906
|
+
const keep = (a, v) => {
|
|
907
|
+
a.push(v);
|
|
908
|
+
if (a.length > 10) a.shift();
|
|
909
|
+
};
|
|
910
|
+
keep(this.av, s.angularVelocity);
|
|
911
|
+
keep(this.q, s.q);
|
|
912
|
+
keep(this.dq, s.dq);
|
|
913
|
+
keep(this.act, Array.from(lastAction));
|
|
914
|
+
keep(this.g, s.gravity);
|
|
915
|
+
}
|
|
916
|
+
/** The decoder's 994 inputs: the token, then each history oldest first. */
|
|
917
|
+
decoderInput(token) {
|
|
918
|
+
const d = /* @__PURE__ */ new Float32Array(994);
|
|
919
|
+
let p = 0;
|
|
920
|
+
for (let k = 0; k < 64; k++) d[p++] = token[k];
|
|
921
|
+
for (const h of [
|
|
922
|
+
this.av,
|
|
923
|
+
this.q,
|
|
924
|
+
this.dq,
|
|
925
|
+
this.act,
|
|
926
|
+
this.g
|
|
927
|
+
]) for (const tick of h) for (const v of tick) d[p++] = v;
|
|
928
|
+
return d;
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
// src/sonic-tables.ts
|
|
842
932
|
/** The policy files and their terms. */
|
|
843
933
|
const SONIC = {
|
|
844
934
|
name: "gear-sonic, the default g1 controller",
|
|
@@ -987,111 +1077,85 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
987
1077
|
for (let j = 0; j < 29; j++) out[SONIC_JOINTS.policyIndex[j]] = v[j];
|
|
988
1078
|
return out;
|
|
989
1079
|
}
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
1 - 2 * (x * x + z * z),
|
|
1002
|
-
2 * (y * z - w * x)
|
|
1003
|
-
],
|
|
1004
|
-
[
|
|
1005
|
-
2 * (x * z - w * y),
|
|
1006
|
-
2 * (y * z + w * x),
|
|
1007
|
-
1 - 2 * (x * x + y * y)
|
|
1008
|
-
]
|
|
1009
|
-
];
|
|
1010
|
-
return [
|
|
1011
|
-
0,
|
|
1012
|
-
1,
|
|
1013
|
-
2
|
|
1014
|
-
].map((i) => r[0][i] * v[0] + r[1][i] * v[1] + r[2][i] * v[2]);
|
|
1015
|
-
}
|
|
1080
|
+
// src/sonic-motion.ts
|
|
1081
|
+
const positive = (name, v) => {
|
|
1082
|
+
if (!(Number.isFinite(v) && v > 0)) throw new HumanoidError(`${name} must be a positive number of seconds or waves; it was ${v}.`);
|
|
1083
|
+
return v;
|
|
1084
|
+
};
|
|
1085
|
+
const at = (name) => G1.joints.indexOf(name);
|
|
1086
|
+
/** Smoothstep from 0 to 1 over `seconds`, so a motion starts from the standing angles without a jump. */
|
|
1087
|
+
const ease = (t, seconds) => {
|
|
1088
|
+
const x = Math.min(1, Math.max(0, t / seconds));
|
|
1089
|
+
return x * x * (3 - 2 * x);
|
|
1090
|
+
};
|
|
1016
1091
|
/**
|
|
1017
|
-
* The
|
|
1018
|
-
*
|
|
1019
|
-
* reference, read row by row.
|
|
1092
|
+
* The encoder's ten reference frames for a motion at time `t`: angles at t, t + step, and so on,
|
|
1093
|
+
* with velocities by central difference (one sided at 0), both in policy order.
|
|
1020
1094
|
*/
|
|
1021
|
-
function
|
|
1022
|
-
const
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1095
|
+
function referenceFrames(motion, t, step = .1, count = 10) {
|
|
1096
|
+
const h = .01;
|
|
1097
|
+
return Array.from({ length: count }, (_, f) => {
|
|
1098
|
+
const tf = t + step * f;
|
|
1099
|
+
const lo = Math.max(0, tf - h), hi = tf + h;
|
|
1100
|
+
const a = motion(hi), b = motion(lo);
|
|
1101
|
+
return {
|
|
1102
|
+
q: toPolicyOrder(motion(tf)),
|
|
1103
|
+
dq: toPolicyOrder(a.map((v, k) => (v - b[k]) / (hi - lo)))
|
|
1104
|
+
};
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
/** Squats from the standing angles to the crouch and back, again and again. */
|
|
1108
|
+
function squat(o = {}) {
|
|
1109
|
+
const period = positive("period", o.period ?? 3), depth = Math.min(1, Math.max(0, o.depth ?? 1));
|
|
1110
|
+
const low = [
|
|
1111
|
+
["hip_pitch", -.7],
|
|
1112
|
+
["knee", 1.3],
|
|
1113
|
+
["ankle_pitch", -.6]
|
|
1033
1114
|
];
|
|
1115
|
+
return (t) => {
|
|
1116
|
+
const s = depth * (1 - Math.cos(2 * Math.PI * t / period)) / 2;
|
|
1117
|
+
const q = SONIC_JOINTS.defaults.slice();
|
|
1118
|
+
for (const side of ["left", "right"]) for (const [joint, v] of low) {
|
|
1119
|
+
const j = at(`${side}_${joint}_joint`);
|
|
1120
|
+
q[j] = q[j] + s * (v - q[j]);
|
|
1121
|
+
}
|
|
1122
|
+
return q;
|
|
1123
|
+
};
|
|
1034
1124
|
}
|
|
1035
1125
|
/**
|
|
1036
|
-
*
|
|
1037
|
-
*
|
|
1038
|
-
*
|
|
1126
|
+
* Raises one arm out to the side with the forearm up over 0.8 s, then waves it by the elbow.
|
|
1127
|
+
* On the G1 a negative shoulder roll takes the right arm out, a negative shoulder yaw then turns
|
|
1128
|
+
* its forearm up, and the elbow tips the forearm out and in; the left arm mirrors roll and yaw.
|
|
1039
1129
|
*/
|
|
1040
|
-
function
|
|
1041
|
-
const
|
|
1042
|
-
const
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1130
|
+
function wave(o = {}) {
|
|
1131
|
+
const s = o.side ?? "right", sign = s === "right" ? -1 : 1;
|
|
1132
|
+
const hz = positive("hz", o.hz ?? 1), swing = Math.min(1, Math.max(0, o.swing ?? .6));
|
|
1133
|
+
const roll = at(`${s}_shoulder_roll_joint`), yaw = at(`${s}_shoulder_yaw_joint`), elbow = at(`${s}_elbow_joint`);
|
|
1134
|
+
return (t) => {
|
|
1135
|
+
const e = ease(t, .8), q = SONIC_JOINTS.defaults.slice();
|
|
1136
|
+
q[roll] = q[roll] + e * (sign * 1.3 - q[roll]);
|
|
1137
|
+
q[yaw] = q[yaw] + e * (sign * 1.5 - q[yaw]);
|
|
1138
|
+
q[elbow] = q[elbow] + e * (swing * Math.sin(2 * Math.PI * hz * Math.max(0, t - .8)) - q[elbow]);
|
|
1139
|
+
return q;
|
|
1140
|
+
};
|
|
1049
1141
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
}
|
|
1068
|
-
/** Adds a tick's state, with the actions the last tick produced. */
|
|
1069
|
-
add(s, lastAction) {
|
|
1070
|
-
const keep = (a, v) => {
|
|
1071
|
-
a.push(v);
|
|
1072
|
-
if (a.length > 10) a.shift();
|
|
1073
|
-
};
|
|
1074
|
-
keep(this.av, s.angularVelocity);
|
|
1075
|
-
keep(this.q, s.q);
|
|
1076
|
-
keep(this.dq, s.dq);
|
|
1077
|
-
keep(this.act, Array.from(lastAction));
|
|
1078
|
-
keep(this.g, s.gravity);
|
|
1079
|
-
}
|
|
1080
|
-
/** The decoder's 994 inputs: the token, then each history oldest first. */
|
|
1081
|
-
decoderInput(token) {
|
|
1082
|
-
const d = /* @__PURE__ */ new Float32Array(994);
|
|
1083
|
-
let p = 0;
|
|
1084
|
-
for (let k = 0; k < 64; k++) d[p++] = token[k];
|
|
1085
|
-
for (const h of [
|
|
1086
|
-
this.av,
|
|
1087
|
-
this.q,
|
|
1088
|
-
this.dq,
|
|
1089
|
-
this.act,
|
|
1090
|
-
this.g
|
|
1091
|
-
]) for (const tick of h) for (const v of tick) d[p++] = v;
|
|
1092
|
-
return d;
|
|
1093
|
-
}
|
|
1094
|
-
};
|
|
1142
|
+
// src/sonic.ts
|
|
1143
|
+
/**
|
|
1144
|
+
* GEAR-SONIC, NVIDIA's whole body controller for the Unitree G1, run in the
|
|
1145
|
+
* page: an encoder turns a reference motion into a 64 number token, a decoder
|
|
1146
|
+
* turns the token and ten ticks of the robot's state into 29 joint actions,
|
|
1147
|
+
* 50 times a second; a PD loop here turns actions into motor torques at every
|
|
1148
|
+
* MuJoCo step. Written for this library from the published deployment
|
|
1149
|
+
* description and parameters (observation names, sizes and order, joint
|
|
1150
|
+
* orders, gains from motor armature, action scale, default angles). The
|
|
1151
|
+
* details the description leaves open were settled by trial on the deploy
|
|
1152
|
+
* model: joint positions relative to the default angles, the reference in
|
|
1153
|
+
* absolute angles, histories oldest first, a rotation's first two columns
|
|
1154
|
+
* row by row, and the anchor as the base's tilt against an upright reference
|
|
1155
|
+
* that follows the robot's heading (measured: only that combination stands).
|
|
1156
|
+
* The weights are NVIDIA's, under the NVIDIA Open Model License, and load at
|
|
1157
|
+
* run time; onnxruntime-web is an optional peer, passed in or loaded from a URL.
|
|
1158
|
+
*/
|
|
1095
1159
|
/** Where onnxruntime-web loads from when no module is given. */
|
|
1096
1160
|
const ORT_URL = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.wasm.min.mjs";
|
|
1097
1161
|
async function bytesOf(src, f, progress) {
|
|
@@ -1166,8 +1230,11 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
1166
1230
|
var SonicDriver = class {
|
|
1167
1231
|
sonic;
|
|
1168
1232
|
sim;
|
|
1169
|
-
|
|
1170
|
-
|
|
1233
|
+
still = SONIC_JOINTS.defaults.slice();
|
|
1234
|
+
motion = null;
|
|
1235
|
+
clock = 0;
|
|
1236
|
+
/** Bumped by play, so a tick that awaited inference across a restart does not move the new clock. */
|
|
1237
|
+
plays = 0;
|
|
1171
1238
|
/** Milliseconds the last tick's inference took (measured). */
|
|
1172
1239
|
inferenceMs = 0;
|
|
1173
1240
|
adr;
|
|
@@ -1197,10 +1264,44 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
1197
1264
|
this.perTick = Math.max(1, Math.round(1 / SONIC.hz / sim.timestep));
|
|
1198
1265
|
this.reset();
|
|
1199
1266
|
}
|
|
1200
|
-
/**
|
|
1267
|
+
/**
|
|
1268
|
+
* The reference pose the policy tracks, absolute angles in the model's joint order: the standing
|
|
1269
|
+
* angles by default, or where a playing motion is now. Setting it ends a motion.
|
|
1270
|
+
*/
|
|
1271
|
+
get reference() {
|
|
1272
|
+
return this.motion ? this.motion(this.clock) : this.still;
|
|
1273
|
+
}
|
|
1274
|
+
set reference(pose) {
|
|
1275
|
+
this.motion = null;
|
|
1276
|
+
this.still = pose;
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Plays a moving reference from its start: each tick the encoder reads its next second as ten
|
|
1280
|
+
* frames 0.1 s apart, with velocities. On the G1 a squat tracks within 2.8 degrees on average and a
|
|
1281
|
+
* wave's elbow swings about 70 percent of its range (measured). Setting `reference` or `stop` ends it.
|
|
1282
|
+
*/
|
|
1283
|
+
play(motion) {
|
|
1284
|
+
this.motion = motion;
|
|
1285
|
+
this.clock = 0;
|
|
1286
|
+
this.plays++;
|
|
1287
|
+
}
|
|
1288
|
+
/** Ends a motion, holding the pose it had reached. */
|
|
1289
|
+
stop() {
|
|
1290
|
+
if (this.motion) this.reference = this.motion(this.clock);
|
|
1291
|
+
}
|
|
1292
|
+
/** Whether a motion is playing. */
|
|
1293
|
+
get playing() {
|
|
1294
|
+
return this.motion !== null;
|
|
1295
|
+
}
|
|
1296
|
+
/** Seconds into the playing motion, 0 when none plays. */
|
|
1297
|
+
get motionTime() {
|
|
1298
|
+
return this.motion ? this.clock : 0;
|
|
1299
|
+
}
|
|
1300
|
+
/** 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. */
|
|
1201
1301
|
reset() {
|
|
1202
1302
|
const { sim } = this;
|
|
1203
1303
|
if (sim.disposed) return;
|
|
1304
|
+
if (this.motion) this.reference = SONIC_JOINTS.defaults.slice();
|
|
1204
1305
|
this.generation++;
|
|
1205
1306
|
sim.reset();
|
|
1206
1307
|
const d = sim.data;
|
|
@@ -1249,8 +1350,11 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
1249
1350
|
d.qpos[5],
|
|
1250
1351
|
d.qpos[6]
|
|
1251
1352
|
]);
|
|
1252
|
-
const
|
|
1353
|
+
const motion = this.motion, plays = this.plays;
|
|
1354
|
+
const ref = motion ? referenceFrames(motion, this.clock) : [{ q: toPolicyOrder(this.still) }];
|
|
1355
|
+
const a = await this.sonic.act(encoderInput(ref, anchor), this.history);
|
|
1253
1356
|
if (gen !== this.generation || this.sim.disposed) return;
|
|
1357
|
+
if (motion && this.plays === plays && this.motion === motion) this.clock += 1 / SONIC.hz;
|
|
1254
1358
|
this.inferenceMs = performance.now() - t0;
|
|
1255
1359
|
this.last = Float32Array.from(a);
|
|
1256
1360
|
const pi = SONIC_JOINTS.policyIndex;
|
|
@@ -1305,8 +1409,11 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
1305
1409
|
exports.filesFrom = filesFrom;
|
|
1306
1410
|
exports.intoBody = intoBody;
|
|
1307
1411
|
exports.loadMujoco = loadMujoco;
|
|
1412
|
+
exports.referenceFrames = referenceFrames;
|
|
1308
1413
|
exports.referencedFiles = referencedFiles;
|
|
1309
1414
|
exports.restPoses = restPoses;
|
|
1415
|
+
exports.squat = squat;
|
|
1310
1416
|
exports.toPolicyOrder = toPolicyOrder;
|
|
1417
|
+
exports.wave = wave;
|
|
1311
1418
|
return exports;
|
|
1312
1419
|
})({}, globalThis.THREE);
|
|
@@ -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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
154
|
-
|
|
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
|
-
/**
|
|
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 {};
|
package/dist/sonic.js
CHANGED
|
@@ -16,149 +16,12 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import { G1 } from "./g1.js";
|
|
18
18
|
import { HumanoidError } from "./runtime.js";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx",
|
|
26
|
-
/** Encoder and decoder together, in bytes (vendor: Hugging Face's sizes). */
|
|
27
|
-
bytes: 50_100_513 + 40_900_688,
|
|
28
|
-
hz: 50,
|
|
29
|
-
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.",
|
|
30
|
-
};
|
|
31
|
-
/** The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction. */
|
|
32
|
-
export const G1_SONIC = {
|
|
33
|
-
name: "unitree g1, 29 joints, the gear-sonic deploy model",
|
|
34
|
-
/** The scene, at GR00T-WholeBodyControl commit 2a5e6a1; it includes the robot file beside it. */
|
|
35
|
-
url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml",
|
|
36
|
-
/** The meshes are in Git LFS; pass this as `assetBase` to `fetchModel`. */
|
|
37
|
-
assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/",
|
|
38
|
-
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.",
|
|
39
|
-
};
|
|
40
|
-
// motor constants of the deployment parameters: armature (kg m2), effort limit (N m)
|
|
41
|
-
const MOTORS = { m22: [0.025101925, 139], m14: [0.01017752, 88], m5020: [0.003609725, 25], m4010: [0.00425, 5] };
|
|
42
|
-
// each joint's motor, in the model's order (G1.joints)
|
|
43
|
-
const MOTOR_OF = [
|
|
44
|
-
"m22", "m22", "m14", "m22", "m5020", "m5020", "m22", "m22", "m14", "m22", "m5020", "m5020",
|
|
45
|
-
"m14", "m5020", "m5020",
|
|
46
|
-
"m5020", "m5020", "m5020", "m5020", "m5020", "m4010", "m4010",
|
|
47
|
-
"m5020", "m5020", "m5020", "m5020", "m5020", "m4010", "m4010",
|
|
48
|
-
];
|
|
49
|
-
// ankles and waist roll and pitch run at twice their motor's gains
|
|
50
|
-
const DOUBLED = new Set([4, 5, 10, 11, 13, 14]);
|
|
51
|
-
const OMEGA = 10 * 2 * Math.PI;
|
|
52
|
-
const ZETA = 2;
|
|
53
|
-
/** The deployment's joint tables, each in the model's joint order (`G1.joints`). */
|
|
54
|
-
export const SONIC_JOINTS = {
|
|
55
|
-
/** Standing angles, radians. */
|
|
56
|
-
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],
|
|
57
|
-
/** Position gain, N m per rad: armature times (10 Hz in rad/s) squared (computed). */
|
|
58
|
-
kp: MOTOR_OF.map((m, j) => MOTORS[m][0] * OMEGA * OMEGA * (DOUBLED.has(j) ? 2 : 1)),
|
|
59
|
-
/** Velocity gain, N m s per rad: 2 times damping ratio 2 times armature times omega (computed). */
|
|
60
|
-
kd: MOTOR_OF.map((m, j) => 2 * ZETA * MOTORS[m][0] * OMEGA * (DOUBLED.has(j) ? 2 : 1)),
|
|
61
|
-
/** Torque limit, N m. */
|
|
62
|
-
effort: MOTOR_OF.map((m) => MOTORS[m][1]),
|
|
63
|
-
/** Radians per unit of action: 0.25 times effort over stiffness (computed). */
|
|
64
|
-
scale: MOTOR_OF.map((m) => (0.25 * MOTORS[m][1]) / (MOTORS[m][0] * OMEGA * OMEGA)),
|
|
65
|
-
/** The policy's (IsaacLab) index of each joint. */
|
|
66
|
-
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],
|
|
67
|
-
};
|
|
68
|
-
/** A model order array (29) in the policy's order. */
|
|
69
|
-
export function toPolicyOrder(v) {
|
|
70
|
-
const out = new Array(29).fill(0);
|
|
71
|
-
for (let j = 0; j < 29; j++)
|
|
72
|
-
out[SONIC_JOINTS.policyIndex[j]] = v[j];
|
|
73
|
-
return out;
|
|
74
|
-
}
|
|
75
|
-
/** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
|
|
76
|
-
export function intoBody(q, v) {
|
|
77
|
-
const [w, x, y, z] = q;
|
|
78
|
-
const r = [
|
|
79
|
-
[1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)],
|
|
80
|
-
[2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)],
|
|
81
|
-
[2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)],
|
|
82
|
-
];
|
|
83
|
-
return [0, 1, 2].map((i) => r[0][i] * v[0] + r[1][i] * v[1] + r[2][i] * v[2]);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
|
|
87
|
-
* the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
|
|
88
|
-
* reference, read row by row.
|
|
89
|
-
*/
|
|
90
|
-
export function anchorSix(base) {
|
|
91
|
-
const [w, x, y, z] = base;
|
|
92
|
-
const n = Math.max(Math.hypot(w, z), 1e-9);
|
|
93
|
-
const hw = w / n, hz = z / n;
|
|
94
|
-
// base without heading: conj(h) * q; the relative rotation is its conjugate
|
|
95
|
-
const rw = hw * w + hz * z, rx = -(hw * x + hz * y), ry = -(hw * y - hz * x), rz = -(hw * z - hz * w);
|
|
96
|
-
const r00 = 1 - 2 * (ry * ry + rz * rz), r01 = 2 * (rx * ry - rw * rz);
|
|
97
|
-
const r10 = 2 * (rx * ry + rw * rz), r11 = 1 - 2 * (rx * rx + rz * rz);
|
|
98
|
-
const r20 = 2 * (rx * rz - rw * ry), r21 = 2 * (ry * rz + rw * rx);
|
|
99
|
-
return [r00, r01, r10, r11, r20, r21];
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
|
|
103
|
-
* angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
|
|
104
|
-
* stay zero. `ref` holds 10 frames, or 1 repeated.
|
|
105
|
-
*/
|
|
106
|
-
export function encoderInput(ref, anchor) {
|
|
107
|
-
const e = new Float32Array(1762);
|
|
108
|
-
const frame = (f) => ref[Math.min(f, ref.length - 1)];
|
|
109
|
-
let o = 4; // mode id 0, three zeros
|
|
110
|
-
for (let f = 0; f < 10; f++)
|
|
111
|
-
for (let k = 0; k < 29; k++)
|
|
112
|
-
e[o++] = frame(f).q[k];
|
|
113
|
-
for (let f = 0; f < 10; f++)
|
|
114
|
-
for (let k = 0; k < 29; k++)
|
|
115
|
-
e[o++] = frame(f).dq?.[k] ?? 0;
|
|
116
|
-
o += 10 + 1 + 6; // root height windows and the single anchor belong to other modes
|
|
117
|
-
for (let f = 0; f < 10; f++)
|
|
118
|
-
for (let k = 0; k < 6; k++)
|
|
119
|
-
e[o++] = anchor[k];
|
|
120
|
-
return e;
|
|
121
|
-
}
|
|
122
|
-
/** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
|
|
123
|
-
export class SonicHistory {
|
|
124
|
-
av = [];
|
|
125
|
-
q = [];
|
|
126
|
-
dq = [];
|
|
127
|
-
act = [];
|
|
128
|
-
g = [];
|
|
129
|
-
/** Fills all ten ticks with one state and zero actions. */
|
|
130
|
-
prime(s) {
|
|
131
|
-
for (const k of ["av", "q", "dq", "act", "g"])
|
|
132
|
-
this[k] = [];
|
|
133
|
-
for (let i = 0; i < 10; i++)
|
|
134
|
-
this.add(s, new Array(29).fill(0));
|
|
135
|
-
}
|
|
136
|
-
/** Adds a tick's state, with the actions the last tick produced. */
|
|
137
|
-
add(s, lastAction) {
|
|
138
|
-
const keep = (a, v) => {
|
|
139
|
-
a.push(v);
|
|
140
|
-
if (a.length > 10)
|
|
141
|
-
a.shift();
|
|
142
|
-
};
|
|
143
|
-
keep(this.av, s.angularVelocity);
|
|
144
|
-
keep(this.q, s.q);
|
|
145
|
-
keep(this.dq, s.dq);
|
|
146
|
-
keep(this.act, Array.from(lastAction));
|
|
147
|
-
keep(this.g, s.gravity);
|
|
148
|
-
}
|
|
149
|
-
/** The decoder's 994 inputs: the token, then each history oldest first. */
|
|
150
|
-
decoderInput(token) {
|
|
151
|
-
const d = new Float32Array(994);
|
|
152
|
-
let p = 0;
|
|
153
|
-
for (let k = 0; k < 64; k++)
|
|
154
|
-
d[p++] = token[k];
|
|
155
|
-
for (const h of [this.av, this.q, this.dq, this.act, this.g])
|
|
156
|
-
for (const tick of h)
|
|
157
|
-
for (const v of tick)
|
|
158
|
-
d[p++] = v;
|
|
159
|
-
return d;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
19
|
+
import { SonicHistory, anchorSix, encoderInput, intoBody } from "./sonic-obs.js";
|
|
20
|
+
import { referenceFrames } from "./sonic-motion.js";
|
|
21
|
+
import { SONIC, SONIC_JOINTS, toPolicyOrder } from "./sonic-tables.js";
|
|
22
|
+
export { G1_SONIC, SONIC, SONIC_JOINTS, toPolicyOrder } from "./sonic-tables.js";
|
|
23
|
+
export { SonicHistory, anchorSix, encoderInput, intoBody } from "./sonic-obs.js";
|
|
24
|
+
export { referenceFrames, squat, wave } from "./sonic-motion.js";
|
|
162
25
|
/** Where onnxruntime-web loads from when no module is given. */
|
|
163
26
|
export const ORT_URL = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.wasm.min.mjs";
|
|
164
27
|
async function bytesOf(src, f, progress) {
|
|
@@ -235,8 +98,11 @@ export class Sonic {
|
|
|
235
98
|
export class SonicDriver {
|
|
236
99
|
sonic;
|
|
237
100
|
sim;
|
|
238
|
-
|
|
239
|
-
|
|
101
|
+
still = SONIC_JOINTS.defaults.slice();
|
|
102
|
+
motion = null;
|
|
103
|
+
clock = 0;
|
|
104
|
+
/** Bumped by play, so a tick that awaited inference across a restart does not move the new clock. */
|
|
105
|
+
plays = 0;
|
|
240
106
|
/** Milliseconds the last tick's inference took (measured). */
|
|
241
107
|
inferenceMs = 0;
|
|
242
108
|
adr;
|
|
@@ -270,11 +136,47 @@ export class SonicDriver {
|
|
|
270
136
|
this.perTick = Math.max(1, Math.round(1 / SONIC.hz / sim.timestep));
|
|
271
137
|
this.reset();
|
|
272
138
|
}
|
|
273
|
-
/**
|
|
139
|
+
/**
|
|
140
|
+
* The reference pose the policy tracks, absolute angles in the model's joint order: the standing
|
|
141
|
+
* angles by default, or where a playing motion is now. Setting it ends a motion.
|
|
142
|
+
*/
|
|
143
|
+
get reference() {
|
|
144
|
+
return this.motion ? this.motion(this.clock) : this.still;
|
|
145
|
+
}
|
|
146
|
+
set reference(pose) {
|
|
147
|
+
this.motion = null;
|
|
148
|
+
this.still = pose;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Plays a moving reference from its start: each tick the encoder reads its next second as ten
|
|
152
|
+
* frames 0.1 s apart, with velocities. On the G1 a squat tracks within 2.8 degrees on average and a
|
|
153
|
+
* wave's elbow swings about 70 percent of its range (measured). Setting `reference` or `stop` ends it.
|
|
154
|
+
*/
|
|
155
|
+
play(motion) {
|
|
156
|
+
this.motion = motion;
|
|
157
|
+
this.clock = 0;
|
|
158
|
+
this.plays++;
|
|
159
|
+
}
|
|
160
|
+
/** Ends a motion, holding the pose it had reached. */
|
|
161
|
+
stop() {
|
|
162
|
+
if (this.motion)
|
|
163
|
+
this.reference = this.motion(this.clock);
|
|
164
|
+
}
|
|
165
|
+
/** Whether a motion is playing. */
|
|
166
|
+
get playing() {
|
|
167
|
+
return this.motion !== null;
|
|
168
|
+
}
|
|
169
|
+
/** Seconds into the playing motion, 0 when none plays. */
|
|
170
|
+
get motionTime() {
|
|
171
|
+
return this.motion ? this.clock : 0;
|
|
172
|
+
}
|
|
173
|
+
/** 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. */
|
|
274
174
|
reset() {
|
|
275
175
|
const { sim } = this;
|
|
276
176
|
if (sim.disposed)
|
|
277
177
|
return;
|
|
178
|
+
if (this.motion)
|
|
179
|
+
this.reference = SONIC_JOINTS.defaults.slice();
|
|
278
180
|
this.generation++;
|
|
279
181
|
sim.reset();
|
|
280
182
|
const d = sim.data;
|
|
@@ -307,9 +209,13 @@ export class SonicDriver {
|
|
|
307
209
|
const t0 = performance.now();
|
|
308
210
|
this.history.add(this.state(), this.last);
|
|
309
211
|
const anchor = anchorSix([d.qpos[3], d.qpos[4], d.qpos[5], d.qpos[6]]);
|
|
310
|
-
const
|
|
212
|
+
const motion = this.motion, plays = this.plays;
|
|
213
|
+
const ref = motion ? referenceFrames(motion, this.clock) : [{ q: toPolicyOrder(this.still) }];
|
|
214
|
+
const a = await this.sonic.act(encoderInput(ref, anchor), this.history);
|
|
311
215
|
if (gen !== this.generation || this.sim.disposed)
|
|
312
216
|
return;
|
|
217
|
+
if (motion && this.plays === plays && this.motion === motion)
|
|
218
|
+
this.clock += 1 / SONIC.hz;
|
|
313
219
|
this.inferenceMs = performance.now() - t0;
|
|
314
220
|
this.last = Float32Array.from(a);
|
|
315
221
|
const pi = SONIC_JOINTS.policyIndex;
|
package/how.ts
CHANGED
|
@@ -8,6 +8,7 @@ const how: HowDoc = {
|
|
|
8
8
|
"the robot's mjcf and every file it names (includes, meshes, textures) are read from a url or a dropped folder into mujoco's virtual file system, and the model compiles there.",
|
|
9
9
|
"each frame runs whole timesteps of the model and carries the rest; targets go to the position actuators, a keyframe resets the pose, and the view moves one mesh per visible geom.",
|
|
10
10
|
"nvidia's gear-sonic policy can drive the g1 instead: an encoder and a decoder 50 times a second turn a reference pose and the robot's recent state into joint targets, and a pd loop turns those into motor torques at every step, so it balances and catches a shove.",
|
|
11
|
+
"the reference can move: a motion such as a squat or a wave is read ten frames ahead, 0.1 s apart, so the policy tracks it with no lag while it balances.",
|
|
11
12
|
"a humanoid body can follow the robot: its limbs are first turned to the robot's zero pose, then each bone takes the turn its robot body has made since, and the hips move with the pelvis at the body's scale.",
|
|
12
13
|
],
|
|
13
14
|
packages: ["@hackbuild/humanoid"],
|
package/llms.md
CHANGED
|
@@ -87,7 +87,7 @@ class Humanoid {
|
|
|
87
87
|
forward(): void;
|
|
88
88
|
hold(): void;
|
|
89
89
|
joint(name: string): { dq: number; q: number } | null;
|
|
90
|
-
moveTo(targets:
|
|
90
|
+
moveTo(targets: Record<string, number> | ArrayLike<number>, seconds?: number): void;
|
|
91
91
|
pose(body: string | number): Pose | null;
|
|
92
92
|
push(vx: number, vy: number, vz?: number): boolean;
|
|
93
93
|
reset(key?: string | number): void;
|
|
@@ -283,6 +283,12 @@ function intoBody(q: Q, v: [number, number, number]): [number, number, number]
|
|
|
283
283
|
|
|
284
284
|
Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v.
|
|
285
285
|
|
|
286
|
+
```ts
|
|
287
|
+
type Motion = (t: number) => number[]
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Absolute joint angles in the model's joint order (`G1.joints`) at `t` seconds since the motion started.
|
|
291
|
+
|
|
286
292
|
```ts
|
|
287
293
|
interface Ort {
|
|
288
294
|
env: { wasm: { numThreads?: number; wasmPaths?: string } };
|
|
@@ -308,6 +314,21 @@ interface OrtSession {
|
|
|
308
314
|
|
|
309
315
|
An onnxruntime session.
|
|
310
316
|
|
|
317
|
+
```ts
|
|
318
|
+
interface ReferenceFrame {
|
|
319
|
+
dq: number[];
|
|
320
|
+
q: number[];
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Frames the encoder reads, policy order: angles and velocities.
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
function referenceFrames(motion: Motion, t: number, step?: number, count?: number): ReferenceFrame[]
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
The encoder's ten reference frames for a motion at time `t`: angles at t, t + step, and so on, with velocities by central difference (one sided at 0), both in policy order.
|
|
331
|
+
|
|
311
332
|
```ts
|
|
312
333
|
class Sonic {
|
|
313
334
|
readonly ort: Ort;
|
|
@@ -336,11 +357,15 @@ The deployment's joint tables, each in the model's joint order (`G1.joints`).
|
|
|
336
357
|
class SonicDriver {
|
|
337
358
|
constructor(sonic: Sonic, sim: Humanoid);
|
|
338
359
|
inferenceMs: number;
|
|
339
|
-
reference: number[];
|
|
340
360
|
readonly sim: Humanoid;
|
|
361
|
+
readonly motionTime: number;
|
|
362
|
+
readonly playing: boolean;
|
|
363
|
+
reference: number[];
|
|
341
364
|
advance(seconds: number): Promise<number>;
|
|
365
|
+
play(motion: Motion): void;
|
|
342
366
|
reset(): void;
|
|
343
367
|
state(): SonicState;
|
|
368
|
+
stop(): void;
|
|
344
369
|
tick(): Promise<void>;
|
|
345
370
|
}
|
|
346
371
|
```
|
|
@@ -383,12 +408,43 @@ interface SonicState {
|
|
|
383
408
|
|
|
384
409
|
One control tick of robot state, in the policy's order.
|
|
385
410
|
|
|
411
|
+
```ts
|
|
412
|
+
function squat(o?: SquatOptions): Motion
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
Squats from the standing angles to the crouch and back, again and again.
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
interface SquatOptions {
|
|
419
|
+
depth?: number;
|
|
420
|
+
period?: number;
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
Options for squat.
|
|
425
|
+
|
|
386
426
|
```ts
|
|
387
427
|
function toPolicyOrder(v: ArrayLike<number>): number[]
|
|
388
428
|
```
|
|
389
429
|
|
|
390
430
|
A model order array (29) in the policy's order.
|
|
391
431
|
|
|
432
|
+
```ts
|
|
433
|
+
function wave(o?: WaveOptions): Motion
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
Raises one arm out to the side with the forearm up over 0.8 s, then waves it by the elbow.
|
|
437
|
+
|
|
438
|
+
```ts
|
|
439
|
+
interface WaveOptions {
|
|
440
|
+
hz?: number;
|
|
441
|
+
side?: "left" | "right";
|
|
442
|
+
swing?: number;
|
|
443
|
+
}
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
Options for wave.
|
|
447
|
+
|
|
392
448
|
### @hackbuild/humanoid/three
|
|
393
449
|
|
|
394
450
|
```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/humanoid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A physics humanoid in the page: MuJoCo (Google DeepMind's WebAssembly build, loaded at run time and never bundled) runs a robot from its MJCF files, holds joint targets with its position actuators, and gives the body's pose to a three.js view. Starts with the Unitree G1, and runs NVIDIA's GEAR-SONIC policy on it so it balances (weights and onnxruntime-web loaded at run time). A humanoid body (a VRM or GLB person) can follow it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|