-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathGCode.cpp
622 lines (587 loc) · 15.9 KB
/
GCode.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
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
#include "GCode.h"
#include "GcodeQueue.h"
#include "Time.h"
#include "Globals.h"
#include "Temperature.h"
#include "SDCard.h"
#include "Eeprom.h"
#include <avr/pgmspace.h>
#include "ArduinoMap.h"
#ifndef USE_MARLIN
#include "Motion.h"
#else
#include "Marlin.h"
#endif
#ifdef HAS_LCD
#include "LCDKeypad.h"
extern LCDKeypad LCDKEYPAD;
#endif
#ifdef USE_MARLIN
#define SETOBJ(x) Marlin::x
#else
#define SETOBJ(x) MOTION.x
#endif
Point GCode::lastpos;
float GCode::lastfeed;
Pin GCode::fanpin = Pin();
bool GCode::DONTRUNEXTRUDER=false;
bool GCode::ISRELATIVE=false;
Pin GCode::powerpin = Pin();
// Stuff to do if it's a G move code, otherwise not I guess.
// This function MAY get called repeatedly before the execute() function.
// It WILL get called at least once.
void GCode::prepare()
{
#ifdef USE_MARLIN
// Marlin might be able to prepare in advance of being asked to execute the codes with some help.
state = PREPARED;
return;
#else
if(state == PREPARED)
return;
if(DONTRUNEXTRUDER)
{
cps[E].unset();
}
// Only GCodes need preparing.
if(cps[G].isUnused())
state = PREPARED;
else
MOTION.gcode_precalc(*this,lastfeed,&lastpos);
#endif
}
// Called when we have extra time to optimize a gcode.
void GCode::optimize(GCode& next)
{
#ifndef USE_MARLIN
if(state != PREPARED)
return;
if(next.state != PREPARED)
return;
MOTION.gcode_optimize(*this, next);
#endif
}
// Called at the time of enqueue
void GCode::enqueue()
{
if(!cps[G].isUnused())
{
switch(cps[G].getInt())
{
case 0:
case 1:
case 2:
for(int ax=0;ax<NUM_AXES;ax++)
{
if(!cps[ax].isUnused())
{
if(ISRELATIVE)
{
float foo = lastpos[ax] + cps[ax].getFloat();
cps[ax].setFloat(foo);
}
}
}
break;
case 90: // Set Absolute Positioning
ISRELATIVE = false;
break;
case 91: // Set Relative Positioning
ISRELATIVE = true;
break;
}
}
prepare(); // Need to prepare at this point so that we have the correct lastpos
}
// Do some stuff and return. This function will be called repeatedly while
// the state is still ACTIVE, and you can set up an interrupt for precise timings.
void GCode::execute()
{
if(startmillis == 0)
startmillis = millis();
if(state < PREPARED)
{
prepare();
return;
}
if(state == DONE)
{
return;
}
if(cps[G].isUnused())
{
do_m_code();
}
else
{
do_g_code();
}
}
void GCode::dump_to_host()
{
HOST.labelnum("L:", linenum);
for(int x=0;x<T;x++)
cps[x].dump_to_host();
HOST.endl();
}
void GCode::wrapupmove()
{
#ifndef USE_MARLIN
if(!cps[G].isUnused())
{
#ifdef DEBUG_MOVE
dump_movedata();
#endif
#ifndef REPRAP_COMPAT
Host::Instance(source).labelnum("done ", linenum, false); Host::Instance(source).labelnum(" G", cps[G].getInt());
#endif
MOTION.wrapup(*this);
}
#endif
}
#define SKIPEXTRUDE if(DONTRUNEXTRUDER) { state = DONE; return; }
void GCode::do_g_code()
{
#ifdef USE_MARLIN
// Marlin's only method of synchronizing these sorts of commands is to wait 'till it's cleared out.
if(cps[G].getInt() != 1 && !Marlin::isBufferEmpty())
return;
#endif
switch(cps[G].getInt())
{
case 0:
case 1: // Linear Move
#ifdef USE_MARLIN
if(Marlin::add_buffer_line(*this))
state = DONE;
#else
MOTION.gcode_execute(*this);
#endif
break;
case 4: // Pause for P millis
if(millis() - cps[P].getInt() > startmillis)
{
state = DONE;
}
else
state = ACTIVE;
break;
case 21: // Units are mm
state = DONE;
break;
case 90: // Absolute positioning
// We shouldn't arrive here as it's handled during enqueue();
state = DONE;
break;
case 91: // Relative positioning
// We shouldn't arrive here as it's handled during enqueue();
state = DONE;
break;
case 92: // Set position
SETOBJ(setCurrentPosition(*this));
state = DONE;
break;
default:
Host::Instance(source).labelnum("warn ",linenum, false);
Host::Instance(source).write_P(PSTR(" GCODE "));
Host::Instance(source).write(cps[G].getInt(), 10);
Host::Instance(source).write_P(PSTR(" NOT SUPPORTED\n"));
state = DONE;
break;
}
}
void GCode::write_temps_to_host(int port)
{
Host::Instance(port).labelnum("T:",TEMPERATURE.getHotend(),false);
Host::Instance(port).labelnum(" B:", TEMPERATURE.getPlatform(),false);
#ifdef REPG_COMPAT
Host::Instance(port).write('\n');
#else
Host::Instance(port).labelnum(" / SH:", TEMPERATURE.getHotendST(),false);
Host::Instance(port).labelnum(" SP:", TEMPERATURE.getPlatformST());
#endif
}
void GCode::do_m_code()
{
#ifdef USE_MARLIN
// Marlin's only method of synchronizing these sorts of commands is to wait 'till it's cleared out.
if(!Marlin::isBufferEmpty())
return;
#endif
switch(cps[M].getInt())
{
case 0: // Finish up and shut down.
case 18: // 18 used by RepG.
case 80: // power on
powerpin.setDirection(true);
powerpin.setValue(false);
state = DONE;
break;
case 81: // power off
powerpin.setValue(true);
powerpin.setDirection(false);
state = DONE;
break;
case 84: // "Stop Idle Hold" == shut down motors.
SETOBJ(disableAllMotors());
state = DONE;
break;
case 104: // Set Extruder Temperature (Fast)
SKIPEXTRUDE;
if(cps[S].isUnused() || TEMPERATURE.setHotend(cps[S].getInt()))
{
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false); Host::Instance(source).write(' '); write_temps_to_host(source);
#endif
state = DONE;
}
break;
case 105: // Get Extruder Temperature
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false); Host::Instance(source).write(' '); write_temps_to_host(source);
#else
write_temps_to_host(source);
#endif
state = DONE;
state = DONE;
break;
#ifndef USE_MBIEC
case 106: // Fan on
if(!fanpin.isNull())
{
fanpin.setValue(true);
}
state = DONE;
break;
case 107: // Fan off
if(!fanpin.isNull())
{
fanpin.setValue(false);
}
state = DONE;
break;
#else
case 106: // Fan on
if(TEMPERATURE.setFan(true))
state = DONE;
break;
case 107: // Fan off
if(TEMPERATURE.setFan(false))
state = DONE;
break;
#endif
case 109: // Set Extruder Temperature
SKIPEXTRUDE;
if(state != ACTIVE && (cps[S].isUnused() || TEMPERATURE.setHotend(cps[S].getInt())))
state = ACTIVE;
if(state == ACTIVE && TEMPERATURE.getHotend() >= TEMPERATURE.getHotendST())
{
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false); Host::Instance(source).write(' '); write_temps_to_host(source);
write_temps_to_host(source);
#endif
state = DONE;
}
if(millis() - lastms > 1000)
{
lastms = millis();
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false); Host::Instance(source).write(' '); write_temps_to_host(source);
#else
write_temps_to_host(source);
#endif
}
break;
case 110: // Set Current Line Number
GCODES.setLineNumber(cps[S].isUnused() ? 1 : cps[S].getInt());
state = DONE;
break;
case 114: // Get Current Position
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false);
Host::Instance(source).write(' ');
#endif
Host::Instance(source).write("C: ");
SETOBJ(writePositionToHost(*this));
// TODO
Host::Instance(source).endl();
state = DONE;
break;
case 115: // Get Firmware Version and Capabilities
Host::Instance(source).labelnum("prog ", linenum, false);
Host::Instance(source).write_P(PSTR(" VERSION:" SJFW_VERSION " FREE_RAM:"));
Host::Instance(source).write(getFreeRam(),10);
Host::Instance(source).write_P(PSTR(" FEATURES:0/crc-v2"));
Host::Instance(source).endl();
state = DONE;
break;
case 116: // Wait on temperatures
SKIPEXTRUDE;
if(TEMPERATURE.getHotend() >= TEMPERATURE.getHotendST() && TEMPERATURE.getPlatform() >= TEMPERATURE.getPlatformST())
state = DONE;
if(millis() - lastms > 1000)
{
lastms = millis();
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false); Host::Instance(source).write(' '); write_temps_to_host(source);
#else
write_temps_to_host(source);
#endif
}
break;
case 118: // Choose optional features TODO: implement properly when we have > 1 option
if(!cps[P].isUnused())
{
if(cps[P].getInt() == 1)
GCODES.enableADVANCED_CRC(source);
else
GCODES.disableADVANCED_CRC(source);
}
case 140: // Bed Temperature (Fast)
SKIPEXTRUDE;
if(cps[S].isUnused() || TEMPERATURE.setPlatform(cps[S].getInt()))
{
#ifndef REPG_COMPAT
Host::Instance(source).labelnum("prog ", linenum, false); Host::Instance(source).write(' '); write_temps_to_host(source);
#endif
state = DONE;
}
break;
case 200: // NOT STANDARD - set Steps Per Unit
SETOBJ(setStepsPerUnit(*this));
state = DONE;
break;
case 201: // NOT STANDARD - set Feedrates
SETOBJ(setMinimumFeedrate(*this));
state = DONE;
break;
case 202: // NOT STANDARD - set Feedrates
SETOBJ(setMaximumFeedrate(*this));
state = DONE;
break;
case 203: // NOT STANDARD - set Feedrates
SETOBJ(setAverageFeedrate(*this));
state = DONE;
break;
#ifdef HAS_SD
case 204: // NOT STANDARD - get next filename from SD
Host::Instance(source).labelnum("prog ", linenum, false);
Host::Instance(source).write(' ');
Host::Instance(source).write(sdcard::getNextfile());
Host::Instance(source).endl();
state = DONE;
break;
case 205: // NOT STANDARD - print file from last 204
Host::Instance(source).labelnum("prog ", linenum, false);
Host::Instance(source).write(sdcard::getCurrentfile());
if(sdcard::printcurrent())
Host::Instance(source).write_P(PSTR(" BEGUN"));
else
Host::Instance(source).write_P(PSTR(" FAIL"));
Host::Instance(source).endl();
state = DONE;
break;
#endif
case 206: // NOT STANDARD - set accel rate
SETOBJ(setAccel(*this));
state = DONE;
break;
case 207: // NOT STANDARD - set hotend thermistor pin
if(!cps[P].isUnused())
TEMPERATURE.changePinHotend(cps[P].getInt());
if(!cps[S].isUnused())
TEMPERATURE.changeOutputPinHotend(cps[S].getInt());
state = DONE;
break;
case 208: // NOT STANDARD - set platform thermistor pin
if(!cps[P].isUnused())
TEMPERATURE.changePinPlatform(cps[P].getInt());
if(!cps[S].isUnused())
TEMPERATURE.changeOutputPinPlatform(cps[S].getInt());
state = DONE;
break;
case 211: // NOT STANDARD - request temp reports at regular interval
if(cps[P].isUnused())
TEMPERATURE.changeReporting(0, Host::Instance(source));
else
TEMPERATURE.changeReporting(cps[P].getInt(), Host::Instance(source));
state = DONE;
break;
case 215: // NOT STANDARD - set arbitrary digital pin
if(!cps[P].isUnused() && !cps[S].isUnused())
{
doPinSet(cps[P].getInt(), cps[S].getInt());
}
state = DONE;
break;
case 216: // NOT STANDARD - set fan pin
if(!cps[P].isUnused())
{
fanpin = Pin(ArduinoMap::getPort(cps[P].getInt()), ArduinoMap::getPinnum(cps[P].getInt()));
fanpin.setDirection(true);
fanpin.setValue(false);
}
state = DONE;
break;
case 217: // set power pin
if(!cps[P].isUnused())
{
powerpin = Pin(ArduinoMap::getPort(cps[P].getInt()), ArduinoMap::getPinnum(cps[P].getInt()));
powerpin.setDirection(false);
powerpin.setValue(true);
}
state = DONE;
break;
case 220: // NOT STANDARD - set endstop minimum positions
SETOBJ(setMinStopPos(*this));
state = DONE;
break;
case 221: // NOT STANDARD - set endstop minimum positions
SETOBJ(setMaxStopPos(*this));
state = DONE;
break;
#ifdef HAS_LCD
case 250: // NOT STANDARD - LCD RS pin
if(!cps[P].isUnused())
LCDKEYPAD.setRS(cps[P].getInt());
state = DONE;
break;
case 251: // NOT STANDARD - LCD RW pin
if(!cps[P].isUnused())
LCDKEYPAD.setRW(cps[P].getInt());
state = DONE;
break;
case 252: // NOT STANDARD - LCD E pin
if(!cps[P].isUnused())
LCDKEYPAD.setE(cps[P].getInt());
state = DONE;
break;
case 253: // NOT STANDARD - LCD D pins
if(!cps[P].isUnused() && !cps[S].isUnused())
LCDKEYPAD.setD(cps[S].getInt(), cps[P].getInt());
state = DONE;
break;
case 254: // NOT STANDARD - Keypad Row Pins
if(!cps[P].isUnused() && !cps[S].isUnused())
LCDKEYPAD.setRowPin(cps[S].getInt(), cps[P].getInt());
state = DONE;
break;
case 255: // NOT STANDARD - Keypad Col Pins
if(!cps[P].isUnused() && !cps[S].isUnused())
LCDKEYPAD.setColPin(cps[S].getInt(), cps[P].getInt());
state = DONE;
break;
case 256: // NOT STANDARD - reinit LCD. This is a temporary 'fix' for the init timing errors
LCDKEYPAD.reinit();
state = DONE;
break;
#endif
case 300: // NOT STANDARD - set axis STEP pin
SETOBJ(setStepPins(*this));
state = DONE;
break;
case 301: // NOT STANDARD - set axis DIR pin
SETOBJ(setDirPins(*this));
state = DONE;
break;
case 302: // NOT STANDARD - set axis ENABLE pin
SETOBJ(setEnablePins(*this));
state = DONE;
break;
case 304: // NOT STANDARD - set axis MIN pin
SETOBJ(setMinPins(*this));
state = DONE;
break;
case 305: // NOT STANDARD - set axis MAX pin
SETOBJ(setMaxPins(*this));
state = DONE;
break;
case 307: // NOT STANDARD - set axis invert
SETOBJ(setAxisInvert(*this));
state = DONE;
break;
case 308: // NOT STANDARD - set axis disable
SETOBJ(setAxisDisable(*this));
state = DONE;
break;
case 309: // NOT STANDARD - set global endstop invert, endstop pullups.
SETOBJ(setEndstopGlobals(cps[P].getInt() == 1 ? true: false, cps[S].getInt() == 1 ? true: false));
state = DONE;
break;
case 310: // NOT STANDARD - report axis configuration status
SETOBJ(reportConfigStatus(Host::Instance(source)));
state = DONE;
break;
case 350: // NOT STANDARD - change gcode optimization
if(!cps[P].isUnused() && cps[P].getInt() == 1)
GCODES.enableOptimize();
else
GCODES.disableOptimize();
state = DONE;
break;
case 351: // NOT STANDARD - disable extruder commands (test print option)
if(!cps[P].isUnused() && cps[P].getInt() == 1)
DONTRUNEXTRUDER = true;
else
DONTRUNEXTRUDER = false;
state = DONE;
break;
// case 400,402 handled by GcodeQueue immediately, not queued.
case 401: // Execute stored eeprom code.
Host::Instance(source).write_P(PSTR("EEPROM READ: "));
if(eeprom::beginRead())
Host::Instance(source).write_P(PSTR("BEGUN"));
else
Host::Instance(source).write_P(PSTR("FAIL"));
Host::Instance(source).endl();
state = DONE;
break;
// NOT STANDARD - set thermistor table
case 501: case 502: case 503: case 504: case 505: case 506: case 507: case 508: case 509: case 510:
case 511: case 512: case 513: case 514: case 515: case 516: case 517: case 518: case 519: case 520:
if(!cps[P].isUnused() && !cps[S].isUnused())
{
TEMPERATURE.changeTempTable(cps[P].getInt(), cps[S].getInt(), cps[M].getInt() - 501);
}
state = DONE;
break;
default:
Host::Instance(source).labelnum("warn ", linenum, false);
Host::Instance(source).write_P(PSTR(" MCODE ")); Host::Instance(source).write(cps[M].getInt(), 10); Host::Instance(source).write_P(PSTR(" NOT SUPPORTED\n"));
state = DONE;
break;
}
#ifndef REPRAP_COMPAT
if(state == DONE)
{
if(linenum != -1)
Host::Instance(source).labelnum("done ", linenum, false);
else
Host::Instance(source).write_P(PSTR("done "));
Host::Instance(source).labelnum(" M", cps[M].getInt(), true);
}
#endif
}
void GCode::resetlastpos()
{
#ifndef USE_MARLIN
lastpos = MOTION.getCurrentPosition();
#endif
// TODO?
}
void GCode::doPinSet(int arduinopin, int on)
{
Pin p = Pin(ArduinoMap::getPort(arduinopin), ArduinoMap::getPinnum(arduinopin));
p.setDirection(true);
p.setValue(on == 0 ? false : true);
}
void GCode::togglefan()
{
#ifndef USE_MBIEC
if(fanpin.isNull())
return;
fanpin.setValue(!fanpin.getValue());
#else
TEMPERATURE.togglefan();
#endif
}