cncjs-handwheel 0.1.0 → 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/README.md +165 -8
- package/bin/cncjs-handwheel +291 -28
- package/cncjs-handwheel.config.example.json +40 -0
- package/index.js +311 -62
- package/lib/service.js +109 -8
- package/package.json +2 -2
package/bin/cncjs-handwheel
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// Health/status of both links is exposed as JSON on http://127.0.0.1:8001/status (configurable).
|
|
8
8
|
// Pendant logs can be accessed on http://127.0.0.1:8001/log (configurable)
|
|
9
9
|
|
|
10
|
-
// "install-service" / "uninstall-service": set up the systemd unit (and udev rule), then exit
|
|
10
|
+
// "install-service" / "uninstall-service": set up the systemd unit (and udev rule or Bluetooth binding), then exit
|
|
11
11
|
const subcommand = process.argv[2];
|
|
12
12
|
if (subcommand === 'install-service' || subcommand === 'uninstall-service') {
|
|
13
13
|
require('../lib/service')(subcommand, process.argv.slice(3));
|
|
@@ -32,13 +32,19 @@ const DEFAULTS = {
|
|
|
32
32
|
controllerType: 'Grbl', // Grbl | Smoothie (TinyG: state is not mapped yet)
|
|
33
33
|
socketAddress: 'localhost',
|
|
34
34
|
socketPort: 8000,
|
|
35
|
-
accessTokenLifetime: '30d'
|
|
35
|
+
accessTokenLifetime: '30d',
|
|
36
|
+
retryInterval: 5000, // ms before a failed or lost port open is retried (0 = never)
|
|
37
|
+
openTimeout: 5000, // ms to wait for CNCjs to answer an open request
|
|
38
|
+
maxOpenTries: 3, // unanswered open requests before the connection to CNCjs is rebuilt
|
|
39
|
+
keepaliveMs: 30000, // repeat the open request this often to verify the attachment (0 = off)
|
|
40
|
+
reopenOnClose: true // reopen the CNC port when it gets closed (e.g. from the CNCjs UI)
|
|
36
41
|
},
|
|
37
42
|
device: {
|
|
38
43
|
path: '/dev/pendant', // the pendant's serial port (e.g. /dev/ttyACM0 or /dev/rfcomm0)
|
|
39
44
|
baudRate: 115200,
|
|
40
45
|
reconnectMs: 3000,
|
|
41
46
|
settleMs: 2000, // ignore pendant input this long after opening the port
|
|
47
|
+
send: true, // false: never write anything to the pendant (status, PONG, ERR)
|
|
42
48
|
|
|
43
49
|
// Modem-control lines. The CH340 + auto-reset transistors wire DTR/RTS to EN/IO0, so on Linux
|
|
44
50
|
// the DTR/RTS toggling around open/close can reset the ESP32 or drop it into download mode.
|
|
@@ -48,16 +54,21 @@ const DEFAULTS = {
|
|
|
48
54
|
},
|
|
49
55
|
status: {
|
|
50
56
|
format: 'grbl', // 'grbl' -> <Idle|MPos:..|WPos:..|FS:..> 'json' -> {"t":"status",...}
|
|
51
|
-
intervalMs:
|
|
52
|
-
minGapMs:
|
|
57
|
+
intervalMs: 400, // unsolicited status line at least this often (also the pendant's "host alive" signal)
|
|
58
|
+
minGapMs: 350, // minimum gap between two status lines (about 3 per second at most)
|
|
53
59
|
pollAfterCommand: true, // ask CNCjs/Grbl for a fresh status report after every pendant command
|
|
54
60
|
decimals: 3
|
|
55
61
|
},
|
|
56
62
|
http: { enabled: true, host: '127.0.0.1', port: 8001 },
|
|
57
63
|
|
|
58
|
-
// Grbl 1.1 only: rewrite "G21G91X+10F2000" into "$J=G21G91X+10F2000" so a pendant jog does not
|
|
64
|
+
// translate (Grbl 1.1 only): rewrite "G21G91X+10F2000" into "$J=G21G91X+10F2000" so a pendant jog does not
|
|
59
65
|
// leave CNCjs/Grbl stuck in relative mode (and so jogs go through Grbl's jog queue)
|
|
60
|
-
|
|
66
|
+
// maxInFlight: at most this many gcode lines handed to CNCjs and not yet answered with ok/error. While the
|
|
67
|
+
// limit is reached, jogs on the same axis and feed are merged into one longer jog (0 = no limit, no merging)
|
|
68
|
+
// stallMs: if nothing is acknowledged for this long, assume lines were lost and start counting afresh
|
|
69
|
+
// echo: tag jog lines sent to CNCjs with {echo: false} in their context. CNCjs passes the context on in its
|
|
70
|
+
// 'serialport:write' event, so a console widget can skip them (see README); it changes nothing else
|
|
71
|
+
jog: { translate: true, maxInFlight: 4, stallMs: 2000, echo: false },
|
|
61
72
|
|
|
62
73
|
// pendant console lines ("; ...") are kept here for the widget instead of being sent to Grbl
|
|
63
74
|
log: { max: 500 },
|
|
@@ -82,7 +93,9 @@ program
|
|
|
82
93
|
.option('--pendant-port <path>', 'serial port of the pendant')
|
|
83
94
|
.option('--pendant-baudrate <baudrate>', 'baud rate of the pendant')
|
|
84
95
|
.option('-v, --verbose', 'log pendant events and the commands sent to CNCjs')
|
|
85
|
-
.option('--trace', 'also log all controller traffic (very noisy)')
|
|
96
|
+
.option('--trace', 'also log all controller traffic (very noisy)')
|
|
97
|
+
.option('--no-pendant-send', 'do not write anything to the pendant (same as device.send: false)')
|
|
98
|
+
.option('--no-jog-translate', 'send pendant jogs as plain gcode instead of $J= (same as jog.translate: false)');
|
|
86
99
|
program.parse(process.argv);
|
|
87
100
|
const cli = program.opts();
|
|
88
101
|
|
|
@@ -137,6 +150,8 @@ if (cli.pendantPort) cfg.device.path = cli.pendantPort;
|
|
|
137
150
|
if (cli.pendantBaudrate) cfg.device.baudRate = cli.pendantBaudrate;
|
|
138
151
|
if (cli.verbose) cfg.verbose = true;
|
|
139
152
|
if (cli.trace) cfg.trace = true;
|
|
153
|
+
if (cli.pendantSend === false) cfg.device.send = false;
|
|
154
|
+
if (cli.jogTranslate === false) cfg.jog.translate = false;
|
|
140
155
|
|
|
141
156
|
if (!cfg.cncjs.port) { console.error('Missing CNC controller port (cncjs.port / --port)'); process.exit(1); }
|
|
142
157
|
if (!cfg.device.path) { console.error('Missing pendant port (device.path / --pendant-port). Try --list'); process.exit(1); }
|
|
@@ -149,7 +164,8 @@ const health = {
|
|
|
149
164
|
pendant: { path: cfg.device.path, connected: false, lastRx: null, lastTx: null, rxLines: 0, txLines: 0,
|
|
150
165
|
reconnects: 0, errors: 0, lastError: null, logLines: 0, garbageLines: 0 },
|
|
151
166
|
cncjs: { socketConnected: false, portOpen: false, port: cfg.cncjs.port, controllerType: cfg.cncjs.controllerType,
|
|
152
|
-
lastError: null },
|
|
167
|
+
lastError: null, droppedCommands: 0 },
|
|
168
|
+
jog: { sent: 0, merged: 0, discarded: 0, stalls: 0, cancels: 0, inFlight: 0, held: false },
|
|
153
169
|
lastCommand: null
|
|
154
170
|
};
|
|
155
171
|
|
|
@@ -172,9 +188,14 @@ const socket = connectToCncjs(cncOpts, (err) => {
|
|
|
172
188
|
});
|
|
173
189
|
|
|
174
190
|
socket.on('connect', () => { health.cncjs.socketConnected = true; });
|
|
175
|
-
socket.on('disconnect', () => { health.cncjs.socketConnected = false; health.cncjs.portOpen = false; });
|
|
176
|
-
socket.on('serialport:
|
|
177
|
-
socket.on('serialport:
|
|
191
|
+
socket.on('disconnect', () => { health.cncjs.socketConnected = false; health.cncjs.portOpen = false; resetJogs(); });
|
|
192
|
+
socket.on('serialport:read', onControllerLine);
|
|
193
|
+
socket.on('serialport:open', () => {
|
|
194
|
+
if (!health.cncjs.portOpen) { lastWco = null; resetJogs(); } // a keepalive confirmation must not discard the work offset
|
|
195
|
+
health.cncjs.portOpen = true;
|
|
196
|
+
health.cncjs.lastError = null;
|
|
197
|
+
});
|
|
198
|
+
socket.on('serialport:close', () => { health.cncjs.portOpen = false; resetJogs(); });
|
|
178
199
|
|
|
179
200
|
['Grbl', 'Smoothie', 'TinyG'].forEach((type) => {
|
|
180
201
|
let dumped = false;
|
|
@@ -197,7 +218,26 @@ let lastWco = null; // Grbl 1.1 only reports MPos *or* WPos, plus WCO now and th
|
|
|
197
218
|
*
|
|
198
219
|
* @returns {boolean}
|
|
199
220
|
*/
|
|
200
|
-
const cncReady = () =>
|
|
221
|
+
const cncReady = () => socket.connected && health.cncjs.portOpen;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Records and reports a command that could not be sent (at most one console line per second).
|
|
225
|
+
*
|
|
226
|
+
* @param {string} name - the CNCjs command that was dropped
|
|
227
|
+
* @param {Array} args - its arguments
|
|
228
|
+
* @returns {void}
|
|
229
|
+
*/
|
|
230
|
+
let lastDropWarn = 0;
|
|
231
|
+
const fmtArgs = (args) => args.map((a) => (a !== null && typeof a === 'object' ? JSON.stringify(a) : a));
|
|
232
|
+
function noteDropped(name, args) {
|
|
233
|
+
health.cncjs.droppedCommands++;
|
|
234
|
+
const now = Date.now();
|
|
235
|
+
if (now - lastDropWarn < 1000) return;
|
|
236
|
+
lastDropWarn = now;
|
|
237
|
+
console.warn('Dropped pendant command (' + [name].concat(fmtArgs(args)).join(' ') + '): ' +
|
|
238
|
+
(socket.connected ? 'the CNC port is not open' : 'not connected to CNCjs') +
|
|
239
|
+
' [' + health.cncjs.droppedCommands + ' dropped so far]');
|
|
240
|
+
}
|
|
201
241
|
|
|
202
242
|
/**
|
|
203
243
|
* Sends a command to the CNC controller through CNCjs and answers the pendant with a status line.
|
|
@@ -210,13 +250,17 @@ const cncReady = () => health.cncjs.socketConnected && health.cncjs.portOpen;
|
|
|
210
250
|
*/
|
|
211
251
|
function sendCommand(name, ...args) {
|
|
212
252
|
if (!cncReady()) {
|
|
253
|
+
noteDropped(name, args);
|
|
213
254
|
sendToPendant('ERR:CNCJS_NOT_READY');
|
|
214
|
-
|
|
255
|
+
queueStatus();
|
|
215
256
|
return false;
|
|
216
257
|
}
|
|
217
258
|
health.lastCommand = { at: Date.now(), name, args };
|
|
218
|
-
log('pendant ->', name, ...args);
|
|
219
|
-
socket.emit('command', cfg.cncjs.port, name, ...args)
|
|
259
|
+
log('pendant ->', name, ...fmtArgs(args));
|
|
260
|
+
if (!socket.emit('command', cfg.cncjs.port, name, ...args)) {
|
|
261
|
+
noteDropped(name, args);
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
220
264
|
answerCommand();
|
|
221
265
|
return true;
|
|
222
266
|
}
|
|
@@ -226,15 +270,16 @@ function sendCommand(name, ...args) {
|
|
|
226
270
|
// a few tens of ms (forwarded by the :state handler).
|
|
227
271
|
let lastPoll = 0;
|
|
228
272
|
/**
|
|
229
|
-
* Answers a command from the pendant with a status line
|
|
230
|
-
*
|
|
273
|
+
* Answers a command from the pendant with a status line (within status.minGapMs, so a burst of
|
|
274
|
+
* commands does not flood the pendant), and asks the controller for a fresh status report (at most
|
|
275
|
+
* once per status.minGapMs) so the new position follows shortly after.
|
|
231
276
|
*
|
|
232
277
|
* @returns {void}
|
|
233
278
|
*/
|
|
234
279
|
function answerCommand() {
|
|
235
|
-
|
|
280
|
+
queueStatus();
|
|
236
281
|
const now = Date.now();
|
|
237
|
-
if (cfg.status.pollAfterCommand && now - lastPoll >= 50) {
|
|
282
|
+
if (cfg.status.pollAfterCommand && now - lastPoll >= Math.max(50, cfg.status.minGapMs)) {
|
|
238
283
|
lastPoll = now;
|
|
239
284
|
socket.emit('command', cfg.cncjs.port, 'statusreport');
|
|
240
285
|
}
|
|
@@ -352,12 +397,17 @@ function dropLine(raw) {
|
|
|
352
397
|
function onPendantLine(raw) {
|
|
353
398
|
health.pendant.rxLines++;
|
|
354
399
|
if (raw === '?') return sendStatus(true);
|
|
400
|
+
// Grbl jog cancel: the raw byte 0x85 (also accepted as UTF-8, C2 85), or the text alias JOGCANCEL
|
|
401
|
+
if (raw === '\x85' || raw === '\xc2\x85' || raw.toUpperCase() === 'JOGCANCEL') return cancelJog();
|
|
355
402
|
if (REALTIME[raw]) {
|
|
356
403
|
if (raw === '\x18') {
|
|
357
404
|
const now = Date.now();
|
|
358
405
|
if (now - lastReset < 2000) return;
|
|
359
406
|
lastReset = now;
|
|
360
407
|
}
|
|
408
|
+
// stop means stop: jogs still held back must not fire after a feed hold or a reset
|
|
409
|
+
if (raw === '!' || raw === '\x18') discardHeldJog();
|
|
410
|
+
if (raw === '\x18') jog.inFlight = 0; // a soft reset flushes everything Grbl had not answered yet
|
|
361
411
|
return sendCommand(REALTIME[raw]);
|
|
362
412
|
}
|
|
363
413
|
// decode as UTF-8, drop ANSI colour codes, tabs -> spaces
|
|
@@ -378,9 +428,203 @@ function onPendantLine(raw) {
|
|
|
378
428
|
if (cfg.jog.translate && cfg.cncjs.controllerType === 'Grbl') {
|
|
379
429
|
line = toJog(line);
|
|
380
430
|
}
|
|
381
|
-
|
|
431
|
+
sendGcodeLine(line);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ---------------------------------------------------------------- jog governor
|
|
435
|
+
// CNCjs hands every gcode line it is given to Grbl without waiting for the previous one to be
|
|
436
|
+
// answered. Grbl only has a 128 byte serial buffer (about 6 jog lines), and stops reading it while its
|
|
437
|
+
// planner is full, so a fast wheel makes it lose characters: jogs vanish or turn into garbage.
|
|
438
|
+
// The bridge therefore counts the lines it has handed to CNCjs minus the ok / error replies it sees,
|
|
439
|
+
// and while jog.maxInFlight is reached it merges further jogs (same axis, feed and relative mode;
|
|
440
|
+
// opposite directions cancel out) into one pending jog that is sent as soon as a reply frees a slot.
|
|
441
|
+
const jog = { inFlight: 0, lastAck: 0, pending: null };
|
|
442
|
+
const JOG_PARTS = /^\$J=((?:G(?:20|21|90|91))+)([XYZABC])([-+]?\d*\.?\d+)F(\d*\.?\d+)$/;
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Splits a single-axis relative Grbl jog (`$J=G21G91Y-0.1F2000`) into its parts.
|
|
446
|
+
*
|
|
447
|
+
* @param {string} line - gcode line, already translated by toJog()
|
|
448
|
+
* @returns {?{raw: string, prefix: string, axis: string, dist: number, feed: string}} null if the line
|
|
449
|
+
* is not a jog that can be merged with others
|
|
450
|
+
*/
|
|
451
|
+
function parseJog(line) {
|
|
452
|
+
const m = JOG_PARTS.exec(line);
|
|
453
|
+
if (!m) return null;
|
|
454
|
+
const words = m[1].match(/G\d+/g);
|
|
455
|
+
if (!words.includes('G91') || words.includes('G90')) return null;
|
|
456
|
+
return { raw: line, prefix: m[1], axis: m[2], dist: Number(m[3]), feed: m[4] };
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Formats a signed jog distance without floating point noise, e.g. -0.1, +0.3, +10.
|
|
461
|
+
*
|
|
462
|
+
* @param {number} d - distance
|
|
463
|
+
* @returns {string}
|
|
464
|
+
*/
|
|
465
|
+
function fmtDist(d) {
|
|
466
|
+
const s = Math.abs(d).toFixed(4).replace(/\.?0+$/, '');
|
|
467
|
+
return (d < 0 ? '-' : '+') + (s || '0');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Sends a gcode line to CNCjs and counts it as unanswered (when the jog governor is on). Jogs ($J=)
|
|
472
|
+
* carry `{echo: false}` as context when jog.echo is off.
|
|
473
|
+
*
|
|
474
|
+
* @param {string} line - the gcode line
|
|
475
|
+
* @returns {boolean} true if the line was sent
|
|
476
|
+
*/
|
|
477
|
+
function writeGcode(line) {
|
|
478
|
+
const quiet = !cfg.jog.echo && /^\$J=/.test(line);
|
|
479
|
+
if (!(quiet ? sendCommand('gcode', line, { echo: false }) : sendCommand('gcode', line))) return false;
|
|
480
|
+
if (cfg.jog.maxInFlight > 0) {
|
|
481
|
+
if (jog.inFlight === 0) jog.lastAck = Date.now();
|
|
482
|
+
jog.inFlight++;
|
|
483
|
+
health.jog.inFlight = jog.inFlight;
|
|
484
|
+
}
|
|
485
|
+
health.jog.sent++;
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Sends a gcode line from the pendant: jogs go through the governor, everything else goes straight
|
|
491
|
+
* out (after any held back jog, to keep the order).
|
|
492
|
+
*
|
|
493
|
+
* @param {string} line - the gcode line
|
|
494
|
+
* @returns {boolean} true if the line was sent or held back
|
|
495
|
+
*/
|
|
496
|
+
function sendGcodeLine(line) {
|
|
497
|
+
const j = cfg.jog.maxInFlight > 0 ? parseJog(line) : null;
|
|
498
|
+
if (j) return queueJog(j);
|
|
499
|
+
flushJog(true);
|
|
500
|
+
return writeGcode(line);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Adds a jog to the pending one (same axis, feed and mode: distances add up, opposite jogs cancel;
|
|
505
|
+
* anything else replaces it) and sends it if there is room.
|
|
506
|
+
*
|
|
507
|
+
* @param {object} j - jog from parseJog()
|
|
508
|
+
* @returns {boolean} true
|
|
509
|
+
*/
|
|
510
|
+
function queueJog(j) {
|
|
511
|
+
const p = jog.pending;
|
|
512
|
+
if (p && p.prefix === j.prefix && p.axis === j.axis && p.feed === j.feed) {
|
|
513
|
+
p.dist += j.dist;
|
|
514
|
+
p.raw = null;
|
|
515
|
+
health.jog.merged++;
|
|
516
|
+
if (Math.abs(p.dist) < 1e-9) jog.pending = null;
|
|
517
|
+
} else {
|
|
518
|
+
if (p) health.jog.discarded++;
|
|
519
|
+
jog.pending = j;
|
|
520
|
+
}
|
|
521
|
+
flushJog(false);
|
|
522
|
+
health.jog.held = !!jog.pending;
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Sends the pending jog if the limit allows (or when forced).
|
|
528
|
+
*
|
|
529
|
+
* @param {boolean} force - send even if jog.maxInFlight is reached
|
|
530
|
+
* @returns {void}
|
|
531
|
+
*/
|
|
532
|
+
function flushJog(force) {
|
|
533
|
+
const p = jog.pending;
|
|
534
|
+
if (!p) return;
|
|
535
|
+
if (!force && (cancelTimer || jog.inFlight >= cfg.jog.maxInFlight)) return;
|
|
536
|
+
jog.pending = null;
|
|
537
|
+
health.jog.held = false;
|
|
538
|
+
writeGcode(p.raw || '$J=' + p.prefix + p.axis + fmtDist(p.dist) + 'F' + p.feed);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const CANCEL_SETTLE_DEFAULT_MS = 40;
|
|
542
|
+
let cancelTimer = null;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Cancels the jog in progress (pendant line 0x85 or JOGCANCEL). Three steps, in this order:
|
|
546
|
+
* 1. the jog held back by the governor is discarded;
|
|
547
|
+
* 2. `feeder:stop` empties the CNCjs feeder queue right away, so no further jog line leaves for Grbl
|
|
548
|
+
* (a Grbl jog cancel only flushes Grbl's planner, jogs still queued in CNCjs would continue);
|
|
549
|
+
* 3. after jog.cancelSettleMs the `jogCancel` command (0x85) is sent. Grbl handles 0x85 the moment it
|
|
550
|
+
* arrives, ahead of lines still unparsed in its serial buffer, so the short wait lets those lines
|
|
551
|
+
* be parsed first; otherwise a long jog sent just before the cancel could start after it.
|
|
552
|
+
* Jogs from the pendant that arrive during the wait are held and released after the cancel.
|
|
553
|
+
* Lines CNCjs had already written to Grbl may still run (at most jog.maxInFlight short jogs).
|
|
554
|
+
*
|
|
555
|
+
* @returns {boolean} true if the cancel was started
|
|
556
|
+
*/
|
|
557
|
+
function cancelJog() {
|
|
558
|
+
health.jog.cancels++;
|
|
559
|
+
if (cfg.cncjs.controllerType !== 'Grbl') {
|
|
560
|
+
log('pendant -> jog cancel ignored: only supported for Grbl');
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
discardHeldJog();
|
|
564
|
+
if (!sendCommand('feeder:stop')) return false;
|
|
565
|
+
jog.inFlight = 0; // lines removed from the feeder will never be answered
|
|
566
|
+
health.jog.inFlight = 0;
|
|
567
|
+
clearTimeout(cancelTimer);
|
|
568
|
+
cancelTimer = setTimeout(() => {
|
|
569
|
+
cancelTimer = null;
|
|
570
|
+
sendCommand('jogCancel');
|
|
571
|
+
flushJog(false);
|
|
572
|
+
}, Number(cfg.jog.cancelSettleMs !== undefined ? cfg.jog.cancelSettleMs : CANCEL_SETTLE_DEFAULT_MS));
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Throws away a jog that is being held back (after a feed hold or reset).
|
|
578
|
+
*
|
|
579
|
+
* @returns {void}
|
|
580
|
+
*/
|
|
581
|
+
function discardHeldJog() {
|
|
582
|
+
if (jog.pending) health.jog.discarded++;
|
|
583
|
+
jog.pending = null;
|
|
584
|
+
health.jog.held = false;
|
|
382
585
|
}
|
|
383
586
|
|
|
587
|
+
/**
|
|
588
|
+
* Forgets all jog accounting (CNCjs connection or CNC port changed): nothing stale is replayed.
|
|
589
|
+
*
|
|
590
|
+
* @returns {void}
|
|
591
|
+
*/
|
|
592
|
+
function resetJogs() {
|
|
593
|
+
jog.inFlight = 0;
|
|
594
|
+
jog.pending = null;
|
|
595
|
+
health.jog.inFlight = 0;
|
|
596
|
+
health.jog.held = false;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Handles a line CNCjs read from the controller: every ok / error reply frees a slot and may release
|
|
601
|
+
* the held jog.
|
|
602
|
+
*
|
|
603
|
+
* @param {*} data - the line (string) as emitted by CNCjs
|
|
604
|
+
* @returns {void}
|
|
605
|
+
*/
|
|
606
|
+
function onControllerLine(data) {
|
|
607
|
+
const line = String(data === null || data === undefined ? '' : data).trim();
|
|
608
|
+
if (line !== 'ok' && line.slice(0, 6).toLowerCase() !== 'error:') return;
|
|
609
|
+
if (jog.inFlight > 0) jog.inFlight--;
|
|
610
|
+
health.jog.inFlight = jog.inFlight;
|
|
611
|
+
jog.lastAck = Date.now();
|
|
612
|
+
flushJog(false);
|
|
613
|
+
health.jog.held = !!jog.pending;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Lines the controller never answers (lost in transit) would keep the counter up for good.
|
|
617
|
+
const jogTimer = setInterval(() => {
|
|
618
|
+
if (jog.inFlight > 0 && Date.now() - jog.lastAck > cfg.jog.stallMs) {
|
|
619
|
+
health.jog.stalls++;
|
|
620
|
+
console.warn('Controller did not acknowledge ' + jog.inFlight + ' gcode line(s) for ' + cfg.jog.stallMs +
|
|
621
|
+
' ms: they may have been lost. [' + health.jog.stalls + ' stall(s) so far]');
|
|
622
|
+
jog.inFlight = 0;
|
|
623
|
+
health.jog.inFlight = 0;
|
|
624
|
+
}
|
|
625
|
+
flushJog(false);
|
|
626
|
+
}, 250);
|
|
627
|
+
|
|
384
628
|
// ---------------------------------------------------------------- CNCjs -> pendant
|
|
385
629
|
/**
|
|
386
630
|
* Converts a value to a number, treating anything non-numeric as 0.
|
|
@@ -469,38 +713,54 @@ function formatStatus(s) {
|
|
|
469
713
|
|
|
470
714
|
let lastLine = '';
|
|
471
715
|
let lastStatusAt = 0;
|
|
716
|
+
let statusQueued = false;
|
|
472
717
|
|
|
473
718
|
/**
|
|
474
719
|
* Sends the current status to the pendant if one is due: forced, at least every status.intervalMs,
|
|
475
|
-
*
|
|
720
|
+
* when the values changed and status.minGapMs has passed, or when one was queued with queueStatus().
|
|
476
721
|
*
|
|
477
722
|
* @param {boolean} [force=false] - send regardless of timing
|
|
478
723
|
* @returns {void}
|
|
479
724
|
*/
|
|
480
725
|
function sendStatus(force) {
|
|
726
|
+
if (!cfg.device.send) return;
|
|
481
727
|
const line = formatStatus(snapshot());
|
|
482
728
|
const now = Date.now();
|
|
729
|
+
const gapOk = now - lastStatusAt >= cfg.status.minGapMs;
|
|
483
730
|
const due = now - lastStatusAt >= cfg.status.intervalMs;
|
|
484
|
-
const changed = line !== lastLine &&
|
|
485
|
-
if (force || due || changed) {
|
|
731
|
+
const changed = line !== lastLine && gapOk;
|
|
732
|
+
if (force || due || changed || (statusQueued && gapOk)) {
|
|
486
733
|
if (sendToPendant(line, changed)) {
|
|
487
734
|
lastLine = line;
|
|
488
735
|
lastStatusAt = now;
|
|
736
|
+
statusQueued = false;
|
|
489
737
|
}
|
|
490
738
|
}
|
|
491
739
|
}
|
|
492
740
|
|
|
741
|
+
/**
|
|
742
|
+
* Asks for a status line to go to the pendant soon: now if status.minGapMs has passed since the last
|
|
743
|
+
* one, otherwise as soon as it has (the periodic timer sends it). Many requests in a row cost one line.
|
|
744
|
+
*
|
|
745
|
+
* @returns {void}
|
|
746
|
+
*/
|
|
747
|
+
function queueStatus() {
|
|
748
|
+
statusQueued = true;
|
|
749
|
+
sendStatus(false);
|
|
750
|
+
}
|
|
751
|
+
|
|
493
752
|
/**
|
|
494
753
|
* Writes one line to the pendant's serial port.
|
|
495
754
|
*
|
|
496
755
|
* @param {string} text - the line, without terminator (a newline is added)
|
|
497
|
-
* @param {boolean} [
|
|
756
|
+
* @param {boolean} [should_log=false] - print the line when -v is on
|
|
498
757
|
* @returns {boolean} false if the pendant port is not open
|
|
499
758
|
*/
|
|
500
|
-
function sendToPendant(text,
|
|
759
|
+
function sendToPendant(text, should_log=false) {
|
|
760
|
+
if (!cfg.device.send) return false;
|
|
501
761
|
const port = pendantPort;
|
|
502
762
|
if (!port || !port.isOpen) return false;
|
|
503
|
-
if(
|
|
763
|
+
if(should_log) log('cncjs -> ', 'pendant', text);
|
|
504
764
|
port.write(text + '\n', (err) => {
|
|
505
765
|
if (err) noteError(err);
|
|
506
766
|
});
|
|
@@ -509,7 +769,7 @@ function sendToPendant(text, shouldLog=true) {
|
|
|
509
769
|
return true;
|
|
510
770
|
}
|
|
511
771
|
|
|
512
|
-
const statusTimer = setInterval(() => sendStatus(false), Math.max(10, Math.min(cfg.status.minGapMs, cfg.status.intervalMs)));
|
|
772
|
+
const statusTimer = setInterval(() => sendStatus(false), Math.max(10, Math.min(100, cfg.status.minGapMs, cfg.status.intervalMs)));
|
|
513
773
|
|
|
514
774
|
// ---------------------------------------------------------------- pendant serial link
|
|
515
775
|
let pendantPort = null;
|
|
@@ -605,9 +865,10 @@ function healthReport() {
|
|
|
605
865
|
ok: health.pendant.connected && cncReady(),
|
|
606
866
|
version: pkg.version,
|
|
607
867
|
uptimeMs: now - health.startedAt,
|
|
608
|
-
pendant: Object.assign({}, health.pendant, { lastRxAgeMs: age(health.pendant.lastRx),
|
|
868
|
+
pendant: Object.assign({}, health.pendant, { sendEnabled: !!cfg.device.send, lastRxAgeMs: age(health.pendant.lastRx),
|
|
609
869
|
lastTxAgeMs: age(health.pendant.lastTx) }),
|
|
610
870
|
cncjs: health.cncjs,
|
|
871
|
+
jog: health.jog,
|
|
611
872
|
lastCommand: health.lastCommand,
|
|
612
873
|
status: snapshot()
|
|
613
874
|
};
|
|
@@ -647,6 +908,8 @@ function shutdown() {
|
|
|
647
908
|
if (shuttingDown) return;
|
|
648
909
|
shuttingDown = true;
|
|
649
910
|
clearInterval(statusTimer);
|
|
911
|
+
clearInterval(jogTimer);
|
|
912
|
+
clearTimeout(cancelTimer);
|
|
650
913
|
clearTimeout(reconnectTimer);
|
|
651
914
|
socket.close();
|
|
652
915
|
if (httpServer) httpServer.close();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cncjs": {
|
|
3
|
+
"port": "/dev/ttyUSB0",
|
|
4
|
+
"baudrate": 115200,
|
|
5
|
+
"controllerType": "Grbl",
|
|
6
|
+
"socketAddress": "localhost",
|
|
7
|
+
"socketPort": 8000,
|
|
8
|
+
"retryInterval": 5000,
|
|
9
|
+
"openTimeout": 5000,
|
|
10
|
+
"maxOpenTries": 3,
|
|
11
|
+
"keepaliveMs": 30000,
|
|
12
|
+
"reopenOnClose": true
|
|
13
|
+
},
|
|
14
|
+
"device": {
|
|
15
|
+
"path": "/dev/pendant",
|
|
16
|
+
"baudRate": 115200,
|
|
17
|
+
"reconnectMs": 3000,
|
|
18
|
+
"settleMs": 2000,
|
|
19
|
+
"send": true,
|
|
20
|
+
"hupcl": false,
|
|
21
|
+
"dtr": null,
|
|
22
|
+
"rts": null
|
|
23
|
+
},
|
|
24
|
+
"status": {
|
|
25
|
+
"format": "grbl",
|
|
26
|
+
"intervalMs": 400,
|
|
27
|
+
"minGapMs": 350,
|
|
28
|
+
"pollAfterCommand": false,
|
|
29
|
+
"decimals": 3
|
|
30
|
+
},
|
|
31
|
+
"jog": {
|
|
32
|
+
"translate": true,
|
|
33
|
+
"maxInFlight": 4,
|
|
34
|
+
"stallMs": 2000,
|
|
35
|
+
"cancelSettleMs": 40,
|
|
36
|
+
"echo": false
|
|
37
|
+
},
|
|
38
|
+
"http": { "enabled": true, "host": "127.0.0.1", "port": 8001 },
|
|
39
|
+
"log": { "max": 500 }
|
|
40
|
+
}
|