File size: 25,402 Bytes
98a23ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Scripted solutions of our robocurve-inspired dev tasks (rc_*, PROD-120; dev_suite/robocurve/README.md).

As in dev_scripts: they read the objects from the episode's layout file (not the cameras) and show that each task can be
done and that its success check fires. Most need both hands at once (``move_both``). Object frames: the rc_ objects'
sizes and part positions are in dev_suite/robocurve/make_objects.py (asset frame: bbox centre at the origin).
Run with ``remote_eval --planner script --script dev --tasks rc_...``.
"""

from __future__ import annotations

import logging
import math

import numpy as np

from robodojo_agent.dev_scripts import Obj, flat, fold, heading, point, scene
from robodojo_agent.frames import TABLE_Z, grasp_point
from robodojo_agent.library import call
from robodojo_agent.target import Target

log = logging.getLogger("rc_scripts")
ARMS = ("left", "right")


def hand(p, pitch: float = 0.0, yaw: float = 0.0, spin: float = 0.0) -> dict:
    """A ``move_both`` hand: grasp point p; pitch 0 = fingers down, yaw = the direction (deg) the jaws close along
    (folded into +-90 for a top-down hand: the jaws are symmetric, and the arms cannot turn the wrist all the way)."""
    return {"point": [float(v) for v in p], "pitch_deg": pitch, "yaw_deg": fold(yaw) if pitch == 0.0 else yaw, "spin_deg": spin}


def pinch(arm: str, p, close_deg: float, hover: float = 0.10):
    """Fingers down, jaws closing along ``close_deg``: open over p, go down to it, close. Returns the close outcome."""
    yield call("open", arm=arm)
    yield call("move", point(np.asarray(p) + [0, 0, hover], "above", arm), arm=arm, pitch_deg=0.0, yaw_deg=fold(close_deg))
    out = yield call("move", point(p, "grasp point", arm), arm=arm, pitch_deg=0.0, yaw_deg=fold(close_deg))
    if not out:
        log.warning("%s: %s", arm, out.note)
    return (yield call("close", arm=arm))


def top_down_first(p, close_deg: dict):
    """Turn both hands fingers-down where they are (home) before a long move: a long move that also turns the hand
    takes the arm on a wide path, which sweeps light objects off the table."""
    here = {a: grasp_point(p.obs.ee_pose[a]) + [0, 0, 0.03] for a in ARMS}
    yield call("move_both", left=hand(here["left"], yaw=close_deg["left"]), right=hand(here["right"], yaw=close_deg["right"]),
               tol=0.03)


def pinch_both(p, points: dict, close_deg: dict, hover: float = 0.10):
    """Both hands open over their points (from high up: a light object is not brushed on the way), go down together and
    close: nothing is lifted before both hold."""
    yield call("open")
    yield from top_down_first(p, close_deg)
    yield call("move_both", left=hand(np.asarray(points["left"]) + [0, 0, hover + 0.05], yaw=close_deg["left"]),
               right=hand(np.asarray(points["right"]) + [0, 0, hover + 0.05], yaw=close_deg["right"]))
    yield call("move_both", left=hand(np.asarray(points["left"]) + [0, 0, hover], yaw=close_deg["left"]),
               right=hand(np.asarray(points["right"]) + [0, 0, hover], yaw=close_deg["right"]))
    yield call("move_both", left=hand(points["left"], yaw=close_deg["left"]),
               right=hand(points["right"], yaw=close_deg["right"]), tol=0.035, max_step_m=0.01)
    for arm in ARMS:
        out = yield call("close", arm=arm)
        log.info("%s", out.note)


def carry_both(moves: list, yaw: dict, pitch: float = 0.0, tol: float = 0.03, step: float | None = 0.01):
    """``moves``: a list of {arm: point}; both hands go through them in step, keeping their orientation."""
    for m in moves:
        out = yield call("move_both", left=hand(m["left"], pitch, yaw["left"]), right=hand(m["right"], pitch, yaw["right"]),
                         tol=tol, max_step_m=step)
        if not out:
            log.warning("carry: %s", out.note)


def release_both(up: float = 0.08):
    yield call("open")
    yield call("nudge", None, arm="left", dz=up)
    yield call("nudge", None, arm="right", dz=up)
    yield call("home")


# ----------------------------------------------------------------------------------------------------------------------
def rc_plank_unload(p):
    """Either load alone tips the plank. Both hands close on their load first (left: the cup, right: the block), then
    lift together, so the plank never carries one load alone. Then each puts its load in the tray, one after the other."""
    s = scene()
    cup, block, tray, plank = s["cup"], s["block"], s["tray"], s["plank"]
    across = plank.yaw(0) + 90.0  # jaws across the plank's length
    top = {"left": cup.pos[2] + cup.extents[2] / 2, "right": block.pos[2] + block.extents[2] / 2}
    grip = {"left": [cup.pos[0], cup.pos[1], top["left"] - 0.025], "right": [block.pos[0], block.pos[1], top["right"] - 0.015]}
    yield from pinch_both(p, grip, {"left": across, "right": across}, hover=0.05)  # low: reach shrinks with height
    up = {a: np.asarray(grip[a]) + [0, 0, 0.08] for a in ARMS}
    yield from carry_both([up], {"left": across, "right": across})
    for arm, size in (("right", block.extents[2]), ("left", cup.extents[2])):
        dest = tray.pos + [0.03 if arm == "right" else -0.03, 0, 0]
        yield call("place", Target((dest[0], dest[1], TABLE_Z + 0.006), "tray", arm=arm))
        yield call("home", arm=arm)
    yield call("home")


def rc_carry_box_stack(p):
    """Each hand pinches one end of the bottom box (the middle box covers its middle), across its 7 cm depth; both
    lift together, carry the stack over the platform and set it down level."""
    s = scene()
    box, plat = s["box_long"], s["platform"]
    ax = flat(box.rot[:, 0])
    across = box.yaw(0) + 90.0
    z = box.pos[2] + box.extents[2] / 2 - 0.02
    ends = {"left": box.pos - ax * 0.085, "right": box.pos + ax * 0.085}
    if ends["left"][0] > ends["right"][0]:
        ends = {"left": ends["right"], "right": ends["left"]}
    grip = {a: [ends[a][0], ends[a][1], z] for a in ARMS}
    yield from pinch_both(p, grip, {"left": across, "right": across})
    lift = 0.06 + 0.04  # the platform is 6 cm tall
    offset = {a: np.asarray(grip[a]) - box.pos for a in ARMS}  # the grip relative to the box's centre
    dest = plat.pos.copy()
    dest[2] = TABLE_Z + 0.06 + box.extents[2] / 2 + 0.005
    moves = [{a: np.asarray(grip[a]) + [0, 0, lift] for a in ARMS},
             {a: dest + offset[a] + [0, 0, 0.04] for a in ARMS},
             {a: dest + offset[a] for a in ARMS}]
    yield from carry_both(moves, {"left": across, "right": across})
    yield from release_both()


def rc_box_on_edge_in_slot(p):
    """The box is wider than the gripper every way, so both hands squeeze its two end faces (fingers closed, pressing
    1.5 cm inside), lift it together onto the riser and let go sideways; then the right hand pushes it left off the
    riser's edge, and it tips over into the slot, standing on its end."""
    s = scene()
    box, slot = s["box"], s["slot"]
    ax = flat(box.rot[:, 0])  # the box's 20 cm side
    half = box.extents[0] / 2
    z = box.pos[2] - box.extents[2] / 2 + 0.035
    along = box.yaw(0)
    for arm in ARMS:
        yield call("close", arm=arm)
    faces = {"left": box.pos - ax * (half + 0.03), "right": box.pos + ax * (half + 0.03)}
    if faces["left"][0] > faces["right"][0]:
        ax, faces = -ax, {"left": faces["right"], "right": faces["left"]}
    yaw = {a: along for a in ARMS}  # jaws along the box's axis: a finger's flat outer face presses on the end face
    above = {a: np.asarray(faces[a]) + [0, 0, 0.08] for a in ARMS}
    for a in ARMS:
        above[a][2] = z + 0.08
    yield from carry_both([above, {a: [*faces[a][:2], z] for a in ARMS}], yaw)
    squeeze = {"left": box.pos - ax * (half - 0.03), "right": box.pos + ax * (half - 0.03)}  # 3 cm in: the arms push
    yield from carry_both([{a: [*squeeze[a][:2], z] for a in ARMS}], yaw, tol=0.06)
    # over the riser: the riser is the slot object's +x part (make_objects: x 0.005 .. 0.125, top 10 cm)
    riser = slot.world([0.065, 0.0, 0.0])
    lift = TABLE_Z + 0.10 + 0.03 + (z - (box.pos[2] - box.extents[2] / 2))
    rel = {a: np.asarray(squeeze[a]) - box.pos for a in ARMS}
    over = {a: [riser[0] + rel[a][0], riser[1] + rel[a][1], lift] for a in ARMS}
    yield from carry_both([{a: [*squeeze[a][:2], lift] for a in ARMS}, over,
                           {a: [over[a][0], over[a][1], lift - 0.025] for a in ARMS}], yaw, tol=0.06)
    yield from carry_both([{a: [over[a][0] - rel[a][0] * 0.4, over[a][1] - rel[a][1] * 0.4, lift - 0.025] for a in ARMS}],
                          yaw, tol=0.06)  # out sideways: let go
    yield call("move_both", left=hand([over["left"][0] - rel["left"][0] * 0.4, over["left"][1] - rel["left"][1] * 0.4, lift + 0.08], yaw=yaw["left"]),
               right=hand([over["right"][0] - rel["right"][0] * 0.4, over["right"][1] - rel["right"][1] * 0.4, lift + 0.08], yaw=yaw["right"]))
    yield call("home", arm="left")
    # push left (the slot side, the riser's -x) with the right hand, fingers closed, low on the box's face
    to_slot = -flat(slot.rot[:, 0])
    start = riser - to_slot * (half + 0.04)
    start[2] = TABLE_Z + 0.10 + 0.03
    yield call("move", point(start + [0, 0, 0.08], "over the riser's right"), arm="right", pitch_deg=0.0, yaw_deg=fold(slot.yaw(0) + 90))
    yield call("move", point(start, "beside the box"), arm="right", pitch_deg=0.0, yaw_deg=fold(slot.yaw(0) + 90))
    end = riser + to_slot * 0.13
    end[2] = start[2]
    yield call("move_both", right=hand(end, 0.0, slot.yaw(0) + 90), tol=0.08, max_step_m=0.006)
    yield call("home")


