"""Estimated machine values for the pancake printer, with where each number comes from.

Written by Claude (AI) for Andrin Schmid, 17 Nov 2026. Measured inputs are from the team:
  * Hector Astrom, firmware/extrusion/utils.py:
        MM_PER_TURN = 8.0     lead screw: 8 mm plunger travel per motor turn
        VOLUME_PER_CM = 15.81 cm^3 of batter per cm of plunger travel (about a 45 mm bore)
  * Tudor Mocioi, firmware/README.md: 200 full steps per turn, current <= 0.2 on USB power,
    5..12 V external supply for more.
  * Photos in the repo (images/machine-week/extrusion): catheter tip syringe with marks
    50 / 100 / 150 ml, so about 150 ml. Pancake lines drawn by hand are about 4 to 5 mm wide.
Everything else is a first estimate to tune on the machine. The GUI shows these numbers in
Settings > Syringe calculator, where the inputs can be changed.
"""

import math
from dataclasses import dataclass, asdict

STEPS_PER_TURN = 200          # full steps (the UFP firmware microsteps internally)


@dataclass
class SyringeInputs:
    lead_mm_per_turn: float = 8.0       # Hector, utils.py MM_PER_TURN
    ml_per_cm: float = 15.81            # Hector, utils.py VOLUME_PER_CM
    syringe_ml: float = 150.0           # from the photo: marks up to 150 ml
    usable_fraction: float = 0.9        # keep a margin at the end of the stroke
    line_width_mm: float = 4.0          # batter line on the griddle (spreads a bit)
    line_height_mm: float = 1.5         # thickness of one batter line


def syringe_estimates(i: SyringeInputs = SyringeInputs()) -> dict:
    area_mm2 = i.ml_per_cm * 100.0                    # 15.81 cm^3/cm = 1581 mm^3/mm
    bore_mm = 2 * math.sqrt(area_mm2 / math.pi)
    mm_per_step = i.lead_mm_per_turn / STEPS_PER_TURN
    mm3_per_step = area_mm2 * mm_per_step
    line_mm2 = math.pi / 4 * i.line_width_mm * i.line_height_mm   # elliptic cross section
    stroke_mm = i.syringe_ml * 1000 / area_mm2
    return {
        "bore_mm": bore_mm,
        "plunger_mm_per_step": mm_per_step,
        "ml_per_step": mm3_per_step / 1000,
        "ml_per_turn": mm3_per_step * STEPS_PER_TURN / 1000,
        "steps_per_ml": 1000 / mm3_per_step,
        "line_mm3_per_mm": line_mm2,
        "plunger_steps_per_mm": line_mm2 / mm3_per_step,      # -> ufp.plunger_steps_per_mm
        "stroke_mm": stroke_mm,
        "plunger_travel": stroke_mm / mm_per_step * i.usable_fraction,  # -> ufp.plunger_travel
        "ml_per_m_of_line": line_mm2,                           # mm^3/mm = ml/m
    }


# values written into app/pancake_config.yaml, with the reason shown in the GUI
ESTIMATES = {
    "print.line_width": (4.0, "photos: hand drawn lines are 4 to 5 mm wide; was 3.0 (placeholder)"),
    "ufp.plunger_steps_per_mm": (None, "computed: line cross section / batter per step (calculator)"),
    "ufp.plunger_travel": (None, "computed: 150 ml syringe, 90 % of the stroke (calculator)"),
    "ufp.retract_steps": (0.5, "anti drip pull back, 0.5 step = about 0.03 ml. Raise if it drips"),
    "ufp.plunger_speed": (100.0, "prime / retract speed, steps/s (0.5 turn/s)"),
    "ufp.plunger_accel": (400.0, "steps/s^2, gentle on the batter"),
    "app.xy_current": (0.5, "about 0.55 A peak per coil, enough for a light gantry. Needs 12 V"),
    "app.plunger_current": (0.9, "Hector runs the syringes at 0.99. Needs 12 V"),
    "app.usb_power_only": (False, "true caps every current at 0.2 (Tudor's README)"),
    "print.pause_between_colors": (True, "first colour must set before the next one goes on"),
    "design.fit_to_print_area": (False, "photo designs come at their real size in mm"),
}


if __name__ == "__main__":
    for k, v in syringe_estimates().items():
        print(f"{k:22s} {v:10.4f}")
    print(asdict(SyringeInputs()))
