# pancake_machine: app to run the pancake printer

One window to run the pancake printer of the EECS section machine week (MAS.863, Nov 2026) from a
laptop: picture or SVG in, up to 3 batter colours, 5 steppers (CoreXY A and B, syringes 0 to 2).
It runs the team's code from `team/` (a copy of `machine-week/src`) unchanged.

Put together by Andrin Schmid with Claude (AI) on 17 and 18 Nov 2026. Who wrote what: `CREDITS.md`.

## Folders

| Folder | Content | Changed? |
|---|---|---|
| `team/` | `machine-week/src` of 863.26/EECS, commit `a32cc4e771` of 17 Nov, 21:52 (Aaron, Tudor, Hector) | no, exact copy |
| `third_party/ufp/` | Quentin Bolsee's UFP library and stepper firmware (MIT) | no, exact copy |
| `app/` | the window (`simple_gui.py`), glue code (`backend.py`), photo converter, estimates, wiring picture, config | new (Claude for Andrin) |
| `out/` | converted photos, test squares (SVG, preview) | created when used |

The app imports the team files as they are. Python is told not to write `__pycache__` into them,
so `team/` and `third_party/` stay identical to the downloads.

## Setup (Windows, once)

1. Python 3.12 or newer from python.org ("Add python.exe to PATH" ticked; 3.14 works).
2. Double click `setup.bat`. It makes `.venv` here and installs the team requirements, pyserial,
   Pillow, SciPy, contourpy and rembg (background removal, downloads a 176 MB model on first use).
3. Double click `run_gui.bat`. `app/pancake_config.yaml` holds the settings used on the machine
   on 18 Nov; `app/pancake_config.default.yaml` the defaults.

`upload_to_forge.bat` puts this folder on forge as `machine-week/pancake_machine` (one commit, with
your own forge login; `.venv`, `out` and the script itself are left out).

