File size: 1,726 Bytes
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
// The pick policy in the kitchen, where it was not trained: go to each object with the approach skill,
// then let the policy pick it. Uses the page's code and ONNX Runtime Web, as tools/eval_policy.mjs.
//
// usage: node tools/eval_kitchen.mjs [assets/policies/xlerobot_pick]
import { readFileSync } from "node:fs";
import { join } from "node:path";
import * as ort from "onnxruntime-web";
import { loadScene, ROOT } from "./node_env.mjs";
import { approach, restArm, runSkill, runSkillAsync } from "../src/robots/skills.js";
import { PolicyRunner, policyPick } from "../src/policy/runner.js";

const dir = process.argv[2] ?? join(ROOT, "assets", "policies", "xlerobot_pick");
ort.env.wasm.numThreads = 1;
const meta = JSON.parse(readFileSync(join(dir, "policy.json"), "utf8"));
const runner = await PolicyRunner.load(ort, readFileSync(join(dir, meta.onnx)), meta);
const { sim, robot } = await loadScene("kitchen");
sim.reset(() => {
  robot.home();
  robot.snapJoints();
});
sim.run(1.0, () => robot.update());
const settled = sim.snapshot();
let ok = 0;
const names = ["orange", "apple", "cube"];
for (const name of names) {
  sim.restore(settled);
  robot.home();
  const id = sim.id("body", name);
  const a = runSkill(sim, robot, approach(robot, id), { maxSeconds: 60 });
  // The go-to skill drives with the arms folded; the policy learned to start with the arm at rest.
  if (a.ok) runSkill(sim, robot, restArm(robot, a.side));
  const r = a.ok ? await runSkillAsync(sim, robot, policyPick(robot, runner, id, a.side), { maxSeconds: 14 }) : { ok: false, message: a.message };
  if (r.ok) ok++;
  console.log(`${name}: ${a.message} -> ${r.message}`);
}
console.log(`kitchen: the policy picked ${ok}/${names.length}`);