File size: 10,198 Bytes
d72771b
 
 
 
60f575d
d72771b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60f575d
d72771b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60f575d
 
 
d72771b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Checks of the web app's logic in Node, with the page's own WebAssembly physics:
// teleop keys and the base speed loop, scripted picks in the tabletop scene, grasps of objects turned
// every way, go-to and pick of every object in the tabletop scene without the base touching furniture,
// go-to / pick / place in the kitchen, the kitchen's household tasks (their BDDL problems, and the
// scripted demonstrator doing five of them), and that reading contacts does not leak WebAssembly memory.
// Exits non-zero if a check fails.
//
// usage: node tools/check_sim.mjs
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { ASSETS, loadScene } from "./node_env.mjs";
import { Episode, Household } from "../src/tasks/household.js";
import { solve } from "../src/tasks/solver.js";
import { XLeRobotTeleop } from "../src/robots/xlerobot.js";
import { approach, objectInfo, pick, place, runSkill } from "../src/robots/skills.js";
import { ContactWatch } from "../src/robots/nav.js";

let failed = 0;
const check = (ok, what) => {
  console.log(`${ok ? "PASS" : "FAIL"}: ${what}`);
  if (!ok) failed++;
};

// Teleop and base, on the empty floor.
{
  const { sim, robot } = await loadScene("basic");
  const control = () => robot.update();
  sim.reset(() => robot.home());
  sim.run(0.5, control);
  const teleop = new XLeRobotTeleop(robot);
  const p0 = robot.pose();
  teleop.down.add("KeyW");
  sim.run(2.0, (dt) => (teleop.tick(dt), robot.update()));
  teleop.down.clear();
  sim.run(0.5, (dt) => (teleop.tick(dt), robot.update()));
  const p1 = robot.pose();
  const moved = (p1[0] - p0[0]) * Math.cos(p0[2]) + (p1[1] - p0[1]) * Math.sin(p0[2]);
  check(moved > 0.3, `W drives forward (${moved.toFixed(2)} m in 2 s)`);
  const y0 = robot.armPositions("left")[1];
  teleop.down.add("Digit8");
  sim.run(0.5, (dt) => (teleop.tick(dt), robot.update()));
  teleop.down.clear();
  check(Math.abs(robot.armPositions("left")[1] - y0) > 0.05, "8 moves the left arm");
  robot.setBaseVelocity(0.2, 0);
  sim.run(3, control);
  const [v] = robot.baseVelocity();
  check(Math.abs(v - 0.2) < 0.04, `base speed loop holds 0.2 m/s (${v.toFixed(3)})`);
  robot.stopBase();
}

// Scripted picks in the tabletop scene, with the robot in front of the long table.
{
  const { sim, robot } = await loadScene("tabletop", "xlerobot", { objects: "robot" });
  const control = () => robot.update();
  sim.reset(() => robot.home());
  sim.run(1.5, control);
  const settled = sim.snapshot();
  let ok = 0;
  const cases = [["box1", 0.25], ["sphere2", 0.05], ["cylinder1", 0.25], ["sphere1", 0.35], ["box1", 0.0], ["sphere2", 0.3], ["cylinder1", 0.5], ["sphere1", 0.55]];
  for (const [name, x] of cases) {
    sim.restore(settled);
    robot.home();
    robot.spawn(x, -0.09, -Math.PI / 2);
    robot.snapJoints();
    sim.forward();
    sim.run(0.5, control);
    if (runSkill(sim, robot, pick(robot, sim.id("body", name))).ok) ok++;
  }
  check(ok >= cases.length - 1, `tabletop scripted picks ${ok}/${cases.length}`);
}

