# Pancake printer — software (from image to tool path)

Converts an SVG design into motor moves for the CoreXY pancake printer (UFP stepper boards), plus a preview image for reference.
High level pipeline:
svg input -> list[Polyline](design size) -> list[Polyline](Bed coordinates) -> tool paths(events) -> motion (mm steps) -> firmware wrappers (XYStage / StepperMotor) -> UFP boards
(the preview is drawn from the events)

## Setup (may be different depending on your system)

Python 3.10+ (honestly any non-ancient python). run from software folder, below assumes bash

```bash
python -m venv .venv
PY=.venv/Scripts/python.exe        # macOS / Linux: PY=.venv/bin/python
"$PY" -m pip install -r requirements.txt
```


To drive real boards (or to run `--dry-run`, which uses the firmware's simulated boards) you also need
Quentin's ufp library, plus the `[serial]` extra for pyserial (needed for COM ports). The software finds the
firmware wrappers in `../firmware` by itself (`scripts/firmware_link.py`; set `PANCAKE_FIRMWARE_DIR` to use another folder).

```bash
cd "<downloaded ufp repo>/Python"
"<path to software>/.venv/Scripts/python.exe" -m pip install -e ".[serial]"
```


Planning and the preview work without `ufp`. `--dry-run` and the tests that drive the machine need it.

## Config before start
Before a real print: put each board's COM port and MOTOR_ID in `ufp:` in the config, and set `machine.travel_limits` and
`home_offset` (see below). Make sure both XY carriages are free to run towards their limit switches, then
press Enter: every MOTOR_ID is checked first, then the gantry **homes itself** on its limit switches and the print
starts. Ctrl+C stops all motors.

Frames: the firmware wrapper works in a *stage frame* where (0, 0) = the homed position (both XY motors on their switches).
The software works in *machine* coordinates (origin at the lower left of the bed). `ufp.home_offset` is the machine position of the
homed gantry; `stage = machine - home_offset`. The usable region is `machine.travel_limits`, an x / y box that is handed to the
wrapper as its x / y limits (`cfg.stage_limits`); the range of each motor follows from it and may reach past its switch position, so
straight horizontal and vertical lines are never refused. Loading the config fails if `home_offset` is outside the box.

## Usage examples

```bash
PY=.venv/Scripts/python.exe                                         # once per window (macOS / Linux: .venv/bin/python)
"$PY" -m pytest -q                                                  # run the tests
"$PY" scripts/print_ufp.py data/svg/smiley.svg -o out --dry-run     # simulate, writes out/smiley.png
"$PY" scripts/print_ufp.py data/svg/hi.svg -o out --dry-run --show  # also open a preview window
"$PY" scripts/print_ufp.py data/svg/two_color.svg -o out --dry-run # two colours: prints the colour -> nozzle mapping
"$PY" -m serial.tools.list_ports -v                                 # find the boards' COM ports
"$PY" scripts/print_ufp.py data/svg/smiley.svg -o out               # print on the real boards
```
Important: Ctrl + C is hard stop for any major issue (big red button)
Three colours = three syringes, each with its own board (`ufp.plungers`: nozzle id -> port + MOTOR_ID), used one at a time. The plunger is moved separately from the gantry, and every stroke is the same cycle on that colour's syringe: **prime** (push `ufp.retract_steps`) -> **extrude** (one move spread over the stroke's planned time, not locked to position along the line) -> **retract** (pull back the same `retract_steps`, anti-drip). So each stroke starts with exactly the push that the last retraction pulled back, and a colour change needs no extra motion. Two colours cannot share a port (unless it is the same board, see `pause_between_colors`).
Now has multi color control support, note that we currently assume one pluger board per nozzle, you may need to change config (give the nozzles the same port and set pause_between_colors: true) if you want different behavior. Color is not exact match, will use the color closest to svg from our conifguration
## Changelogs and notes
11/15 add in multiple color support
11/17 x / y limits: `XYStage` takes `x_limits` / `y_limits` instead of `travel_a` / `travel_b` (the per-motor travel made the usable region a rotated rectangle and refused straight horizontal / vertical lines from the homed position). `ufp.travel_a/b` are gone from the config; the box is `machine.travel_limits`.
11/16 three colours: `firmware/syringe_wrapper.py` (`Syringe`, `SyringeBank`) owns the prime / extrude / retract cycle and allows one primed syringe at a time; `prime_steps` is gone, `retract_steps` is used in both directions.
11/16 reconciled with the firmware wrappers: motion.py plans in mm, ufp_driver.py is a thin adapter over `XYStage` and `StepperMotor` (homing, workspace checks, MOTOR_ID checks and stop/close all come from the wrapper). Config: `steps_per_rev`/`capstan_diameter` -> `mm_per_step`, `stop_accel` -> `emergency_decel` (mm/s^2), new `home_offset`, `motor_v_max/a_max`, `plunger_travel`.
I may be breaching the job of firmware team somewhat(motion.py, print_ufp.py, and ufp_driver.py) as I want to make sure the pipeline works end to end. Firmware team feel free to ignore those files.
11/13 Init the 5 folders I think we will need for now, will fill in the README file to carve out exact functions. The goal is to keep everything as modular as humanly possible
Under constrcution

Design choice:
Machine origin at lower left corner to mirror our in lab CNC miller
Uses UFP control now after discussion with hardware team

Code excution:
All command tested on local windows machine, should be compatile in Mac terminal but may need different commands


AI usage:
Worked with both copilot and Claude to produce the package, but code generation followed my planned outline. I also reviewed and verified line by line to make sure quality
Happy to take responsiblity should any issue arise

## Next Step
TODO functional UI control
TODO direct photo conversion? (online jpg to svg may be a better option here)