# -*- coding: utf-8 -*- """Sinh hinh hoc cho tai lieu dock_pattern_detector. Moi hinh ve tu SO THAT.""" import math, os import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.patches import Arc, Rectangle, Circle, FancyArrowPatch plt.rcParams['font.family'] = 'DejaVu Sans' plt.rcParams['font.size'] = 9 plt.rcParams['svg.fonttype'] = 'path' # nhung chu thanh duong -> khong phu thuoc font may in OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fig') INK, ACC, WARN, MUT = '#1b2733', '#1f6feb', '#c9401a', '#7d8996' GRID = '#dfe4ea' def save(fig, name): fig.savefig(os.path.join(OUT, name), format='svg', bbox_inches='tight', transparent=True) plt.close(fig) print(' ', name) def frame(ax, xlim, ylim, title=None): ax.set_xlim(*xlim); ax.set_ylim(*ylim); ax.set_aspect('equal') ax.grid(True, color=GRID, lw=0.5, zorder=0) for s in ('top', 'right'): ax.spines[s].set_visible(False) for s in ('left', 'bottom'): ax.spines[s].set_color(MUT) ax.tick_params(colors=MUT, labelsize=7) if title: ax.set_title(title, color=INK, fontsize=10, pad=8) def sensor(ax, x=0, y=0, label='cảm biến\n(gốc frame scan)'): ax.plot([x], [y], marker='o', ms=7, color=INK, zorder=5) ax.annotate(label, (x, y), textcoords='offset points', xytext=(6, -22), fontsize=7.5, color=INK) def arrow(ax, p, q, color=ACC, lw=1.4, style='-|>', ms=9): ax.add_patch(FancyArrowPatch(p, q, arrowstyle=style, mutation_scale=ms, color=color, lw=lw, zorder=6, shrinkA=0, shrinkB=0)) def dim(ax, p, q, text, off=0.0, color=WARN, fs=8, above=True): """Duong kich thuoc hai dau mui ten.""" (x1, y1), (x2, y2) = p, q dx, dy = x2 - x1, y2 - y1 L = math.hypot(dx, dy) or 1.0 nx, ny = -dy / L * off, dx / L * off a, b = (x1 + nx, y1 + ny), (x2 + nx, y2 + ny) ax.add_patch(FancyArrowPatch(a, b, arrowstyle='<|-|>', mutation_scale=7, color=color, lw=1.0, zorder=6, shrinkA=0, shrinkB=0)) mx, my = (a[0] + b[0]) / 2, (a[1] + b[1]) / 2 ax.annotate(text, (mx, my), textcoords='offset points', xytext=(0, 5 if above else -11), ha='center', fontsize=fs, color=color, zorder=7) # ================================================================================================= # CHỮ V # ================================================================================================= AP, VANG, WL = 2.0, math.pi / 2, 0.30 # đỉnh ở 2 m, mở 90 độ, cánh 0.30 m HALF = VANG / 2 TIPX, TIPY = AP - WL * math.cos(HALF), WL * math.sin(HALF) def v_plate(ax, apex=AP, vang=VANG, wl=WL, color=INK, lw=2.6, z=4): h = vang / 2 tx, ty = apex - wl * math.cos(h), wl * math.sin(h) ax.plot([apex, tx], [0, ty], color=color, lw=lw, zorder=z, solid_capstyle='round') ax.plot([apex, tx], [0, -ty], color=color, lw=lw, zorder=z, solid_capstyle='round') return (tx, ty), (tx, -ty) def fig_v_geometry(): fig, ax = plt.subplots(figsize=(7.4, 4.3)) frame(ax, (-0.15, 2.45), (-0.75, 0.75)) sensor(ax) t1, t2 = v_plate(ax) ax.plot([AP], [0], marker='o', ms=6, color=WARN, zorder=7) ax.annotate(u'đỉnh (apex)\ngiao điểm HAI ĐƯỜNG đã khớp,\nkhông phải trung điểm hai đầu mút', (AP, 0), textcoords='offset points', xytext=(10, 26), fontsize=7.5, color=WARN) # goc dinh a1 = math.degrees(math.atan2(t1[1], t1[0] - AP)) a2 = math.degrees(math.atan2(t2[1], t2[0] - AP)) ax.add_patch(Arc((AP, 0), 0.30, 0.30, theta1=a1, theta2=a2 + 360, color=ACC, lw=1.3, zorder=6)) ax.annotate(u'vertex_angle\n= 1.5708 rad (90°)', (AP - 0.17, 0), textcoords='offset points', xytext=(-96, -6), fontsize=8, color=ACC) dim(ax, (AP, 0), t1, u'wing_length = 0.30 m', off=0.075) # phan giac -> yaw arrow(ax, (AP, 0), (AP - 0.55, 0), color='#0d8050', lw=1.8) ax.annotate(u'yaw = phân giác, hướng RA phía cảm biến', (AP - 0.55, 0), textcoords='offset points', xytext=(-16, 12), fontsize=8, color='#0d8050', ha='right') # offset -> dich gx = AP - 0.95 ax.plot([gx], [0], marker='*', ms=15, color='#7b3fbf', zorder=7) dim(ax, (AP, 0), (gx, 0), u'offset_x = 0.95 m', off=-0.30, color='#7b3fbf', above=False) ax.annotate(u'pose ĐÍCH ghi ra `dock_target`\n(pose của base_link, KHÔNG phải mép robot)', (gx, 0), textcoords='offset points', xytext=(-6, -50), fontsize=7.5, color='#7b3fbf', ha='center') save(fig, 'v_geometry.svg') def fig_v_gates(): fig, axs = plt.subplots(1, 3, figsize=(11.6, 3.5)) # (a) vertex_gap ax = axs[0]; frame(ax, (-0.1, 2.6), (-0.95, 0.95), u'(a) vertex_gap — hai tường cắt nhau ở XA') ax.plot([1.2, 2.4], [0.8, -0.4], color=MUT, lw=2.4) ax.plot([1.2, 2.4], [-0.8, 0.4], color=MUT, lw=2.4) ax.plot([1.8], [0.2], marker='x', ms=9, color=WARN, mew=2, zorder=7) ax.plot([1.8], [0.2], marker='o', ms=26, mfc='none', mec=WARN, lw=1.0, ls=':', zorder=6) ax.annotate(u'giao điểm có,\nnhưng KHÔNG đầu đoạn nào\nkết thúc tại đó → LOẠI', (1.8, 0.2), textcoords='offset points', xytext=(-4, 30), fontsize=7.5, color=WARN, ha='center') ax.annotate(u'Mọi cặp đường không song song\nđều có giao điểm. Chỉ chữ V thật\nmới có hai đoạn KẾT THÚC ở đó.', (0.05, -0.88), fontsize=7.5, color=INK) # (b) angle_tolerance ax = axs[1]; frame(ax, (-0.1, 2.6), (-0.95, 0.95), u'(b) angle_tolerance — đúng đỉnh, sai góc') v_plate(ax, apex=2.0, vang=math.pi / 2, wl=0.32, color='#b9c2cc', lw=2.0) v_plate(ax, apex=2.0, vang=2.6, wl=0.32, color=WARN, lw=2.6) ax.annotate(u'profile: 90°', (1.62, 0.30), fontsize=7.5, color='#7d8996') ax.annotate(u'đo được: 149°\n→ LOẠI', (1.55, -0.44), fontsize=7.5, color=WARN) ax.annotate(u'Góc đo giữa hai VECTOR CÁNH\nxuất phát từ đỉnh — không phải\ngiữa hai pháp tuyến.', (0.05, -0.88), fontsize=7.5, color=INK) # (c) luat chieu dai bat doi xung ax = axs[2]; frame(ax, (-0.1, 2.6), (-0.95, 0.95), u'(c) chiều dài — luật BẤT ĐỐI XỨNG') apex = 2.0 ax.plot([apex, apex - 0.212], [0, 0.212], color=INK, lw=2.6) ax.plot([apex, apex - 0.212], [0, -0.212], color='#b9c2cc', lw=2.6, ls=(0, (4, 3))) ax.plot([apex, apex - 0.10], [0, -0.10], color=WARN, lw=2.6) for b in (0.03, 0.09, 0.15, 0.21, 0.27): ax.plot([0, apex * 1.12], [0, b * 3.4], color='#e9edf1', lw=0.7, zorder=1) ax.annotate(u'cánh gần: đủ 0.30 m', (apex - 0.30, 0.27), fontsize=7.5, color=INK) ax.annotate(u'cánh xa: chỉ thấy 0.14 m\n(gần song song với tia)', (apex - 0.62, -0.42), fontsize=7.5, color=WARN) ax.annotate(u'Che khuất chỉ làm cánh NGẮN đi.\nLuật: không cánh nào DÀI hơn ngưỡng,\nít nhất MỘT cánh đúng ngưỡng.', (0.05, -0.88), fontsize=7.5, color=INK) save(fig, 'v_gates.svg') def fig_v_tape(): fig, axs = plt.subplots(1, 2, figsize=(11.6, 4.0), gridspec_kw={'width_ratios': [1.15, 1]}) # trai: hinh hoc dai ax = axs[0]; frame(ax, (1.55, 2.35), (-0.36, 0.36), u'Dải phản quang trên tấm chữ V') t1, t2 = v_plate(ax) # dai o giua canh, dai tape_w doc canh TW = 0.12 cs = [] for sy in (1, -1): dx, dy = (TIPX - AP) / WL, sy * TIPY / WL mx, my = AP + dx * WL / 2, dy * WL / 2 ax.plot([mx - dx * TW / 2, mx + dx * TW / 2], [my - dy * TW / 2, my + dy * TW / 2], color='#e8b923', lw=6.0, solid_capstyle='butt', zorder=5) cs.append((mx, my)) ax.plot([c[0] for c in cs], [c[1] for c in cs], color=WARN, lw=1.0, ls=':', zorder=6) dim(ax, cs[0], cs[1], u'tape_spacing = 0.1945 m', off=0.0) dxa, dya = (TIPX - AP) / WL, TIPY / WL dim(ax, (cs[0][0] - dxa * TW / 2, cs[0][1] - dya * TW / 2), (cs[0][0] + dxa * TW / 2, cs[0][1] + dya * TW / 2), u'tape_width = 0.12 m', off=0.055, color='#8a6d00') ax.annotate(u'khoảng cách đo THẲNG giữa hai TÂM dải,\nkhông đi vòng theo cánh, không qua đỉnh', (1.58, -0.31), fontsize=7.5, color=WARN) # phai: bu luong tu hoa ax = axs[1]; frame(ax, (-0.02, 0.20), (-0.062, 0.075), u'Vì sao chiều dài dải luôn bị ĐO HỤT') ax.plot([0.0, 0.16], [0, 0], color='#e8b923', lw=9, solid_capstyle='butt', zorder=3) ax.annotate(u'dải THẬT, 0.12 m quy đổi', (0.055, 0.016), fontsize=7.5, color='#8a6d00') step = 0.16 / 6.0 hits = [0.0 + step * (i + 0.5) for i in range(6)] for h in hits: ax.plot([h], [0], marker='o', ms=6, color=ACC, zorder=6) dim(ax, (hits[0], 0), (hits[-1], 0), u'đo được = (n−1) bước', off=-0.030, color=ACC, above=False) dim(ax, (0.0, 0), (0.16, 0), u'thật = n bước', off=0.036, color=WARN) ax.annotate(u'Tia sáng ngoài cùng nằm BÊN TRONG dải,\nkhông đúng ở mép — thiếu tới một bước tia.\n' u'Ở 3 m một bước nanoscan3 là 0.021 m,\ntức 17% của dải 0.12 m: LỆCH HỆ THỐNG.\n\n' u'Bù: n tia trải đúng n−1 bước, nên cộng\nthêm MỘT bước = thêm nửa bước mỗi mép.\n' u'Bước tia lấy từ chính vệt đó → tự hiệu\nchỉnh theo tầm và độ chếch, không thêm\ntham số nào.', (-0.01, -0.058), fontsize=7.3, color=INK) save(fig, 'v_tape.svg') def fig_v_offset(): fig, ax = plt.subplots(figsize=(8.6, 3.6)) frame(ax, (-0.05, 2.55), (-0.78, 0.78), u'offset_x cho chữ V — vì sao KHÔNG ai cộng footprint hộ') v_plate(ax) ax.plot([AP], [0], marker='o', ms=6, color=WARN, zorder=7) gx = AP - 0.95 # robot ax.add_patch(Rectangle((gx - 0.583, -0.48), 1.166, 0.96, fc='#eef3f8', ec=ACC, lw=1.6, zorder=3)) ax.plot([gx], [0], marker='*', ms=15, color='#7b3fbf', zorder=8) ax.annotate(u'base_link', (gx, 0), textcoords='offset points', xytext=(0, -13), fontsize=7.5, color='#7b3fbf', ha='center') segs = [(gx, gx + 0.583, u'rear_extent\n0.583'), (gx + 0.583, gx + 0.653, u'padding\n0.070'), (gx + 0.653, gx + 0.865, u'đầu cánh nhô\n0.212'), (gx + 0.865, AP, u'khe hở\n0.080')] for i, (a, b, lab) in enumerate(segs): yy = 0.56 + 0.0 ax.plot([a, a], [0, yy], color=MUT, lw=0.6, ls=':', zorder=2) ax.plot([b, b], [0, yy], color=MUT, lw=0.6, ls=':', zorder=2) dim(ax, (a, yy), (b, yy), lab, off=0.0, color=WARN, fs=7) dim(ax, (gx, 0), (AP, 0), u'offset_x = 0.583 + 0.070 + 0.212 + 0.080 = 0.945 → 0.95 m', off=-0.66, color='#7b3fbf', above=False) save(fig, 'v_offset.svg') # ================================================================================================= # CUNG TRON LOM # ================================================================================================= CX, R, SPAN = 1.60, 0.40, 2.0 # tâm ở 1.60 m, bán kính 0.40, mở 2.0 rad DEEP = CX + R # điểm sâu nhất ở 2.00 m def cradle(ax, cx=CX, r=R, span=SPAN, color=INK, lw=2.8, z=4, concave=True): t = [(-span / 2 + span * i / 240.0) for i in range(241)] if concave: xs = [cx + r * math.cos(a) for a in t] else: xs = [cx - r * math.cos(a) for a in t] ys = [r * math.sin(a) for a in t] ax.plot(xs, ys, color=color, lw=lw, zorder=z, solid_capstyle='round') return (xs[0], ys[0]), (xs[-1], ys[-1]) def fig_arc_geometry(): fig, ax = plt.subplots(figsize=(7.8, 4.6)) frame(ax, (-0.15, 2.45), (-0.85, 0.85)) sensor(ax) e1, e2 = cradle(ax) ax.plot([CX], [0], marker='o', ms=6, color=ACC, zorder=7) ax.annotate(u'tâm C — nằm PHÍA CẢM BIẾN\n(đây là điều làm nó LÕM)', (CX, 0), textcoords='offset points', xytext=(-118, 18), fontsize=8, color=ACC) for e in (e1, e2): ax.plot([CX, e[0]], [0, e[1]], color=MUT, lw=0.9, ls=':', zorder=3) dim(ax, (CX, 0), e1, u'arc_radius = 0.40 m', off=0.0, color=WARN) a1 = math.degrees(math.atan2(e2[1], e2[0] - CX)) a2 = math.degrees(math.atan2(e1[1], e1[0] - CX)) ax.add_patch(Arc((CX, 0), 0.34, 0.34, theta1=a1, theta2=a2, color=ACC, lw=1.4, zorder=6)) ax.annotate(u'arc_span — góc mở đo TẠI TÂM\n2.0 rad = 114.6°', (CX + 0.18, 0), textcoords='offset points', xytext=(6, -34), fontsize=8, color=ACC) ax.plot([DEEP], [0], marker='o', ms=7, color=WARN, zorder=8) ax.annotate(u'điểm SÂU NHẤT\nP = C · (1 + R/|C|)', (DEEP, 0), textcoords='offset points', xytext=(8, 26), fontsize=8, color=WARN) arrow(ax, (DEEP, 0), (DEEP - 0.55, 0), color='#0d8050', lw=1.8) ax.annotate(u'yaw — hướng từ P về phía cảm biến', (DEEP - 0.56, 0), textcoords='offset points', xytext=(-8, 12), fontsize=8, color='#0d8050', ha='right') gx = DEEP - 0.92 ax.plot([gx], [0], marker='*', ms=15, color='#7b3fbf', zorder=8) dim(ax, (DEEP, 0), (gx, 0), u'offset_x = 0.92 m', off=-0.34, color='#7b3fbf', above=False) ax.annotate(u'P chỉ phụ thuộc TÂM và BÁN KÍNH — cả hai từ phép khớp —\n' u'nên nó KHÔNG dịch khi một phần cung bị che.\n' u'Lấy trung điểm cung nhìn thấy thì dễ hơn, nhưng lệch ngay khi khuất một bên.', (-0.12, -0.80), fontsize=7.5, color=INK) save(fig, 'arc_geometry.svg') def fig_arc_concave_convex(): fig, axs = plt.subplots(1, 2, figsize=(10.4, 3.9)) for ax, concave in zip(axs, (True, False)): ttl = (u'LÕM (pattern: arc) — máng ôm robot' if concave else u'LỒI (cylinder) — trụ tròn, CHƯA hiện thực') frame(ax, (-0.1, 2.6), (-0.75, 0.75), ttl) sensor(ax, label=u'cảm biến') cradle(ax, concave=concave) ax.plot([CX], [0], marker='o', ms=6, color=ACC, zorder=7) ax.annotate(u'C', (CX, 0), textcoords='offset points', xytext=(-4, 7), fontsize=9, color=ACC, ha='center') # cu ly trung binh cua diem mat so voi |C| mean_r = CX + (R * 0.6 if concave else -R * 0.6) ax.plot([0, CX], [0, 0], color=ACC, lw=0.9, ls=':', zorder=3) if concave: ax.plot([DEEP], [0], marker='o', ms=7, color=WARN, zorder=8) ax.annotate(u'|C| < cự ly TB các điểm mặt\n→ tâm nằm GIỮA cảm biến và mặt cong', (0.02, -0.66), fontsize=7.5, color='#0d8050') ax.annotate(u'P đúng chỗ', (DEEP, 0), textcoords='offset points', xytext=(8, 10), fontsize=7.5, color=WARN) else: ax.plot([CX + R], [0], marker='x', ms=11, color=WARN, mew=2.2, zorder=8) ax.annotate(u'|C| > cự ly TB các điểm mặt\n→ tâm nằm SAU mặt cong: LOẠI', (0.02, -0.66), fontsize=7.5, color=WARN) ax.annotate(u'nếu nhận nhầm, P rơi vào ĐÂY —\nbên trong vật. Robot được lệnh\nlái xuyên qua nó.', (CX + R, 0), textcoords='offset points', xytext=(-30, 16), fontsize=7.5, color=WARN, ha='right') save(fig, 'arc_concave.svg') fig_arc_geometry(); fig_arc_concave_convex()