forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCodes.cpp
4651 lines (4129 loc) · 105 KB
/
GCodes.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
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************************************
RepRapFirmware - G Codes
This class interprets G Codes from one or more sources, and calls the functions in Move, Heat etc
that drive the machine to do what the G Codes command.
Most of the functions in here are designed not to wait, and they return a boolean. When you want them to do
something, you call them. If they return false, the machine can't do what you want yet. So you go away
and do something else. Then you try again. If they return true, the thing you wanted done has been done.
-----------------------------------------------------------------------------------------------------
Version 0.1
13 February 2013
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
const float minutesToSeconds = 60.0;
const float secondsToMinutes = 1.0 / minutesToSeconds;
GCodes::GCodes(Platform* p, Webserver* w)
{
active = false;
platform = p;
webserver = w;
webGCode = new GCodeBuffer(platform, "web: ");
fileGCode = new GCodeBuffer(platform, "file: ");
serialGCode = new GCodeBuffer(platform, "serial: ");
auxGCode = new GCodeBuffer(platform, "aux: ");
fileMacroGCode = new GCodeBuffer(platform, "macro: ");
queuedGCode = new GCodeBuffer(platform, "queued: ");
}
void GCodes::Exit()
{
platform->Message(BOTH_MESSAGE, "GCodes class exited.\n");
active = false;
}
void GCodes::Init()
{
Reset();
drivesRelative = true;
axesRelative = false;
ARRAY_INIT(axisLetters, AXIS_LETTERS);
distanceScale = 1.0;
for (int8_t extruder = 0; extruder < DRIVES - AXES; extruder++)
{
lastExtruderPosition[extruder] = 0.0;
}
configFile = NULL;
eofString = EOF_STRING;
eofStringCounter = 0;
eofStringLength = strlen(eofString);
homing = false;
homeX = false;
homeY = false;
homeZ = false;
offSetSet = false;
zProbesSet = false;
active = true;
longWait = platform->Time();
dwellTime = longWait;
limitAxes = true;
axisIsHomed[X_AXIS] = axisIsHomed[Y_AXIS] = axisIsHomed[Z_AXIS] = false;
toolChangeSequence = 0;
coolingInverted = false;
lastFanValue = 0.0;
internalCodeQueue = NULL;
releasedQueueItems = NULL;
for(uint8_t i=0; i<codeQueueLength; i++)
{
releasedQueueItems = new CodeQueueItem(releasedQueueItems);
}
}
// This is called from Init and when doing an emergency stop
void GCodes::Reset()
{
webGCode->Init();
fileGCode->Init();
serialGCode->Init();
auxGCode->Init();
fileMacroGCode->Init();
queuedGCode->Init();
moveAvailable = false;
totalMoves = 0;
movesCompleted = 0;
fileBeingPrinted.Close();
fileToPrint.Close();
fileBeingWritten = NULL;
endStopsToCheck = 0;
doingFileMacro = returningFromMacro = false;
doPauseMacro = isPausing = isResuming = false;
fractionOfFilePrinted = -1.0;
dwellWaiting = false;
stackPointer = 0;
waitingForMoveToComplete = false;
probeCount = 0;
cannedCycleMoveCount = 0;
cannedCycleMoveQueued = false;
auxDetected = false;
}
void GCodes::Spin()
{
if (!active)
return;
// Macro files are the most important. We must finish them in one go before we proceed
// with the other G-Codes, because at least one of them will call DoFileMacro() again.
if (doingFileMacro && !returningFromMacro)
{
if (fileMacroGCode->Active())
{
// We must NOT enqueue macro G-Codes, because the code queue doesn't work for these
fileMacroGCode->SetFinished(ActOnCode(fileMacroGCode, true));
}
else
{
// Process more of the macro file
size_t i = 0;
do
{
char b;
if (fileBeingPrinted.Read(b))
{
if (fileMacroGCode->Put(b))
{
fileMacroGCode->SetFinished(ActOnCode(fileMacroGCode, true));
break;
}
}
else
{
if (!fileMacroGCode->IsEmpty())
{
if (fileMacroGCode->Put('\n')) // In case there wasn't one ending the file
{
fileMacroGCode->SetFinished(ActOnCode(fileMacroGCode, true));
break;
}
}
if (!fileMacroGCode->Active())
{
fileBeingPrinted.Close();
returningFromMacro = true;
}
break;
}
++i;
} while (i < GCODE_LENGTH);
}
platform->ClassReport(longWait);
return;
}
// Check each of the sources of G Codes (web, aux, serial, queued and file) to
// see if they are finished in order to feed them new codes.
//
// Note the order establishes a priority: web, aux, serial, queued, file.
// If file weren't last, then the others would never get a look in when
// a file was being printed.
if (!webGCode->Active() && webserver->GCodeAvailable())
{
uint8_t i = 0;
do
{
char b = webserver->ReadGCode();
if (webGCode->Put(b))
{
// we have a complete gcode
if (webGCode->WritingFileDirectory() != NULL)
{
WriteGCodeToFile(webGCode);
webGCode->SetFinished(true);
}
else
{
webGCode->SetFinished(ActOnCode(webGCode, true));
}
break; // stop after receiving a complete gcode in case we haven't finished processing it
}
++i;
} while (i < GCODE_LENGTH && webserver->GCodeAvailable());
platform->ClassReport(longWait);
return;
}
// Now the serial interfaces.
if (!auxGCode->Active() && (platform->GetAux()->Status() & byteAvailable))
{
uint8_t i = 0;
do
{
char b;
platform->GetAux()->Read(b);
if (auxGCode->Put(b)) // add char to buffer and test whether the gcode is complete
{
auxDetected = true;
auxGCode->SetFinished(ActOnCode(auxGCode, true));
break; // stop after receiving a complete gcode in case we haven't finished processing it
}
++i;
} while (i < GCODE_LENGTH && (platform->GetAux()->Status() & byteAvailable));
platform->ClassReport(longWait);
return;
}
if (platform->GetLine()->Status() & byteAvailable)
{
// First check the special case of uploading the reprap.htm file
if (serialGCode->WritingFileDirectory() == platform->GetWebDir())
{
char b;
platform->GetLine()->Read(b);
WriteHTMLToFile(b, serialGCode);
platform->ClassReport(longWait);
return;
}
// Otherwise just deal in general with incoming bytes from the serial interface
else if (!serialGCode->Active())
{
// Read several bytes instead of just one. This approximately doubles the speed of file uploading.
uint8_t i = 0;
do
{
char b;
platform->GetLine()->Read(b);
if (serialGCode->Put(b)) // add char to buffer and test whether the gcode is complete
{
// we have a complete gcode
if (serialGCode->WritingFileDirectory() != NULL)
{
WriteGCodeToFile(serialGCode);
serialGCode->SetFinished(true);
}
else
{
serialGCode->SetFinished(ActOnCode(serialGCode, reprap.GetMove()->IsPaused()));
}
break; // stop after receiving a complete gcode in case we haven't finished processing it
}
++i;
} while (i < GCODE_LENGTH && (platform->GetLine()->Status() & byteAvailable));
platform->ClassReport(longWait);
return;
}
}
// Then check if there are any queued codes left to be executed in-time
if (internalCodeQueue != NULL)
{
if (!queuedGCode->Active() && reprap.GetMove()->IsRunning())
{
// Check if the last queued code is complete and remove its entry
if (internalCodeQueue->IsExecuting())
{
CodeQueueItem *temp = internalCodeQueue;
internalCodeQueue = internalCodeQueue->Next();
temp->SetNext(releasedQueueItems);
releasedQueueItems = temp;
platform->ClassReport(longWait);
return;
}
// Check if a new code can be executed
else if (internalCodeQueue->ExecuteAtMove() <= movesCompleted)
{
internalCodeQueue->Execute();
if (queuedGCode->Put(internalCodeQueue->GetCommand(), internalCodeQueue->GetCommandLength()))
{
queuedGCode->SetFinished(ActOnCode(queuedGCode, true));
}
platform->ClassReport(longWait);
return;
}
}
}
else if (totalMoves == movesCompleted != 0)
{
// If we don't have any queued codes left and all moves are complete, we can safely reset our counters here
totalMoves = 0;
movesCompleted = 0;
}
// At last, see if we can read some more bytes from the the file being printed
if (!fileGCode->Active() && reprap.GetMove()->IsRunning() && fileBeingPrinted.IsLive())
{
uint8_t i = 0;
do
{
char b;
if (fileBeingPrinted.Read(b))
{
if (fileGCode->Put(b))
{
fileGCode->SetFinished(ActOnCode(fileGCode));
break;
}
}
else
{
if (fileGCode->Put('\n')) // In case there wasn't one ending the file
{
fileGCode->SetFinished(ActOnCode(fileGCode));
}
if (!fileGCode->Active() && AllMovesAreFinishedAndMoveBufferIsLoaded())
{
fileBeingPrinted.Close();
reprap.GetPrintMonitor()->StoppedPrint();
}
break;
}
++i;
} while (i < GCODE_LENGTH);
platform->ClassReport(longWait);
return;
}
// Now run the G-Code buffers. It's important to fill up the G-Code buffers before we do this,
// otherwise we wouldn't have a chance to pause/cancel running prints.
if (webGCode->Active())
{
// Note: Direct web-printing has been dropped, so it's safe to execute web codes immediately
webGCode->SetFinished(ActOnCode(webGCode, true));
}
else if (auxGCode->Active())
{
// Don't use code-queuing for codes received from AUX
auxGCode->SetFinished(ActOnCode(auxGCode, true));
}
else if (serialGCode->Active())
{
// We want codes from the serial interface to be queued unless the print has been paused
serialGCode->SetFinished(ActOnCode(serialGCode, reprap.GetMove()->IsPaused()));
}
else if (queuedGCode->Active())
{
queuedGCode->SetFinished(ActOnCode(queuedGCode, true));
}
else if (fileGCode->Active())
{
fileGCode->SetFinished(ActOnCode(fileGCode));
}
platform->ClassReport(longWait);
}
void GCodes::Diagnostics()
{
platform->AppendMessage(BOTH_MESSAGE, "GCodes Diagnostics:\n");
platform->AppendMessage(BOTH_MESSAGE, "Move available? %s\n", moveAvailable ? "yes" : "no");
platform->AppendMessage(BOTH_MESSAGE, "Internal code queue is %s\n", (internalCodeQueue == NULL) ? "empty." :"not empty:");
if (internalCodeQueue != NULL)
{
platform->AppendMessage(BOTH_MESSAGE, "Total moves: %d, moves completed: %d\n", totalMoves, movesCompleted);
unsigned int count = 0;
CodeQueueItem *item = internalCodeQueue;
do {
count++;
platform->AppendMessage(BOTH_MESSAGE, "Queued '%s' for move %d\n", item->GetCommand(), item->ExecuteAtMove());
} while ((item = item->Next()) != NULL);
platform->AppendMessage(BOTH_MESSAGE, "%d codes have been queued.\n", count);
}
}
// The wait till everything's done function. If you need the machine to
// be idle before you do something (for example homing an axis, or shutting down) call this
// until it returns true. As a side-effect it loads moveBuffer with the last
// position and feedrate for you.
bool GCodes::AllMovesAreFinishedAndMoveBufferIsLoaded()
{
// Last one gone?
if (moveAvailable)
return false;
// Wait for all the queued moves to stop so we get the actual last position and feedrate
if (!reprap.GetMove()->AllMovesAreFinished())
return false;
reprap.GetMove()->AddMoreMoves();
// Load the last position; If Move can't accept more, return false - should never happen
if (!reprap.GetMove()->GetCurrentUserPosition(moveBuffer))
return false;
return true;
}
// Save (some of) the state of the machine for recovery in the future.
// Call repeatedly till it returns true.
bool GCodes::Push()
{
if (stackPointer >= STACK)
{
platform->Message(BOTH_ERROR_MESSAGE, "Push(): stack overflow!\n");
return true;
}
if (!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
drivesRelativeStack[stackPointer] = drivesRelative;
axesRelativeStack[stackPointer] = axesRelative;
feedrateStack[stackPointer] = moveBuffer[DRIVES];
for(size_t extruder=0; extruder<DRIVES-AXES; extruder++)
{
extruderPositionStack[stackPointer][extruder] = lastExtruderPosition[extruder];
}
doingFileMacroStack[stackPointer] = doingFileMacro;
fileStack[stackPointer].CopyFrom(fileBeingPrinted);
if (stackPointer == 0)
{
fractionOfFilePrinted = fileBeingPrinted.FractionRead(); // save this so that we don't return the fraction of the macro file read
}
stackPointer++;
platform->PushMessageIndent();
return true;
}
// Recover a saved state. Call repeatedly till it returns true.
bool GCodes::Pop()
{
if (stackPointer < 1)
{
platform->Message(BOTH_ERROR_MESSAGE, "Pop(): stack underflow!\n");
return true;
}
if (!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
stackPointer--;
if (stackPointer == 0)
{
fractionOfFilePrinted = -1.0; // restore live updates of fraction read from the file being printed
}
drivesRelative = drivesRelativeStack[stackPointer];
axesRelative = axesRelativeStack[stackPointer];
moveBuffer[DRIVES] = feedrateStack[stackPointer];
doingFileMacro = doingFileMacroStack[stackPointer];
for(size_t extruder=0; extruder<DRIVES-AXES; extruder++)
{
lastExtruderPosition[extruder] = extruderPositionStack[stackPointer][extruder];
}
fileBeingPrinted.MoveFrom(fileStack[stackPointer]);
endStopsToCheck = 0;
platform->PopMessageIndent();
return true;
}
// Move expects all axis movements to be absolute, and all
// extruder drive moves to be relative. This function serves that.
// If applyLimits is true and we have homed the relevant axes, then we don't allow movement beyond the bed.
bool GCodes::LoadMoveBufferFromGCode(GCodeBuffer *gb, bool doingG92, bool applyLimits)
{
// Zero every extruder drive as some drives may not be changed
for(size_t drive = AXES; drive < DRIVES; drive++)
{
moveBuffer[drive] = 0.0;
}
// Deal with feed rate
if (gb->Seen(FEEDRATE_LETTER))
{
moveBuffer[DRIVES] = gb->GetFValue() * distanceScale * secondsToMinutes; // G Code feedrates are in mm/minute; we need mm/sec
}
// First do extrusion, and check, if we are extruding, that we have a tool to extrude with
Tool* tool = reprap.GetCurrentTool();
if (gb->Seen(EXTRUDE_LETTER))
{
if (tool == NULL)
{
platform->Message(BOTH_ERROR_MESSAGE, "Attempting to extrude with no tool selected.\n");
return false;
}
int eMoveCount = tool->DriveCount();
if (eMoveCount > 0)
{
float eMovement[DRIVES-AXES];
if(tool->Mixing())
{
float length = gb->GetFValue();
for(size_t drive = 0; drive < tool->DriveCount(); drive++)
{
eMovement[drive] = length * tool->GetMix()[drive];
}
}
else
{
gb->GetFloatArray(eMovement, eMoveCount);
if (tool->DriveCount() != eMoveCount)
{
platform->Message(BOTH_ERROR_MESSAGE, "Wrong number of extruder drives for the selected tool: %s\n", gb->Buffer());
return false;
}
}
// Set the drive values for this tool.
for(size_t eDrive = 0; eDrive < eMoveCount; eDrive++)
{
size_t drive = tool->Drive(eDrive);
float moveArg = eMovement[eDrive] * distanceScale;
if (doingG92)
{
moveBuffer[drive + AXES] = moveArg;
lastExtruderPosition[drive] = moveArg;
}
else
{
if (drivesRelative)
{
moveBuffer[drive + AXES] = moveArg;
lastExtruderPosition[drive] += moveArg;
}
else
{
moveBuffer[drive + AXES] = moveArg - lastExtruderPosition[drive];
lastExtruderPosition[drive] = moveArg;
}
}
}
}
}
// Now the movement axes
const Tool *currentTool = reprap.GetCurrentTool();
for(size_t axis = 0; axis < AXES; axis++)
{
if(gb->Seen(axisLetters[axis]))
{
float moveArg = gb->GetFValue() * distanceScale;
if (doingG92)
{
axisIsHomed[axis] = true; // doing a G92 defines the absolute axis position
}
else
{
if (axesRelative)
{
moveArg += moveBuffer[axis];
}
else if (currentTool != NULL)
{
moveArg -= currentTool->GetOffset()[axis]; // adjust requested position to compensate for tool offset
}
if (applyLimits && axis < 2 && axisIsHomed[axis]) // limit X & Y moves unless doing G92
{
if (moveArg < platform->AxisMinimum(axis))
{
moveArg = platform->AxisMinimum(axis);
}
else if (moveArg > platform->AxisMaximum(axis))
{
moveArg = platform->AxisMaximum(axis);
}
}
}
moveBuffer[axis] = moveArg;
}
}
return true;
}
// This function is called for a G Code that makes a move.
// If the Move class can't receive the move (i.e. things have to wait), return 0.
// If we have queued the move and the caller doesn't need to wait for it to complete, return 1.
// If we need to wait for the move to complete before doing another one (because endstops are checked in this move), return 2.
int GCodes::SetUpMove(GCodeBuffer *gb)
{
// Last one gone yet?
if (moveAvailable)
return 0;
// Load the last position and feed rate into moveBuffer; If Move can't accept more, return false
if (!reprap.GetMove()->GetCurrentUserPosition(moveBuffer))
return 0;
// Check to see if the move is a 'homing' move that endstops are checked on.
endStopsToCheck = 0;
if (gb->Seen('S'))
{
if (gb->GetIValue() == 1)
{
for (size_t axis = 0; axis < AXES; axis++)
{
if (gb->Seen(axisLetters[axis]))
{
endStopsToCheck |= (1 << axis);
}
}
}
}
// Check for 'R' parameter here to go back to the coordinates at which the print was paused
if (gb->Seen('R') && gb->GetIValue() > 0)
{
moveAvailable = reprap.GetMove()->GetPauseCoordinates(moveBuffer);
if (moveAvailable)
{
// Allow specification of axis offsets as seen in dc42's firmware fork
for(size_t axis=0; axis<AXES; axis++)
{
if (gb->Seen(axisLetters[axis]))
{
moveBuffer[axis] += gb->GetFValue();
}
}
for(size_t drive=AXES; drive<DRIVES; drive++)
{
moveBuffer[drive] = 0.0;
}
if (gb->Seen(FEEDRATE_LETTER))
{
moveBuffer[DRIVES] = gb->GetFValue();
}
return 2;
}
else
{
platform->Message(BOTH_ERROR_MESSAGE, "Could not obtain pause coordinates!\n");
return 1;
}
}
// Load the move buffer with either the absolute movement required or the relative movement required
moveAvailable = LoadMoveBufferFromGCode(gb, false, (endStopsToCheck == 0) && limitAxes);
return (endStopsToCheck != 0 || reprap.GetMove()->IsPaused()) ? 2 : 1;
}
// The Move class calls this function to find what to do next.
bool GCodes::ReadMove(float m[], EndstopChecks& ce)
{
if (!moveAvailable)
return false;
for (size_t i = 0; i <= DRIVES; i++) // 1 more for feedrate
{
m[i] = moveBuffer[i];
}
ce = endStopsToCheck;
moveAvailable = false;
endStopsToCheck = 0;
return true;
}
bool GCodes::DoFileMacro(const char* fileName)
{
// Are we returning from a macro?
if (returningFromMacro)
{
if (!Pop())
{
return false;
}
returningFromMacro = false;
return true;
}
// No, see if we can push some values on the stack
if (!Push())
{
return false;
}
// Then see if we can open the file
FileStore *f = platform->GetFileStore((fileName[0] == '/') ? "0:" : platform->GetSysDir(), fileName, false);
if (f == NULL)
{
platform->Message(BOTH_ERROR_MESSAGE, "Macro file %s not found.\n", fileName);
if(!Pop())
{
platform->Message(BOTH_ERROR_MESSAGE, "Cannot pop the stack.\n");
}
return true;
}
fileBeingPrinted.Set(f);
// Deal with nested macros (rewind back to the position before the last code so it is called again later)
unsigned int lastStackPointer = stackPointer - 1;
if (doingFileMacroStack[lastStackPointer])
{
fileStack[lastStackPointer].Seek(fileStack[lastStackPointer].Position() - fileMacroGCode->Length());
}
// Set some values so the macro file gets processed properly
doingFileMacro = true;
fileMacroGCode->Init();
return false;
}
bool GCodes::FileMacroCyclesReturn()
{
if (!doingFileMacro)
return true;
if (!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
fileBeingPrinted.Close();
fileMacroGCode->Init();
returningFromMacro = true;
return Pop();
}
// To execute any move, call this until it returns true.
// moveToDo[] entries corresponding with false entries in action[] will
// be ignored. Recall that moveToDo[DRIVES] should contain the feed rate
// you want (if action[DRIVES] is true).
bool GCodes::DoCannedCycleMove(EndstopChecks ce)
{
// Is the move already running?
if (cannedCycleMoveQueued)
{ // Yes.
if (!Pop()) // Wait for the move to finish then restore the state
{
return false;
}
cannedCycleMoveQueued = false;
return true;
}
else
{ // No.
if (!Push()) // Wait for the RepRap to finish whatever it was doing, save it's state, and load moveBuffer[] with the current position.
{
return false;
}
for (size_t drive = 0; drive <= DRIVES; drive++)
{
if (activeDrive[drive])
{
moveBuffer[drive] = moveToDo[drive];
}
}
endStopsToCheck = ce;
cannedCycleMoveQueued = true;
moveAvailable = true;
}
return false;
}
// This sets positions. I.e. it handles G92.
bool GCodes::SetPositions(GCodeBuffer *gb)
{
if (!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
if(LoadMoveBufferFromGCode(gb, true, false))
{
SetPositions(moveBuffer);
}
return true;
}
void GCodes::SetPositions(float positionNow[DRIVES])
{
// Transform the position so that e.g. if the user does G92 Z0,
// the position we report (which gets inverse-transformed) really is Z=0 afterwards
reprap.GetMove()->Transform(moveBuffer);
reprap.GetMove()->SetLiveCoordinates(moveBuffer);
reprap.GetMove()->SetPositions(moveBuffer);
}
// Offset the axes by the X, Y, and Z amounts in the M code in gb. Say the machine is at [10, 20, 30] and
// the offsets specified are [8, 2, -5]. The machine will move to [18, 22, 25] and henceforth consider that point
// to be [10, 20, 30].
bool GCodes::OffsetAxes(GCodeBuffer* gb)
{
if (!offSetSet)
{
if (!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
for(size_t drive = 0; drive <= DRIVES; drive++)
{
if (drive < AXES || drive == DRIVES)
{
record[drive] = moveBuffer[drive];
moveToDo[drive] = moveBuffer[drive];
}
else
{
record[drive] = 0.0;
moveToDo[drive] = 0.0;
}
activeDrive[drive] = false;
}
for(size_t axis = 0; axis < AXES; axis++)
{
if (gb->Seen(axisLetters[axis]))
{
moveToDo[axis] += gb->GetFValue();
activeDrive[axis] = true;
}
}
if (gb->Seen(FEEDRATE_LETTER)) // Has the user specified a feedrate?
{
moveToDo[DRIVES] = gb->GetFValue();
activeDrive[DRIVES] = true;
}
offSetSet = true;
}
if (DoCannedCycleMove(0))
{
for (size_t drive = 0; drive <= DRIVES; drive++)
{
moveBuffer[drive] = record[drive];
}
reprap.GetMove()->SetLiveCoordinates(record); // This doesn't transform record
reprap.GetMove()->SetPositions(record); // This does
offSetSet = false;
return true;
}
return false;
}
// Home one or more of the axes. Which ones are decided by the
// booleans homeX, homeY and homeZ.
// Returns true if completed, false if needs to be called again.
// 'reply' is only written if there is an error.
// 'error' is false on entry, gets changed to true if there is an error.
bool GCodes::DoHome(StringRef& reply, bool& error)
//pre(reply.upb == STRING_LENGTH)
{
if (homeX && homeY && homeZ)
{
if (!homing)
{
homing = true;
axisIsHomed[X_AXIS] = false;
axisIsHomed[Y_AXIS] = false;
axisIsHomed[Z_AXIS] = false;
}
if (DoFileMacro(HOME_ALL_G))
{
homing = false;
homeX = false;
homeY = false;
homeZ = false;
return true;
}
return false;
}
if (homeX)
{
if (!homing)
{
homing = true;
axisIsHomed[X_AXIS] = false;
}
if (DoFileMacro(HOME_X_G))
{
homing = false;
homeX = false;
return NoHome();
}
return false;
}
if (homeY)
{
if (!homing)
{
homing = true;
axisIsHomed[Y_AXIS] = false;
}
if (DoFileMacro(HOME_Y_G))
{
homing = false;
homeY = false;
return NoHome();
}
return false;
}
if (homeZ)
{
if (platform->MustHomeXYBeforeZ() && (!axisIsHomed[X_AXIS] || !axisIsHomed[Y_AXIS]))
{
// We can only home Z if X and Y have already been homed
reply.copy("Must home X and Y before homing Z\n");
error = true;
homing = false;
homeZ = false;
return true;
}
if (!homing)
{
homing = true;
axisIsHomed[Z_AXIS] = false;
}
if (DoFileMacro(HOME_Z_G))
{
homing = false;
homeZ = false;
return NoHome();
}
return false;
}
// Should never get here
endStopsToCheck = 0;
moveAvailable = false;
return true;
}
// This lifts Z a bit, moves to the probe XY coordinates (obtained by a call to GetProbeCoordinates() ),
// probes the bed height, and records the Z coordinate probed. If you want to program any general
// internal canned cycle, this shows how to do it.
bool GCodes::DoSingleZProbeAtPoint()
{
reprap.GetMove()->SetIdentityTransform(); // It doesn't matter if these are called repeatedly
for (size_t drive = 0; drive <= DRIVES; drive++)
{
activeDrive[drive] = false;
}
switch (cannedCycleMoveCount)
{
case 0: // Raise Z to 5mm. This only does anything on the first move; on all the others Z is already there
moveToDo[Z_AXIS] = Z_DIVE;
activeDrive[Z_AXIS] = true;
moveToDo[DRIVES] = platform->MaxFeedrate(Z_AXIS);
activeDrive[DRIVES] = true;
reprap.GetMove()->SetZProbing(false);
if (DoCannedCycleMove(0))
{
cannedCycleMoveCount++;
}
return false;