"""依頼 351 の 4: 下見の絵 (読むだけ. replan_351.json から). preview_351.png 左: 50 本の置く所と向き (机を上から見た絵. アームの座標 mm. 根もとが原点. 右上の箱の向きは見ない). 青い四角 = 決めた向きのまま, 赤い四角 = 決まり 4 で向きを置き換えた本 (灰色の点線 = 決めた向き). 数字 = 何本目. 四角の中の印: ○ = 決まり 2 (別の面の組). 右 (上から): 本ごとの 爪の傾き・爪の面の傾き・つかむ所の手首の回転. 棒の色 = 使った決まり (灰 = 1・5 だけ / 橙 = 3 爪を立てた / 紫 = 2 別の面の組 / 赤 = 4 向きを置き換えた. 重なるときは 4 → 2 → 3 の順). × = 依頼 347 の式の爪の傾きのまま (爪を立てない) の値. 点線 = 許し ±8°・手首の回転の範囲 −67〜58°. python3 so101/check_351/preview_351.py [--replan replan_351.json] (python3. matplotlib. 字は英字) """ import argparse import json import math from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt # noqa: E402 from matplotlib.patches import Polygon # noqa: E402 HERE = Path(__file__).resolve().parent BLOCK = 22.0 COL = {"yaw_arm": "tab:red", "face": "tab:purple", "tilt": "tab:orange", "none": "0.6"} def square(c, yaw): a = math.radians(yaw) out = [] for sx, sy in ((-1, -1), (1, -1), (1, 1), (-1, 1)): x, y = sx * BLOCK / 2, sy * BLOCK / 2 out.append((c[0] + x * math.cos(a) - y * math.sin(a), c[1] + x * math.sin(a) + y * math.cos(a))) return out def kind(e): u = e["rules_used"] return next((k for k in ("yaw_arm", "face", "tilt") if u[k]), "none") def main(): ap = argparse.ArgumentParser() ap.add_argument("--replan", default=str(HERE / "replan_351.json")) ap.add_argument("--out", default=str(HERE / "preview_351.png")) a = ap.parse_args() rep = json.loads(Path(a.replan).read_text()) eps = rep["episodes"] fig = plt.figure(figsize=(20, 10)) ax = fig.add_axes([0.03, 0.06, 0.40, 0.86]) for e in eps: phi, rad = math.radians(e["arm_dir_deg"]), e["arm_r_mm"] c = (rad * math.cos(phi), rad * math.sin(phi)) arm = e["yaw_frame"] == "arm" if arm: ax.add_patch(Polygon(square(c, e["yaw_orig"]), closed=True, fill=False, ec="0.5", lw=1, ls=":")) ax.plot([0, c[0]], [0, c[1]], c="tab:red", lw=0.5, alpha=0.4) ax.add_patch(Polygon(square(c, e["yaw"]), closed=True, fill=False, ec="tab:red" if arm else "tab:blue", lw=2 if arm else 1.3)) if e["rules_used"]["face"]: ax.plot(*c, "o", mfc="none", mec="tab:purple", ms=9) ax.text(c[0], c[1], str(e["index"] + 1), fontsize=7, ha="center", va="center") ax.plot(0, 0, "ks") ax.text(0, -12, "base", ha="center", fontsize=8) ax.set_aspect("equal") ax.autoscale_view() ax.grid(alpha=0.3) ax.set_xlabel("robot x (mm)") ax.set_ylabel("robot y (mm)") c = rep["counts"] ax.set_title(f"50 places / yaw. blue = planned yaw kept, RED = rule 4 yaw replaced ({c['yaw_arm']}; dotted = original), " f"purple o = rule 2 face pair ({c['face']})", fontsize=9) x = [e["index"] + 1 for e in eps] cols = [COL[kind(e)] for e in eps] panels = [("tilt_deg", "tilt_347_deg", "jaw tilt from vertical (deg)", None), ("jaw_elev_deg", "elev_347_deg", "jaw plane tilt vs table (deg)", (-8, 8)), ("roll_grasp_deg", None, "wrist roll at grasp (deg)", (-67, 58))] for k, (key, ref, lab, band) in enumerate(panels): a2 = fig.add_axes([0.48, 0.70 - k * 0.31, 0.50, 0.25]) a2.bar(x, [e[key] for e in eps], color=cols) if ref: a2.plot(x, [e[ref] if e[ref] is not None else float("nan") for e in eps], "kx", ms=5, label="req 347 tilt (not stood up)") a2.legend(fontsize=7, loc="upper right") if band: for b in band: a2.axhline(b, c="k", ls=":", lw=1) a2.axhline(0, c="0.3", lw=0.6) a2.set_xlim(0.3, 50.7) a2.set_xticks(range(1, 51, 1 if k == 2 else 5)) a2.tick_params(labelsize=7) a2.set_ylabel(lab, fontsize=9) a2.grid(alpha=0.3, axis="y") fig.text(0.48, 0.965, f"rule_351: grey = rules 1,5 only ({c['none']}) orange = rule 3 tilt stood up ({c['tilt']}) " f"purple = rule 2 other face pair ({c['face']}) red = rule 4 yaw replaced ({c['yaw_arm']})", fontsize=10) fig.savefig(a.out, dpi=80) if __name__ == "__main__": main()