// Grasps that follow how an object lies: a block, a banana and a mug turned to different yaws, in
// front of the robot at the tabletop's back counter (the other objects moved out of the way).
{
  const { sim, robot } = await loadScene("tabletop");
  const control = () => robot.update();
  sim.reset(() => {
    robot.home();
    robot.snapJoints();
  });
  sim.run(1.0, control);
  const settled = sim.snapshot();
  const { model } = sim;
  let ok = 0;
  let n = 0;
  for (const [name, yaws] of [["cube", [0, 30, 60]], ["banana", [0, 50, 100]], ["mug", [0, 150]]]) {
    const id = sim.id("body", name);
    const qa = model.jnt_qposadr[model.body_jntadr[id]];
    const z = settled.qpos[qa + 2];
    const q0 = Array.from(settled.qpos.subarray(qa + 3, qa + 7));
    for (const deg of yaws) {
      sim.restore(settled);
      robot.home();
      robot.spawn(0, -0.3 + 0.022 + 0.196, -Math.PI / 2);
      robot.apply();
      robot.snapJoints();
      sim.forward();
      sim.freeBodies([robot.root]).forEach((o, k) => {
        if (o.id !== id) sim.data.qpos.set([3 + 0.3 * k, 3, 0.1, 1, 0, 0, 0], model.jnt_qposadr[model.body_jntadr[o.id]]);
      });
      const p = robot.toWorld([0.27, robot.arms.left.axis[1], 0]);
      const a = (deg * Math.PI) / 180;
      const [w, x, y, zq] = [Math.cos(a / 2), 0, 0, Math.sin(a / 2)];
      // the yaw on top of how it rests
      const q = [w * q0[0] - zq * q0[3], w * q0[1] - zq * q0[2], w * q0[2] + zq * q0[1], w * q0[3] + zq * q0[0]];
      sim.data.qpos.set([p[0], p[1], z, ...q], qa);
      sim.forward();
      sim.run(0.5, control);
      n++;
      if (runSkill(sim, robot, pick(robot, id, { side: "left" })).ok) ok++;
    }
  }
  check(ok >= n - 1, `grasps across objects' narrow sides, turned every way: ${ok}/${n}`);
}

// Tabletop: from the start, go to each object and pick it (the bowl and plate: go to them). The base
// must never touch the counters.
{
  const { sim, robot } = await loadScene("tabletop");
  const control = () => robot.update();
  sim.reset(() => {
    robot.home();
    robot.snapJoints();
  });
  sim.run(1.0, control);
  const settled = sim.snapshot();
  const watch = new ContactWatch(sim, robot.root);
  let reached = 0;
  let picked = 0;
  let touched = 0;
  const names = sim.freeBodies([robot.root]).map((o) => o.name);
  const containers = ["bowl", "plate"];
  for (const name of names) {
    sim.restore(settled);
    robot.home();
    robot.snapJoints();
    sim.forward();
    const id = sim.id("body", name);
    let phase = "go";
    const onTick = () => {
      const c = watch.check();
      // a finger on the worktop while grasping is picking; the base on furniture is not
      if (c && (phase === "go" || /chassis|wheel/.test(c.robot))) touched++;
    };
    const a = runSkill(sim, robot, approach(robot, id), { maxSeconds: 60, onTick });
    if (a.ok) reached++;
    phase = "pick";
    if (a.ok && !containers.includes(name) && runSkill(sim, robot, pick(robot, id), { maxSeconds: 30, onTick }).ok) picked++;
    if (!a.ok) console.log(`   ${name}: ${a.message}`);
  }
  check(reached === names.length, `tabletop: went to ${reached}/${names.length} objects`);
  check(picked >= names.length - containers.length - 1, `tabletop: picked ${picked}/${names.length - containers.length}`);
  check(touched === 0, `tabletop: the base never touched furniture (${(touched * sim.controlDt).toFixed(2)} s of contact)`);
}

