Skip to content

fix(gcode): emit a self-describing modal preamble (G21/G90/G94) (#322) - #324

Merged
CameronBrooks11 merged 1 commit into
mainfrom
fix/emit-gcode-preamble
Jun 30, 2026
Merged

CameronBrooks11 merged 1 commit into
mainfrom
fix/emit-gcode-preamble

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

Closes #322.

Problem

FiberPath emitted no modal setup codes. MarlinDialect.prologue() existed but was never wired into serialize() — dead since introduction, inherited from the original Cyclone TS reference (which also emitted none). A streamed program relied entirely on the controller's power-on modal state for units, positioning, and feed mode; marlin-host's handshake sends only M110 (line-number reset), nothing modal. It works today only because Marlin's defaults (mm / absolute / units-per-minute) happen to match — a latent footgun if the controller isn't freshly at defaults.

Change

serialize() now emits a preamble right after the ; Parameters header:

; Parameters {...}
G21 ; millimeter units
G90 ; absolute positioning
G94 ; feed rate in units per minute
G0 X0 A0 B0
...

Goldens

All 7 example goldens regenerated: exactly +3 lines each, 0 motion bytes changed (verified via numstat). The cone example stays equivalence-gated (no byte golden). Sim/plot snapshot is unchanged (it's IR-based; the preamble is skipped on read).

Tests / checks

  • pytest 244 pass — incl. round-trip byte-exact on regenerated goldens, a new test_modal_preamble_lines_are_skipped, and test_unhonorable_modes_still_raise (G20/G91/G93). · ruff/format/scoped-mypy clean · no wind-schema/openapi drift.
  • Adversarial review: the only blocker it raised (over-broad modal skip dropping G20/G91/G93) was caught and fixed before commit — narrowed to {G21,G90,G94} with a regression test. Round-trip, dialect detection, and requiredGcodes completeness all verified correct.

Decision

Per maintainer call on #322: emit (self-describing output) over require (document-only). Includes G90 (absolute) alongside G21/G94 — same class of assumption, and the most dangerous one to leave implicit.

Every program now begins with a modal preamble — `G21` (mm units), `G90`
(absolute positioning), `G94` (feed in units per minute) — right after the
`; Parameters` header, so the output no longer silently depends on the
controller's power-on modal state. Previously `MarlinDialect.prologue()` existed
but was never wired into `serialize()`, and the toolchain emitted no setup codes
(inherited from the original Cyclone reference); it worked only because Marlin's
defaults happen to match, and marlin-host's handshake sends only `M110`.

The preamble is a serialization concern (like the `; Parameters` header), not
motion, so it stays out of the Motion IR: `serialize()` emits it from the
dialect, and `read_program` skips the modal lines it represents, keeping the
round-trip byte-exact. The skip set is exactly the modes the reader already
assumes (`G21`/`G90`/`G94`); the opposite modes (`G20` inch, `G91` relative,
`G93` inverse-time) still raise `ProgramReadError` rather than be silently
misread as absolute mm.

The `marlin-xab` profile's `requiredGcodes` now lists the full emitted set
(`G0/G21/G90/G92/G94`), and the machine-profile guide states the preamble as the
compatibility contract. Example goldens regenerated: motion bytes unchanged,
three preamble lines added.
@CameronBrooks11
CameronBrooks11 merged commit e8d4991 into main Jun 30, 2026
21 checks passed
@CameronBrooks11
CameronBrooks11 deleted the fix/emit-gcode-preamble branch June 30, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dead code: MarlinDialect.prologue() is never emitted — G21/G94 (units & feed-mode) absent from output

1 participant