@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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { MUJOCO_LICENCE, MUJOCO_URL, MUJOCO_VERSION, HumanoidError, loadMujoco, type Mujoco, type MujocoOptions, type MjModel, type MjData, type MjVFS } from "./runtime.js";
|
|
2
|
+
export { fetchModel, filesFrom, referencedFiles, type ModelFiles } from "./files.js";
|
|
3
|
+
export { Humanoid, type Pose } from "./sim.js";
|
|
4
|
+
export { G1 } from "./g1.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MuJoCo in the page: Google DeepMind's WebAssembly build (`@mujoco/mujoco`,
|
|
3
|
+
* Apache-2.0), the single threaded one, so the page needs no cross-origin
|
|
4
|
+
* isolation. This library never bundles it: pass the factory a bundler
|
|
5
|
+
* imported, or it loads at run time from a URL (default the pinned copy on
|
|
6
|
+
* jsDelivr, its wasm read beside it), and the page is told whose licence
|
|
7
|
+
* applies. The types here cover the part of its surface this library uses.
|
|
8
|
+
*/
|
|
9
|
+
/** The @mujoco/mujoco version the default URL pins. */
|
|
10
|
+
export declare const MUJOCO_VERSION = "4.0.1";
|
|
11
|
+
/** Where MuJoCo loads from when no factory is given. */
|
|
12
|
+
export declare const MUJOCO_URL = "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js";
|
|
13
|
+
/** The line a page shows the first time MuJoCo loads. */
|
|
14
|
+
export declare 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.";
|
|
15
|
+
/** A compiled model: sizes, options and the arrays this library reads. */
|
|
16
|
+
export interface MjModel {
|
|
17
|
+
nq: number;
|
|
18
|
+
nv: number;
|
|
19
|
+
nu: number;
|
|
20
|
+
nbody: number;
|
|
21
|
+
njnt: number;
|
|
22
|
+
ngeom: number;
|
|
23
|
+
nmesh: number;
|
|
24
|
+
nkey: number;
|
|
25
|
+
opt: {
|
|
26
|
+
timestep: number;
|
|
27
|
+
};
|
|
28
|
+
jnt_type: Int32Array;
|
|
29
|
+
jnt_qposadr: Int32Array;
|
|
30
|
+
jnt_dofadr: Int32Array;
|
|
31
|
+
jnt_bodyid: Int32Array;
|
|
32
|
+
actuator_trnid: Int32Array;
|
|
33
|
+
geom_type: Int32Array;
|
|
34
|
+
geom_size: Float64Array;
|
|
35
|
+
geom_rgba: Float32Array;
|
|
36
|
+
geom_group: Int32Array;
|
|
37
|
+
geom_dataid: Int32Array;
|
|
38
|
+
mesh_vertadr: Int32Array;
|
|
39
|
+
mesh_vertnum: Int32Array;
|
|
40
|
+
mesh_faceadr: Int32Array;
|
|
41
|
+
mesh_facenum: Int32Array;
|
|
42
|
+
mesh_vert: Float32Array;
|
|
43
|
+
mesh_face: Int32Array;
|
|
44
|
+
delete(): void;
|
|
45
|
+
}
|
|
46
|
+
/** A model's state; the arrays are views on the wasm heap, live after each step. */
|
|
47
|
+
export interface MjData {
|
|
48
|
+
time: number;
|
|
49
|
+
qpos: Float64Array;
|
|
50
|
+
qvel: Float64Array;
|
|
51
|
+
ctrl: Float64Array;
|
|
52
|
+
xpos: Float64Array;
|
|
53
|
+
xquat: Float64Array;
|
|
54
|
+
geom_xpos: Float64Array;
|
|
55
|
+
geom_xmat: Float64Array;
|
|
56
|
+
delete(): void;
|
|
57
|
+
}
|
|
58
|
+
/** MuJoCo's virtual file system, for the files a model names. */
|
|
59
|
+
export interface MjVFS {
|
|
60
|
+
addBuffer(name: string, bytes: Uint8Array): void;
|
|
61
|
+
delete(): void;
|
|
62
|
+
}
|
|
63
|
+
/** The MuJoCo module: the classes and functions this library calls. */
|
|
64
|
+
export interface Mujoco {
|
|
65
|
+
MjModel: {
|
|
66
|
+
from_xml_string(xml: string, vfs?: MjVFS): MjModel;
|
|
67
|
+
};
|
|
68
|
+
MjData: new (model: MjModel) => MjData;
|
|
69
|
+
MjVFS: new () => MjVFS;
|
|
70
|
+
mj_step(model: MjModel, data: MjData): void;
|
|
71
|
+
mj_forward(model: MjModel, data: MjData): void;
|
|
72
|
+
mj_resetData(model: MjModel, data: MjData): void;
|
|
73
|
+
mj_resetDataKeyframe(model: MjModel, data: MjData, key: number): void;
|
|
74
|
+
mj_id2name(model: MjModel, type: number, id: number): string;
|
|
75
|
+
mjtObj: Record<string, {
|
|
76
|
+
value: number;
|
|
77
|
+
}>;
|
|
78
|
+
}
|
|
79
|
+
/** Options for {@link loadMujoco}. */
|
|
80
|
+
export interface MujocoOptions {
|
|
81
|
+
/** The default export of `@mujoco/mujoco`, when a bundler imports it. */
|
|
82
|
+
factory?: (options?: Record<string, unknown>) => Promise<unknown>;
|
|
83
|
+
/** Where mujoco.js lives when no factory is given. Default {@link MUJOCO_URL}. */
|
|
84
|
+
url?: string;
|
|
85
|
+
/** The wasm as bytes or a URL. Default mujoco.wasm beside mujoco.js. */
|
|
86
|
+
wasm?: ArrayBuffer | Uint8Array | string;
|
|
87
|
+
/** Called once with {@link MUJOCO_LICENCE} when MuJoCo loads. */
|
|
88
|
+
onLicence?: (line: string) => void;
|
|
89
|
+
}
|
|
90
|
+
/** A model or runtime this library cannot open, with the reason in words a page can show. */
|
|
91
|
+
export declare class HumanoidError extends Error {
|
|
92
|
+
constructor(message: string);
|
|
93
|
+
}
|
|
94
|
+
/** Loads MuJoCo once per factory or URL and resolves to the module. */
|
|
95
|
+
export declare function loadMujoco(o?: MujocoOptions): Promise<Mujoco>;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MuJoCo in the page: Google DeepMind's WebAssembly build (`@mujoco/mujoco`,
|
|
3
|
+
* Apache-2.0), the single threaded one, so the page needs no cross-origin
|
|
4
|
+
* isolation. This library never bundles it: pass the factory a bundler
|
|
5
|
+
* imported, or it loads at run time from a URL (default the pinned copy on
|
|
6
|
+
* jsDelivr, its wasm read beside it), and the page is told whose licence
|
|
7
|
+
* applies. The types here cover the part of its surface this library uses.
|
|
8
|
+
*/
|
|
9
|
+
/** The @mujoco/mujoco version the default URL pins. */
|
|
10
|
+
export const MUJOCO_VERSION = "4.0.1";
|
|
11
|
+
/** Where MuJoCo loads from when no factory is given. */
|
|
12
|
+
export const MUJOCO_URL = `https://cdn.jsdelivr.net/npm/@mujoco/mujoco@${MUJOCO_VERSION}/mujoco.js`;
|
|
13
|
+
/** The line a page shows the first time MuJoCo loads. */
|
|
14
|
+
export 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.";
|
|
15
|
+
/** A model or runtime this library cannot open, with the reason in words a page can show. */
|
|
16
|
+
export class HumanoidError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "HumanoidError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const loads = new Map();
|
|
23
|
+
/** Loads MuJoCo once per factory or URL and resolves to the module. */
|
|
24
|
+
export function loadMujoco(o = {}) {
|
|
25
|
+
const key = o.factory ?? o.url ?? MUJOCO_URL;
|
|
26
|
+
const have = loads.get(key);
|
|
27
|
+
if (have)
|
|
28
|
+
return have;
|
|
29
|
+
const p = (async () => {
|
|
30
|
+
let factory = o.factory;
|
|
31
|
+
const url = o.url ?? MUJOCO_URL;
|
|
32
|
+
if (!factory) {
|
|
33
|
+
try {
|
|
34
|
+
factory = (await import(/* @vite-ignore */ url)).default;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw new HumanoidError(`mujoco did not load from ${url}: ${e instanceof Error ? e.message : String(e)}. check the connection, or pass the factory from @mujoco/mujoco.`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const opts = {};
|
|
41
|
+
const w = o.wasm;
|
|
42
|
+
if (w instanceof ArrayBuffer || ArrayBuffer.isView(w))
|
|
43
|
+
opts.wasmBinary = w;
|
|
44
|
+
else if (typeof w === "string")
|
|
45
|
+
opts.locateFile = () => w;
|
|
46
|
+
else if (!o.factory)
|
|
47
|
+
opts.locateFile = (path) => new URL(path, url).href;
|
|
48
|
+
const mj = (await factory(opts));
|
|
49
|
+
o.onLicence?.(MUJOCO_LICENCE);
|
|
50
|
+
return mj;
|
|
51
|
+
})();
|
|
52
|
+
loads.set(key, p);
|
|
53
|
+
p.catch(() => loads.delete(key));
|
|
54
|
+
return p;
|
|
55
|
+
}
|
package/dist/sim.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One robot in MuJoCo: compiled from its files, stepped at the model's own
|
|
3
|
+
* timestep however the page's frames fall, reset to a keyframe, its joint
|
|
4
|
+
* targets written to the position actuators, and its bodies' poses read in
|
|
5
|
+
* MuJoCo's frame (z up, meters, quaternions w x y z). Every handle MuJoCo
|
|
6
|
+
* made is freed by `dispose`.
|
|
7
|
+
*/
|
|
8
|
+
import type { ModelFiles } from "./files.js";
|
|
9
|
+
import { type MjData, type MjModel, type Mujoco } from "./runtime.js";
|
|
10
|
+
/** A body's world pose: position x y z and quaternion w x y z, in MuJoCo's frame. */
|
|
11
|
+
export interface Pose {
|
|
12
|
+
pos: [number, number, number];
|
|
13
|
+
quat: [number, number, number, number];
|
|
14
|
+
}
|
|
15
|
+
/** A robot running in MuJoCo. */
|
|
16
|
+
export declare class Humanoid {
|
|
17
|
+
private mj;
|
|
18
|
+
readonly model: MjModel;
|
|
19
|
+
readonly data: MjData;
|
|
20
|
+
/** Joint names in model order (a free joint included). */
|
|
21
|
+
readonly joints: string[];
|
|
22
|
+
/** The joint each actuator drives, in actuator order: `targets[i]` is that joint's target for a position actuator. */
|
|
23
|
+
readonly actuated: string[];
|
|
24
|
+
readonly bodies: string[];
|
|
25
|
+
readonly keyframes: string[];
|
|
26
|
+
/** Files the model named that did not arrive. */
|
|
27
|
+
readonly missing: string[];
|
|
28
|
+
/** The model's timestep, seconds. */
|
|
29
|
+
readonly timestep: number;
|
|
30
|
+
private carry;
|
|
31
|
+
private gone;
|
|
32
|
+
private ramp;
|
|
33
|
+
private constructor();
|
|
34
|
+
/** Compiles a robot from its files; a model MuJoCo cannot compile is an error with MuJoCo's reason. */
|
|
35
|
+
static open(mj: Mujoco, files: Pick<ModelFiles, "xml" | "assets"> & {
|
|
36
|
+
missing?: string[];
|
|
37
|
+
}): Humanoid;
|
|
38
|
+
/** Seconds simulated so far. */
|
|
39
|
+
get time(): number;
|
|
40
|
+
/** The actuators' controls: targets in radians for position actuators, a view on the model's data. */
|
|
41
|
+
get targets(): Float64Array;
|
|
42
|
+
/** Back to a keyframe by name or index (its controls too), or to the model's rest state. */
|
|
43
|
+
reset(key?: string | number): void;
|
|
44
|
+
/**
|
|
45
|
+
* Eases the targets named (by actuated joint, or an array in actuator order) from where they are to
|
|
46
|
+
* the new values over `seconds` of simulated time, on a smoothstep; others keep theirs. A sudden
|
|
47
|
+
* jump in targets can topple a robot that a ramp of half a second leaves standing (measured on the G1).
|
|
48
|
+
*/
|
|
49
|
+
moveTo(targets: Record<string, number> | ArrayLike<number>, seconds?: number): void;
|
|
50
|
+
/** True while a {@link moveTo} is still easing. */
|
|
51
|
+
get moving(): boolean;
|
|
52
|
+
private applyRamp;
|
|
53
|
+
/** Each actuated joint's target set to where the joint is now: the body holds its pose. */
|
|
54
|
+
hold(): void;
|
|
55
|
+
/** Advances by `seconds` in steps of the model's timestep, carrying the remainder; at most `max` seconds a call (default 0.1). Returns the steps run. */
|
|
56
|
+
step(seconds: number, max?: number): number;
|
|
57
|
+
/** Runs `n` timesteps, calling `control` before each so a controller can set the actuators every step (a PD loop on motor actuators). */
|
|
58
|
+
run(n: number, control?: (sim: Humanoid) => void): void;
|
|
59
|
+
/** Where a hinge or slide joint keeps its position in qpos and its velocity in qvel, or null for a name the model lacks. */
|
|
60
|
+
address(joint: string): {
|
|
61
|
+
qpos: number;
|
|
62
|
+
qvel: number;
|
|
63
|
+
} | null;
|
|
64
|
+
/** A body's world pose, or null for a name the model lacks. */
|
|
65
|
+
pose(body: string | number): Pose | null;
|
|
66
|
+
/** The body carried by the first free joint (a robot's pelvis), or null when the robot is fixed to the world. */
|
|
67
|
+
get rootBody(): string | null;
|
|
68
|
+
/** A shove: adds a velocity in m/s, in MuJoCo's world frame, to the body on the first free joint. False when there is none. */
|
|
69
|
+
push(vx: number, vy: number, vz?: number): boolean;
|
|
70
|
+
/** A hinge or slide joint's position and velocity, or null for a name the model lacks. */
|
|
71
|
+
joint(name: string): {
|
|
72
|
+
q: number;
|
|
73
|
+
dq: number;
|
|
74
|
+
} | null;
|
|
75
|
+
/** Frees the model and its data. */
|
|
76
|
+
dispose(): void;
|
|
77
|
+
}
|
package/dist/sim.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One robot in MuJoCo: compiled from its files, stepped at the model's own
|
|
3
|
+
* timestep however the page's frames fall, reset to a keyframe, its joint
|
|
4
|
+
* targets written to the position actuators, and its bodies' poses read in
|
|
5
|
+
* MuJoCo's frame (z up, meters, quaternions w x y z). Every handle MuJoCo
|
|
6
|
+
* made is freed by `dispose`.
|
|
7
|
+
*/
|
|
8
|
+
import { HumanoidError } from "./runtime.js";
|
|
9
|
+
const FREE = 0;
|
|
10
|
+
const isArrayLike = (v) => Array.isArray(v) || ArrayBuffer.isView(v);
|
|
11
|
+
/** A robot running in MuJoCo. */
|
|
12
|
+
export class Humanoid {
|
|
13
|
+
mj;
|
|
14
|
+
model;
|
|
15
|
+
data;
|
|
16
|
+
/** Joint names in model order (a free joint included). */
|
|
17
|
+
joints;
|
|
18
|
+
/** The joint each actuator drives, in actuator order: `targets[i]` is that joint's target for a position actuator. */
|
|
19
|
+
actuated;
|
|
20
|
+
bodies;
|
|
21
|
+
keyframes;
|
|
22
|
+
/** Files the model named that did not arrive. */
|
|
23
|
+
missing;
|
|
24
|
+
/** The model's timestep, seconds. */
|
|
25
|
+
timestep;
|
|
26
|
+
carry = 0;
|
|
27
|
+
gone = false;
|
|
28
|
+
ramp = null;
|
|
29
|
+
constructor(mj, model, missing) {
|
|
30
|
+
this.mj = mj;
|
|
31
|
+
this.model = model;
|
|
32
|
+
this.data = new mj.MjData(model);
|
|
33
|
+
const name = (type, i) => mj.mj_id2name(model, mj.mjtObj[type].value, i) || `${type.replace("mjOBJ_", "").toLowerCase()} ${i}`;
|
|
34
|
+
this.joints = Array.from({ length: model.njnt }, (_, i) => name("mjOBJ_JOINT", i));
|
|
35
|
+
this.bodies = Array.from({ length: model.nbody }, (_, i) => name("mjOBJ_BODY", i));
|
|
36
|
+
this.keyframes = Array.from({ length: model.nkey }, (_, i) => name("mjOBJ_KEY", i));
|
|
37
|
+
this.actuated = Array.from({ length: model.nu }, (_, i) => this.joints[model.actuator_trnid[i * 2]] ?? `actuator ${i}`);
|
|
38
|
+
this.timestep = model.opt.timestep;
|
|
39
|
+
this.missing = missing;
|
|
40
|
+
mj.mj_forward(model, this.data);
|
|
41
|
+
}
|
|
42
|
+
/** Compiles a robot from its files; a model MuJoCo cannot compile is an error with MuJoCo's reason. */
|
|
43
|
+
static open(mj, files) {
|
|
44
|
+
const vfs = new mj.MjVFS();
|
|
45
|
+
try {
|
|
46
|
+
for (const [path, bytes] of files.assets)
|
|
47
|
+
vfs.addBuffer(path, bytes);
|
|
48
|
+
let model;
|
|
49
|
+
try {
|
|
50
|
+
model = mj.MjModel.from_xml_string(files.xml, vfs);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
const why = e instanceof Error ? e.message : String(e);
|
|
54
|
+
throw new HumanoidError(`mujoco could not compile the model: ${why}${files.missing?.length ? `. files it names that did not arrive: ${files.missing.slice(0, 3).join(", ")}` : ""}`);
|
|
55
|
+
}
|
|
56
|
+
return new Humanoid(mj, model, files.missing ?? []);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
vfs.delete();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Seconds simulated so far. */
|
|
63
|
+
get time() {
|
|
64
|
+
return this.data.time;
|
|
65
|
+
}
|
|
66
|
+
/** The actuators' controls: targets in radians for position actuators, a view on the model's data. */
|
|
67
|
+
get targets() {
|
|
68
|
+
return this.data.ctrl;
|
|
69
|
+
}
|
|
70
|
+
/** Back to a keyframe by name or index (its controls too), or to the model's rest state. */
|
|
71
|
+
reset(key) {
|
|
72
|
+
const k = typeof key === "string" ? this.keyframes.indexOf(key) : key;
|
|
73
|
+
if (typeof key === "string" && k < 0)
|
|
74
|
+
throw new HumanoidError(`the model has no keyframe ${key}; it has ${this.keyframes.join(", ") || "none"}.`);
|
|
75
|
+
if (k === undefined)
|
|
76
|
+
this.mj.mj_resetData(this.model, this.data);
|
|
77
|
+
else
|
|
78
|
+
this.mj.mj_resetDataKeyframe(this.model, this.data, k);
|
|
79
|
+
this.mj.mj_forward(this.model, this.data);
|
|
80
|
+
this.carry = 0;
|
|
81
|
+
this.ramp = null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Eases the targets named (by actuated joint, or an array in actuator order) from where they are to
|
|
85
|
+
* the new values over `seconds` of simulated time, on a smoothstep; others keep theirs. A sudden
|
|
86
|
+
* jump in targets can topple a robot that a ramp of half a second leaves standing (measured on the G1).
|
|
87
|
+
*/
|
|
88
|
+
moveTo(targets, seconds = 0.5) {
|
|
89
|
+
const from = Float64Array.from(this.data.ctrl);
|
|
90
|
+
const to = Float64Array.from(from);
|
|
91
|
+
if (isArrayLike(targets))
|
|
92
|
+
for (let i = 0; i < Math.min(to.length, targets.length); i++)
|
|
93
|
+
to[i] = targets[i];
|
|
94
|
+
else
|
|
95
|
+
for (const [name, v] of Object.entries(targets)) {
|
|
96
|
+
const i = this.actuated.indexOf(name);
|
|
97
|
+
if (i < 0)
|
|
98
|
+
throw new HumanoidError(`no actuator drives ${name}.`);
|
|
99
|
+
to[i] = v;
|
|
100
|
+
}
|
|
101
|
+
this.ramp = { from, to, t0: this.data.time, dur: Math.max(0, seconds) };
|
|
102
|
+
if (seconds <= 0)
|
|
103
|
+
this.applyRamp();
|
|
104
|
+
}
|
|
105
|
+
/** True while a {@link moveTo} is still easing. */
|
|
106
|
+
get moving() {
|
|
107
|
+
return this.ramp !== null;
|
|
108
|
+
}
|
|
109
|
+
applyRamp() {
|
|
110
|
+
const r = this.ramp;
|
|
111
|
+
if (!r)
|
|
112
|
+
return;
|
|
113
|
+
const k = r.dur > 0 ? Math.min(1, (this.data.time - r.t0) / r.dur) : 1;
|
|
114
|
+
const e = k * k * (3 - 2 * k);
|
|
115
|
+
for (let i = 0; i < r.to.length; i++)
|
|
116
|
+
this.data.ctrl[i] = r.from[i] + (r.to[i] - r.from[i]) * e;
|
|
117
|
+
if (k >= 1)
|
|
118
|
+
this.ramp = null;
|
|
119
|
+
}
|
|
120
|
+
/** Each actuated joint's target set to where the joint is now: the body holds its pose. */
|
|
121
|
+
hold() {
|
|
122
|
+
const { model, data } = this;
|
|
123
|
+
for (let i = 0; i < model.nu; i++) {
|
|
124
|
+
const j = model.actuator_trnid[i * 2];
|
|
125
|
+
data.ctrl[i] = data.qpos[model.jnt_qposadr[j]];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** Advances by `seconds` in steps of the model's timestep, carrying the remainder; at most `max` seconds a call (default 0.1). Returns the steps run. */
|
|
129
|
+
step(seconds, max = 0.1) {
|
|
130
|
+
if (this.gone)
|
|
131
|
+
return 0;
|
|
132
|
+
this.carry = Math.min(this.carry + Math.max(0, seconds), max);
|
|
133
|
+
// counted by division, so float drift in the carry never drops a step
|
|
134
|
+
const n = Math.floor(this.carry / this.timestep + 1e-6);
|
|
135
|
+
for (let i = 0; i < n; i++) {
|
|
136
|
+
this.applyRamp();
|
|
137
|
+
this.mj.mj_step(this.model, this.data);
|
|
138
|
+
}
|
|
139
|
+
this.carry = Math.max(0, this.carry - n * this.timestep);
|
|
140
|
+
return n;
|
|
141
|
+
}
|
|
142
|
+
/** Runs `n` timesteps, calling `control` before each so a controller can set the actuators every step (a PD loop on motor actuators). */
|
|
143
|
+
run(n, control) {
|
|
144
|
+
if (this.gone)
|
|
145
|
+
return;
|
|
146
|
+
for (let i = 0; i < n; i++) {
|
|
147
|
+
control?.(this);
|
|
148
|
+
this.applyRamp();
|
|
149
|
+
this.mj.mj_step(this.model, this.data);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Where a hinge or slide joint keeps its position in qpos and its velocity in qvel, or null for a name the model lacks. */
|
|
153
|
+
address(joint) {
|
|
154
|
+
const j = this.joints.indexOf(joint);
|
|
155
|
+
return j < 0 ? null : { qpos: this.model.jnt_qposadr[j], qvel: this.model.jnt_dofadr[j] };
|
|
156
|
+
}
|
|
157
|
+
/** A body's world pose, or null for a name the model lacks. */
|
|
158
|
+
pose(body) {
|
|
159
|
+
const i = typeof body === "number" ? body : this.bodies.indexOf(body);
|
|
160
|
+
if (i < 0 || i >= this.model.nbody)
|
|
161
|
+
return null;
|
|
162
|
+
const p = this.data.xpos, q = this.data.xquat;
|
|
163
|
+
return { pos: [p[i * 3], p[i * 3 + 1], p[i * 3 + 2]], quat: [q[i * 4], q[i * 4 + 1], q[i * 4 + 2], q[i * 4 + 3]] };
|
|
164
|
+
}
|
|
165
|
+
/** The body carried by the first free joint (a robot's pelvis), or null when the robot is fixed to the world. */
|
|
166
|
+
get rootBody() {
|
|
167
|
+
for (let j = 0; j < this.model.njnt; j++)
|
|
168
|
+
if (this.model.jnt_type[j] === FREE)
|
|
169
|
+
return this.bodies[this.model.jnt_bodyid[j]] ?? null;
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
/** A shove: adds a velocity in m/s, in MuJoCo's world frame, to the body on the first free joint. False when there is none. */
|
|
173
|
+
push(vx, vy, vz = 0) {
|
|
174
|
+
for (let j = 0; j < this.model.njnt; j++) {
|
|
175
|
+
if (this.model.jnt_type[j] !== FREE)
|
|
176
|
+
continue;
|
|
177
|
+
const d = this.model.jnt_dofadr[j];
|
|
178
|
+
this.data.qvel[d] += vx;
|
|
179
|
+
this.data.qvel[d + 1] += vy;
|
|
180
|
+
this.data.qvel[d + 2] += vz;
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
/** A hinge or slide joint's position and velocity, or null for a name the model lacks. */
|
|
186
|
+
joint(name) {
|
|
187
|
+
const j = this.joints.indexOf(name);
|
|
188
|
+
if (j < 0)
|
|
189
|
+
return null;
|
|
190
|
+
return { q: this.data.qpos[this.model.jnt_qposadr[j]], dq: this.data.qvel[this.model.jnt_dofadr[j]] };
|
|
191
|
+
}
|
|
192
|
+
/** Frees the model and its data. */
|
|
193
|
+
dispose() {
|
|
194
|
+
if (this.gone)
|
|
195
|
+
return;
|
|
196
|
+
this.gone = true;
|
|
197
|
+
this.data.delete();
|
|
198
|
+
this.model.delete();
|
|
199
|
+
}
|
|
200
|
+
}
|
package/dist/single.d.ts
ADDED
package/dist/single.js
ADDED
package/dist/sonic.d.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GEAR-SONIC, NVIDIA's whole body controller for the Unitree G1, run in the
|
|
3
|
+
* page: an encoder turns a reference motion into a 64 number token, a decoder
|
|
4
|
+
* turns the token and ten ticks of the robot's state into 29 joint actions,
|
|
5
|
+
* 50 times a second; a PD loop here turns actions into motor torques at every
|
|
6
|
+
* MuJoCo step. Written for this library from the published deployment
|
|
7
|
+
* description and parameters (observation names, sizes and order, joint
|
|
8
|
+
* orders, gains from motor armature, action scale, default angles). The
|
|
9
|
+
* details the description leaves open were settled by trial on the deploy
|
|
10
|
+
* model: joint positions relative to the default angles, the reference in
|
|
11
|
+
* absolute angles, histories oldest first, a rotation's first two columns
|
|
12
|
+
* row by row, and the anchor as the base's tilt against an upright reference
|
|
13
|
+
* that follows the robot's heading (measured: only that combination stands).
|
|
14
|
+
* The weights are NVIDIA's, under the NVIDIA Open Model License, and load at
|
|
15
|
+
* run time; onnxruntime-web is an optional peer, passed in or loaded from a URL.
|
|
16
|
+
*/
|
|
17
|
+
import type { Humanoid } from "./sim.js";
|
|
18
|
+
/** The policy files and their terms. */
|
|
19
|
+
export declare const SONIC: {
|
|
20
|
+
readonly name: "gear-sonic, the default g1 controller";
|
|
21
|
+
/** Hugging Face revision the URLs pin. */
|
|
22
|
+
readonly revision: "6733128a3d8a523b1418b06bca3cdf61c8b0987f";
|
|
23
|
+
readonly encoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_encoder.onnx";
|
|
24
|
+
readonly decoder: "https://huggingface.co/nvidia/GEAR-SONIC/resolve/6733128a3d8a523b1418b06bca3cdf61c8b0987f/model_decoder.onnx";
|
|
25
|
+
/** Encoder and decoder together, in bytes (vendor: Hugging Face's sizes). */
|
|
26
|
+
readonly bytes: number;
|
|
27
|
+
readonly hz: 50;
|
|
28
|
+
readonly licence: "gear-sonic weights by nvidia. licensed by nvidia corporation under the nvidia open model license (https://huggingface.co/nvidia/GEAR-SONIC/blob/main/LICENSE). they load at run time and are not part of this library.";
|
|
29
|
+
};
|
|
30
|
+
/** The G1 model the policy was deployed on: motor actuators, its own joint damping, armature and friction. */
|
|
31
|
+
export declare const G1_SONIC: {
|
|
32
|
+
readonly name: "unitree g1, 29 joints, the gear-sonic deploy model";
|
|
33
|
+
/** The scene, at GR00T-WholeBodyControl commit 2a5e6a1; it includes the robot file beside it. */
|
|
34
|
+
readonly url: "https://raw.githubusercontent.com/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/scene_29dof.xml";
|
|
35
|
+
/** The meshes are in Git LFS; pass this as `assetBase` to `fetchModel`. */
|
|
36
|
+
readonly assetBase: "https://media.githubusercontent.com/media/NVlabs/GR00T-WholeBodyControl/2a5e6a1b55d9d0205a96e757b86007ec020cf589/gear_sonic_deploy/g1/";
|
|
37
|
+
readonly licence: "g1 deploy model from nvidia's gr00t-wholebodycontrol (apache-2.0), meshes copyright unitree robotics (bsd-3-clause). they load at run time and are not part of this library.";
|
|
38
|
+
};
|
|
39
|
+
/** The deployment's joint tables, each in the model's joint order (`G1.joints`). */
|
|
40
|
+
export declare const SONIC_JOINTS: {
|
|
41
|
+
/** Standing angles, radians. */
|
|
42
|
+
defaults: number[];
|
|
43
|
+
/** Position gain, N m per rad: armature times (10 Hz in rad/s) squared (computed). */
|
|
44
|
+
kp: number[];
|
|
45
|
+
/** Velocity gain, N m s per rad: 2 times damping ratio 2 times armature times omega (computed). */
|
|
46
|
+
kd: number[];
|
|
47
|
+
/** Torque limit, N m. */
|
|
48
|
+
effort: (5 | 25 | 88 | 139)[];
|
|
49
|
+
/** Radians per unit of action: 0.25 times effort over stiffness (computed). */
|
|
50
|
+
scale: number[];
|
|
51
|
+
/** The policy's (IsaacLab) index of each joint. */
|
|
52
|
+
policyIndex: number[];
|
|
53
|
+
};
|
|
54
|
+
/** A model order array (29) in the policy's order. */
|
|
55
|
+
export declare function toPolicyOrder(v: ArrayLike<number>): number[];
|
|
56
|
+
type Q = [number, number, number, number];
|
|
57
|
+
/** Rotates world vector v into the frame of quaternion q (w x y z): R(q) transposed times v. */
|
|
58
|
+
export declare function intoBody(q: Q, v: [number, number, number]): [number, number, number];
|
|
59
|
+
/**
|
|
60
|
+
* The anchor observation for an upright reference: the base's tilt with its heading removed (heading is
|
|
61
|
+
* the normalized (w, 0, 0, z) of the quaternion), as the first two columns of the rotation from base to
|
|
62
|
+
* reference, read row by row.
|
|
63
|
+
*/
|
|
64
|
+
export declare function anchorSix(base: Q): number[];
|
|
65
|
+
/**
|
|
66
|
+
* The encoder's 1762 inputs in its g1 mode: mode 0, then ten reference frames 0.1 s apart of joint
|
|
67
|
+
* angles (absolute, policy order) and velocities, then ten anchor observations; the other modes' parts
|
|
68
|
+
* stay zero. `ref` holds 10 frames, or 1 repeated.
|
|
69
|
+
*/
|
|
70
|
+
export declare function encoderInput(ref: {
|
|
71
|
+
q: ArrayLike<number>;
|
|
72
|
+
dq?: ArrayLike<number>;
|
|
73
|
+
}[], anchor: number[]): Float32Array;
|
|
74
|
+
/** One control tick of robot state, in the policy's order. */
|
|
75
|
+
export interface SonicState {
|
|
76
|
+
angularVelocity: number[];
|
|
77
|
+
/** Joint angles minus the default angles. */
|
|
78
|
+
q: number[];
|
|
79
|
+
dq: number[];
|
|
80
|
+
gravity: number[];
|
|
81
|
+
}
|
|
82
|
+
/** Ten ticks of state and actions, oldest first, laid out as the decoder reads them. */
|
|
83
|
+
export declare class SonicHistory {
|
|
84
|
+
private av;
|
|
85
|
+
private q;
|
|
86
|
+
private dq;
|
|
87
|
+
private act;
|
|
88
|
+
private g;
|
|
89
|
+
/** Fills all ten ticks with one state and zero actions. */
|
|
90
|
+
prime(s: SonicState): void;
|
|
91
|
+
/** Adds a tick's state, with the actions the last tick produced. */
|
|
92
|
+
add(s: SonicState, lastAction: ArrayLike<number>): void;
|
|
93
|
+
/** The decoder's 994 inputs: the token, then each history oldest first. */
|
|
94
|
+
decoderInput(token: ArrayLike<number>): Float32Array;
|
|
95
|
+
}
|
|
96
|
+
/** The parts of onnxruntime-web this library calls. */
|
|
97
|
+
export interface Ort {
|
|
98
|
+
InferenceSession: {
|
|
99
|
+
create(model: Uint8Array, options?: Record<string, unknown>): Promise<OrtSession>;
|
|
100
|
+
};
|
|
101
|
+
Tensor: new (type: "float32", data: Float32Array, dims: number[]) => unknown;
|
|
102
|
+
env: {
|
|
103
|
+
wasm: {
|
|
104
|
+
numThreads?: number;
|
|
105
|
+
wasmPaths?: string;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** An onnxruntime session. */
|
|
110
|
+
export interface OrtSession {
|
|
111
|
+
run(feeds: Record<string, unknown>): Promise<Record<string, {
|
|
112
|
+
data: unknown;
|
|
113
|
+
}>>;
|
|
114
|
+
release?(): Promise<void>;
|
|
115
|
+
}
|
|
116
|
+
/** Where onnxruntime-web loads from when no module is given. */
|
|
117
|
+
export declare const ORT_URL = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.wasm.min.mjs";
|
|
118
|
+
/** Options for {@link Sonic.load}. */
|
|
119
|
+
export interface SonicOptions {
|
|
120
|
+
/** The onnxruntime-web module; default loaded from {@link ORT_URL}. */
|
|
121
|
+
ort?: Ort;
|
|
122
|
+
/** Where to load onnxruntime-web from. Default {@link ORT_URL}; its wasm is read beside it. */
|
|
123
|
+
ortUrl?: string;
|
|
124
|
+
/** The encoder and decoder as URLs or bytes. Default {@link SONIC}'s pinned URLs. */
|
|
125
|
+
encoder?: string | Uint8Array | ArrayBuffer;
|
|
126
|
+
decoder?: string | Uint8Array | ArrayBuffer;
|
|
127
|
+
fetch?: typeof fetch;
|
|
128
|
+
/** Called with {@link SONIC}'s licence line when the weights load. */
|
|
129
|
+
onLicence?: (line: string) => void;
|
|
130
|
+
/** Bytes read of the two files together. */
|
|
131
|
+
onProgress?: (loaded: number, total: number) => void;
|
|
132
|
+
}
|
|
133
|
+
/** The loaded policy: an encoder and a decoder session. Drive a G1 with {@link Sonic.drive}. */
|
|
134
|
+
export declare class Sonic {
|
|
135
|
+
readonly ort: Ort;
|
|
136
|
+
private encoder;
|
|
137
|
+
private decoder;
|
|
138
|
+
private constructor();
|
|
139
|
+
/** Loads onnxruntime-web and both policy files. */
|
|
140
|
+
static load(o?: SonicOptions): Promise<Sonic>;
|
|
141
|
+
/** The decoder's actions (policy order) for one tick. */
|
|
142
|
+
act(encoderIn: Float32Array, history: SonicHistory): Promise<Float32Array>;
|
|
143
|
+
/** A driver for a G1 whose 29 joints are on motor actuators (the {@link G1_SONIC} model). */
|
|
144
|
+
drive(sim: Humanoid): SonicDriver;
|
|
145
|
+
dispose(): Promise<void>;
|
|
146
|
+
}
|
|
147
|
+
/** Runs the policy on one robot: 50 ticks a second, the PD loop at every timestep between. */
|
|
148
|
+
export declare class SonicDriver {
|
|
149
|
+
private sonic;
|
|
150
|
+
readonly sim: Humanoid;
|
|
151
|
+
/** The reference pose the policy tracks, absolute angles in the model's joint order. Default the standing angles. */
|
|
152
|
+
reference: number[];
|
|
153
|
+
/** Milliseconds the last tick's inference took (measured). */
|
|
154
|
+
inferenceMs: number;
|
|
155
|
+
private adr;
|
|
156
|
+
private act;
|
|
157
|
+
private history;
|
|
158
|
+
private targets;
|
|
159
|
+
private last;
|
|
160
|
+
private carry;
|
|
161
|
+
private busy;
|
|
162
|
+
private perTick;
|
|
163
|
+
constructor(sonic: Sonic, sim: Humanoid);
|
|
164
|
+
/** The robot back at the standing angles, upright at the origin, and the history primed. */
|
|
165
|
+
reset(): void;
|
|
166
|
+
/** The robot's state this tick, in the policy's order. */
|
|
167
|
+
state(): SonicState;
|
|
168
|
+
/** One control tick: the policy's actions to joint targets, then the PD loop for one tick of timesteps. */
|
|
169
|
+
tick(): Promise<void>;
|
|
170
|
+
/** 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. */
|
|
171
|
+
advance(seconds: number): Promise<number>;
|
|
172
|
+
}
|
|
173
|
+
export {};
|