"""床に置いた段ボールの島(2×2・ふたを開けた箱)のまわりを回って(手前・左・右の3方向から寄って)、箱の吸着式の道具で取り、荷台のコンテナへ入れる動きを作る。 寄る向きごとに、届く箱から近い物を3つずつ選ぶ。道具の本体(100mm 角)が箱の壁に当たる壁ぎわの物は選ばない。 荷台のコンテナがいっぱい(大4・ほか9)になったら、降ろしたことにして空にする(降ろす動きは作っていない)。 当たりの見方は tour_poses.py と同じ(腕・道具は球の列、持っている物は点の格子、置き場所の物は箱で見る)。 使い方: python3 pallet_poses.py (page/arm_poses.json に pallet を足す。出力は out_pallet_poses.txt)""" import sys, json, math, numpy as np import tour_common as TC src = open('tour_poses.py', encoding='utf-8').read().split("TOUR = {}; PROBLEMS = []")[0] sys.argv = ['tour_poses.py']; ns = {'__name__': 'tp'}; exec(compile(src, 'tp', 'exec'), ns) fk, solve, to_u, B = ns['fk'], ns['solve'], ns['to_u'], ns['B'] pose_list, chain, first_chain, route, hit, set_j6, dur = (ns[k] for k in ('pose_list', 'chain', 'first_chain', 'route', 'hit', 'set_j6', 'dur')) DOWN, XF, XR, BASE_H, U = ns['DOWN'], ns['XF'], ns['XR'], 0.698, ns['U'] P = json.load(open('page/arm_poses.json')); FOLD = np.array(P['fold']) LOG = [] def say(*a): s = ' '.join(str(x) for x in a); print(s, flush=True); LOG.append(s) ALL = TC.pallet_top_items(); TAKEN = set() FACES = ['front', 'west', 'east'] TOPZ = TC.box_top() # 箱のふち def pallet_obst(face, cur=None): """パレットの上の物(アームの座標)。cur は今つかむ物(その所だけ、中身の板に穴を空ける)""" o = []; wt = TC.WT / 1000 for i in range(TC.NI): for j in range(TC.NJ): f0, f1, r0, r1 = TC.box_rect_arm(face, i, j) for nm, rect in (('箱の壁', (f0, f0 + wt, r0, r1)), ('箱の壁', (f1 - wt, f1, r0, r1)), ('箱の壁', (f0, f1, r0, r0 + wt)), ('箱の壁', (f0, f1, r1 - wt, r1))): o.append((nm, rect + (U(TC.BOX_BASE), U(TOPZ)))) k = TC.PAL_TOP[i][j]; h = TC.SIZES[k][2] / 1000; top = TC.box_floor() + TC.LAYERS[k] * (h + 0.001) - 0.015 # 中の物の上の面より 15mm 下までを板とみる g0, g1, s0, s1 = f0 + wt, f1 - wt, r0 + wt, r1 - wt; lo, hi = U(TC.box_floor() - 0.005), U(top) if cur is not None and cur['box'] == (i, j): w, d, _ = TC.SIZES[k]; cf, cr = TC.to_arm(face, cur['x'], cur['z']); m = 0.008 af, ar = (d / 2000 + m, w / 2000 + m) if face == 'front' else (w / 2000 + m, d / 2000 + m) # 物の w は倉庫の X の向き for rect in ((g0, cf - af, s0, s1), (cf + af, g1, s0, s1), (cf - af, cf + af, s0, cr - ar), (cf - af, cf + af, cr + ar, s1)): if rect[1] > rect[0] and rect[3] > rect[2]: o.append(('箱の中のほかの物', rect + (lo, hi))) else: o.append(('箱の中のほかの物', (g0, g1, s0, s1, lo, hi))) return o def wall_ok(it): # 道具の本体(100mm 角)が箱の壁に当たらない所か return abs(it['dx']) <= (TC.CW / 2 - TC.WT) / 1000 - 0.050 and abs(it['dz']) <= (TC.CD / 2 - TC.WT) / 1000 - 0.050 L_SLOTS = [[627.5, -77.5], [627.5, 77.5], [542.5, -77.5], [542.5, 77.5]] O_SLOTS = [[b, r] for b in (445.0, 345.0, 245.0) for r in (-100.0, 0.0, 100.0)] pools = {'l': [], 'o': []}; inbox = [] def reset_pools(): pools['l'] = list(range(4)); pools['o'] = list(range(9)); inbox.clear() reset_pools() def slot_box(kind, si): # コンテナの中の物(アームの座標) w, d, h = TC.SIZES[kind]; b, r = (L_SLOTS if kind == 'l' else O_SLOTS)[si] return ('コンテナの中の物', B(b - w / 2, b + w / 2, r - d / 2, r + d / 2, 14, 14 + h)) PER_BOX = 3; OUT = []; PROBLEMS = [] for face in FACES: say(f'■ {face}({dict(front="手前", west="左", east="右")[face]}から寄る)') cands = [] for it in ALL: if id(it) in TAKEN or not wall_ok(it): continue f, r = TC.to_arm(face, it['x'], it['z']) if f > 0.70: continue cands.append((f + 0.3 * abs(r), f, r, it)) cands.sort(key=lambda c: c[0]); per = {}; tries = {} chosen = [(f, r, it) for _, f, r, it in cands] frames = []; prev = FOLD def add(q, ev=None): frames.append([np.array(q).tolist(), dur(frames[-1][0] if frames else FOLD, q), ev]) def go(a, b, it, extra, label, t0=0.0): vs, note = route(a, b, it, extra, label, t0) if note: say(' ' + note) if vs is None: PROBLEMS.append(f'{face}:{note}'); vs = [] for v in vs: add(v) add(b) for f, r, it in chosen: bx_ = it['box'] if per.get(bx_, 0) >= PER_BOX or tries.get(bx_, 0) >= 6: continue # 1箱で取るのは3つまで。試すのは6つまで tries[bx_] = tries.get(bx_, 0) + 1 k = it['k']; w, d, h = TC.SIZES[k] pit = dict(k=k, grasp='top', f=f, r=r, z0=it['z0']) slot_kind = 'l' if k == 'l' else 'o' if not pools[slot_kind]: add(prev, 'unload'); reset_pools(); say(' コンテナがいっぱい → 降ろした(降ろす動きは省いた)') si = pools[slot_kind][0] ex = pallet_obst(face, it) + [slot_box(kk, ss) for kk, ss in inbox] tip = (f, r, it['z0'] + h / 1000); pre_tip = (f, r, TOPZ + 0.02 + h / 1000) xdir = XR if face == 'front' else XF # 道具の X(物の w の向き)を、倉庫の X に合わせる(手前からは左右、横からは前後) qpre, down, e1 = first_chain(pose_list(pre_tip, DOWN, xdir, ex, None, prev, front=True), pre_tip, tip, DOWN, xdir, ex, None, 4) if down is None: say(f' {k}(箱 {it["box"]}):下りる {e1}'); continue up, e2 = chain(down[-1], tip, pre_tip, DOWN, xdir, ex, pit, 4) if up is None: say(f' {k}(箱 {it["box"]}):持ち上げ {e2}'); continue hi_tip = (f, r, max(pre_tip[2], 0.398 + 0.221 + 0.05 + h / 1000)) # 持った物の底が、荷台のコンテナのふちより 50mm 上になるまで、まっすぐ上げる if hi_tip[2] > pre_tip[2] + 1e-3: up2, e3 = chain(up[-1], pre_tip, hi_tip, DOWN, xdir, ex, pit, 4) if up2 is None: say(f' {k}(箱 {it["box"]}):高く上げる {e3}'); continue up = up + up2 b, rr = (L_SLOTS if k == 'l' else O_SLOTS)[si] ptip = (0.065 - b / 1000, rr / 1000, 0.398 + 0.017 + h / 1000) qpp = pd = None for up_h in (0.32, 0.25, 0.18, 0.12): ppre = (ptip[0], ptip[1], ptip[2] + up_h) qpp, pd, e4 = first_chain(pose_list(ppre, DOWN, XF, ex, pit, up[-1]), ppre, ptip, DOWN, XF, ex, pit, 4) if pd is not None: break if pd is None: say(f' {k}(箱 {it["box"]}):コンテナに置く姿勢が見つからない'); continue vs, note = route(up[-1], qpp, pit, ex, '持ったまま → コンテナの上') if vs is None: say(f' {k}(箱 {it["box"]}):{note}'); continue # ここまで来たら、この物は取れる go(prev, qpre, None, ex, '前の姿勢 → 箱の上') for q in down: add(q) idx = ALL.index(it); frames[-1][2] = f'pgrab:{idx}' for q in up: add(q) if note: say(' ' + note) for v in vs: add(v) add(qpp) for q in pd: add(q) frames[-1][2] = f'pput:{idx}:{k}:{si}' add(qpp); prev = qpp pools[slot_kind].pop(0); inbox.append((k, si)); TAKEN.add(id(it)); per[bx_] = per.get(bx_, 0) + 1 say(f' 取れる:{k}(箱 {it["box"]}・前 {f*1000:.0f}・右 {r*1000:.0f})→ コンテナの {"大" if k == "l" else "ほか"}の{si+1}番') go(prev, FOLD, None, pallet_obst(face) + [slot_box(kk, ss) for kk, ss in inbox], '最後 → 畳む') frames[-1][2] = 'pdone' n = sum(1 for fr in frames if fr[2] and fr[2].startswith('pgrab')) say(f' {face}:{n}個・動きの区切り {len(frames)}・約{sum(fr[1] for fr in frames):.0f}秒') OUT.append(dict(face=face, pose=TC.face_pose(face), frames=frames)) say('■ 結果:', '粗い当たりの範囲で、選んだ物を続けて取れる' if not PROBLEMS else 'うまくいかない所:' + ' / '.join(PROBLEMS)) P['pallet'] = dict(faces=OUT, items=ALL, L_SLOTS=L_SLOTS, O_SLOTS=O_SLOTS) json.dump(P, open('page/arm_poses.json', 'w'), indent=1) open('out_pallet_poses.txt', 'w').write('\n'.join(LOG) + '\n')