@hackbuild/humanoid 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +157 -2
- package/dist/files.d.ts +38 -0
- package/dist/files.js +130 -0
- package/dist/g1.d.ts +18 -0
- package/dist/g1.js +24 -0
- package/dist/humanoid.iife.js +1000 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +55 -0
- package/dist/sim.d.ts +77 -0
- package/dist/sim.js +200 -0
- package/dist/single.d.ts +3 -0
- package/dist/single.js +5 -0
- package/dist/sonic.d.ts +173 -0
- package/dist/sonic.js +329 -0
- package/dist/three.d.ts +36 -0
- package/dist/three.js +104 -0
- package/how.ts +30 -0
- package/llms.md +426 -0
- package/package.json +58 -4
package/llms.md
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
# @hackbuild/humanoid
|
|
2
|
+
|
|
3
|
+
> Run a physics humanoid in the page: MuJoCo simulates a robot from its MJCF files, NVIDIA's GEAR-SONIC policy balances the Unitree G1, and you pose it, shove it and draw it.
|
|
4
|
+
|
|
5
|
+
Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples of hackbuild-motion. Human docs: https://hackbuild.github.io/hackbuild-motion/docs/humanoid/
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
- Loads MuJoCo, Google DeepMind's WebAssembly build (`@mujoco/mujoco`, single threaded, so the page needs no cross-origin isolation), at run time: from a factory your bundler imports, or from a URL. It is never bundled.
|
|
10
|
+
- Reads a robot from the URL of its MJCF or from a dropped folder, with every file the MJCF names (includes, meshes, textures), keyed the way MuJoCo's virtual file system asks for them; files that do not arrive are listed.
|
|
11
|
+
- Steps the robot at the model's own timestep however the page's frames fall, resets it to a keyframe, writes joint targets to its position actuators, holds its current pose, shoves it, and reads any body's world pose.
|
|
12
|
+
- Draws it in three.js from the `./three` subpath: one mesh per visible geom, MuJoCo's meshes shared, turned from MuJoCo's z up into y up with the robot facing +Z.
|
|
13
|
+
- Names the Unitree G1 (29 joints) from MuJoCo Menagerie, pinned to one commit, as a ready robot; its files load from GitHub at run time.
|
|
14
|
+
- Runs GEAR-SONIC, NVIDIA's whole body controller for the G1, from the `./sonic` subpath: an encoder and a decoder 50 times a second through onnxruntime-web and a PD loop at every MuJoCo step, so the G1 stands, holds a crouch or a raised arm, and steps to catch a shove. The weights load from Hugging Face at run time.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @hackbuild/humanoid
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
It has no dependencies. MuJoCo, onnxruntime-web and three.js are optional peers: install `@mujoco/mujoco` to import its factory, or let `loadMujoco` load it from `https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js`, with `mujoco.wasm` read beside it. `onLicence` receives the line to show. The G1's files come from GitHub when you load it.
|
|
23
|
+
|
|
24
|
+
| download | size |
|
|
25
|
+
|---|---|
|
|
26
|
+
| mujoco.wasm | 11.2 MB, 2.8 MB gzip (measured) |
|
|
27
|
+
| the G1's meshes | 19.7 MB (vendor) |
|
|
28
|
+
| GEAR-SONIC encoder and decoder | 91.0 MB (vendor), from Hugging Face, pinned to one revision |
|
|
29
|
+
|
|
30
|
+
`Sonic.load` loads onnxruntime-web from jsDelivr when you pass no `ort` module, and the policy files from their pinned URLs when you pass no bytes.
|
|
31
|
+
|
|
32
|
+
## Exports
|
|
33
|
+
|
|
34
|
+
### @hackbuild/humanoid
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
function fetchModel(url: string, o?: { assetBase?: string; fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>; onFile?: (path: string, done: number, of: number) => void }): Promise<ModelFiles>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
A robot from the URL of its main MJCF; every file it names is fetched beside it, or for files that are not MJCF beside `assetBase` when given (a repository whose meshes live on another host, such as Git LFS).
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
function filesFrom(list: Iterable<File>): Promise<ModelFiles>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
A robot from dropped files (a folder keeps its paths): the main MJCF is the one with a worldbody that no other file includes.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
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
|
+
```
|
|
51
|
+
|
|
52
|
+
The G1 from MuJoCo Menagerie, by URL, with its licence and download size.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
class Humanoid {
|
|
56
|
+
readonly actuated: string[];
|
|
57
|
+
readonly bodies: string[];
|
|
58
|
+
readonly data: MjData;
|
|
59
|
+
readonly joints: string[];
|
|
60
|
+
readonly keyframes: string[];
|
|
61
|
+
readonly missing: string[];
|
|
62
|
+
readonly model: MjModel;
|
|
63
|
+
readonly timestep: number;
|
|
64
|
+
readonly moving: boolean;
|
|
65
|
+
readonly rootBody: string | null;
|
|
66
|
+
readonly targets: Float64Array;
|
|
67
|
+
readonly time: number;
|
|
68
|
+
address(joint: string): { qpos: number; qvel: number } | null;
|
|
69
|
+
dispose(): void;
|
|
70
|
+
hold(): void;
|
|
71
|
+
joint(name: string): { dq: number; q: number } | null;
|
|
72
|
+
moveTo(targets: ArrayLike<number> | Record<string, number>, seconds?: number): void;
|
|
73
|
+
pose(body: string | number): Pose | null;
|
|
74
|
+
push(vx: number, vy: number, vz?: number): boolean;
|
|
75
|
+
reset(key?: string | number): void;
|
|
76
|
+
run(n: number, control?: (sim: Humanoid) => void): void;
|
|
77
|
+
step(seconds: number, max?: number): number;
|
|
78
|
+
static open(mj: Mujoco, files: Pick<ModelFiles, "xml" | "assets"> & { missing?: string[] }): Humanoid;
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
A robot running in MuJoCo.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
class HumanoidError extends Error {
|
|
86
|
+
constructor(message: string);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
A model or runtime this library cannot open, with the reason in words a page can show.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
function loadMujoco(o?: MujocoOptions): Promise<Mujoco>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Loads MuJoCo once per factory or URL and resolves to the module.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
interface MjData {
|
|
100
|
+
ctrl: Float64Array;
|
|
101
|
+
geom_xmat: Float64Array;
|
|
102
|
+
geom_xpos: Float64Array;
|
|
103
|
+
qpos: Float64Array;
|
|
104
|
+
qvel: Float64Array;
|
|
105
|
+
time: number;
|
|
106
|
+
xpos: Float64Array;
|
|
107
|
+
xquat: Float64Array;
|
|
108
|
+
delete(): void;
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
A model's state; the arrays are views on the wasm heap, live after each step.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
interface MjModel {
|
|
116
|
+
actuator_trnid: Int32Array;
|
|
117
|
+
geom_dataid: Int32Array;
|
|
118
|
+
geom_group: Int32Array;
|
|
119
|
+
geom_rgba: Float32Array;
|
|
120
|
+
geom_size: Float64Array;
|
|
121
|
+
geom_type: Int32Array;
|
|
122
|
+
jnt_bodyid: Int32Array;
|
|
123
|
+
jnt_dofadr: Int32Array;
|
|
124
|
+
jnt_qposadr: Int32Array;
|
|
125
|
+
jnt_type: Int32Array;
|
|
126
|
+
mesh_face: Int32Array;
|
|
127
|
+
mesh_faceadr: Int32Array;
|
|
128
|
+
mesh_facenum: Int32Array;
|
|
129
|
+
mesh_vert: Float32Array;
|
|
130
|
+
mesh_vertadr: Int32Array;
|
|
131
|
+
mesh_vertnum: Int32Array;
|
|
132
|
+
nbody: number;
|
|
133
|
+
ngeom: number;
|
|
134
|
+
njnt: number;
|
|
135
|
+
nkey: number;
|
|
136
|
+
nmesh: number;
|
|
137
|
+
nq: number;
|
|
138
|
+
nu: number;
|
|
139
|
+
nv: number;
|
|
140
|
+
opt: { timestep: number };
|
|
141
|
+
delete(): void;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
A compiled model: sizes, options and the arrays this library reads.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
interface MjVFS {
|
|
149
|
+
addBuffer(name: string, bytes: Uint8Array): void;
|
|
150
|
+
delete(): void;
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
MuJoCo's virtual file system, for the files a model names.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
interface ModelFiles {
|
|
158
|
+
assets: Map<string, Uint8Array<ArrayBufferLike>>;
|
|
159
|
+
missing: string[];
|
|
160
|
+
xml: string;
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
A robot's main MJCF and the files it names.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
interface Mujoco {
|
|
168
|
+
MjData: new (model: MjModel) => MjData;
|
|
169
|
+
MjModel: { from_xml_string(xml: string, vfs?: MjVFS): MjModel };
|
|
170
|
+
mjtObj: Record<string, { value: number }>;
|
|
171
|
+
MjVFS: new () => MjVFS;
|
|
172
|
+
mj_forward(model: MjModel, data: MjData): void;
|
|
173
|
+
mj_id2name(model: MjModel, type: number, id: number): string;
|
|
174
|
+
mj_resetData(model: MjModel, data: MjData): void;
|
|
175
|
+
mj_resetDataKeyframe(model: MjModel, data: MjData, key: number): void;
|
|
176
|
+
mj_step(model: MjModel, data: MjData): void;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The MuJoCo module: the classes and functions this library calls.
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
const MUJOCO_LICENCE: "mujoco by google deepmind, under the apache license 2.0 (https://github.com/google-deepmind/mujoco). it loads at run time and is not part of this library."
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The line a page shows the first time MuJoCo loads.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
const MUJOCO_URL: "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js"
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Where MuJoCo loads from when no factory is given.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
const MUJOCO_VERSION: "4.0.1"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
The @mujoco/mujoco version the default URL pins.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
interface MujocoOptions {
|
|
202
|
+
factory?: (options?: Record<string, unknown>) => Promise<unknown>;
|
|
203
|
+
onLicence?: (line: string) => void;
|
|
204
|
+
url?: string;
|
|
205
|
+
wasm?: string | ArrayBuffer | Uint8Array<ArrayBufferLike>;
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Options for loadMujoco.
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
interface Pose {
|
|
213
|
+
pos: [number, number, number];
|
|
214
|
+
quat: [number, number, number, number];
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
A body's world pose: position x y z and quaternion w x y z, in MuJoCo's frame.
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
function referencedFiles(xml: string, dirs?: { assetdir?: string; meshdir?: string; texturedir?: string }): { files: string[]; includes: string[] }
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
The files one MJCF names, split into includes and the rest, joined onto its compiler's directories.
|
|
225
|
+
|
|
226
|
+
### @hackbuild/humanoid/sonic
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
function anchorSix(base: Q): number[]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
The anchor observation for an upright reference: the base's tilt with its heading removed (heading is the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to reference, read row by row.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
function encoderInput(ref: { dq?: ArrayLike<number>; q: ArrayLike<number> }[], anchor: number[]): Float32Array
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts stay zero.
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const G1_SONIC: { readonly assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/"; 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."; readonly name: "unitree g1, 29 joints, the gear-sonic deploy model"; readonly url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml" }
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction.
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
function intoBody(q: Q, v: [number, number, number]): [number, number, number]
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v.
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
interface Ort {
|
|
254
|
+
env: { wasm: { numThreads?: number; wasmPaths?: string } };
|
|
255
|
+
InferenceSession: { create(model: Uint8Array, options?: Record<string, unknown>): Promise<OrtSession> };
|
|
256
|
+
Tensor: new (type: "float32", data: Float32Array, dims: number[]) => unknown;
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
The parts of onnxruntime-web this library calls.
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
const ORT_URL: "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.wasm.min.mjs"
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Where onnxruntime-web loads from when no module is given.
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
interface OrtSession {
|
|
270
|
+
release?(): Promise<void>;
|
|
271
|
+
run(feeds: Record<string, unknown>): Promise<Record<string, { data: unknown }>>;
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
An onnxruntime session.
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
class Sonic {
|
|
279
|
+
readonly ort: Ort;
|
|
280
|
+
act(encoderIn: Float32Array, history: SonicHistory): Promise<Float32Array<ArrayBufferLike>>;
|
|
281
|
+
dispose(): Promise<void>;
|
|
282
|
+
drive(sim: Humanoid): SonicDriver;
|
|
283
|
+
static load(o?: SonicOptions): Promise<Sonic>;
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
The loaded policy: an encoder and a decoder session.
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
const SONIC: { readonly bytes: number; readonly decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx"; readonly encoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_encoder.onnx"; readonly hz: 50; 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."; readonly name: "gear-sonic, the default g1 controller"; readonly revision: "6733128a3d8a523b1418b06bca3cdf61c8b0987f" }
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
The policy files and their terms.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
const SONIC_JOINTS: { defaults: number[]; effort: (5 | 25 | 88 | 139)[]; kd: number[]; kp: number[]; policyIndex: number[]; scale: number[] }
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
The deployment's joint tables, each in the model's joint order (`G1.joints`).
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
class SonicDriver {
|
|
303
|
+
constructor(sonic: Sonic, sim: Humanoid);
|
|
304
|
+
inferenceMs: number;
|
|
305
|
+
reference: number[];
|
|
306
|
+
readonly sim: Humanoid;
|
|
307
|
+
advance(seconds: number): Promise<number>;
|
|
308
|
+
reset(): void;
|
|
309
|
+
state(): SonicState;
|
|
310
|
+
tick(): Promise<void>;
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
Runs the policy on one robot: 50 ticks a second, the PD loop at every timestep between.
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
class SonicHistory {
|
|
318
|
+
constructor();
|
|
319
|
+
add(s: SonicState, lastAction: ArrayLike<number>): void;
|
|
320
|
+
decoderInput(token: ArrayLike<number>): Float32Array;
|
|
321
|
+
prime(s: SonicState): void;
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Ten ticks of state and actions, oldest first, laid out as the decoder reads them.
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
interface SonicOptions {
|
|
329
|
+
decoder?: string | ArrayBuffer | Uint8Array<ArrayBufferLike>;
|
|
330
|
+
encoder?: string | ArrayBuffer | Uint8Array<ArrayBufferLike>;
|
|
331
|
+
fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
332
|
+
onLicence?: (line: string) => void;
|
|
333
|
+
onProgress?: (loaded: number, total: number) => void;
|
|
334
|
+
ort?: Ort;
|
|
335
|
+
ortUrl?: string;
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
Options for Sonic.load.
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
interface SonicState {
|
|
343
|
+
angularVelocity: number[];
|
|
344
|
+
dq: number[];
|
|
345
|
+
gravity: number[];
|
|
346
|
+
q: number[];
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
One control tick of robot state, in the policy's order.
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
function toPolicyOrder(v: ArrayLike<number>): number[]
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
A model order array (29) in the policy's order.
|
|
357
|
+
|
|
358
|
+
### @hackbuild/humanoid/three
|
|
359
|
+
|
|
360
|
+
```ts
|
|
361
|
+
class HumanoidView {
|
|
362
|
+
constructor(sim: Humanoid, o?: HumanoidViewOptions);
|
|
363
|
+
readonly root: Group<Object3DEventMap>;
|
|
364
|
+
readonly count: number;
|
|
365
|
+
dispose(): void;
|
|
366
|
+
update(): void;
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
A three.js group that follows a Humanoid; add `root` to a scene and call `update` after each step.
|
|
371
|
+
|
|
372
|
+
```ts
|
|
373
|
+
interface HumanoidViewOptions {
|
|
374
|
+
floor?: boolean;
|
|
375
|
+
forward?: "x" | "-x" | "y" | "-y";
|
|
376
|
+
groups?: number[];
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Options for HumanoidView.
|
|
381
|
+
|
|
382
|
+
## Examples
|
|
383
|
+
|
|
384
|
+
### quick start
|
|
385
|
+
|
|
386
|
+
```ts
|
|
387
|
+
import * as THREE from "three";
|
|
388
|
+
import { G1, Humanoid, fetchModel, loadMujoco } from "@hackbuild/humanoid";
|
|
389
|
+
import { HumanoidView } from "@hackbuild/humanoid/three";
|
|
390
|
+
|
|
391
|
+
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
|
392
|
+
renderer.setSize(innerWidth, innerHeight);
|
|
393
|
+
document.body.append(renderer.domElement);
|
|
394
|
+
const scene = new THREE.Scene().add(new THREE.HemisphereLight(0xffffff, 0x887766, 2));
|
|
395
|
+
const camera = new THREE.PerspectiveCamera(35, innerWidth / innerHeight, 0.05, 50).translateZ(2.6).translateY(0.8);
|
|
396
|
+
|
|
397
|
+
// mujoco loads from jsdelivr and the g1's files from github, at run time
|
|
398
|
+
const mj = await loadMujoco({ onLicence: (line) => console.info(line) });
|
|
399
|
+
const robot = Humanoid.open(mj, await fetchModel(G1.url));
|
|
400
|
+
robot.reset(G1.keyframe); // upright, arms down
|
|
401
|
+
const view = new HumanoidView(robot);
|
|
402
|
+
scene.add(view.root);
|
|
403
|
+
// bend the knees through the position actuators, eased over 0.8 s
|
|
404
|
+
robot.moveTo({ left_knee_joint: 0.6, right_knee_joint: 0.6, left_hip_pitch_joint: -0.3, right_hip_pitch_joint: -0.3 }, 0.8);
|
|
405
|
+
renderer.setAnimationLoop(() => (robot.step(1 / 60), view.update(), renderer.render(scene, camera)));
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Mistakes to avoid
|
|
409
|
+
|
|
410
|
+
- 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.
|
|
411
|
+
- Do not use the `/mt` MuJoCo build; it needs SharedArrayBuffer and cross-origin isolation. This package uses the single threaded one.
|
|
412
|
+
- Do not commit the G1's meshes or any robot's files to an app; load them from `G1.url` or from a drop, and show the robot's licence.
|
|
413
|
+
- Call `dispose()` on a Humanoid you are done with; MuJoCo's model and data live on the wasm heap and are not garbage collected.
|
|
414
|
+
- Read poses after `step()`; they are MuJoCo's frame (z up), and the three.js view turns them to y up for you.
|
|
415
|
+
- Do not give the GEAR-SONIC driver the menagerie G1; it needs the deploy model (`G1_SONIC.url` with `assetBase: G1_SONIC.assetBase`), whose joints are on motors.
|
|
416
|
+
- Do not bundle or commit the GEAR-SONIC weights; load them from their pinned Hugging Face URLs and show `SONIC.licence`.
|
|
417
|
+
- Write targets in the actuators' order (`actuated`), not the model's joint order; for the G1 they are the same 29 joints in `G1.joints`.
|
|
418
|
+
|
|
419
|
+
## Errors this package throws
|
|
420
|
+
|
|
421
|
+
- `mujoco did not load from ...` (offline, or the URL is blocked): install `@mujoco/mujoco` and pass its factory, or serve it and pass `url`
|
|
422
|
+
- `no actuator drives ... load the g1 deploy model` (the policy was given the menagerie G1): load `G1_SONIC.url` with `assetBase: G1_SONIC.assetBase`
|
|
423
|
+
- `onnxruntime-web did not load from ...` (offline, or jsDelivr is blocked): install `onnxruntime-web` and pass the module as `ort`
|
|
424
|
+
- `mujoco could not compile the model: ...` (a named file is missing or the MJCF is invalid): keep the robot's folder structure; the message names files that did not arrive
|
|
425
|
+
- `the model has no keyframe ...` (a keyframe name the model lacks): use one from `keyframes`
|
|
426
|
+
- `no mjcf here` (the drop holds no MJCF with a worldbody): drop the robot's whole folder
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/humanoid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.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).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Moheeb Zara <hackbuildvideo@gmail.com>",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/hackbuild/hackbuild-motion.git",
|
|
11
|
+
"directory": "packages/humanoid"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^22.18.0 || >=24.11.0"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./three": {
|
|
23
|
+
"types": "./dist/three.d.ts",
|
|
24
|
+
"import": "./dist/three.js"
|
|
25
|
+
},
|
|
26
|
+
"./sonic": {
|
|
27
|
+
"types": "./dist/sonic.d.ts",
|
|
28
|
+
"import": "./dist/sonic.js"
|
|
29
|
+
},
|
|
30
|
+
"./iife": "./dist/humanoid.iife.js"
|
|
31
|
+
},
|
|
32
|
+
"unpkg": "dist/humanoid.iife.js",
|
|
33
|
+
"jsdelivr": "dist/humanoid.iife.js",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"llms.md",
|
|
38
|
+
"LICENSE",
|
|
39
|
+
"how.ts"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "node ../../tools/build-package.mjs humanoid"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@mujoco/mujoco": ">=4.0.1",
|
|
46
|
+
"three": ">=0.186.1",
|
|
47
|
+
"onnxruntime-web": ">=1.23.2"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@mujoco/mujoco": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"three": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"onnxruntime-web": {
|
|
57
|
+
"optional": true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|