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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
|
// State machine function for GCode class
#include "GCodes.h"
#include "GCodeBuffer/GCodeBuffer.h"
#include <Platform/RepRap.h>
#include <Movement/Move.h>
#include <Tools/Tool.h>
#include <Heating/Heat.h>
#include <Endstops/ZProbe.h>
#if HAS_WIFI_NETWORKING || HAS_AUX_DEVICES
# include <Comms/FirmwareUpdater.h>
#endif
// Execute a step of the state machine
// CAUTION: don't allocate any long strings or other large objects directly within this function.
// The reason is that this function calls FinishedBedProbing(), which on a delta calls DoAutoCalibration(), which uses lots of stack.
// So any large local objects allocated here increase the amount of MAIN stack size needed.
void GCodes::RunStateMachine(GCodeBuffer& gb, const StringRef& reply) noexcept
{
#if HAS_LINUX_INTERFACE
// Wait for the G-code replies and abort requests to go before anything else is done in the state machine
if (reprap.UsingLinuxInterface() && (gb.IsAbortRequested() || gb.IsAbortAllRequested()))
{
return;
}
#endif
// Perform the next operation of the state machine for this gcode source
GCodeResult stateMachineResult = GCodeResult::ok;
const GCodeState state = gb.GetState();
switch (state)
{
case GCodeState::waitingForSpecialMoveToComplete:
case GCodeState::abortWhenMovementFinished:
if (LockMovementAndWaitForStandstill(gb)) // movement should already be locked, but we need to wait for standstill and fetch the current position
{
// Check whether we made any G1 S3 moves and need to set the axis limits
axesToSenseLength.Iterate([this](unsigned int axis, unsigned int)
{
const EndStopPosition stopType = platform.GetEndstops().GetEndStopPosition(axis);
if (stopType == EndStopPosition::highEndStop)
{
platform.SetAxisMaximum(axis, moveBuffer.coords[axis], true);
}
else if (stopType == EndStopPosition::lowEndStop)
{
platform.SetAxisMinimum(axis, moveBuffer.coords[axis], true);
}
}
);
if (gb.LatestMachineState().compatibility == Compatibility::NanoDLP && !DoingFileMacro())
{
reply.copy("Z_move_comp");
}
gb.SetState(GCodeState::normal);
if (state == GCodeState::abortWhenMovementFinished)
{
AbortPrint(gb);
}
}
break;
case GCodeState::waitingForSegmentedMoveToGo:
// Wait for all segments of the arc move to go into the movement queue and check whether an error occurred
switch (moveBuffer.segMoveState)
{
case SegmentedMoveState::inactive: // move completed without error
gb.SetState(GCodeState::normal);
break;
case SegmentedMoveState::aborted: // move terminated abnormally
if (!LockMovementAndWaitForStandstill(gb)) // update the the user position from the machine position at which we stop
{
break;
}
gb.LatestMachineState().SetError("G1/G2/G3: intermediate position outside machine limits");
gb.SetState(GCodeState::normal);
if (machineType != MachineType::fff)
{
AbortPrint(gb);
}
break;
case SegmentedMoveState::active: // move still ongoing
break;
}
break;
case GCodeState::probingToolOffset2: // Z probe has been deployed and recovery timer is running
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (millis() - lastProbedTime >= (uint32_t)(zp->GetRecoveryTime() * SecondsToMillis))
{
gb.AdvanceState();
}
}
break;
case GCodeState::probingToolOffset3: // executing M585 with an endstop, or with a probe after deploying the probe and waiting for the recovery time
if (SetupM585ProbingMove(gb))
{
gb.AdvanceState();
}
else
{
AbortPrint(gb);
gb.SetState(GCodeState::checkError);
if (m585Settings.useProbe)
{
reprap.GetHeat().SuspendHeaters(false);
RetractZProbe(gb);
}
}
break;
case GCodeState::probingToolOffset4: // executing M585, probing move has started
if (LockMovementAndWaitForStandstill(gb))
{
if (m585Settings.useProbe)
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
zp->SetProbing(false);
reprap.GetHeat().SuspendHeaters(false);
if (!zProbeTriggered)
{
gb.LatestMachineState().SetError("Probe was not triggered during probing move");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
break;
}
}
Tool * const currentTool = reprap.GetCurrentTool();
if (currentTool != nullptr)
{
// We get here when the tool probe has been activated. In this case we know how far we
// went (i.e. the difference between our start and end positions) and if we need to
// incorporate any correction factors. That's why we only need to set the final tool
// offset to this value in order to finish the tool probing.
const float coord = toolChangeRestorePoint.moveCoords[m585Settings.axisNumber] - currentUserPosition[m585Settings.axisNumber] + m585Settings.offset;
currentTool->SetOffset(m585Settings.axisNumber, coord, true);
}
gb.SetState(GCodeState::normal);
if (m585Settings.useProbe)
{
RetractZProbe(gb);
}
}
break;
case GCodeState::findCenterOfCavity1: // Executing M675 using a Z probe, have already deployed the probe
case GCodeState::probingToolOffset1: // Executing M585 using a probe, which we have deployed
if (LockMovementAndWaitForStandstill(gb))
{
lastProbedTime = millis(); // start the recovery timer
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (zp->GetProbeType() != ZProbeType::none && zp->GetTurnHeatersOff())
{
reprap.GetHeat().SuspendHeaters(true);
}
gb.AdvanceState();
}
break;
case GCodeState::findCenterOfCavity2: // Executing M675 using a Z probe, recovery timer is running
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (millis() - lastProbedTime >= (uint32_t)(zp->GetRecoveryTime() * SecondsToMillis))
{
if (SetupM675ProbingMove(gb, true))
{
gb.AdvanceState();
}
else
{
AbortPrint(gb);
gb.SetState(GCodeState::checkError);
reprap.GetHeat().SuspendHeaters(false);
RetractZProbe(gb);
}
}
}
break;
case GCodeState::findCenterOfCavity3: // Executing M675, min probing move has started
if (LockMovementAndWaitForStandstill(gb))
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
zp->SetProbing(false);
if (zProbeTriggered)
{
m675Settings.minDistance = currentUserPosition[m675Settings.axisNumber];
SetupM675BackoffMove(gb, m675Settings.minDistance + m675Settings.backoffDistance);
gb.AdvanceState();
}
else
{
gb.LatestMachineState().SetError("Probe was not triggered during probing move");
gb.SetState(GCodeState::checkError);
reprap.GetHeat().SuspendHeaters(false);
RetractZProbe(gb);
}
}
break;
case GCodeState::findCenterOfCavity4: // Executing M675, backoff move from min has started
if (LockMovementAndWaitForStandstill(gb))
{
if (SetupM675ProbingMove(gb, false))
{
gb.AdvanceState();
}
else
{
AbortPrint(gb);
gb.SetState(GCodeState::checkError);
reprap.GetHeat().SuspendHeaters(false);
RetractZProbe(gb);
}
}
break;
case GCodeState::findCenterOfCavity5: // Executing M675, max probing move has started
if (LockMovementAndWaitForStandstill(gb))
{
reprap.GetHeat().SuspendHeaters(false);
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
zp->SetProbing(false);
if (zProbeTriggered)
{
const float centre = (m675Settings.minDistance + currentUserPosition[m675Settings.axisNumber])/2;
SetupM675BackoffMove(gb, centre);
gb.AdvanceState();
}
else
{
gb.LatestMachineState().SetError("Probe was not triggered during probing move");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
}
}
break;
case GCodeState::findCenterOfCavity6: // Executing M675, move to centre has started
if (LockMovementAndWaitForStandstill(gb))
{
gb.SetState(GCodeState::normal);
RetractZProbe(gb);
}
break;
case GCodeState::homing1:
if (toBeHomed.IsEmpty())
{
gb.SetState(GCodeState::normal);
}
else
{
String<StringLength20> nextHomingFileName;
const AxesBitmap mustHomeFirst = reprap.GetMove().GetKinematics().GetHomingFileName(toBeHomed, axesHomed, numVisibleAxes, nextHomingFileName.GetRef());
if (mustHomeFirst.IsNonEmpty())
{
// Error, can't home this axes
reply.copy("Must home axes [");
AppendAxes(reply, mustHomeFirst);
reply.cat("] before homing [");
AppendAxes(reply, toBeHomed);
reply.cat(']');
stateMachineResult = GCodeResult::error;
toBeHomed.Clear();
gb.SetState(GCodeState::normal);
}
else
{
gb.SetState(GCodeState::homing2);
if (!DoFileMacro(gb, nextHomingFileName.c_str(), false, SystemHelperMacroCode))
{
reply.printf("Homing file %s not found", nextHomingFileName.c_str());
stateMachineResult = GCodeResult::error;
toBeHomed.Clear();
gb.SetState(GCodeState::normal);
}
}
}
break;
case GCodeState::homing2:
if (LockMovementAndWaitForStandstill(gb)) // movement should already be locked, but we need to wait for the previous homing move to complete
{
// Test whether the previous homing move homed any axes
if (toBeHomed.Disjoint(axesHomed))
{
reply.copy("Homing failed");
stateMachineResult = GCodeResult::error;
toBeHomed.Clear();
gb.SetState(GCodeState::normal);
}
else
{
toBeHomed &= ~axesHomed;
gb.SetState((toBeHomed.IsEmpty()) ? GCodeState::normal : GCodeState::homing1);
}
}
break;
case GCodeState::toolChange0: // run tfree for the old tool (if any)
case GCodeState::m109ToolChange0: // run tfree for the old tool (if any)
doingToolChange = true;
SavePosition(toolChangeRestorePoint, gb);
toolChangeRestorePoint.toolNumber = reprap.GetCurrentToolNumber();
toolChangeRestorePoint.fanSpeed = lastDefaultFanSpeed;
reprap.SetPreviousToolNumber();
gb.AdvanceState();
// If the tool is in the firmware-retracted state, there may be some Z hop applied, which we must remove
currentUserPosition[Z_AXIS] += currentZHop;
currentZHop = 0.0;
if ((toolChangeParam & TFreeBit) != 0)
{
const Tool * const oldTool = reprap.GetCurrentTool();
if (oldTool != nullptr) // 2020-04-29: run tfree file even if not all axes have been homed
{
String<StringLength20> scratchString;
scratchString.printf("tfree%d.g", oldTool->Number());
DoFileMacro(gb, scratchString.c_str(), false, ToolChangeMacroCode); // don't pass the T code here because it may be negative
}
}
break;
case GCodeState::toolChange1: // release the old tool (if any), then run tpre for the new tool
case GCodeState::m109ToolChange1: // release the old tool (if any), then run tpre for the new tool
if (LockMovementAndWaitForStandstill(gb)) // wait for tfree.g to finish executing
{
const Tool * const oldTool = reprap.GetCurrentTool();
if (oldTool != nullptr)
{
reprap.StandbyTool(oldTool->Number(), simulationMode != 0);
UpdateCurrentUserPosition(); // the tool offset may have changed, so get the current position
}
gb.AdvanceState();
if (reprap.GetTool(newToolNumber).IsNotNull() && (toolChangeParam & TPreBit) != 0) // 2020-04-29: run tpre file even if not all axes have been homed
{
String<StringLength20> scratchString;
scratchString.printf("tpre%d.g", newToolNumber);
DoFileMacro(gb, scratchString.c_str(), false, ToolChangeMacroCode);
}
}
break;
case GCodeState::toolChange2: // select the new tool if it exists and run tpost
case GCodeState::m109ToolChange2: // select the new tool if it exists and run tpost
if (LockMovementAndWaitForStandstill(gb)) // wait for tpre.g to finish executing
{
reprap.SelectTool(newToolNumber, simulationMode != 0);
UpdateCurrentUserPosition(); // get the actual position of the new tool
gb.AdvanceState();
if (machineType != MachineType::fff || toolChangeParam == 0)
{
gb.AdvanceState(); // skip moving tool to the new height if not a 3D printer or Tn P0 was given
}
if (reprap.GetCurrentTool() != nullptr && (toolChangeParam & TPostBit) != 0) // 2020-04-29: run tpost file even if not all axes have been homed
{
String<StringLength20> scratchString;
scratchString.printf("tpost%d.g", newToolNumber);
DoFileMacro(gb, scratchString.c_str(), false, ToolChangeMacroCode);
}
}
break;
case GCodeState::toolChange3: // move the new tool to the correct height
case GCodeState::m109ToolChange3: // move the new tool to the correct height
if (LockMovementAndWaitForStandstill(gb)) // wait for tpost.g to finish executing
{
// Check here if the tool offsets being applied would exceed Z axis limits
auto currentTool = reprap.GetCurrentTool();
if (currentTool != nullptr)
{
const float newZPos = (moveBuffer.coords[Z_AXIS] - currentTool->GetOffset(Z_AXIS));
if (newZPos > platform.AxisMaximum(Z_AXIS) || newZPos < platform.AxisMinimum(Z_AXIS))
{
gb.LatestMachineState().feedRate = toolChangeRestorePoint.feedRate;
doingToolChange = false;
gb.LatestMachineState().SetError("New tool too close to Z axis limit. Aborting tool change");
AbortPrint(gb);
gb.SetState(GCodeState::checkError);
break;
}
}
// Restore the original Z axis user position, so that different tool Z offsets work even if the first move after the tool change doesn't have a Z coordinate
SetMoveBufferDefaults();
currentUserPosition[Z_AXIS] = toolChangeRestorePoint.moveCoords[Z_AXIS];
ToolOffsetTransform(currentUserPosition, moveBuffer.coords);
moveBuffer.feedRate = ConvertSpeedFromMmPerMin(DefaultFeedRate); // ask for a good feed rate, we may have paused during a slow move
moveBuffer.tool = reprap.GetCurrentTool(); // needed so that bed compensation is applied correctly
NewMoveAvailable(1);
gb.AdvanceState();
}
break;
case GCodeState::toolChangeComplete:
case GCodeState::m109ToolChangeComplete:
if (LockMovementAndWaitForStandstill(gb)) // wait for the move to height to finish
{
gb.LatestMachineState().feedRate = toolChangeRestorePoint.feedRate;
// We don't restore the default fan speed in case the user wants to use a different one for the new tool
doingToolChange = false;
if (gb.GetState() == GCodeState::toolChangeComplete)
{
gb.SetState(GCodeState::normal);
}
else
{
UnlockAll(gb); // allow movement again
gb.AdvanceState(); // advance to m109WaitForTemperature
}
}
break;
case GCodeState::m109WaitForTemperature:
if (cancelWait || simulationMode != 0 || ToolHeatersAtSetTemperatures(reprap.GetCurrentTool(), gb.LatestMachineState().waitWhileCooling, TEMPERATURE_CLOSE_ENOUGH))
{
cancelWait = isWaiting = false;
gb.SetState(GCodeState::normal);
}
else
{
isWaiting = true;
}
break;
case GCodeState::pausing1:
if (LockMovementAndWaitForStandstill(gb))
{
gb.AdvanceState();
if (AllAxesAreHomed())
{
DoFileMacro(gb, PAUSE_G, true, SystemHelperMacroCode);
}
}
break;
case GCodeState::filamentChangePause1:
if (LockMovementAndWaitForStandstill(gb))
{
gb.AdvanceState();
if (AllAxesAreHomed())
{
if (!DoFileMacro(gb, FILAMENT_CHANGE_G, false, AsyncSystemMacroCode))
{
DoFileMacro(gb, PAUSE_G, true, AsyncSystemMacroCode);
}
}
}
break;
case GCodeState::filamentErrorPause1:
if (LockMovementAndWaitForStandstill(gb))
{
gb.AdvanceState();
if (AllAxesAreHomed())
{
String<StringLength20> macroName;
macroName.printf(FILAMENT_ERROR "%u.g", gb.LatestMachineState().stateParameter);
if (!DoFileMacro(gb, macroName.c_str(), false, AsyncSystemMacroCode))
{
if (!DoFileMacro(gb, FILAMENT_ERROR ".g", false, AsyncSystemMacroCode))
{
DoFileMacro(gb, PAUSE_G, true, AsyncSystemMacroCode);
}
}
}
}
break;
case GCodeState::pausing2:
case GCodeState::filamentChangePause2:
case GCodeState::filamentErrorPause2:
if (LockMovementAndWaitForStandstill(gb))
{
reply.printf((gb.GetState() == GCodeState::filamentChangePause2) ? "Printing paused for filament change at" : "Printing paused at");
for (size_t axis = 0; axis < numVisibleAxes; ++axis)
{
reply.catf(" %c%.1f", axisLetters[axis], (double)pauseRestorePoint.moveCoords[axis]);
}
platform.MessageF(LogWarn, "%s\n", reply.c_str());
pauseState = PauseState::paused;
gb.SetState(GCodeState::normal);
}
break;
case GCodeState::resuming1:
case GCodeState::resuming2:
// Here when we have just finished running the resume macro file.
// Move the head back to the paused location
if (LockMovementAndWaitForStandstill(gb))
{
const float currentZ = moveBuffer.coords[Z_AXIS];
for (size_t axis = 0; axis < numVisibleAxes; ++axis)
{
currentUserPosition[axis] = pauseRestorePoint.moveCoords[axis];
}
SetMoveBufferDefaults();
ToolOffsetTransform(currentUserPosition, moveBuffer.coords);
moveBuffer.feedRate = ConvertSpeedFromMmPerMin(DefaultFeedRate); // ask for a good feed rate, we may have paused during a slow move
moveBuffer.tool = reprap.GetCurrentTool(); // needed so that bed compensation is applied correctly
if (gb.GetState() == GCodeState::resuming1 && currentZ > pauseRestorePoint.moveCoords[Z_AXIS])
{
// First move the head to the correct XY point, then move it down in a separate move
moveBuffer.coords[Z_AXIS] = currentZ;
gb.SetState(GCodeState::resuming2);
}
else
{
// Just move to the saved position in one go
gb.SetState(GCodeState::resuming3);
}
NewMoveAvailable(1);
}
break;
case GCodeState::resuming3:
if (LockMovementAndWaitForStandstill(gb))
{
// We no longer restore the paused fan speeds automatically on resuming, because that messes up the print cooling fan speed if a tool change has been done
// They can be restored manually in resume.g if required
virtualExtruderPosition = pauseRestorePoint.virtualExtruderPosition; // reset the extruder position in case we are receiving absolute extruder moves
moveBuffer.virtualExtruderPosition = pauseRestorePoint.virtualExtruderPosition;
fileGCode->LatestMachineState().feedRate = pauseRestorePoint.feedRate;
moveFractionToSkip = pauseRestorePoint.proportionDone;
restartInitialUserC0 = pauseRestorePoint.initialUserC0;
restartInitialUserC1 = pauseRestorePoint.initialUserC1;
reply.copy("Printing resumed");
platform.Message(LogWarn, "Printing resumed\n");
pauseState = PauseState::notPaused;
gb.SetState(GCodeState::normal);
}
break;
case GCodeState::flashing1:
#if HAS_WIFI_NETWORKING || HAS_AUX_DEVICES
// Update additional modules before the main firmware
if (FirmwareUpdater::IsReady())
{
bool updating = false;
String<MaxFilenameLength> filenameString;
try
{
bool dummy;
gb.TryGetQuotedString('P', filenameString.GetRef(), dummy);
}
catch (const GCodeException&) { }
for (unsigned int module = 1; module < NumFirmwareUpdateModules; ++module)
{
if (firmwareUpdateModuleMap.IsBitSet(module))
{
firmwareUpdateModuleMap.ClearBit(module);
FirmwareUpdater::UpdateModule(module, serialChannelForPanelDueFlashing, filenameString.GetRef());
updating = true;
isFlashingPanelDue = (module == FirmwareUpdater::PanelDueFirmwareModule);
break;
}
}
if (!updating)
{
isFlashingPanelDue = false;
gb.SetState(GCodeState::flashing2);
}
}
# if HAS_AUX_DEVICES
else
{
PanelDueUpdater* panelDueUpdater = platform.GetPanelDueUpdater();
if (panelDueUpdater != nullptr)
{
panelDueUpdater->Spin();
}
}
# endif
#else
gb.SetState(GCodeState::flashing2);
#endif
break;
case GCodeState::flashing2:
if (firmwareUpdateModuleMap.IsBitSet(0))
{
// Update main firmware
firmwareUpdateModuleMap.Clear();
String<MaxFilenameLength> filenameString;
try
{
bool dummy;
gb.TryGetQuotedString('P', filenameString.GetRef(), dummy);
reprap.UpdateFirmware(filenameString.GetRef());
// The above call does not return unless an error occurred
}
catch (const GCodeException&) { }
}
isFlashing = false;
gb.SetState(GCodeState::normal);
break;
case GCodeState::stoppingWithHeatersOff: // MO or M1 after executing stop.g/sleep.g if present
reprap.GetHeat().SwitchOffAll(true);
// no break
case GCodeState::stoppingWithHeatersOn: // M0 H1 or M1 H1 after executing stop.g/sleep.g if present
if (LockMovementAndWaitForStandstill(gb))
{
pauseState = PauseState::notPaused;
platform.SetDriversIdle();
gb.SetState(GCodeState::normal);
}
break;
// States used for grid probing
case GCodeState::gridProbing1: // ready to move to next grid probe point
{
// Move to the current probe point
Move& move = reprap.GetMove();
const GridDefinition& grid = move.AccessHeightMap().GetGrid();
const float axis0Coord = grid.GetCoordinate(0, gridAxis0index);
const float axis1Coord = grid.GetCoordinate(1, gridAxis1index);
if (grid.IsInRadius(axis0Coord, axis1Coord))
{
const size_t axis0Num = grid.GetAxisNumber(0);
const size_t axis1Num = grid.GetAxisNumber(1);
AxesBitmap axes;
axes.SetBit(axis0Num);
axes.SetBit(axis1Num);
float axesCoords[MaxAxes];
axesCoords[axis0Num] = axis0Coord;
axesCoords[axis1Num] = axis1Coord;
if (move.IsAccessibleProbePoint(axesCoords, axes))
{
SetMoveBufferDefaults();
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
moveBuffer.coords[axis0Num] = axis0Coord - zp->GetOffset(axis0Num);
moveBuffer.coords[axis1Num] = axis1Coord - zp->GetOffset(axis1Num);
moveBuffer.coords[Z_AXIS] = zp->GetStartingHeight();
moveBuffer.feedRate = zp->GetTravelSpeed();
NewMoveAvailable(1);
InitialiseTaps(false);
gb.AdvanceState();
}
else
{
platform.MessageF(WarningMessage, "Skipping grid point %c=%.1f, %c=%.1f because Z probe cannot reach it\n", grid.GetAxisLetter(0), (double)axis0Coord, grid.GetAxisLetter(1), (double)axis1Coord);
gb.SetState(GCodeState::gridProbing6);
}
}
else
{
gb.SetState(GCodeState::gridProbing6);
}
}
break;
case GCodeState::gridProbing2a: // ready to probe the current grid probe point (we return to this state when doing the second and subsequent taps)
if (LockMovementAndWaitForStandstill(gb))
{
gb.AdvanceState();
if (platform.GetZProbeOrDefault(currentZProbeNumber)->GetProbeType() == ZProbeType::blTouch)
{
DeployZProbe(gb);
}
}
break;
case GCodeState::gridProbing2b: // ready to probe the current grid probe point
if (LockMovementAndWaitForStandstill(gb))
{
lastProbedTime = millis(); // start the recovery timer
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (zp->GetProbeType() != ZProbeType::none && zp->GetTurnHeatersOff())
{
reprap.GetHeat().SuspendHeaters(true);
}
gb.AdvanceState();
}
break;
case GCodeState::gridProbing3: // ready to probe the current grid probe point
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (millis() - lastProbedTime >= (uint32_t)(zp->GetRecoveryTime() * SecondsToMillis))
{
// Probe the bed at the current XY coordinates
// Check for probe already triggered at start
if (zp->GetProbeType() == ZProbeType::none)
{
// No Z probe, so do manual mesh levelling instead
UnlockAll(gb); // release the movement lock to allow manual Z moves
gb.AdvanceState(); // resume at next state when user has finished adjusting the height
doingManualBedProbe = true; // suspend the Z movement limit
DoManualBedProbe(gb);
}
else if (zp->Stopped())
{
reprap.GetMove().heightMapLock.ReleaseWriter();
reprap.GetHeat().SuspendHeaters(false);
gb.LatestMachineState().SetError("Probe already triggered before probing move started");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
break;
}
else
{
zProbeTriggered = false;
SetMoveBufferDefaults();
if (!platform.GetEndstops().EnableZProbe(currentZProbeNumber) || !zp->SetProbing(true))
{
reprap.GetMove().heightMapLock.ReleaseWriter();
gb.LatestMachineState().SetError("Failed to enable probe");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
break;
}
moveBuffer.checkEndstops = true;
moveBuffer.reduceAcceleration = true;
moveBuffer.coords[Z_AXIS] = -zp->GetDiveHeight() + zp->GetActualTriggerHeight();
moveBuffer.feedRate = zp->GetProbingSpeed(tapsDone);
NewMoveAvailable(1);
gb.AdvanceState();
}
}
}
break;
case GCodeState::gridProbing4: // ready to lift the probe after probing the current grid probe point
if (LockMovementAndWaitForStandstill(gb))
{
doingManualBedProbe = false;
++tapsDone;
reprap.GetHeat().SuspendHeaters(false);
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (zp->GetProbeType() == ZProbeType::none)
{
// No Z probe, so we are doing manual mesh levelling. Take the current Z height as the height error.
g30zHeightError = moveBuffer.coords[Z_AXIS];
}
else
{
zp->SetProbing(false);
if (!zProbeTriggered)
{
reprap.GetMove().heightMapLock.ReleaseWriter();
gb.LatestMachineState().SetError("Probe was not triggered during probing move");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
break;
}
// Grid probing never does an additional fast tap, so we can always include this tap in the average
g30zHeightError = moveBuffer.coords[Z_AXIS] - zp->GetActualTriggerHeight();
g30zHeightErrorSum += g30zHeightError;
}
gb.AdvanceState();
if (zp->GetProbeType() == ZProbeType::blTouch) // bltouch needs to be retracted when it triggers
{
RetractZProbe(gb);
}
}
break;
case GCodeState::gridProbing4a: // ready to lift the probe after probing the current grid probe point
// Move back up to the dive height
SetMoveBufferDefaults();
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
moveBuffer.coords[Z_AXIS] = zp->GetStartingHeight();
moveBuffer.feedRate = zp->GetTravelSpeed();
}
NewMoveAvailable(1);
gb.AdvanceState();
break;
case GCodeState::gridProbing5: // finished probing a point and moved back to the dive height
if (LockMovementAndWaitForStandstill(gb))
{
// See whether we need to do any more taps
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
bool acceptReading = false;
if (zp->GetMaxTaps() < 2)
{
acceptReading = true;
}
else if (tapsDone >= 2)
{
g30zHeightErrorLowestDiff = min<float>(g30zHeightErrorLowestDiff, fabsf(g30zHeightError - g30PrevHeightError));
if (zp->GetTolerance() > 0.0)
{
if (g30zHeightErrorLowestDiff <= zp->GetTolerance())
{
g30zHeightError = (g30zHeightError + g30PrevHeightError)/2;
acceptReading = true;
}
}
else if (tapsDone == (int)zp->GetMaxTaps())
{
g30zHeightError = g30zHeightErrorSum/tapsDone;
acceptReading = true;
}
}
if (acceptReading)
{
reprap.GetMove().AccessHeightMap().SetGridHeight(gridAxis0index, gridAxis1index, g30zHeightError);
gb.AdvanceState();
}
else if (tapsDone < (int)zp->GetMaxTaps())
{
// Tap again
lastProbedTime = millis();
g30PrevHeightError = g30zHeightError;
gb.SetState(GCodeState::gridProbing2a);
}
else
{
reprap.GetMove().heightMapLock.ReleaseWriter();
gb.LatestMachineState().SetError("Z probe readings not consistent");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
}
}
break;
case GCodeState::gridProbing6: // ready to compute the next probe point
{
const HeightMap& hm = reprap.GetMove().AccessHeightMap();
if (gridAxis1index & 1)
{
// Odd row, so decreasing X
if (gridAxis0index == 0)
{
++gridAxis1index;
}
else
{
--gridAxis0index;
}
}
else
{
// Even row, so increasing X
if (gridAxis0index + 1 == hm.GetGrid().NumAxisPoints(0))
{
++gridAxis1index;
}
else
{
++gridAxis0index;
}
}
if (gridAxis1index == hm.GetGrid().NumAxisPoints(1))
{
// Done all the points
gb.AdvanceState();
RetractZProbe(gb);
}
else
{
gb.SetState(GCodeState::gridProbing1);
}
}
break;
case GCodeState::gridProbing7:
// Finished probing the grid, and retracted the probe if necessary
{
float minError, maxError;
Deviation deviation;
const uint32_t numPointsProbed = reprap.GetMove().AccessHeightMap().GetStatistics(deviation, minError, maxError);
if (numPointsProbed >= 4)
{
reprap.GetMove().SetLatestMeshDeviation(deviation);
reply.printf("%" PRIu32 " points probed, min error %.3f, max error %.3f, mean %.3f, deviation %.3f\n",
numPointsProbed, (double)minError, (double)maxError, (double)deviation.GetMean(), (double)deviation.GetDeviationFromMean());
#if HAS_MASS_STORAGE
# if HAS_LINUX_INTERFACE
if (!reprap.UsingLinuxInterface())
# endif
{
if (TrySaveHeightMap(DefaultHeightMapFile, reply))
{
stateMachineResult = GCodeResult::error;
}
}
#endif
reprap.GetMove().AccessHeightMap().ExtrapolateMissing();
reprap.GetMove().UseMesh(true);
const float absMean = fabsf(deviation.GetMean());
if (absMean >= 0.05 && absMean >= 2 * deviation.GetDeviationFromMean())
{
platform.Message(WarningMessage, "the height map has a substantial Z offset. Suggest use Z-probe to establish Z=0 datum, then re-probe the mesh.\n");
}
}
else
{
gb.LatestMachineState().SetError("Too few points probed");
}
reprap.GetMove().heightMapLock.ReleaseWriter();
}
if (stateMachineResult == GCodeResult::ok)
{
reprap.GetPlatform().MessageF(LogWarn, "%s\n", reply.c_str());
}
gb.SetState(GCodeState::normal);
break;
// States used for G30 probing
case GCodeState::probingAtPoint0:
// Initial state when executing G30 with a P parameter. Start by moving to the dive height at the current position.
SetMoveBufferDefaults();
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
moveBuffer.coords[Z_AXIS] = zp->GetStartingHeight();
moveBuffer.feedRate = zp->GetTravelSpeed();
}
NewMoveAvailable(1);
gb.AdvanceState();
break;
case GCodeState::probingAtPoint1:
// The move to raise/lower the head to the correct dive height has been commanded.
if (LockMovementAndWaitForStandstill(gb))
{
// Head is at the dive height but needs to be moved to the correct XY position. The XY coordinates have already been stored.
SetMoveBufferDefaults();
(void)reprap.GetMove().GetProbeCoordinates(g30ProbePointIndex, moveBuffer.coords[X_AXIS], moveBuffer.coords[Y_AXIS], true);
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
moveBuffer.coords[Z_AXIS] = zp->GetStartingHeight();
moveBuffer.feedRate = zp->GetTravelSpeed();
NewMoveAvailable(1);
InitialiseTaps(false);
gb.AdvanceState();
}
break;
case GCodeState::probingAtPoint2a: // note we return to this state when doing the second and subsequent taps
// Executing G30 with a P parameter. The move to put the head at the specified XY coordinates has been commanded.
// OR initial state when executing G30 with no P parameter (must call InitialiseTaps first)
if (LockMovementAndWaitForStandstill(gb))
{
gb.AdvanceState();
if (platform.GetZProbeOrDefault(currentZProbeNumber)->GetProbeType() == ZProbeType::blTouch) // bltouch needs to be redeployed prior to each probe point
{
DeployZProbe(gb);
}
}
break;
case GCodeState::probingAtPoint2b:
if (LockMovementAndWaitForStandstill(gb))
{
// Head has finished moving to the correct XY position and BLTouch has been deployed
lastProbedTime = millis(); // start the probe recovery timer
if (platform.GetZProbeOrDefault(currentZProbeNumber)->GetTurnHeatersOff())
{
reprap.GetHeat().SuspendHeaters(true);
}
gb.AdvanceState();
}
break;
case GCodeState::probingAtPoint3:
// Executing G30 with a P parameter. The move to put the head at the specified XY coordinates has been completed and the recovery timer started.
// OR executing G30 without a P parameter, and the recovery timer has been started.
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (millis() - lastProbedTime >= (uint32_t)(zp->GetRecoveryTime() * SecondsToMillis))
{
// The probe recovery time has elapsed, so we can start the probing move
if (zp->GetProbeType() == ZProbeType::none)
{
// No Z probe, so we are doing manual 'probing'
UnlockAll(gb); // release the movement lock to allow manual Z moves
gb.AdvanceState(); // resume at the next state when the user has finished
doingManualBedProbe = true; // suspend the Z movement limit
DoManualBedProbe(gb);
}
else if (zp->Stopped()) // check for probe already triggered at start
{
// Z probe is already triggered at the start of the move, so abandon the probe and record an error
reprap.GetHeat().SuspendHeaters(false);
gb.LatestMachineState().SetError("Probe already triggered at start of probing move");
if (g30ProbePointIndex >= 0)
{
reprap.GetMove().SetZBedProbePoint(g30ProbePointIndex, zp->GetDiveHeight(), true, true);
}
gb.SetState(GCodeState::checkError); // no point in doing anything else
RetractZProbe(gb);
}
else
{
zProbeTriggered = false;
SetMoveBufferDefaults();
if (!platform.GetEndstops().EnableZProbe(currentZProbeNumber) || !zp->SetProbing(true))
{
gb.LatestMachineState().SetError("Failed to enable probe");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
break;
}
moveBuffer.checkEndstops = true;
moveBuffer.reduceAcceleration = true;
moveBuffer.coords[Z_AXIS] = (IsAxisHomed(Z_AXIS))
? platform.AxisMinimum(Z_AXIS) - zp->GetDiveHeight() + zp->GetActualTriggerHeight() // Z axis has been homed, so no point in going very far
: -1.1 * platform.AxisTotalLength(Z_AXIS); // Z axis not homed yet, so treat this as a homing move
moveBuffer.feedRate = zp->GetProbingSpeed(tapsDone);
NewMoveAvailable(1);
gb.AdvanceState();
}
}
}
break;
case GCodeState::probingAtPoint4:
// Executing G30. The probe wasn't triggered at the start of the move, and the probing move has been commanded.
if (LockMovementAndWaitForStandstill(gb))
{
// Probing move has stopped
reprap.GetHeat().SuspendHeaters(false);
doingManualBedProbe = false;
hadProbingError = false;
++tapsDone;
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
if (zp->GetProbeType() == ZProbeType::none)
{
// No Z probe, so we are doing manual mesh levelling. Take the current Z height as the height error.
g30zHeightError = moveBuffer.coords[Z_AXIS];
zp->SetLastStoppedHeight(g30zHeightError);
}
else
{
zp->SetProbing(false);
if (!zProbeTriggered)
{
gb.LatestMachineState().SetError("Probe was not triggered during probing move");
g30zHeightErrorSum = g30zHeightError = 0.0;
hadProbingError = true;
}
else
{
// Successful probing
float m[MaxAxes];
reprap.GetMove().GetCurrentMachinePosition(m, false); // get height without bed compensation
const float g30zStoppedHeight = m[Z_AXIS] - g30HValue; // save for later
zp->SetLastStoppedHeight(g30zStoppedHeight);
if (tapsDone > 0) // don't accumulate the result of we are doing fast-then-slow probing and this was the fast probe
{
g30zHeightError = g30zStoppedHeight - zp->GetActualTriggerHeight();
g30zHeightErrorSum += g30zHeightError;
}
}
}
if (g30ProbePointIndex < 0) // if no P parameter
{
// Simple G30 probing move
if (g30SValue == -1 || g30SValue == -2 || g30SValue == -3)
{
// G30 S-1 command taps once and reports the height, S-2 sets the tool offset to the negative of the current height, S-3 sets the Z probe trigger height
gb.SetState(GCodeState::probingAtPoint7); // special state for reporting the stopped height at the end
RetractZProbe(gb); // retract the probe before moving to the new state
break;
}
if (tapsDone <= 1 && !hadProbingError)
{
// Reset the Z axis origin according to the height error so that we can move back up to the dive height
moveBuffer.coords[Z_AXIS] = zp->GetActualTriggerHeight();
reprap.GetMove().SetNewPosition(moveBuffer.coords, false);
// Find the coordinates of the Z probe to pass to SetZeroHeightError
float tempCoords[MaxAxes];
memcpyf(tempCoords, moveBuffer.coords, ARRAY_SIZE(tempCoords));
tempCoords[X_AXIS] += zp->GetOffset(X_AXIS);
tempCoords[Y_AXIS] += zp->GetOffset(Y_AXIS);
reprap.GetMove().SetZeroHeightError(tempCoords);
ToolOffsetInverseTransform(moveBuffer.coords, currentUserPosition);
g30zHeightErrorSum = g30zHeightError = 0; // there is no longer any height error from this probe
SetAxisIsHomed(Z_AXIS); // this is only correct if the Z axis is Cartesian-like, but other architectures must be homed before probing anyway
zDatumSetByProbing = true;
}
}
gb.AdvanceState();
if (zp->GetProbeType() == ZProbeType::blTouch) // bltouch needs to be retracted when it triggers
{
RetractZProbe(gb);
}
}
break;
case GCodeState::probingAtPoint4a:
// Move back up to the dive height before we change anything, in particular before we adjust leadscrews
SetMoveBufferDefaults();
{
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
moveBuffer.coords[Z_AXIS] = zp->GetStartingHeight();
moveBuffer.feedRate = zp->GetTravelSpeed();
}
NewMoveAvailable(1);
gb.AdvanceState();
break;
case GCodeState::probingAtPoint5:
// Here when we have moved the head back up to the dive height
if (LockMovementAndWaitForStandstill(gb))
{
// See whether we need to do any more taps
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
bool acceptReading = false;
if (zp->GetMaxTaps() < 2 && tapsDone == 1)
{
acceptReading = true;
}
else if (tapsDone >= 2)
{
g30zHeightErrorLowestDiff = min<float>(g30zHeightErrorLowestDiff, fabsf(g30zHeightError - g30PrevHeightError));
if (zp->GetTolerance() > 0.0 && g30zHeightErrorLowestDiff <= zp->GetTolerance())
{
g30zHeightError = (g30zHeightError + g30PrevHeightError)/2;
acceptReading = true;
}
}
if (!acceptReading)
{
if (tapsDone < (int)zp->GetMaxTaps())
{
// Tap again
g30PrevHeightError = g30zHeightError;
lastProbedTime = millis();
gb.SetState(GCodeState::probingAtPoint2a);
break;
}
// We no longer flag this as a probing error, instead we take the average and issue a warning
g30zHeightError = g30zHeightErrorSum/tapsDone;
if (zp->GetTolerance() > 0.0) // zero or negative tolerance means always average all readings, so no warning message
{
gb.LatestMachineState().SetError("Z probe readings not consistent");
}
}
if (g30ProbePointIndex >= 0)
{
reprap.GetMove().SetZBedProbePoint(g30ProbePointIndex, g30zHeightError, true, hadProbingError);
}
else
{
// Setting the Z height with G30
moveBuffer.coords[Z_AXIS] -= g30zHeightError;
reprap.GetMove().SetNewPosition(moveBuffer.coords, false);
// Find the coordinates of the Z probe to pass to SetZeroHeightError
float tempCoords[MaxAxes];
memcpyf(tempCoords, moveBuffer.coords, ARRAY_SIZE(tempCoords));
tempCoords[X_AXIS] += zp->GetOffset(X_AXIS);
tempCoords[Y_AXIS] += zp->GetOffset(Y_AXIS);
reprap.GetMove().SetZeroHeightError(tempCoords);
ToolOffsetInverseTransform(moveBuffer.coords, currentUserPosition);
}
gb.AdvanceState();
if (zp->GetProbeType() != ZProbeType::blTouch) // if it's a BLTouch then we have already retracted it
{
RetractZProbe(gb);
}
}
break;
case GCodeState::probingAtPoint6:
// Here when we have finished probing and have retracted the probe if necessary
if (LockMovementAndWaitForStandstill(gb)) // retracting the Z probe
{
if (g30SValue == 1)
{
// G30 with a silly Z value and S=1 is equivalent to G30 with no parameters in that it sets the current Z height
// This is useful because it adjusts the XY position to account for the probe offset.
moveBuffer.coords[Z_AXIS] -= g30zHeightError;
reprap.GetMove().SetNewPosition(moveBuffer.coords, false);
ToolOffsetInverseTransform(moveBuffer.coords, currentUserPosition);
}
else if (g30SValue >= -1)
{
if (reprap.GetMove().FinishedBedProbing(g30SValue, reply))
{
stateMachineResult = GCodeResult::error;
}
else if (reprap.GetMove().GetKinematics().SupportsAutoCalibration())
{
zDatumSetByProbing = true; // if we successfully auto calibrated or adjusted leadscrews, we've set the Z datum by probing
}
}
gb.SetState(GCodeState::normal);
}
break;
case GCodeState::probingAtPoint7:
// Here when we have finished executing G30 S-1 or S-2 or S-3 including retracting the probe if necessary
if (g30SValue == -3)
{
// Adjust the Z probe trigger height to the stop height
const auto zp = platform.GetZProbeOrDefault(currentZProbeNumber);
zp->SetTriggerHeight(zp->GetLastStoppedHeight());
reply.printf("Z probe trigger height set to %.3f mm", (double)zp->GetLastStoppedHeight());
}
else if (g30SValue == -2)
{
// Adjust the Z offset of the current tool to account for the height error
Tool * const tool = reprap.GetCurrentTool();
if (tool == nullptr)
{
gb.LatestMachineState().SetError("Tool was deselected during G30 S-2 command");
}
else
{
tool->SetOffset(Z_AXIS, -g30zHeightError, true);
ToolOffsetInverseTransform(moveBuffer.coords, currentUserPosition); // update user coordinates to reflect the new tool offset
}
}
else
{
// Just print the stop height
reply.printf("Stopped at height %.3f mm", (double)platform.GetZProbeOrDefault(currentZProbeNumber)->GetLastStoppedHeight());
}
gb.SetState(GCodeState::normal);
break;
case GCodeState::straightProbe0: // ready to deploy the probe
if (LockMovementAndWaitForStandstill(gb))
{
gb.AdvanceState();
currentZProbeNumber = straightProbeSettings.GetZProbeToUse();
DeployZProbe(gb);
}
break;
case GCodeState::straightProbe1:
if (LockMovementAndWaitForStandstill(gb))
{
const auto zp = platform.GetEndstops().GetZProbe(straightProbeSettings.GetZProbeToUse());
lastProbedTime = millis(); // start the probe recovery timer
if (zp.IsNotNull() && zp->GetTurnHeatersOff())
{
reprap.GetHeat().SuspendHeaters(true);
}
gb.AdvanceState();
}
break;
case GCodeState::straightProbe2:
// Executing G38. The probe has been deployed and the recovery timer has been started.
{
if (millis() - lastProbedTime >= (uint32_t)(platform.GetZProbeOrDefault(straightProbeSettings.GetZProbeToUse())->GetRecoveryTime() * SecondsToMillis))
{
// The probe recovery time has elapsed, so we can start the probing move
const auto zp = platform.GetEndstops().GetZProbe(straightProbeSettings.GetZProbeToUse());
if (zp.IsNull() || zp->GetProbeType() == ZProbeType::none)
{
// No Z probe, so we are doing manual 'probing'
UnlockAll(gb); // release the movement lock to allow manual Z moves
gb.AdvanceState(); // resume at the next state when the user has finished
DoStraightManualProbe(gb, straightProbeSettings); // call out to separate function because it used a lot of stack
}
else
{
const bool probingAway = straightProbeSettings.ProbingAway();
const bool atStop = zp->Stopped();
if (probingAway != atStop)
{
// Z probe is already in target state at the start of the move, so abandon the probe and signal an error if the type demands so
reprap.GetHeat().SuspendHeaters(false);
if (straightProbeSettings.SignalError())
{
gb.LatestMachineState().SetError((probingAway) ? "Probe not triggered at start of probing move" : "Probe already triggered at start of probing move");
}
gb.SetState(GCodeState::checkError); // no point in doing anything else
RetractZProbe(gb);
}
else
{
zProbeTriggered = false;
SetMoveBufferDefaults();
if (!platform.GetEndstops().EnableZProbe(straightProbeSettings.GetZProbeToUse(), probingAway) || !zp->SetProbing(true))
{
gb.LatestMachineState().SetError("Failed to enable probe");
gb.SetState(GCodeState::checkError);
RetractZProbe(gb);
break;
}
moveBuffer.checkEndstops = true;
moveBuffer.reduceAcceleration = true;
straightProbeSettings.SetCoordsToTarget(moveBuffer.coords);
moveBuffer.feedRate = zp->GetProbingSpeed(0);
NewMoveAvailable(1);
gb.AdvanceState();
}
}
}
}
break;
case GCodeState::straightProbe3:
// Executing G38. The probe wasn't in target state at the start of the move, and the probing move has been commanded.
if (LockMovementAndWaitForStandstill(gb))
{
// Probing move has stopped
reprap.GetHeat().SuspendHeaters(false);
const bool probingAway = straightProbeSettings.ProbingAway();
const auto zp = platform.GetEndstops().GetZProbe(straightProbeSettings.GetZProbeToUse());
if (zp.IsNotNull() && zp->GetProbeType() != ZProbeType::none)
{
zp->SetProbing(false);
if (!zProbeTriggered && straightProbeSettings.SignalError())
{
gb.LatestMachineState().SetError((probingAway) ? "Probe did not lose contact during probing move" : "Probe was not triggered during probing move");
}
}
gb.SetState(GCodeState::checkError);
RetractZProbe(gb); // retract the probe before moving to the new state
}
break;
// Firmware retraction/un-retraction states
case GCodeState::doingFirmwareRetraction:
// We just did the retraction part of a firmware retraction, now we need to do the Z hop
if (moveBuffer.segmentsLeft == 0)
{
const Tool * const tool = reprap.GetCurrentTool();
if (tool != nullptr)
{
SetMoveBufferDefaults();
moveBuffer.tool = tool;
reprap.GetMove().GetCurrentUserPosition(moveBuffer.coords, 0, moveBuffer.tool);
moveBuffer.coords[Z_AXIS] += tool->GetRetractHop();
moveBuffer.feedRate = platform.MaxFeedrate(Z_AXIS);
moveBuffer.filePos = (&gb == fileGCode) ? gb.GetFilePosition() : noFilePosition;
moveBuffer.canPauseAfter = false; // don't pause after a retraction because that could cause too much retraction
currentZHop = tool->GetRetractHop();
NewMoveAvailable(1);
}
gb.SetState(GCodeState::normal);
}
break;
case GCodeState::doingFirmwareUnRetraction:
// We just undid the Z-hop part of a firmware un-retraction, now we need to do the un-retract
if (moveBuffer.segmentsLeft == 0)
{
const Tool * const tool = reprap.GetCurrentTool();
if (tool != nullptr && tool->DriveCount() != 0)
{
SetMoveBufferDefaults();
moveBuffer.tool = tool;
reprap.GetMove().GetCurrentUserPosition(moveBuffer.coords, 0, tool);
for (size_t i = 0; i < tool->DriveCount(); ++i)
{
moveBuffer.coords[ExtruderToLogicalDrive(tool->Drive(i))] = tool->GetRetractLength() + tool->GetRetractExtra();
}
moveBuffer.feedRate = tool->GetUnRetractSpeed() * tool->DriveCount();
moveBuffer.filePos = (&gb == fileGCode) ? gb.GetFilePosition() : noFilePosition;
moveBuffer.canPauseAfter = true;
NewMoveAvailable(1);
}
gb.SetState(GCodeState::normal);
}
break;
case GCodeState::loadingFilament:
// We just returned from the filament load macro
if (reprap.GetCurrentTool() != nullptr)
{
reprap.GetCurrentTool()->GetFilament()->Load(filamentToLoad);
if (reprap.Debug(moduleGcodes))
{
platform.MessageF(LoggedGenericMessage, "Filament %s loaded", filamentToLoad);
}
}
gb.SetState(GCodeState::normal);
break;
case GCodeState::unloadingFilament:
// We just returned from the filament unload macro
if (reprap.GetCurrentTool() != nullptr)
{
if (reprap.Debug(moduleGcodes))
{
platform.MessageF(LoggedGenericMessage, "Filament %s unloaded", reprap.GetCurrentTool()->GetFilament()->GetName());
}
reprap.GetCurrentTool()->GetFilament()->Unload();
}
gb.SetState(GCodeState::normal);
break;
#if HAS_VOLTAGE_MONITOR
case GCodeState::powerFailPausing1:
if (gb.IsReady() || gb.IsExecuting())
{
gb.SetFinished(ActOnCode(gb, reply)); // execute the pause script
}
else
{
# if HAS_MASS_STORAGE || HAS_LINUX_INTERFACE
SaveResumeInfo(true); // create the resume file so that we can resume after power down
# endif
platform.Message(LoggedGenericMessage, "Print auto-paused due to low voltage\n");
gb.SetState(GCodeState::normal);
}
break;
#endif
#if HAS_MASS_STORAGE
case GCodeState::timingSDwrite:
for (uint32_t writtenThisTime = 0; writtenThisTime < 100 * 1024; )
{
if (timingBytesWritten >= timingBytesRequested)
{
const uint32_t ms = millis() - timingStartMillis;
const float fileMbytes = (float)timingBytesWritten/(float)(1024 * 1024);
const float mbPerSec = (fileMbytes * 1000.0)/(float)ms;
platform.MessageF(gb.GetResponseMessageType(), "SD write speed for %.1fMbyte file was %.2fMbytes/sec\n", (double)fileMbytes, (double)mbPerSec);
sdTimingFile->Close();
sdTimingFile = platform.OpenFile(platform.GetGCodeDir(), TimingFileName, OpenMode::read);
if (sdTimingFile == nullptr)
{
platform.Delete(platform.GetGCodeDir(), TimingFileName);
gb.LatestMachineState().SetError("Failed to re-open timing file");
gb.SetState(GCodeState::normal);
break;
}
platform.Message(gb.GetResponseMessageType(), "Testing SD card read speed...\n");
timingBytesWritten = 0;
timingStartMillis = millis();
gb.SetState(GCodeState::timingSDread);
break;
}
const unsigned int bytesToWrite = min<size_t>(reply.Capacity(), timingBytesRequested - timingBytesWritten);
if (!sdTimingFile->Write(reply.c_str(), bytesToWrite))
{
sdTimingFile->Close();
platform.Delete(platform.GetGCodeDir(), TimingFileName);
gb.LatestMachineState().SetError("Failed to write to timing file");
gb.SetState(GCodeState::normal);
break;
}
timingBytesWritten += bytesToWrite;
writtenThisTime += bytesToWrite;
}
break;
case GCodeState::timingSDread:
for (uint32_t readThisTime = 0; readThisTime < 100 * 1024; )
{
if (timingBytesWritten >= timingBytesRequested)
{
const uint32_t ms = millis() - timingStartMillis;
const float fileMbytes = (float)timingBytesWritten/(float)(1024 * 1024);
const float mbPerSec = (fileMbytes * 1000.0)/(float)ms;
sdTimingFile->Close();
reply.printf("SD read speed for %.1fMbyte file was %.2fMbytes/sec", (double)fileMbytes, (double)mbPerSec);
platform.Delete(platform.GetGCodeDir(), TimingFileName);
gb.SetState(GCodeState::normal);
break;
}
const unsigned int bytesToRead = min<size_t>(reply.Capacity(), timingBytesRequested - timingBytesWritten);
if (sdTimingFile->Read(reply.Pointer(), bytesToRead) != (int)bytesToRead)
{
sdTimingFile->Close();
platform.Delete(platform.GetGCodeDir(), TimingFileName);
gb.LatestMachineState().SetError("Failed to read from timing file");
gb.SetState(GCodeState::normal);
break;
}
timingBytesWritten += bytesToRead;
readThisTime += bytesToRead;
}
break;
#endif
#if HAS_LINUX_INTERFACE
case GCodeState::waitingForAcknowledgement: // finished M291 and the SBC expects a response next
#endif
case GCodeState::checkError: // we return to this state after running the retractprobe macro when there may be a stored error message
gb.SetState(GCodeState::normal);
break;
default: // should not happen
gb.LatestMachineState().SetError("Undefined GCodeState");
gb.SetState(GCodeState::normal);
break;
}
if (gb.GetState() == GCodeState::normal)
{
// We completed a command, so unlock resources and tell the host about it
gb.StopTimer();
UnlockAll(gb);
gb.LatestMachineState().RetrieveStateMachineResult(stateMachineResult, reply);
HandleReply(gb, stateMachineResult, reply.c_str());
CheckForDeferredPause(gb);
}
}
// Do a manual probe. This is in its own function to reduce the amount of stack space needed by RunStateMachine(). See the comment at the top of that function.
void GCodes::DoStraightManualProbe(GCodeBuffer& gb, const StraightProbeSettings& sps)
{
String<StringLength256> message;
message.printf("Adjust position until the reference point just %s the target, then press OK", sps.ProbingAway() ? "loses contact with" : "touches");
DoManualProbe(gb, message.c_str(), "Manual Straight Probe", sps.GetMovingAxes());
}
// End
|