File size: 4,118 Bytes
7a35d14
 
 
 
 
 
 
 
 
5fc5f70
 
 
 
7a35d14
5fc5f70
7a35d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// The pick task's random layouts in the tabletop scene, for the demonstration generator and the
// policy evaluation: the robot in front of the long table and one object in reach of one arm.
import { loadScene } from "./node_env.mjs";
import { graspPlan } from "../src/robots/skills.js";

export const TABLE = { edgeY: -0.3, topZ: 0.82 }; // tabletop scene, table2
export const OBJECTS = ["sphere1", "sphere2", "box1", "cylinder1"]; // box2 is wider than the gripper opens
const SPARE_SPOTS = [[-1.25, -0.5], [-1.25, -0.2], [-1.25, 0.1], [-1.25, 0.4], [-1.45, 0.0]]; // on the far table

/**
 * Load the tabletop scene with MuJoCo-GS-Web's objects (the blocks and balls the pick policy was trained
 * on, not the scene's kitchen objects), let them settle and return {sim, robot, layout(rng)}.
 */
export async function pickScene() {
  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();

  // Object sizes vary between episodes so the policy covers more than the scene's five objects:
  // the target's collision geom is resized in the compiled model (and its bounding radius with it).
  const originals = new Map();
  function resize(bodyId, rng) {
    const { model } = sim;
    const g = model.body_geomadr[bodyId];
    if (!originals.has(g)) originals.set(g, Array.from(model.geom_size.subarray(3 * g, 3 * g + 3)));
    const type = model.geom_type[g];
    let size;
    if (type === 2) size = [rng.uniform(0.018, 0.026), 0, 0]; // sphere
    else if (type === 5) size = [rng.uniform(0.017, 0.025), rng.uniform(0.03, 0.06), 0]; // cylinder: radius, half height
    else size = [rng.uniform(0.015, 0.03), rng.uniform(0.015, 0.03), rng.uniform(0.015, 0.05)]; // box half sizes
    model.geom_size.set(size, 3 * g);
    model.geom_rbound[g] = type === 2 ? size[0] : type === 5 ? Math.hypot(size[0], size[1]) : Math.hypot(...size);
  }
  function restoreSizes() {
    for (const [g, size] of originals) {
      sim.model.geom_size.set(size, 3 * g);
      const type = sim.model.geom_type[g];
      sim.model.geom_rbound[g] = type === 2 ? size[0] : type === 5 ? Math.hypot(size[0], size[1]) : Math.hypot(...size);
    }
  }

  function setFree(bodyId, pos, yaw) {
    const j = sim.model.body_jntadr[bodyId];
    const q = sim.model.jnt_qposadr[j];
    const v = sim.model.jnt_dofadr[j];
    sim.data.qpos.set([pos[0], pos[1], pos[2], Math.cos(yaw / 2), 0, 0, Math.sin(yaw / 2)], q);
    sim.data.qvel.fill(0, v, v + 6);
  }

  /** Restore the settled scene and lay out a random episode; returns null if it is not graspable. */
  function layout(rng, { randomSize = true } = {}) {
    restoreSizes();
    sim.restore(settled);
    robot.home();
    const gap = rng.uniform(0.03, 0.06);
    const yaw = -Math.PI / 2 + rng.uniform(-0.2, 0.2);
    const x = rng.uniform(-0.6, 0.6);
    robot.spawn(x, TABLE.edgeY + 0.196 + gap + 0.23 * Math.abs(Math.sin(yaw + Math.PI / 2)), yaw);
    robot.apply();
    robot.snapJoints();
    sim.forward();
    const side = rng.choice(["left", "right"]);
    const name = rng.choice(OBJECTS);
    const id = sim.id("body", name);
    OBJECTS.concat("box2").forEach((other, k) => {
      if (other === name) return;
      const [sx, sy] = SPARE_SPOTS[k];
      setFree(sim.id("body", other), [sx, sy, TABLE.topZ + 0.07], 0);
    });
    // The object goes in front of the arm, on the table.
    const arm = robot.arms[side];
    const reach = rng.uniform(0.15, 0.29);
    const lateral = rng.uniform(-0.08, 0.08);
    const p = robot.toWorld([arm.axis[0] + reach, arm.axis[1] + lateral, 0]);
    if (p[1] > TABLE.edgeY - 0.035) return null;
    if (randomSize) resize(id, rng);
    setFree(id, [p[0], p[1], TABLE.topZ + 0.08], rng.uniform(-Math.PI, Math.PI));
    sim.forward();
    sim.run(0.6, control); // the objects drop onto the tables
    const plan = graspPlan(robot, id, side);
    if (plan.reason) return null;
    return { side, name, id, startZ: sim.bodyPos(id)[2] };
  }

  return { sim, robot, layout };
}