@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/README.md +37 -7
- package/dist/follow-three.d.ts +61 -0
- package/dist/follow-three.js +135 -0
- package/dist/follow.d.ts +24 -0
- package/dist/follow.js +32 -0
- package/dist/frame.d.ts +12 -0
- package/dist/frame.js +10 -0
- package/dist/humanoid.iife.js +484 -123
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/rest.d.ts +14 -0
- package/dist/rest.js +31 -0
- package/dist/runtime.d.ts +6 -0
- 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/dist/three.d.ts +4 -1
- package/dist/three.js +2 -1
- package/how.ts +2 -0
- package/llms.md +137 -3
- package/package.json +2 -2
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/dist/three.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { Group } from "three";
|
|
10
10
|
import type { Humanoid } from "./sim.js";
|
|
11
|
+
import { type Forward } from "./frame.js";
|
|
11
12
|
/** Options for {@link HumanoidView}. */
|
|
12
13
|
export interface HumanoidViewOptions {
|
|
13
14
|
/** Geom groups to draw. Default 0, 1 and 2, MuJoCo's visible groups. */
|
|
@@ -15,7 +16,7 @@ export interface HumanoidViewOptions {
|
|
|
15
16
|
/** Draw plane geoms (the scene's floor). Default false: the page has its own. */
|
|
16
17
|
floor?: boolean;
|
|
17
18
|
/** The axis the robot faces in its model, turned to +Z. Default "x", as MuJoCo Menagerie's humanoids face. */
|
|
18
|
-
forward?:
|
|
19
|
+
forward?: Forward;
|
|
19
20
|
}
|
|
20
21
|
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
21
22
|
export declare class HumanoidView {
|
|
@@ -34,3 +35,5 @@ export declare class HumanoidView {
|
|
|
34
35
|
/** Removes the group and frees its geometries and materials. */
|
|
35
36
|
dispose(): void;
|
|
36
37
|
}
|
|
38
|
+
export { BodyFollow, type BodyFollowOptions, type FollowBody } from "./follow-three.js";
|
|
39
|
+
export type { Forward } from "./frame.js";
|
package/dist/three.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* space. Collision geoms (group 3 and up) stay hidden unless asked for.
|
|
8
8
|
*/
|
|
9
9
|
import { BoxGeometry, BufferAttribute, BufferGeometry, CapsuleGeometry, CylinderGeometry, Group, Mesh, MeshStandardMaterial, PlaneGeometry, SphereGeometry } from "three";
|
|
10
|
+
import { TURN } from "./frame.js";
|
|
10
11
|
const PLANE = 0, SPHERE = 2, CAPSULE = 3, ELLIPSOID = 4, CYLINDER = 5, BOX = 6, MESH = 7;
|
|
11
|
-
const TURN = { x: -Math.PI / 2, "-x": Math.PI / 2, y: Math.PI, "-y": 0 };
|
|
12
12
|
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
13
13
|
export class HumanoidView {
|
|
14
14
|
sim;
|
|
@@ -107,3 +107,4 @@ export class HumanoidView {
|
|
|
107
107
|
this.meshes = [];
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
+
export { BodyFollow } from "./follow-three.js";
|
package/how.ts
CHANGED
|
@@ -8,6 +8,8 @@ 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.",
|
|
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.",
|
|
11
13
|
],
|
|
12
14
|
packages: ["@hackbuild/humanoid"],
|
|
13
15
|
install: "npm install @hackbuild/humanoid",
|
package/llms.md
CHANGED
|
@@ -45,12 +45,28 @@ function filesFrom(list: Iterable<File>): Promise<ModelFiles>
|
|
|
45
45
|
|
|
46
46
|
A robot from a file list: an `<input type="file" webkitdirectory>` keeps each file's path in the folder; loose files are keyed by name only, so a model whose meshes sit in a folder needs the folder picked.
|
|
47
47
|
|
|
48
|
+
```ts
|
|
49
|
+
interface FollowMap {
|
|
50
|
+
bones: Record<string, string>;
|
|
51
|
+
root: string;
|
|
52
|
+
segments: { bone: string; child: string; from: string; to: string }[];
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Robot bodies to humanoid bones (VRM 1.0 names), and the segments posed to the robot's zero pose first.
|
|
57
|
+
|
|
48
58
|
```ts
|
|
49
59
|
const G1: { readonly bytes: 19666440; readonly joints: readonly ["left_hip_pitch_joint", "left_hip_roll_joint", "left_hip_yaw_joint", "left_knee_joint", "left_ankle_pitch_joint", "left_ankle_roll_joint", "right_hip_pitch_joint", "right_hip_roll_joint", "right_hip_yaw_joint", "right_knee_joint", "right_ankle_pitch_joint", "right_ankle_roll_joint", "waist_yaw_joint", "waist_roll_joint", "waist_pitch_joint", "left_shoulder_pitch_joint", "left_shoulder_roll_joint", "left_shoulder_yaw_joint", "left_elbow_joint", "left_wrist_roll_joint", "left_wrist_pitch_joint", "left_wrist_yaw_joint", "right_shoulder_pitch_joint", "right_shoulder_roll_joint", "right_shoulder_yaw_joint", "right_elbow_joint", "right_wrist_roll_joint", "right_wrist_pitch_joint", "right_wrist_yaw_joint"]; readonly keyframe: "stand"; readonly licence: "unitree g1 model and meshes from mujoco menagerie, bsd-3-clause, copyright unitree robotics. they load at run time and are not part of this library."; readonly name: "unitree g1, 29 joints"; readonly url: "https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/98a70c569c4c1b6da467a20012f766ee1f8dc521/unitree_g1/scene.xml" }
|
|
50
60
|
```
|
|
51
61
|
|
|
52
62
|
The G1 from MuJoCo Menagerie, by URL, with its licence and download size.
|
|
53
63
|
|
|
64
|
+
```ts
|
|
65
|
+
const G1_FOLLOW: FollowMap
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The Unitree G1, both models (MuJoCo Menagerie's and the gear-sonic deploy model share these body names).
|
|
69
|
+
|
|
54
70
|
```ts
|
|
55
71
|
class Humanoid {
|
|
56
72
|
readonly actuated: string[];
|
|
@@ -71,7 +87,7 @@ class Humanoid {
|
|
|
71
87
|
forward(): void;
|
|
72
88
|
hold(): void;
|
|
73
89
|
joint(name: string): { dq: number; q: number } | null;
|
|
74
|
-
moveTo(targets:
|
|
90
|
+
moveTo(targets: Record<string, number> | ArrayLike<number>, seconds?: number): void;
|
|
75
91
|
pose(body: string | number): Pose | null;
|
|
76
92
|
push(vx: number, vy: number, vz?: number): boolean;
|
|
77
93
|
reset(key?: string | number): void;
|
|
@@ -118,6 +134,9 @@ interface MjModel {
|
|
|
118
134
|
actuator_biastype: Int32Array;
|
|
119
135
|
actuator_trnid: Int32Array;
|
|
120
136
|
actuator_trntype: Int32Array;
|
|
137
|
+
body_parentid: Int32Array;
|
|
138
|
+
body_pos: Float64Array;
|
|
139
|
+
body_quat: Float64Array;
|
|
121
140
|
geom_dataid: Int32Array;
|
|
122
141
|
geom_group: Int32Array;
|
|
123
142
|
geom_rgba: Float32Array;
|
|
@@ -232,6 +251,12 @@ function referencedFiles(text: string, dirs?: { assetdir?: string; meshdir?: str
|
|
|
232
251
|
|
|
233
252
|
The files one MJCF names, split into includes and the rest, joined onto its compiler's directories.
|
|
234
253
|
|
|
254
|
+
```ts
|
|
255
|
+
function restPoses(model: Pick<MjModel, "nbody" | "body_parentid" | "body_pos" | "body_quat">): Pose[]
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root where the model places it), from the chain of body offsets.
|
|
259
|
+
|
|
235
260
|
### @hackbuild/humanoid/sonic
|
|
236
261
|
|
|
237
262
|
```ts
|
|
@@ -258,6 +283,12 @@ function intoBody(q: Q, v: [number, number, number]): [number, number, number]
|
|
|
258
283
|
|
|
259
284
|
Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v.
|
|
260
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
|
+
|
|
261
292
|
```ts
|
|
262
293
|
interface Ort {
|
|
263
294
|
env: { wasm: { numThreads?: number; wasmPaths?: string } };
|
|
@@ -283,6 +314,21 @@ interface OrtSession {
|
|
|
283
314
|
|
|
284
315
|
An onnxruntime session.
|
|
285
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
|
+
|
|
286
332
|
```ts
|
|
287
333
|
class Sonic {
|
|
288
334
|
readonly ort: Ort;
|
|
@@ -311,11 +357,15 @@ The deployment's joint tables, each in the model's joint order (`G1.joints`).
|
|
|
311
357
|
class SonicDriver {
|
|
312
358
|
constructor(sonic: Sonic, sim: Humanoid);
|
|
313
359
|
inferenceMs: number;
|
|
314
|
-
reference: number[];
|
|
315
360
|
readonly sim: Humanoid;
|
|
361
|
+
readonly motionTime: number;
|
|
362
|
+
readonly playing: boolean;
|
|
363
|
+
reference: number[];
|
|
316
364
|
advance(seconds: number): Promise<number>;
|
|
365
|
+
play(motion: Motion): void;
|
|
317
366
|
reset(): void;
|
|
318
367
|
state(): SonicState;
|
|
368
|
+
stop(): void;
|
|
319
369
|
tick(): Promise<void>;
|
|
320
370
|
}
|
|
321
371
|
```
|
|
@@ -358,14 +408,81 @@ interface SonicState {
|
|
|
358
408
|
|
|
359
409
|
One control tick of robot state, in the policy's order.
|
|
360
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
|
+
|
|
361
426
|
```ts
|
|
362
427
|
function toPolicyOrder(v: ArrayLike<number>): number[]
|
|
363
428
|
```
|
|
364
429
|
|
|
365
430
|
A model order array (29) in the policy's order.
|
|
366
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
|
+
|
|
367
448
|
### @hackbuild/humanoid/three
|
|
368
449
|
|
|
450
|
+
```ts
|
|
451
|
+
class BodyFollow {
|
|
452
|
+
constructor(sim: Humanoid, body: FollowBody, o?: BodyFollowOptions);
|
|
453
|
+
readonly missing: string[];
|
|
454
|
+
reset(): void;
|
|
455
|
+
update(): void;
|
|
456
|
+
}
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
Drives a humanoid body from a Humanoid: call `update` after each step.
|
|
460
|
+
|
|
461
|
+
```ts
|
|
462
|
+
interface BodyFollowOptions {
|
|
463
|
+
forward?: Forward;
|
|
464
|
+
map?: FollowMap;
|
|
465
|
+
travel?: boolean;
|
|
466
|
+
}
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
Options for BodyFollow.
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
interface FollowBody {
|
|
473
|
+
bones: Partial<Record<string, Object3D>>;
|
|
474
|
+
root: Object3D;
|
|
475
|
+
}
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
A body to drive: its root and its bones by VRM 1.0 name, as `@hackbuild/avatar` loads one.
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
type Forward = "x" | "-x" | "y" | "-y"
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
The axis a robot faces in its model.
|
|
485
|
+
|
|
369
486
|
```ts
|
|
370
487
|
class HumanoidView {
|
|
371
488
|
constructor(sim: Humanoid, o?: HumanoidViewOptions);
|
|
@@ -381,7 +498,7 @@ A three.js group that follows a Humanoid; add `root` to a scene and call `update
|
|
|
381
498
|
```ts
|
|
382
499
|
interface HumanoidViewOptions {
|
|
383
500
|
floor?: boolean;
|
|
384
|
-
forward?:
|
|
501
|
+
forward?: Forward;
|
|
385
502
|
groups?: number[];
|
|
386
503
|
}
|
|
387
504
|
```
|
|
@@ -414,6 +531,23 @@ robot.moveTo({ left_knee_joint: 0.6, right_knee_joint: 0.6, left_hip_pitch_joint
|
|
|
414
531
|
renderer.setAnimationLoop(() => (robot.step(1 / 60), view.update(), renderer.render(scene, camera)));
|
|
415
532
|
```
|
|
416
533
|
|
|
534
|
+
### from the concepts
|
|
535
|
+
|
|
536
|
+
```ts
|
|
537
|
+
import { load } from "@hackbuild/avatar";
|
|
538
|
+
import { loadAvatar } from "@hackbuild/avatar/three";
|
|
539
|
+
import { BodyFollow } from "@hackbuild/humanoid/three";
|
|
540
|
+
|
|
541
|
+
await load(); // the bone mapper's wasm
|
|
542
|
+
const person = await loadAvatar("person.glb");
|
|
543
|
+
person.root.position.x = 0.9; // beside the robot
|
|
544
|
+
scene.add(person.root);
|
|
545
|
+
const follow = new BodyFollow(robot, person);
|
|
546
|
+
// each frame, after the robot steps
|
|
547
|
+
follow.update();
|
|
548
|
+
person.update(dt);
|
|
549
|
+
```
|
|
550
|
+
|
|
417
551
|
## Mistakes to avoid
|
|
418
552
|
|
|
419
553
|
- Do not bundle @mujoco/mujoco or copy mujoco.wasm into the package build; pass its factory from your bundler or let `loadMujoco` load it from its URL.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/humanoid",
|
|
3
|
-
"version": "0.
|
|
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).",
|
|
3
|
+
"version": "0.4.0",
|
|
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",
|
|
7
7
|
"author": "Moheeb Zara <hackbuildvideo@gmail.com>",
|