This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPrintr.cpp
591 lines (512 loc) · 15 KB
/
Printr.cpp
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
/*
* Handles the communication and state handling with the g2 printer board. It needs to
* handle this communication without blocking the main loop. This results in a somewhat
* more complicated code. It's also important that g2 gets new GCodes all the time, so
* timing is important
*
* More Info and documentation:
* http://www.appfruits.com/2016/11/behind-the-scenes-printrbot-simple-2016/
*
* Copyright (c) 2016 Printrbot Inc.
* Author: Mick Balaban, Phillip Schuster
* https://github.com/Printrbot/Printrhub
*
* Developed in cooperation with Phillip Schuster (@appfruits) from appfruits.com
* http://www.appfruits.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "Printr.h"
#include <ArduinoJson.h>
#include "SD.h"
#include "framework/core/HAL.h"
#include "scenes/settings/DataStore.h"
extern DataStore dataStore;
Printr::Printr() :
readBuffer(PrintrBuffer()),
_sendNext(true),
_printing(false),
_listener(NULL),
_homeX(false),
_homeY(false),
_homeZ(false),
_progress(0.0),
_processedProgramLine(0),
_currentAction(0),
_lightOn(true),
_lightColor(4) {
_setupCode = new MemoryStream(64);
_waiting = false;
_waitStart = 0;
_printrCurrentStatus = PRINTR_STATUS_INITIALIZING;
_currentMode == PrintrMode::ImmediateMode;
_lineSent = true;
_currentLineBuffer = new MemoryStream(255);
}
Printr::~Printr() {
delete _setupCode;
delete _currentLineBuffer;
}
void Printr::init() {
Serial1.begin(115200);
Serial1.attachCts(CTS_PIN);
Serial1.attachRts(RTS_PIN);
reset();
//When we init we wait for the printer to send the first status response
_linesToSend = 4;
//sendLine("{sr:{line:t,he1t:t,he1st:t,he1at:t,stat:t}}");
stopListening();
sendLine("{_leds:4}");
sendWaitCommand(500);
sendLine("{_leds:1}");
sendWaitCommand(500);
sendLine("{_leds:2}");
sendWaitCommand(500);
sendLine("{_leds:3}");
sendWaitCommand(500);
sendLine("{_leds:4}");
//sendLine("M100({_leds:4})",false); //switch to blue light
sendLine("{sv:1}");
}
void Printr::startListening() {
sendLine("M100({sr:{line:t,he1t:t,he1st:t,he1at:t,stat:t}})");
sendWaitCommand(500);
}
void Printr::stopListening() {
sendLine("M100({sr:{line:t}})");
sendWaitCommand(500);
}
void Printr::reset() {
_currentMode = PrintrMode::ImmediateMode;
_printing = false;
_setupCode->flush();
_linesToSend = 4;
}
void Printr::loop() {
if (_printrCurrentStatus == PRINTR_STATUS_INITIALIZING) {
//We only read and wait for status to become OK
readResponses();
} else if (_printrCurrentStatus == PRINTR_STATUS_OK) {
//First Send Commands
sendCommands();
//Then read responses
readResponses();
} else {
//Any other status is considered an error
}
}
void Printr::turnLightOn() {
this->setLightColor(this->_lightColor);
this->_lightOn = true;
}
void Printr::turnLightOff() {
sendLine("{_leds:0}");
this->_lightOn = false;
}
void Printr::setLightColor(int colorId) {
String lc = "{_leds:";
lc += String(colorId);
lc += "}";
sendLine(lc.c_str());
}
bool Printr::queryCurrentLine(Stream *stream, int lineNumber) {
if (!stream->available()) {
//Nothing to read from the stream
return false;
}
//Add a line number if necessary
if (lineNumber > 0) {
_currentLineBuffer->print("N");
_currentLineBuffer->print(lineNumber);
_currentLineBuffer->print(" ");
}
//Now try to read the line
bool lineRead = false;
while (stream->available() > 0) {
int byte = stream->read();
if (byte < 0) {
//Stream is EOF
break;
} else {
_currentLineBuffer->write(byte);
if (byte == '\n') {
//This is the end of the line
lineRead = true;
break;
}
}
}
//We did not read a line, so flush the current line buffer
if (!lineRead) {
_currentLineBuffer->flush();
} else {
//Mark this line to not been sent yet
_lineSent = false;
}
return lineRead;
}
void Printr::handlePBCode(const char *pbcode) {
String code(pbcode);
if (code.startsWith(";PBCODE;wait;")) {
String durationString = code.replace(";PBCODE;wait;", "");
int duration = durationString.toInt();
PRINTER_SPAM("Got a wait command: %d", duration);
_waiting = true;
_waitStart = millis();
_waitDuration = duration;
}
}
void Printr::sendCommands() {
if (_linesToSend <= 0) {
return;
}
//We had a wait command, let's wait if necessary
if (_waiting) {
if ((millis() - _waitStart) > _waitDuration) {
_waiting = false;
} else {
return;
}
}
if (_lineSent) {
//The current line has been sent, get a new one
if (_currentMode == PrintrMode::ImmediateMode) {
//Only send commands from the buffer
if (queryCurrentLine(_setupCode)) {
PRINTER_SPAM("Immediate-Mode: Queried new line from setupCode: %s", _currentLineBuffer->c_str());
}
} else if (_currentMode == PrintrMode::PrintMode) {
//If we are in print mode, we work through the setup buffer, after that we switch to the file
if (queryCurrentLine(_setupCode)) {
PRINTER_SPAM("Print-Mode: Queried new line from setupCode: %s", _currentLineBuffer->c_str());
} else {
//Setup buffer has been sent, switch to file
if (_printFile) {
if (queryCurrentLine(&_printFile, _lastSentProgramLine)) {
PRINTER_SPAM("Print-Mode: Queried new line from print file: %s", _currentLineBuffer->c_str());
_lastSentProgramLine++;
}
}
}
}
} else {
//Line has not been sent yet, try to send it now
if (_currentLineBuffer->available() <= 0) {
_lineSent = true;
} else if (_currentLineBuffer->peek() == ';') {
if (strncmp(";PBCODE;", _currentLineBuffer->c_str(), strlen(";PBCODE;")) == 0) {
//We got a PB code, do something about it
PRINTER_SPAM("Got a PBCODE: %s", _currentLineBuffer->c_str());
handlePBCode(_currentLineBuffer->c_str());
} else {
//This is a comment, skip that
PRINTER_SPAM("Skipped comment: %s", _currentLineBuffer->c_str());
}
_lineSent = true;
_currentLineBuffer->flush();
} else if (_currentLineBuffer->peek() == '\n') {
PRINTER_SPAM("Just got a newline");
_lineSent = true;
_currentLineBuffer->flush();
} else {
//This line is ok, send it to the printer if enough space is in the buffer
while (true) {
if (Serial1.availableForWrite() <= 0) {
//We cannot send anything now, break out
break;
} else {
int byte = _currentLineBuffer->read();
if (byte < 0) {
//This line has been sent completely
_lineSent = true;
_currentLineBuffer->flush();
_linesToSend--;
PRINTER_SPAM("Line sent, lines left to send: %d", _linesToSend);
break;
} else {
//Write the byte we just read to the printr
Serial1.write(byte);
}
}
}
}
}
}
void Printr::readResponses() {
//Read from Serial
if (Serial1.available()) {
digitalWrite(CODE_INDICATOR_1, LOW);
while (Serial1.available()) {
readBuffer.line_buff[readBuffer.line_idx] = Serial1.read();
if (readBuffer.line_buff[readBuffer.line_idx] == '\n') {
readBuffer.line_buff[readBuffer.line_idx + 1] = '\0';
parseResponse();
readBuffer.line_idx = 0;
readBuffer.line_buff[0] = '\0';
//We want to exit to loop once we read a line
break;
} else
readBuffer.line_idx++;
}
digitalWrite(CODE_INDICATOR_1, HIGH);
}
}
void Printr::turnOffHotend() {
sendLine("M100({he1st:0})");
}
void Printr::stopAndFlush() {
sendLine("!%");
}
int Printr::startJob(String filePath) {
PRINTER_NOTICE("Printing file: %s", filePath.c_str());
_printFile = SD.open(filePath.c_str(), FILE_READ);
_totalProgramLines = -1;
_progress = 0.0;
// read json header (if available)
char i[2];
_printFile.read(&i, 2);
if (i[0] == ';' && i[1] == '{') {
// found json string in header, parse it now
_printFile.seek(1);
String js = _printFile.readStringUntil('\n', 200);
StaticJsonBuffer<512> jb;
JsonObject &h = jb.parseObject(js);
if (h.success()) {
_totalProgramLines = h["lines"];
_totalPrintTime = h["time"];
const char *ptr = h["readable"];
_printTimeReadable = String(ptr);
_printVolume = h["volume"];
_printFilamentLength = h["filament"];
_printSupport = h["support"];
_printBrim = h["brim"];
const char *res = h["resolution"];
_printResolution = String(res);
const char *infill = h["infill"];
_printInfill = String(infill);
}
}
startListening();
//Setup printer and run the file
runJobStartGCode();
return _totalProgramLines;
}
void Printr::runJobStartGCode() {
_currentMode = PrintrMode::PrintMode;
_lastSentProgramLine = 1;
_processedProgramLine = 0;
Material *_selectedMaterial = dataStore.getLoadedMaterial();
// set temperature
char tempCmd[20];
sprintf(tempCmd, "M100({he1st:%d})", _selectedMaterial->temperature);
String tc = tempCmd;
sendLine(tc);
// adjust speed
// we will use 1620 as 100% maximum extruder speed
char speedCmd[20];
int aJm = 1620 * (float) (_selectedMaterial->speed / 100.0);
//sprintf(speedCmd, "M100({afr:%d})", aJm);
//sendLine(speedCmd);
sendLine("G92.1 X0 Y0 Z0 A0 B0");
_printing = true;
// reset all
sendLine("G28.2 X0 Y0");
sendLine("G0 X110");
sendLine("M100({_leds:2})");
sendLine("M101 ({he1at:t})");
sendLine("M100({_leds:3})");
sendLine("G28.2 Z0");
sendLine("G0 X0 Y145 Z6");
sendLine("G38.2 Z-10 F200");
sendLine("G0 Z5");
sendLine("M100({_leds:5})");
sendLine("G0 X210 Y65");
sendLine("G38.2 Z-10 F200");
sendLine("G0 Z5");
sendLine("M100({_leds:6})");
sendLine("G0 X0 Y10");
sendLine("G38.2 Z-10 F200");
sendLine("G0 Z5");
sendLine("M100({_leds:3})");
sendLine("M100 ({tram:1})");
sendLine("G92 A0");
// switch to white light
sendLine("M100({_leds:1})");
sendLine("G0 Z5");
// apply hotend offset
float headOffset = 5.0 - dataStore.getHeadOffset();
String gco = String("G92 Z") + String(headOffset);
sendLine(gco);
// clean the nozzle
sendLine("G0 X0 Y0 Z0.3");
sendLine("G1 X220.000 A12 F1200");
sendLine("G0 Y0.4");
sendLine("G1 X110.000 A18");
sendLine("G0 Z1");
sendLine("G92 A0");
}
void Printr::cancelCurrentJob() {
_currentMode = PrintrMode::ImmediateMode;
_currentLineBuffer->flush();
stopAndFlush();
sendWaitCommand(1000);
reset();
turnOffHotend();
stopListening();
sendLine("M100({_leds:4})"); //switch to blue light
sendLine("G91"); //Relative mode
sendLine("G0 Z10"); //Move up
sendLine("G90"); //Back to absolute mode
sendLine("G0 X110 Y150"); //Home back with bed centered
_printing = false;
}
void Printr::homeX() {
sendLine("G28.2 X0");
_homeX = true;
}
void Printr::homeY() {
sendLine("G28.2 Y0");
_homeY = true;
}
void Printr::homeZ() {
sendLine("G28.2 Z0");
_homeZ = true;
}
void Printr::programEnd(bool success) {
if (!_printing)
return;
if (_currentMode != PrintrMode::PrintMode)
return;
//sendLine("M100({_leds:4})");
//Reset the printer and prepare memory buffers
reset();
_printFile.close();
_printing = false;
_lastSentProgramLine = 0;
_processedProgramLine = 0;
_totalProgramLines = 0;
// turn off the hotend just in case
turnOffHotend();
stopListening();
if (_listener != nullptr) {
_listener->onPrintComplete(success);
}
}
void Printr::sendLine(String line) {
if (_setupCode->println(line) <= 0) {
PRINTER_ERROR("Could not send line: %s", line.c_str());
}
}
void Printr::sendWaitCommand(int millis) {
String line(";PBCODE;wait;");
line = line + millis;
sendLine(line);
}
void Printr::parseResponse() {
char *line = readBuffer.line_buff;
PRINTER_SPAM("Received line: %s", line);
if (line[0] == '{') {
StaticJsonBuffer<512> jsonBuffer;
StaticJsonBuffer<512> rBuffer;
StaticJsonBuffer<512> fBuffer;
JsonObject &o = jsonBuffer.parseObject(line);
if (!o.success()) {
// failed...
PRINTER_ERROR("Could not parse printer response: %s", line);
digitalWrite(CODE_INDICATOR_2, LOW);
delayMicroseconds(5);
digitalWrite(CODE_INDICATOR_2, HIGH);
//Make sure we proceed sending data so we don't stall
_sendNext = true;
} else {
// Parse status response
String sr = o["sr"];
String r = o["r"];
String f = o["f"];
if (sr.length() > 0) {
JsonObject &_sr = rBuffer.parseObject(sr);
// {sr: {stat:0}}
// https://github.com/synthetos/TinyG/wiki/TinyG-Status-Codes#status-report-enumerations
if (_sr["stat"]) {
_stat = _sr["stat"];
switch (_stat) {
case PRINTR_STAT_ALARM:
// hmmmm ... need to handle this better
// alarm
Display.fillRect(0, 0, 320, 240, ILI9341_RED);
Display.setCursor(10, 10);
Display.setTextColor(ILI9341_WHITE);
Display.println("ALARM: machine is in alarm state!");
Display.setCursor(10, 30);
Display.println(_stat);
Display.fadeIn();
break;
case PRINTR_STAT_PROGRAM_END:
// program end via M2, M30
// finish print if printing
PRINTER_NOTICE("Printer finished, closing");
programEnd(true);
break;
}
}
// parse hotend 1 temperature
if (_sr["he1t"]) {
_hotend1Temp = (float) _sr["he1t"];
if (_listener != NULL) {
_listener->onNewNozzleTemperature(_hotend1Temp);
}
}
if (_sr["line"]) {
_sendNext = true;
_processedProgramLine = _sr["line"];
_progress = ((float) _processedProgramLine / (float) _totalProgramLines);
if (_listener != NULL) {
_listener->onPrintProgress(_progress);
}
}
}
//Parse line response
if (r.length() > 0) {
JsonObject &_r = rBuffer.parseObject(r);
if (_r["n"]) {
_processedProgramLine = _r["n"];
_progress = ((float) _processedProgramLine / (float) _totalProgramLines);
if (_listener != NULL) {
_listener->onPrintProgress(_progress);
}
}
//We got a r-response, so increment number of lines that can be sent
_linesToSend++;
PRINTER_SPAM("Got a r message, line-nr: %d, progress: %d", _processedProgramLine, (int) (_progress * 100.0f));
}
//Parse status
if (f.length() > 0) {
JsonArray &_f = fBuffer.parseArray(f);
if (_f.size() <= 0) {
//Parsing failed
PRINTER_ERROR("Got status array, but could not parse it: %s", f.c_str());
} else {
//Parsing successful, save values
_printrCurrentStatus = _f[1];
_printrBufferSize = _f[2];
PRINTER_SPAM("Got status: %s, Status-Code: %d, Available line buffer: %d, Lines to send: %d", f.c_str(), _printrCurrentStatus, _printrBufferSize, _linesToSend);
}
}
}
}
}