forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Move.cpp
1750 lines (1432 loc) · 43.8 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"
const float zeroExtruderPositions[DRIVES - AXES] = ZERO_EXTRUDER_POSITIONS;
Move::Move(Platform* p, GCodes* g)
{
active = false;
platform = p;
gCodes = g;
// Build the DDA ring
ddaRingAddPointer = new DDA(this, platform, NULL);
dda = ddaRingAddPointer;
for(uint8_t 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(size_t 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(size_t i = 0; i <= LOOK_AHEAD_RING_LENGTH; i++)
{
lookAheadRingAddPointer = lookAheadRingAddPointer->Next();
lookAheadRingAddPointer->previous = lookAheadRingGetPointer;
lookAheadRingGetPointer = lookAheadRingAddPointer;
}
lookAheadDDA = new DDA(this, platform, NULL);
// We need an isolated DDA entry to perform moves in case the look-ahead queue is paused
isolatedMove = new LookAhead(this, platform, NULL);
isolatedMove->previous = NULL;
ddaIsolatedMove = new DDA(this, platform, NULL);
}
void Move::Init()
{
long ep[DRIVES];
for(size_t drive = 0; drive < DRIVES; drive++)
{
platform->SetDirection(drive, FORWARDS);
}
// Empty the rings
ddaRingGetPointer = ddaRingAddPointer;
ddaRingLocked = false;
for(uint8_t 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.
lastRingMove = lookAheadRingAddPointer->Previous();
for(size_t drive = 0; drive < DRIVES; drive++)
{
ep[drive] = 0;
liveCoordinates[drive] = 0.0;
}
for(size_t extruder = 0; extruder < DRIVES - AXES; extruder++)
{
rawExtruderPos[extruder] = 0.0;
}
int8_t slow = platform->SlowestDrive();
lastRingMove->Init(ep, platform->HomeFeedRate(slow), platform->InstantDv(slow), platform->MaxFeedrate(slow), platform->Acceleration(slow), 0, zeroExtruderPositions);
lastRingMove->Release();
isolatedMove->Init(ep, platform->HomeFeedRate(slow), platform->InstantDv(slow), platform->MaxFeedrate(slow), platform->Acceleration(slow), 0, zeroExtruderPositions);
isolatedMove->Release();
readIsolatedMove = isolatedMoveAvailable = false;
currentFeedrate = liveCoordinates[DRIVES] = platform->HomeFeedRate(slow);
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->AxisMaximum(X_AXIS);
yBedProbePoints[point] = (0.0 + 0.9*(float)(point/2))*platform->AxisMaximum(Y_AXIS);
zBedProbePoints[point] = 0.0;
probePointSet[point] = unset;
}
xRectangle = 1.0/(0.8*platform->AxisMaximum(X_AXIS));
yRectangle = xRectangle;
longWait = platform->Time();
for(uint8_t extruder = 0; extruder < DRIVES - AXES; extruder++)
{
extrusionFactors[extruder] = 1.0;
}
speedFactor = 1.0;
doingSplitMove = false;
isResuming = false;
state = running;
active = true;
}
void Move::Exit()
{
platform->Message(BOTH_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(BOTH_ERROR_MESSAGE, "Can't add to non-full DDA ring!\n"); // Should never happen...
}
}
}
// If we're paused and there is no live movement, see if we can perform an isolated move.
if (IsPaused() && isolatedMoveAvailable)
{
if (GetDDARingLock())
{
readIsolatedMove = true;
isolatedMoveAvailable = false;
ReleaseDDARingLock();
}
platform->ClassReport(longWait);
return;
}
// If we're done purging all pending moves, see if we can reset our properties again.
if (IsCancelled())
{
if (LookAheadRingEmpty() && DDARingEmpty())
{
// Make sure the last look-ahead entry points to the same coordinates we're at right now
float currentCoordinates[DRIVES + 1];
for(uint8_t axis=0; axis<AXES; axis++)
{
currentCoordinates[axis] = liveCoordinates[axis];
}
currentCoordinates[DRIVES] = currentFeedrate;
SetPositions(currentCoordinates);
// We've skipped all incoming moves, so reset our state again
lookAheadRingAddPointer->Release();
doingSplitMove = false;
state = running;
}
platform->ClassReport(longWait);
return;
}
// If we either don't want to, or can't, add to the look-ahead ring, go home.
const bool splitNextMove = IsRunning() && doingSplitMove;
if ((!splitNextMove && addNoMoreMoves) || LookAheadRingFull() || isolatedMoveAvailable)
{
platform->ClassReport(longWait);
return;
}
// We don't need to obtain any move if we're still busy processing one.
EndstopChecks endStopsToCheck = 0;
if (splitNextMove)
{
for(size_t drive=0; drive<DRIVES; drive++)
{
nextMove[drive] = splitMove[drive];
}
}
// Read a new move and apply extrusion factors right away.
else if (gCodes->ReadMove(nextMove, endStopsToCheck))
{
for(size_t drive = AXES; drive < DRIVES; drive++)
{
rawEDistances[drive - AXES] = nextMove[drive];
nextMove[drive] *= extrusionFactors[drive - AXES];
}
currentFeedrate = nextMove[DRIVES]; // Might be G1 with just an F field
}
// We cannot process any moves, so stop here.
else
{
platform->ClassReport(longWait);
return;
}
// If there's a new move available, split it up and add it to the look-ahead ring for processing.
if (endStopsToCheck == 0)
{
doingSplitMove = SplitNextMove(); // TODO: Make this work with more than one inner probe point
}
Transform(nextMove);
const LookAhead *lastMove = (IsPaused()) ? isolatedMove : lastRingMove;
bool noMove = true;
for(size_t drive = 0; drive < DRIVES; drive++)
{
nextMachineEndPoints[drive] = LookAhead::EndPointToMachine(drive, nextMove[drive]);
if (drive < AXES)
{
if (nextMachineEndPoints[drive] - lastMove->MachineCoordinates()[drive] != 0)
{
platform->EnableDrive(drive);
noMove = false;
}
normalisedDirectionVector[drive] = nextMove[drive] - lastMove->MachineToEndPoint(drive);
}
else
{
if (nextMachineEndPoints[drive] != 0)
{
platform->EnableDrive(drive);
noMove = false;
}
normalisedDirectionVector[drive] = nextMove[drive];
}
}
// Throw it away if there's no real movement.
if (noMove)
{
platform->ClassReport(longWait);
return;
}
// Compute the direction of motion, moved to the positive hyperquadrant
Absolute(normalisedDirectionVector, DRIVES);
if (Normalise(normalisedDirectionVector, DRIVES) <= 0.0)
{
platform->Message(BOTH_ERROR_MESSAGE, "Attempt to normalise zero-length move.\n"); // Should never get here - noMove above
platform->ClassReport(longWait);
return;
}
// Set the feedrate maximum and minimum, and the acceleration
float minSpeed = VectorBoxIntersection(normalisedDirectionVector, platform->InstantDvs(), DRIVES);
float acceleration = VectorBoxIntersection(normalisedDirectionVector, platform->Accelerations(), DRIVES);
float maxSpeed = VectorBoxIntersection(normalisedDirectionVector, platform->MaxFeedrates(), DRIVES);
if (IsPaused())
{
// Do not pass raw extruder distances here, because they would mess around with print time estimation
if (!SetUpIsolatedMove(nextMachineEndPoints, currentFeedrate, minSpeed, maxSpeed, acceleration, endStopsToCheck))
{
platform->Message(BOTH_ERROR_MESSAGE, "Couldn't set up isolated move!\n");
}
}
else
{
const float feedRate = (endStopsToCheck == 0) ? currentFeedrate * speedFactor : currentFeedrate;
const float *unmodifiedEDistances = (doingSplitMove) ? zeroExtruderPositions : rawEDistances;
if (LookAheadRingAdd(nextMachineEndPoints, feedRate, minSpeed, maxSpeed, acceleration, endStopsToCheck, unmodifiedEDistances))
{
// Tell GCodes class we're about to perform a new (regular) move
reprap.GetGCodes()->MoveQueued();
}
else
{
platform->Message(BOTH_ERROR_MESSAGE, "Can't add to non-full look ahead ring!\n"); // Should never happen...
}
}
platform->ClassReport(longWait);
}
/* Check if we need to split up the next move to make 5-point bed compensation work well.
* Do this by verifying whether we cross either X or Y of the fifth bed compensation point.
*
* Returns true if the next move has been split up
*/
bool Move::SplitNextMove()
{
if (!IsRunning() || doingSplitMove || identityBedTransform || NumberOfProbePoints() != 5)
return false;
// Get the last untransformed XYZ coordinates
float lastXYZ[AXES];
for(uint8_t axis=0; axis<AXES; axis++)
{
lastXYZ[axis] = lastRingMove->MachineToEndPoint(axis);
}
InverseTransform(lastXYZ);
// Are we crossing X coordinate of 5th bed compensation point?
const float x1 = lastXYZ[X_AXIS];
const float x2 = nextMove[X_AXIS];
const float xCenter = xBedProbePoints[4];
bool crossingX = false;
float scaleX;
if ((fabs(x2 - x1) > MINIMUM_SPLIT_DISTANCE) && ((x1 < xCenter && x2 > xCenter) || (x2 < xCenter && x1 > xCenter)))
{
crossingX = true;
scaleX = (xCenter - x1) / (x2 - x1);
}
// Are we crossing Y coordinate of 5th bed compensation point?
const float y1 = lastXYZ[Y_AXIS];
const float y2 = nextMove[Y_AXIS];
const float yCenter = yBedProbePoints[4];
bool crossingY = false;
float scaleY;
if ((fabs(y2 - y1) > MINIMUM_SPLIT_DISTANCE) && ((y1 < yCenter && y2 > yCenter) || (y2 < yCenter && y1 > yCenter)))
{
crossingY = true;
scaleY = (yCenter - y1) / (y2 - y1);
}
// Split components of the next move proportionally into two move endpoints
if (crossingX || crossingY)
{
float splitFactor;
if (crossingX && crossingY)
{
splitFactor = 0.5 * (scaleX + scaleY);
}
else
{
splitFactor = (crossingX) ? scaleX : scaleY;
}
for(uint8_t drive=0; drive<DRIVES; drive++)
{
if (drive < AXES)
{
splitMove[drive] = nextMove[drive];
nextMove[drive] = lastXYZ[drive] + (nextMove[drive] - lastXYZ[drive]) * splitFactor;
}
else
{
splitMove[drive] = nextMove[drive] * (1.0 - splitFactor);
nextMove[drive] *= splitFactor;
}
}
return true;
}
return false;
}
/*
* Take a unit positive-hyperquadrant vector, and return the factor needed to obtain
* length of the vector as projected to touch box[].
*/
float Move::VectorBoxIntersection(const float v[], const float box[], int8_t dimensions)
{
// Generate a vector length that is guaranteed to exceed the size of the box
float biggerThanBoxDiagonal = 2.0*Magnitude(box, dimensions);
float magnitude = biggerThanBoxDiagonal;
for(int8_t d = 0; d < dimensions; d++)
{
if(biggerThanBoxDiagonal*v[d] > box[d])
{
float a = box[d]/v[d];
if(a < magnitude)
{
magnitude = a;
}
}
}
return magnitude;
}
// Normalise a vector, and also return its previous magnitude
// If the vector is of 0 length, return a negative magnitude
float Move::Normalise(float v[], int8_t dimensions)
{
float magnitude = Magnitude(v, dimensions);
if(magnitude <= 0.0)
return -1.0;
Scale(v, 1.0/magnitude, dimensions);
return magnitude;
}
// Return the magnitude of a vector
float Move::Magnitude(const float v[], int8_t dimensions)
{
float magnitude = 0.0;
for(int8_t d = 0; d < dimensions; d++)
{
magnitude += v[d]*v[d];
}
magnitude = sqrt(magnitude);
return magnitude;
}
// Multiply a vector by a scalar
void Move::Scale(float v[], float scale, int8_t dimensions)
{
for(int8_t d = 0; d < dimensions; d++)
{
v[d] = scale*v[d];
}
}
// Move a vector into the positive hyperquadrant
void Move::Absolute(float v[], int8_t dimensions)
{
for(int8_t d = 0; d < dimensions; d++)
{
v[d] = fabs(v[d]);
}
}
// These are the actual numbers we want in the positions, so don't transform them.
void Move::SetPositions(float move[])
{
LookAhead *lastMove = (IsPaused()) ? isolatedMove : lastRingMove;
for(uint8_t drive = 0; drive < DRIVES; drive++)
{
lastMove->SetDriveCoordinate(move[drive], drive);
}
currentFeedrate = move[DRIVES];
lastMove->SetFeedRate(currentFeedrate);
}
void Move::Diagnostics()
{
platform->AppendMessage(BOTH_MESSAGE, "Move Diagnostics:\n");
platform->AppendMessage(BOTH_MESSAGE, "State: ");
switch (state)
{
case running:
platform->AppendMessage(BOTH_MESSAGE, "running\n");
break;
case pausing:
platform->AppendMessage(BOTH_MESSAGE, "pausing\n");
break;
case paused:
platform->AppendMessage(BOTH_MESSAGE, "paused\n");
break;
case cancelled:
platform->AppendMessage(BOTH_MESSAGE, "cancelled\n");
break;
default:
platform->AppendMessage(BOTH_MESSAGE, "unknown\n");
break;
}
/* 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");
*/
}
// Return the untransformed machine coordinates
// 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::GetCurrentMachinePosition(float m[]) const
{
// If moves are still running, use the last look-ahead entry to retrieve the current position
if (IsRunning())
{
if(LookAheadRingFull() || doingSplitMove)
return false;
for(size_t drive = 0; drive < DRIVES; drive++)
{
m[drive] = lastRingMove->MachineToEndPoint(drive);
}
m[DRIVES] = currentFeedrate;
return true;
}
// If there is no real movement, return liveCoordinates instead
else if (NoLiveMovement())
{
for(size_t drive = 0; drive <= DRIVES; drive++)
{
m[drive] = liveCoordinates[drive];
}
return true;
}
return false;
}
// Return the transformed machine coordinates
bool Move::GetCurrentUserPosition(float m[]) const
{
if(!GetCurrentMachinePosition(m))
return false;
InverseTransform(m);
return true;
}
// 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(BOTH_ERROR_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 or from an isolated move, if we can.
DDA* Move::DDARingGet()
{
DDA* result = NULL;
if(GetDDARingLock())
{
// If we're paused and have a valid DDA, perform an isolated move
if (IsPaused())
{
if (readIsolatedMove)
{
result = ddaIsolatedMove;
readIsolatedMove = false;
}
ReleaseDDARingLock();
return result;
}
// If we've finished the last move while pausing or ran out of moves, stop here
if (IsPausing() || DDARingEmpty())
{
ReleaseDDARingLock();
return NULL;
}
// Get an ordinary entry from the DDA ring
result = ddaRingGetPointer;
ddaRingGetPointer = ddaRingGetPointer->Next();
ReleaseDDARingLock();
return result;
}
return NULL;
}
// Do the look-ahead calculations
void Move::DoLookAhead()
{
if ((!IsRunning() && !IsCancelled()) || LookAheadRingEmpty())
{
return;
}
LookAhead* n0;
LookAhead* n1;
LookAhead* n2;
// 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();
while (n1 != lookAheadRingAddPointer)
{
if(!(n0->Processed() & complete))
{
if(n0->Processed() & vCosineSet)
{
float u = n0->V();
float v = n1->V();
if(lookAheadDDA->Init(n1, u, v) & change)
{
n0->SetV(u);
n1->SetV(v);
}
}
}
n0 = n1;
n1 = n1->Next();
}
// Now run down
do
{
if(!(n1->Processed() & complete))
{
if(n1->Processed() & vCosineSet)
{
float u = n0->V();
float v = n1->V();
if(lookAheadDDA->Init(n1, u, v) & change)
{
n0->SetV(u);
n1->SetV(v);
}
n1->SetProcessed(complete);
}
}
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 = n1->V();
float m = min<float>(n1->MinSpeed(), n2->MinSpeed()); // FIXME we use min as one move's max may not be able to cope with the min for the other. But should this be max?
c = c*n1->Cosine();
if(c < m)
{
c = m;
}
n1->SetV(c);
n1->SetProcessed(vCosineSet);
}
n0 = n1;
n1 = n2;
n2 = n2->Next();
}
// If we have no more moves to process, set the last move's end velocity to an appropriate minimum speed.
if(!doingSplitMove && (addNoMoreMoves || !gCodes->HaveIncomingData()))
{
n1->SetV(platform->InstantDv(platform->SlowestDrive())); // The next thing may be the slowest; be prepared.
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)
{
if (IsCancelled())
{
dda->Release(); // Yes - but don't use it. All pending moves have been cancelled.
dda = NULL;
}
else
{
dda->Start(); // Yes - got it. So fire it up if the print is still running.
dda->Step(); // And take the first step.
}
}
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->Release();
dda = NULL;
}
// Records a new lookahead object and adds it to the lookahead ring, returns false if it's full
bool Move::LookAheadRingAdd(long ep[], float requestedFeedRate, float minSpeed, float maxSpeed,
float acceleration, EndstopChecks ce, const float extrDiffs[])
{
if(LookAheadRingFull())
{
return false;
}
if(!(lookAheadRingAddPointer->Processed() & released)) // Should never happen...
{
platform->Message(BOTH_ERROR_MESSAGE, "Attempt to alter a non-released lookahead ring entry!\n");
return false;
}
lookAheadRingAddPointer->Init(ep, requestedFeedRate, minSpeed, maxSpeed, acceleration, ce, extrDiffs);
lastRingMove = 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;
}
// Sets up a single lookahead entry to perform an isolated move ( start velocity = end velocity = instantDv )
bool Move::SetUpIsolatedMove(long ep[], float requestedFeedRate, float minSpeed, float maxSpeed, float acceleration, EndstopChecks ce)
{
if (isolatedMoveAvailable)
{
return false;
}
if(!(isolatedMove->Processed() & released)) // Should never happen...
{
platform->Message(BOTH_ERROR_MESSAGE, "Attempt to alter a non-released isolated lookahead entry!\n");
return false;
}
isolatedMove->Init(ep, requestedFeedRate, minSpeed, maxSpeed, acceleration, ce, zeroExtruderPositions);
// Perform acceleration calculation
const float instantDv = platform->InstantDv(platform->SlowestDrive());
float u = instantDv, v = instantDv;
ddaIsolatedMove->Init(isolatedMove, u, v);
isolatedMoveAvailable = true;
// reprap.GetPlatform()->Message(BOTH_MESSAGE, "minSpeed: %f maxSpeed: %f instantDv: %f\n", minSpeed, maxSpeed, instantDv);
// reprap.GetPlatform()->AppendMessage(BOTH_MESSAGE, "DDA-v: %f timeStep: %f\n", ddaIsolatedMove->velocity, ddaIsolatedMove->timeStep);
// reprap.GetPlatform()->AppendMessage(BOTH_MESSAGE, "stopAStep: %u startDStep: %u totalSteps: %u\n", ddaIsolatedMove->stopAStep, ddaIsolatedMove->startDStep, ddaIsolatedMove->totalSteps);
// reprap.GetPlatform()->AppendMessage(BOTH_MESSAGE, "V: %f Feedrate: %f\n", isolatedMove->V(), ddaIsolatedMove->feedRate);
return true;
}
bool Move::SetUpIsolatedMove(float to[], float feedRate, bool axesOnly)
{
if (isolatedMoveAvailable)
{
return false;
}
if(!(isolatedMove->Processed() & released)) // Should never happen...
{
platform->Message(BOTH_ERROR_MESSAGE, "Attempt to alter a non-released isolated lookahead entry!\n");
return false;
}
// Analyze this move basically the same way as in Spin()
long ep[DRIVES];
for(size_t drive = 0; drive < DRIVES; drive++)
{
if (drive < AXES)
{
normalisedDirectionVector[drive] = to[drive] - liveCoordinates[drive];
ep[drive] = LookAhead::EndPointToMachine(drive, to[drive]);
}
else
{
if (!axesOnly)
{
normalisedDirectionVector[drive] = to[drive];
ep[drive] = LookAhead::EndPointToMachine(drive, to[drive]);
}
else
{
ep[drive] = 0;
}
}
}
Absolute(normalisedDirectionVector, DRIVES);
if (Normalise(normalisedDirectionVector, DRIVES) <= 0.0)
{
platform->Message(BOTH_ERROR_MESSAGE, "Attempt to normalise zero-length move.\n"); // Should never get here - noMove above
return false;
}
float minSpeed = VectorBoxIntersection(normalisedDirectionVector, platform->InstantDvs(), DRIVES);
float acceleration = VectorBoxIntersection(normalisedDirectionVector, platform->Accelerations(), DRIVES);
float maxSpeed = VectorBoxIntersection(normalisedDirectionVector, platform->MaxFeedrates(), DRIVES);
isolatedMove->Init(ep, feedRate, minSpeed, maxSpeed, acceleration, 0, zeroExtruderPositions);
// Perform acceleration calculation
const float instantDv = platform->InstantDv(platform->SlowestDrive());
float u = instantDv, v = instantDv;
ddaIsolatedMove->Init(isolatedMove, u, v);
isolatedMoveAvailable = true;
return true;
}
// Do the bed transform AFTER the axis transform
void Move::BedTransform(float xyzPoint[]) const
{
if(identityBedTransform)
return;
switch(NumberOfProbePoints())
{
case 0:
return;
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:
platform->Message(BOTH_ERROR_MESSAGE, "BedTransform: wrong number of sample points.");
}
}
// Invert the bed transform BEFORE the axis transform
void Move::InverseBedTransform(float xyzPoint[]) const
{
if(identityBedTransform)
return;