cncjs-handwheel 0.7.2 → 0.7.3
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/README.md +29 -1
- package/bin/cncjs-handwheel +111 -4
- package/cncjs-handwheel.config.example.json +1 -0
- package/lib/guard.js +89 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -139,6 +139,7 @@ Start from `cncjs-handwheel.config.example.json`.
|
|
|
139
139
|
| `jog.cancelSettleMs` | wait between emptying the CNCjs feeder queue and sending the Grbl jog cancel (default 40 ms, see "Jog cancel") |
|
|
140
140
|
| `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 |
|
|
141
141
|
| `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") |
|
|
142
|
+
| `guard.duringJob` | `true` (default): while a job is running or paused (or its state is not known yet) no gcode line from the pendant is sent, see "Jobs and the pendant" |
|
|
142
143
|
| `http` | health endpoint (`enabled`, `host`, `port`) |
|
|
143
144
|
| `log.max` | pendant log lines kept for the widget |
|
|
144
145
|
|
|
@@ -264,7 +265,7 @@ Pendant to bridge:
|
|
|
264
265
|
| Line | Effect |
|
|
265
266
|
|---|---|
|
|
266
267
|
| `G21G91X+10F2000` | jog (sent as `$J=...`); any other line that looks like gcode (words such as `G10 L20 P1 Z15.05`, a Grbl `$` command, a CNCjs `%` line) is sent to the controller as gcode. Other text is not sent: it is shown in the widget's log as `[not gcode, not sent] ...` and counted (`pendant.strayLines`, and in `garbageLines`, the widget's Drop). Turn that off with `device.strictGcode: false` |
|
|
267
|
-
| `!` / `~` / `^X` (0x18) | feed hold / cycle start / reset, each as a line of its own (reset at most once per 2 s) |
|
|
268
|
+
| `!` / `~` / `^X` (0x18) | feed hold / cycle start / reset, each as a line of its own (reset at most once per 2 s). While a job is running `!` is sent as CNCjs `gcode:pause` and while it is paused `~` as `gcode:resume` (see "Jobs and the pendant"); otherwise they are `feedhold` and `cyclestart` |
|
|
268
269
|
| `$X` | unlock; sent to CNCjs as its `unlock` command, because a gcode line is dropped by CNCjs while the machine is in Alarm |
|
|
269
270
|
| `... ; comment` | as in gcode, everything after a `;` that is not at the start of the line is a comment: it is not sent, only the part before it is the command. So `0x18; Reset the machine` is a reset and `$X; Unlock` an unlock. (A line that starts with `;` is a log entry) |
|
|
270
271
|
| `0x85` (the single byte, as a line of its own) or `JOGCANCEL` | cancel the jog in progress (Grbl only), see "Jog cancel" below |
|
|
@@ -280,6 +281,7 @@ Bridge to pendant:
|
|
|
280
281
|
| `<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 |
|
|
281
282
|
| `<Disconnected>` | the bridge is up, but CNCjs or the CNC port is not |
|
|
282
283
|
| `ERR:CNCJS_NOT_READY` | a command was dropped because the CNC port is not open |
|
|
284
|
+
| `ERR:JOB_ACTIVE` | a gcode line was dropped because a job is running or paused (at most once a second), see "Jobs and the pendant" |
|
|
283
285
|
| `PONG` | answer to `PING` |
|
|
284
286
|
|
|
285
287
|
The status right after a command shows where the machine was at that moment; Grbl's `ok` only means the move was
|
|
@@ -369,6 +371,32 @@ Two other sources of console noise can be reduced from the bridge's side:
|
|
|
369
371
|
Set `status.pollAfterCommand` to `false`; CNCjs polls the controller by itself and the pendant still gets status lines.
|
|
370
372
|
- The `ok` for every jog is echoed by CNCjs (`serialport:read`); only a console filter can hide those.
|
|
371
373
|
|
|
374
|
+
## Jobs and the pendant
|
|
375
|
+
|
|
376
|
+
When a job is paused in CNCjs only the job itself is held, not the queue that other commands go through. A jog, a button's
|
|
377
|
+
gcode or a macro sent from the pendant during the pause is written to Grbl at once; Grbl does not run it before the job is
|
|
378
|
+
resumed, and then it runs *between two program lines*. It also upsets the accounting of the sender (the `ok` of the injected
|
|
379
|
+
line is counted for a program line) and, if it fails, pauses the job again with a misleading error.
|
|
380
|
+
|
|
381
|
+
The bridge therefore follows the CNCjs workflow state (`workflow:state`: `idle`, `running`, `paused`) and, unless it is `idle`
|
|
382
|
+
(or not known yet, for example just after a reconnect):
|
|
383
|
+
|
|
384
|
+
- drops every gcode line from the pendant: jogs, button gcode, each line of a macro, `$X`. The pendant gets `ERR:JOB_ACTIVE`
|
|
385
|
+
(at most once a second), the widget's log shows `[a job is paused, not sent] ...`, and `/status` counts them under
|
|
386
|
+
`guard.dropped`;
|
|
387
|
+
- lets the real-time commands through: `!`, `~`, `^X`, `?` and jog cancel. With a job on, `!` pauses the job (CNCjs `gcode:pause`) and `~` resumes a paused one (`gcode:resume`) instead of the plain `feedhold` / `cyclestart`, so the CNCjs workflow follows the machine, and an `M0` / `M6` pause can be continued from the pendant;
|
|
388
|
+
- turns **`M5`** and **`M3`** into Grbl's real-time *toggle spindle stop* (`0x9E`) instead of sending them as gcode. Grbl
|
|
389
|
+
accepts that byte only while the machine is in **Hold** (a job paused with a feed hold); the spindle then stops, and
|
|
390
|
+
Grbl switches it on again by itself when the job is resumed. `M5` stops the spindle, `M3` (with or without `S`, which
|
|
391
|
+
cannot be applied) switches on again a spindle that the bridge stopped. Because `0x9E` is a toggle, a second `M5` and an
|
|
392
|
+
`M3` without a stop before are ignored. Outside Hold, and for controllers other than Grbl, they are dropped like other
|
|
393
|
+
gcode.
|
|
394
|
+
|
|
395
|
+
`/status` shows the workflow state under `cncjs.workflow` and the counters under `guard` (`dropped`, `spindle`, `lastAgeMs`).
|
|
396
|
+
`guard.duringJob: false` turns all this off. The byte is written through CNCjs (`write`); CNCjs sends a string, which a
|
|
397
|
+
serial port encodes as UTF-8, so Grbl sees `0xC2 0x9E`. Grbl 1.1 ignores the extended-ASCII byte it does not know (`0xC2`) and
|
|
398
|
+
acts on `0x9E`, as it does for the override buttons of CNCjs; check once on your machine.
|
|
399
|
+
|
|
372
400
|
## Jog cancel
|
|
373
401
|
|
|
374
402
|
For real jogging the pendant sends one long jog when the wheel or button starts (`$J=G21G91X1000F2000`) and a jog cancel when
|
package/bin/cncjs-handwheel
CHANGED
|
@@ -21,6 +21,7 @@ const http = require('http');
|
|
|
21
21
|
const { Command } = require('commander');
|
|
22
22
|
const { SerialPort } = require('serialport');
|
|
23
23
|
const connectToCncjs = require('../index');
|
|
24
|
+
const guard = require('../lib/guard');
|
|
24
25
|
const pkg = require('../package.json');
|
|
25
26
|
|
|
26
27
|
const DEFAULT_CONFIG_FILE = path.join(os.homedir(), '.cncjs-handwheel.config');
|
|
@@ -88,6 +89,12 @@ const DEFAULTS = {
|
|
|
88
89
|
// 'serialport:write' event, so a console widget can skip them (see README); it changes nothing else
|
|
89
90
|
jog: { translate: true, maxInFlight: 4, stallMs: 2000, echo: false },
|
|
90
91
|
|
|
92
|
+
// duringJob: while a job is running or paused (CNCjs workflow state not idle) no gcode line from the pendant is sent
|
|
93
|
+
// (jogs, button gcode, macros): CNCjs only holds the job, not its own queue, so a line sent during a pause would
|
|
94
|
+
// run in the middle of the program after the resume. Real-time commands (! ~ ^X ? jog cancel) still go through.
|
|
95
|
+
// M5 and M3 are turned into Grbl's real-time "toggle spindle stop" (0x9E), which works while the machine is in Hold
|
|
96
|
+
guard: { duringJob: true },
|
|
97
|
+
|
|
91
98
|
// pendant console lines ("; ...") are kept here for the widget instead of being sent to Grbl
|
|
92
99
|
log: { max: 500 },
|
|
93
100
|
|
|
@@ -205,7 +212,8 @@ const health = {
|
|
|
205
212
|
reconnects: 0, errors: 0, lastError: null, logLines: 0, garbageLines: 0, strayLines: 0, link: null, info: null,
|
|
206
213
|
powered: null, powerError: null }, // powered: null = unknown or not configured, true / false = switched on / off
|
|
207
214
|
cncjs: { socketConnected: false, portOpen: false, port: cfg.cncjs.port, controllerType: cfg.cncjs.controllerType,
|
|
208
|
-
lastError: null, droppedCommands: 0 },
|
|
215
|
+
lastError: null, droppedCommands: 0, workflow: null },
|
|
216
|
+
guard: { dropped: 0, spindle: 0, lastAt: null },
|
|
209
217
|
jog: { sent: 0, merged: 0, discarded: 0, stalls: 0, cancels: 0, inFlight: 0, held: false, lastAt: null },
|
|
210
218
|
lastCommand: null
|
|
211
219
|
};
|
|
@@ -266,19 +274,21 @@ const socket = connectToCncjs(cncOpts, (err) => {
|
|
|
266
274
|
});
|
|
267
275
|
|
|
268
276
|
socket.on('connect', () => { health.cncjs.socketConnected = true; });
|
|
269
|
-
socket.on('disconnect', () => { health.cncjs.socketConnected = false; health.cncjs.portOpen = false; resetJogs(); });
|
|
277
|
+
socket.on('disconnect', () => { health.cncjs.socketConnected = false; health.cncjs.portOpen = false; resetJogs(); setWorkflow(null); });
|
|
270
278
|
socket.on('serialport:read', onControllerLine);
|
|
271
279
|
socket.on('serialport:open', () => {
|
|
272
280
|
if (!health.cncjs.portOpen) { lastWco = null; resetJogs(); } // a keepalive confirmation must not discard the work offset
|
|
273
281
|
health.cncjs.portOpen = true;
|
|
274
282
|
health.cncjs.lastError = null;
|
|
275
283
|
});
|
|
276
|
-
socket.on('serialport:close', () => { health.cncjs.portOpen = false; resetJogs(); });
|
|
284
|
+
socket.on('serialport:close', () => { health.cncjs.portOpen = false; resetJogs(); setWorkflow(null); });
|
|
285
|
+
socket.on('workflow:state', (state) => setWorkflow(typeof state === 'string' ? state : null));
|
|
277
286
|
|
|
278
287
|
['Grbl', 'Smoothie', 'TinyG'].forEach((type) => {
|
|
279
288
|
let dumped = false;
|
|
280
289
|
socket.on(type + ':state', (state) => {
|
|
281
290
|
store.controller.state = state || {};
|
|
291
|
+
if (activeState() !== 'Hold') spindleStopped = false; // Grbl switches the spindle back on when the hold ends
|
|
282
292
|
const wco = state && state.status && state.status.wco;
|
|
283
293
|
if (wco) lastWco = wco;
|
|
284
294
|
sendStatus(false); // forward changes right away (rate limited by status.minGapMs)
|
|
@@ -289,6 +299,84 @@ socket.on('serialport:close', () => { health.cncjs.portOpen = false; resetJogs()
|
|
|
289
299
|
|
|
290
300
|
socket.on('sender:status', (data) => { store.sender.status = data || {}; });
|
|
291
301
|
|
|
302
|
+
// ---------------------------------------------------------------- job guard
|
|
303
|
+
// CNCjs workflow state ('idle', 'running', 'paused'); null until CNCjs has told us. See lib/guard.js.
|
|
304
|
+
let workflowState = null;
|
|
305
|
+
let spindleStopped = false; // the spindle was stopped with 0x9E from here and not switched on again
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Records the CNCjs workflow state. When a job starts, a jog that is being held back is thrown away: it must
|
|
309
|
+
* not be released into the job.
|
|
310
|
+
*
|
|
311
|
+
* @param {?string} state - 'idle', 'running', 'paused' or null (not known)
|
|
312
|
+
* @returns {void}
|
|
313
|
+
*/
|
|
314
|
+
function setWorkflow(state) {
|
|
315
|
+
if (state === workflowState) return;
|
|
316
|
+
log('workflow state:', workflowState, '->', state);
|
|
317
|
+
workflowState = state;
|
|
318
|
+
health.cncjs.workflow = state;
|
|
319
|
+
if (state !== 'idle') discardHeldJog();
|
|
320
|
+
if (state === 'idle') spindleStopped = false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @returns {?string} the Grbl state from the last status report, e.g. 'Hold', 'Run', 'Idle'
|
|
325
|
+
*/
|
|
326
|
+
function activeState() {
|
|
327
|
+
const st = store.controller.state && store.controller.state.status;
|
|
328
|
+
return (st && st.activeState) || null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Tells whether the guard keeps gcode from the pendant out of the machine right now.
|
|
333
|
+
*
|
|
334
|
+
* @returns {boolean}
|
|
335
|
+
*/
|
|
336
|
+
const jobGuarded = () => cfg.guard.duringJob !== false && workflowState !== 'idle';
|
|
337
|
+
|
|
338
|
+
let lastGuardNote = 0;
|
|
339
|
+
/**
|
|
340
|
+
* Applies the decision of the job guard to a gcode line: drops it (told to the pendant and the widget log, at most
|
|
341
|
+
* once a second) or sends the real-time byte in its place.
|
|
342
|
+
*
|
|
343
|
+
* @param {string} line - the gcode line from the pendant
|
|
344
|
+
* @param {object} d - the decision from guard.decide()
|
|
345
|
+
* @returns {boolean} true if something was sent to the machine
|
|
346
|
+
*/
|
|
347
|
+
function applyGuard(line, d) {
|
|
348
|
+
if (d.action === 'realtime') {
|
|
349
|
+
if (!cncReady() || !socket.emit('write', cfg.cncjs.port, d.byte)) {
|
|
350
|
+
noteDropped('write', [d.what]);
|
|
351
|
+
sendToPendant('ERR:CNCJS_NOT_READY');
|
|
352
|
+
queueStatus();
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
spindleStopped = d.spindleStopped;
|
|
356
|
+
health.guard.spindle++;
|
|
357
|
+
health.lastCommand = { at: Date.now(), name: 'write', args: [d.what] };
|
|
358
|
+
log('pendant ->', d.what, '(real-time 0x9E) instead of', JSON.stringify(line));
|
|
359
|
+
answerCommand();
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
if (!d.quiet) {
|
|
363
|
+
health.guard.dropped++;
|
|
364
|
+
health.guard.lastAt = Date.now();
|
|
365
|
+
}
|
|
366
|
+
const now = Date.now();
|
|
367
|
+
if (!d.quiet && now - lastGuardNote >= 1000) {
|
|
368
|
+
lastGuardNote = now;
|
|
369
|
+
const text = line.replace(/[\x00-\x1f\x7f]/g, '');
|
|
370
|
+
console.warn('Dropped pendant gcode (' + d.reason + '): ' + text + ' [' + health.guard.dropped + ' dropped so far]');
|
|
371
|
+
addPendantLog('[' + d.reason + ', not sent] ' + text);
|
|
372
|
+
sendToPendant('ERR:JOB_ACTIVE');
|
|
373
|
+
queueStatus();
|
|
374
|
+
} else {
|
|
375
|
+
log('pendant -> dropped', JSON.stringify(line), '(' + d.reason + ')');
|
|
376
|
+
}
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
|
|
292
380
|
let lastWco = null; // Grbl 1.1 only reports MPos *or* WPos, plus WCO now and then: WPos = MPos - WCO
|
|
293
381
|
/**
|
|
294
382
|
* Tells whether commands can currently reach the CNC controller
|
|
@@ -569,7 +657,8 @@ function onPendantLine(raw) {
|
|
|
569
657
|
// stop means stop: jogs still held back must not fire after a feed hold or a reset
|
|
570
658
|
if (raw === '!' || raw === '\x18') discardHeldJog();
|
|
571
659
|
if (raw === '\x18') jog.inFlight = 0; // a soft reset flushes everything Grbl had not answered yet
|
|
572
|
-
|
|
660
|
+
// with a job on, ! and ~ pause and resume the job, so that the CNCjs workflow follows the machine
|
|
661
|
+
return sendCommand(guard.realtimeCommand(raw, workflowState));
|
|
573
662
|
}
|
|
574
663
|
// decode as UTF-8, drop ANSI colour codes, tabs -> spaces
|
|
575
664
|
let line = Buffer.from(raw, 'binary').toString('utf8').replace(ANSI, '').replace(/\t/g, ' ').trim();
|
|
@@ -668,6 +757,10 @@ function writeGcode(line) {
|
|
|
668
757
|
* @returns {boolean} true if the line was sent or held back
|
|
669
758
|
*/
|
|
670
759
|
function sendGcodeLine(line) {
|
|
760
|
+
// without a connection the normal path answers with ERR:CNCJS_NOT_READY
|
|
761
|
+
const d = !cncReady() ? { action: 'pass' } : guard.decide(line, { enabled: cfg.guard.duringJob !== false, workflow: workflowState, activeState: activeState(),
|
|
762
|
+
controllerType: cfg.cncjs.controllerType, spindleStopped });
|
|
763
|
+
if (d.action !== 'pass') return applyGuard(line, d);
|
|
671
764
|
const j = cfg.jog.maxInFlight > 0 ? parseJog(line) : null;
|
|
672
765
|
if (j) return queueJog(j);
|
|
673
766
|
flushJog(true);
|
|
@@ -707,6 +800,7 @@ function queueJog(j) {
|
|
|
707
800
|
function flushJog(force) {
|
|
708
801
|
const p = jog.pending;
|
|
709
802
|
if (!p) return;
|
|
803
|
+
if (jobGuarded()) { discardHeldJog(); return; }
|
|
710
804
|
if (!force && (cancelTimer || jog.inFlight >= cfg.jog.maxInFlight)) return;
|
|
711
805
|
jog.pending = null;
|
|
712
806
|
health.jog.held = false;
|
|
@@ -949,6 +1043,8 @@ const statusTimer = setInterval(() => sendStatus(false), Math.max(10, Math.min(1
|
|
|
949
1043
|
// ---------------------------------------------------------------- pendant serial link
|
|
950
1044
|
let pendantPort = null;
|
|
951
1045
|
let reconnectTimer = null;
|
|
1046
|
+
let downSince = null; // when the pendant link was lost (null while it is up)
|
|
1047
|
+
const REAL_OUTAGE_MS = 10000; // a link that was down this long was unplugged or switched off, not a glitch
|
|
952
1048
|
|
|
953
1049
|
/**
|
|
954
1050
|
* Records a pendant serial error in the health data and prints it.
|
|
@@ -970,6 +1066,7 @@ function noteError(err) {
|
|
|
970
1066
|
*/
|
|
971
1067
|
function scheduleReconnect() {
|
|
972
1068
|
if (shuttingDown || reconnectTimer) return;
|
|
1069
|
+
if (downSince === null) downSince = Date.now();
|
|
973
1070
|
reconnectTimer = setTimeout(() => {
|
|
974
1071
|
reconnectTimer = null;
|
|
975
1072
|
health.pendant.reconnects++;
|
|
@@ -1108,6 +1205,14 @@ function openPendant() {
|
|
|
1108
1205
|
port.set(lines, (setErr) => { if (setErr) noteError(setErr); });
|
|
1109
1206
|
}
|
|
1110
1207
|
health.pendant.connected = true;
|
|
1208
|
+
// Every retry while the wheel was away counted as a reconnect and an error (hundreds after a night unplugged).
|
|
1209
|
+
// After a real outage they say nothing about the link, so the rolling counters start over; short drops stay counted
|
|
1210
|
+
// so a flapping link still shows. The totals since start are not touched.
|
|
1211
|
+
if (downSince !== null && Date.now() - downSince >= REAL_OUTAGE_MS) {
|
|
1212
|
+
recentEvents.reconnects.length = 0;
|
|
1213
|
+
recentEvents.errors.length = 0;
|
|
1214
|
+
}
|
|
1215
|
+
downSince = null;
|
|
1111
1216
|
health.pendant.link = detectLink(cfg.device.path);
|
|
1112
1217
|
health.pendant.lastError = null;
|
|
1113
1218
|
console.log('Pendant connected on ' + cfg.device.path);
|
|
@@ -1145,6 +1250,8 @@ function healthReport() {
|
|
|
1145
1250
|
lastError: recentCount('errors') > 0 ? health.pendant.lastError : null
|
|
1146
1251
|
},
|
|
1147
1252
|
jog: Object.assign({}, health.jog, { lastAgeMs: age(health.jog.lastAt) }),
|
|
1253
|
+
// gcode from the pendant kept out of the machine during a job; spindle = real-time spindle toggles sent instead of M3 / M5
|
|
1254
|
+
guard: Object.assign({}, health.guard, { enabled: cfg.guard.duringJob !== false, lastAgeMs: age(health.guard.lastAt) }),
|
|
1148
1255
|
lastCommand: health.lastCommand,
|
|
1149
1256
|
status: snapshot()
|
|
1150
1257
|
};
|
package/lib/guard.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Guard against sending gcode lines from the pendant while a job is on (running or paused).
|
|
4
|
+
//
|
|
5
|
+
// Why: in CNCjs only the job (the sender) is held when a job is paused; a line sent in the meantime (a jog, a
|
|
6
|
+
// button's gcode, a macro) goes to Grbl at once and ends up between two program lines, where it runs after the
|
|
7
|
+
// resume. See "Jobs and the pendant" in the README.
|
|
8
|
+
//
|
|
9
|
+
// Rule: while the CNCjs workflow is not idle (or not known yet) every gcode line from the pendant is dropped.
|
|
10
|
+
// Exception: M3 and M5 are not sent as gcode but turned into Grbl's real-time "toggle spindle stop" (0x9E),
|
|
11
|
+
// which Grbl only accepts in the Hold state and which it undoes by itself when the job is resumed:
|
|
12
|
+
// M5 -> 0x9E, the spindle stops (the line is ignored if this guard stopped it already)
|
|
13
|
+
// M3 (or M3 S..)-> 0x9E, the spindle that this guard stopped is switched on again, with the speed it had
|
|
14
|
+
// (the S word of M3 cannot be applied: the spindle comes back with the speed of the program)
|
|
15
|
+
|
|
16
|
+
/** Grbl 1.1 real-time command "toggle spindle stop". */
|
|
17
|
+
const SPINDLE_STOP = '\x9e';
|
|
18
|
+
|
|
19
|
+
const SPINDLE_ON = /^M0?3(?:S(?:\d+\.?\d*|\.\d+))?$/;
|
|
20
|
+
const SPINDLE_OFF = /^M0?5$/;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reduces a gcode line to its words: comments and blanks removed, upper case.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} line - a gcode line
|
|
26
|
+
* @returns {string} e.g. 'M3S6000' for 'M3 S6000 ; Start the spindle'
|
|
27
|
+
*/
|
|
28
|
+
function compact(line) {
|
|
29
|
+
return String(line).replace(/;.*$/, '').replace(/\([^)]*\)/g, '').replace(/\s+/g, '').toUpperCase();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Tells whether a line is a plain spindle command.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} line - a gcode line
|
|
36
|
+
* @returns {?string} 'start' for M3 (optionally with a speed), 'stop' for M5, null for anything else
|
|
37
|
+
*/
|
|
38
|
+
function spindleCommand(line) {
|
|
39
|
+
const c = compact(line);
|
|
40
|
+
if (SPINDLE_OFF.test(c)) return 'stop';
|
|
41
|
+
if (SPINDLE_ON.test(c)) return 'start';
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Says what to do with a gcode line from the pendant.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} line - the gcode line
|
|
49
|
+
* @param {object} ctx
|
|
50
|
+
* @param {boolean} ctx.enabled - the guard is on (config guard.duringJob)
|
|
51
|
+
* @param {?string} ctx.workflow - CNCjs workflow state: 'idle', 'running', 'paused'; null = not known yet
|
|
52
|
+
* @param {?string} ctx.activeState - Grbl state from the status report, e.g. 'Hold'
|
|
53
|
+
* @param {string} ctx.controllerType - 'Grbl', 'Smoothie' or 'TinyG'
|
|
54
|
+
* @param {boolean} ctx.spindleStopped - the spindle was stopped by this guard and not switched on again
|
|
55
|
+
* @returns {{action: 'pass'}
|
|
56
|
+
* | {action: 'drop', reason: string, quiet?: boolean}
|
|
57
|
+
* | {action: 'realtime', byte: string, spindleStopped: boolean, what: string}} what to do: send the line as
|
|
58
|
+
* it is, drop it (quiet: no complaint to the pendant), or send the real-time byte instead
|
|
59
|
+
*/
|
|
60
|
+
function decide(line, ctx) {
|
|
61
|
+
if (!ctx.enabled || ctx.workflow === 'idle') return { action: 'pass' };
|
|
62
|
+
const why = ctx.workflow ? 'a job is ' + ctx.workflow : 'the job state is not known yet';
|
|
63
|
+
const word = spindleCommand(line);
|
|
64
|
+
if (!word) return { action: 'drop', reason: why };
|
|
65
|
+
if (ctx.controllerType !== 'Grbl') return { action: 'drop', reason: why + ' (spindle control during a job needs Grbl)' };
|
|
66
|
+
if (ctx.activeState !== 'Hold') return { action: 'drop', reason: why + ' (the spindle can only be switched while the machine is in Hold)' };
|
|
67
|
+
if (word === 'stop' && ctx.spindleStopped) return { action: 'drop', reason: 'the spindle is stopped already', quiet: true };
|
|
68
|
+
if (word === 'start' && !ctx.spindleStopped) return { action: 'drop', reason: 'the spindle was not stopped from the pendant', quiet: true };
|
|
69
|
+
return { action: 'realtime', byte: SPINDLE_STOP, spindleStopped: word === 'stop', what: word === 'stop' ? 'spindle stop' : 'spindle on' };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Chooses the CNCjs command for a real-time byte from the pendant, so that the CNCjs workflow follows what the
|
|
74
|
+
* machine does. With a job on, `!` pauses the job (`gcode:pause`: workflow paused, then `!` to Grbl) and `~` resumes
|
|
75
|
+
* it (`gcode:resume`: `~` to Grbl, then workflow resumed); with the plain `feedhold` / `cyclestart` the machine
|
|
76
|
+
* stops and restarts while CNCjs still thinks the job is running, or stays paused after the machine moves again.
|
|
77
|
+
* Without a job (idle or not known) they are the plain commands.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} raw - '!', '~' or '\x18'
|
|
80
|
+
* @param {?string} workflow - CNCjs workflow state: 'idle', 'running', 'paused'; null = not known yet
|
|
81
|
+
* @returns {string} the CNCjs command name
|
|
82
|
+
*/
|
|
83
|
+
function realtimeCommand(raw, workflow) {
|
|
84
|
+
if (raw === '!') return workflow === 'running' ? 'gcode:pause' : 'feedhold';
|
|
85
|
+
if (raw === '~') return workflow === 'paused' ? 'gcode:resume' : 'cyclestart';
|
|
86
|
+
return 'reset';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = { decide, spindleCommand, compact, realtimeCommand, SPINDLE_STOP };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cncjs-handwheel",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "CNCjs pendant bridge for a serial (USB/Bluetooth) ESP32 CNC handwheel",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cncjs",
|
|
@@ -29,10 +29,11 @@
|
|
|
29
29
|
],
|
|
30
30
|
"scripts": {
|
|
31
31
|
"start": "node bin/cncjs-handwheel",
|
|
32
|
-
"check": "node --check index.js && node --check bin/cncjs-handwheel && node --check lib/service.js",
|
|
32
|
+
"check": "node --check index.js && node --check bin/cncjs-handwheel && node --check lib/service.js && node --check lib/guard.js",
|
|
33
33
|
"prepublishOnly": "npm run check && npm run build:widget",
|
|
34
34
|
"build:widget": "npm install --prefix widget && npm run build --prefix widget",
|
|
35
|
-
"dev:widget": "npm run dev --prefix widget"
|
|
35
|
+
"dev:widget": "npm run dev --prefix widget",
|
|
36
|
+
"test": "node --test test/*.test.js"
|
|
36
37
|
},
|
|
37
38
|
"engines": {
|
|
38
39
|
"node": ">=20"
|