def rc_turn_parcel_into_bay(p):
    """Turn the 42 cm parcel 90 degrees in place: the left hand pushes its left end forward while the right hand pushes
    its right end back, both closed, in 10-degree steps round the parcel's centre."""
    s = scene()
    parcel = s["parcel"]
    c = parcel.pos.copy()
    yaw0 = math.radians(parcel.yaw(0))
    if math.cos(yaw0) < 0:
        yaw0 += math.pi
    r, w = 0.13, parcel.extents[1] / 2 + 0.035  # 8 cm in from each end (the white boxes are 2.3 cm past them)
    z = TABLE_Z + 0.04
    for arm in ARMS:
        yield call("close", arm=arm)

    def contacts(a, press=0.0):
        u, v = np.array([math.cos(a), math.sin(a), 0.0]), np.array([-math.sin(a), math.cos(a), 0.0])  # long axis, its normal
        d = w - press
        return {"left": c - u * r - v * d + [0, 0, z - c[2]], "right": c + u * r + v * d + [0, 0, z - c[2]]}

    def yaws(a):
        d = math.degrees(a)
        return {"left": d, "right": d}  # jaws along the long axis: fingers side by side across the face

    first = contacts(yaw0)
    yield from carry_both([{k: v + [0, 0, 0.08] for k, v in first.items()}, first], yaws(yaw0), step=None)
    for k in range(1, 10):
        a = yaw0 - math.radians(10 * k)  # clockwise seen from above: the left end goes forward
        yield from carry_both([contacts(a, press=0.015)], yaws(a), tol=0.06, step=0.005)
    yield call("nudge", None, arm="left", dz=0.08)
    yield call("nudge", None, arm="right", dz=0.08)
    yield call("home")