Also there: `run_team_tests.bat` (the team's 47 tests, unchanged) and `run_team_cli.bat`
(the team's `print_ufp.py` with the app config, e.g. `run_team_cli.bat out\cat.svg --dry-run`).

## Setup (Mac)
1. Make sure to have a proper Python installed.
2. From this folder `chmod +x setup.sh`, then `./setup.sh`.
3. To run the GUI, `chmod +x run_gui.sh`, then `./run_gui.sh`.

## Using it (run_gui.bat, one simple window)

1. **Find boards**: asks every USB port for its MOTOR_ID (Hector's search, also on Windows). For
   each board choose what it is (gantry A, gantry B, syringe 0, 1, 2); "turn" moves that motor a
   little so you can see which one it is. The choice is saved by MOTOR_ID.
2. **Connect**, then **Home** (Tudor's homing). Every motor has its own current (gantry A, B,
   syringe 0, 1, 2; 0.1 to 0.99, never 1.0, which the firmware reads as 0; on USB power 0.2).
   A change works at once on the connected motors.
3. **Move**: jog the gantry, push or pull each syringe, "refilled: zero" after refilling.
4. **Print**: open a picture (made into 3 colours) or an SVG, **Draw** one with the mouse (one
   colour = one syringe per line), or make a **Test square** (size and syringe), all through the
   same pipeline. Colours go to the syringe with the nearest batter colour; **all with syringe**
   (empty = by colour) prints everything with one syringe. If a design needs a syringe that is not
   connected and only one is, the app offers to use that one. **Dry run** on Tudor's simulated boards first.
   **Offset test (3 squares)**: syringe 0 draws a 50 mm square, syringe 1 a 40 mm and syringe 2 a
   30 mm square around the same centre. With correct nozzle offsets the gaps to the outer square
   are 5 and 10 mm on every side; x error = (left gap - right gap) / 2, y error = (front gap -
   back gap) / 2, add it to that syringe's offset in the row next to the button and press Set.
   Or measure by eye: move syringe 0 over a mark with the arrows, press "syringe 0 is on it", move
   syringe 1 (or 2) over the same mark and press its button: the x and y offset are set from the
   difference (gantry 0 + offset 0 = gantry N + offset N). Works without homing.
   **Syringe push**: steps the plunger turns per mm the gantry draws (all syringes; 1 step is about
   0.063 ml). **Retract**: steps pulled back after every line (anti drip). **Backlash**: steps of
   slack lost at every change of plunger direction. Tudor's syringe cycle pushes `retract_steps`
   before a line and pulls them back after it, so retract + backlash go into `ufp.retract_steps`:
   the real pull back stays "retract" and the slack is taken up before batter comes out. The arrow
   buttons for the syringes add the backlash too when the direction changes. Before the first
   line of a print each syringe is pulled back once by retract + backlash (unless it already was),
   so the first prime does not push a blob of extra batter. Set re-plans the
   open design and works at once on connected syringes.
   **Speed %**: overall speed for print and travel moves of every design and for the arrows (the
   syringe flow follows, each stroke is timed from its length). Homing keeps Tudor's 20 mm/s.
   **PRINT HERE (no homing)**: move the nozzle over the middle of where the design should go and
   press it; the print is made around that spot and the gantry comes back to it. **PRINT (after
   Home)** uses the homed coordinates. STOP or Esc stops every motor.

All values are in `app/pancake_config.yaml` ("Settings file" button, then "Reload settings").

## What the app does on top of the team code

1. **Firmware folder**: `firmware_link.py` (team) looks for the wrappers in `team/firmware`, but they
   are in `team/firmware/scripts`, so the team code does not import as downloaded. The app sets
   `PANCAKE_FIRMWARE_DIR` to the right folder instead of editing the file.
2. **Separate currents**: gantry and syringes get their own coil current (team: one value for all).
3. **Homing = Tudor's `XYStage.home()`** (17 Nov, tested on the machine): +X until a switch closes,
   that point is x max; back to x min; -Y until a switch closes, that point is y min. The gantry
   then stands at (x min, y min) = `ufp.home_offset`. The app passes `x_home_pos` = x max of the box
   (the team's `ufp_driver` leaves Tudor's default of 500 mm, which breaks every box narrower than
   500 mm) and lets STOP end a running homing at once (his loop would wait for its timeout).
   The app's own switch search (slow second touch) is still selectable in Settings, and the Setup
   tab uses it to drive to one switch only. Manual homing (declare the position) as before.
4. **Syringe strokes while the gantry moves**: since commit `a32cc4e771` (17 Nov, 21:52) Hector's
   `Syringe.extrude()` waits until the plunger stops. The team's `ufp_driver.run` calls it before the
   stroke's XY moves, so all batter would come out on one spot and the gantry would then draw the line
   empty. The app starts the push the same way but returns at once, and polls the syringe through
   his wrapper during every move, so a limit switch hit still stops it, backs off one turn and raises.
5. **Whole plunger steps**: Hector found that fractional step targets leave the motor in a weak
   "half hold" (commit `99f2489a73`). The app rounds every stroke to whole steps and carries the rest
   to the next stroke, so the total batter stays right.
6. **USB scan on Windows**: `find_devices.py` only looks at macOS `usbmodem` ports.
7. **Photo to 3 colours** (`app/image_to_pancake.py`): background removed, darkest batter draws the
   outline and dark details, the other two fill, mirrored because the pancake is flipped.
8. **Jog, syringe purge and zero, calibration, STOP flag**, all through the team wrappers.
10. **Old configs** (with `travel_a` / `travel_b`) are converted to Tudor's box when loaded.

## Team code at a32cc4e771: things to tell the team

* **Current 1.0 means no current.** `stepperDriver.cpp` (Jake Read's driver in Quentin's
  `ufp_stepper` firmware) does `amplitude & 0b1111111111`, so `set_current(1.0)` gives 1024 -> 0:
  the coils are off and nothing moves, while everything else (get_id, switches) works. Use at most
  0.99 (Hector does). The app caps every current at 0.99. One line fix in the firmware: clamp to
  1023 instead of 1024 in `stepper_setAmplitude`.
* 11 of the 51 software tests and 32 of 35 `xy_stage_selftest` cases fail: `XYStage` defaults to
  `x_home_pos=500` (commit 77569fbda5), so homing in any box narrower than 500 mm ends with "path
  leaves the workspace". One line fix: `ufp_driver.open_machine` passes `x_home_pos=x_limits[1]`
  (or `XYStage` uses the box's x max when `x_home_pos` is not given). The syringe and single stepper
  self tests pass.
* `Syringe.extrude()` now blocks (see 4 above), so `print_ufp.py` would extrude before each line
  instead of during it. The comment in `ufp_driver.extrude` still says it returns at once.

## Estimates (marked `# EST` in the config)

From Hector's measurements (8 mm per turn, 15.81 cm3 per cm of plunger, so a 45 mm bore) and the
150 ml syringe in the extrusion photos:

| Value | Estimate | Team value | Why |
|---|---|---|---|
| line width | 4.0 mm | 3.0 | hand drawn lines in the photos are 4 to 5 mm |
| plunger steps per mm | 0.0745 | 1.0 | 4 x 1.5 mm line = 4.71 mm3/mm, one step pushes 63.2 mm3 |
| plunger travel | 2135 steps | 100000 | 150 ml = 94.9 mm stroke = 2372 steps, 90 % usable |
| retract steps | 1 | 0 | anti drip, one whole step = 0.06 ml |
| syringe current | 0.9 | 0.2 | Hector runs the syringes at 0.99 (needs 12 V) |
| gantry current | 0.9 | 0.2 | Tudor's homing runs at 0.9 (needs 12 V) |
| pause between colours | on, 20 s | off | the first colour has to set |
| fit to print area | off | on | photos come at their real size |
| curve tolerance | 0.3 mm | 0.2 | fewer points, fewer gantry stops |

From Tudor's tested homing (17 Nov): `mm_per_step` 0.308, `invert_a` / `invert_b` true, gantry
current 0.9, box x 0 to 500 (X switch at 500), y 0 to 500 (y max not measured), X switch on board A,
Y switch on board B. Still team placeholders, to measure: print area, nozzle offsets.

## Known open points

* Tested only on Tudor's simulated boards, not on the real machine yet.
* Switch homing and the Setup steps are tested on a simulated gantry with wrong wiring on purpose
  (swapped motors, one reversed, 5 % scale error): directions fixed, scale found within 0.05 %,
  homing repeatable to 0.04 mm. Not yet on the real gantry: start at low current, hand near STOP.
* The syringe is timed per stroke while the gantry stops at every point; long curvy strokes can
  get uneven. Larger curve tolerance helps; `queue_blocks` in the firmware would fix it properly.
* The config of the team has motor A and syringe 0 both on MOTOR_ID 3; the app uses 1 to 5.
