forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Move.cpp
1224 lines (989 loc) · 32.1 KB
/
Move.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************************************
RepRapFirmware - Move
This is all the code to deal with movement and kinematics.
-----------------------------------------------------------------------------------------------------
Version 0.1
18 November 2012
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
Move::Move(Platform* p, GCodes* g)
{
int8_t i;
active = false;
platform = p;
gCodes = g;
// Build the DDA ring
ddaRingAddPointer = new DDA(this, platform, NULL);
dda = ddaRingAddPointer;
for(i = 1; i < DDA_RING_LENGTH; i++)
dda = new DDA(this, platform, dda);
ddaRingAddPointer->next = dda;
dda = NULL;
// Build the lookahead ring
lookAheadRingAddPointer = new LookAhead(this, platform, NULL);
lookAheadRingGetPointer = lookAheadRingAddPointer;
for(i = 1; i < LOOK_AHEAD_RING_LENGTH; i++)
lookAheadRingGetPointer = new LookAhead(this, platform, lookAheadRingGetPointer);
lookAheadRingAddPointer->next = lookAheadRingGetPointer;
// Set the lookahead backwards pointers (some oxymoron, surely?)
lookAheadRingGetPointer = lookAheadRingAddPointer;
for(i = 0; i <= LOOK_AHEAD_RING_LENGTH; i++)
{
lookAheadRingAddPointer = lookAheadRingAddPointer->Next();
lookAheadRingAddPointer->previous = lookAheadRingGetPointer;
lookAheadRingGetPointer = lookAheadRingAddPointer;
}
lookAheadDDA = new DDA(this, platform, NULL);
}
void Move::Init()
{
int8_t i, j;
long ep[DRIVES];
for(i = 0; i < DRIVES; i++)
platform->SetDirection(i, FORWARDS);
// Empty the rings
ddaRingGetPointer = ddaRingAddPointer;
ddaRingLocked = false;
for(i = 0; i <= LOOK_AHEAD_RING_LENGTH; i++)
{
lookAheadRingAddPointer->Release();
lookAheadRingAddPointer = lookAheadRingAddPointer->Next();
}
lookAheadRingGetPointer = lookAheadRingAddPointer;
lookAheadRingCount = 0;
addNoMoreMoves = false;
// Put the origin on the lookahead ring with default velocity in the previous
// position to the first one that will be used.
lastMove = lookAheadRingAddPointer->Previous();
for(i = 0; i < DRIVES; i++)
{
ep[i] = 0;
liveCoordinates[i] = 0.0;
}
lastMove->Init(ep, platform->HomeFeedRate(Z_AXIS), platform->InstantDv(Z_AXIS), false, zMove); // Typically Z is the slowest Axis
lastMove->Release();
liveCoordinates[DRIVES] = platform->HomeFeedRate(Z_AXIS);
SetStepHypotenuse();
currentFeedrate = -1.0;
SetIdentityTransform();
tanXY = 0.0;
tanYZ = 0.0;
tanXZ = 0.0;
lastZHit = 0.0;
zProbing = false;
for(uint8_t point = 0; point < NUMBER_OF_PROBE_POINTS; point++)
{
xBedProbePoints[point] = (0.3 + 0.6*(float)(point%2))*platform->AxisLength(X_AXIS);
yBedProbePoints[point] = (0.0 + 0.9*(float)(point/2))*platform->AxisLength(Y_AXIS);
zBedProbePoints[point] = 0.0;
probePointSet[point] = unset;
}
xRectangle = 1.0/(0.8*platform->AxisLength(X_AXIS));
yRectangle = xRectangle;
secondDegreeCompensation = false;
lastTime = platform->Time();
longWait = lastTime;
active = true;
}
void Move::Exit()
{
platform->Message(HOST_MESSAGE, "Move class exited.\n");
active = false;
}
void Move::Spin()
{
if(!active)
return;
// Do some look-ahead work, if there's any to do
DoLookAhead();
// If there's space in the DDA ring, and there are completed
// moves in the look-ahead ring, transfer them.
if(!DDARingFull())
{
LookAhead* nextFromLookAhead = LookAheadRingGet();
if(nextFromLookAhead != NULL)
{
if(!DDARingAdd(nextFromLookAhead))
platform->Message(HOST_MESSAGE, "Can't add to non-full DDA ring!\n"); // Should never happen...
}
}
// If we either don't want to, or can't, add to the look-ahead ring, go home.
if(addNoMoreMoves || LookAheadRingFull())
{
platform->ClassReport("Move", longWait);
return;
}
// If there's a G Code move available, add it to the look-ahead
// ring for processing.
bool checkEndStopsOnNextMove;
if(gCodes->ReadMove(nextMove, checkEndStopsOnNextMove))
{
Transform(nextMove);
currentFeedrate = nextMove[DRIVES]; // Might be G1 with just an F field
for(int8_t drive = 0; drive < DRIVES; drive++)
nextMachineEndPoints[drive] = LookAhead::EndPointToMachine(drive, nextMove[drive]);
int8_t movementType = GetMovementType(lastMove->MachineEndPoints(), nextMachineEndPoints);
// Throw it away if there's no real movement.
if(movementType == noMove)
{
platform->ClassReport("Move", longWait);
return;
}
// Real move - record its feedrate with it, not here.
currentFeedrate = -1.0;
// Promote minimum feedrates
if(movementType & xyMove)
nextMove[DRIVES] = fmax(nextMove[DRIVES], platform->InstantDv(X_AXIS));
else if(movementType & eMove)
nextMove[DRIVES] = fmax(nextMove[DRIVES], platform->InstantDv((AXES+gCodes->GetSelectedHead())));
else
nextMove[DRIVES] = fmax(nextMove[DRIVES], platform->InstantDv(Z_AXIS));
// Restrict maximum feedrates; assumes xy overrides e overrides z FIXME??
if(movementType & xyMove)
nextMove[DRIVES] = fmin(nextMove[DRIVES], platform->MaxFeedrate(X_AXIS)); // Assumes X and Y are equal. FIXME?
else if(movementType & eMove)
nextMove[DRIVES] = fmin(nextMove[DRIVES], platform->MaxFeedrate(AXES+gCodes->GetSelectedHead())); // Fixed
else // Must be z
nextMove[DRIVES] = fmin(nextMove[DRIVES], platform->MaxFeedrate(Z_AXIS));
if(!LookAheadRingAdd(nextMachineEndPoints, nextMove[DRIVES], 0.0, checkEndStopsOnNextMove, movementType))
platform->Message(HOST_MESSAGE, "Can't add to non-full look ahead ring!\n"); // Should never happen...
}
platform->ClassReport("Move", longWait);
}
// These are the actual numbers we want in the positions, so don't transform them.
void Move::SetPositions(float move[])
{
//Transform(move);
for(uint8_t drive = 0; drive < DRIVES; drive++)
lastMove->SetDriveCoordinateAndZeroEndSpeed(move[drive], drive);
lastMove->SetFeedRate(move[DRIVES]);
}
void Move::Diagnostics()
{
platform->Message(HOST_MESSAGE, "Move Diagnostics:\n");
/* if(active)
platform->Message(HOST_MESSAGE, " active\n");
else
platform->Message(HOST_MESSAGE, " not active\n");
platform->Message(HOST_MESSAGE, " look ahead ring count: ");
snprintf(scratchString, STRING_LENGTH, "%d\n", lookAheadRingCount);
platform->Message(HOST_MESSAGE, scratchString);
if(dda == NULL)
platform->Message(HOST_MESSAGE, " dda: NULL\n");
else
{
if(dda->Active())
platform->Message(HOST_MESSAGE, " dda: active\n");
else
platform->Message(HOST_MESSAGE, " dda: not active\n");
}
if(ddaRingLocked)
platform->Message(HOST_MESSAGE, " dda ring is locked\n");
else
platform->Message(HOST_MESSAGE, " dda ring is not locked\n");
if(addNoMoreMoves)
platform->Message(HOST_MESSAGE, " addNoMoreMoves is true\n\n");
else
platform->Message(HOST_MESSAGE, " addNoMoreMoves is false\n\n");
*/
}
// This returns false if it is not possible
// to use the result as the basis for the
// next move because the look ahead ring
// is full. True otherwise.
bool Move::GetCurrentState(float m[])
{
if(LookAheadRingFull())
return false;
for(int8_t i = 0; i < DRIVES; i++)
{
if(i < AXES)
m[i] = lastMove->MachineToEndPoint(i);
else
m[i] = 0.0; //FIXME This resets extruders to 0.0, even the inactive ones (is this behaviour desired?)
//m[i] = lastMove->MachineToEndPoint(i); //FIXME TEST alternative that does not reset extruders to 0
}
if(currentFeedrate >= 0.0)
m[DRIVES] = currentFeedrate;
else
m[DRIVES] = lastMove->FeedRate();
currentFeedrate = -1.0;
InverseTransform(m);
return true;
}
// Classify a move between two points.
// Is it (a combination of):
// A Z movement?
// An XY movement?
// Extruder movements?
// It treats XY moves and Z moves as mutually exclusive, though all may happen together
// of course. The reason is that they all happen together as a result of the compensation
// for the bed's plane, which means that a move is MAINLY and XY move, or MAINLY a Z move. It
// is the main type of move that is returned.
int8_t Move::GetMovementType(long p0[], long p1[])
{
int8_t result = noMove;
long dxy = 0;
long dz = 0;
long d;
for(int8_t drive = 0; drive < DRIVES; drive++)
{
if(drive < AXES)
{
d = llabs(p1[drive] - p0[drive]);
if(drive == Z_AXIS)
dz = d;
else if(d > dxy)
dxy = d;
} else
{
if( p1[drive] )
result |= eMove;
}
}
dxy *= (long)roundf(platform->DriveStepsPerUnit(Z_AXIS)/platform->DriveStepsPerUnit(X_AXIS));
if(dxy > dz)
result |= xyMove;
else if(dz)
result |= zMove;
return result;
}
void Move::SetStepHypotenuse()
{
// The stepDistances arrays are look-up tables of the Euclidean distance
// between the start and end of a step. If the step is just along one axis,
// it's just that axis's step length. If it's more, it is a Pythagoran
// sum of all the axis steps that take part.
float d, e;
int8_t i, j;
for(i = 0; i < (1<<AXES); i++)
{
d = 0.0;
for(j = 0; j < AXES; j++)
{
if(i & (1<<j))
{
e = 1.0/platform->DriveStepsPerUnit(j);
d += e*e;
}
}
stepDistances[i] = sqrt(d);
}
for(i = 0; i < (1<<(DRIVES-AXES)); i++)
{
d = 0.0;
for(j = 0; j < (DRIVES-AXES); j++)
{
if(i & (1<<j))
{
e = 1.0/platform->DriveStepsPerUnit(AXES + j);
d += e*e;
}
}
extruderStepDistances[i] = sqrt(d);
}
// We don't want 0. If no axes/extruders are moving these should never be used.
// But try to be safe.
stepDistances[0] = 1.0/platform->DriveStepsPerUnit(AXES); //FIXME this is not multi extruder safe (but we should never get here)
extruderStepDistances[0] = stepDistances[0];
}
// Take an item from the look-ahead ring and add it to the DDA ring, if
// possible.
bool Move::DDARingAdd(LookAhead* lookAhead)
{
if(GetDDARingLock())
{
if(DDARingFull())
{
ReleaseDDARingLock();
return false;
}
if(ddaRingAddPointer->Active()) // Should never happen...
{
platform->Message(HOST_MESSAGE, "Attempt to alter an active ring buffer entry!\n");
ReleaseDDARingLock();
return false;
}
// We don't care about Init()'s return value - that should all have been sorted
// out by LookAhead.
float u, v;
ddaRingAddPointer->Init(lookAhead, u, v);
ddaRingAddPointer = ddaRingAddPointer->Next();
ReleaseDDARingLock();
return true;
}
return false;
}
// Get a movement from the DDA ring, if we can.
DDA* Move::DDARingGet()
{
DDA* result = NULL;
if(GetDDARingLock())
{
if(DDARingEmpty())
{
ReleaseDDARingLock();
return NULL;
}
result = ddaRingGetPointer;
ddaRingGetPointer = ddaRingGetPointer->Next();
ReleaseDDARingLock();
return result;
}
return NULL;
}
// Do the look-ahead calculations
void Move::DoLookAhead()
{
if(LookAheadRingEmpty())
return;
LookAhead* n0;
LookAhead* n1;
LookAhead* n2;
float u, v;
// If there are a reasonable number of moves in there (LOOK_AHEAD), or if we are
// doing single moves with no other move immediately following on, run up and down
// the moves using the DDA Init() function to reduce the start or the end speed
// or both to the maximum that can be achieved because of the requirements of
// the adjacent moves.
if(addNoMoreMoves || !gCodes->HaveIncomingData() || lookAheadRingCount > LOOK_AHEAD)
{
// Run up the moves
n1 = lookAheadRingGetPointer;
n0 = n1->Previous();
n2 = n1->Next();
while(n2 != lookAheadRingAddPointer)
{
if(!(n1->Processed() & complete))
{
if(n1->Processed() & vCosineSet)
{
u = n0->V();
v = n1->V();
if(lookAheadDDA->Init(n1, u, v) & change)
{
n0->SetV(u);
n1->SetV(v);
}
}
}
n0 = n1;
n1 = n2;
n2 = n2->Next();
}
// Now run down
do
{
if(!(n1->Processed() & complete))
{
if(n1->Processed() & vCosineSet)
{
u = n0->V();
v = n1->V();
if(lookAheadDDA->Init(n1, u, v) & change)
{
n0->SetV(u);
n1->SetV(v);
}
n1->SetProcessed(complete);
}
}
n2 = n1;
n1 = n0;
n0 = n0->Previous();
}while(n0 != lookAheadRingGetPointer);
n0->SetProcessed(complete);
}
// If there are any new unprocessed moves in there, set their end speeds
// according to the cosine of the angle between them.
if(addNoMoreMoves || !gCodes->HaveIncomingData() || lookAheadRingCount > 1)
{
n1 = lookAheadRingGetPointer;
n0 = n1->Previous();
n2 = n1->Next();
while(n2 != lookAheadRingAddPointer)
{
if(n1->Processed() == unprocessed)
{
float c = fmin(n1->FeedRate(), n2->FeedRate());
c = c*n1->Cosine();
if(c < platform->InstantDv(Z_AXIS)) // Z is typically the slowest.
{
int8_t mt = n1->GetMovementType();
// Assumes xy overrides z overrides e
if(mt & xyMove)
c = platform->InstantDv(X_AXIS);
else if (mt & zMove)
c = platform->InstantDv(Z_AXIS);
else
c = platform->InstantDv((AXES+gCodes->GetSelectedHead())); // value for current extruder
}
n1->SetV(c);
n1->SetProcessed(vCosineSet);
}
n0 = n1;
n1 = n2;
n2 = n2->Next();
}
// If we are just doing one isolated move, set its end velocity to InstantDv(Z_AXIS).
if(addNoMoreMoves || !gCodes->HaveIncomingData())
{
n1->SetV(platform->InstantDv(Z_AXIS));
n1->SetProcessed(complete);
}
}
}
// This is the function that's called by the timer interrupt to step the motors.
void Move::Interrupt()
{
// Have we got a live DDA?
if(dda == NULL)
{
// No - see if a new one is available.
dda = DDARingGet();
if(dda != NULL)
dda->Start(true); // Yes - got it. So fire it up.
return;
}
// We have a DDA. Has it finished?
if(dda->Active())
{
// No - it's still live. Step it and return.
dda->Step();
return;
}
// Yes - it's finished. Throw it away so the code above will then find a new one.
dda = NULL;
}
// creates a new lookahead object adds it to the lookahead ring, returns false if its full
bool Move::LookAheadRingAdd(long ep[], float feedRate, float vv, bool ce, int8_t mt)
{
if(LookAheadRingFull())
return false;
if(!(lookAheadRingAddPointer->Processed() & released))
platform->Message(HOST_MESSAGE, "Attempt to alter a non-released lookahead ring entry!\n"); // Should never happen...
lookAheadRingAddPointer->Init(ep, feedRate, vv, ce, mt);
lastMove = lookAheadRingAddPointer;
lookAheadRingAddPointer = lookAheadRingAddPointer->Next();
lookAheadRingCount++;
return true;
}
LookAhead* Move::LookAheadRingGet()
{
LookAhead* result;
if(LookAheadRingEmpty())
return NULL;
result = lookAheadRingGetPointer;
if(!(result->Processed() & complete))
return NULL;
lookAheadRingGetPointer = lookAheadRingGetPointer->Next();
lookAheadRingCount--;
return result;
}
// Note that we don't set the tan values to 0 here. This means that the bed probe
// values will be a fraction of a millimeter out in X and Y, which, as the bed should
// be nearly flat (and the probe doesn't coincide with the nozzle anyway), won't matter.
// But it means that the tan values can be set for the machine
// at the start in the configuration file and be retained, without having to know and reset
// them after every Z probe of the bed.
void Move::SetIdentityTransform()
{
aX = 0.0;
aY = 0.0;
aC = 0.0;
secondDegreeCompensation = false;
}
void Move::Transform(float xyzPoint[])
{
xyzPoint[X_AXIS] = xyzPoint[X_AXIS] + tanXY*xyzPoint[Y_AXIS] + tanXZ*xyzPoint[Z_AXIS];
xyzPoint[Y_AXIS] = xyzPoint[Y_AXIS] + tanYZ*xyzPoint[Z_AXIS];
if(secondDegreeCompensation)
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] + SecondDegreeTransformZ(xyzPoint[X_AXIS], xyzPoint[Y_AXIS]);
else
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] + aX*xyzPoint[X_AXIS] + aY*xyzPoint[Y_AXIS] + aC;
}
void Move::InverseTransform(float xyzPoint[])
{
if(secondDegreeCompensation)
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] - SecondDegreeTransformZ(xyzPoint[X_AXIS], xyzPoint[Y_AXIS]);
else
xyzPoint[Z_AXIS] = xyzPoint[Z_AXIS] - (aX*xyzPoint[X_AXIS] + aY*xyzPoint[Y_AXIS] + aC);
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::SetAxisCompensation(int8_t axis, float tangent)
{
float currentPositions[DRIVES+1];
if(!GetCurrentState(currentPositions))
{
platform->Message(HOST_MESSAGE, "Setting bed equation - can't get position!");
return;
}
switch(axis)
{
case X_AXIS:
tanXY = tangent;
break;
case Y_AXIS:
tanYZ = tangent;
break;
case Z_AXIS:
tanXZ = tangent;
break;
default:
platform->Message(HOST_MESSAGE, "SetAxisCompensation: dud axis.\n");
}
Transform(currentPositions);
SetPositions(currentPositions);
}
void Move::SetProbedBedEquation()
{
float currentPositions[DRIVES+1];
if(!GetCurrentState(currentPositions))
{
platform->Message(HOST_MESSAGE, "Setting bed equation - can't get position!");
return;
}
if(NumberOfProbePoints() >= 3)
{
secondDegreeCompensation = (NumberOfProbePoints() == 4);
if(secondDegreeCompensation)
{
/*
* 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]);
Transform(currentPositions);
SetPositions(currentPositions);
return;
}
} else
{
platform->Message(HOST_MESSAGE, "Attempt to set bed compensation before all probe points have been recorded.");
return;
}
float xkj, ykj, zkj;
float xlj, ylj, zlj;
float a, b, c, d; // Implicit plane equation - what we need to do a proper job
xkj = xBedProbePoints[1] - xBedProbePoints[0];
ykj = yBedProbePoints[1] - yBedProbePoints[0];
zkj = zBedProbePoints[1] - zBedProbePoints[0];
xlj = xBedProbePoints[2] - xBedProbePoints[0];
ylj = yBedProbePoints[2] - yBedProbePoints[0];
zlj = zBedProbePoints[2] - zBedProbePoints[0];
a = ykj*zlj - zkj*ylj;
b = zkj*xlj - xkj*zlj;
c = xkj*ylj - ykj*xlj;
d = -(xBedProbePoints[1]*a + yBedProbePoints[1]*b + zBedProbePoints[1]*c);
aX = -a/c;
aY = -b/c;
aC = -d/c;
Transform(currentPositions);
SetPositions(currentPositions);
}
// FIXME
// This function is never normally called. It is a test to time
// the interrupt function. To activate it, uncomment the line that calls
// this in Platform.ino.
void Move::InterruptTime()
{
/* char buffer[50];
float a[] = {1.0, 2.0, 3.0, 4.0, 5.0};
float b[] = {2.0, 3.0, 4.0, 5.0, 6.0};
float u = 50;
float v = 50;
lookAheadDDA->Init(a, b, u, v);
lookAheadDDA->Start(false);
float t = platform->Time();
for(long i = 0; i < 100000; i++)
lookAheadDDA->Step(false);
t = platform->Time() - t;
platform->Message(HOST_MESSAGE, "Time for 100000 calls of the interrupt function: ");
snprintf(buffer, 50, "%ld", t);
platform->Message(HOST_MESSAGE, buffer);
platform->Message(HOST_MESSAGE, " microseconds.\n");*/
}
//****************************************************************************************************
DDA::DDA(Move* m, Platform* p, DDA* n)
{
active = false;
move = m;
platform = p;
next = n;
}
/*
DDA::Init(...)
Sets up the DDA to take us between two positions and extrude states.
The start velocity is u, and the end one is v. The requested maximum feedrate
is in myLookAheadEntry->FeedRate().
Almost everything that needs to be done to set this up is also useful
for GCode look-ahead, so this one function is used for both. It flags when
u and v cannot be satisfied with the distance available and reduces them
proportionately to give values that can just be achieved, which is why they
are passed by reference.
The return value is indicates if the move is a trapezium or triangle, and if
the u and u values need to be changed.
Every drive has an acceleration associated with it, so when more than one drive is
moving there have to be rules of precedence that say which acceleration (and which
instantDv value) to use.
The rules are these:
if X and/or Y are moving
Use X acceleration
else if Z is moving
Use Z acceleration
else
Use the acceleration for the extruder that's moving.
In the case of multiple extruders moving at once, their minimum acceleration (and its
associated instantDv) are used. The variables axesMoving and extrudersMoving track what's
going on. The bits in the int8_t axesMoving are ORed:
msb -> 00000ZYX <- lsb
a 1 meaning that that axis is moving. The bits of extrudersMoving contain a similar
pattern for the moving extruders.
Note that all this assumes that X and Y accelerations are equal, though in fact there is a
value stored for each.
In the case of only extruders moving, the distance moved is taken to be the Pythagoran distance in
the configuration space of the extruders.
TODO: Worry about having more than eight extruders; X and Y behaving radically differently...
*/
MovementProfile DDA::AccelerationCalculation(float& u, float& v, MovementProfile result)
{
// At which DDA step should we stop accelerating? myLookAheadEntry->FeedRate() gives
// the desired feedrate.
float d = 0.5*(myLookAheadEntry->FeedRate()*myLookAheadEntry->FeedRate() - u*u)/acceleration; // d = (v1^2 - v0^2)/2a
stopAStep = (long)roundf((d*totalSteps)/distance);
// At which DDA step should we start decelerating?
d = 0.5*(v*v - myLookAheadEntry->FeedRate()*myLookAheadEntry->FeedRate())/acceleration; // This should be 0 or negative...
startDStep = totalSteps + (long)roundf((d*totalSteps)/distance);
// If acceleration stop is at or after deceleration start, then the distance moved
// is not enough to get to full speed.
if(stopAStep >= startDStep)
{
result = noFlat;
// Work out the point at which to stop accelerating and then
// immediately start decelerating.
float dCross = 0.5*(0.5*(v*v - u*u)/acceleration + distance);
if(dCross < 0.0 || dCross > distance)
{
// With the acceleration available, it is not possible
// to satisfy u and v within the distance; reduce u and v
// proportionately to get ones that work and flag the fact.
// The result is two velocities that can just be accelerated
// or decelerated between over the distance to get
// from one to the other.
result = change;
float k = v/u;
u = 2.0*acceleration*distance/(k*k - 1);
if(u >= 0.0)
{
u = sqrt(u);
v = k*u;
} else
{
v = sqrt(-u);
u = v/k;
}
dCross = 0.5*(0.5*(v*v - u*u)/acceleration + distance);
}
// The DDA steps at which acceleration stops and deceleration starts
stopAStep = (long)((dCross*totalSteps)/distance);
startDStep = stopAStep + 1;
}
return result;
}
void DDA::SetXYAcceleration() // Slight hack - assumes dY = dX
{
acceleration = platform->Acceleration(X_AXIS);
instantDv = platform->InstantDv(X_AXIS);
timeStep = 1.0/platform->DriveStepsPerUnit(X_AXIS);
}
void DDA::SetEAcceleration(float eDistance)
{
acceleration = FLT_MAX; // Slight hack
distance = eDistance;
for(int8_t drive = AXES; drive < DRIVES; drive++)
{
if(delta[drive])
{
if(platform->Acceleration(drive) < acceleration)
{
acceleration = platform->Acceleration(drive);
instantDv = platform->InstantDv(drive);
timeStep = 1.0/platform->DriveStepsPerUnit(drive);
}
}
}
}
MovementProfile DDA::Init(LookAhead* lookAhead, float& u, float& v)
{
int8_t drive;
active = false;
myLookAheadEntry = lookAhead;
MovementProfile result = moving;
totalSteps = -1;
distance = 0.0; // X+Y+Z
float eDistance = 0.0;
float d;
long* targetPosition = myLookAheadEntry->MachineEndPoints();
v = myLookAheadEntry->V();
long* positionNow = myLookAheadEntry->Previous()->MachineEndPoints();
u = myLookAheadEntry->Previous()->V();
checkEndStops = myLookAheadEntry->CheckEndStops();
// How far are we going, both in steps and in mm?
for(drive = 0; drive < DRIVES; drive++)
{
if(drive < AXES) // XY, Z
{
delta[drive] = targetPosition[drive] - positionNow[drive]; //Absolute
d = myLookAheadEntry->MachineToEndPoint(drive, delta[drive]);
distance += d*d;
} else
{ // E
delta[drive] = targetPosition[drive]; // Relative
d = myLookAheadEntry->MachineToEndPoint(drive, delta[drive]);
eDistance += d*d;
}
if(delta[drive] >= 0)
directions[drive] = FORWARDS;
else
directions[drive] = BACKWARDS;
delta[drive] = abs(delta[drive]);
// Keep track of the biggest drive move in totalSteps
if(delta[drive] > totalSteps)
totalSteps = delta[drive];
}
// Not going anywhere? Should have been chucked away before we got here.
if(totalSteps <= 0)
{
if(reprap.Debug())
platform->Message(HOST_MESSAGE, "DDA.Init(): Null movement.\n");
myLookAheadEntry->Release();
return result;
}
// Set up the DDA
counter[0] = -totalSteps/2;
for(drive = 1; drive < DRIVES; drive++)
counter[drive] = counter[0];
// Acceleration and velocity calculations
distance = sqrt(distance);
eDistance = sqrt(eDistance);
// Decide the appropriate acceleration and instantDv values
// timeStep is set here to the distance of the
// corresponding axis step. It will be divided
// by a velocity later.
int8_t mt = myLookAheadEntry->GetMovementType();
if(mt & xyMove) // X or Y involved?
{
// If XY (or Z) are moving, then the extruder won't be considered in the
// acceleration calculation. Usually this is OK. But check that we are not asking
// the extruder to accelerate, decelerate, or move too fast. The common place
// for this to happen is when it is moving back from a previous retraction during
// an XY move.
if(mt & eMove)
{
if(eDistance > distance)
SetEAcceleration(eDistance);
else
SetXYAcceleration();
} else
SetXYAcceleration();
} else if (mt & zMove) // Z involved?
{
acceleration = platform->Acceleration(Z_AXIS);
instantDv = platform->InstantDv(Z_AXIS);
timeStep = 1.0/platform->DriveStepsPerUnit(Z_AXIS);
} else // Must be extruders only
SetEAcceleration(eDistance);
// If we are going from an XY move or extruder move to a Z move, u needs to be platform->InstantDv(Z_AXIS).
if((myLookAheadEntry->Previous()->GetMovementType() & (xyMove | eMove)) && (mt & zMove))
{
u = platform->InstantDv(Z_AXIS);
result = change;
}
// if we are going from a Z move to an XY move or E move, v needs to be platform->InstantDv(Z_AXIS),
// as does instantDv.