forked from Duet3D/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.cpp
More file actions
1505 lines (1353 loc) · 43.3 KB
/
Move.cpp
File metadata and controls
1505 lines (1353 loc) · 43.3 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
/*
* Move.cpp
*
* Created on: 7 Dec 2014
* Author: David
*/
#include "RepRapFirmware.h"
Move::Move(Platform* p, GCodes* g) : currentDda(NULL)
{
active = false;
// Build the DDA ring
DDA *dda = new DDA(NULL);
ddaRingGetPointer = ddaRingAddPointer = dda;
for (size_t i = 1; i < DdaRingLength; i++)
{
DDA *oldDda = dda;
dda = new DDA(dda);
oldDda->SetPrevious(dda);
}
ddaRingAddPointer->SetNext(dda);
dda->SetPrevious(ddaRingAddPointer);
}
void Move::Init()
{
// Reset Cartesian mode
deltaParams.Init();
coreXYMode = 0;
for (size_t axis = 0; axis < AXES; ++axis)
{
axisFactors[axis] = 1.0;
}
deltaProbing = false;
// Empty the ring
ddaRingGetPointer = ddaRingCheckPointer = ddaRingAddPointer;
DDA *dda = ddaRingAddPointer;
do
{
dda->Init();
dda = dda->GetNext();
} while (dda != ddaRingAddPointer);
currentDda = nullptr;
addNoMoreMoves = false;
stepErrors = 0;
// Clear the transforms
SetIdentityTransform();
tanXY = tanYZ = tanXZ = 0.0;
// Put the origin on the lookahead ring with default velocity in the previous position to the first one that will be used.
// Do this by calling SetLiveCoordinates and SetPositions, so that the motor coordinates will be correct too even on a delta.
float move[DRIVES];
for (size_t i = 0; i < DRIVES; i++)
{
move[i] = 0.0;
liveEndPoints[i] = 0; // not actually right for a delta, but better than printing random values in response to M114
reprap.GetPlatform()->SetDirection(i, FORWARDS); // DC: I don't see any reason why we do this
}
SetLiveCoordinates(move);
SetPositions(move);
currentFeedrate = DefaultFeedRate;
// Set up default bed probe points. This is only a guess, because we don't know the bed size yet.
for (size_t point = 0; point < MaxProbePoints; point++)
{
if (point < 4)
{
xBedProbePoints[point] = (0.3 + 0.6*(float)(point%2))*reprap.GetPlatform()->AxisMaximum(X_AXIS);
yBedProbePoints[point] = (0.0 + 0.9*(float)(point/2))*reprap.GetPlatform()->AxisMaximum(Y_AXIS);
}
zBedProbePoints[point] = 0.0;
probePointSet[point] = unset;
}
xRectangle = 1.0/(0.8*reprap.GetPlatform()->AxisMaximum(X_AXIS));
yRectangle = xRectangle;
longWait = reprap.GetPlatform()->Time();
idleTimeout = defaultIdleTimeout;
iState = IdleState::idle;
idleCount = 0;
simulating = false;
simulationTime = 0.0;
active = true;
}
void Move::Exit()
{
reprap.GetPlatform()->Message(BOTH_MESSAGE, "Move class exited.\n");
active = false;
}
void Move::Spin()
{
if (!active)
{
return;
}
if (idleCount < 1000)
{
++idleCount;
}
// Check for DDA errors to print if Move debug is enabled
while (ddaRingCheckPointer->GetState() == DDA::completed)
{
if (ddaRingCheckPointer->HasStepError())
{
if (reprap.Debug(moduleMove))
{
ddaRingCheckPointer->DebugPrint();
}
++stepErrors;
}
ddaRingCheckPointer->Free();
ddaRingCheckPointer = ddaRingCheckPointer->GetNext();
}
// See if we can add another move to the ring
if (!addNoMoreMoves && ddaRingAddPointer->GetState() == DDA::empty)
{
DDA *dda = ddaRingAddPointer;
// In order to react faster to speed and extrusion rate changes, only add more moves if the total duration of
// all un-frozen moves is less than 2 seconds, or the total duration of all but the first un-frozen move is
// less than 0.5 seconds.
float unPreparedTime = 0.0;
float prevMoveTime = 0.0;
for(;;)
{
dda = dda->GetPrevious();
if (dda->GetState() != DDA::provisional)
{
break;
}
unPreparedTime += prevMoveTime;
prevMoveTime = dda->CalcTime();
}
if (unPreparedTime < 0.5 || unPreparedTime + prevMoveTime < 2.0)
{
// If there's a G Code move available, add it to the DDA ring for processing.
float nextMove[DRIVES + 1];
EndstopChecks endStopsToCheck;
uint8_t moveType;
FilePosition filePos;
if (reprap.GetGCodes()->ReadMove(nextMove, endStopsToCheck, moveType, filePos))
{
// We have a new move
currentFeedrate = nextMove[DRIVES]; // might be G1 with just an F field
#if 0 //*** This code is not finished yet ***
// If we are doing bed compensation and the move crosses a compensation boundary by a significant amount,
// segment it so that we can apply proper bed compensation
// Issues here:
// 1. Are there enough DDAs? need to make nextMove static and remember whether we have the remains of a move in there.
// 2. Pause/restart: if we restart a segmented move when we have already executed part of it, we will extrude too much.
// Perhaps remember how much of the last move we executed? Or always insist on completing all the segments in a move?
bool isSegmented;
do
{
float tempMove[DRIVES + 1];
memcpy(tempMove, nextMove, sizeof(tempMove));
isSegmented = SegmentMove(tempMove);
if (isSegmented)
{
// Extruder moves are relative, so we need to adjust the extrusion amounts in the original move
for (size_t drive = AXES; drive < DRIVES; ++drive)
{
nextMove[drive] -= tempMove[drive];
}
}
bool doMotorMapping = (moveType == 0) || (moveType == 1 && !IsDeltaMode());
if (doMotorMapping)
{
Transform(tempMove);
}
if (ddaRingAddPointer->Init(tempMove, endStopsToCheck, doMotorMapping, filePos))
{
ddaRingAddPointer = ddaRingAddPointer->GetNext();
idleCount = 0;
}
} while (isSegmented);
#else // Use old code
bool doMotorMapping = (moveType == 0) || (moveType == 1 && !IsDeltaMode());
if (doMotorMapping)
{
Transform(nextMove);
}
if (ddaRingAddPointer->Init(nextMove, endStopsToCheck, doMotorMapping, filePos))
{
ddaRingAddPointer = ddaRingAddPointer->GetNext();
idleCount = 0;
}
#endif
}
}
}
if (simulating)
{
if (idleCount > 10 && !DDARingEmpty())
{
// No move added this time, so simulate executing one already in the queue
DDA *dda = ddaRingGetPointer;
simulationTime += dda->CalcTime();
liveCoordinatesValid = dda->FetchEndPosition(const_cast<int32_t*>(liveEndPoints), const_cast<float *>(liveCoordinates));
dda->Complete();
ddaRingGetPointer = ddaRingGetPointer->GetNext();
}
}
else if (!deltaProbing)
{
// See whether we need to kick off a move
DDA *cdda = currentDda; // currentDda is volatile, so copy it
if (cdda == nullptr)
{
// No DDA is executing, so start executing a new one if possible
if (idleCount > 10) // better to have a few moves in the queue so that we can do lookahead
{
DDA *dda = ddaRingGetPointer;
if (dda->GetState() == DDA::provisional)
{
dda->Prepare();
}
if (dda->GetState() == DDA::frozen)
{
cpu_irq_disable(); // must call StartNextMove and Interrupt with interrupts disabled
if (StartNextMove(Platform::GetInterruptClocks())) // start the next move if none is executing already
{
Interrupt();
}
cpu_irq_enable();
iState = IdleState::busy;
}
else if (iState == IdleState::busy && !reprap.GetGCodes()->IsPaused() && idleTimeout > 0.0)
{
lastMoveTime = reprap.GetPlatform()->Time(); // record when we first noticed that the machine was idle
iState = IdleState::timing;
}
else if (iState == IdleState::timing && reprap.GetPlatform()->Time() - lastMoveTime >= idleTimeout)
{
reprap.GetPlatform()->SetDrivesIdle(); // put all drives in idle hold
iState = IdleState::idle;
}
}
}
else
{
// See whether we need to prepare any moves
int32_t preparedTime = 0;
DDA::DDAState st;
while ((st = cdda->GetState()) == DDA::completed || st == DDA::executing || st == DDA::frozen)
{
preparedTime += cdda->GetTimeLeft();
cdda = cdda->GetNext();
if (cdda == ddaRingAddPointer)
{
break;
}
}
// If the number of prepared moves will execute in less than the minimum time, prepare another move
while (st == DDA::provisional && preparedTime < (int32_t)(DDA::stepClockRate/8)) // prepare moves one eighth of a second ahead of when they will be needed
{
cdda->Prepare();
preparedTime += cdda->GetTimeLeft();
cdda = cdda->GetNext();
st = cdda->GetState();
}
}
}
reprap.GetPlatform()->ClassReport(longWait);
}
// Pause the print as soon as we can.
// Returns the file position of the first queue move we are going to skip, or noFilePosition we we are not skipping any moves.
// We update 'positions' to the positions and feed rate expected for the next move, and the amount of extrusion in the moves we skipped.
FilePosition Move::PausePrint(float positions[DRIVES+1])
{
// Find a move we can pause after.
// Ideally, we would adjust a move if necessary and possible so that we can pause after it, but for now we don't do that.
// There are a few possibilities:
// 1. There are no moves in the queue.
// 2. There is a currently-executing move, and possibly some more in the queue.
// 3. There are moves in the queue, but we haven't started executing them yet. Unlikely, but possible.
// First, see if there is a currently-executing move, and if so, whether we can safely pause at the end of it
const DDA *savedDdaRingAddPointer = ddaRingAddPointer;
cpu_irq_disable();
DDA *dda = currentDda;
if (dda != nullptr)
{
// A move is being executed. See if we can safely pause at the end of it.
if (dda->CanPause())
{
ddaRingAddPointer = dda->GetNext();
}
else
{
// We can't safely pause after the currently-executing move because its end speed is too high so we may miss steps.
// Search for the next move that we can safely stop after.
dda = ddaRingGetPointer;
while (dda != ddaRingAddPointer)
{
if (dda->CanPause())
{
ddaRingAddPointer = dda->GetNext();
if (ddaRingAddPointer->GetState() == DDA::frozen)
{
// Change the state so that the ISR won't start executing this move
ddaRingAddPointer->Free();
}
break;
}
dda = dda->GetNext();
}
}
}
else
{
// No move being executed
ddaRingAddPointer = ddaRingGetPointer;
}
cpu_irq_enable();
FilePosition fPos = noFilePosition;
if (ddaRingAddPointer != savedDdaRingAddPointer)
{
// We are going to skip some moves. dda points to the last move we are going to print.
for (size_t axis = 0; axis < AXES; ++axis)
{
positions[axis] = dda->GetEndCoordinate(axis, false);
}
for (size_t drive = AXES; drive < DRIVES; ++drive)
{
positions[drive] = 0.0; // clear out extruder movement
}
positions[DRIVES] = dda->GetRequestedSpeed();
// Free the DDAs for the moves we are going to skip, and work out how much extrusion they would have performed
dda = ddaRingAddPointer;
do
{
for (size_t drive = AXES; drive < DRIVES; ++drive)
{
positions[drive] += dda->GetEndCoordinate(drive, true); // update the amount of extrusion we are going to skip
}
if (fPos == noFilePosition)
{
fPos = dda->GetFilePosition();
}
dda->Free();
dda = dda->GetNext();
}
while (dda != savedDdaRingAddPointer);
}
else
{
GetCurrentUserPosition(positions, 0); // gets positions and feed rate, and clears out extrusion values
}
return fPos;
}
uint32_t maxReps = 0;
#if 0
extern uint32_t sqSum1, sqSum2, sqCount, sqErrors, lastRes1, lastRes2;
extern uint64_t lastNum;
#endif
void Move::Diagnostics()
{
reprap.GetPlatform()->AppendMessage(BOTH_MESSAGE, "Move Diagnostics:\n");
reprap.GetPlatform()->AppendMessage(BOTH_MESSAGE, "MaxReps: %u, StepErrors: %u\n", maxReps, stepErrors);
maxReps = 0;
#if 0
if (sqCount != 0)
{
reprap.GetPlatform()->AppendMessage(BOTH_MESSAGE, "Average sqrt times %.2f, %.2f, count %u, errors %u, last %" PRIu64 " %u %u\n",
(float)sqSum1/sqCount, (float)sqSum2/sqCount, sqCount, sqErrors, lastNum, lastRes1, lastRes2);
sqSum1 = sqSum2 = sqCount = sqErrors = 0;
}
#endif
}
// These are the actual numbers we want in the positions, so don't transform them.
void Move::SetPositions(const float move[DRIVES])
{
if (DDARingEmpty())
{
ddaRingAddPointer->GetPrevious()->SetPositions(move, DRIVES);
}
else
{
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "SetPositions called when DDA ring not empty\n");
}
}
void Move::EndPointToMachine(const float coords[], int32_t ep[], size_t numDrives) const
{
MotorTransform(coords, ep);
for (size_t drive = AXES; drive < numDrives; ++drive)
{
ep[drive] = MotorEndPointToMachine(drive, coords[drive]);
}
}
void Move::SetFeedrate(float feedRate)
{
if (DDARingEmpty())
{
DDA *lastMove = ddaRingAddPointer->GetPrevious();
currentFeedrate = feedRate;
lastMove->SetFeedRate(feedRate);
}
else
{
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "SetFeedrate called when DDA ring not empty\n");
}
}
// Returns steps from units (mm) for a particular drive
int32_t Move::MotorEndPointToMachine(size_t drive, float coord)
{
return (int32_t)roundf(coord * reprap.GetPlatform()->DriveStepsPerUnit(drive));
}
// Convert motor coordinates to machine coordinates
// Used after homing and after individual motor moves.
// This is computationally expensive on a delta, so only call it when necessary, and never from the step ISR.
void Move::MachineToEndPoint(const int32_t motorPos[], float machinePos[], size_t numDrives) const
{
const float *stepsPerUnit = reprap.GetPlatform()->GetDriveStepsPerUnit();
// Convert the axes
if (IsDeltaMode())
{
deltaParams.InverseTransform(motorPos[A_AXIS]/stepsPerUnit[A_AXIS], motorPos[B_AXIS]/stepsPerUnit[B_AXIS], motorPos[C_AXIS]/stepsPerUnit[C_AXIS], machinePos);
#if 0
// We don't do inverse transforms very often, so if debugging is enabled, print them
if (reprap.Debug(moduleMove))
{
debugPrintf("Inverse transformed %d %d %d to %f %f %f\n", motorPos[0], motorPos[1], motorPos[2], machinePos[0], machinePos[1], machinePos[2]);
}
#endif
}
else
{
switch (coreXYMode)
{
case 1: // CoreXY
machinePos[X_AXIS] = ((motorPos[X_AXIS] * stepsPerUnit[Y_AXIS]) - (motorPos[Y_AXIS] * stepsPerUnit[X_AXIS]))
/(2 * axisFactors[X_AXIS] * stepsPerUnit[X_AXIS] * stepsPerUnit[Y_AXIS]);
machinePos[Y_AXIS] = ((motorPos[X_AXIS] * stepsPerUnit[Y_AXIS]) + (motorPos[Y_AXIS] * stepsPerUnit[X_AXIS]))
/(2 * axisFactors[Y_AXIS] * stepsPerUnit[X_AXIS] * stepsPerUnit[Y_AXIS]);
machinePos[Z_AXIS] = motorPos[Z_AXIS]/stepsPerUnit[Z_AXIS];
break;
case 2: // CoreXZ
machinePos[X_AXIS] = ((motorPos[X_AXIS] * stepsPerUnit[Z_AXIS]) - (motorPos[Z_AXIS] * stepsPerUnit[X_AXIS]))
/(2 * axisFactors[X_AXIS] * stepsPerUnit[X_AXIS] * stepsPerUnit[Z_AXIS]);
machinePos[Y_AXIS] = motorPos[Y_AXIS]/stepsPerUnit[Y_AXIS];
machinePos[Z_AXIS] = ((motorPos[X_AXIS] * stepsPerUnit[Z_AXIS]) + (motorPos[Z_AXIS] * stepsPerUnit[X_AXIS]))
/(2 * axisFactors[Z_AXIS] * stepsPerUnit[X_AXIS] * stepsPerUnit[Z_AXIS]);
break;
case 3: // CoreYZ
machinePos[X_AXIS] = motorPos[X_AXIS]/stepsPerUnit[X_AXIS];
machinePos[Y_AXIS] = ((motorPos[Y_AXIS] * stepsPerUnit[Z_AXIS]) - (motorPos[Z_AXIS] * stepsPerUnit[Y_AXIS]))
/(2 * axisFactors[Y_AXIS] * stepsPerUnit[Y_AXIS] * stepsPerUnit[Z_AXIS]);
machinePos[Z_AXIS] = ((motorPos[Y_AXIS] * stepsPerUnit[Z_AXIS]) + (motorPos[Z_AXIS] * stepsPerUnit[Y_AXIS]))
/(2 * axisFactors[Z_AXIS] * stepsPerUnit[Y_AXIS] * stepsPerUnit[Z_AXIS]);
break;
default:
machinePos[X_AXIS] = motorPos[X_AXIS]/stepsPerUnit[X_AXIS];
machinePos[Y_AXIS] = motorPos[Y_AXIS]/stepsPerUnit[Y_AXIS];
machinePos[Z_AXIS] = motorPos[Z_AXIS]/stepsPerUnit[Z_AXIS];
break;
}
}
// Convert the extruders
for (size_t drive = AXES; drive < numDrives; ++drive)
{
machinePos[drive] = motorPos[drive]/stepsPerUnit[drive];
}
}
// Convert Cartesian coordinates to delta motor steps
void Move::MotorTransform(const float machinePos[AXES], int32_t motorPos[AXES]) const
{
if (IsDeltaMode())
{
for (size_t axis = 0; axis < AXES; ++axis)
{
motorPos[axis] = MotorEndPointToMachine(axis, deltaParams.Transform(machinePos, axis));
}
if (reprap.Debug(moduleMove) && reprap.Debug(moduleDda))
{
debugPrintf("Transformed %f %f %f to %d %d %d\n", machinePos[0], machinePos[1], machinePos[2], motorPos[0], motorPos[1], motorPos[2]);
}
}
else
{
switch (coreXYMode)
{
case 1: // CoreXY
motorPos[X_AXIS] = MotorEndPointToMachine(X_AXIS, (machinePos[X_AXIS] * axisFactors[X_AXIS]) + (machinePos[Y_AXIS] * axisFactors[Y_AXIS]));
motorPos[Y_AXIS] = MotorEndPointToMachine(Y_AXIS, (machinePos[Y_AXIS] * axisFactors[Y_AXIS]) - (machinePos[X_AXIS] * axisFactors[X_AXIS]));
motorPos[Z_AXIS] = MotorEndPointToMachine(Z_AXIS, machinePos[Z_AXIS]);
break;
case 2: // CoreXZ
motorPos[X_AXIS] = MotorEndPointToMachine(X_AXIS, (machinePos[X_AXIS] * axisFactors[X_AXIS]) + (machinePos[Z_AXIS] * axisFactors[Z_AXIS]));
motorPos[Y_AXIS] = MotorEndPointToMachine(Y_AXIS, machinePos[Y_AXIS]);
motorPos[Z_AXIS] = MotorEndPointToMachine(Z_AXIS, (machinePos[Z_AXIS] * axisFactors[Z_AXIS]) - (machinePos[X_AXIS] * axisFactors[X_AXIS]));
break;
case 3: // CoreYZ
motorPos[X_AXIS] = MotorEndPointToMachine(X_AXIS, machinePos[X_AXIS]);
motorPos[Y_AXIS] = MotorEndPointToMachine(Y_AXIS, (machinePos[Y_AXIS] * axisFactors[Y_AXIS]) + (machinePos[Z_AXIS] * axisFactors[Z_AXIS]));
motorPos[Z_AXIS] = MotorEndPointToMachine(Z_AXIS, (machinePos[Z_AXIS] * axisFactors[Z_AXIS]) - (machinePos[Y_AXIS] * axisFactors[Y_AXIS]));
break;
default: // Cartesian
motorPos[X_AXIS] = MotorEndPointToMachine(X_AXIS, machinePos[X_AXIS]);
motorPos[Y_AXIS] = MotorEndPointToMachine(Y_AXIS, machinePos[Y_AXIS]);
motorPos[Z_AXIS] = MotorEndPointToMachine(Z_AXIS, machinePos[Z_AXIS]);
break;
}
}
}
// Do the Axis transform BEFORE the bed transform
void Move::AxisTransform(float xyzPoint[AXES]) const
{
xyzPoint[X_AXIS] = xyzPoint[X_AXIS] + tanXY*xyzPoint[Y_AXIS] + tanXZ*xyzPoint[Z_AXIS];
xyzPoint[Y_AXIS] = xyzPoint[Y_AXIS] + tanYZ*xyzPoint[Z_AXIS];
}
// Invert the Axis transform AFTER the bed transform
void Move::InverseAxisTransform(float xyzPoint[AXES]) const
{
xyzPoint[Y_AXIS] = xyzPoint[Y_AXIS] - tanYZ*xyzPoint[Z_AXIS];
xyzPoint[X_AXIS] = xyzPoint[X_AXIS] - (tanXY*xyzPoint[Y_AXIS] + tanXZ*xyzPoint[Z_AXIS]);
}
void Move::Transform(float xyzPoint[AXES]) const
{
AxisTransform(xyzPoint);
BedTransform(xyzPoint);
}
void Move::InverseTransform(float xyzPoint[AXES]) const
{
InverseBedTransform(xyzPoint);
InverseAxisTransform(xyzPoint);
}
// Do the bed transform AFTER the axis transform
void Move::BedTransform(float xyzPoint[AXES]) const
{
switch(numBedCompensationPoints)
{
case 0:
break;
case 3:
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] + aX*xyzPoint[X_AXIS] + aY*xyzPoint[Y_AXIS] + aC;
break;
case 4:
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] + SecondDegreeTransformZ(xyzPoint[X_AXIS], xyzPoint[Y_AXIS]);
break;
case 5:
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] + TriangleZ(xyzPoint[X_AXIS], xyzPoint[Y_AXIS]);
break;
default:
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "BedTransform: wrong number of sample points.");
}
}
// Invert the bed transform BEFORE the axis transform
void Move::InverseBedTransform(float xyzPoint[AXES]) const
{
switch(numBedCompensationPoints)
{
case 0:
break;
case 3:
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] - (aX*xyzPoint[X_AXIS] + aY*xyzPoint[Y_AXIS] + aC);
break;
case 4:
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] - SecondDegreeTransformZ(xyzPoint[X_AXIS], xyzPoint[Y_AXIS]);
break;
case 5:
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] - TriangleZ(xyzPoint[X_AXIS], xyzPoint[Y_AXIS]);
break;
default:
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "InverseBedTransform: wrong number of sample points.");
}
}
void Move::SetIdentityTransform()
{
numBedCompensationPoints = 0;
}
float Move::AxisCompensation(int8_t axis) const
{
switch(axis)
{
case X_AXIS:
return tanXY;
case Y_AXIS:
return tanYZ;
case Z_AXIS:
return tanXZ;
default:
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "Axis compensation requested for non-existent axis.\n");
}
return 0.0;
}
void Move::SetAxisCompensation(int8_t axis, float tangent)
{
switch(axis)
{
case X_AXIS:
tanXY = tangent;
break;
case Y_AXIS:
tanYZ = tangent;
break;
case Z_AXIS:
tanXZ = tangent;
break;
default:
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "SetAxisCompensation: dud axis.\n");
}
}
void Move::BarycentricCoordinates(size_t p1, size_t p2, size_t p3, float x, float y, float& l1, float& l2, float& l3) const
{
float y23 = baryYBedProbePoints[p2] - baryYBedProbePoints[p3];
float x3 = x - baryXBedProbePoints[p3];
float x32 = baryXBedProbePoints[p3] - baryXBedProbePoints[p2];
float y3 = y - baryYBedProbePoints[p3];
float x13 = baryXBedProbePoints[p1] - baryXBedProbePoints[p3];
float y13 = baryYBedProbePoints[p1] - baryYBedProbePoints[p3];
float iDet = 1.0 / (y23 * x13 + x32 * y13);
l1 = (y23 * x3 + x32 * y3) * iDet;
l2 = (-y13 * x3 + x13 * y3) * iDet;
l3 = 1.0 - l1 - l2;
}
/*
* Interpolate on a triangular grid. The triangle corners are indexed:
*
* ^ [1] [2]
* |
* Y [4]
* |
* | [0] [3]
* -----X---->
*
*/
float Move::TriangleZ(float x, float y) const
{
for (size_t i = 0; i < 4; i++)
{
size_t j = (i + 1) % 4;
float l1, l2, l3;
BarycentricCoordinates(i, j, 4, x, y, l1, l2, l3);
if (l1 > TRIANGLE_0 && l2 > TRIANGLE_0 && l3 > TRIANGLE_0)
{
return l1 * baryZBedProbePoints[i] + l2 * baryZBedProbePoints[j] + l3 * baryZBedProbePoints[4];
}
}
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "Triangle interpolation: point outside all triangles!\n");
return 0.0;
}
// Calibrate or set the bed equation after probing.
// sParam is the value of the S parameter in the G30 command that provoked this call.
void Move::FinishedBedProbing(int sParam, StringRef& reply)
{
const int numPoints = NumberOfProbePoints();
if (sParam < 0)
{
// A negative sParam just prints the probe heights
reply.copy("Bed probe heights:");
float sum = 0.0;
float sumOfSquares = 0.0;
for (size_t i = 0; (int)i < numPoints; ++i)
{
reply.catf(" %.3f", zBedProbePoints[i]);
sum += zBedProbePoints[i];
sumOfSquares += fsquare(zBedProbePoints[i]);
}
const float mean = sum/numPoints;
reply.catf(", mean %.3f, deviation from mean %.3f\n", mean, sqrt(sumOfSquares/numPoints - fsquare(mean)));
}
else if (numPoints < sParam)
{
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE,
"Bed calibration error: %d factor calibration requested but only %d points provided\n", sParam, numPoints);
}
else
{
if (reprap.Debug(moduleMove))
{
debugPrintf("Z probe offsets:");
float sum = 0.0;
float sumOfSquares = 0.0;
for (size_t i = 0; (int)i < numPoints; ++i)
{
debugPrintf(" %.3f", zBedProbePoints[i]);
sum += zBedProbePoints[i];
sumOfSquares += fsquare(zBedProbePoints[i]);
}
const float mean = sum/numPoints;
debugPrintf(", mean %.3f, deviation from mean %.3f\n", mean, sqrt(sumOfSquares/numPoints - fsquare(mean)));
}
if (sParam == 0)
{
sParam = numPoints;
}
if (IsDeltaMode())
{
DoDeltaCalibration(sParam, reply);
}
else
{
SetProbedBedEquation(sParam, reply);
}
// Clear out the Z heights so that we don't re-use old points.
// This allows us to use different numbers of probe point on different occasions.
for (size_t i = 0; i < MaxProbePoints; ++i)
{
probePointSet[i] &= ~zSet;
}
}
}
void Move::SetProbedBedEquation(size_t numPoints, StringRef& reply)
{
switch(numPoints)
{
case 3:
/*
* Transform to a plane
*/
{
float x10 = xBedProbePoints[1] - xBedProbePoints[0];
float y10 = yBedProbePoints[1] - yBedProbePoints[0];
float z10 = zBedProbePoints[1] - zBedProbePoints[0];
float x20 = xBedProbePoints[2] - xBedProbePoints[0];
float y20 = yBedProbePoints[2] - yBedProbePoints[0];
float z20 = zBedProbePoints[2] - zBedProbePoints[0];
float a = y10 * z20 - z10 * y20;
float b = z10 * x20 - x10 * z20;
float c = x10 * y20 - y10 * x20;
float d = -(xBedProbePoints[1] * a + yBedProbePoints[1] * b + zBedProbePoints[1] * c);
aX = -a / c;
aY = -b / c;
aC = -d / c;
}
break;
case 4:
/*
* Transform to a ruled-surface quadratic. The corner points for interpolation are indexed:
*
* ^ [1] [2]
* |
* Y
* |
* | [0] [3]
* -----X---->
*
* These are the scaling factors to apply to x and y coordinates to get them into the
* unit interval [0, 1].
*/
xRectangle = 1.0 / (xBedProbePoints[3] - xBedProbePoints[0]);
yRectangle = 1.0 / (yBedProbePoints[1] - yBedProbePoints[0]);
break;
case 5:
for (size_t i = 0; i < 4; i++)
{
float x10 = xBedProbePoints[i] - xBedProbePoints[4];
float y10 = yBedProbePoints[i] - yBedProbePoints[4];
float z10 = zBedProbePoints[i] - zBedProbePoints[4];
baryXBedProbePoints[i] = xBedProbePoints[4] + 2.0 * x10;
baryYBedProbePoints[i] = yBedProbePoints[4] + 2.0 * y10;
baryZBedProbePoints[i] = zBedProbePoints[4] + 2.0 * z10;
}
baryXBedProbePoints[4] = xBedProbePoints[4];
baryYBedProbePoints[4] = yBedProbePoints[4];
baryZBedProbePoints[4] = zBedProbePoints[4];
break;
default:
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "Bed calibration error: %d points provided but only 3, 4 and 5 supported\n", numPoints);
return;
}
numBedCompensationPoints = numPoints;
reply.copy("Bed equation fits points");
for (size_t point = 0; point < numPoints; point++)
{
reply.catf(" [%.1f, %.1f, %.3f]", xBedProbePoints[point], yBedProbePoints[point], zBedProbePoints[point]);
}
reply.cat("\n");
}
// Perform 3-, 4-, 6- or 7-factor delta adjustment
void Move::AdjustDeltaParameters(const float v[], size_t numFactors)
{
// Save the old home carriage heights
float homedCarriageHeights[AXES];
for (size_t drive = 0; drive < AXES; ++drive)
{
homedCarriageHeights[drive] = deltaParams.GetHomedCarriageHeight(drive);
}
deltaParams.Adjust(numFactors, v); // adjust the delta parameters
// Adjust the motor endpoints to allow for the change in endstop adjustments
DDA *lastQueuedMove = ddaRingAddPointer->GetPrevious();
const int32_t *endCoordinates = lastQueuedMove->DriveCoordinates();
const float *driveStepsPerUnit = reprap.GetPlatform()->GetDriveStepsPerUnit();
for (size_t drive = 0; drive < AXES; ++drive)
{
const float heightAdjust = deltaParams.GetHomedCarriageHeight(drive) - homedCarriageHeights[drive];
int32_t ep = endCoordinates[drive] + (int32_t)(heightAdjust * driveStepsPerUnit[drive]);
lastQueuedMove->SetDriveCoordinate(ep, drive);
liveEndPoints[drive] = ep;
}
liveCoordinatesValid = false; // force the live XYZ position to be recalculated
}
// Do delta calibration. We adjust the three endstop corrections, and either the delta radius,
// or the X positions of the front two towers, the Y position of the rear tower, and the diagonal rod length.
void Move::DoDeltaCalibration(size_t numFactors, StringRef& reply)
{
const size_t NumDeltaFactors = 7; // number of delta machine factors we can adjust
const size_t numPoints = NumberOfProbePoints();
if (numFactors != 3 && numFactors != 4 && numFactors != 6 && numFactors != 7)
{
reprap.GetPlatform()->Message(BOTH_ERROR_MESSAGE, "Delta calibration error: %d factors requested but only 3, 4, 6 and 7 supported\n", numFactors);
return;
}
if (reprap.Debug(moduleMove))
{
deltaParams.PrintParameters(scratchString);
debugPrintf("%s\n", scratchString.Pointer());
}
// The following is for printing out the calculation time, see later
//uint32_t startTime = reprap.GetPlatform()->GetInterruptClocks();
// Transform the probing points to motor endpoints and store them in a matrix, so that we can do multiple iterations using the same data
FixedMatrix<float, MaxDeltaCalibrationPoints, AXES> probeMotorPositions;
float corrections[MaxDeltaCalibrationPoints];
float initialSumOfSquares = 0.0;
for (size_t i = 0; i < numPoints; ++i)
{
corrections[i] = 0.0;
float machinePos[AXES];
float xp = xBedProbePoints[i], yp = yBedProbePoints[i];
if (probePointSet[i] & xyCorrected)
{
// The point was probed with the sensor at the specified XY coordinates, so subtract the sensor offset to get the head position
const ZProbeParameters& zparams = reprap.GetPlatform()->GetZProbeParameters();
xp -= zparams.xOffset;
yp -= zparams.yOffset;
}
machinePos[X_AXIS] = xp;
machinePos[Y_AXIS] = yp;
machinePos[Z_AXIS] = 0.0;
probeMotorPositions(i, A_AXIS) = deltaParams.Transform(machinePos, A_AXIS);
probeMotorPositions(i, B_AXIS) = deltaParams.Transform(machinePos, B_AXIS);
probeMotorPositions(i, C_AXIS) = deltaParams.Transform(machinePos, C_AXIS);
initialSumOfSquares += fsquare(zBedProbePoints[i]);
}
// Do 1 or more Newton-Raphson iterations
unsigned int iteration = 0;
float expectedRmsError;
for (;;)
{
// Build a Nx7 matrix of derivatives with respect to xa, xb, yc, za, zb, zc, diagonal.
FixedMatrix<float, MaxDeltaCalibrationPoints, NumDeltaFactors> derivativeMatrix;
for (size_t i = 0; i < numPoints; ++i)
{
for (size_t j = 0; j < numFactors; ++j)
{
derivativeMatrix(i, j) =
deltaParams.ComputeDerivative(j, probeMotorPositions(i, A_AXIS), probeMotorPositions(i, B_AXIS), probeMotorPositions(i, C_AXIS));
}
}
if (reprap.Debug(moduleMove))
{
PrintMatrix("Derivative matrix", derivativeMatrix, numPoints, numFactors);
}
// Now build the normal equations for least squares fitting
FixedMatrix<float, NumDeltaFactors, NumDeltaFactors + 1> normalMatrix;
for (size_t i = 0; i < numFactors; ++i)
{
for (size_t j = 0; j < numFactors; ++j)
{
float temp = derivativeMatrix(0, i) * derivativeMatrix(0, j);
for (size_t k = 1; k < numPoints; ++k)
{
temp += derivativeMatrix(k, i) * derivativeMatrix(k, j);
}
normalMatrix(i, j) = temp;
}
float temp = derivativeMatrix(0, i) * -(zBedProbePoints[0] + corrections[0]);
for (size_t k = 1; k < numPoints; ++k)
{
temp += derivativeMatrix(k, i) * -(zBedProbePoints[k] + corrections[k]);
}
normalMatrix(i, numFactors) = temp;
}
if (reprap.Debug(moduleMove))
{
PrintMatrix("Normal matrix", normalMatrix, numFactors, numFactors + 1);
}
float solution[NumDeltaFactors];
normalMatrix.GaussJordan(solution, numFactors);
if (reprap.Debug(moduleMove))
{
PrintMatrix("Solved matrix", normalMatrix, numFactors, numFactors + 1);
PrintVector("Solution", solution, numFactors);
// Calculate and display the residuals
float residuals[MaxDeltaCalibrationPoints];
for (size_t i = 0; i < numPoints; ++i)
{
residuals[i] = zBedProbePoints[i];
for (size_t j = 0; j < numFactors; ++j)
{
residuals[i] += solution[j] * derivativeMatrix(i, j);
}
}
PrintVector("Residuals", residuals, numPoints);
}
AdjustDeltaParameters(solution, numFactors);
// Calculate the expected probe heights using the new parameters