File size: 2,504 Bytes
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
// The pick task: lift an object from a table with one arm. Shared by the demonstration generator
// (tools/gen_demos.mjs), the policy evaluation (tools/eval_policy.mjs) and the browser policy runner,
// so the policy sees the same observation everywhere.
//
// Observation, in the arm's own terms so one policy serves both arms:
//   observation.state (6): the arm's joints [Rotation - home, Pitch, Elbow, Wrist_Pitch, Wrist_Roll, Jaw] (rad)
//   observation.environment_state (7): object centre from the arm's rotation axis in the robot frame
//     (x forward, y left, z up; m), object width and height (m), and its box yaw relative to the arm
//     as [sin 2a, cos 2a] (zeros for round objects)
// Action (6): joint targets in the same order and offsets as observation.state.
import { objectInfo } from "../robots/skills.js";

export const FEATURES = {
  state: ["rotation", "pitch", "elbow", "wrist_pitch", "wrist_roll", "jaw"],
  environment: ["object_x", "object_y", "object_z", "object_width", "object_height", "box_sin2", "box_cos2"],
  action: ["rotation", "pitch", "elbow", "wrist_pitch", "wrist_roll", "jaw"],
};

/** The policy's inputs for `side` and the object `bodyId`. */
export function observe(robot, side, bodyId) {
  const arm = robot.arms[side];
  const q = robot.armPositions(side);
  const state = [q[0] - arm.rotationHome, q[1], q[2], q[3], q[4], q[5]];
  const info = objectInfo(robot.sim, bodyId);
  const c = robot.toRobot(info.center);
  const rel = [c[0] - arm.axis[0], c[1] - arm.axis[1], c[2] - arm.axis[2]];
  let s2 = 0;
  let c2 = 0;
  if (info.isBox) {
    const a = info.yaw - robot.pose()[2] - Math.atan2(rel[1], rel[0]);
    s2 = Math.sin(2 * a);
    c2 = Math.cos(2 * a);
  }
  return { state, environment: [...rel, info.width, info.height, s2, c2] };
}

/** The arm's current joint targets as an action (same offsets as the state). */
export function currentAction(robot, side) {
  const arm = robot.arms[side];
  const t = robot.armTargets(side);
  return [t[0] - arm.rotationHome, t[1], t[2], t[3], t[4], t[5]];
}

/** Apply a policy action to an arm. */
export function applyAction(robot, side, action) {
  const arm = robot.arms[side];
  robot.setArmJoints(side, [action[0] + arm.rotationHome, action[1], action[2], action[3], action[4], action[5]]);
}

/** The object counts as picked when it is 4 cm above where it started and between the fingers. */
export function lifted(robot, bodyId, startZ) {
  return robot.sim.bodyPos(bodyId)[2] - startZ > 0.04;
}