-
Notifications
You must be signed in to change notification settings - Fork 583
Expand file tree
/
Copy pathGCodes2.cpp
More file actions
4972 lines (4559 loc) · 137 KB
/
GCodes2.cpp
File metadata and controls
4972 lines (4559 loc) · 137 KB
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
/*
* GCodes2.cpp
*
* Created on: 3 Dec 2016
* Author: David
*
* This file contains the code to see what G, M or T command we have and start processing it.
*/
#include "GCodes.h"
#include "GCodeBuffer/GCodeBuffer.h"
#include "GCodeException.h"
#include "GCodeQueue.h"
#include "Heating/Heat.h"
#if HAS_SBC_INTERFACE
# include <SBC/SbcInterface.h>
#endif
#include <Movement/Move.h>
#include <PrintMonitor/PrintMonitor.h>
#include <Platform/RepRap.h>
#include <Tools/Tool.h>
#include <Endstops/ZProbe.h>
#include <FilamentMonitors/FilamentMonitor.h>
#include <General/IP4String.h>
#include <Movement/StepperDrivers/DriverMode.h>
#include <Hardware/SoftwareReset.h>
#include <Hardware/ExceptionHandlers.h>
#include <Version.h>
#if SUPPORT_IOBITS
# include <Platform/PortControl.h>
#endif
#if HAS_NETWORKING
# include <Networking/Network.h>
#endif
#if SUPPORT_MQTT
# include <Networking/MQTT/MqttClient.h>
#endif
#if HAS_WIFI_NETWORKING
# include <Comms/FirmwareUpdater.h>
#endif
#if SUPPORT_DIRECT_LCD
# include <Display/Display.h>
#endif
#if SUPPORT_CAN_EXPANSION
# include <CAN/CanInterface.h>
# include <CAN/ExpansionManager.h>
#endif
#if SUPPORT_ACCELEROMETERS
# include <Accelerometers/Accelerometers.h>
#endif
#ifdef DUET3_ATE
# include <Duet3Ate.h>
#endif
#if HAS_MASS_STORAGE
# include <Platform/Logger.h>
#endif
// If the code to act on is completed, this returns true, otherwise false.
// It is called repeatedly for a given code until it returns true for that code.
bool GCodes::ActOnCode(GCodeBuffer& gb, const StringRef& reply) noexcept
{
#if SUPPORT_ASYNC_MOVES
// If we are running multiple motion systems in single input stream mode and we have resumed after a pause, then we may need to skip some commands
// Also if we did a synchronous pause then we need to sip the pause command
{
if (&gb == FileGCode() && gb.ExecutingAll())
{
const FilePosition offsetToSkipTo = GetMovementState(gb).fileOffsetToSkipTo;
if (offsetToSkipTo != 0)
{
const FilePosition jobFilePos = gb.GetJobFilePosition();
if (jobFilePos < offsetToSkipTo)
{
// Skip any command except M596
if (!(gb.GetCommandLetter() == 'M' && gb.HasCommandNumber() && gb.GetCommandNumber() == 596))
{
return true;
}
}
else if (jobFilePos == offsetToSkipTo)
{
// If we paused on a synchronous pause command, skip it
int commandNumber;
if ( gb.GetCommandLetter() == 'M'
&& ((commandNumber = gb.GetCommandNumber()) == 226 || commandNumber == 600 || commandNumber == 601 || commandNumber == 25)
)
{
return true;
}
}
else
{
GetMovementState(gb).fileOffsetToSkipTo = 0; // clear this to speed up the test next time
}
}
}
}
#endif
try
{
switch (gb.GetCommandLetter())
{
case 'G':
if (gb.HasCommandNumber())
{
return HandleGcode(gb, reply);
}
break;
case 'M':
if (gb.HasCommandNumber())
{
return HandleMcode(gb, reply);
}
break;
case 'T':
return HandleTcode(gb, reply);
case 'Q':
return HandleQcode(gb, reply);
default:
break;
}
}
catch (const GCodeException& e)
{
e.GetMessage(reply, &gb);
gb.StopTimer();
UnlockAll(gb);
HandleReply(gb, GCodeResult::error, reply.c_str());
return true;
}
// If we get here then we didn't see a command that was worth parsing
reply.printf("Bad command: ");
gb.AppendFullCommand(reply);
HandleReply(gb, GCodeResult::error, reply.c_str());
return true;
}
// Handle G-command returning true if the command completed, false if this function needs to be called again to complete it
bool GCodes::HandleGcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
const int code = gb.GetCommandNumber();
if (stopped)
{
HandleResult(gb, GCodeResult::stopped, reply, nullptr);
return true;
}
GCodeResult result = GCodeResult::ok;
if (IsSimulating() && code > 4 // move & dwell
&& code != 10 && code != 11 // (un)retract
&& code != 17 && code != 18 && code != 19 // selected plane for arc moves
&& code != 68 && code != 69 // coordinate rotation
&& code != 20 && code != 21 // change units
&& (code < 53 || code > 59) // coordinate system
&& (code < 90 || code > 94)) // positioning & feedrate modes
{
HandleReply(gb, result, "");
return true; // we only simulate some gcodes
}
// The only queued GCodes are some subfunctions of G10, so we delay checking for queueing this command until we are in case 10 below
#if SUPPORT_ASYNC_MOVES
const bool executing = gb.Executing(); // this is used by the BREAK_IF_NOT_PRIMARY macro and elsewhere
#endif
if (gb.GetCommandFraction() > 0
&& code != 38 && code != 59 // these are the only G-codes we implement that can have fractional parts
)
{
#if SUPPORT_ASYNC_MOVES
if (executing)
{
result = TryMacroFile(gb);
}
else
{
HandleReply(gb, result, "");
return true;
}
#else
result = TryMacroFile(gb);
#endif
}
else
{
#if SUPPORT_ASYNC_MOVES
# define BREAK_IF_NOT_EXECUTING if (!executing) { break; }
#else
# define BREAK_IF_NOT_EXECUTING // nothing
#endif
switch (code)
{
case 0: // Rapid move
case 1: // Ordinary move
BREAK_IF_NOT_EXECUTING
if (GetMovementState(gb).segmentsLeft != 0) // do this check first to avoid locking movement unnecessarily
{
return false;
}
if (!LockMovement(gb))
{
return false;
}
try
{
if (!DoStraightMove(gb, code == 1))
{
return false;
}
} catch (const GCodeException& exc)
{
platform.GetEndstops().ClearEndstops(); // DoStraightMove may have enabled endstops before quitting, so disable them
gb.SetState(GCodeState::abortWhenMovementFinished); // empty the queue before ending simulation, and force the user position to be restored
gb.LatestMachineState().SetError(exc); // must do this *after* calling SetState
}
break;
case 2: // Clockwise arc
case 3: // Anti clockwise arc
// We only support X and Y axes in these (and optionally Z for corkscrew moves), but you can map them to other axes in the tool definitions
BREAK_IF_NOT_EXECUTING
if (GetMovementState(gb).segmentsLeft != 0) // do this check first to avoid locking movement unnecessarily
{
return false;
}
if (!LockMovement(gb))
{
return false;
}
try
{
if (!DoArcMove(gb, code == 2))
{
return false;
}
} catch (GCodeException& exc)
{
gb.SetState(GCodeState::abortWhenMovementFinished); // empty the queue before ending simulation, and force the user position to be restored
gb.LatestMachineState().SetError(exc); // must do this *after* calling SetState
}
break;
case 4: // Dwell
result = DoDwell(gb);
break;
case 10: // Set/report offsets and temperatures, or retract
{
if (gb.Seen('L'))
{
const uint32_t ival = gb.GetUIValue();
switch (ival)
{
case 1:
BREAK_IF_NOT_EXECUTING
result = SetOrReportOffsets(gb, reply, 10); // same as G10 with offsets and no L parameter
break;
case 2:
result = GetSetWorkplaceCoordinates(gb, reply, false);
break;
case 20:
result = GetSetWorkplaceCoordinates(gb, reply, true);
break;
default:
result = GCodeResult::badOrMissingParameter;
break;
}
}
else
{
BREAK_IF_NOT_EXECUTING
bool modifyingTool = gb.Seen('P') || gb.Seen('R') || gb.Seen('S');
for (size_t axis = 0; axis < numVisibleAxes; ++axis)
{
modifyingTool |= gb.Seen(axisLetters[axis]);
}
if (modifyingTool)
{
if (IsSimulating())
{
break;
}
// Should we queue this code?
if (gb.CanQueueCodes())
{
GCodeQueue * const codeQueue = GetMovementState(gb).codeQueue;
if (codeQueue->ShouldQueueG10(gb))
{
// Don't queue any GCodes if there are segments not yet picked up by Move, because in the event that a segment corresponds to no movement,
// the move gets discarded, which throws out the count of scheduled moves and hence the synchronisation
if (GetMovementState(gb).segmentsLeft == 0 && codeQueue->QueueCode(gb))
{
HandleReply(gb, GCodeResult::ok, "");
return true;
}
return false; // we should queue this code but we can't yet, so wait until we can either execute it or queue it
}
}
// We don't want to queue it
result = SetOrReportOffsets(gb, reply, 10);
}
else
{
result = RetractFilament(gb, true);
}
}
}
break;
case 11: // Un-retract
BREAK_IF_NOT_EXECUTING
result = RetractFilament(gb, false);
break;
case 17: // Select XY plane for G2/G3
case 18: // Select XZ plane
case 19: // Select YZ plane
// If the plane is changing, wait for motion to stop so that if we get a power failure then the selected plane will match
// any not-yet-executed arc moves in the queue and we can resurrect those moves.
// However, some generators prefix every G2/G3 command with G17/18/19 so don't wait if the plane is not changing.
{
const unsigned int newPlane = (unsigned int)code - 17;
if (newPlane != gb.LatestMachineState().selectedPlane)
{
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
gb.LatestMachineState().selectedPlane = newPlane;
reprap.InputsUpdated();
}
}
break;
case 20: // Inches (which century are we living in, here?)
case 21: // mm
gb.UseInches(code == 20);
reprap.InputsUpdated();
break;
case 28: // Home
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
BREAK_IF_NOT_EXECUTING
result = DoHome(gb, reply);
break;
case 29: // Grid-based bed probing
if (!LockCurrentMovementSystemAndWaitForStandstill(gb)) // do this first to make sure that a new grid isn't being defined
{
return false;
}
BREAK_IF_NOT_EXECUTING
{
int sparam;
if (gb.Seen('S'))
{
sparam = gb.GetIValue();
}
else if (DoFileMacroWithParameters(gb, MESH_G, false, 29)) // no S parameter found so try to execute mesh.g
{
break;
}
else
{
sparam = 0; // mesh.g not found, so treat G29 the same as G29 S0
}
switch(sparam)
{
case 0: // probe and save height map
result = ProbeGrid(gb, reply);
break;
case 1: // load height map file
#if HAS_MASS_STORAGE || HAS_SBC_INTERFACE
result = LoadHeightMap(gb, reply);
#else
result = GCodeResult::errorNotSupported;
#endif
break;
case 2: // clear height map
ClearBedMapping();
#if SUPPORT_PROBE_POINTS_FILE
ClearProbePointsInvalid();
#endif
break;
case 3: // save height map to names file
#if HAS_MASS_STORAGE || HAS_SBC_INTERFACE
result = SaveHeightMap(gb, reply);
#else
result = GCodeResult::errorNotSupported;
#endif
break;
case 4: // read grid parameters and can/cannot probe map
#if (HAS_MASS_STORAGE || HAS_SBC_INTERFACE) && SUPPORT_PROBE_POINTS_FILE
result = LoadProbePointsMap(gb, reply);
#else
result = GCodeResult::errorNotSupported;
#endif
break;
default:
result = GCodeResult::badOrMissingParameter;
break;
}
}
break;
case 30: // Z probe/manually set at a position and set that as point P
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
BREAK_IF_NOT_EXECUTING
if (reprap.GetMove().GetKinematics().AxesToHomeBeforeProbing().Intersects(~axesVirtuallyHomed))
{
reply.copy("Insufficient axes homed for bed probing");
result = GCodeResult::error;
}
else
{
result = ExecuteG30(gb, reply);
}
break;
case 31: // Return the probe value, or set probe variables
BREAK_IF_NOT_EXECUTING
result = platform.GetEndstops().HandleG31(gb, reply);
break;
case 32: // Probe Z at multiple positions and generate the bed transform
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
BREAK_IF_NOT_EXECUTING
#if SUPPORT_ASYNC_MOVES
AllocateAxes(gb, GetMovementState(gb), AxesBitmap::MakeFromBits(Z_AXIS), ParameterLetterToBitmap('Z'));
#endif
// We need to unlock the movement system here in case there is no Z probe and we are doing manual probing.
// Otherwise, even though the bed probing code calls UnlockAll when doing a manual bed probe, the movement system
// remains locked because the current MachineState object already held the lock when the macro file was started,
// which means that no gcode source other than the one that executed G32 is allowed to jog the Z axis.
UnlockAll(gb);
DoFileMacroWithParameters(gb, BED_EQUATION_G, true, 32); // Try to execute bed.g
break;
case 38: // Straight probe - move until either the probe is triggered or the commanded move ends
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
BREAK_IF_NOT_EXECUTING
result = StraightProbe(gb, reply);
break;
case 53: // Temporarily use machine coordinates
BREAK_IF_NOT_EXECUTING
gb.LatestMachineState().g53Active = true;
break;
case 54: // Switch to coordinate system 1
case 55: // Switch to coordinate system 2
case 56: // Switch to coordinate system 3
case 57: // Switch to coordinate system 4
case 58: // Switch to coordinate system 5
case 59: // Switch to coordinate system 6,7,8,9
{
unsigned int cs = code - 54;
if (code == 59)
{
const int8_t fraction = gb.GetCommandFraction();
if (fraction > 0)
{
cs += (unsigned int)fraction;
}
}
if (cs < NumCoordinateSystems)
{
GetMovementState(gb).currentCoordinateSystem = cs; // this is the zero-base coordinate system number
reprap.MoveUpdated();
gb.LatestMachineState().g53Active = false; // cancel any active G53
}
else
{
result = GCodeResult::errorNotSupported;
}
}
break;
case 60: // Save position
BREAK_IF_NOT_EXECUTING
result = SavePosition(gb, reply);
break;
#if SUPPORT_COORDINATE_ROTATION
case 68: // Coordinate rotation
result = HandleG68(gb, reply);
break;
case 69: // Cancel coordinate rotation
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
# if SUPPORT_ASYNC_MOVES
if (gb.Executing())
# endif
{
g68Angle = 0.0;
UpdateCurrentUserPosition(gb);
}
break;
#endif
case 90: // Absolute coordinates
gb.LatestMachineState().axesRelative = false;
reprap.InputsUpdated();
break;
case 91: // Relative coordinates
gb.LatestMachineState().axesRelative = true; // Axis movements (i.e. X, Y and Z)
reprap.InputsUpdated();
break;
case 92: // Set position
BREAK_IF_NOT_EXECUTING
result = SetPositions(gb, reply);
break;
case 93: // inverse time mode
gb.LatestMachineState().inverseTimeMode = true;
reprap.InputsUpdated();
break;
case 94: //normal feed rate mode
gb.LatestMachineState().inverseTimeMode = false;
reprap.InputsUpdated();
break;
default:
#if HAS_SBC_INTERFACE
// Send unknown non-binary codes to DSF so potential plugins can interpret them
if (reprap.UsingSbcInterface() && reprap.GetSbcInterface().IsConnected() && !gb.IsBinary())
{
gb.SendToSbc();
return false;
}
#endif
result = TryMacroFile(gb);
break;
}
#undef BREAK_IF_NOT_EXECUTING
}
return HandleResult(gb, result, reply, nullptr);
}
bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
const int code = gb.GetCommandNumber();
if (stopped && code != 105 && code != 112 && code != 115 && code != 122 && code != 408 && code != 409 && code != 999)
{
HandleResult(gb, GCodeResult::stopped, reply, nullptr);
return true;
}
// In simulation mode we don't execute most M-commands
if ( IsSimulating()
&& (code < 20 || code > 37) // allow file operations while simulating
&& code != 0 && code != 1 && code != 82 && code != 83
&& code != 105 && code != 109 && code != 111 && code != 112 && code != 115 && code != 122
&& code != 200 && code != 204 && code != 205 && code != 207
&& code != 408 && code != 409 && code != 486
&& code != 572 && code != 593 // allow changes to PA and IS while simulating
&& code != 997 && code != 999 // allow reset and firmware update while simulating
)
{
HandleReply(gb, GCodeResult::ok, "");
return true; // we don't simulate most M codes
}
#if SUPPORT_ASYNC_MOVES
// If we are not the primary GCode reader for this stream, we don't execute most M-commands
if (!gb.Executing())
{
switch (code)
{
case 0:
case 1:
case 2:
case 23:
case 24:
case 32:
case 37:
case 82:
case 83:
case 98:
case 99:
case 112:
case 116:
case 120:
case 121:
case 190:
case 191:
case 555:
case 596:
case 598:
// These commands are executed by all GCode processors, at least to start with
break;
default:
// All remaining commands are executed by the primary processor only
HandleReply(gb, GCodeResult::ok, "");
return true;
}
}
#endif
// Can we queue this code?
if (gb.CanQueueCodes())
{
GCodeQueue * const codeQueue = GetMovementState(gb).codeQueue;
if (codeQueue->ShouldQueueMCode(gb))
{
// Don't queue any GCodes if there are segments not yet picked up by Move, because in the event that a segment corresponds to no movement,
// the move gets discarded, which throws out the count of scheduled moves and hence the synchronisation
if (GetMovementState(gb).segmentsLeft == 0 && codeQueue->QueueCode(gb))
{
HandleReply(gb, GCodeResult::ok, "");
return true;
}
return false; // we should queue this code but we can't yet, so wait until we can either execute it or queue it
}
}
#if HAS_SBC_INTERFACE
// Pass file- and system-related commands to the SBC service if they came from somewhere else.
// They will be passed back to us via a binary buffer or separate SPI message if necessary.
if ( reprap.UsingSbcInterface() && reprap.GetSbcInterface().IsConnected() && !gb.IsBinary()
&& ( (code >= 0 && code <= 2)
|| (code >= 20 && code <= 24) || (code >= 26 && code <= 30)
|| code == 32 || code == 36 || code == 37 || code == 38 || code == 39
|| (code == 98 && gb.Seen('R'))
|| code == 112
|| code == 121
|| (code >= 470 && code <= 472)
|| code == 503 || code == 505
|| code == 540 || (code >= 550 && code <= 552) || (code >= 586 && code <= 589)
|| code == 596 || code == 606
|| code == 703
|| code == 905 || code == 929 || code == 997 || code == 999
)
)
{
gb.SendToSbc();
return false;
}
#endif
OutputBuffer *outBuf = nullptr;
try
{
#ifdef DUET3_ATE
if (code >= 1000)
{
const GCodeResult rc = Duet3Ate::HandleAteMCode(code, gb, reply);
return HandleResult(gb, rc, reply, outBuf);
}
#endif
GCodeResult result;
if (gb.GetCommandFraction() > 0 && code != 36 && code != 201 && code != 260 && code != 261
#if SUPPORT_SCANNING_PROBES
&& code != 558
#endif
&& code != 569 && code != 586 &&
code != 587 // these are the only M-codes we implement that can have fractional parts
#if SUPPORT_PHASE_STEPPING
&& code != 970
#endif
)
{
result = TryMacroFile(gb);
}
else
{
result = GCodeResult::ok;
switch (code)
{
case 0: // Stop
case 1: // Sleep
case 2: // Stop
if (gb.IsFileChannel())
{
// Stopping a job because of a command in the file
#if SUPPORT_ASYNC_MOVES
if (!DoSync(gb))
{
return false;
}
#else
if (!LockCurrentMovementSystemAndWaitForStandstill(gb)) // wait until everything has stopped and deferred command queue has caught up
{
return false;
}
#endif
StopPrint(&gb, StopPrintReason::normalCompletion);
}
else if (pauseState == PauseState::paused)
{
// Cancelling a print that has been paused
if (!LockAllMovementSystemsAndWaitForStandstill(gb)) // make sure everything has stopped
{
return false;
}
const bool wasSimulating = IsSimulating(); // simulationMode may get cleared by CancelPrint
StopPrint(nullptr, StopPrintReason::userCancelled);
if (!wasSimulating) // don't run any macro files or turn heaters off etc. if we were simulating before we stopped the print
{
// If cancel.g exists then run it and do nothing else
gb.SetState(GCodeState::cancelling);
if (DoFileMacro(gb, CANCEL_G, false, SystemHelperMacroCode))
{
pauseState = PauseState::cancelling;
break;
}
if (!DoFileMacro(gb, STOP_G, false, SystemHelperMacroCode))
{
reprap.GetHeat().SwitchOffAll(true);
}
}
}
else
{
reply.copy("Pause the print before attempting to cancel it");
result = GCodeResult::error;
}
break;
case 3: // Spin spindle clockwise
case 4: // Spin spindle counter clockwise
{
MovementState& ms = GetMovementState(gb);
#if SUPPORT_LASER
if (machineType == MachineType::laser)
{
if (code == 3 && gb.Seen('S'))
{
if (ms.segmentsLeft != 0)
{
return false; // don't modify moves that haven't gone yet
}
ms.laserPixelData.pixelPwm[0] = ConvertLaserPwm(gb.GetNonNegativeFValue());
ms.laserPixelData.numPixels = 1;
}
else
{
result = GCodeResult::notSupportedInCurrentMode;
}
}
else
#endif
{
// Determine what spindle number we are using
uint32_t slot;
if (gb.Seen('P'))
{
slot = gb.GetLimitedUIValue('P', MaxSpindles);
}
else if (ms.currentTool != nullptr && ms.currentTool->GetSpindleNumber() >= 0)
{
slot = ms.currentTool->GetSpindleNumber();
}
else
{
reply.copy("No P parameter and no active tool with spindle");
result = GCodeResult::error;
break;
}
Spindle& spindle = platform.AccessSpindle(slot);
if (gb.Seen('S'))
{
const uint32_t rpm = gb.GetUIValue();
if (ms.currentTool != nullptr && ms.currentTool->GetSpindleNumber() == (int)slot)
{
ms.currentTool->SetSpindleRpm(rpm, true);
}
else
{
spindle.SetConfiguredRpm(rpm, false);
}
}
spindle.SetState((code == 4) ? SpindleState::reverse : SpindleState::forward);
}
}
break;
case 5: // Spindle motor off
{
MovementState& ms = GetMovementState(gb);
#if SUPPORT_LASER
if (machineType == MachineType::laser)
{
if (ms.segmentsLeft != 0)
{
return false; // don't modify moves that haven't gone yet
}
ms.laserPixelData.Clear();
}
else
#endif
{
// Determine what spindle number we are using
uint32_t slot;
if (gb.Seen('P'))
{
slot = gb.GetLimitedUIValue('P', MaxSpindles);
}
else if (ms.currentTool != nullptr && ms.currentTool->GetSpindleNumber() >= 0)
{
slot = ms.currentTool->GetSpindleNumber();
}
else
{
// Turn off every spindle if no 'P' parameter is present and the current tool does not have a spindle
for (size_t i = 0; i < MaxSpindles; i++)
{
platform.AccessSpindle(i).SetState(SpindleState::stopped);
}
break;
}
platform.AccessSpindle(slot).SetState(SpindleState::stopped);
}
}
break;
case 17: // Motors on
case 18: // Motors off
case 84:
if (!LockCurrentMovementSystemAndWaitForStandstill(gb))
{
return false;
}
{
bool seen = false;
Move& move = reprap.GetMove();
for (size_t axis = 0; axis < numTotalAxes; axis++)
{
if (gb.Seen(axisLetters[axis]))
{
if (code == 17)
{
move.EnableDrivers(axis, true);
}
else
{
SetAxisNotHomed(axis);
move.DisableDrivers(axis);
}
seen = true;
}
}
if (gb.Seen(extrudeLetter))
{
uint32_t eDrive[MaxExtruders];
size_t eCount = numExtruders;
gb.GetUnsignedArray(eDrive, eCount, false);
for (size_t i = 0; i < eCount; i++)
{
seen = true;
if (eDrive[i] >= numExtruders)
{
reply.printf("Invalid extruder number specified: %" PRIu32, eDrive[i]);
result = GCodeResult::error;
break;
}
if (code == 17)
{
move.EnableDrivers(ExtruderToLogicalDrive(eDrive[i]), true);
}
else
{
move.DisableDrivers(ExtruderToLogicalDrive(eDrive[i]));
}
}
}
if (gb.Seen('S'))
{
seen = true;
move.SetIdleTimeout(gb.GetPositiveFValue());
}
if (!seen)
{
if (code == 17)
{
for (size_t axis = 0; axis < numTotalAxes; ++axis)
{
move.EnableDrivers(axis, true);
}
for (size_t extruder = 0; extruder < numExtruders; ++extruder)
{
move.EnableDrivers(ExtruderToLogicalDrive(extruder), true);
}
}
else
{
DisableDrives();
}
}
}
break;
#if HAS_MASS_STORAGE || HAS_EMBEDDED_FILES
case 20: // List files on SD card
if (!LockFileSystem(gb)) // don't allow more than one at a time to avoid contention on output buffers
{
return false;
}
{
const int sparam = (gb.Seen('S')) ? gb.GetIValue() : 0;
const unsigned int rparam = (gb.Seen('R')) ? gb.GetUIValue() : 0;
String<MaxFilenameLength> dir;
if (gb.Seen('P'))
{
gb.GetPossiblyQuotedString(dir.GetRef());
}
else
{
dir.copy(Platform::GetGCodeDir());
}
if (sparam == 2)
{
outBuf = reprap.GetFilesResponse(dir.c_str(), rparam, true); // send the file list in JSON format
if (outBuf == nullptr)
{
reply.copy("{\"err\":-1}");
}
}
else if (sparam == 3)
{
outBuf = reprap.GetFilelistResponse(dir.c_str(), rparam);
if (outBuf == nullptr)
{
reply.copy("{\"err\":-1}");
}
}
else
{
if (!OutputBuffer::Allocate(outBuf))
{
return false; // cannot allocate an output buffer, try again later
}
// To mimic the behaviour of the official RepRapPro firmware:
// If we are emulating RepRap then we print "GCode files:\n" at the start, otherwise we don't.
// If we are emulating Marlin and the code came via the serial/USB interface, then we don't put quotes around the names and we separate them with newline;
// otherwise we put quotes around them and separate them with comma.