forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCodes.cpp
More file actions
2535 lines (2166 loc) · 63.4 KB
/
Copy pathGCodes.cpp
File metadata and controls
2535 lines (2166 loc) · 63.4 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
/****************************************************************************************************
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"
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: ");
cannedCycleGCode = new GCodeBuffer(platform, "macro: ");
}
void GCodes::Exit()
{
platform->Message(BOTH_MESSAGE, "GCodes class exited.\n");
active = false;
}
void GCodes::Init()
{
webGCode->Init();
fileGCode->Init();
serialGCode->Init();
cannedCycleGCode->Init();
webGCode->SetFinished(true);
fileGCode->SetFinished(true);
serialGCode->SetFinished(true);
cannedCycleGCode->SetFinished(true);
moveAvailable = false;
drivesRelative = true;
axesRelative = false;
checkEndStops = false;
gCodeLetters = GCODE_LETTERS;
distanceScale = 1.0;
for(int8_t i = 0; i < DRIVES - AXES; i++)
lastPos[i] = 0.0;
fileBeingPrinted = NULL;
fileToPrint = NULL;
fileBeingWritten = NULL;
configFile = NULL;
doingCannedCycleFile = false;
eofString = EOF_STRING;
eofStringCounter = 0;
eofStringLength = strlen(eofString);
homeX = false;
homeY = false;
homeZ = false;
homeAxisMoveCount = 0;
offSetSet = false;
dwellWaiting = false;
stackPointer = 0;
zProbesSet = false;
probeCount = 0;
cannedCycleMoveCount = 0;
cannedCycleMoveQueued = false;
limitAxes = true;
axisIsHomed[X_AXIS] = axisIsHomed[Y_AXIS] = axisIsHomed[Z_AXIS] = false;
toolChangeSequence = 0;
active = true;
longWait = platform->Time();
dwellTime = longWait;
}
void GCodes::DoFilePrint(GCodeBuffer* gb)
{
char b;
if(fileBeingPrinted != NULL)
{
if(fileBeingPrinted->Read(b))
{
if(gb->Put(b))
gb->SetFinished(ActOnGcode(gb));
} else
{
if(gb->Put('\n')) // In case there wasn't one ending the file
gb->SetFinished(ActOnGcode(gb));
fileBeingPrinted->Close();
fileBeingPrinted = NULL;
}
}
}
void GCodes::Spin()
{
if(!active)
return;
// Check each of the sources of G Codes (web, serial, and file) to
// see if what they are doing has been done. If it hasn't, return without
// looking at anything else.
//
// Note the order establishes a priority: web first, then serial, and file
// last. If file weren't last, then the others would never get a look in when
// a file was being printed.
if(!webGCode->Finished())
{
webGCode->SetFinished(ActOnGcode(webGCode));
platform->ClassReport("GCodes", longWait);
return;
}
if(!serialGCode->Finished())
{
serialGCode->SetFinished(ActOnGcode(serialGCode));
platform->ClassReport("GCodes", longWait);
return;
}
if(!fileGCode->Finished())
{
fileGCode->SetFinished(ActOnGcode(fileGCode));
platform->ClassReport("GCodes", longWait);
return;
}
// Now check if a G Code byte is available from each of the sources
// in the same order for the same reason.
if(webserver->GCodeAvailable())
{
int8_t i = 0;
do
{
char b = webserver->ReadGCode();
if(webGCode->Put(b))
{
// we have a complete gcode
if(webGCode->WritingFileDirectory() != NULL)
{
WriteGCodeToFile(webGCode);
}
else
{
webGCode->SetFinished(ActOnGcode(webGCode));
}
break; // stop after receiving a complete gcode in case we haven't finished processing it
}
++i;
} while ( i < 16 && webserver->GCodeAvailable());
platform->ClassReport("GCodes", longWait);
return;
}
// Now the serial interface. First check the special case of our
// uploading the reprap.htm file
if(serialGCode->WritingFileDirectory() == platform->GetWebDir())
{
if(platform->GetLine()->Status() & byteAvailable)
{
char b;
platform->GetLine()->Read(b);
WriteHTMLToFile(b, serialGCode);
}
} else
{
// Otherwise just deal in general with incoming bytes from the serial interface
if(platform->GetLine()->Status() & byteAvailable)
{
// Read several bytes instead of just one. This approximately doubles the speed of file uploading.
int8_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);
}
else
{
serialGCode->SetFinished(ActOnGcode(serialGCode));
}
break; // stop after receiving a complete gcode in case we haven't finished processing it
}
++i;
} while (i < 16 && (platform->GetLine()->Status() & byteAvailable));
platform->ClassReport("GCodes", longWait);
return;
}
}
DoFilePrint(fileGCode);
platform->ClassReport("GCodes", longWait);
}
void GCodes::Diagnostics()
{
platform->Message(BOTH_MESSAGE, "GCodes Diagnostics:\n");
}
// The wait till everything's done function. If you need the machine to
// be idle before you do something (for example homeing 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()->ResumeMoving();
// 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];
fileStack[stackPointer] = fileBeingPrinted;
stackPointer++;
platform->PushMessageIndent();
return true;
}
// Recover a saved state. Call repeatedly till it returns true.
bool GCodes::Pop()
{
if(stackPointer <= 0)
{
platform->Message(BOTH_ERROR_MESSAGE, "Pop(): stack underflow!\n");
return true;
}
if(!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
stackPointer--;
drivesRelative = drivesRelativeStack[stackPointer];
axesRelative = axesRelativeStack[stackPointer];
fileBeingPrinted = fileStack[stackPointer];
platform->PopMessageIndent();
// Remember for next time if we have just been switched
// to absolute drive moves
for(int8_t i = AXES; i < DRIVES; i++)
lastPos[i - AXES] = moveBuffer[i];
// Do a null move to set the correct feedrate
moveBuffer[DRIVES] = feedrateStack[stackPointer];
checkEndStops = false;
moveAvailable = true;
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)
{
// 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;
}
float eMovement[DRIVES-AXES];
int eMoveCount = tool->DriveCount();
gb->GetFloatArray(eMovement, eMoveCount);
if(tool->DriveCount() != eMoveCount)
{
snprintf(scratchString, STRING_LENGTH, "Wrong number of extruder drives for the selected tool: %s\n", gb->Buffer());
platform->Message(HOST_MESSAGE, scratchString);
return false;
}
// Zero every extruder drive as some drives may not be changed
for(int8_t drive = AXES; drive < DRIVES; drive++)
moveBuffer[drive] = 0.0;
// Set the drive values for this tool.
for(int8_t eDrive = 0; eDrive < eMoveCount; eDrive++)
{
int8_t drive = tool->Drive(eDrive);
if(drivesRelative || doingG92)
{
moveBuffer[drive + AXES] = eMovement[eDrive]*distanceScale; //Absolute
if(doingG92)
lastPos[drive] = moveBuffer[drive + AXES];
} else
{
float absE = eMovement[eDrive]*distanceScale;
moveBuffer[drive + AXES] = absE - lastPos[drive];
lastPos[drive] = absE;
}
}
}
// Now the movement axes
for(uint8_t axis = 0; axis < AXES; axis++)
{
if(gb->Seen(gCodeLetters[axis]))
{
float moveArg = gb->GetFValue()*distanceScale;
if (axesRelative && !doingG92)
{
moveArg += moveBuffer[axis];
}
if (applyLimits && axis < 2 && axisIsHomed[axis] && !doingG92) // limit X & Y moves unless doing G92. FIXME: No Z for the moment as we often need to move -ve to set the origin
{
if (moveArg < 0.0)
{
moveArg = 0.0;
} else if (moveArg > platform->AxisLength(axis))
{
moveArg = platform->AxisLength(axis);
}
}
moveBuffer[axis] = moveArg;
if (doingG92)
{
axisIsHomed[axis] = true; // doing a G92 is equivalent to homing the axis. FIXME: No it's not if the coordinate specified wasn't 0.
}
}
}
// Deal with feedrate
if(gb->Seen(FEEDRATE_LETTER))
moveBuffer[DRIVES] = gb->GetFValue()*distanceScale*0.016666667; // G Code feedrates are in mm/minute; we need mm/sec
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)
// this returns false, otherwise true.
bool GCodes::SetUpMove(GCodeBuffer *gb)
{
// Last one gone yet?
if(moveAvailable)
return false;
// Load the last position into moveBuffer; If Move can't accept more, return false
if(!reprap.GetMove()->GetCurrentUserPosition(moveBuffer))
return false;
//check to see if the move is a 'homing' move that endstops are checked on.
checkEndStops = false;
if(gb->Seen('S'))
{
if(gb->GetIValue() == 1)
checkEndStops = true;
}
//loads the movebuffer with either the absolute movement required or the
//relative movement required
moveAvailable = LoadMoveBufferFromGCode(gb, false, !checkEndStops && limitAxes);
return true;
}
// The Move class calls this function to find what to do next.
bool GCodes::ReadMove(float m[], bool& ce)
{
if(!moveAvailable)
return false;
for(int8_t i = 0; i <= DRIVES; i++) // 1 more for feedrate
m[i] = moveBuffer[i];
ce = checkEndStops;
moveAvailable = false;
checkEndStops = false;
return true;
}
bool GCodes::DoFileCannedCycles(const char* fileName)
{
// Have we started the file?
if(!doingCannedCycleFile)
{
// No
if(!Push())
return false;
fileBeingPrinted = platform->GetFileStore(platform->GetSysDir(), fileName, false);
if(fileBeingPrinted == NULL)
{
snprintf(scratchString, STRING_LENGTH, "Macro file %s not found.\n ", fileName);
platform->Message(HOST_MESSAGE, scratchString);
if(!Pop())
platform->Message(HOST_MESSAGE, "Cannot pop the stack.\n");
return true;
}
doingCannedCycleFile = true;
cannedCycleGCode->Init();
return false;
}
// Have we finished the file?
if(fileBeingPrinted == NULL)
{
// Yes
if(!Pop())
return false;
doingCannedCycleFile = false;
cannedCycleGCode->Init();
return true;
}
// No - Do more of the file
if(!cannedCycleGCode->Finished())
{
cannedCycleGCode->SetFinished(ActOnGcode(cannedCycleGCode));
return false;
}
DoFilePrint(cannedCycleGCode);
return false;
}
bool GCodes::FileCannedCyclesReturn()
{
if(!doingCannedCycleFile)
return true;
if(!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
doingCannedCycleFile = false;
cannedCycleGCode->Init();
if(fileBeingPrinted != NULL)
fileBeingPrinted->Close();
fileBeingPrinted = NULL;
return true;
}
// 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 feedrate
// you want (if action[DRIVES] is true).
bool GCodes::DoCannedCycleMove(bool 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(int8_t drive = 0; drive <= DRIVES; drive++)
{
if(activeDrive[drive])
moveBuffer[drive] = moveToDo[drive];
}
checkEndStops = 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))
{
// 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);
reprap.GetMove()->SetFeedrate(platform->InstantDv(platform->SlowestDrive())); // On a G92 we must effectively be stationary
}
return true;
}
// 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(int8_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(int8_t axis = 0; axis < AXES; axis++)
{
if(gb->Seen(gCodeLetters[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(false))
{
//LoadMoveBufferFromArray(record);
for(int 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(char* reply, bool& error)
//pre(reply.upb == STRING_LENGTH)
{
if(homeX && homeY && homeZ)
{
if(DoFileCannedCycles(HOME_ALL_G))
{
homeAxisMoveCount = 0;
homeX = false;
homeY = false;
homeZ = false;
axisIsHomed[X_AXIS] = axisIsHomed[Y_AXIS] = axisIsHomed[Z_AXIS] = true;
return true;
}
return false;
}
if(homeX)
{
if(DoFileCannedCycles(HOME_X_G))
{
homeAxisMoveCount = 0;
homeX = false;
axisIsHomed[X_AXIS] = true;
return NoHome();
}
return false;
}
if(homeY)
{
if(DoFileCannedCycles(HOME_Y_G))
{
homeAxisMoveCount = 0;
homeY = false;
axisIsHomed[Y_AXIS] = true;
return NoHome();
}
return false;
}
if(homeZ)
{
//FIXME this check should be optional
if (!(axisIsHomed[X_AXIS] && axisIsHomed[Y_AXIS]))
{
// We can only home Z if X and Y have already been homed. Possibly this should only be if we are using an IR probe.
strncpy(reply, "Must home X and Y before homing Z", STRING_LENGTH);
error = true;
homeZ = false;
return true;
}
if(DoFileCannedCycles(HOME_Z_G))
{
homeAxisMoveCount = 0;
homeZ = false;
axisIsHomed[Z_AXIS] = true;
return NoHome();
}
return false;
}
// Should never get here
checkEndStops = false;
moveAvailable = false;
homeAxisMoveCount = 0;
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()
{
float x, y, z;
reprap.GetMove()->SetIdentityTransform(); // It doesn't matter if these are called repeatedly
for(int8_t drive = 0; drive <= DRIVES; drive++)
activeDrive[drive] = false;
switch(cannedCycleMoveCount)
{
case 0: // 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->HomeFeedRate(Z_AXIS);
activeDrive[DRIVES] = true;
reprap.GetMove()->SetZProbing(false);
if(DoCannedCycleMove(false))
cannedCycleMoveCount++;
return false;
case 1:
GetProbeCoordinates(probeCount, moveToDo[X_AXIS], moveToDo[Y_AXIS], moveToDo[Z_AXIS]);
activeDrive[X_AXIS] = true;
activeDrive[Y_AXIS] = true;
// NB - we don't use the Z value
moveToDo[DRIVES] = platform->HomeFeedRate(X_AXIS);
activeDrive[DRIVES] = true;
reprap.GetMove()->SetZProbing(false);
if(DoCannedCycleMove(false))
cannedCycleMoveCount++;
return false;
case 2:
moveToDo[Z_AXIS] = -2.0*platform->AxisLength(Z_AXIS);
activeDrive[Z_AXIS] = true;
moveToDo[DRIVES] = platform->HomeFeedRate(Z_AXIS);
activeDrive[DRIVES] = true;
reprap.GetMove()->SetZProbing(true);
if(DoCannedCycleMove(true))
{
cannedCycleMoveCount++;
axisIsHomed[Z_AXIS] = true; // we now home the Z-axis in Move.cpp it is wasn't already
}
return false;
case 3:
moveToDo[Z_AXIS] = Z_DIVE;
activeDrive[Z_AXIS] = true;
moveToDo[DRIVES] = platform->HomeFeedRate(Z_AXIS);
activeDrive[DRIVES] = true;
reprap.GetMove()->SetZProbing(false);
if(DoCannedCycleMove(false))
cannedCycleMoveCount++;
return false;
default:
cannedCycleMoveCount = 0;
reprap.GetMove()->SetZBedProbePoint(probeCount, reprap.GetMove()->GetLastProbedZ());
return true;
}
}
// This simply moves down till the Z probe/switch is triggered.
bool GCodes::DoSingleZProbe()
{
if(!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
for(int8_t drive = 0; drive <= DRIVES; drive++)
activeDrive[drive] = false;
moveToDo[Z_AXIS] = -1.1*platform->AxisLength(Z_AXIS);
activeDrive[Z_AXIS] = true;
moveToDo[DRIVES] = platform->HomeFeedRate(Z_AXIS);
activeDrive[DRIVES] = true;
if(DoCannedCycleMove(true))
{
cannedCycleMoveCount = 0;
probeCount = 0;
axisIsHomed[Z_AXIS] = true; // we have homed the Z axis
return true;
}
return false;
}
// This sets wherever we are as the probe point P (probePointIndex)
// then probes the bed, or gets all its parameters from the arguments.
// If X or Y are specified, use those; otherwise use the machine's
// coordinates. If no Z is specified use the machine's coordinates. If it
// is specified and is greater than SILLY_Z_VALUE (i.e. greater than -9999.0)
// then that value is used. If it's less than SILLY_Z_VALUE the bed is
// probed and that value is used.
bool GCodes::SetSingleZProbeAtAPosition(GCodeBuffer *gb)
{
if(!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
if(!gb->Seen('P'))
return DoSingleZProbe();
int probePointIndex = gb->GetIValue();
float x, y, z;
if(gb->Seen(gCodeLetters[X_AXIS]))
x = gb->GetFValue();
else
x = moveBuffer[X_AXIS];
if(gb->Seen(gCodeLetters[Y_AXIS]))
y = gb->GetFValue();
else
y = moveBuffer[Y_AXIS];
if(gb->Seen(gCodeLetters[Z_AXIS]))
z = gb->GetFValue();
else
z = moveBuffer[Z_AXIS];
probeCount = probePointIndex;
reprap.GetMove()->SetXBedProbePoint(probeCount, x);
reprap.GetMove()->SetYBedProbePoint(probeCount, y);
if(z > SILLY_Z_VALUE)
{
reprap.GetMove()->SetZBedProbePoint(probeCount, z);
reprap.GetMove()->SetZProbing(false); // Not really needed, but let's be safe
probeCount = 0;
if(gb->Seen('S'))
{
zProbesSet = true;
reprap.GetMove()->SetProbedBedEquation();
}
return true;
} else
{
if(DoSingleZProbeAtPoint())
{
probeCount = 0;
reprap.GetMove()->SetZProbing(false);
if(gb->Seen('S'))
{
zProbesSet = true;
reprap.GetMove()->SetProbedBedEquation();
}
return true;
}
}
return false;
}
// This probes multiple points on the bed (three in a
// triangle or four in the corners), then sets the bed transformation to compensate
// for the bed not quite being the plane Z = 0.
bool GCodes::DoMultipleZProbe()
{
if(reprap.GetMove()->NumberOfXYProbePoints() < 3)
{
platform->Message(HOST_MESSAGE, "Bed probing: there needs to be 3 or more points set.\n");
return true;
}
if(DoSingleZProbeAtPoint())
probeCount++;
if(probeCount >= reprap.GetMove()->NumberOfXYProbePoints())
{
probeCount = 0;
zProbesSet = true;
reprap.GetMove()->SetZProbing(false);
reprap.GetMove()->SetProbedBedEquation();
return true;
}
return false;
}
// This returns the (X, Y) points to probe the bed at probe point count. When probing,
// it returns false. If called after probing has ended it returns true, and the Z coordinate
// probed is also returned.
bool GCodes::GetProbeCoordinates(int count, float& x, float& y, float& z)
{
x = reprap.GetMove()->xBedProbePoint(count);
y = reprap.GetMove()->yBedProbePoint(count);
z = reprap.GetMove()->zBedProbePoint(count);
return zProbesSet;
}
bool GCodes::SetPrintZProbe(GCodeBuffer* gb, char* reply)
{
if(!AllMovesAreFinishedAndMoveBufferIsLoaded())
return false;
if(gb->Seen(gCodeLetters[Z_AXIS]))
{
platform->SetZProbeStopHeight(gb->GetFValue());
if(gb->Seen('P'))
{
platform->SetZProbe(gb->GetIValue());
}
} else if (platform->GetZProbeType() == 2)
{
snprintf(reply, STRING_LENGTH, "%d (%d)", platform->ZProbe(), platform->ZProbeOnVal());
}
else
{
snprintf(reply, STRING_LENGTH, "%d", platform->ZProbe());
}
return true;
}
// Return the current coordinates as a printable string. Coordinates
// are updated at the end of each movement, so this won't tell you
// where you are mid-movement.
//Fixed to deal with multiple extruders
char* GCodes::GetCurrentCoordinates()
{
float liveCoordinates[DRIVES+1];
reprap.GetMove()->LiveCoordinates(liveCoordinates);
snprintf(scratchString, STRING_LENGTH, "X:%f Y:%f Z:%f ", liveCoordinates[X_AXIS], liveCoordinates[Y_AXIS], liveCoordinates[Z_AXIS]);
char eString[STRING_LENGTH];
for(int i = AXES; i< DRIVES; i++)
{
snprintf(eString,STRING_LENGTH,"E%d:%f ",i-AXES,liveCoordinates[i]);
strncat(scratchString,eString,STRING_LENGTH);
}
return scratchString;
}
void GCodes::OpenFileToWrite(const char* directory, const char* fileName, GCodeBuffer *gb)
{
fileBeingWritten = platform->GetFileStore(directory, fileName, true);
if(fileBeingWritten == NULL)
{
platform->Message(HOST_MESSAGE, "Can't open GCode file for writing.\n");
}
else
{
gb->SetWritingFileDirectory(directory);
}
eofStringCounter = 0;
}
void GCodes::WriteHTMLToFile(char b, GCodeBuffer *gb)
{
char reply[1];
reply[0] = 0;
if(fileBeingWritten == NULL)
{
platform->Message(HOST_MESSAGE, "Attempt to write to a null file.\n");
return;
}
fileBeingWritten->Write(b);
if(b == eofString[eofStringCounter])
{
eofStringCounter++;
if(eofStringCounter >= eofStringLength)
{
fileBeingWritten->Close();
fileBeingWritten = NULL;
gb->SetWritingFileDirectory(NULL);
char* r = reply;
if(platform->Emulating() == marlin)
r = "Done saving file.";
HandleReply(false, gb == serialGCode , r, 'M', 560, false);
return;
}
} else
eofStringCounter = 0;
}
void GCodes::WriteGCodeToFile(GCodeBuffer *gb)
{
char reply[1];
reply[0] = 0;
if(fileBeingWritten == NULL)
{
platform->Message(HOST_MESSAGE, "Attempt to write to a null file.\n");
return;
}
// End of file?
if(gb->Seen('M'))
{
if(gb->GetIValue() == 29)
{
fileBeingWritten->Close();
fileBeingWritten = NULL;
gb->SetWritingFileDirectory(NULL);
char* r = reply;
if(platform->Emulating() == marlin)