def rc_bin_from_lane(p):
    """The left hand pinches the near wall of the next bin up the lane and holds it; the right hand takes the red bin by
    its near wall, lifts it over the rails and puts it in the tote; then the left hand lets go and the bins roll down."""
    s = scene()
    red, nxt, tote, lane = s["red_bin"], s["bin_1"], s["tote"], s["lane"]
    near = -flat(lane.rot[:, 1])  # towards the robot
    along = lane.yaw(0)

    def wall(b: Obj, dx: float):
        """The near wall's top, dx along the lane (the two hands 16 cm apart: their wrists do not touch)."""
        q = b.pos + near * (b.extents[1] / 2 - 0.006) + flat(lane.rot[:, 0]) * dx
        q[2] = b.pos[2] + b.extents[2] / 2 - 0.012
        return q

    yield from pinch("left", wall(nxt, -0.03), along + 90.0)
    out = yield from pinch("right", wall(red, 0.03), along + 90.0)
    log.info("red bin: %s", out.note)
    grip = wall(red, 0.03)
    yield call("move", point(grip + [0, 0, 0.10], "red bin up"), arm="right")
    dest = tote.pos + near * (red.extents[1] / 2 - 0.006)  # the grip over the spot that puts the bin's middle there
    yield call("move", point([dest[0], dest[1], grip[2] + 0.10], "over the tote"), arm="right")
    yield call("move", point([dest[0], dest[1], TABLE_Z + 0.006 + (grip[2] - (red.pos[2] - red.extents[2] / 2)) + 0.01],
                             "down in the tote"), arm="right")
    yield call("open", arm="right")
    yield call("home", arm="right")
    yield call("open", arm="left")
    yield call("home")


