@hackbuild/humanoid 0.1.0 → 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/README.md +27 -8
- package/dist/files.d.ts +6 -1
- package/dist/files.js +7 -2
- package/dist/humanoid.iife.js +482 -2
- package/dist/sim.d.ts +7 -0
- package/dist/sim.js +15 -0
- package/dist/single.d.ts +1 -0
- package/dist/single.js +2 -1
- package/dist/sonic.d.ts +173 -0
- package/dist/sonic.js +329 -0
- package/how.ts +1 -0
- package/llms.md +147 -5
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @hackbuild/humanoid
|
|
2
2
|
|
|
3
|
-
Run a physics humanoid in the page: MuJoCo simulates a robot from its MJCF files,
|
|
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
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/@hackbuild/humanoid) [](../../LICENSE) ](https://www.npmjs.com/package/@hackbuild/humanoid) [](../../LICENSE)  
|
|
6
6
|
|
|
7
7
|
## What it does
|
|
8
8
|
|
|
@@ -11,6 +11,7 @@ Run a physics humanoid in the page: MuJoCo simulates a robot from its MJCF files
|
|
|
11
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
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
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.
|
|
14
15
|
|
|
15
16
|
## Install
|
|
16
17
|
|
|
@@ -18,12 +19,15 @@ Run a physics humanoid in the page: MuJoCo simulates a robot from its MJCF files
|
|
|
18
19
|
npm install @hackbuild/humanoid
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
It has no dependencies. MuJoCo 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.
|
|
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.
|
|
22
23
|
|
|
23
24
|
| download | size |
|
|
24
25
|
|---|---|
|
|
25
26
|
| mujoco.wasm | 11.2 MB, 2.8 MB gzip (measured) |
|
|
26
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.
|
|
27
31
|
|
|
28
32
|
## Quick start
|
|
29
33
|
|
|
@@ -63,6 +67,10 @@ MuJoCo is Google DeepMind's physics engine, compiled to WebAssembly and publishe
|
|
|
63
67
|
|
|
64
68
|
An MJCF names other files relative to its compiler's directories: `meshdir` or `assetdir` for meshes, `texturedir` for textures, and includes by their own path. `fetchModel` reads the main file, follows includes, then fetches every named file beside the main one; `filesFrom` does the same over a dropped folder, taking the MJCF with a worldbody that no other file includes as the main one.
|
|
65
69
|
|
|
70
|
+
### the policy
|
|
71
|
+
|
|
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
|
+
|
|
66
74
|
### the robot
|
|
67
75
|
|
|
68
76
|
`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 targets in over simulated time; a sudden jump can topple a robot that a ramp of half a second leaves standing. `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.
|
|
@@ -78,6 +86,10 @@ An MJCF names other files relative to its compiler's directories: `meshdir` or `
|
|
|
78
86
|
| [`Humanoid`](../../docs/api/humanoid/index.md#humanoid) | step, reset, targets, moveTo, hold, push, poses, dispose |
|
|
79
87
|
| [`G1`](../../docs/api/humanoid/index.md#g1) | the Unitree G1 from MuJoCo Menagerie: url, joints, keyframe, licence |
|
|
80
88
|
| [`HumanoidView`](../../docs/api/humanoid/three.md#humanoidview) | the robot in three.js, from `@hackbuild/humanoid/three` |
|
|
89
|
+
| [`Sonic`](../../docs/api/humanoid/sonic.md#sonic) | load the GEAR-SONIC policy, from `@hackbuild/humanoid/sonic` |
|
|
90
|
+
| [`SonicDriver`](../../docs/api/humanoid/sonic.md#sonicdriver) | run it on a G1: advance, reference, reset |
|
|
91
|
+
| [`G1_SONIC`](../../docs/api/humanoid/sonic.md#g1_sonic) | the G1 deploy model the policy runs on: url, asset host, licence |
|
|
92
|
+
| [`SONIC_JOINTS`](../../docs/api/humanoid/sonic.md#sonic_joints) | standing angles, gains, torque limits, action scale and policy order per joint |
|
|
81
93
|
| [`HumanoidError`](../../docs/api/humanoid/index.md#humanoiderror) | a model or runtime this package cannot open, with the reason |
|
|
82
94
|
|
|
83
95
|
The full reference is in [docs/api/humanoid](../../docs/api/humanoid/README.md).
|
|
@@ -88,6 +100,7 @@ The full reference is in [docs/api/humanoid](../../docs/api/humanoid/README.md).
|
|
|
88
100
|
|---|---|---|
|
|
89
101
|
| MuJoCo | WebAssembly, a network fetch or the installed package | 206 ms to load from a local copy in headless Chrome on an Apple M1 (measured) |
|
|
90
102
|
| stepping | nothing more | 0.084 ms per 2 ms step for the G1, about 24 times real time (measured) |
|
|
103
|
+
| the policy | onnxruntime-web on wasm, one thread | 5.7 ms a 20 ms tick in headless Chrome on an Apple M1 (measured) |
|
|
91
104
|
| drawing | three.js 0.186 or later, WebGL | the G1 draws 35 meshes |
|
|
92
105
|
| single file build | `dist/humanoid.iife.js` | `HackbuildHumanoid` global with `THREE` from the page; MuJoCo still loads from its URL |
|
|
93
106
|
|
|
@@ -96,6 +109,8 @@ The full reference is in [docs/api/humanoid](../../docs/api/humanoid/README.md).
|
|
|
96
109
|
| message | cause | what to do |
|
|
97
110
|
|---|---|---|
|
|
98
111
|
| `mujoco did not load from ...` | offline, or the URL is blocked | install `@mujoco/mujoco` and pass its factory, or serve it and pass `url` |
|
|
112
|
+
| `no actuator drives ... load the g1 deploy model` | the policy was given the menagerie G1 | load `G1_SONIC.url` with `assetBase: G1_SONIC.assetBase` |
|
|
113
|
+
| `onnxruntime-web did not load from ...` | offline, or jsDelivr is blocked | install `onnxruntime-web` and pass the module as `ort` |
|
|
99
114
|
| `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 |
|
|
100
115
|
| `the model has no keyframe ...` | a keyframe name the model lacks | use one from `keyframes` |
|
|
101
116
|
| `no mjcf here` | the drop holds no MJCF with a worldbody | drop the robot's whole folder |
|
|
@@ -107,7 +122,7 @@ The full reference is in [docs/api/humanoid](../../docs/api/humanoid/README.md).
|
|
|
107
122
|
|
|
108
123
|
## Changelog and licences
|
|
109
124
|
|
|
110
|
-
Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LICENSE](../../LICENSE)); this README is CC-BY-4.0 with its code samples also MIT. MuJoCo is Google DeepMind's, Apache-2.0, and loads at run time. The G1 model and meshes are from MuJoCo Menagerie under BSD-3-Clause, copyright Unitree Robotics, and load at run time;
|
|
125
|
+
Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LICENSE](../../LICENSE)); this README is CC-BY-4.0 with its code samples also MIT. MuJoCo is Google DeepMind's, Apache-2.0, and loads at run time. The G1 model and meshes are from MuJoCo Menagerie under BSD-3-Clause, copyright Unitree Robotics, and load at run time; the deploy model is from NVIDIA's GR00T-WholeBodyControl (Apache-2.0) with Unitree's meshes. The GEAR-SONIC weights are NVIDIA's: licensed by NVIDIA Corporation under the NVIDIA Open Model License; they load from Hugging Face at run time and are not part of this package. Robots you bring carry their own licences.
|
|
111
126
|
|
|
112
127
|
## What is verified, and what is not
|
|
113
128
|
|
|
@@ -115,14 +130,18 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
115
130
|
|
|
116
131
|
| check | result |
|
|
117
132
|
|---|---|
|
|
118
|
-
| `npx vitest run packages/humanoid` |
|
|
133
|
+
| `npx vitest run packages/humanoid` | 14 of 14 tests pass: the 7 below, 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, and the refusal of a robot without the G1's joints (counted) |
|
|
134
|
+
| 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) |
|
|
119
135
|
| 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) |
|
|
120
136
|
| 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) |
|
|
137
|
+
| GEAR-SONIC on the deploy G1 in node, MuJoCo 4.0.1 and onnxruntime-web 1.23.2 | of seven guesses at the open layout details only one stands; with the tilt anchor it stands 30 s at 0.766 m within 1.1 degrees, steps 0.5 m to catch a 1.0 m/s shove and catches 1.5 m/s, where the position actuators alone fall at 0.3 m/s; it holds a crouch at 0.680 m and a raised arm; 2.6 to 4.5 ms a tick, 15 s simulated in 4.5 s (measured) |
|
|
138
|
+
| 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) |
|
|
121
139
|
| 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) |
|
|
122
140
|
|
|
123
141
|
### Not verified by anything
|
|
124
142
|
|
|
125
|
-
-
|
|
143
|
+
- The policy tracks static poses only: no reference motion, walking command or the 774 MB planner runs, and the low latency and v1.1 variants are untried.
|
|
144
|
+
- On its actuators alone (no policy) the robot holds targets but does not balance; a shove topples it.
|
|
126
145
|
- No VRM follows the robot yet.
|
|
127
146
|
- Robots other than the G1 and the test model are untried, and so are textures in MJCF.
|
|
128
147
|
- Nobody has watched it at real frame rate in a visible browser; the checks are headless.
|
|
@@ -131,8 +150,8 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
131
150
|
|
|
132
151
|
| file | gzip (measured) | ceiling |
|
|
133
152
|
|---|---|---|
|
|
134
|
-
| `dist/index.js` (main entry, imports external) |
|
|
135
|
-
| `dist/humanoid.iife.js` (single file build) |
|
|
153
|
+
| `dist/index.js` (main entry, imports external) | 6,006 bytes | 6,306 bytes |
|
|
154
|
+
| `dist/humanoid.iife.js` (single file build) | 12,044 bytes | 12,646 bytes |
|
|
136
155
|
|
|
137
156
|
Measured 2026-11-30 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
138
157
|
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
package/dist/files.d.ts
CHANGED
|
@@ -24,9 +24,14 @@ export declare function referencedFiles(xml: string, dirs?: {
|
|
|
24
24
|
includes: string[];
|
|
25
25
|
files: string[];
|
|
26
26
|
};
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* A robot from the URL of its main MJCF; every file it names is fetched beside it, or for files that
|
|
29
|
+
* are not MJCF beside `assetBase` when given (a repository whose meshes live on another host, such as
|
|
30
|
+
* Git LFS).
|
|
31
|
+
*/
|
|
28
32
|
export declare function fetchModel(url: string, o?: {
|
|
29
33
|
fetch?: typeof fetch;
|
|
34
|
+
assetBase?: string;
|
|
30
35
|
onFile?: (path: string, done: number, of: number) => void;
|
|
31
36
|
}): Promise<ModelFiles>;
|
|
32
37
|
/** A robot from dropped files (a folder keeps its paths): the main MJCF is the one with a worldbody that no other file includes. */
|
package/dist/files.js
CHANGED
|
@@ -86,14 +86,19 @@ async function gather(main, read, onFile) {
|
|
|
86
86
|
}));
|
|
87
87
|
return { xml, assets, missing };
|
|
88
88
|
}
|
|
89
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* A robot from the URL of its main MJCF; every file it names is fetched beside it, or for files that
|
|
91
|
+
* are not MJCF beside `assetBase` when given (a repository whose meshes live on another host, such as
|
|
92
|
+
* Git LFS).
|
|
93
|
+
*/
|
|
90
94
|
export function fetchModel(url, o = {}) {
|
|
91
95
|
const f = o.fetch ?? globalThis.fetch.bind(globalThis);
|
|
92
96
|
const here = globalThis.location?.href;
|
|
93
97
|
const base = new URL(url, here);
|
|
98
|
+
const assets = o.assetBase ? new URL(o.assetBase.endsWith("/") ? o.assetBase : `${o.assetBase}/`, here) : base;
|
|
94
99
|
const read = async (path) => {
|
|
95
100
|
try {
|
|
96
|
-
const r = await f(path === url ? base.href : new URL(path, base).href);
|
|
101
|
+
const r = await f(path === url ? base.href : new URL(path, /\.xml$/i.test(path) ? base : assets).href);
|
|
97
102
|
return r.ok ? new Uint8Array(await r.arrayBuffer()) : null;
|
|
98
103
|
}
|
|
99
104
|
catch {
|
package/dist/humanoid.iife.js
CHANGED
|
@@ -134,14 +134,19 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
134
134
|
missing
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
-
/**
|
|
137
|
+
/**
|
|
138
|
+
* A robot from the URL of its main MJCF; every file it names is fetched beside it, or for files that
|
|
139
|
+
* are not MJCF beside `assetBase` when given (a repository whose meshes live on another host, such as
|
|
140
|
+
* Git LFS).
|
|
141
|
+
*/
|
|
138
142
|
function fetchModel(url, o = {}) {
|
|
139
143
|
const f = o.fetch ?? globalThis.fetch.bind(globalThis);
|
|
140
144
|
const here = globalThis.location?.href;
|
|
141
145
|
const base = new URL(url, here);
|
|
146
|
+
const assets = o.assetBase ? new URL(o.assetBase.endsWith("/") ? o.assetBase : `${o.assetBase}/`, here) : base;
|
|
142
147
|
const read = async (path) => {
|
|
143
148
|
try {
|
|
144
|
-
const r = await f(path === url ? base.href : new URL(path, base).href);
|
|
149
|
+
const r = await f(path === url ? base.href : new URL(path, /\.xml$/i.test(path) ? base : assets).href);
|
|
145
150
|
return r.ok ? new Uint8Array(await r.arrayBuffer()) : null;
|
|
146
151
|
} catch {
|
|
147
152
|
return null;
|
|
@@ -288,6 +293,23 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
288
293
|
this.carry = Math.max(0, this.carry - n * this.timestep);
|
|
289
294
|
return n;
|
|
290
295
|
}
|
|
296
|
+
/** Runs `n` timesteps, calling `control` before each so a controller can set the actuators every step (a PD loop on motor actuators). */
|
|
297
|
+
run(n, control) {
|
|
298
|
+
if (this.gone) return;
|
|
299
|
+
for (let i = 0; i < n; i++) {
|
|
300
|
+
control?.(this);
|
|
301
|
+
this.applyRamp();
|
|
302
|
+
this.mj.mj_step(this.model, this.data);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/** Where a hinge or slide joint keeps its position in qpos and its velocity in qvel, or null for a name the model lacks. */
|
|
306
|
+
address(joint) {
|
|
307
|
+
const j = this.joints.indexOf(joint);
|
|
308
|
+
return j < 0 ? null : {
|
|
309
|
+
qpos: this.model.jnt_qposadr[j],
|
|
310
|
+
qvel: this.model.jnt_dofadr[j]
|
|
311
|
+
};
|
|
312
|
+
}
|
|
291
313
|
/** A body's world pose, or null for a name the model lacks. */
|
|
292
314
|
pose(body) {
|
|
293
315
|
const i = typeof body === "number" ? body : this.bodies.indexOf(body);
|
|
@@ -505,16 +527,474 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
505
527
|
this.meshes = [];
|
|
506
528
|
}
|
|
507
529
|
};
|
|
530
|
+
// src/sonic.ts
|
|
531
|
+
/**
|
|
532
|
+
* GEAR-SONIC, NVIDIA's whole body controller for the Unitree G1, run in the
|
|
533
|
+
* page: an encoder turns a reference motion into a 64 number token, a decoder
|
|
534
|
+
* turns the token and ten ticks of the robot's state into 29 joint actions,
|
|
535
|
+
* 50 times a second; a PD loop here turns actions into motor torques at every
|
|
536
|
+
* MuJoCo step. Written for this library from the published deployment
|
|
537
|
+
* description and parameters (observation names, sizes and order, joint
|
|
538
|
+
* orders, gains from motor armature, action scale, default angles). The
|
|
539
|
+
* details the description leaves open were settled by trial on the deploy
|
|
540
|
+
* model: joint positions relative to the default angles, the reference in
|
|
541
|
+
* absolute angles, histories oldest first, a rotation's first two columns
|
|
542
|
+
* row by row, and the anchor as the base's tilt against an upright reference
|
|
543
|
+
* that follows the robot's heading (measured: only that combination stands).
|
|
544
|
+
* The weights are NVIDIA's, under the NVIDIA Open Model License, and load at
|
|
545
|
+
* run time; onnxruntime-web is an optional peer, passed in or loaded from a URL.
|
|
546
|
+
*/
|
|
547
|
+
/** The policy files and their terms. */
|
|
548
|
+
const SONIC = {
|
|
549
|
+
name: "gear-sonic, the default g1 controller",
|
|
550
|
+
/** Hugging Face revision the URLs pin. */
|
|
551
|
+
revision: "6733128a3d8a523b1418b06bca3cdf61c8b0987f",
|
|
552
|
+
encoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_encoder.onnx",
|
|
553
|
+
decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx",
|
|
554
|
+
/** Encoder and decoder together, in bytes (vendor: Hugging Face's sizes). */
|
|
555
|
+
bytes: 91001201,
|
|
556
|
+
hz: 50,
|
|
557
|
+
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."
|
|
558
|
+
};
|
|
559
|
+
/** The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction. */
|
|
560
|
+
const G1_SONIC = {
|
|
561
|
+
name: "unitree g1, 29 joints, the gear-sonic deploy model",
|
|
562
|
+
/** The scene, at GR00T-WholeBodyControl commit 2a5e6a1; it includes the robot file beside it. */
|
|
563
|
+
url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml",
|
|
564
|
+
/** The meshes are in Git LFS; pass this as `assetBase` to `fetchModel`. */
|
|
565
|
+
assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/",
|
|
566
|
+
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."
|
|
567
|
+
};
|
|
568
|
+
const MOTORS = {
|
|
569
|
+
m22: [.025101925, 139],
|
|
570
|
+
m14: [.01017752, 88],
|
|
571
|
+
m5020: [.003609725, 25],
|
|
572
|
+
m4010: [.00425, 5]
|
|
573
|
+
};
|
|
574
|
+
const MOTOR_OF = [
|
|
575
|
+
"m22",
|
|
576
|
+
"m22",
|
|
577
|
+
"m14",
|
|
578
|
+
"m22",
|
|
579
|
+
"m5020",
|
|
580
|
+
"m5020",
|
|
581
|
+
"m22",
|
|
582
|
+
"m22",
|
|
583
|
+
"m14",
|
|
584
|
+
"m22",
|
|
585
|
+
"m5020",
|
|
586
|
+
"m5020",
|
|
587
|
+
"m14",
|
|
588
|
+
"m5020",
|
|
589
|
+
"m5020",
|
|
590
|
+
"m5020",
|
|
591
|
+
"m5020",
|
|
592
|
+
"m5020",
|
|
593
|
+
"m5020",
|
|
594
|
+
"m5020",
|
|
595
|
+
"m4010",
|
|
596
|
+
"m4010",
|
|
597
|
+
"m5020",
|
|
598
|
+
"m5020",
|
|
599
|
+
"m5020",
|
|
600
|
+
"m5020",
|
|
601
|
+
"m5020",
|
|
602
|
+
"m4010",
|
|
603
|
+
"m4010"
|
|
604
|
+
];
|
|
605
|
+
const DOUBLED = /* @__PURE__ */ new Set([
|
|
606
|
+
4,
|
|
607
|
+
5,
|
|
608
|
+
10,
|
|
609
|
+
11,
|
|
610
|
+
13,
|
|
611
|
+
14
|
|
612
|
+
]);
|
|
613
|
+
const OMEGA = 20 * Math.PI;
|
|
614
|
+
/** The deployment's joint tables, each in the model's joint order (`G1.joints`). */
|
|
615
|
+
const SONIC_JOINTS = {
|
|
616
|
+
/** Standing angles, radians. */
|
|
617
|
+
defaults: [
|
|
618
|
+
-.312,
|
|
619
|
+
0,
|
|
620
|
+
0,
|
|
621
|
+
.669,
|
|
622
|
+
-.363,
|
|
623
|
+
0,
|
|
624
|
+
-.312,
|
|
625
|
+
0,
|
|
626
|
+
0,
|
|
627
|
+
.669,
|
|
628
|
+
-.363,
|
|
629
|
+
0,
|
|
630
|
+
0,
|
|
631
|
+
0,
|
|
632
|
+
0,
|
|
633
|
+
.2,
|
|
634
|
+
.2,
|
|
635
|
+
0,
|
|
636
|
+
.6,
|
|
637
|
+
0,
|
|
638
|
+
0,
|
|
639
|
+
0,
|
|
640
|
+
.2,
|
|
641
|
+
-.2,
|
|
642
|
+
0,
|
|
643
|
+
.6,
|
|
644
|
+
0,
|
|
645
|
+
0,
|
|
646
|
+
0
|
|
647
|
+
],
|
|
648
|
+
/** Position gain, N m per rad: armature times (10 Hz in rad/s) squared (computed). */
|
|
649
|
+
kp: MOTOR_OF.map((m, j) => MOTORS[m][0] * OMEGA * OMEGA * (DOUBLED.has(j) ? 2 : 1)),
|
|
650
|
+
/** Velocity gain, N m s per rad: 2 times damping ratio 2 times armature times omega (computed). */
|
|
651
|
+
kd: MOTOR_OF.map((m, j) => 4 * MOTORS[m][0] * OMEGA * (DOUBLED.has(j) ? 2 : 1)),
|
|
652
|
+
/** Torque limit, N m. */
|
|
653
|
+
effort: MOTOR_OF.map((m) => MOTORS[m][1]),
|
|
654
|
+
/** Radians per unit of action: 0.25 times effort over stiffness (computed). */
|
|
655
|
+
scale: MOTOR_OF.map((m) => .25 * MOTORS[m][1] / (MOTORS[m][0] * OMEGA * OMEGA)),
|
|
656
|
+
/** The policy's (IsaacLab) index of each joint. */
|
|
657
|
+
policyIndex: [
|
|
658
|
+
0,
|
|
659
|
+
3,
|
|
660
|
+
6,
|
|
661
|
+
9,
|
|
662
|
+
13,
|
|
663
|
+
17,
|
|
664
|
+
1,
|
|
665
|
+
4,
|
|
666
|
+
7,
|
|
667
|
+
10,
|
|
668
|
+
14,
|
|
669
|
+
18,
|
|
670
|
+
2,
|
|
671
|
+
5,
|
|
672
|
+
8,
|
|
673
|
+
11,
|
|
674
|
+
15,
|
|
675
|
+
19,
|
|
676
|
+
21,
|
|
677
|
+
23,
|
|
678
|
+
25,
|
|
679
|
+
27,
|
|
680
|
+
12,
|
|
681
|
+
16,
|
|
682
|
+
20,
|
|
683
|
+
22,
|
|
684
|
+
24,
|
|
685
|
+
26,
|
|
686
|
+
28
|
|
687
|
+
]
|
|
688
|
+
};
|
|
689
|
+
/** A model order array (29) in the policy's order. */
|
|
690
|
+
function toPolicyOrder(v) {
|
|
691
|
+
const out = new Array(29).fill(0);
|
|
692
|
+
for (let j = 0; j < 29; j++) out[SONIC_JOINTS.policyIndex[j]] = v[j];
|
|
693
|
+
return out;
|
|
694
|
+
}
|
|
695
|
+
/** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
|
|
696
|
+
function intoBody(q, v) {
|
|
697
|
+
const [w, x, y, z] = q;
|
|
698
|
+
const r = [
|
|
699
|
+
[
|
|
700
|
+
1 - 2 * (y * y + z * z),
|
|
701
|
+
2 * (x * y - w * z),
|
|
702
|
+
2 * (x * z + w * y)
|
|
703
|
+
],
|
|
704
|
+
[
|
|
705
|
+
2 * (x * y + w * z),
|
|
706
|
+
1 - 2 * (x * x + z * z),
|
|
707
|
+
2 * (y * z - w * x)
|
|
708
|
+
],
|
|
709
|
+
[
|
|
710
|
+
2 * (x * z - w * y),
|
|
711
|
+
2 * (y * z + w * x),
|
|
712
|
+
1 - 2 * (x * x + y * y)
|
|
713
|
+
]
|
|
714
|
+
];
|
|
715
|
+
return [
|
|
716
|
+
0,
|
|
717
|
+
1,
|
|
718
|
+
2
|
|
719
|
+
].map((i) => r[0][i] * v[0] + r[1][i] * v[1] + r[2][i] * v[2]);
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
|
|
723
|
+
* the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
|
|
724
|
+
* reference, read row by row.
|
|
725
|
+
*/
|
|
726
|
+
function anchorSix(base) {
|
|
727
|
+
const [w, x, y, z] = base;
|
|
728
|
+
const n = Math.max(Math.hypot(w, z), 1e-9);
|
|
729
|
+
const hw = w / n, hz = z / n;
|
|
730
|
+
const rw = hw * w + hz * z, rx = -(hw * x + hz * y), ry = -(hw * y - hz * x), rz = -(hw * z - hz * w);
|
|
731
|
+
return [
|
|
732
|
+
1 - 2 * (ry * ry + rz * rz),
|
|
733
|
+
2 * (rx * ry - rw * rz),
|
|
734
|
+
2 * (rx * ry + rw * rz),
|
|
735
|
+
1 - 2 * (rx * rx + rz * rz),
|
|
736
|
+
2 * (rx * rz - rw * ry),
|
|
737
|
+
2 * (ry * rz + rw * rx)
|
|
738
|
+
];
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
|
|
742
|
+
* angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
|
|
743
|
+
* stay zero. `ref` holds 10 frames, or 1 repeated.
|
|
744
|
+
*/
|
|
745
|
+
function encoderInput(ref, anchor) {
|
|
746
|
+
const e = /* @__PURE__ */ new Float32Array(1762);
|
|
747
|
+
const frame = (f) => ref[Math.min(f, ref.length - 1)];
|
|
748
|
+
let o = 4;
|
|
749
|
+
for (let f = 0; f < 10; f++) for (let k = 0; k < 29; k++) e[o++] = frame(f).q[k];
|
|
750
|
+
for (let f = 0; f < 10; f++) for (let k = 0; k < 29; k++) e[o++] = frame(f).dq?.[k] ?? 0;
|
|
751
|
+
o += 17;
|
|
752
|
+
for (let f = 0; f < 10; f++) for (let k = 0; k < 6; k++) e[o++] = anchor[k];
|
|
753
|
+
return e;
|
|
754
|
+
}
|
|
755
|
+
/** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
|
|
756
|
+
var SonicHistory = class {
|
|
757
|
+
av = [];
|
|
758
|
+
q = [];
|
|
759
|
+
dq = [];
|
|
760
|
+
act = [];
|
|
761
|
+
g = [];
|
|
762
|
+
/** Fills all ten ticks with one state and zero actions. */
|
|
763
|
+
prime(s) {
|
|
764
|
+
for (const k of [
|
|
765
|
+
"av",
|
|
766
|
+
"q",
|
|
767
|
+
"dq",
|
|
768
|
+
"act",
|
|
769
|
+
"g"
|
|
770
|
+
]) this[k] = [];
|
|
771
|
+
for (let i = 0; i < 10; i++) this.add(s, new Array(29).fill(0));
|
|
772
|
+
}
|
|
773
|
+
/** Adds a tick's state, with the actions the last tick produced. */
|
|
774
|
+
add(s, lastAction) {
|
|
775
|
+
const keep = (a, v) => {
|
|
776
|
+
a.push(v);
|
|
777
|
+
if (a.length > 10) a.shift();
|
|
778
|
+
};
|
|
779
|
+
keep(this.av, s.angularVelocity);
|
|
780
|
+
keep(this.q, s.q);
|
|
781
|
+
keep(this.dq, s.dq);
|
|
782
|
+
keep(this.act, Array.from(lastAction));
|
|
783
|
+
keep(this.g, s.gravity);
|
|
784
|
+
}
|
|
785
|
+
/** The decoder's 994 inputs: the token, then each history oldest first. */
|
|
786
|
+
decoderInput(token) {
|
|
787
|
+
const d = /* @__PURE__ */ new Float32Array(994);
|
|
788
|
+
let p = 0;
|
|
789
|
+
for (let k = 0; k < 64; k++) d[p++] = token[k];
|
|
790
|
+
for (const h of [
|
|
791
|
+
this.av,
|
|
792
|
+
this.q,
|
|
793
|
+
this.dq,
|
|
794
|
+
this.act,
|
|
795
|
+
this.g
|
|
796
|
+
]) for (const tick of h) for (const v of tick) d[p++] = v;
|
|
797
|
+
return d;
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
/** Where onnxruntime-web loads from when no module is given. */
|
|
801
|
+
const ORT_URL = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.wasm.min.mjs";
|
|
802
|
+
async function bytesOf(src, f, progress) {
|
|
803
|
+
if (typeof src !== "string") return src instanceof Uint8Array ? src : new Uint8Array(src);
|
|
804
|
+
const r = await f(src);
|
|
805
|
+
if (!r.ok || !r.body) throw new HumanoidError(`the policy file ${src} answered ${r.status}.`);
|
|
806
|
+
const reader = r.body.getReader();
|
|
807
|
+
const parts = [];
|
|
808
|
+
let n = 0;
|
|
809
|
+
for (;;) {
|
|
810
|
+
const { done, value } = await reader.read();
|
|
811
|
+
if (done) break;
|
|
812
|
+
parts.push(value);
|
|
813
|
+
n += value.byteLength;
|
|
814
|
+
progress(value.byteLength);
|
|
815
|
+
}
|
|
816
|
+
const out = new Uint8Array(n);
|
|
817
|
+
let o = 0;
|
|
818
|
+
for (const p of parts) out.set(p, o), o += p.byteLength;
|
|
819
|
+
return out;
|
|
820
|
+
}
|
|
821
|
+
/** The loaded policy: an encoder and a decoder session. Drive a G1 with {@link Sonic.drive}. */
|
|
822
|
+
var Sonic = class Sonic {
|
|
823
|
+
ort;
|
|
824
|
+
encoder;
|
|
825
|
+
decoder;
|
|
826
|
+
constructor(ort, encoder, decoder) {
|
|
827
|
+
this.ort = ort;
|
|
828
|
+
this.encoder = encoder;
|
|
829
|
+
this.decoder = decoder;
|
|
830
|
+
}
|
|
831
|
+
/** Loads onnxruntime-web and both policy files. */
|
|
832
|
+
static async load(o = {}) {
|
|
833
|
+
let ort = o.ort;
|
|
834
|
+
if (!ort) {
|
|
835
|
+
const url = o.ortUrl ?? "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.wasm.min.mjs";
|
|
836
|
+
try {
|
|
837
|
+
ort = await import(
|
|
838
|
+
/* @vite-ignore */
|
|
839
|
+
url
|
|
840
|
+
);
|
|
841
|
+
} catch (e) {
|
|
842
|
+
throw new HumanoidError(`onnxruntime-web did not load from ${url}: ${e instanceof Error ? e.message : String(e)}.`);
|
|
843
|
+
}
|
|
844
|
+
ort.env.wasm.wasmPaths = new URL("./", url).href;
|
|
845
|
+
}
|
|
846
|
+
ort.env.wasm.numThreads = 1;
|
|
847
|
+
const f = o.fetch ?? globalThis.fetch.bind(globalThis);
|
|
848
|
+
let loaded = 0;
|
|
849
|
+
const tick = (n) => o.onProgress?.(loaded += n, SONIC.bytes);
|
|
850
|
+
const [eb, db] = await Promise.all([bytesOf(o.encoder ?? SONIC.encoder, f, tick), bytesOf(o.decoder ?? SONIC.decoder, f, tick)]);
|
|
851
|
+
const [encoder, decoder] = await Promise.all([ort.InferenceSession.create(eb), ort.InferenceSession.create(db)]);
|
|
852
|
+
o.onLicence?.(SONIC.licence);
|
|
853
|
+
return new Sonic(ort, encoder, decoder);
|
|
854
|
+
}
|
|
855
|
+
/** The decoder's actions (policy order) for one tick. */
|
|
856
|
+
async act(encoderIn, history) {
|
|
857
|
+
const T = this.ort.Tensor;
|
|
858
|
+
const token = (await this.encoder.run({ obs_dict: new T("float32", encoderIn, [1, 1762]) })).encoded_tokens.data;
|
|
859
|
+
return (await this.decoder.run({ obs_dict: new T("float32", history.decoderInput(token), [1, 994]) })).action.data;
|
|
860
|
+
}
|
|
861
|
+
/** A driver for a G1 whose 29 joints are on motor actuators (the {@link G1_SONIC} model). */
|
|
862
|
+
drive(sim) {
|
|
863
|
+
return new SonicDriver(this, sim);
|
|
864
|
+
}
|
|
865
|
+
async dispose() {
|
|
866
|
+
await Promise.all([this.encoder.release?.(), this.decoder.release?.()]);
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
/** Runs the policy on one robot: 50 ticks a second, the PD loop at every timestep between. */
|
|
870
|
+
var SonicDriver = class {
|
|
871
|
+
sonic;
|
|
872
|
+
sim;
|
|
873
|
+
/** The reference pose the policy tracks, absolute angles in the model's joint order. Default the standing angles. */
|
|
874
|
+
reference = SONIC_JOINTS.defaults.slice();
|
|
875
|
+
/** Milliseconds the last tick's inference took (measured). */
|
|
876
|
+
inferenceMs = 0;
|
|
877
|
+
adr;
|
|
878
|
+
act;
|
|
879
|
+
history = new SonicHistory();
|
|
880
|
+
targets = SONIC_JOINTS.defaults.slice();
|
|
881
|
+
last = /* @__PURE__ */ new Float32Array(29);
|
|
882
|
+
carry = 0;
|
|
883
|
+
busy = false;
|
|
884
|
+
perTick;
|
|
885
|
+
constructor(sonic, sim) {
|
|
886
|
+
this.sonic = sonic;
|
|
887
|
+
this.sim = sim;
|
|
888
|
+
this.adr = G1.joints.map((j) => {
|
|
889
|
+
const a = sim.address(j);
|
|
890
|
+
if (!a) throw new HumanoidError(`the robot has no joint ${j}; the policy drives the unitree g1.`);
|
|
891
|
+
return a;
|
|
892
|
+
});
|
|
893
|
+
this.act = G1.joints.map((j) => {
|
|
894
|
+
const i = sim.actuated.indexOf(j);
|
|
895
|
+
if (i < 0) throw new HumanoidError(`no actuator drives ${j}; load the g1 deploy model (G1_SONIC), whose joints are on motors.`);
|
|
896
|
+
return i;
|
|
897
|
+
});
|
|
898
|
+
this.perTick = Math.max(1, Math.round(1 / SONIC.hz / sim.timestep));
|
|
899
|
+
this.reset();
|
|
900
|
+
}
|
|
901
|
+
/** The robot back at the standing angles, upright at the origin, and the history primed. */
|
|
902
|
+
reset() {
|
|
903
|
+
const { sim } = this;
|
|
904
|
+
sim.reset();
|
|
905
|
+
const d = sim.data;
|
|
906
|
+
d.qpos[2] = .79;
|
|
907
|
+
for (let j = 0; j < 29; j++) d.qpos[this.adr[j].qpos] = SONIC_JOINTS.defaults[j];
|
|
908
|
+
this.targets = SONIC_JOINTS.defaults.slice();
|
|
909
|
+
this.last.fill(0);
|
|
910
|
+
this.carry = 0;
|
|
911
|
+
this.history.prime(this.state());
|
|
912
|
+
}
|
|
913
|
+
/** The robot's state this tick, in the policy's order. */
|
|
914
|
+
state() {
|
|
915
|
+
const d = this.sim.data;
|
|
916
|
+
const quat = [
|
|
917
|
+
d.qpos[3],
|
|
918
|
+
d.qpos[4],
|
|
919
|
+
d.qpos[5],
|
|
920
|
+
d.qpos[6]
|
|
921
|
+
];
|
|
922
|
+
return {
|
|
923
|
+
angularVelocity: [
|
|
924
|
+
d.qvel[3],
|
|
925
|
+
d.qvel[4],
|
|
926
|
+
d.qvel[5]
|
|
927
|
+
],
|
|
928
|
+
q: toPolicyOrder(this.adr.map((a, j) => d.qpos[a.qpos] - SONIC_JOINTS.defaults[j])),
|
|
929
|
+
dq: toPolicyOrder(this.adr.map((a) => d.qvel[a.qvel])),
|
|
930
|
+
gravity: intoBody(quat, [
|
|
931
|
+
0,
|
|
932
|
+
0,
|
|
933
|
+
-1
|
|
934
|
+
])
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
/** One control tick: the policy's actions to joint targets, then the PD loop for one tick of timesteps. */
|
|
938
|
+
async tick() {
|
|
939
|
+
const d = this.sim.data;
|
|
940
|
+
const t0 = performance.now();
|
|
941
|
+
this.history.add(this.state(), this.last);
|
|
942
|
+
const anchor = anchorSix([
|
|
943
|
+
d.qpos[3],
|
|
944
|
+
d.qpos[4],
|
|
945
|
+
d.qpos[5],
|
|
946
|
+
d.qpos[6]
|
|
947
|
+
]);
|
|
948
|
+
const a = await this.sonic.act(encoderInput([{ q: toPolicyOrder(this.reference) }], anchor), this.history);
|
|
949
|
+
this.inferenceMs = performance.now() - t0;
|
|
950
|
+
this.last = Float32Array.from(a);
|
|
951
|
+
const pi = SONIC_JOINTS.policyIndex;
|
|
952
|
+
for (let j = 0; j < 29; j++) this.targets[j] = SONIC_JOINTS.defaults[j] + a[pi[j]] * SONIC_JOINTS.scale[j];
|
|
953
|
+
const { kp, kd, effort } = SONIC_JOINTS;
|
|
954
|
+
this.sim.run(this.perTick, () => {
|
|
955
|
+
for (let j = 0; j < 29; j++) {
|
|
956
|
+
const tau = kp[j] * (this.targets[j] - d.qpos[this.adr[j].qpos]) - kd[j] * d.qvel[this.adr[j].qvel];
|
|
957
|
+
d.ctrl[this.act[j]] = Math.max(-effort[j], Math.min(effort[j], tau));
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
/** Advances by `seconds` in whole ticks, carrying the rest, at most 0.1 s a call; a call while one runs does nothing. Returns the ticks run. */
|
|
962
|
+
async advance(seconds) {
|
|
963
|
+
if (this.busy) return 0;
|
|
964
|
+
this.busy = true;
|
|
965
|
+
try {
|
|
966
|
+
const per = 1 / SONIC.hz;
|
|
967
|
+
this.carry = Math.min(this.carry + Math.max(0, seconds), .1);
|
|
968
|
+
const n = Math.floor(this.carry / per + 1e-6);
|
|
969
|
+
for (let i = 0; i < n; i++) await this.tick();
|
|
970
|
+
this.carry = Math.max(0, this.carry - n * per);
|
|
971
|
+
return n;
|
|
972
|
+
} finally {
|
|
973
|
+
this.busy = false;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
};
|
|
508
977
|
exports.G1 = G1;
|
|
978
|
+
exports.G1_SONIC = G1_SONIC;
|
|
509
979
|
exports.Humanoid = Humanoid;
|
|
510
980
|
exports.HumanoidError = HumanoidError;
|
|
511
981
|
exports.HumanoidView = HumanoidView;
|
|
512
982
|
exports.MUJOCO_LICENCE = MUJOCO_LICENCE;
|
|
513
983
|
exports.MUJOCO_URL = MUJOCO_URL;
|
|
514
984
|
exports.MUJOCO_VERSION = MUJOCO_VERSION;
|
|
985
|
+
exports.ORT_URL = ORT_URL;
|
|
986
|
+
exports.SONIC = SONIC;
|
|
987
|
+
exports.SONIC_JOINTS = SONIC_JOINTS;
|
|
988
|
+
exports.Sonic = Sonic;
|
|
989
|
+
exports.SonicDriver = SonicDriver;
|
|
990
|
+
exports.SonicHistory = SonicHistory;
|
|
991
|
+
exports.anchorSix = anchorSix;
|
|
992
|
+
exports.encoderInput = encoderInput;
|
|
515
993
|
exports.fetchModel = fetchModel;
|
|
516
994
|
exports.filesFrom = filesFrom;
|
|
995
|
+
exports.intoBody = intoBody;
|
|
517
996
|
exports.loadMujoco = loadMujoco;
|
|
518
997
|
exports.referencedFiles = referencedFiles;
|
|
998
|
+
exports.toPolicyOrder = toPolicyOrder;
|
|
519
999
|
return exports;
|
|
520
1000
|
})({}, THREE);
|