dechatter 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/CHANGELOG.md +103 -0
- package/README.md +281 -241
- package/THIRD_PARTY_NOTICES.md +24 -24
- package/package.json +40 -39
- package/web/dechatter.zip +0 -0
- package/web/engine.js +92 -92
- package/web/index.html +81 -81
- package/web/mic-worklet.js +34 -34
- package/web/mic.js +106 -106
- package/web/pyodide/pyodide.js +4 -4
- package/web/widget.css +95 -85
- package/web/widget.js +863 -806
- package/web/wizard.js +354 -190
package/web/widget.js
CHANGED
|
@@ -1,806 +1,863 @@
|
|
|
1
|
-
// dechatter cncjs widget. Everything runs in this page: cncjs is reached over its socket.io
|
|
2
|
-
// API (load the program, watch the job), the microphone is the browser's, and the planning
|
|
3
|
-
// and analysis are dechatter's Python running in a worker (engine.js).
|
|
4
|
-
|
|
5
|
-
import { Mic } from './mic.js';
|
|
6
|
-
import { STEPS, draw, read } from './wizard.js';
|
|
7
|
-
|
|
8
|
-
const params = new URLSearchParams(location.search);
|
|
9
|
-
const token = params.get('token') ?? '';
|
|
10
|
-
const cncjsOrigin = params.get('host') ?? location.origin;
|
|
11
|
-
|
|
12
|
-
const TIMELINE_LIMIT = 5000;
|
|
13
|
-
const LEVEL_MS = 250;
|
|
14
|
-
const SESSION_KEY = 'dechatter.session';
|
|
15
|
-
const DEVICE_KEY = 'dechatter.microphone';
|
|
16
|
-
const $ = (id) => document.getElementById(id);
|
|
17
|
-
|
|
18
|
-
const MACHINE_UNITS = { G20: 'in', G21: 'mm' };
|
|
19
|
-
const UNITS_TEXT = {
|
|
20
|
-
mm: 'Lengths in mm: the machine is in G21.',
|
|
21
|
-
in: 'Lengths in inches: the machine is in G20.',
|
|
22
|
-
unknown: 'Lengths in mm. With the machine connected they follow its G20/G21 mode.',
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let
|
|
44
|
-
let
|
|
45
|
-
let
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
function
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
//
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
render();
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
function
|
|
306
|
-
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
//
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
socket.on('
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if (!
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
if (!mic.recording && round && cnc.workflow === 'running' &&
|
|
437
|
-
if (!mic.open) {
|
|
438
|
-
showError('The job is running but the microphone is not open, so this run cannot be analysed. Enable the microphone and run the round again.');
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
mic.record();
|
|
442
|
-
act('record_start', round.round);
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
if (mic.recording && cnc.workflow === 'idle' && cnc.link === 'ready') {
|
|
446
|
-
const samples = mic.finish();
|
|
447
|
-
act('record_stop', samples, mic.rate, JSON.stringify(timeline));
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
async function loadProgram(round) {
|
|
452
|
-
busy = true;
|
|
453
|
-
render();
|
|
454
|
-
try {
|
|
455
|
-
// Capture has to be running before the job can start; the listener below does not wait.
|
|
456
|
-
await mic.resume();
|
|
457
|
-
const
|
|
458
|
-
await
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
showError(
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
$('
|
|
496
|
-
$('
|
|
497
|
-
$('
|
|
498
|
-
$('mic
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
$('setup-
|
|
529
|
-
$('setup-
|
|
530
|
-
$('setup-
|
|
531
|
-
|
|
532
|
-
$('
|
|
533
|
-
$('
|
|
534
|
-
// Redraw the fields only when the step or the machine's units change, so typing is not
|
|
535
|
-
// interrupted otherwise. A change of units drops an entry that is only half typed.
|
|
536
|
-
const key = `${current}|${editing.step}|${units()}`;
|
|
537
|
-
if ($('setup-form').dataset.key !== key) {
|
|
538
|
-
draw($('setup-form'), step, editing.values, units(), editing
|
|
539
|
-
$('setup-form').dataset.key = key;
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
if (
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
};
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
const
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
),
|
|
647
|
-
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
})
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
$('
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
)
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
};
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
$('
|
|
761
|
-
$('
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
if (
|
|
789
|
-
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1
|
+
// dechatter cncjs widget. Everything runs in this page: cncjs is reached over its socket.io
|
|
2
|
+
// API (load the program, watch the job), the microphone is the browser's, and the planning
|
|
3
|
+
// and analysis are dechatter's Python running in a worker (engine.js).
|
|
4
|
+
|
|
5
|
+
import { Mic } from './mic.js';
|
|
6
|
+
import { STEPS, draw, placehold, read, reveal } from './wizard.js';
|
|
7
|
+
|
|
8
|
+
const params = new URLSearchParams(location.search);
|
|
9
|
+
const token = params.get('token') ?? '';
|
|
10
|
+
const cncjsOrigin = params.get('host') ?? location.origin;
|
|
11
|
+
|
|
12
|
+
const TIMELINE_LIMIT = 5000;
|
|
13
|
+
const LEVEL_MS = 250;
|
|
14
|
+
const SESSION_KEY = 'dechatter.session';
|
|
15
|
+
const DEVICE_KEY = 'dechatter.microphone';
|
|
16
|
+
const $ = (id) => document.getElementById(id);
|
|
17
|
+
|
|
18
|
+
const MACHINE_UNITS = { G20: 'in', G21: 'mm' };
|
|
19
|
+
const UNITS_TEXT = {
|
|
20
|
+
mm: 'Lengths in mm: the machine is in G21.',
|
|
21
|
+
in: 'Lengths in inches: the machine is in G20.',
|
|
22
|
+
unknown: 'Lengths in mm. With the machine connected they follow its G20/G21 mode.',
|
|
23
|
+
};
|
|
24
|
+
const cnc = {
|
|
25
|
+
socket: null,
|
|
26
|
+
link: 'connecting', // connecting | no-port | ready | offline
|
|
27
|
+
port: '',
|
|
28
|
+
workflow: null, // idle | running | paused; null until cncjs reports it
|
|
29
|
+
program: '', // name of the G-code file cncjs has loaded
|
|
30
|
+
pause: null, // { text, settled } while the program waits at an M0, else null
|
|
31
|
+
// mm | in: the machine's G21/G20 mode as cncjs last reported it; null until it has. The
|
|
32
|
+
// wizard shows lengths in it and programs are written in it, so a job never changes it.
|
|
33
|
+
units: null,
|
|
34
|
+
loadedUnits: null, // mm | in: the units of the program this widget last loaded into cncjs
|
|
35
|
+
};
|
|
36
|
+
const mic = new Mic();
|
|
37
|
+
let boot = 'Starting'; // what the engine is loading; null once it is ready
|
|
38
|
+
let fatal = ''; // why the engine could not start; nothing works without it
|
|
39
|
+
let names = []; // sessions stored in this browser
|
|
40
|
+
let current = localStorage.getItem(SESSION_KEY) ?? '';
|
|
41
|
+
let state = null; // the current session, as the engine last reported it
|
|
42
|
+
let editing = null; // { values, defaults, materials, computed, step } while the setup wizard is open, else null
|
|
43
|
+
let timeline = []; // what cncjs reported since the job last started; saved with the recording
|
|
44
|
+
let busy = false; // an engine request is in flight
|
|
45
|
+
let stockChoice = 'same'; // survives redraws: planning on the wrong face position cuts air or too deep
|
|
46
|
+
|
|
47
|
+
// ---- engine (Python in a worker) -------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
const worker = new Worker(new URL('./engine.js', import.meta.url), { type: 'module' });
|
|
50
|
+
const pending = new Map();
|
|
51
|
+
let nextId = 1;
|
|
52
|
+
|
|
53
|
+
worker.onmessage = ({ data }) => {
|
|
54
|
+
if ('boot' in data) {
|
|
55
|
+
boot = data.boot;
|
|
56
|
+
fatal = data.fatal ?? fatal;
|
|
57
|
+
render();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const { resolve, reject } = pending.get(data.id);
|
|
61
|
+
pending.delete(data.id);
|
|
62
|
+
if ('error' in data) {
|
|
63
|
+
reject(new Error(data.error));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
resolve(data.ok);
|
|
67
|
+
};
|
|
68
|
+
worker.onerror = (event) => {
|
|
69
|
+
fatal = `dechatter could not start its engine${event.message ? `: ${event.message}` : ''}. Export the widget again with \`dechatter widget\`.`;
|
|
70
|
+
render();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function engine(method, ...args) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const id = nextId;
|
|
76
|
+
nextId += 1;
|
|
77
|
+
pending.set(id, { resolve, reject });
|
|
78
|
+
worker.postMessage({ id, method, args });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function showError(message) {
|
|
83
|
+
$('error').textContent = message;
|
|
84
|
+
$('error').hidden = !message;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Run one request that answers with the session's new state.
|
|
88
|
+
async function act(method, ...args) {
|
|
89
|
+
busy = true;
|
|
90
|
+
render();
|
|
91
|
+
try {
|
|
92
|
+
state = await engine(method, current, ...args);
|
|
93
|
+
showError('');
|
|
94
|
+
} catch (err) {
|
|
95
|
+
showError(err.message);
|
|
96
|
+
} finally {
|
|
97
|
+
busy = false;
|
|
98
|
+
syncRecording();
|
|
99
|
+
render();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function openSession(name) {
|
|
104
|
+
current = name;
|
|
105
|
+
editing = null;
|
|
106
|
+
state = null;
|
|
107
|
+
localStorage.setItem(SESSION_KEY, name);
|
|
108
|
+
if (name) {
|
|
109
|
+
await act('state');
|
|
110
|
+
}
|
|
111
|
+
render();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function newSession() {
|
|
115
|
+
const name = window.prompt('Name this tool assembly, for example al-10mm-3fl');
|
|
116
|
+
if (!name) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
await engine('create', name.trim());
|
|
121
|
+
names = await engine('names');
|
|
122
|
+
current = name.trim();
|
|
123
|
+
localStorage.setItem(SESSION_KEY, current);
|
|
124
|
+
state = null;
|
|
125
|
+
await openWizard();
|
|
126
|
+
showError('');
|
|
127
|
+
} catch (err) {
|
|
128
|
+
showError(err.message);
|
|
129
|
+
}
|
|
130
|
+
render();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Start the wizard on the session's stored setup. The form is redrawn even when it last
|
|
134
|
+
// showed this same step, so nothing typed into an earlier wizard is left on screen.
|
|
135
|
+
async function openWizard() {
|
|
136
|
+
editing = { ...(await engine('setup_form', current)), computed: null, step: 0 };
|
|
137
|
+
$('setup-form').dataset.key = '';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// What the planner makes of the entries left empty, from what the wizard holds so far.
|
|
141
|
+
// Only shown as placeholders, filled in when the answer comes: the step is already drawn
|
|
142
|
+
// and may hold typing. Values the wizard cannot make sense of yet show none, and an answer
|
|
143
|
+
// overtaken by a later request is dropped.
|
|
144
|
+
async function preview(wizard) {
|
|
145
|
+
wizard.asked = (wizard.asked ?? 0) + 1;
|
|
146
|
+
const asked = wizard.asked;
|
|
147
|
+
let computed = null;
|
|
148
|
+
try {
|
|
149
|
+
computed = await engine('setup_preview', JSON.stringify(wizard.values));
|
|
150
|
+
} catch {
|
|
151
|
+
computed = null;
|
|
152
|
+
}
|
|
153
|
+
if (editing !== wizard || wizard.asked !== asked) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
wizard.computed = computed;
|
|
157
|
+
placehold($('setup-form'), STEPS[wizard.step], units(), wizard);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function goToStep(step) {
|
|
161
|
+
editing.step = Math.min(Math.max(step, 0), STEPS.length - 1);
|
|
162
|
+
render();
|
|
163
|
+
preview(editing);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function editSetup() {
|
|
167
|
+
try {
|
|
168
|
+
await openWizard();
|
|
169
|
+
} catch (err) {
|
|
170
|
+
showError(err.message);
|
|
171
|
+
}
|
|
172
|
+
render();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Lengths are shown in the machine's units; millimetres until cncjs has reported them.
|
|
176
|
+
function units() {
|
|
177
|
+
return cnc.units ?? 'mm';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function keepStep() {
|
|
181
|
+
read($('setup-form'), STEPS[editing.step], editing.values, units());
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function nextStep() {
|
|
185
|
+
reveal($('setup-form'));
|
|
186
|
+
if (!$('setup-form').reportValidity()) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
keepStep();
|
|
190
|
+
if (editing.step < STEPS.length - 1) {
|
|
191
|
+
goToStep(editing.step + 1);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (busy) {
|
|
195
|
+
return; // already saving
|
|
196
|
+
}
|
|
197
|
+
// A job may be running one of this session's programs; a new setup could discard it.
|
|
198
|
+
if (cnc.workflow === 'running' || cnc.workflow === 'paused') {
|
|
199
|
+
showError('A job is running in cncjs. Save the setup once it has ended.');
|
|
200
|
+
render();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const wizard = editing;
|
|
204
|
+
await act('save_setup', JSON.stringify(wizard.values));
|
|
205
|
+
if (editing !== wizard) {
|
|
206
|
+
return; // cancelled, or another session opened, while the save was in flight
|
|
207
|
+
}
|
|
208
|
+
// On a refusal the reason is shown and the wizard stays open on what was typed.
|
|
209
|
+
editing = $('error').hidden ? null : wizard;
|
|
210
|
+
render();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function previousStep() {
|
|
214
|
+
keepStep();
|
|
215
|
+
goToStep(editing.step - 1);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function download(blob, filename) {
|
|
219
|
+
const url = URL.createObjectURL(blob);
|
|
220
|
+
// cncjs sandboxes widget frames without allow-downloads, so the cncjs page itself
|
|
221
|
+
// (same origin) starts the download.
|
|
222
|
+
const host = window.parent.document;
|
|
223
|
+
const link = host.createElement('a');
|
|
224
|
+
link.href = url;
|
|
225
|
+
link.download = filename;
|
|
226
|
+
host.body.append(link);
|
|
227
|
+
link.click();
|
|
228
|
+
link.remove();
|
|
229
|
+
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Both carry the report, whose lengths follow the machine's units (mm until known).
|
|
233
|
+
async function exportSession() {
|
|
234
|
+
try {
|
|
235
|
+
const bytes = await engine('export_zip', current, units());
|
|
236
|
+
download(new Blob([bytes], { type: 'application/zip' }), `${current}.zip`);
|
|
237
|
+
} catch (err) {
|
|
238
|
+
showError(err.message);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function downloadReport() {
|
|
243
|
+
try {
|
|
244
|
+
const page = await engine('report_html', current, units());
|
|
245
|
+
download(new Blob([page], { type: 'text/html' }), `${current}-report.html`);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
showError(err.message);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function importSession(file) {
|
|
252
|
+
try {
|
|
253
|
+
const name = await engine('import_zip', new Uint8Array(await file.arrayBuffer()));
|
|
254
|
+
names = await engine('names');
|
|
255
|
+
await openSession(name);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
showError(err.message);
|
|
258
|
+
}
|
|
259
|
+
render();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ---- microphone ------------------------------------------------------------------------
|
|
263
|
+
|
|
264
|
+
async function openMic(deviceId) {
|
|
265
|
+
try {
|
|
266
|
+
await mic.start(deviceId);
|
|
267
|
+
localStorage.setItem(DEVICE_KEY, deviceId ?? '');
|
|
268
|
+
const devices = await mic.devices();
|
|
269
|
+
$('device').replaceChildren(
|
|
270
|
+
el('option', { value: '' }, 'default microphone'),
|
|
271
|
+
...devices.map((d) => el('option', { value: d.id }, d.label || 'microphone')),
|
|
272
|
+
);
|
|
273
|
+
$('device').value = deviceId ?? '';
|
|
274
|
+
showError('');
|
|
275
|
+
} catch (err) {
|
|
276
|
+
showError(`Microphone: ${err.message}. Allow microphone access for this site, then press Enable microphone.`);
|
|
277
|
+
}
|
|
278
|
+
render();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function renderLevel() {
|
|
282
|
+
const level = mic.open ? mic.levelDb : null;
|
|
283
|
+
$('level').value = level ?? -100;
|
|
284
|
+
$('level-db').textContent = level === null ? (mic.open ? 'no signal: click anywhere in this panel' : '') : `${Math.round(level)} dB`;
|
|
285
|
+
if (mic.recording) {
|
|
286
|
+
const clock = $('recording-clock');
|
|
287
|
+
if (clock) {
|
|
288
|
+
clock.textContent = minutes(Math.round(mic.seconds));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// ---- cncjs -----------------------------------------------------------------------------
|
|
294
|
+
|
|
295
|
+
function loadScript(src) {
|
|
296
|
+
return new Promise((resolve, reject) => {
|
|
297
|
+
const script = document.createElement('script');
|
|
298
|
+
script.src = src;
|
|
299
|
+
script.onload = resolve;
|
|
300
|
+
script.onerror = () => reject(new Error(`cannot load ${src}`));
|
|
301
|
+
document.head.append(script);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function note(event, detail) {
|
|
306
|
+
if (timeline.length < TIMELINE_LIMIT) {
|
|
307
|
+
timeline.push({ t: Date.now() / 1000, event, ...detail });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function setPort(port) {
|
|
312
|
+
// Another port may be another machine: its units are unknown until cncjs reports them.
|
|
313
|
+
if (port !== cnc.port) {
|
|
314
|
+
showIn(null);
|
|
315
|
+
cnc.loadedUnits = null;
|
|
316
|
+
}
|
|
317
|
+
cnc.port = port;
|
|
318
|
+
cnc.link = port ? 'ready' : 'no-port';
|
|
319
|
+
render();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function showIn(next) {
|
|
323
|
+
if (next === cnc.units) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (editing) {
|
|
327
|
+
keepStep();
|
|
328
|
+
}
|
|
329
|
+
cnc.units = next;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async function connectCncjs() {
|
|
333
|
+
try {
|
|
334
|
+
await loadScript(`${cncjsOrigin}/socket.io/socket.io.js`);
|
|
335
|
+
} catch (err) {
|
|
336
|
+
cnc.link = 'offline';
|
|
337
|
+
showError(`${err.message}. Open this widget from inside cncjs.`);
|
|
338
|
+
render();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const socket = window.io.connect(cncjsOrigin, { query: `token=${token}` });
|
|
342
|
+
cnc.socket = socket;
|
|
343
|
+
|
|
344
|
+
socket.on('connect', () => {
|
|
345
|
+
cnc.link = cnc.port ? 'ready' : 'no-port';
|
|
346
|
+
// Ask the cncjs page which port it has open; it answers with a "change" message.
|
|
347
|
+
window.parent.postMessage({ token, action: { type: 'connect' } }, '*');
|
|
348
|
+
render();
|
|
349
|
+
});
|
|
350
|
+
socket.on('disconnect', () => {
|
|
351
|
+
cnc.link = 'offline';
|
|
352
|
+
render();
|
|
353
|
+
});
|
|
354
|
+
socket.on('error', (err) => {
|
|
355
|
+
cnc.link = 'offline';
|
|
356
|
+
showError(`cncjs refused the connection: ${err?.message ?? err}`);
|
|
357
|
+
render();
|
|
358
|
+
});
|
|
359
|
+
socket.on('serialport:open', ({ port }) => setPort(port));
|
|
360
|
+
socket.on('serialport:close', () => setPort(''));
|
|
361
|
+
socket.on('gcode:load', (name) => {
|
|
362
|
+
cnc.program = name;
|
|
363
|
+
render();
|
|
364
|
+
});
|
|
365
|
+
socket.on('gcode:unload', () => {
|
|
366
|
+
cnc.program = '';
|
|
367
|
+
cnc.loadedUnits = null;
|
|
368
|
+
render();
|
|
369
|
+
});
|
|
370
|
+
socket.on('sender:status', (status) => {
|
|
371
|
+
cnc.program = status.name ?? cnc.program;
|
|
372
|
+
note('sender', { sent: status.sent, received: status.received, total: status.total });
|
|
373
|
+
// A manual-spindle program stops at "M0 (set the spindle to N rpm, then resume)". cncjs
|
|
374
|
+
// reports the pause as soon as it sends the line; the machine has only stopped once
|
|
375
|
+
// everything sent was acknowledged.
|
|
376
|
+
const reason = status.hold ? status.holdReason : null;
|
|
377
|
+
const pause = reason?.data === 'M0'
|
|
378
|
+
? { text: /\(([^)]*)\)/.exec(reason.msg ?? '')?.[1] ?? 'program pause', settled: status.received >= status.sent }
|
|
379
|
+
: null;
|
|
380
|
+
if (JSON.stringify(pause) !== JSON.stringify(cnc.pause)) {
|
|
381
|
+
cnc.pause = pause;
|
|
382
|
+
render();
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
socket.on('workflow:state', (workflow) => {
|
|
386
|
+
if (workflow === 'running' && cnc.workflow !== 'paused') {
|
|
387
|
+
timeline = [];
|
|
388
|
+
}
|
|
389
|
+
cnc.workflow = workflow;
|
|
390
|
+
note('workflow', { state: workflow });
|
|
391
|
+
syncRecording();
|
|
392
|
+
render();
|
|
393
|
+
});
|
|
394
|
+
socket.on('controller:state', (_type, controller) => {
|
|
395
|
+
showIn(MACHINE_UNITS[controller?.parserstate?.modal?.units] ?? cnc.units);
|
|
396
|
+
render();
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
window.addEventListener('message', (event) => {
|
|
401
|
+
const { token: theirs, action } = { ...event.data };
|
|
402
|
+
if (theirs !== token || action?.type !== 'change') {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const port = action.payload?.port ?? '';
|
|
406
|
+
if (port) {
|
|
407
|
+
// Joining an open port makes cncjs replay its sender and workflow state to this socket.
|
|
408
|
+
cnc.socket.emit('open', port, {});
|
|
409
|
+
}
|
|
410
|
+
setPort(port);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// ---- recording follows the job ---------------------------------------------------------
|
|
414
|
+
|
|
415
|
+
// cncjs holds this round's program as this widget loaded it, in the units the machine
|
|
416
|
+
// reports now. After a reload of the widget the units it was loaded in are unknown.
|
|
417
|
+
function loadedHere(round) {
|
|
418
|
+
return cnc.program === round.program && cnc.loadedUnits !== null && cnc.loadedUnits === cnc.units;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function plannedRound() {
|
|
422
|
+
const last = state?.rounds.at(-1);
|
|
423
|
+
return last && !last.analysed ? last : null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Record exactly while cncjs runs the planned round's program.
|
|
427
|
+
function syncRecording() {
|
|
428
|
+
if (!state || busy) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const round = plannedRound();
|
|
432
|
+
if (!mic.recording && round && cnc.workflow === 'running' && cnc.program === round.program && !loadedHere(round)) {
|
|
433
|
+
showError('This run is not recorded: the program in cncjs may be in other units than the machine is in now. Stop the job, load the program again from here, then run it.');
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (!mic.recording && round && cnc.workflow === 'running' && loadedHere(round)) {
|
|
437
|
+
if (!mic.open) {
|
|
438
|
+
showError('The job is running but the microphone is not open, so this run cannot be analysed. Enable the microphone and run the round again.');
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
mic.record();
|
|
442
|
+
act('record_start', round.round);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (mic.recording && cnc.workflow === 'idle' && cnc.link === 'ready') {
|
|
446
|
+
const samples = mic.finish();
|
|
447
|
+
act('record_stop', samples, mic.rate, JSON.stringify(timeline));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async function loadProgram(round) {
|
|
452
|
+
busy = true;
|
|
453
|
+
render();
|
|
454
|
+
try {
|
|
455
|
+
// Capture has to be running before the job can start; the listener below does not wait.
|
|
456
|
+
await mic.resume();
|
|
457
|
+
const programUnits = cnc.units;
|
|
458
|
+
const gcode = await engine('program', current, round.round, programUnits);
|
|
459
|
+
await new Promise((resolve, reject) => {
|
|
460
|
+
cnc.socket.emit('command', cnc.port, 'gcode:load', round.program, gcode, {}, (err) =>
|
|
461
|
+
err ? reject(new Error(`cncjs could not load the program: ${err.message ?? err}`)) : resolve(),
|
|
462
|
+
);
|
|
463
|
+
});
|
|
464
|
+
cnc.program = round.program;
|
|
465
|
+
cnc.loadedUnits = programUnits;
|
|
466
|
+
showError('');
|
|
467
|
+
} catch (err) {
|
|
468
|
+
showError(err.message);
|
|
469
|
+
} finally {
|
|
470
|
+
busy = false;
|
|
471
|
+
render();
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// ---- rendering -------------------------------------------------------------------------
|
|
476
|
+
|
|
477
|
+
function el(tag, props = {}, ...children) {
|
|
478
|
+
const node = Object.assign(document.createElement(tag), props);
|
|
479
|
+
node.append(...children);
|
|
480
|
+
return node;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function button(label, onclick, props = {}) {
|
|
484
|
+
return el('button', { onclick, ...props }, label);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const LINK_TEXT = {
|
|
488
|
+
connecting: 'connecting',
|
|
489
|
+
'no-port': 'connected; open a serial port in cncjs',
|
|
490
|
+
offline: 'not connected',
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
function renderLinks() {
|
|
494
|
+
const job = cnc.workflow ?? 'state unknown';
|
|
495
|
+
$('cncjs').textContent = cnc.link === 'ready' ? `${cnc.port}, ${job}` : LINK_TEXT[cnc.link];
|
|
496
|
+
$('device').hidden = !mic.open;
|
|
497
|
+
$('level').hidden = !mic.open;
|
|
498
|
+
$('enable-mic').hidden = mic.open;
|
|
499
|
+
$('mic-warning').hidden = mic.processing.length === 0;
|
|
500
|
+
$('mic-warning').textContent = `The browser kept ${mic.processing.join(', ')} on. Verdicts may be wrong; try another microphone or browser.`;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function renderSessions() {
|
|
504
|
+
const select = $('sessions');
|
|
505
|
+
select.replaceChildren(
|
|
506
|
+
el('option', { value: '' }, names.length ? 'choose a session' : 'no sessions yet'),
|
|
507
|
+
...names.map((name) => el('option', { value: name }, name)),
|
|
508
|
+
);
|
|
509
|
+
select.value = names.includes(current) ? current : '';
|
|
510
|
+
select.disabled = busy || mic.recording;
|
|
511
|
+
$('summary').textContent = state?.summary ?? '';
|
|
512
|
+
for (const id of ['edit-setup', 'export']) {
|
|
513
|
+
$(id).disabled = !state || busy || mic.recording;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function minutes(seconds) {
|
|
518
|
+
return seconds < 90 ? `${seconds} s` : `${Math.round(seconds / 60)} min`;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function renderSetup() {
|
|
522
|
+
$('setup').hidden = editing === null;
|
|
523
|
+
if (editing === null) {
|
|
524
|
+
$('setup-form').dataset.key = '';
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const step = STEPS[editing.step];
|
|
528
|
+
$('setup-title').textContent = `Setup ${editing.step + 1} of ${STEPS.length}: ${step.title}`;
|
|
529
|
+
$('setup-hint').textContent = step.hint;
|
|
530
|
+
$('setup-back').hidden = editing.step === 0;
|
|
531
|
+
$('setup-next').textContent = editing.step === STEPS.length - 1 ? 'Save' : 'Next';
|
|
532
|
+
$('setup-next').disabled = busy;
|
|
533
|
+
$('units').textContent = UNITS_TEXT[cnc.units ?? 'unknown'];
|
|
534
|
+
// Redraw the fields only when the step or the machine's units change, so typing is not
|
|
535
|
+
// interrupted otherwise. A change of units drops an entry that is only half typed.
|
|
536
|
+
const key = `${current}|${editing.step}|${units()}`;
|
|
537
|
+
if ($('setup-form').dataset.key !== key) {
|
|
538
|
+
draw($('setup-form'), step, editing.values, units(), editing);
|
|
539
|
+
$('setup-form').dataset.key = key;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// Planning on the wrong face position cuts air or too deep: the choice survives redraws.
|
|
544
|
+
function stockSelect() {
|
|
545
|
+
const stock = el('select', {}, el('option', { value: 'same' }, 'same block'), el('option', { value: 'fresh' }, 'fresh block, face at Y0'));
|
|
546
|
+
stock.value = stockChoice;
|
|
547
|
+
stock.onchange = () => {
|
|
548
|
+
stockChoice = stock.value;
|
|
549
|
+
};
|
|
550
|
+
return stock;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function renderStep() {
|
|
554
|
+
const step = $('step');
|
|
555
|
+
step.replaceChildren();
|
|
556
|
+
step.hidden = !state || editing !== null;
|
|
557
|
+
if (step.hidden) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const round = plannedRound();
|
|
561
|
+
|
|
562
|
+
if (mic.recording) {
|
|
563
|
+
step.append(
|
|
564
|
+
el('p', { className: 'recording' }, `Recording round ${state.recording_round ?? ''}: `, el('span', { id: 'recording-clock' }, '0 s')),
|
|
565
|
+
el('p', {}, 'Feed-hold and stop the job in cncjs if a cut gets violent. The passes that ran still count.'),
|
|
566
|
+
...(cnc.pause ? [el('p', { className: 'instruction' }, cnc.pause.settled ? `Now ${cnc.pause.text} in cncjs.` : 'Wait for the machine to stop.')] : []),
|
|
567
|
+
);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
if (busy) {
|
|
572
|
+
step.append(el('p', {}, 'Working…'));
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (state.discarded_round !== null) {
|
|
577
|
+
step.append(el('p', {}, `Round ${state.discarded_round} had not been run and was planned from the old setup, so it was discarded.`));
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if (state.next === 'converged') {
|
|
581
|
+
step.append(
|
|
582
|
+
el('p', {}, 'Converged: no further cut is expected to add removal rate. The best stable cuts are below.'),
|
|
583
|
+
el('p', {}, button('Download the report', downloadReport, { className: 'primary' }), ' to print and keep with the tool.'),
|
|
584
|
+
);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (state.next === 'sweep') {
|
|
589
|
+
const best = state.best[0];
|
|
590
|
+
const stock = stockSelect();
|
|
591
|
+
step.append(
|
|
592
|
+
el('p', {}, 'Converged: no further cut is expected to add removal rate. The best stable cuts are below.'),
|
|
593
|
+
el('p', {}, `One more round can find how hard the best cut (${best.rpm} rpm, ${best.width_mm.toFixed(2)} mm wide, ${best.depth_mm} mm deep) can be pushed: the same cut at rising chip loads, lightest first. Its cuts do not change the map.`),
|
|
594
|
+
el('p', {}, button('Plan the chip-load sweep', async () => {
|
|
595
|
+
await act('plan_sweep', stockChoice);
|
|
596
|
+
stockChoice = 'same';
|
|
597
|
+
}, { className: 'primary' }), ' on the ', stock),
|
|
598
|
+
el('p', {}, button('Download the report', downloadReport), ' as it stands.'),
|
|
599
|
+
);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if (state.next === 'setup') {
|
|
604
|
+
step.append(
|
|
605
|
+
el('p', {}, 'This session still holds the example setup, not your machine. Fill it in before planning.'),
|
|
606
|
+
el('p', {}, button('Open the setup wizard', editSetup, { className: 'primary' })),
|
|
607
|
+
);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (state.next === 'plan') {
|
|
612
|
+
const first = state.rounds.length === 0;
|
|
613
|
+
const stock = stockSelect();
|
|
614
|
+
const shallower = !first && state.next_depth_mm !== state.depth_mm;
|
|
615
|
+
const label = `Plan round ${state.rounds.length + 1}${shallower ? ` at ${state.next_depth_mm} mm depth` : ''}`;
|
|
616
|
+
const plan = button(label, async () => {
|
|
617
|
+
await act('plan', first ? 'same' : stockChoice);
|
|
618
|
+
stockChoice = 'same';
|
|
619
|
+
}, { className: 'primary' });
|
|
620
|
+
const next = shallower
|
|
621
|
+
? `${state.depth_mm} mm depth is settled. Next come cuts ${state.next_depth_mm} mm deep, starting from the widths that were stable.`
|
|
622
|
+
: 'Plan the next cuts from what was measured.';
|
|
623
|
+
step.append(el('p', {}, first ? 'Plan a coarse grid of test cuts.' : next));
|
|
624
|
+
step.append(el('p', {}, plan, ...(first ? [] : [' on the ', stock])));
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const tests = round.passes.filter((p) => p.kind === 'test').length;
|
|
629
|
+
const speedChanges = (r) => r.passes.filter((p, i) => i === 0 || p.rpm !== r.passes[i - 1].rpm).length;
|
|
630
|
+
const loaded = loadedHere(round);
|
|
631
|
+
// The name matches, but the units it was loaded in differ or are not known: load it again.
|
|
632
|
+
const unsure = cnc.program === round.program && !loaded;
|
|
633
|
+
// The program is written in the machine's units, so they have to be known first.
|
|
634
|
+
const ready = cnc.link === 'ready' && cnc.workflow === 'idle' && cnc.units !== null && mic.open;
|
|
635
|
+
const load = button(loaded ? 'Reload program' : 'Load into cncjs', () => loadProgram(round), { className: loaded ? '' : 'primary', disabled: !ready });
|
|
636
|
+
// cncjs reports the mode from the controller's parser state. Until it does, the operator
|
|
637
|
+
// can say which it is: the program sets that mode itself, so a wrong choice cannot
|
|
638
|
+
// misplace a cut, only leave the machine in the other mode afterwards.
|
|
639
|
+
const declare = el('select', {}, el('option', { value: '' }, 'machine units?'), el('option', { value: 'mm' }, 'mm (G21)'), el('option', { value: 'in' }, 'inches (G20)'));
|
|
640
|
+
declare.onchange = () => {
|
|
641
|
+
showIn(declare.value || null);
|
|
642
|
+
render();
|
|
643
|
+
};
|
|
644
|
+
const unknownUnits = mic.open && cnc.link === 'ready' && cnc.units === null;
|
|
645
|
+
step.append(
|
|
646
|
+
el('p', {}, `Round ${round.round}: ${tests} test cuts ${round.depth_mm} mm deep, about ${minutes(round.run_time_s)} plus rapids. The front face ends at Y${round.face_y_end_mm}.`),
|
|
647
|
+
el('ol', {},
|
|
648
|
+
el('li', {}, 'Zero X at the left end of the stock, Y on its front face, Z on top.'),
|
|
649
|
+
el('li', {}, load, ...(unknownUnits ? [' cncjs has not reported G20 or G21 yet; the machine is in ', declare] : []), mic.open ? '' : ' (enable the microphone first)'),
|
|
650
|
+
el('li', {}, loaded ? 'Press play in cncjs. Recording starts and stops with the job. Keep this tab open.' : 'Press play in cncjs.'),
|
|
651
|
+
...(state.spindle === 'manual' ? [el('li', {}, `The program pauses before each of its ${speedChanges(round)} spindle speeds. Set the speed shown here, then resume in cncjs.`)] : []),
|
|
652
|
+
),
|
|
653
|
+
);
|
|
654
|
+
if (unsure) {
|
|
655
|
+
step.append(el('p', { className: 'instruction' }, 'cncjs holds this round\'s program, but it was loaded in other units than the machine is in now, or before this widget was reloaded. Load it again before pressing play; a run of it as it is will not be recorded.'));
|
|
656
|
+
}
|
|
657
|
+
if (round.has_recording) {
|
|
658
|
+
step.append(el('p', {}, 'The last run could not be analysed. Run the round again, or ', button('Analyse the last recording again', () => act('analyze', round.round))));
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function verdictCell(round, pass) {
|
|
663
|
+
if (pass.kind !== 'test') {
|
|
664
|
+
return el('td', {}, pass.kind);
|
|
665
|
+
}
|
|
666
|
+
if (!round.analysed) {
|
|
667
|
+
return el('td', {}, '');
|
|
668
|
+
}
|
|
669
|
+
const select = el('select', { disabled: busy },
|
|
670
|
+
...(pass.verdict ? [] : [el('option', { value: '' }, 'not heard')]),
|
|
671
|
+
el('option', { value: 'stable' }, pass.status === 'aborted' ? 'stable (aborted)' : 'stable'),
|
|
672
|
+
el('option', { value: 'unstable' }, pass.status === 'aborted' ? 'chatter (aborted)' : 'chatter'),
|
|
673
|
+
);
|
|
674
|
+
select.value = pass.verdict ?? '';
|
|
675
|
+
select.title = pass.chatter_hz ? `peak ${Math.round(pass.chatter_hz)} Hz, ${pass.prominence_db} dB over the floor` : '';
|
|
676
|
+
select.onchange = () => act('set_verdict', round.round, pass.id, select.value);
|
|
677
|
+
return el('td', { className: pass.verdict ?? '' }, select);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function renderRound() {
|
|
681
|
+
const round = editing === null ? state?.rounds.at(-1) : null;
|
|
682
|
+
$('round').hidden = !round;
|
|
683
|
+
if (!round) {
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const kind = round.planner === 'sweep' ? ' (chip-load sweep)' : '';
|
|
687
|
+
$('round-title').textContent = `Round ${round.round}${kind}${round.analysed ? ' results' : ''}, ${round.depth_mm} mm deep`;
|
|
688
|
+
$('passes').replaceChildren(
|
|
689
|
+
...round.passes.map((p) =>
|
|
690
|
+
el('tr', { className: p.kind === 'test' ? '' : 'prep' },
|
|
691
|
+
el('td', {}, String(p.id)),
|
|
692
|
+
el('td', {}, String(p.rpm)),
|
|
693
|
+
el('td', {}, p.width_mm.toFixed(2)),
|
|
694
|
+
el('td', {}, String(Math.round(p.feed_mm_min))),
|
|
695
|
+
el('td', {}, p.p_stable === null ? '' : p.p_stable.toFixed(2)),
|
|
696
|
+
verdictCell(round, p),
|
|
697
|
+
),
|
|
698
|
+
),
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function renderMap() {
|
|
703
|
+
// A depth that has no cuts of its own yet still has a map: what deeper cuts showed stable.
|
|
704
|
+
$('map-section').hidden = editing !== null || !state || !state.rounds.some((r) => r.analysed);
|
|
705
|
+
if ($('map-section').hidden) {
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
$('map-title').textContent = `Stability map at ${state.depth_mm} mm depth`;
|
|
709
|
+
const { speeds, widths, p } = state.map;
|
|
710
|
+
const canvas = $('map');
|
|
711
|
+
const ctx = canvas.getContext('2d');
|
|
712
|
+
const pad = { left: 40, bottom: 16, top: 4, right: 4 };
|
|
713
|
+
const w = canvas.width - pad.left - pad.right;
|
|
714
|
+
const h = canvas.height - pad.top - pad.bottom;
|
|
715
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
716
|
+
|
|
717
|
+
const cellW = w / speeds.length;
|
|
718
|
+
const cellH = h / widths.length;
|
|
719
|
+
speeds.forEach((_, s) => {
|
|
720
|
+
widths.forEach((_, i) => {
|
|
721
|
+
ctx.fillStyle = `hsl(${Math.round(120 * p[s][i])} 65% 60%)`;
|
|
722
|
+
ctx.fillRect(pad.left + s * cellW, pad.top + h - (i + 1) * cellH, cellW + 0.5, cellH + 0.5);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
const x = (rpm) => pad.left + ((rpm - speeds[0]) / (speeds.at(-1) - speeds[0])) * (w - cellW) + cellW / 2;
|
|
727
|
+
const y = (width) => pad.top + h - ((width - widths[0]) / (widths.at(-1) - widths[0])) * (h - cellH) - cellH / 2;
|
|
728
|
+
ctx.lineWidth = 1.5;
|
|
729
|
+
ctx.strokeStyle = '#000';
|
|
730
|
+
for (const o of state.observations) {
|
|
731
|
+
const [cx, cy] = [x(o.rpm), y(o.width_mm)];
|
|
732
|
+
ctx.beginPath();
|
|
733
|
+
if (o.verdict === 'stable') {
|
|
734
|
+
ctx.fillStyle = '#fff';
|
|
735
|
+
ctx.arc(cx, cy, 3, 0, 2 * Math.PI);
|
|
736
|
+
ctx.fill();
|
|
737
|
+
} else {
|
|
738
|
+
ctx.moveTo(cx - 3, cy - 3);
|
|
739
|
+
ctx.lineTo(cx + 3, cy + 3);
|
|
740
|
+
ctx.moveTo(cx + 3, cy - 3);
|
|
741
|
+
ctx.lineTo(cx - 3, cy + 3);
|
|
742
|
+
}
|
|
743
|
+
ctx.stroke();
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
ctx.fillStyle = '#777';
|
|
747
|
+
ctx.font = '10px Arial';
|
|
748
|
+
ctx.textBaseline = 'top';
|
|
749
|
+
ctx.textAlign = 'left';
|
|
750
|
+
ctx.fillText(`${speeds[0]} rpm`, pad.left, pad.top + h + 3);
|
|
751
|
+
ctx.textAlign = 'right';
|
|
752
|
+
ctx.fillText(String(speeds.at(-1)), pad.left + w, pad.top + h + 3);
|
|
753
|
+
ctx.fillText(`${widths.at(-1)}`, pad.left - 3, pad.top);
|
|
754
|
+
ctx.textBaseline = 'bottom';
|
|
755
|
+
ctx.fillText(`${widths[0]} mm`, pad.left - 3, pad.top + h);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
function renderBest() {
|
|
759
|
+
const best = editing === null ? (state?.best ?? []) : [];
|
|
760
|
+
$('best-section').hidden = best.length === 0;
|
|
761
|
+
$('best').replaceChildren(
|
|
762
|
+
...best.map((b) =>
|
|
763
|
+
el('tr', {},
|
|
764
|
+
el('td', {}, String(b.rpm)),
|
|
765
|
+
el('td', {}, `${b.width_mm.toFixed(2)} mm`),
|
|
766
|
+
el('td', {}, `${b.depth_mm} mm`),
|
|
767
|
+
el('td', {}, String(b.feed_mm_min)),
|
|
768
|
+
el('td', {}, b.mrr_cm3_min.toFixed(2)),
|
|
769
|
+
),
|
|
770
|
+
),
|
|
771
|
+
);
|
|
772
|
+
const sweep = state?.sweep;
|
|
773
|
+
$('sweep-result').hidden = !sweep || best.length === 0;
|
|
774
|
+
if (!sweep) {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
const where = `At ${sweep.rpm} rpm, ${sweep.width_mm.toFixed(2)} mm wide and ${sweep.depth_mm} mm deep`;
|
|
778
|
+
$('sweep-result').textContent = sweep.chip_load_mm === null
|
|
779
|
+
? `Chip-load sweep: ${where.toLowerCase()}, even its lightest pass chattered. Keep the session's ${sweep.session_chip_load_mm} mm per tooth.`
|
|
780
|
+
: `Chip-load sweep: ${where.toLowerCase()}, the cut stayed stable up to ${sweep.chip_load_mm} mm per tooth (the session used ${sweep.session_chip_load_mm}): feed ${sweep.feed_mm_min} mm/min, ${sweep.mrr_cm3_min.toFixed(2)} cm³/min.`;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
function render() {
|
|
784
|
+
$('boot').hidden = boot === null && !fatal;
|
|
785
|
+
$('boot').className = fatal ? 'error' : 'muted';
|
|
786
|
+
$('boot').textContent = fatal || (boot === null ? '' : `${boot}. The first load takes a few seconds.`);
|
|
787
|
+
$('main').hidden = boot !== null || Boolean(fatal);
|
|
788
|
+
if ($('main').hidden) {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
renderSessions();
|
|
792
|
+
renderLinks();
|
|
793
|
+
renderSetup();
|
|
794
|
+
renderStep();
|
|
795
|
+
renderRound();
|
|
796
|
+
renderMap();
|
|
797
|
+
renderBest();
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// ---- wiring ----------------------------------------------------------------------------
|
|
801
|
+
|
|
802
|
+
$('sessions').onchange = () => openSession($('sessions').value);
|
|
803
|
+
$('new-session').onclick = newSession;
|
|
804
|
+
$('edit-setup').onclick = editSetup;
|
|
805
|
+
$('setup-back').onclick = previousStep;
|
|
806
|
+
$('setup-form').onsubmit = (event) => {
|
|
807
|
+
event.preventDefault();
|
|
808
|
+
nextStep();
|
|
809
|
+
};
|
|
810
|
+
$('cancel-setup').onclick = () => {
|
|
811
|
+
editing = null;
|
|
812
|
+
showError('');
|
|
813
|
+
render();
|
|
814
|
+
};
|
|
815
|
+
$('export').onclick = exportSession;
|
|
816
|
+
$('download-report').onclick = downloadReport;
|
|
817
|
+
$('import').onclick = () => $('import-file').click();
|
|
818
|
+
$('import-file').onchange = () => {
|
|
819
|
+
const [file] = $('import-file').files;
|
|
820
|
+
$('import-file').value = '';
|
|
821
|
+
if (file) {
|
|
822
|
+
importSession(file);
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
$('enable-mic').onclick = () => openMic(localStorage.getItem(DEVICE_KEY) || undefined);
|
|
826
|
+
$('device').onchange = () => openMic($('device').value || undefined);
|
|
827
|
+
|
|
828
|
+
// Tell the cncjs page how tall the widget wants to be.
|
|
829
|
+
new ResizeObserver(() => {
|
|
830
|
+
const { scrollHeight, scrollWidth } = document.body;
|
|
831
|
+
window.parent.postMessage({ token, action: { type: 'resize', payload: { scrollHeight, scrollWidth } } }, '*');
|
|
832
|
+
}).observe(document.body);
|
|
833
|
+
|
|
834
|
+
// Browsers can hold audio capture back until the user interacts with the page, for instance
|
|
835
|
+
// after a reload. Any click, tap or key in the widget lets it run. Touch only counts as
|
|
836
|
+
// interaction when the finger lifts, hence pointerup as well.
|
|
837
|
+
for (const type of ['pointerdown', 'pointerup', 'keydown']) {
|
|
838
|
+
document.addEventListener(type, () => {
|
|
839
|
+
mic.resume().catch((err) => console.warn(`dechatter: microphone did not resume: ${err.message}`));
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// Leaving mid-recording loses the run: the samples are only in this page.
|
|
844
|
+
window.addEventListener('beforeunload', (event) => {
|
|
845
|
+
if (mic.recording) {
|
|
846
|
+
event.preventDefault();
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
|
|
850
|
+
render();
|
|
851
|
+
connectCncjs();
|
|
852
|
+
openMic(localStorage.getItem(DEVICE_KEY) || undefined);
|
|
853
|
+
setInterval(renderLevel, LEVEL_MS);
|
|
854
|
+
|
|
855
|
+
try {
|
|
856
|
+
names = await engine('names');
|
|
857
|
+
if (!names.includes(current)) {
|
|
858
|
+
current = '';
|
|
859
|
+
}
|
|
860
|
+
await openSession(current);
|
|
861
|
+
} catch (err) {
|
|
862
|
+
showError(err.message);
|
|
863
|
+
}
|