def rc_spool_into_saddles(p):
    """The drum is too fat to grip and the spool tips about one hand: each hand pinches one axle end; both lift it over
    the posts, line the axle up with the notches and lower it into them."""
    s = scene()
    spool, sad = s["spool"], s["saddles"]
    ax = flat(spool.rot[:, 0])
    ends = {"left": spool.pos - ax * 0.15, "right": spool.pos + ax * 0.15}
    if ends["left"][0] > ends["right"][0]:
        ax, ends = -ax, {"left": ends["right"], "right": ends["left"]}
    grip = {a: ends[a] for a in ARMS}  # axle centre height: the spool's centre
    across = {a: spool.yaw(0) + 90.0 for a in ARMS}
    yield from pinch_both(p, grip, across)
    sx = flat(sad.rot[:, 0])
    if sx[0] < 0:
        sx = -sx
    seat = sad.pos.copy()
    seat[2] = TABLE_Z + 0.12 + 0.0125  # the notch bottoms (make_objects: 12 cm up) carry the axle
    target = {"left": seat - sx * 0.15, "right": seat + sx * 0.15}
    yaw_t = {a: math.degrees(math.atan2(sx[1], sx[0])) + 90.0 for a in ARMS}
    high = 0.175 + TABLE_Z  # the axle 1 cm over the posts (15 cm): top-down reach shrinks with height
    yield from carry_both([{a: [*grip[a][:2], high] for a in ARMS}], across)
    yield from carry_both([{a: [*target[a][:2], high] for a in ARMS}], yaw_t)
    yield from carry_both([{a: [*target[a][:2], seat[2] + 0.006] for a in ARMS}], yaw_t)
    yield from release_both()


