forked from FabMo/FabMo-Engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathg2.js
962 lines (856 loc) · 23.7 KB
/
g2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
var SerialPort = require("serialport");
var fs = require("fs");
var events = require('events');
var async = require('async');
var util = require('util');
var Queue = require('./util').Queue;
var Watchdog = require('./util').Watchdog;
var log = require('./log').logger('g2');
var process = require('process');
var jsesc = require('jsesc');
var stream = require('stream');
var Q = require('q');
var LineNumberer = require('./util').LineNumberer
// Values of the **stat** field that is returned from G2 status reports
var STAT_INIT = 0;
var STAT_READY = 1;
var STAT_ALARM = 2;
var STAT_STOP = 3;
var STAT_END = 4;
var STAT_RUNNING = 5;
var STAT_HOLDING = 6;
var STAT_PROBE = 7;
var STAT_CYCLING = 8;
var STAT_HOMING = 9;
var STAT_INTERLOCK = 11;
var STAT_SHUTDOWN = 12;
var STAT_PANIC = 13;
// Should take no longer than CMD_TIMEOUT to do a get or a set operation
var CMD_TIMEOUT = 10000;
var EXPECT_TIMEOUT = 300000;
var _promiseCounter = 1;
var THRESH = 1
var pat = /s*(G(28|38)\.\d|G2(0|1))/g
// Error codes defined by G2
// See https://github.com/synthetos/g2/blob/edge/TinyG2/tinyg2.h for the latest error codes and messages
try {
var G2_ERRORS = JSON.parse(fs.readFileSync('./data/g2_errors.json','utf8'));
} catch(e) {
var G2_ERRORS = {};
}
// A cycle context is created when you run a stream, and is a way to access driver events in the context of the current run
function CycleContext(driver, st, promise) {
this.done = false;
this._firmed = false;
this._driver = driver;
this._stream = st;
this._promise = promise.then(function(value) {
this.firm(); // Firm the tool
this.finish();
}.bind(this));
this.eventHandlers = {}; // eventname -> [listener]
this.eventQueue = {}; // eventname -> {f : listener, data : data to pass to listener}
}
// A cycle context is "firmed" when it has turned over itself as a promise through the then() call, or when the
// run finishes, whichever comes first.
CycleContext.prototype.firm = function() {
if(this.firmed) { return; }
log.debug("Firming the cycle context.")
try {
for(var event in this.eventQueue) {
var handlers = this.eventQueue[event];
for(var i=0; i<handlers.length; i++) {
handlers[i].f(handlers[i].data);
}
}
this.firmed = true;
} catch(e) {
delete this.eventQueue;
throw e;
}
delete this.eventQueue;
}
// Bind the listener to the provided event name.
// Events bound in this way are queued, so if any have occurred between the beginning of the run and when
// the binding occurs, they will be triggered when the cycle is firmed
CycleContext.prototype.on = function(event, f) {
log.debug("Binding to the " + event + " event in the cycle context: " + f);
if(event in this.eventHandlers) {
this.eventHandlers[event].push(f);
} else {
this.eventHandlers[event] = [f];
}
return this;
}
// Return a promise that resolves when the cycle is complete (Q Promises)
CycleContext.prototype.then = function(f) {
this.firm();
return this._promise.then(function() {
return f();
});
}
CycleContext.prototype.finish = function() {
log.debug("Finishing up the cycle context.")
// dunno
}
// Emit the provided data to all the listeners to the subscribed event
CycleContext.prototype.emit = function(event, data) {
var handlers = this.eventHandlers[event];
if(handlers) {
for(var i=0; i<handlers.length; i++) {
handlers[i](data);
}
}
}
CycleContext.prototype.pause = function() {
this._stream.pause();
}
CycleContext.prototype.resume = function() {
this._stream.resume();
}
// G2 Constructor
function G2() {
this._currentData = [];
this._currentGCodeData = [];
this.g2_status = {'stat':null, 'posx':0, 'posy':0, 'posz':0};
this.status = {'stat':'idle', 'posx':0, 'posy':0, 'posz':0};
this._seen_ready = false;
this.gcode_queue = new Queue();
this.command_queue = new Queue();
this.pause_flag = false;
this.connected = false;
// Feedhold/flush
this.quit_pending = false;
this.stat = null;
this.hold = null;
// Readers and callbacks
this.expectations = [];
this.readers = {};
// Members related to streaming
this.qtotal = 0;
this.flooded = false;
this.send_rate = 1;
this.lines_sent = 0;
this.context = null;
// Event emitter inheritance and behavior setup
events.EventEmitter.call(this);
this.setMaxListeners(50);
this.lines_to_send = 4;
this._ignored_responses = 0;
this._primed = false;
this._streamDone = false;
this.lineBuffer = [];
}
util.inherits(G2, events.EventEmitter);
G2.prototype._createCycleContext = function() {
if(this.context) {
throw new Error("Cannot create a new cycle context. One already exists.");
}
var st = new stream.PassThrough();
st.setEncoding('utf8');
this._streamDone = false;
this.lineBuffer = []
// TODO factor this out
st.write('G90\n')
st.write('M100 ({out4:1})\n')
st.on('data', function(chunk) {
chunk = chunk.toString();
var newLines = false;
for(var i=0; i<chunk.length; i++) {
ch = chunk[i];
this.lineBuffer.push(ch);
if(ch === '\n') {
newLines = true;
var s = this.lineBuffer.join('').trim();
this.gcode_queue.enqueue(s);
if(this.gcode_queue.getLength() >= 10) {
this._primed = true;
}
this.lineBuffer = [];
}
}
if(newLines) {
this.sendMore();
}
}.bind(this));
st.on('end', function() {
this._primed = true;
this._streamDone = true;
// TODO factor this out
if(!this.quit_pending) {
this.gcode_queue.enqueue('M30');
}
this.sendMore();
log.debug("Stream END event.")
}.bind(this));
st.on('pipe', function() {
log.debug("Stream PIPE event");
})
var promise = this._createStatePromise([STAT_END]).then(function() {
this.context = null;
this._primed = false;
return this;
}.bind(this))
var ctx = new CycleContext(this, st, promise);
this.context = ctx;
}
// Actually open the serial port and configure G2 based on stored settings
G2.prototype.connect = function(path, callback) {
// Store paths for safe keeping
this._serialPath = path;
// Open both ports
log.info('Opening G2 port: ' + this._serialPath);
this._serialPort = new SerialPort(this._serialPath, {flowcontrol: ['RTSCTS'], autoOpen:false});
this._serialToken = 'S';
// Handle errors
this._serialPort.on('error', this.onSerialError.bind(this));
this._serialPort.on('close', this.onSerialClose.bind(this));
// The control port is the only one to truly handle incoming data
this._serialPort.on('data', this.onData.bind(this));
this.once('ready', function() {
this.connected = true;
this._write('\x04\n', function() {
callback(null, this);
}.bind(this));
}.bind(this));
this._serialPort.open(function(error) {
if(error) {
log.error("ERROR OPENING CONTROL PORT " + error );
return callback(error);
} else {
log.info("G2 Port Opened.")
}
}.bind(this));
};
G2.prototype.disconnect = function(callback) {
this._serialPort.close(callback);
};
// Log serial errors. Most of these are exit-able offenses, though.
G2.prototype.onSerialError = function(data) {
log.error(new Error('There was a serial error'))
log.error(data)
};
G2.prototype.onSerialClose = function(data) {
this.connected= false;
log.error('G2 Core serial link was lost.')
process.exit(14);
};
G2.prototype._write = function(s, callback) {
log.g2(this._serialToken,'out',s);
this._serialPort.write(s, function () {
if(callback) {
this._serialPort.drain(callback);
}
}.bind(this));
}
G2.prototype.clearAlarm = function() {
this.command({"clear":null});
};
G2.prototype.setUnits = function(units, callback) {
if(units === 0 || units == 'in') {
gc = 'G20';
units = 0;
} else if(units === 1 || units === 'mm') {
gc = 'G21';
units = 1;
} else {
return callback(new Error('Invalid unit setting: ' + units));
}
this.runString(gc).then(function() {
callback(null);
});
}
G2.prototype.requestStatusReport = function(callback) {
// Register the callback to be called when the next status report comes in
typeof callback === 'function' && this.once('status', callback);
this.command({'sr':null});
};
G2.prototype.requestQueueReport = function() { this.command({'qr':null}); };
G2.prototype.onWAT = function(data) {
var s = data.toString('ascii');
var len = s.length;
for(var i=0; i<len; i++) {
c = s[i];
if(c === '\n') {
string = this._currentGCodeData.join('');
t = new Date().getTime();
log.g2('D','in',string);
//log.g2('<-G--' + t + '---- ' + string);
this._currentGCodeData = [];
} else {
this._currentGCodeData.push(c);
}
}
};
// Called for every chunk of data returned from G2
G2.prototype.onData = function(data) {
t = new Date().getTime();
this.emit('raw_data',data);
var s = data.toString('ascii');
var len = s.length;
for(var i=0; i<len; i++) {
c = s[i];
if(c === '\n') {
var json_string = this._currentData.join('');
t = new Date().getTime();
log.g2('S','in',json_string);
try {
var obj = JSON.parse(json_string);
this.onMessage(obj);
}catch(e){
throw e
this.emit('error', [-1, 'JSON_PARSE_ERROR', "Could not parse response: '" + jsesc(json_string) + "' (" + e.toString() + ")"]);
}
this._currentData = [];
} else {
this._currentData.push(c);
}
}
};
G2.prototype.handleQueueReport = function(r) {
var qo = r.qo || 0;
// Pass
};
G2.prototype.handleFooter = function(response) {
if(response.f) {
if(response.f[1] !== 0) {
var err_code = response.f[1];
var err_msg = G2_ERRORS[err_code] || ['ERR_UNKNOWN', 'Unknown Error'];
// TODO we'll have to go back and clean up alarms later
// For now, let's not emit a bunch of errors into the log that don't mean anything to us
this.emit('error', [err_code, err_msg[0], err_msg[1]]);
return new Error(err_msg[1]);
}
}
};
G2.prototype.handleExceptionReport = function(response) {
if(response.er) {
this._lastExceptionReport = response.er;
var stat = response.er.st;
if((stat === 207) && this.quit_pending) {
// this.quit_pending = false;
//this._write("{clr:n}\n");
//this.command("M30");
}
}
};
G2.prototype.getLastException = function() {
return this._lastExceptionReport || null;
}
G2.prototype.clearLastException = function() {
this._lastExceptionReport = null;
}
/*
0 machine is initializing
1 machine is ready for use
2 machine is in alarm state (shut down)
3 program stop or no more blocks (M0, M1, M60)
4 program end via M2, M30
5 motion is running
6 motion is holding
7 probe cycle active
8 machine is running (cycling)
9 machine is homing
*/
G2.prototype.handleStatusReport = function(response) {
if(response.sr) {
// Update our copy of the system status
for (var key in response.sr) {
value = response.sr[key];
if(key === 'unit') {
value = value === 0 ? 'in' : 'mm';
}
this.status[key] = value;
}
// Send more g-codes if warranted
if('line' in response.sr) {
line = response.sr.line;
lines_left = this.lines_sent - line;
}
if('stat' in response.sr) {
switch(response.sr.stat) {
case STAT_STOP:
if(this.flushcallback) {
this.flushcallback(null);
this.flushcallback = null;
}
break;
case STAT_END:
this.status.line = null;
break;
}
if(this.quit_pending) {
switch(response.sr.stat) {
case STAT_STOP:
case STAT_HOLDING:
log.info("!!! Issuing the job kill command.")
setTimeout(function() {
this._write('\x04\n', function() {});
}.bind(this), 50)
break;
case STAT_END:
log.info("!!! Clearing the quit pending state.")
this.lines_to_send = 4
this.quit_pending = false;
this.pause_flag = false;
break;
}
} else {
switch(response.sr.stat) {
case STAT_HOLDING:
this.pause_flag = true;
if(this.context) {
this.context.pause()
}
break;
}
}
if(this.expectations.length > 0) {
var expectation = this.expectations.pop();
var stat = states[this.status.stat];
if(stat in expectation) {
if(expectation[stat] === null) {
this.expectations.push(expectation);
} else {
expectation[stat](this);
}
} else if(null in expectation) {
expectation[null](this);
}
}
}
this.stat = this.status.stat !== undefined ? this.status.stat : this.stat;
this.hold = this.status.hold !== undefined ? this.status.hold : this.hold;
if(this.context) {
this.context.emit('status', this.status);
}
// Emit status no matter what
if('stat' in response.sr) {
this.emit('stat', response.sr.stat)
if(this.context) {
this.context.emit('stat', response.sr.stat);
}
}
this.emit('status', this.status);
}
};
// Called once a proper JSON response is decoded from the chunks of data that come back from G2
G2.prototype.onMessage = function(response) {
// TODO more elegant way of dealing with "response" data.
if(response.r) {
if(!this._seen_ready) {
// Special message type for initial system ready message
if(response.r.msg && (response.r.msg === "SYSTEM READY")) {
this.emit('ready', this);
return;
}
}
if(this._ignored_responses > 0) {
this._ignored_responses--;
} else {
this.lines_to_send += 1;
this.sendMore();
}
r = response.r;
this.emit('response', false, response.r);
} else {
r = response;
}
// Deal with G2 status (top priority)
this.handleStatusReport(r);
// Deal with exceptions
this.handleExceptionReport(r);
// Deal with streaming (if response contains a queue report)
// this.handleQueueReport(r);
// Deal with footer
var err = this.handleFooter(response);
// Emitted everytime a message is received, regardless of content
this.emit('message', response);
for(var key in r) {
if(key in this.readers) {
if(typeof this.readers[key][this.readers[key].length-1] === 'function') {
//if(r[key] !== null) {
callback = this.readers[key].shift();
if(err) {
callback(err);
} else {
callback(null, r[key]);
}
//}
}
}
}
};
G2.prototype.feedHold = function(callback) {
this.pause_flag = true;
this.flooded = false;
typeof callback === 'function' && this.once('state', callback);
if(this.status.stat === this.STAT_PROBE) {
return this.quit()
}
log.debug("Sending a feedhold");
if(this.context) {
this.context.pause();
}
this._write('!\n', function() {
log.debug("Drained.");
});
};
G2.prototype.queueFlush = function(callback) {
log.debug('Clearing the queue.');
this.flushcallback = callback;
this.command({'clr':null});
this._write('\%');
};
G2.prototype.resume = function() {
var thisPromise = _promiseCounter;
log.info("Creating promise " + thisPromise);
_promiseCounter += 1;
var deferred = Q.defer();
var that = this;
var onStat = function(stat) {
if(stat !== STAT_RUNNING) {
if(this.quit_pending && stat === STAT_HOLDING) {
return;
}
that.removeListener('stat', onStat);
log.info("Resolving promise (resume): " + thisPromise)
deferred.resolve(stat);
}
}
this.on('stat', onStat);
this._write('~'); //cycle start command character
this.pause_flag = false;
if(this.context) {
this.context.resume();
}
return deferred.promise;
};
G2.prototype.quit = function() {
if(this.quit_pending) {
log.warn("Not quitting because a quit is already pending.");
return;
}
switch(this.status.stat) {
case STAT_END:
return;
break;
default:
this.quit_pending = true;
if(this.stream) {
this.stream.end()
}
this.gcode_queue.clear();
this._write('\x04\n');
break;
}
}
G2.prototype.get = function(key, callback) {
var keys;
if(key instanceof Array) {
keys = key;
is_array = true;
} else {
is_array = false;
keys = [key];
}
async.map(keys,
// Function called for each item in the keys array
function(k, cb) {
cb = cb.bind(this);
cmd = {};
cmd[k] = null;
if(k in this.readers) {
this.readers[k].push(cb);
} else {
this.readers[k] = [cb];
}
// Ensure that an errback is called if the data isn't read out
setTimeout(function() {
if(k in this.readers) {
callbacks = this.readers[k];
stored_cb = callbacks[callbacks.length-1];
if(cb == stored_cb) {
if(typeof cb == 'function') {
this.readers[k].shift();
cb(new Error("Timeout"), null);
}
}
}
}.bind(this), CMD_TIMEOUT);
this.command(cmd);
}.bind(this),
// Function to call with the list of results
function(err, result) {
if(err) {
return callback(err, result);
} else {
// If given an array, return one. Else, return a single item.
if(is_array) {
return callback(err, result);
} else {
return callback(err, result[0]);
}
}
}
);
};
G2.prototype.setMany = function(obj, callback) {
var keys = Object.keys(obj);
async.map(keys,
// Function called for each item in the keys array
function(k, cb) {
cmd = {};
cmd[k] = obj[k];
if(k in this.readers) {
this.readers[k].push(cb.bind(this));
} else {
this.readers[k] = [cb.bind(this)];
}
this.command(cmd);
}.bind(this),
// Function to call with the list of results
function(err, result) {
if(err) {
return callback(err, result);
} else {
var retval = {};
try {
for(i=0; i<keys.length; i++) {
retval[keys[i]] = result[i];
}
} catch(e) {
callback(e, null);
}
return callback(null, retval);
}
}
);
};
G2.prototype.set = function(key, value, callback) {
if(value === undefined) {
return callback(new Error("Undefined value passed to G2"));
}
cmd = {};
cmd[key] = value;
if (key in this.readers) {
this.readers[key].push(callback);
} else {
this.readers[key] = [callback];
}
// Ensure that an errback is called if the data isn't read out
setTimeout(function() {
if(key in this.readers) {
callbacks = this.readers[key];
stored_cb = callbacks[callbacks.length-1];
if(callback == stored_cb) {
if(typeof callback == 'function') {
this.readers[key].shift();
callback(new Error("Timeout"), null);
}
}
}
}.bind(this), CMD_TIMEOUT);
this.command(cmd);
};
// Send a command to G2 (can be string or JSON)
G2.prototype.command = function(obj) {
var cmd;
if((typeof obj) == 'string') {
cmd = obj.trim();
this.gcode_queue.enqueue(cmd);
} else {
cmd = JSON.stringify(obj);
cmd = cmd.replace(/(:\s*)(true)(\s*[},])/g, "$1t$3")
cmd = cmd.replace(/(:\s*)(false)(\s*[},])/g, "$1f$3")
cmd = cmd.replace(/"/g, '');
this.command_queue.enqueue(cmd);
}
this.sendMore();
};
// Send a (possibly multi-line) string
// An M30 will be placed at the end to put the machine back in the "idle" state
G2.prototype.runString = function(data, callback) {
var stringStream = new stream.Readable();
stringStream.push(data + "\n");
//stringStream.push("M30\n");
//stringStream.end()
stringStream.push(null);
return this.runStream(stringStream);
};
G2.prototype.runList = function(l, callback) {
var stringStream = new stream.Readable();
for(var i=0; i<l.length; i++) {
stringStream.push(l[i] + "\n");
}
//stringStream.push("M30\n");
stringStream.push(null);
//stringStream.end()
return this.runStream(stringStream);
}
G2.prototype._createStatePromise = function(states) {
// Track the promise created (debug)
var thisPromise = _promiseCounter;
log.info("Creating promise " + thisPromise);
_promiseCounter += 1;
var deferred = Q.defer();
var that = this;
var onStat = function(stat) {
for(var i=0; i<states.length; i++) {
if(stat === states[i]) {
that.removeListener('stat', onStat);
log.info("Resolving promise " + thisPromise + " because of state " + stat + " which is one of " + states)
deferred.resolve(stat);
}
}
}
this.on('stat', onStat);
return deferred.promise;
}
G2.prototype.waitForState = function(states) {
if(!states.length) {
states = [states]
}
return this._createStatePromise(states);
}
G2.prototype.runStream = function(s) {
this._createCycleContext();
s.pipe(this.context._stream);
return this.context;
}
G2.prototype.runFile = function(filename) {
var st = fs.createReadStream(filename);
var ln = new LineNumberer();
//return this.runStream(st);
return this.runStream(st.pipe(ln));
}
G2.prototype.runImmediate = function(data) {
return this.runString(data);
}
G2.prototype.prime = function() {
log.info("Priming driver (manually)");
this._primed = true;
this.sendMore();
}
G2.prototype.sendMore = function() {
//log.info("sendMore: Lines to send: " + this.lines_to_send);
//log.info(" Lines in queue: " + this.gcode_queue.getLength());
if(this.pause_flag) {
return;
}
var count = this.command_queue.getLength();
if(count) {
var to_send = count;
var codes = this.command_queue.multiDequeue(count)
codes.push("");
this._ignored_responses+=to_send;
this._write(codes.join('\n'), function() {});
}
if(this._primed) {
var count = this.gcode_queue.getLength();
if(this.lines_to_send >= THRESH) {
if(count >= THRESH || this._streamDone) {
var to_send = Math.min(this.lines_to_send, count);
var codes = this.gcode_queue.multiDequeue(to_send);
codes.push("");
if(codes.length > 1) {
this.lines_to_send -= to_send/*-offset*/;
this._write(codes.join('\n'), function() { });
}
}
}
else {
log.warn("Not writing to gcode due to lapse in responses")
}
} else {
if(this.gcode_queue.getLength() > 0) {
//log.warn("!!! Not sending because not primed.");
}
}
};
G2.prototype.setMachinePosition = function(position, callback) {
var axes = ['x','y','z','a','b','c','u','v','w']
var gcodes = ['G21']
axes.forEach(function(axis) {
if(position[axis] != undefined) {
gcodes.push('G28.3 ' + axis + position[axis].toFixed(5));
}
});
gcodes.push(this.status.unit === 'in' ? 'G20' : 'G21');
this.runList(gcodes).then(function() {callback && callback()})
}
// Function works like "once()" for a state change
// callbacks is an associative array mapping states to callbacks
// If the *next* state change matches a state in the associative array, the callback it maps to is called.
// If null is specified in the array, this callback is used for any state that is unspecified
//
// eg:
// this.expectStateChange {
// STAT_END : end_callback,
// STAT_PAUSE : pause_callback,
// null : other_callback};
//
// In the above example, when the next change of state happens, the appropriate callback is called in the case
// that the new state is either STAT_END or STAT_PAUSE. If the new state is neither, other_callback is called.
G2.prototype.expectStateChange = function(callbacks) {
if("timeout" in callbacks) {
var fn = callbacks.timeout;
setTimeout(function() {
if(this.expectations.length > 0) {
callbacks = this.expectations[this.expectations.length-1];
if(callbacks.timeout === fn) {
log.debug("Calling timeout function");
this.expectations.pop();
fn(this);
}
}
}.bind(this), EXPECT_TIMEOUT);
}
this.expectations.push(callbacks);
};
states = {
0 : "init",
1 : "ready",
2 : "alarm",
3 : "stop",
4 : "end" ,
5 : "running",
6 : "holding",
7 : "probe",
8 : "cycling",
9 : "homing",
11 : "interlock",
12 : "shutdown",
13 : "panic"
};
var state = function(s) {
return states[s];
};
// export the class
exports.G2 = G2;
exports.STAT_INIT = STAT_INIT;
exports.STAT_READY = STAT_READY;
exports.STAT_ALARM = STAT_ALARM;
exports.STAT_STOP = STAT_STOP;
exports.STAT_END = STAT_END;
exports.STAT_RUNNING = STAT_RUNNING;
exports.STAT_HOLDING = STAT_HOLDING;
exports.STAT_PROBE = STAT_PROBE;
exports.STAT_CYCLING = STAT_CYCLING;
exports.STAT_HOMING = STAT_HOMING;
exports.STAT_INTERLOCK = STAT_INTERLOCK;
exports.STAT_SHUTDOWN = STAT_SHUTDOWN;
exports.STAT_PANIC = STAT_PANIC;
G2.prototype.STAT_INIT = STAT_INIT;
G2.prototype.STAT_READY = STAT_READY;
G2.prototype.STAT_ALARM = STAT_ALARM;
G2.prototype.STAT_STOP = STAT_STOP;
G2.prototype.STAT_END = STAT_END;
G2.prototype.STAT_RUNNING = STAT_RUNNING;
G2.prototype.STAT_HOLDING = STAT_HOLDING;
G2.prototype.STAT_PROBE = STAT_PROBE;
G2.prototype.STAT_CYCLING = STAT_CYCLING;
G2.prototype.STAT_HOMING = STAT_HOMING;
G2.prototype.STAT_INTERLOCK = STAT_INTERLOCK;
G2.prototype.STAT_SHUTDOWN = STAT_SHUTDOWN;
G2.prototype.STAT_PANIC = STAT_PANIC;