Files
p5-wallplotter/lib/README.md
T

216 lines
6.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# WallPlotter library
p5 helper for a Marlin polargraph over Web Serial. Coordinates are **mm**. Origin is machine home (`G92`). Y positive is up. Chrome or Edge, page on localhost or HTTPS.
Load from this package:
```html
<script src="../../lib/WallPlotter.js"></script>
```
Example sketch: `examples/zickzack/`.
---
## `new WallPlotter(options?)`
All options are optional.
| Option | Default | What it is |
|--------|---------|------------|
| `baudRate` | `115200` | Serial baud (must match Marlin) |
| `stepSize` | `10` | Keyboard jog step, mm |
| `feedRateTravel` | `1500` | Pen-up speed, **mm/min** (G-code `F`) |
| `feedRateDraw` | `300` | Pen-down speed, **mm/min** |
| `penUpGcode` | `M280 P0 S90` | Command to lift pen |
| `penDownGcode` | `M280 P0 S50` | Command to lower pen |
| `penUpDelayMs` | `150` | Pause after pen up, ms |
| `penDownDelayMs` | `50` | Pause after pen down, ms |
| `machineXMin` / `machineXMax` | `-830` / `830` | Overlay X extents, mm (idler spacing 1660) |
| `machineYMin` / `machineYMax` | `-1200` / `800` | Overlay Y; idlers at Y=800 (80 cm above work centre) |
| `jogIntervalMs` | `150` | Repeat delay while holding arrows, ms |
| `maxLogLines` | `8` | Serial lines shown on the HUD |
| `previewMinDelayMs` | `30` | Floor for preview segment wait, ms |
| `previewFrameMs` | `16` | Preview animation frame time, ms |
| `timingMultiplier` | `1` | Scale **Test** preview waits (`2` = twice as slow) |
| `onLog(line)` | no-op | Extra callback for each log line |
| `onStatusChange(status)` | no-op | `'connected'` or `'disconnected'` |
---
## Setup / UI
### `enableKeyInputs()`
Keyboard controls. Call from `setup()`. No arguments.
| Key | Action |
|-----|--------|
| `C` | Connect / disconnect |
| `H` | Set home here (`G92 X0 Y0`) — required before Plot |
| `0` | Travel to `(0, 0)` |
| `P` | Toggle pen |
| `← → ↑ ↓` | Jog one step, pen up |
| `Shift + arrows` | Jog one step, pen down |
| `+` / `-` | Jog step size 1–100 mm |
| `L` `R` `T` `B` | Mark left / right / top / bottom bound at current position |
### `enablePlotButtons(options?)`
Adds Plot and Test buttons. Expects global `plot()` and `test()` in the sketch.
| Option | Default | What it is |
|--------|---------|------------|
| `parentId` | `'wallplotter-controls'` | DOM id for the button row |
| `plotLabel` | `'Plot'` | Plot button text |
| `testLabel` | `'Test'` | Test button text |
| `stopLabel` | `'Stop'` | Test button while a test is running |
### `showInfos()`
Draw HUD, path preview, bounds, and key help. Call every frame from `draw()`. Also runs held-arrow jogging when keys are enabled.
---
## Speed
### `setSpeed(travel, draw)`
Feed rates in **mm/min**. Used as G-code `F` and for Test preview timing. Clamped to **1–10000**.
- `travel` (number): pen-up / `move()` speed. Or `{ travel, draw }`.
- `draw` (number, optional): pen-down / `draw()` speed. Omit to leave draw rate unchanged.
Practical range about **100–10000**. Values above **10000** are clamped. Do not use `0`.
```javascript
wp.setSpeed(6000, 2500);
wp.setSpeed({ travel: 6000, draw: 2500 });
```
### `setTimingMultiplier(mult)`
Scales **Test** preview waits only. Plot is paced by Marlin (`ok` + `M400`).
- `mult` (number): `1` = match `setSpeed` estimate, `2` = twice as slow, `0.5` = twice as fast. Clamped to ≥ `0.1`.
---
## Path (buffered drawing)
`move` / `draw` / `wait` / `speed` only **queue**. Nothing is sent until `runPath`.
### `startPath()`
Clear the path buffer. Returns `this`.
### `move(x, y)`
Queue pen-up travel to `(x, y)` mm. Returns `this`.
### `draw(x, y)`
Queue pen-down stroke to `(x, y)` mm. Returns `this`.
### `wait(ms)`
Queue a pause. `ms` is milliseconds. On Plot, sends `G4 P<ms>`. Returns `this`.
### `speed(travel, drawRate)`
Queue a mid-path speed change (mm/min, clamped 1–10000). Either argument may be omitted. Restored after `runPath`. Returns `this`.
### `runPath(testOnly)` / `runPath(commands, testOnly?)`
Run the path. **async**.
- `testOnly` (boolean): `true` = preview only, no serial. `false` = send G-code. Home must be set for Plot.
- `commands` (array, optional): explicit list instead of the `startPath()` buffer.
Plot: wait for Marlin `ok`, then `M400` (move finished). Test: animate using distance / `setSpeed` × `timingMultiplier`.
```javascript
wp.startPath();
wp.move(-200, -200);
wp.draw(200, -200);
wp.wait(500);
await wp.runPath(false); // plot
await wp.runPath(true); // test
```
Sketch: `plot()` → `runPath(false)`, `test()` → `runPath(true)`.
### `estimatePathMs(commands?)`
Returns estimated duration in **milliseconds** for the current path buffer, or for `commands` if given. Uses `setSpeed`, queued `speed()`, `wait()`, and `timingMultiplier`. The HUD shows elapsed / est / remain while plotting.
### `stopPath()` / `stopPlot()`
`stopPath()` aborts the current `runPath`. `stopPlot()` also sends Marlin `M410` (quick stop) during a live plot. Key **S** calls `stopPlot()` while plotting.
---
## Serial / machine
All **async**. Coordinate moves on Plot require home (`H`) first.
### `connect()`
Web Serial port picker, enable steppers (`M17`), pen up, query polargraph area (`M665` → machine overlay bounds), `M114`.
### `disconnect()`
Close the serial port.
### `send(line, options?)`
Write one G-code line and wait for Marlin `ok`.
- `line` (string): command without newline, e.g. `'M114'`.
- `options.allowWithoutHome` (boolean): allow `G0`/`G1` before home. Default `false`.
- `options.waitOk` (boolean): wait for `ok`. Default `true`.
- `options.timeoutMs` (number): `ok` timeout, default `300000`.
### `penUp(force?)` / `penDown(force?)`
Send pen G-code and wait the pen delay. By default, no-op if already in that state. Pass `true` to always send (used by `P` and on connect) so you can test the configured angles.
### `moveTo(x, y)`
Pen up, travel to `(x, y)` mm immediately (not queued). Requires home.
### `drawTo(x, y)`
Pen down, draw to `(x, y)` mm immediately. Requires home.
### `goToZero()`
`moveTo(0, 0)`.
### `step(dx, dy, draw?)`
Relative move in mm.
- `dx`, `dy` (number): delta mm
- `draw` (boolean, default `false`): `true` → `drawTo`, else `moveTo`
### `setHomeHere()`
`G92 X0 Y0`. Sets local `x,y` to 0 and marks home as set.
---
## Fields (read)
| Field | Meaning |
|-------|---------|
| `connected` | Serial open |
| `homeSet` | Home was set with `H` / `setHomeHere()` |
| `x`, `y` | Current position, mm |
| `isPenDown` | Pen state |
| `isPlotting` | `runPath` in progress |
| `feedRateTravel` / `feedRateDraw` | Current speeds, mm/min |
| `timingMultiplier` | Preview wait scale |
| `stepSize` | Jog step, mm |
| `drawBounds` | `{ left, right, top, bottom }` or `null` |