def rc_mug_into_drawer(p):
    """The drawer springs shut: the left hand pulls it open by its handle and holds it, the right hand puts the mug in,
    then the left lets go and the drawer closes with the mug inside."""
    s = scene()
    cab, mug = s["cabinet"], s["mug"]
    # make_objects: the handle bar 10 cm up, 3.5 cm in front of the drawer's front (object frame y -0.151, after the
    # bbox shift of +0.0285); pulled open 15 cm along -y of the cabinet
    out_dir = -flat(cab.rot[:, 1])
    bar = cab.world([-0.03, -0.1795 + 0.0285, 0.10 - 0.082])  # left of the bar's middle: the mug goes in on the right
    yield from pinch("left", bar + [0, 0, 0.012], cab.yaw(0) + 90.0)
    pulled = bar + out_dir * 0.15
    yield call("move_both", left=hand(pulled + [0, 0, 0.012], 0.0, cab.yaw(0) + 90.0), tol=0.04, max_step_m=0.005)
    e = mug.extents
    cc = mug.meta["active"]["place"]["up"]["contact_circle"]["center"]
    body = mug.world([cc[0], cc[1], 0.0])
    target = Target((body[0], body[1], TABLE_Z + e[2] / 2), "mug", (e[1], e[1], e[2]), mug.yaw(0), "right")
    out = yield call("grasp_top", target, depth=0.03, lift=0.12)
    if not out:
        log.warning("mug: %s", out.note)
        return
    inside = cab.world([0.06, -0.19 + 0.0285, 0.0])  # the drawer's open part, 15 cm out, right of the holding hand
    yield call("place", Target((inside[0], inside[1], TABLE_Z + 0.025), "drawer", arm="right"))
    yield call("home", arm="right")
    yield call("open", arm="left")
    yield call("move_both", left=hand(pulled + [0, 0, 0.10], 0.0, cab.yaw(0) + 90.0), tol=0.05)
    yield call("home")


def rc_cube_into_lidded_bin(p):
    """The lid falls shut: the left hand, fingers closed and level, lifts it from under its front tab round the hinge to
    50 degrees and holds it; the right hand drops the cube in; the left hand lowers the lid again."""
    s = scene()
    b, cube = s["bin"], s["cube"]
    f = flat(b.rot[:, 1])  # front to back
    # make_objects (object frame, bbox shift y +0.0125, z -0.066): hinge at y 0.09, z 0.12; tab front at y -0.115
    hinge = b.world([0.0, 0.09 + 0.0125, 0.12 - 0.066])
    radius = 0.19
    move = dict(arm="left", pitch_deg=90.0, yaw_deg=heading(f), spin_deg=90.0)

    def on_arc(deg: float) -> np.ndarray:
        a = math.radians(deg)
        return hinge - f * radius * math.cos(a) + [0, 0, radius * math.sin(a) - 0.012]  # just under the tab

    yield call("close", arm="left")
    start = on_arc(0.0) - f * 0.06
    yield call("move", point(start + [0, 0, 0.0], "in front of the tab"), **move)
    for deg in range(0, 51, 10):
        yield call("move", point(on_arc(deg), f"lid {deg} deg"), **move)
    target = Target(tuple(cube.pos), "cube", tuple(cube.extents), cube.yaw(0), "right")
    out = yield call("grasp_top", target, lift=0.10)
    if not out:
        log.warning("cube: %s", out.note)
    inside = b.world([0.0, 0.0125, 0.0])  # the bin's middle (bbox shift y +0.0125); the lid's underside is 22 cm up there
    yield call("move", point([inside[0], inside[1], TABLE_Z + 0.165], "over the bin"), arm="right")
    yield call("open", arm="right")  # drop it in
    yield call("home", arm="right")
    for deg in range(40, -1, -10):
        yield call("move", point(on_arc(deg), f"lid {deg} deg"), **move)
    yield call("move", point(on_arc(0.0) - f * 0.08, "out from under the tab"), **move)
    yield call("home")


def rc_jar_off_board(p):
    """The jar is out of reach on the board's far end: the right hand pulls the board in by its near end, then the
    left hand takes the jar and puts it in the tray."""
    s = scene()
    board, jar, tray = s["board"], s["jar"], s["tray"]
    ax = flat(board.rot[:, 1])  # along the board, away from the robot
    near_end = board.pos - ax * (board.extents[1] / 2 - 0.04)
    near_end[2] = board.pos[2] + 0.005
    yield from pinch("right", near_end, board.yaw(1) + 90.0)
    yield call("move_both", right=hand(near_end - ax * 0.30, 0.0, board.yaw(1) + 90.0), tol=0.04, max_step_m=0.004)  # slowly
    yield call("open", arm="right")
    yield call("home", arm="right")
    jar_now = jar.pos - ax * 0.30
    target = Target((jar_now[0], jar_now[1], jar_now[2]), "jar", tuple(jar.extents), None, "left")
    out = yield call("grasp_top", target, lift=0.10)
    if not out:
        log.warning("jar: %s", out.note)
    yield call("place", Target((tray.pos[0], tray.pos[1], TABLE_Z + 0.006), "tray", arm="left"))
    yield call("home")


