Spaces:
Running
Running
File size: 25,621 Bytes
85e0fe1 d9a54f4 85e0fe1 60f575d 675b16f 85e0fe1 ef0afec 85e0fe1 675b16f 85e0fe1 675b16f 85e0fe1 675b16f 85e0fe1 675b16f 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 60f575d d9a54f4 c36e12e d9a54f4 c36e12e 85e0fe1 c36e12e 85e0fe1 c36e12e 85e0fe1 60f575d c36e12e 60f575d c36e12e 60f575d c36e12e d9a54f4 c36e12e d9a54f4 c36e12e d9a54f4 c36e12e d9a54f4 85e0fe1 d9a54f4 60f575d 85e0fe1 60f575d 85e0fe1 c36e12e d9a54f4 85e0fe1 d9a54f4 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 d9a54f4 85e0fe1 d9a54f4 85e0fe1 60f575d d9a54f4 60f575d 85e0fe1 d9a54f4 60f575d d9a54f4 85e0fe1 d9a54f4 60f575d 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 d9a54f4 85e0fe1 60f575d d9a54f4 60f575d d9a54f4 85e0fe1 d9a54f4 85e0fe1 60f575d d9a54f4 85e0fe1 d9a54f4 85e0fe1 ef0afec 85e0fe1 ef0afec 85e0fe1 ef0afec 675b16f 60f575d d9a54f4 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 d9a54f4 85e0fe1 d9a54f4 85e0fe1 60f575d 85e0fe1 60f575d 85e0fe1 | 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | // Drawers and doors, opened and closed by their bar pulls (the kitchen's, src/../tools/make_kitchen.py),
// as RoboCasa's and the BEHAVIOR Challenge's kitchens ask of a robot. A level bar (a drawer's, the
// microwave's) is grasped from above with the jaws closing across it, so they hold it both ways; an
// upright one (the fridge's) from the side, the hand level, the jaws round it.
// - A drawer slides: the robot stands facing its front and backs straight out with it, or drives in.
// - A door turns on its hinge: the robot drives a circle round the hinge, its axle's line through the
// hinge, so the robot and the door turn as one body and the arm holds still (a base on two wheels
// cannot follow the pull's arc sideways; turning with the door, it does not have to).
// Closing needs no grasp: the closed gripper comes down against the bar's near side and the robot drives
// the same way back, pushing it shut. A door without a pull (the microwave's: its button opens it) has a mark
// instead, a capsule named "*push" on its face that neither collides nor shows: the gripper pushes the face.
import { Footprint, ObstacleMap } from "./nav.js";
import { TUCK, driveToPose, effectivePitch, glide, graspPose, gripper, heldBodies, holding, pushIn, tuckedFootprint, wristReaches } from "./skills.js";
const wrap = (a) => Math.atan2(Math.sin(a), Math.cos(a));
const SLIDE = 2; // mjJNT_SLIDE
/**
* The fixture a joint moves: {name, joint, body, type ("slide" | "hinge"), qpos (address), range, handle
* (the bar pull's geom, a capsule named "*handle" on the body, or a mark where it is pushed, "*push"; or -1),
* pushOnly (the handle is such a mark)}.
*/
export function fixtureOf(sim, joint) {
const { model } = sim;
const j = typeof joint === "string" ? sim.id("joint", joint) : joint;
if (j < 0) return null;
const body = model.jnt_bodyid[j];
let handle = -1;
let pushOnly = false;
for (let g = model.body_geomadr[body]; g < model.body_geomadr[body] + model.body_geomnum[body]; g++) {
const name = model.geom(g).name ?? "";
if (model.geom_type[g] === 3 && /(handle|push)$/.test(name)) [handle, pushOnly] = [g, name.endsWith("push")];
}
return {
name: sim.bodyNames[body],
joint: j,
body,
type: model.jnt_type[j] === SLIDE ? "slide" : "hinge",
qpos: model.jnt_qposadr[j],
range: [model.jnt_range[2 * j], model.jnt_range[2 * j + 1]],
handle,
pushOnly,
};
}
/** How far in front of the bar's middle a closed gripper pushes it (its radius, half the fingers, a gap). */
const PUSH_OFF = 0.035;
/** How far a fixture is open: metres out for a drawer, radians for a door. */
export function openness(sim, fx) {
return sim.data.qpos[fx.qpos];
}
/**
* How far "open" goes: what the fixture's `openTo` says (set from the task registry: as far as the robot
* can turn with a door before the room is in its way, and still get out round it), else a drawer 30 cm
* out, a door to about 85 degrees.
*/
export function openTarget(fx) {
if (fx.openTo !== undefined) return fx.openTo;
return fx.type === "slide" ? Math.min(0.3, fx.range[1] - 0.02) : Math.min(1.48, fx.range[1] - 0.1);
}
/**
* The arm that reaches past an open door into what it closes: the one on the hinge's side. Facing the
* opening, the robot stands toward the door's free edge, clear of the door standing out from the hinge,
* and that arm is the one in line with the inside. Null for a drawer (either arm reaches over its side).
*/
export function reachingArm(sim, fx) {
if (fx.type !== "hinge") return null;
const geo = geometry(sim, fx);
// the face, and the leaf's middle seen from the hinge, as they are with the door shut
const a = -geo.spin * openness(sim, fx);
const shut = ([x, y]) => [Math.cos(a) * x - Math.sin(a) * y, Math.sin(a) * x + Math.cos(a) * y];
const n = shut(geo.face);
const c = shut([geo.panel[0] - geo.hinge[0], geo.panel[1] - geo.hinge[1]]);
// facing in (along -n), the robot's left is (n_y, -n_x); the hinge is at -c from the leaf's middle
return c[1] * n[0] - c[0] * n[1] > 0 ? "left" : "right";
}
// An upright bar is grasped with the hand level, near shoulder height, where the arm reaches best (world,
// m; at 0.8 m the best place to stand for the fridge is near its hinge, which leaves the robot in the
// corner behind the open door), and pushed shut a little lower, by a closed gripper come down just in
// front of it (higher, the wrist stops pointing it down). Both keep clear of the posts: POST_ROOM from the
// bar's ends at least (posts 5 cm in, and half the jaws' height).
const UPRIGHT_GRIP = 0.83;
const UPRIGHT_PUSH = 0.78;
const POST_ROOM = 0.09;
// The level hand comes in to the bar from this far back along its fingers (the tips clear the bar).
const SIDE_BACK = 0.05;
/**
* Where the hand closes on the bar (world: a level bar's middle, an upright bar's point at UPRIGHT_GRIP),
* where a closed gripper pushes it (`push`: in front of it, at UPRIGHT_PUSH on an upright bar), the bar's
* direction, the face it is on, how the jaws close across it (`across`, a world yaw: across the bar, which
* for an upright bar is along the face's normal), the bar's top, and the fixture's motion there.
*/
function geometry(sim, fx) {
const { model, data } = sim;
const g = fx.handle;
let H = Array.from(data.geom_xpos.subarray(3 * g, 3 * g + 3));
const Rg = data.geom_xmat.subarray(9 * g, 9 * g + 9);
const bar = [Rg[2], Rg[5], Rg[8]]; // a capsule lies along its frame's z
const upright = Math.abs(bar[2]) > 0.9;
const Rb = data.xmat.subarray(9 * fx.body, 9 * fx.body + 9);
const ax = model.jnt_axis.subarray(3 * fx.joint, 3 * fx.joint + 3);
// the joint's axis in the world (the body's frame turns it)
const axis = [0, 1, 2].map((r) => Rb[3 * r] * ax[0] + Rb[3 * r + 1] * ax[1] + Rb[3 * r + 2] * ax[2]);
// The face the pull is on looks out along the level part of panel-to-bar that is across the bar (the
// panel: the body's biggest geom, a door's leaf or a drawer's floor); for an upright bar, along the
// leaf's thinnest axis, toward the bar.
let panel = -1;
let big = 0;
for (let k = model.body_geomadr[fx.body]; k < model.body_geomadr[fx.body] + model.body_geomnum[fx.body]; k++) {
const v = model.geom_size[3 * k] * model.geom_size[3 * k + 1] * model.geom_size[3 * k + 2];
if (model.geom_type[k] === 6 && v > big) [panel, big] = [k, v];
}
const C = Array.from(data.geom_xpos.subarray(3 * panel, 3 * panel + 3));
const d = [H[0] - C[0], H[1] - C[1]];
let n;
if (upright) {
const s = model.geom_size.subarray(3 * panel, 3 * panel + 3);
const k = s[0] <= s[1] && s[0] <= s[2] ? 0 : s[1] <= s[2] ? 1 : 2;
const Rp = data.geom_xmat.subarray(9 * panel, 9 * panel + 9);
n = [Rp[k], Rp[3 + k]];
if (d[0] * n[0] + d[1] * n[1] < 0) n = [-n[0], -n[1]];
} else {
const along = d[0] * bar[0] + d[1] * bar[1];
n = [d[0] - along * bar[0], d[1] - along * bar[1]];
}
const nl = Math.hypot(n[0], n[1]) || 1;
const face = [n[0] / nl, n[1] / nl];
let top = H[2] + 0.012;
let P = H;
if (upright) {
// the point on the bar's axis at height z, kept between its posts
const half = model.geom_size[3 * g + 1] * Math.abs(bar[2]);
const on = (z) => {
const at = Math.min(H[2] + half - POST_ROOM, Math.max(H[2] - half + POST_ROOM, z));
const k = (at - H[2]) / bar[2];
return [H[0] + k * bar[0], H[1] + k * bar[1], at];
};
top = H[2] + half + model.geom_size[3 * g];
P = on(UPRIGHT_PUSH);
H = on(UPRIGHT_GRIP);
}
const push = [P[0] + PUSH_OFF * face[0], P[1] + PUSH_OFF * face[1], P[2]];
const across = upright ? Math.atan2(face[1], face[0]) : Math.atan2(bar[1], bar[0]) + Math.PI / 2;
const out = { H, push, bar, level: Math.abs(bar[2]) < 0.2, upright, face, panel: C, top, across };
if (fx.type === "slide") {
out.slide = [axis[0], axis[1]]; // the way it comes out (positive qpos)
} else {
const jp = model.jnt_pos.subarray(3 * fx.joint, 3 * fx.joint + 3);
const B = sim.bodyPos(fx.body);
out.hinge = [0, 1].map((r) => B[r] + Rb[3 * r] * jp[0] + Rb[3 * r + 1] * jp[1] + Rb[3 * r + 2] * jp[2]);
out.spin = Math.sign(axis[2]); // world turn (about +z) per radian of opening
}
return out;
}
/**
* Where to stand to work a fixture from `q0` to `q1` with arm `side`, best first: [{side, pose (chassis
* [x, y, yaw]), u (the bar in the robot frame), drive (+1 forward, -1 back, as it opens), rho (a door: the
* axle's circle round the hinge), clear (how far along it stays clear of the furniture), cost}].
* The robot holds the bar with an arm (graspPose: from above; an upright bar level, from the side,
* further out), its free arms folded; for a drawer it faces the front, for a door it stands on the circle
* through its axle round the hinge, its heading along it. Along the whole way the folded outline keeps
* clear of everything but the fixture (which moves with it).
* `push`: the hand is a closed gripper against the bar's near side (to close it), not round the bar.
*/
export function standPoses(robot, fx, q0, q1, { sides = ["left", "right"], push = false } = {}) {
const sim = robot.sim;
const geo = geometry(sim, fx);
if (!geo.level && !geo.upright) return [];
// where the hand goes: round the bar, or just in front of it
const level = geo.upright && !push;
const H = push ? geo.push : geo.H;
const held = heldBodies(robot);
const outline = tuckedFootprint(robot, held);
const all = new Footprint(new ObstacleMap(sim, robot.root, { objects: true, except: held }), outline);
const others = new Footprint(new ObstacleMap(sim, robot.root, { objects: true, except: [...held, fx.body] }), outline);
const d = outline.axle;
const zc = sim.bodyPos(robot.root)[2];
const span = q1 - q0;
const out = [];
for (const side of sides) {
if (["left", "right"].some((s) => s === side && held.some((id) => holding(robot, s, id)))) continue;
const arm = robot.arms[side];
for (const ux of level ? [0.37, 0.39, 0.41, 0.43] : [0.28, 0.3, 0.32, 0.34, 0.36]) {
for (const dy of [-0.06, -0.03, 0, 0.03, 0.06]) {
const uy = arm.axis[1] + dy;
const options = [];
if (fx.type === "slide") {
const yaw = Math.atan2(-geo.slide[1], -geo.slide[0]);
options.push({ yaw, drive: -1 }); // it opens as the robot backs out
} else {
const D = [H[0] - geo.hinge[0], H[1] - geo.hinge[1]];
const m2 = D[0] * D[0] + D[1] * D[1] - (ux - d) * (ux - d);
if (m2 <= 0) continue;
for (const root of [Math.sqrt(m2), -Math.sqrt(m2)]) {
const srho = root - uy; // the hinge is |srho| to the robot's left (+) or right (-) of its axle
if (Math.abs(srho) < 0.3) continue;
const yaw = Math.atan2(D[1], D[0]) - Math.atan2(root, ux - d);
// the axle moves along the heading as -spin * srho per radian of opening
options.push({ yaw, rho: Math.abs(srho), drive: Math.sign(-geo.spin * srho) || 1 });
}
}
for (const o of options) {
const c = Math.cos(o.yaw);
const s = Math.sin(o.yaw);
const pose = [H[0] - (c * ux - s * uy), H[1] - (s * ux + c * uy), o.yaw];
// in front of the face the pull is on (from behind a door the arm cannot reach round it)
if ((pose[0] - geo.H[0]) * geo.face[0] + (pose[1] - geo.H[1]) * geo.face[1] < 0.12) continue;
if (!all.free(pose[0], pose[1], pose[2], 0.02)) continue;
// the way: straight out along the slide, or round the hinge
let clear = 0;
const steps = Math.max(2, Math.ceil(Math.abs(span) / (fx.type === "slide" ? 0.03 : 0.05)));
for (let k = 1; k <= steps; k++) {
const t = (k / steps) * span;
let p;
if (fx.type === "slide") p = [pose[0] + geo.slide[0] * t, pose[1] + geo.slide[1] * t, pose[2]];
else {
const a = geo.spin * t;
const x = pose[0] - geo.hinge[0];
const y = pose[1] - geo.hinge[1];
p = [geo.hinge[0] + Math.cos(a) * x - Math.sin(a) * y, geo.hinge[1] + Math.sin(a) * x + Math.cos(a) * y, pose[2] + a];
}
if (!others.free(p[0], p[1], p[2], 0.015)) break;
clear = t;
}
// the grasp, and with the base a couple of centimetres off (it stops about so close to a pose)
// (pushing an upright bar, the gripper comes down in front of it, not over its top)
const graspAt = (x, y) =>
level
? sideGrasp(robot, side, [x, y, H[2] - zc])
: graspPose(robot, side, [x, y, H[2] - zc], { width: push ? 0.01 : 0.024, narrowYaw: geo.across - o.yaw, support: H[2] - zc - 0.3, top: (geo.upright ? -Infinity : geo.top) - zc, mode: "top" });
const grasp = graspAt(ux, uy);
if (!grasp || [[0.02, 0], [-0.02, 0], [0, 0.02], [0, -0.02]].some(([ex, ey]) => !graspAt(ux + ex, uy + ey))) continue;
const short = Math.abs(span - clear);
const best = level ? { ux: 0.4, pitch: 0 } : { ux: 0.32, pitch: Math.PI / 2 };
out.push({ side, pose, u: [ux, uy], drive: o.drive, rho: o.rho, clear, cost: 10 * short + Math.abs(ux - best.ux) + Math.abs(dy) + 0.3 * Math.abs(best.pitch - grasp.pitch) });
}
}
}
}
return out.sort((a, b) => a.cost - b.cost);
}
/**
* A level hand round an upright bar at `p` (robot frame), if the arm also reaches where it comes in from
* (SIDE_BACK behind, along the fingers); or null.
*/
function sideGrasp(robot, side, p) {
const pose = graspPose(robot, side, p, { width: 0.024, support: p[2] - 0.3, mode: "side" });
if (!pose) return null;
const back = pose.wrist.map((v, k) => v - SIDE_BACK * pose.along[k]);
return wristReaches(robot, side, back, pose.pitch) ? pose : null;
}
/**
* Grasp the bar from where the robot stands: the gripper over it, down, closed (an upright bar: level, in
* round it from behind). `push`: the gripper closed first, and down just in front of the bar, to push it.
*/
function* grip(robot, fx, side, { push = false } = {}) {
const sim = robot.sim;
const geo = geometry(sim, fx);
const H = push ? geo.push : geo.H;
const p = robot.toRobot(H);
if (geo.upright && !push) return yield* gripLevel(robot, fx, side, p);
const yaw = robot.pose()[2];
const top = geo.upright ? -Infinity : p[2] + (geo.top - H[2]);
const pose = graspPose(robot, side, p, { width: push ? 0.01 : 0.024, narrowYaw: geo.across - yaw, support: p[2] - 0.3, top, mode: "top" });
if (!pose) return { ok: false, message: `the ${side} arm cannot ${push ? "reach" : "grasp"} the pull from here` };
const at = (dz) => ({ ...robot.wristTarget(side, [pose.wrist[0], pose.wrist[1], pose.wrist[2] + dz], pose.pitch), roll: pose.roll });
// The gripper turns to point down near the shoulder first (from the folded arm it points up; turned on
// the way out, it sweeps forward into the door behind the bar), then goes out over the bar and down,
// pointing straight down all the way: high, the wrist joint stops short and the fingers swing forward.
const down = (t) => Math.abs(effectivePitch(t) - pose.pitch) < 0.03;
let lift = 0.12;
while (lift > 0.04 && !down(at(lift))) lift -= 0.01;
const over = at(lift);
let near = { ...over, x: Math.max(0.06, over.x - 0.12) };
for (let k = 0; k < 8 && !down(near); k++) near = { ...near, y: near.y - 0.01 };
// a little open: wider, the far jaw would swing into the front behind the bar (closed, to push)
if (push) yield* gripper(robot, side, false, { duration: 0.3, settle: 0.05 });
else yield* gripper(robot, side, true, { duration: 0.3, settle: 0.05, opening: 0.2 });
yield* glide(robot, side, [{ to: near, duration: 1.0 }, { to: over, duration: 0.8 }, { to: at(0), duration: 0.8 }], { settle: 0.1 });
if (push) return { ok: true };
yield* gripper(robot, side, false, { duration: 0.4, settle: 0.2 });
return gripping(robot, fx, side) ? { ok: true } : { ok: false, message: "the gripper missed the pull" };
}
/**
* Grasp an upright bar at `p` (robot frame) with the hand level: a little open, level near the shoulder at
* the bar's height, out to just behind the bar, in along the fingers round it, closed.
*/
function* gripLevel(robot, fx, side, p) {
const pose = sideGrasp(robot, side, p);
if (!pose) return { ok: false, message: `the ${side} arm cannot grasp the pull from here` };
const at = (back) => ({ ...robot.wristTarget(side, pose.wrist.map((v, k) => v - back * pose.along[k]), pose.pitch), roll: pose.roll });
const out = at(SIDE_BACK);
const near = { ...out, x: Math.max(0.06, out.x - 0.1) };
// opened wider, the moving jaw would swing into the door behind the bar
yield* gripper(robot, side, true, { duration: 0.3, settle: 0.05, opening: 0.25 });
yield* glide(robot, side, [{ to: near, duration: 1.0 }, { to: out, duration: 0.6 }, { to: at(0), duration: 0.8 }], { settle: 0.1 });
yield* gripper(robot, side, false, { duration: 0.4, settle: 0.2 });
return gripping(robot, fx, side) ? { ok: true } : { ok: false, message: "the gripper missed the pull" };
}
/** Whether both jaws touch the fixture. */
function gripping(robot, fx, side) {
const { model, data } = robot.sim;
const jaws = (side === "left" ? ["Fixed_Jaw", "Moving_Jaw"] : ["Fixed_Jaw_2", "Moving_Jaw_2"]).map((n) => robot.sim.id("body", n));
const touched = new Set();
const contacts = data.contact; // a copy, freed by delete()
for (let i = 0; i < data.ncon; i++) {
const c = contacts.get(i);
const b1 = model.geom_bodyid[c.geom1];
const b2 = model.geom_bodyid[c.geom2];
c.delete();
if (b1 === fx.body && jaws.includes(b2)) touched.add(b2);
else if (b2 === fx.body && jaws.includes(b1)) touched.add(b1);
}
contacts.delete();
return touched.size === 2;
}
/**
* Let go of the pull: open just enough to free the bar (the far jaw is between the bar and the front;
* opened further it swings onto the front, and drags it as the hand comes away), straight up off the bar,
* back toward the robot, and the arm folded up (at rest it would reach out and push the door it stands at).
* `level`: the hand is level round an upright bar; it comes straight back off it along the fingers.
* `push`: the gripper is closed in front of the bar; it only comes away.
*/
function* release(robot, side, { push = false, level = false } = {}) {
const arm = robot.arms[side];
if (!push) yield* gripper(robot, side, true, { duration: 0.3, settle: 0.1, opening: level ? 0.3 : 0.18 });
const t = arm.target;
const rest = { ...TUCK, rotation: arm.rotationHome, roll: arm.rollHome };
const away = level ? [{ to: { ...t, x: Math.max(0.05, t.x - 0.07) }, duration: 0.6 }] : [{ to: { ...t, y: t.y + 0.09 }, duration: 0.5 }, { to: { ...t, x: t.x - 0.07, y: t.y + 0.1 }, duration: 0.5 }];
yield* glide(robot, side, [...away, { to: rest, duration: 1.0 }], { settle: 0.1 });
yield* gripper(robot, side, false, { duration: 0.3, settle: 0 });
}
/**
* Drive with the pull in hand until the fixture is at `q1`: straight along the slide, or round the hinge
* as one body with the door. Steers to keep the axle on its line or circle; stops when the fixture gets
* there, stops following, or the way is blocked.
*/
function* carryAlong(robot, fx, stand, q1, { speed = 0.07 } = {}) {
const sim = robot.sim;
const geo = geometry(sim, fx);
const dt = sim.controlDt;
const d = tuckedFootprint(robot, heldBodies(robot)).axle;
const q0 = openness(sim, fx);
const sign = Math.sign(q1 - q0);
const start = robot.pose();
let still = 0;
let last = q0;
let v = 0;
for (let t = 0; t < 40; t += dt) {
const q = openness(sim, fx);
if ((q1 - q) * sign <= 0.004) break;
still = Math.abs(q - last) < 0.0004 && t > 1 ? still + dt : 0;
last = q;
if (still > 1.5) {
robot.stopBase();
return { ok: false, message: `the ${fx.name.replace(/_/g, " ")} stopped moving` };
}
const [x, y, yaw] = robot.pose();
// slow over the last stretch, and to a stop at the end
const left = Math.abs(q1 - q) * (fx.type === "slide" ? 1 : stand.rho);
v = Math.min(speed, v + 0.15 * dt, 0.02 + 1.2 * left);
let w = 0;
if (fx.type === "slide") {
// hold the heading, and the axle on the line it started on
const side = -(x - start[0]) * Math.sin(start[2]) + (y - start[1]) * Math.cos(start[2]);
w = 2.0 * wrap(start[2] - yaw) - 3.0 * side * stand.drive * sign;
} else {
const A = [x + d * Math.cos(yaw) - geo.hinge[0], y + d * Math.sin(yaw) - geo.hinge[1]];
const r = Math.hypot(A[0], A[1]);
const omega = (geo.spin * sign * v) / stand.rho; // the door's (and the robot's) turn rate
// the heading the robot has as one body with the door: turned as far as the door has (the hand
// holds the bar but can swing round it, and a robot turning ahead of the door pulls it less and
// less open); nudged in or out to the radius
const turned = start[2] + geo.spin * (q - q0);
const want = turned + Math.sign(omega) * (stand.drive * sign) * 1.5 * (r - stand.rho);
w = omega + 2.0 * wrap(want - yaw);
}
robot.setBaseVelocity(stand.drive * sign * v, w);
yield;
}
robot.stopBase();
return { ok: true };
}
/**
* Open a door that a button lets go of (`fx.release`, the button's slide joint: the microwave's, which a
* magnet holds shut and a spring opens): press the button in along its slide, 4 mm past its travel (it
* pushes back), with the level gripper (pushIn), and wait for the door to swing open.
*/
function* pressOpen(robot, fx, label) {
const sim = robot.sim;
const { model, data } = sim;
const j = fx.release;
const into = Array.from(data.xaxis.subarray(3 * j, 3 * j + 3));
// the button's face: its cap's middle, out against the press by the cap's depth along it
const g = model.body_geomadr[model.jnt_bodyid[j]];
const R = data.geom_xmat.subarray(9 * g, 9 * g + 9);
const depth = [0, 1, 2].reduce((sum, k) => sum + Math.abs(R[k] * into[0] + R[3 + k] * into[1] + R[6 + k] * into[2]) * model.geom_size[3 * g + k], 0);
const face = [0, 1, 2].map((k) => data.geom_xpos[3 * g + k] - depth * into[k]);
const pressed = yield* pushIn(robot, face, [into[0], into[1], 0], { travel: model.jnt_range[2 * j + 1] + 0.004 });
if (!pressed.ok) return { ok: false, message: `could not press ${label}'s door button: ${pressed.message}` };
for (let t = 0; t < 2 && openness(sim, fx) < 0.2; t += sim.controlDt) yield;
const deg = ((openness(sim, fx) * 180) / Math.PI).toFixed(0);
return openness(sim, fx) > 0.2
? { ok: true, message: `opened ${label} (open ${deg} degrees) with its door button, with the ${pressed.side} arm` }
: { ok: false, message: `pressed ${label}'s door button with the ${pressed.side} arm, but it is open ${deg} degrees` };
}
/**
* Open (`to` > its openness) or close a drawer or door by its pull: drive to where an arm holds the bar
* all the way, grasp it (to close, a closed gripper against it), move with it, let go; a door a button lets
* go of opens by its button. Returns {ok, message}.
*/
export function* moveFixture(robot, fx, to, { what } = {}) {
const sim = robot.sim;
const label = what ?? `the ${fx.name.replace(/_/g, " ")}`;
if (fx.handle < 0) return { ok: false, message: `${label} has no pull to grasp` };
const q0 = openness(sim, fx);
if (Math.abs(to - q0) < (fx.type === "slide" ? 0.01 : 0.03)) return { ok: true, message: `${label} is ${to > 0 ? "open" : "closed"} already` };
// a door that a button lets go of (the microwave's) opens by its button
if (to > q0 && fx.release !== undefined) return yield* pressOpen(robot, fx, label);
if (to > q0 && fx.pushOnly) return { ok: false, message: `${label} has no pull to open it by` };
const push = to < q0;
const level = geometry(sim, fx).upright && !push;
let stand = null;
let got = null;
for (let attempt = 0; attempt < 2 && !got?.ok; attempt++) {
const stands = standPoses(robot, fx, q0, to, { push });
if (!stands.length) return { ok: false, message: `there is no clear spot to stand from which an arm holds ${label}'s pull` };
// the fixture is in the way like any furniture until the robot stands at its pull (the best few
// places to stand: the best may be squeezed in where there is no way to it)
const went = yield* driveToPose(robot, stands.slice(0, 4).map((s) => s.pose), { what: `${label}'s pull` });
if (!went.ok) return { ok: false, message: went.message };
stand = stands[went.index];
got = yield* grip(robot, fx, stand.side, { push });
// missed: let go, square up to the pull again and grasp once more
if (!got.ok) yield* release(robot, stand.side, { push, level });
}
if (!got.ok) return got;
const end = q0 + stand.clear; // as far as the way is clear
const moved = yield* carryAlong(robot, fx, stand, end);
yield* release(robot, stand.side, { push, level });
const q = openness(sim, fx);
// open or closed as the tasks' `open` says (src/tasks/household.js): a drawer 5 cm out, a door 11 degrees
const done = to > q0 ? q > (fx.type === "slide" ? 0.05 : 0.2) : q <= (fx.type === "slide" ? 0.05 : 0.2);
const how = fx.type === "slide" ? `${(q * 100).toFixed(0)} cm out` : `open ${((q * 180) / Math.PI).toFixed(0)} degrees`;
return { ok: done, message: done ? `${to > q0 ? "opened" : "closed"} ${label} (${q < (fx.type === "slide" ? 0.02 : 0.05) ? "shut" : how}) with the ${stand.side} arm` : `${label} is ${how}${moved.ok ? "" : `: ${moved.message}`}` };
}
|