"""Parametric fiducial (ArUco) marker holder bracket for the exoskeleton.
A printed pad with a RECESSED board pocket so the physical marker seats exactly where the
CAD says (the fidex CAD<->vision coupling). Mounts to a link's holder by its flat base.
Usage: fidex_holder.py <board_mm> <out.stl>
"""
import sys
from build123d import Box, Cylinder, Pos, export_stl


def make_fidex_holder(board=15.0, wall=2.5, pocket_depth=1.6, thick=4.5, bolt_d=2.2):
    w = board + 2 * wall
    block = Pos(0, 0, thick / 2) * Box(w, w, thick)
    pocket = Pos(0, 0, thick - pocket_depth / 2 + 0.01) * Box(board + 0.4, board + 0.4, pocket_depth)
    part = block - pocket
    # two M2 mounting bolt holes in the wall (flanking the pocket) so the bracket bolts to the link
    off = w / 2 - wall / 2
    for sy in (-off, off):
        part = part - (Pos(0, sy, thick / 2) * Cylinder(bolt_d / 2, thick + 1))
    return part


if __name__ == "__main__":
    board = float(sys.argv[1]) if len(sys.argv) > 1 else 15.0
    out = sys.argv[2] if len(sys.argv) > 2 else "fidex_holder.stl"
    p = make_fidex_holder(board)
    export_stl(p, out, tolerance=0.1, ascii_format=False)
    print(f"wrote {out}  board={board}mm  vol={p.volume:.0f}")