def rc_refit_drawer(p):
    """Both hands pinch the drawer's side walls (with their lips) at mid depth, lift it to just over the rails and
    carry it square into the cabinet's opening, about half way; they let go there (the lips on the rails), and the
    right hand, closed, pushes the front panel the rest of the way in."""
    s = scene()
    d, cab = s["drawer"], s["cabinet"]
    fwd = flat(cab.rot[:, 1])  # into the cabinet
    side = flat(cab.rot[:, 0])
    # make_objects: drawer lips at x +-0.138 (object frame), walls 8 cm tall; centre 1.5 cm behind its parts' origin
    wall_x, mid_y = 0.132, 0.015
    yaw_along = cab.yaw(1) + 90.0  # jaws across the wall (along the cabinet's x)
    dside = flat(d.rot[:, 0])
    if dside @ side < 0:
        dside = -dside
    top = d.pos[2] - d.extents[2] / 2 + 0.08
    grip = {a: d.pos + dside * (wall_x * (-1 if a == "left" else 1)) + flat(d.rot[:, 1]) * mid_y for a in ARMS}
    for a in ARMS:
        grip[a][2] = top - 0.02
    yield from pinch_both(p, grip, {a: d.yaw(1) + 90.0 for a in ARMS})
    lift_z = top - 0.02 + 0.035  # drawer bottom 3.5 cm up: lips 1.5 cm over the 9 cm rails
    closed = cab.pos + fwd * (-0.029) + [0, 0, 0]
    half_in = closed - fwd * 0.16  # the hands stay 5 cm out of the cabinet: the wrists clear its top
    out_front = closed - fwd * 0.30
    rel = {a: (side * (wall_x * (-1 if a == "left" else 1)) + fwd * mid_y) for a in ARMS}
    yaw = {a: yaw_along for a in ARMS}
    yield from carry_both([{a: [*grip[a][:2], lift_z] for a in ARMS},
                           {a: [*(out_front + rel[a])[:2], lift_z] for a in ARMS},
                           {a: [*(half_in + rel[a])[:2], lift_z] for a in ARMS},
                           {a: [*(half_in + rel[a])[:2], lift_z - 0.012] for a in ARMS}], yaw)
    yield call("open")
    yield call("move_both", left=hand([*(half_in + rel["left"])[:2], lift_z + 0.06], yaw=yaw_along),
               right=hand([*(half_in + rel["right"])[:2], lift_z + 0.06], yaw=yaw_along))
    yield call("home", arm="left")
    # push the front panel the rest of the way in: fingers closed and down, low on the panel (the wrist, 15 cm up, passes
    # under the cabinet's top board)
    panel = half_in - fwd * (0.103 + 0.06)  # 6 cm in front of the panel's face if the drawer stayed where it was let go
    panel[2] = TABLE_Z + 0.035
    shut = closed - fwd * (0.103 + 0.012)
    shut[2] = panel[2]
    yield call("close", arm="right")
    yield call("move", point(panel + [0, 0, 0.10], "over the panel"), arm="right", pitch_deg=0.0, yaw_deg=fold(cab.yaw(0)))
    yield call("move", point(panel, "in front of the panel"), arm="right", pitch_deg=0.0, yaw_deg=fold(cab.yaw(0)))
    yield call("move_both", right=hand(shut, 0.0, cab.yaw(0)), tol=0.05, max_step_m=0.006)
    yield call("home")