// Kitchen: go to the orange, pick it, put it in the bowl.
{
  const { sim, robot } = await loadScene("kitchen");
  const control = () => robot.update();
  sim.reset(() => {
    robot.home();
    robot.snapJoints();
  });
  sim.run(1.0, control);
  const settled = sim.snapshot();
  // The can is behind the island from the start: around it, without touching it.
  const watch = new ContactWatch(sim, robot.root);
  let touched = 0;
  const can = sim.id("body", "can");
  const toCan = runSkill(sim, robot, approach(robot, can), { maxSeconds: 60, onTick: () => (watch.check() ? touched++ : 0) });
  const gotCan = toCan.ok && runSkill(sim, robot, pick(robot, can)).ok;
  check(gotCan && touched === 0, `kitchen: around the island to the can and pick it (${toCan.message}; ${touched} ticks touching)`);
  sim.restore(settled);
  robot.home();
  robot.snapJoints();
  sim.forward();
  const orange = sim.id("body", "orange");
  const bowl = sim.id("body", "bowl");
  const a = runSkill(sim, robot, approach(robot, orange), { maxSeconds: 40 });
  check(a.ok, `kitchen: go to the orange (${a.message})`);
  const p = runSkill(sim, robot, pick(robot, orange));
  check(p.ok, `kitchen: pick the orange (${p.message})`);
  const b = robot.toRobot(sim.bodyPos(bowl));
  const info = objectInfo(sim, bowl);
  runSkill(sim, robot, place(robot, p.side, [b[0], b[1], robot.toRobot([0, 0, info.bottom])[2] + 0.012], orange));
  sim.run(0.5, control);
  const o = sim.bodyPos(orange);
  const c = sim.bodyPos(bowl);
  check(Math.hypot(o[0] - c[0], o[1] - c[1]) < 0.055, "kitchen: the orange ends in the bowl");
}

// Household tasks: every BDDL problem parses and grounds; the demonstrator does five of them; and the
// contact checks the base makes every control period do not grow the WebAssembly heap.
{
  const { sim, robot } = await loadScene("kitchen");
  sim.reset(() => {
    robot.home();
    robot.snapJoints();
  });
  sim.run(1.0, () => robot.update());
  const settled = sim.snapshot();
  const dir = join(ASSETS, "environments", "kitchen", "tasks");
  const registry = JSON.parse(readFileSync(join(dir, "index.json"), "utf8"));
  const problems = Object.fromEntries(registry.tasks.map((t) => [t.file, readFileSync(join(dir, t.file), "utf8")]));
  const household = new Household(sim, robot, registry, problems);
  const literals = household.tasks.reduce((n, t) => n + t.goals.length, 0);
  check(household.tasks.length === registry.tasks.length && literals >= 20, `tasks: ${household.tasks.length}/${registry.tasks.length} BDDL problems, ${literals} goal conditions`);
  // a switch, things carried to a shelf, the fridge door and the island drawer opened and shut, the
  // microwave's door pushed shut
  for (const [id, seed] of [["turning_on_radio", 0], ["putting_away_groceries", 0], ["getting_the_juice", 0], ["putting_away_the_cube", 0], ["closing_the_microwave", 0]]) {
    const task = household.task(id);
    household.setup(task, seed, settled);
    const episode = new Episode(household, task, seed);
    runSkill(sim, robot, solve(household, episode), {
      maxSeconds: task.timeLimit,
      onTick: () => {
        household.update();
        episode.tick(sim.controlDt);
      },
    });
    const r = episode.report();
    check(r.success, `tasks: the demonstrator does ${id} #${seed} (${r.score} in ${r.time_s} s)`);
  }
  const watch = new ContactWatch(sim, robot.root);
  const heap = () => sim.data.qpos.buffer.byteLength;
  const before = heap();
  for (let i = 0; i < 3000; i++) watch.check();
  check(heap() === before, `memory: 3000 contact checks leave the WebAssembly heap as it was (${(before / 1e6).toFixed(0)} MB)`);
}

console.log(failed ? `${failed} check(s) failed` : "all checks passed");
process.exit(failed ? 1 : 0);