cncjs-handwheel 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Thor Schueler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,254 @@
1
- # Temporary Holding Version
1
+ # cncjs-handwheel
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Bridge between CNCjs and a serial (USB or Bluetooth) ESP32 handwheel pendant. It runs on the same machine as CNCjs.
4
+
5
+ - pendant -> CNCjs: gcode lines, realtime commands, console log lines
6
+ - CNCjs -> pendant: status lines (state, MPos, WPos, feed/spindle), about 10 per second
7
+ - health and the pendant log as JSON on `http://127.0.0.1:8001` (for a CNCjs widget)
8
+
9
+ Requires Node 20 or newer and a CNCjs that uses socket.io 2.x (current CNCjs releases do).
10
+
11
+ ## Install
12
+
13
+ npm install -g cncjs-handwheel
14
+
15
+ Create the config (see below), then run it by hand to try it:
16
+
17
+ cncjs-handwheel --config ~/.cncjs-handwheel.config -v
18
+
19
+ `-v` logs pendant events and the commands sent to CNCjs, `--trace` also logs all controller traffic,
20
+ `--list` lists the serial ports.
21
+
22
+ ### Run as a service (systemd)
23
+
24
+ sudo env "PATH=$PATH" cncjs-handwheel install-service --user <user running CNCjs> --usb-port 1-2:1.0
25
+ sudo systemctl start cncjs-handwheel
26
+
27
+ - `--user` must be the user that runs CNCjs: the bridge reads the CNCjs secret from that user's `~/.cncrc`.
28
+ - `--usb-port` is optional. It is the USB port the pendant is plugged into, the first `KERNELS` value of
29
+ `udevadm info -a -n /dev/ttyUSBx`. It installs a udev rule that creates `/dev/pendant` and sets up the port so
30
+ that opening it does not reset the ESP32 through its auto-reset circuit. Replug the pendant afterwards.
31
+ - `--bt-mac` and `--bt-channel` are for a Bluetooth pendant instead of a USB one (use one or the other, not both).
32
+ They install a second service that binds the pendant to `/dev/rfcomm0` at boot. See
33
+ [Bluetooth pendant](#bluetooth-pendant-raspberry-pi), which also covers a kernel problem on the Raspberry Pi 4.
34
+ - `sudo env "PATH=$PATH"` keeps an nvm-installed Node visible to root; with a system Node plain `sudo` works.
35
+ - The unit stores the absolute path of the Node that ran the command. After a Node upgrade run `install-service` again.
36
+ - `--dry-run` prints the unit and rule without changing anything.
37
+
38
+ Update: `npm install -g cncjs-handwheel@latest && sudo systemctl restart cncjs-handwheel`.
39
+ Remove: `sudo env "PATH=$PATH" cncjs-handwheel uninstall-service` (the config file stays).
40
+
41
+ journalctl -u cncjs-handwheel -f # logs
42
+ curl http://127.0.0.1:8001/status # health
43
+ curl "http://127.0.0.1:8001/log?since=0" # pendant console log
44
+
45
+ ## Bluetooth pendant (Raspberry Pi)
46
+
47
+ The pendant can connect over Bluetooth instead of USB. The firmware must offer **Bluetooth Classic serial (SPP)**,
48
+ for example the Arduino `BluetoothSerial` library. A BLE-only device does not work with this method.
49
+
50
+ > **Raspberry Pi 4 kernel warning.** With the Pi 4's **onboard** Bluetooth, **kernel 6.18 cannot pair or connect to the
51
+ > handwheel** (tested with `6.18.60+rpt-rpi-v8`): `btmon` shows `Status: Page Timeout (0x04)` for the connection
52
+ > attempt, `bluetoothctl pair` fails with `ConnectionAttemptFailed`, and `rfcomm connect` fails with `Host is down`.
53
+ > **Kernel 6.12 is required for that particular configuration** (Pi 4, onboard Bluetooth); on 6.12 the same handwheel
54
+ > connects normally. A **USB Bluetooth adapter also works on 6.18**. The cause is the kernel, not the handwheel. `install-service --bt-mac` prints a warning when it sees a 6.18 Raspberry Pi kernel.
55
+
56
+ ### Setup
57
+
58
+ 1. **Check the kernel.** `uname -r` should show a `6.12.x` kernel if you use the onboard Bluetooth. If apt has installed
59
+ 6.18, install a 6.12 kernel image and reboot (see the Raspberry Pi kernel documentation), then check `uname -r` again.
60
+ To stop apt from upgrading it again, hold the kernel packages (check the names with `dpkg -l | grep linux-image`):
61
+
62
+ sudo apt-mark hold linux-image-rpi-v8 linux-headers-rpi-v8
63
+
64
+ A held kernel receives no updates, so remove the hold (`apt-mark unhold`) and retest when a newer kernel is
65
+ out. With a USB adapter on 6.18 you can skip this; disable the onboard controller so there is only one,
66
+ by adding `dtoverlay=disable-bt` to `/boot/firmware/config.txt` and rebooting.
67
+
68
+ 2. **Pair the pendant** once, with the pendant switched on and close to the Pi. Stop scanning before you pair; pairing while
69
+ a scan is running makes connection attempts fail:
70
+
71
+ bluetoothctl
72
+ agent on
73
+ default-agent
74
+ scan on # wait for: [NEW] Device AA:BB:CC:DD:EE:FF <pendant name>
75
+ scan off
76
+ pair AA:BB:CC:DD:EE:FF # if asked for a PIN, enter it in this window (often 1234 or 0000)
77
+ trust AA:BB:CC:DD:EE:FF
78
+ quit
79
+
80
+ `Connected: no` after pairing is normal; the connection is made when the bridge opens the port.
81
+
82
+ 3. **Install the bindings and the service** (needs `bluez`, which provides `rfcomm`):
83
+
84
+ sudo env "PATH=$PATH" cncjs-handwheel install-service --user <user running CNCjs> --bt-mac AA:BB:CC:DD:EE:FF
85
+
86
+ Use `--bt-channel N` if the serial profile is not on RFCOMM channel 1 (the default and what the ESP32 uses).
87
+ `--dry-run` shows what would be written. This creates `cncjs-handwheel-rfcomm.service`, which runs
88
+ `rfcomm bind 0 <mac> <channel>` at boot, and makes the bridge service start after it.
89
+
90
+ 4. **Point the bridge at the port.** In the config set `device.path` to `/dev/rfcomm0`, then
91
+ `sudo systemctl start cncjs-handwheel`.
92
+
93
+ ### Notes
94
+
95
+ - **One connection at a time.** The pendant accepts a single Bluetooth client. While the bridge is running it connects
96
+ whenever the pendant is on and in range, so other programs (for example Candle on a PC) cannot use it. Stop the
97
+ service first: `sudo systemctl stop cncjs-handwheel`.
98
+ - **Binding is not connecting.** The binding exists whether or not the pendant is on. The connection is made when the
99
+ bridge opens `/dev/rfcomm0` and it retries every few seconds, so the pendant can be switched on later.
100
+ - **No reset problem.** The USB auto-reset issue (see Troubleshooting) does not exist over Bluetooth; the udev rule
101
+ and `--usb-port` are not needed.
102
+ - **Removing:** `uninstall-service` stops both services and releases `/dev/rfcomm0`.
103
+
104
+ ### Troubleshooting Bluetooth
105
+
106
+ - `Page Timeout` / `Host is down` / `ConnectionAttemptFailed`: first check `uname -r` (see the warning above). If the
107
+ kernel is fine, check that no other device is connected to the pendant, move it closer, and look at `sudo btmon` while
108
+ you retry.
109
+ - `rfcomm -a` lists the bindings, `bluetoothctl info AA:BB:CC:DD:EE:FF` shows the pairing state, and
110
+ `journalctl -u cncjs-handwheel-rfcomm -u cncjs-handwheel -f` shows what the services are doing.
111
+ - No `/dev/rfcomm0`: `systemctl status cncjs-handwheel-rfcomm` (is `bluetooth.service` running? is `rfcomm` installed?).
112
+
113
+ ## Configuration
114
+
115
+ Precedence, lowest to highest: defaults, the `pendant` section of `~/.cncrc`, `~/.cncjs-handwheel.config`
116
+ (or the file given with `--config`), command line options. CNCjs rewrites `.cncrc` itself, so a separate file is safer.
117
+ Start from `cncjs-handwheel.config.example.json`.
118
+
119
+ | Key | Meaning |
120
+ |---|---|
121
+ | `cncjs.port` | CNC controller port as CNCjs knows it (not the pendant) |
122
+ | `cncjs.controllerType` | `Grbl` (Smoothie shares the state format) |
123
+ | `cncjs.retryInterval` | ms before a failed or lost port open is retried (default 5000, 0 = never) |
124
+ | `cncjs.openTimeout`, `cncjs.maxOpenTries` | ms to wait for CNCjs to answer an open request (5000) and how many unanswered requests are tolerated before the connection to CNCjs is rebuilt (3) |
125
+ | `cncjs.keepaliveMs` | the open request is repeated this often (30000, 0 = off) to verify that CNCjs still has the bridge attached; no answer rebuilds the connection |
126
+ | `cncjs.reopenOnClose` | reopen the CNC port when it is closed behind the bridge's back, e.g. from the CNCjs UI (default `true`). Set `false` if you want the UI's Close to release the machine |
127
+ | `device.path` | pendant serial port: `/dev/pendant` (USB, with the udev rule) or `/dev/rfcomm0` (Bluetooth) |
128
+ | `device.baudRate`, `device.settleMs` | pendant baud rate; pendant input is ignored this long after opening |
129
+ | `device.send` | `false` (or `--no-pendant-send`): never write anything to the pendant, no status, `PONG` or `ERR`. Pendant to CNCjs keeps working. Useful when the firmware does not read from the serial port yet, or to rule the status stream out when debugging |
130
+ | `device.hupcl`, `dtr`, `rts` | modem control line handling (the defaults are fine with the udev rule) |
131
+ | `status.intervalMs`, `status.minGapMs` | status line at least every 400 ms (the heartbeat), and never more often than every 350 ms (about 3 per second); a burst of commands or position changes costs one line per gap, always carrying the latest values. Explicit `?` / `STATUS?` requests are answered at once |
132
+ | `status.pollAfterCommand` | ask Grbl for a fresh report after pendant commands (at most one per `status.minGapMs`) |
133
+ | `status.format` | `grbl` or `json` |
134
+ | `jog.translate` | rewrite jogs like `G21G91X10F500` to `$J=...` so they do not leave Grbl in relative mode (default `true`; `false` or `--no-jog-translate` sends the pendant's lines as plain gcode, for comparing behavior) |
135
+ | `jog.maxInFlight` | jog governor (default `4`, `0` = off): at most this many gcode lines are handed to CNCjs without an `ok`/`error` reply from Grbl. While the limit is reached, further jogs on the same axis and feed are merged into one longer jog (`Y-0.1` three times becomes `Y-0.3`), opposite jogs cancel out, and the result is sent as soon as a reply frees a slot. The position stays exact; nothing is queued without limit. A different axis or feed replaces the held jog, and a feed hold or reset discards it. This protects Grbl's small serial buffer, which loses characters when a fast wheel outruns it |
136
+ | `jog.cancelSettleMs` | wait between emptying the CNCjs feeder queue and sending the Grbl jog cancel (default 40 ms, see "Jog cancel") |
137
+ | `jog.stallMs` | if the controller acknowledges nothing for this long (default 2000), the unanswered lines are assumed lost: the bridge prints a warning, counts a stall and starts counting afresh |
138
+ | `jog.echo` | tag jog lines with `{echo: false}` as context (default `false`; other gcode is not tagged). CNCjs passes it on in its `serialport:write` event, so a console widget can hide the `feeder> $J=...` echo. Stock CNCjs ignores it (see "Quieting the CNCjs console") |
139
+ | `http` | health endpoint (`enabled`, `host`, `port`) |
140
+ | `log.max` | pendant log lines kept for the widget |
141
+
142
+ ## Pendant protocol
143
+
144
+ Newline-terminated text. Everything except log lines must be printable ASCII.
145
+
146
+ Pendant to bridge:
147
+
148
+ | Line | Effect |
149
+ |---|---|
150
+ | `G21G91X+10F2000` | jog (sent as `$J=...`); any other line is sent to the controller as gcode |
151
+ | `!` / `~` / `^X` (0x18) | feed hold / cycle start / reset, each as a line of its own (reset at most once per 2 s) |
152
+ | `0x85` (the single byte, as a line of its own) or `JOGCANCEL` | cancel the jog in progress (Grbl only), see "Jog cancel" below |
153
+ | `?` or `STATUS?` | immediate status line |
154
+ | `PING` | answered with `PONG` |
155
+ | `; text` | console log entry for the widget; never sent to the controller |
156
+
157
+ Bridge to pendant:
158
+
159
+ | Line | Meaning |
160
+ |---|---|
161
+ | `<Idle\|MPos:x,y,z\|WPos:x,y,z\|FS:feed,spindle>` | status: at most about 3 per second, at least every 400 ms, and shortly after every command |
162
+ | `<Disconnected>` | the bridge is up, but CNCjs or the CNC port is not |
163
+ | `ERR:CNCJS_NOT_READY` | a command was dropped because the CNC port is not open |
164
+ | `PONG` | answer to `PING` |
165
+
166
+ The status right after a command shows where the machine was at that moment; Grbl's `ok` only means the move was
167
+ queued. A fresh report follows within a few hundred milliseconds, and the status stream (about 3 lines per second while
168
+ the machine moves) tracks the move. If no line arrives for about 1 second, treat the bridge as gone.
169
+
170
+ ## Troubleshooting
171
+
172
+ - **CNCjs restarted or was down for a while.** The bridge rebuilds its connection by itself (new socket, new access
173
+ token, backoff of 1 to 10 s) and reattaches to the CNC port; the console shows `Disconnected`, `Reconnecting in ...`,
174
+ `Connected to ws://...` and `Connected to port ...`. No restart of the bridge is needed. Nothing is queued while the
175
+ connection is down, so a stale jog is never replayed afterwards.
176
+ - **`Controller did not acknowledge N gcode line(s)`.** Grbl never answered lines CNCjs was given, so they were most
177
+ likely lost on the way (serial buffer overflow, cable or adapter trouble). Jogs are merged and limited by
178
+ `jog.maxInFlight` to avoid this; if it still happens, lower it (try `2`). `/status` shows the counters under `jog`
179
+ (`sent`, `merged`, `discarded`, `stalls`, `inFlight`, `held`).
180
+ - **Commands from the pendant have no effect.** Look for `Dropped pendant command (...)` in the console. It says whether
181
+ the bridge is not connected to CNCjs or the CNC port is not open, and `/status` counts them in
182
+ `cncjs.droppedCommands`. The pendant also receives `ERR:CNCJS_NOT_READY`. If the bridge claims everything is fine but
183
+ CNCjs still ignores commands, the CNCjs log contains `Serial port "..." not accessible`.
184
+
185
+ - **ESP32 reboots or enters download mode when the port is opened.** Linux raises DTR and RTS on the first open, and
186
+ the auto-reset circuit turns that into a reset. Use the udev rule from `--usb-port`. A capacitor of about 1 uF from
187
+ EN to GND on the board removes the problem at its source.
188
+ - **Nothing from the pendant.** Run with `-v`. Lines rejected as non-text are reported as `dropped non-text line`
189
+ and counted in `/status` (`pendant.garbageLines`); wrong baud rate and a rebooting ESP32 look like that.
190
+ - **`$$` and `$I` output at start.** Normal: CNCjs queries the controller when a client opens the CNC port.
191
+ - **No WPos.** Grbl reports the work offset only every 10-30 status reports; the first WPos can take a few seconds.
192
+
193
+ ## Testing without hardware (pendant simulator)
194
+
195
+ `tools/pendant-sim.js` emulates the handwheel on a serial port, so you can exercise the bridge from a dev machine (it lives in the repository only; it is not part of the npm package).
196
+
197
+ Create a virtual serial pair and start the bridge on one end:
198
+
199
+ ```bash
200
+ socat -d -d pty,raw,echo=0,link=/tmp/pendant-sim pty,raw,echo=0,link=/tmp/pendant-bridge
201
+ cncjs-handwheel --pendant-port /tmp/pendant-bridge -v
202
+ ```
203
+
204
+ Then attach the simulator to the other end:
205
+
206
+ ```bash
207
+ node tools/pendant-sim.js --port /tmp/pendant-sim
208
+ ```
209
+
210
+ Interactive keys: arrows = X/Y jog, PgUp/PgDn (or `u`/`j`) = Z jog, `1`-`4` = step 1/0.1/0.01/0.001, `h` = `$H`, space = `!`, `r` = `~`, `X` = Ctrl-X, `?`, `p` = PING, `l` = log line, `b` = burst of jogs in the last direction, `q` = quit. The status line shows state, WPos, tx/rx counters and the age of the last status.
211
+
212
+ Script mode runs unattended and prints a one-line `SUMMARY`:
213
+
214
+ ```bash
215
+ node tools/pendant-sim.js --port /tmp/pendant-sim --script "step:0.1, x+*500@500, wait:500, home, hold, resume"
216
+ ```
217
+
218
+ Tokens: `x+ x- y+ y- z+ z-`, `x+*200@100` (200 jogs at 100/s), `step:N`, `feed:N`, `home hold resume reset status ping cancel`, `log:text`, `raw:LINE`, `wait:MS`.
219
+
220
+ ## Quieting the CNCjs console
221
+
222
+ Every line the bridge sends goes through the CNCjs *feeder*, and CNCjs echoes feeder lines in its Console widget as `feeder> ...`
223
+ (lines of a G-code file are not echoed, because the sender never emits `serialport:write`). The bridge tags jogs with
224
+ `{echo: false}` in the command context; CNCjs passes the context along, so the Console widget can skip them with one check in
225
+ `src/app/widgets/Console/index.jsx`:
226
+
227
+ ```js
228
+ 'serialport:write': (data, context) => {
229
+ const { source, __sender__ } = { ...context };
230
+ if (__sender__ === this.senderId) { return; }
231
+ if (context && context.echo === false) { return; } // <- jogs from the handwheel bridge
232
+ ...
233
+ ```
234
+
235
+ Two other sources of console noise can be reduced from the bridge's side:
236
+ - `client> ?` and the `<Jog|MPos...>` reply come from the status request the bridge sends after pendant commands.
237
+ Set `status.pollAfterCommand` to `false`; CNCjs polls the controller by itself and the pendant still gets status lines.
238
+ - The `ok` for every jog is echoed by CNCjs (`serialport:read`); only a console filter can hide those.
239
+
240
+ ## Jog cancel
241
+
242
+ For real jogging the pendant sends one long jog when the wheel or button starts (`$J=G21G91X1000F2000`) and a jog cancel when
243
+ it stops. Send the single byte `0x85` followed by a newline (or the text line `JOGCANCEL`). The bridge then
244
+
245
+ 1. discards a jog the governor is holding back,
246
+ 2. sends CNCjs `feeder:stop`, which empties CNCjs's own queue so no further jog line leaves for Grbl (Grbl's cancel flushes
247
+ only Grbl's planner; jogs still waiting in CNCjs would start again afterwards),
248
+ 3. after `jog.cancelSettleMs` sends CNCjs `jogCancel`, which writes the realtime byte `0x85` to Grbl. Grbl acts on `0x85` the
249
+ moment it arrives, ahead of lines still unparsed in its serial buffer, so the short wait lets those be parsed first;
250
+ without it a long jog sent just before the cancel could start after it.
251
+
252
+ Jogs that arrive during that wait are held and released after the cancel. Lines CNCjs had already written to Grbl may still
253
+ run: with the governor that is at most `jog.maxInFlight` short jogs. Jog cancel is Grbl only and needs a CNCjs that knows the
254
+ `jogCancel` command (1.11 or a fork based on it). `/status` counts cancels under `jog.cancels`.