def rc_carry_open_carton(p):
    """The carton's bottom is two loose flaps; its front faces the right arm. The left hand pinches the front rim (a tilted
    grasp: it is far) and tips the carton back about its back bottom edge, so the flaps stay shut on the table; the
    right hand, level, fingers closed into a flat paddle, slides in under the raised front across the flaps' seam; both
    carry it, levelled, onto the platform beside it, the front overhanging the platform where the paddle is; the paddle
    comes out, the left hand lets go."""
    from robodojo_agent.frames import approach_yaw_deg

    s = scene()
    c, plat = s["carton"], s["platform"]
    fwd = flat(c.rot[:, 1])  # front (towards the right arm) is -fwd
    h, half = c.extents[2], c.extents[1] / 2
    bottom = c.pos[2] - h / 2
    back_edge = c.pos + fwd * half
    back_edge[2] = bottom
    rim = c.pos - fwd * (half - 0.002)
    rim[2] = bottom + h - 0.015
    tilt = math.radians(30)

    def tipped(pt, a):  # pt turned by a about the back bottom edge (front up)
        d = pt - back_edge
        along, up = -(d @ fwd), d[2]
        return back_edge - fwd * (along * math.cos(a) - up * math.sin(a)) + [0, 0, along * math.sin(a) + up * math.cos(a)]

    lyaw = approach_yaw_deg("left", rim)
    left = dict(pitch_deg=40.0, yaw_deg=lyaw)  # tilted away from the left base: the jaws close across that, ~along fwd
    yield call("open", arm="left")
    yield call("move", point(rim + [0, 0, 0.08], "over the front rim"), arm="left", **left)
    yield call("move", point(rim, "front rim"), arm="left", **left)
    out = yield call("close", arm="left")
    log.info("rim: %s", out.note)
    for k in range(1, 6):
        yield call("move_both", left=hand(tipped(rim, tilt * k / 5), 40.0, lyaw), tol=0.03, max_step_m=0.006)
    # the paddle: level, pointing along fwd (straight out of the right arm), the closed jaws side by side (flat)
    yield call("close", arm="right")
    ryaw = heading(fwd)
    front_bottom = tipped(c.pos - fwd * half + [0, 0, -h / 2], tilt)
    under = front_bottom + fwd * 0.05 + [0, 0, -0.012 - 0.05 * math.tan(tilt)]
    yield call("move_both", right=hand(under - fwd * 0.06 + [0, 0, 0.06], 90.0, ryaw, 0.0), tol=0.03)
    yield call("move_both", right=hand(under - fwd * 0.06, 90.0, ryaw, 0.0), tol=0.03)  # a level hand cannot fold in closer
    yield call("move_both", right=hand(under, 90.0, ryaw, 0.0), tol=0.04, max_step_m=0.005)
    # carry: up 6 cm, level it, over the platform (front 4 cm over its front edge, as laid out), down onto it
    rim_t = tipped(rim, tilt)
    lift = np.array([0, 0, 0.06 + 0.02])
    yield call("move_both", left=hand(rim_t + lift, 40.0, lyaw), right=hand(under + lift, 90.0, ryaw, 0.0),
               tol=0.04, max_step_m=0.005)
    level_rim = rim + lift + [0, 0, 0.012]  # the rim back down to level, the front resting on the paddle
    yield call("move_both", left=hand(level_rim, 40.0, lyaw), right=hand(under + lift, 90.0, ryaw, 0.0),
               tol=0.04, max_step_m=0.005)
    shift = plat.pos - fwd * 0.044 - c.pos  # the carton's centre over the platform, its front over the paddle
    shift[2] = 0.0
    yield call("move_both", left=hand(level_rim + shift, 40.0, lyaw), right=hand(under + lift + shift, 90.0, ryaw, 0.0),
               tol=0.04, max_step_m=0.005)
    down = np.array([0, 0, -lift[2] + 0.06 + 0.004])  # the carton's bottom on the 6 cm platform
    yield call("move_both", left=hand(level_rim + shift + down, 40.0, lyaw), right=hand(under + lift + shift + down, 90.0, ryaw, 0.0),
               tol=0.05, max_step_m=0.004)
    yield call("move_both", right=hand(under + lift + shift + down - fwd * 0.14, 90.0, ryaw, 0.0), tol=0.05, max_step_m=0.005)
    yield call("open", arm="left")
    yield call("nudge", None, arm="left", dz=0.08)
    yield call("home")


RC_SCRIPTS = {f.__name__: f for f in (
    rc_plank_unload, rc_carry_box_stack, rc_box_on_edge_in_slot, rc_turn_parcel_into_bay, rc_bin_from_lane,
    rc_spool_into_saddles, rc_mug_into_drawer, rc_cube_into_lidded_bin, rc_jar_off_board, rc_refit_drawer,
    rc_carry_open_carton)}