#!/usr/bin/env python3
"""p011 orrery -- where the pins stand on the carrier, and what clears what.
Brim (an AI agent), wake 329.  Pure arithmetic; no mesh.  Units mm.
Edit the block of numbers, run, read the FAIL lines.  DESIGN.md quotes this output.
"""
import math

M_MOON = 1.15            # module of the Moon train (1.25 until the harness refused a 201 mm wheel, 329)
N1, NI, N2, N3, N4 = 58, 16, 16, 58, 17
M_RIM = 1.4              # module of the rim drive
N_RIM, N_PIN = 126, 21
D_P2 = 46.0              # centre -> pin P2 (carries N2+N3), chosen
R_EARTH = 62.0           # centre -> Earth pin, chosen
MOON_ARM = 14.0          # Earth pin -> Moon centre
MOON_BALL_R = 4.0
POST_R = 6.0             # centre post radius
PIN_R = 3.0              # carrier pins
CRANK_R = 16.0           # crank handle circle radius about the pinion axis
KNOB_R = 5.0             # crank knob radius (the arm is no wider at its end)
GAP = 1.0                # the least air I accept between things that must not touch
BACKLASH_CD = 0.15       # added to every centre distance (lean)

pr = lambda n, m: n * m / 2.0            # pitch radius
tip = lambda n, m: pr(n, m) + m          # tip radius
root = lambda n, m: pr(n, m) - 1.25 * m  # root radius

def tri(a, b, c):
    """angle (rad) at the vertex between sides a and b, opposite c"""
    return math.acos((a * a + b * b - c * c) / (2 * a * b))

cd_1i = pr(N1, M_MOON) + pr(NI, M_MOON) + BACKLASH_CD   # centre -> idler pin
cd_i2 = pr(NI, M_MOON) + pr(N2, M_MOON) + BACKLASH_CD   # idler pin -> P2
cd_34 = pr(N3, M_MOON) + pr(N4, M_MOON) + BACKLASH_CD   # P2 -> Earth pin
cd_rim = pr(N_RIM, M_RIM) + pr(N_PIN, M_RIM) + BACKLASH_CD

# P2 on the +x axis; idler above it (+y), Earth below it (-y side) so the two branches do not cross
P2 = (D_P2, 0.0)
a_i = tri(D_P2, cd_1i, cd_i2)
IDL = (cd_1i * math.cos(a_i), cd_1i * math.sin(a_i))
a_e = tri(D_P2, R_EARTH, cd_34)
EARTH = (R_EARTH * math.cos(-a_e), R_EARTH * math.sin(-a_e))
dist = lambda p, q: math.hypot(p[0] - q[0], p[1] - q[1])
O = (0.0, 0.0)

checks = []
def need(name, air):
    checks.append((name, air, air >= GAP))

# level 1: N1 (centre), idler, N2 -- only the meshing pairs may touch
need("N1 tip to N2 tip (they must not mesh directly)", D_P2 - tip(N1, M_MOON) - tip(N2, M_MOON))
need("idler tip to Earth pin", dist(IDL, EARTH) - tip(NI, M_MOON) - PIN_R)
need("N1 tip to Earth pin (level 1)", R_EARTH - tip(N1, M_MOON) - PIN_R)
need("N2 tip to Earth pin (level 1)", dist(P2, EARTH) - tip(N2, M_MOON) - PIN_R)
# level 2: N3 on P2, N4 on the Earth pin, centre post, idler pin (if it rises that far it must clear N3)
need("N3 tip to centre post (level 2)", D_P2 - tip(N3, M_MOON) - POST_R)
need("N4 tip to centre post (level 2)", R_EARTH - tip(N4, M_MOON) - POST_R)
idler_pin_under_n3 = dist(IDL, P2) < tip(N3, M_MOON) + PIN_R
# reach: nothing on the carrier beyond the rim's root circle, and the crank stays outside all of it
reach_n3 = D_P2 + tip(N3, M_MOON)
reach_moon = R_EARTH + MOON_ARM + MOON_BALL_R
reach = max(reach_n3, reach_moon)
need("carrier load inside the rim root circle", root(N_RIM, M_RIM) - reach)
crank_inner = cd_rim - CRANK_R - KNOB_R
need("crank knob circle outside everything on the carrier", crank_inner - reach)

print(f"Moon train m={M_MOON}: N1 {N1} fixed, idler {NI}, N2 {N2} + N3 {N3} one part, N4 {N4} at Earth")
print(f"  ratio {(N1*N3)/(N2*N4):.6f} synodic months a year")
print(f"  centre->idler {cd_1i:.3f}   idler->P2 {cd_i2:.3f}   P2->Earth {cd_34:.3f}")
print(f"  P2     at ({P2[0]:8.3f}, {P2[1]:8.3f})")
print(f"  idler  at ({IDL[0]:8.3f}, {IDL[1]:8.3f})")
print(f"  Earth  at ({EARTH[0]:8.3f}, {EARTH[1]:8.3f})   radius {R_EARTH}")
print(f"rim drive m={M_RIM}: wheel {N_RIM} (pitch r {pr(N_RIM,M_RIM):.2f}, tip r {tip(N_RIM,M_RIM):.2f}, root r {root(N_RIM,M_RIM):.2f}), pinion {N_PIN}")
print(f"  crank turns a year {N_RIM/N_PIN:.4f};  synodic months a crank turn {(N1*N3)/(N2*N4)*N_PIN/N_RIM:.4f}")
print(f"  centre->crank axis {cd_rim:.3f};  crank knob sweeps r {crank_inner:.2f} .. {cd_rim+CRANK_R+KNOB_R:.2f} from the centre")
print(f"reach: N3 {reach_n3:.2f}, Moon {reach_moon:.2f}")
print(f"idler pin stands under N3: {idler_pin_under_n3}  (if True the idler pin must stop below level 2)")
# the harness refuses a part whose bounding SQUARE, turned 45 degrees, passes 256 mm (found 329)
need("wheel fits the bed at any rotation the arranger picks (tip dia * 1.4142 <= 256)", (256.0 - 2 * tip(N_RIM, M_RIM) * math.sqrt(2)) + GAP)
for name, air, ok in checks:
    print(f"  {'ok  ' if ok else 'FAIL'} {air:7.2f} mm  {name}")
