-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.h
More file actions
662 lines (649 loc) · 18.7 KB
/
motion.h
File metadata and controls
662 lines (649 loc) · 18.7 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
/*
This file is part of Repetier-Firmware.
Repetier-Firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Repetier-Firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
This firmware is a nearly complete rewrite of the sprinter firmware
by kliment (https://github.com/kliment/Sprinter)
which based on Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
#
Functions in this file are used to communicate using ascii or repetier protocol.
*/
#ifndef MOTION_H_INCLUDED
#define MOTION_H_INCLUDED
/** Marks the first step of a new move */
#define FLAG_WARMUP 1
#define FLAG_NOMINAL 2
#define FLAG_DECELERATING 4
#define FLAG_ACCELERATION_ENABLED 8
#define FLAG_CHECK_ENDSTOPS 16
#define FLAG_SKIP_ACCELERATING 32
#define FLAG_SKIP_DEACCELERATING 64
#define FLAG_BLOCKED 128
/** Are the step parameter computed */
#define FLAG_JOIN_STEPPARAMS_COMPUTED 1
/** The right speed is fixed. Don't check this block or any block to the left. */
#define FLAG_JOIN_END_FIXED 2
/** The left speed is fixed. Don't check left block. */
#define FLAG_JOIN_START_FIXED 4
/** Start filament retraction at move start */
#define FLAG_JOIN_START_RETRACT 8
/** Wait for filament pushback, before ending move */
#define FLAG_JOIN_END_RETRACT 16
/** Disable retract for this line */
#define FLAG_JOIN_NO_RETRACT 32
/** Wait for the extruder to finish it's up movement */
#define FLAG_JOIN_WAIT_EXTRUDER_UP 64
/** Wait for the extruder to finish it's down movement */
#define FLAG_JOIN_WAIT_EXTRUDER_DOWN 128
// Printing related data
#if NONLINEAR_SYSTEM
// Allow the delta cache to store segments for every line in line cache. Beware this gets big ... fast.
// MAX_DELTA_SEGMENTS_PER_LINE *
#define DELTA_CACHE_SIZE (MAX_DELTA_SEGMENTS_PER_LINE * MOVE_CACHE_SIZE)
class PrintLine;
typedef struct
{
uint8_t dir; ///< Direction of delta movement.
uint16_t deltaSteps[3]; ///< Number of steps in move.
inline void checkEndstops(PrintLine *cur,bool checkall);
inline void setXMoveFinished()
{
dir&=~16;
}
inline void setYMoveFinished()
{
dir&=~32;
}
inline void setZMoveFinished()
{
dir&=~64;
}
inline void setXYMoveFinished()
{
dir&=~48;
}
inline bool isXPositiveMove()
{
return (dir & 17)==17;
}
inline bool isXNegativeMove()
{
return (dir & 17)==16;
}
inline bool isYPositiveMove()
{
return (dir & 34)==34;
}
inline bool isYNegativeMove()
{
return (dir & 34)==32;
}
inline bool isZPositiveMove()
{
return (dir & 68)==68;
}
inline bool isZNegativeMove()
{
return (dir & 68)==64;
}
inline bool isEPositiveMove()
{
return (dir & 136)==136;
}
inline bool isENegativeMove()
{
return (dir & 136)==128;
}
inline bool isXMove()
{
return (dir & 16);
}
inline bool isYMove()
{
return (dir & 32);
}
inline bool isXOrYMove()
{
return dir & 48;
}
inline bool isZMove()
{
return (dir & 64);
}
inline bool isEMove()
{
return (dir & 128);
}
inline bool isEOnlyMove()
{
return (dir & 240)==128;
}
inline bool isNoMove()
{
return (dir & 240)==0;
}
inline bool isXYZMove()
{
return dir & 112;
}
inline bool isMoveOfAxis(uint8_t axis)
{
return (dir & (16<<axis));
}
inline void setMoveOfAxis(uint8_t axis)
{
dir |= 16<<axis;
}
inline void setPositiveDirectionForAxis(uint8_t axis)
{
dir |= 1<<axis;
}
} DeltaSegment;
extern uint8_t lastMoveID;
#endif
class UIDisplay;
class PrintLine // RAM usage: 24*4+15 = 113 Byte
{
friend class UIDisplay;
#if CPU_ARCH==ARCH_ARM
static volatile bool nlFlag;
#endif
static uint8_t linesPos; // Position for executing line movement
static PrintLine lines[];
static uint8_t linesWritePos; // Position where we write the next cached line move
uint8_t primaryAxis;
volatile uint8_t flags;
long timeInTicks;
uint8_t joinFlags;
uint8_t halfStep; ///< 4 = disabled, 1 = halfstep, 2 = fulstep
uint8_t dir; ///< Direction of movement. 1 = X+, 2 = Y+, 4= Z+, values can be combined.
long delta[4]; ///< Steps we want to move.
long error[4]; ///< Error calculation for Bresenham algorithm
float speedX; ///< Speed in x direction at fullInterval in mm/s
float speedY; ///< Speed in y direction at fullInterval in mm/s
float speedZ; ///< Speed in z direction at fullInterval in mm/s
float speedE; ///< Speed in E direction at fullInterval in mm/s
float fullSpeed; ///< Desired speed mm/s
float invFullSpeed; ///< 1.0/fullSpeed for fatser computation
float accelerationDistance2; ///< Real 2.0*distanceÜacceleration mm²/s²
float maxJunctionSpeed; ///< Max. junction speed between this and next segment
float startSpeed; ///< Staring speed in mm/s
float endSpeed; ///< Exit speed in mm/s
float minSpeed;
float distance;
#if NONLINEAR_SYSTEM
uint8_t numDeltaSegments; ///< Number of delta segments left in line. Decremented by stepper timer.
uint8_t moveID; ///< ID used to identify moves which are all part of the same line
long numPrimaryStepPerSegment; ///< Number of primary bresenham axis steps in each delta segment
DeltaSegment segments[MAX_DELTA_SEGMENTS_PER_LINE];
#endif
ticks_t fullInterval; ///< interval at full speed in ticks/step.
unsigned int accelSteps; ///< How much steps does it take, to reach the plateau.
unsigned int decelSteps; ///< How much steps does it take, to reach the end speed.
unsigned long accelerationPrim; ///< Acceleration along primary axis
unsigned long fAcceleration; ///< accelerationPrim*262144/F_CPU
speed_t vMax; ///< Maximum reached speed in steps/s.
speed_t vStart; ///< Starting speed in steps/s.
speed_t vEnd; ///< End speed in steps/s
#ifdef USE_ADVANCE
#ifdef ENABLE_QUADRATIC_ADVANCE
long advanceRate; ///< Advance steps at full speed
long advanceFull; ///< Maximum advance at fullInterval [steps*65536]
long advanceStart;
long advanceEnd;
#endif
unsigned int advanceL; ///< Recomputated L value
#endif
#ifdef DEBUG_STEPCOUNT
long totalStepsRemaining;
#endif
public:
long stepsRemaining; ///< Remaining steps, until move is finished
static PrintLine *cur;
static volatile uint8_t linesCount; // Number of lines cached 0 = nothing to do
inline bool areParameterUpToDate()
{
return joinFlags & FLAG_JOIN_STEPPARAMS_COMPUTED;
}
inline void invalidateParameter()
{
joinFlags &= ~FLAG_JOIN_STEPPARAMS_COMPUTED;
}
inline void setParameterUpToDate()
{
joinFlags |= FLAG_JOIN_STEPPARAMS_COMPUTED;
}
inline bool isStartSpeedFixed()
{
return joinFlags & FLAG_JOIN_START_FIXED;
}
inline void setStartSpeedFixed(bool newState)
{
joinFlags = (newState ? joinFlags | FLAG_JOIN_START_FIXED : joinFlags & ~FLAG_JOIN_START_FIXED);
}
inline void fixStartAndEndSpeed()
{
joinFlags |= FLAG_JOIN_END_FIXED | FLAG_JOIN_START_FIXED;
}
inline bool isEndSpeedFixed()
{
return joinFlags & FLAG_JOIN_END_FIXED;
}
inline void setEndSpeedFixed(bool newState)
{
joinFlags = (newState ? joinFlags | FLAG_JOIN_END_FIXED : joinFlags & ~FLAG_JOIN_END_FIXED);
}
inline bool isWarmUp()
{
return flags & FLAG_WARMUP;
}
inline uint8_t getWaitForXLinesFilled()
{
return primaryAxis;
}
inline void setWaitForXLinesFilled(uint8_t b)
{
primaryAxis = b;
}
inline bool isExtruderForwardMove()
{
return (dir & 136)==136;
}
inline void block()
{
flags |= FLAG_BLOCKED;
}
inline void unblock()
{
flags &= ~FLAG_BLOCKED;
}
inline bool isBlocked()
{
return flags & FLAG_BLOCKED;
}
inline bool isCheckEndstops()
{
return flags & FLAG_CHECK_ENDSTOPS;
}
inline bool isNominalMove()
{
return flags & FLAG_NOMINAL;
}
inline bool setNominalMove()
{
flags |= FLAG_NOMINAL;
}
inline void checkEndstops()
{
if(isCheckEndstops())
{
if(isXNegativeMove() && Printer::isXMinEndstopHit())
setXMoveFinished();
if(isYNegativeMove() && Printer::isYMinEndstopHit())
setYMoveFinished();
if(isXPositiveMove() && Printer::isXMaxEndstopHit())
setXMoveFinished();
if(isYPositiveMove() && Printer::isYMaxEndstopHit())
setYMoveFinished();
}
#if FEATURE_Z_PROBE
if(Printer::isZProbingActive() && isZNegativeMove() && Printer::isZProbeHit())
{
setZMoveFinished();
Printer::stepsRemainingAtZHit = stepsRemaining;
}
else
#endif
// Test Z-Axis every step if necessary, otherwise it could easyly ruin your printer!
if(isZNegativeMove() && Printer::isZMinEndstopHit())
setZMoveFinished();
if(isZPositiveMove() && Printer::isZMaxEndstopHit())
{
#if MAX_HARDWARE_ENDSTOP_Z
Printer::stepsRemainingAtZHit = stepsRemaining;
#endif
setZMoveFinished();
}
if(isZPositiveMove() && Printer::isZMaxEndstopHit())
setZMoveFinished();
}
inline void setXMoveFinished()
{
#if DRIVE_SYSTEM==0 || NONLINEAR_SYSTEM
dir&=~16;
#else
dir&=~48;
#endif
}
inline void setYMoveFinished()
{
#if DRIVE_SYSTEM==0 || NONLINEAR_SYSTEM
dir&=~32;
#else
dir&=~48;
#endif
}
inline void setZMoveFinished()
{
dir&=~64;
}
inline void setXYMoveFinished()
{
dir&=~48;
}
inline bool isXPositiveMove()
{
return (dir & 17)==17;
}
inline bool isXNegativeMove()
{
return (dir & 17)==16;
}
inline bool isYPositiveMove()
{
return (dir & 34)==34;
}
inline bool isYNegativeMove()
{
return (dir & 34)==32;
}
inline bool isZPositiveMove()
{
return (dir & 68)==68;
}
inline bool isZNegativeMove()
{
return (dir & 68)==64;
}
inline bool isEPositiveMove()
{
return (dir & 136)==136;
}
inline bool isENegativeMove()
{
return (dir & 136)==128;
}
inline bool isXMove()
{
return (dir & 16);
}
inline bool isYMove()
{
return (dir & 32);
}
inline bool isXOrYMove()
{
return dir & 48;
}
inline bool isZMove()
{
return (dir & 64);
}
inline bool isEMove()
{
return (dir & 128);
}
inline bool isEOnlyMove()
{
return (dir & 240)==128;
}
inline bool isNoMove()
{
return (dir & 240)==0;
}
inline bool isXYZMove()
{
return dir & 112;
}
inline bool isMoveOfAxis(uint8_t axis)
{
return (dir & (16<<axis));
}
inline void setMoveOfAxis(uint8_t axis)
{
dir |= 16<<axis;
}
inline void setPositiveDirectionForAxis(uint8_t axis)
{
dir |= 1<<axis;
}
inline static void resetPathPlanner()
{
linesCount = 0;
linesPos = linesWritePos;
}
inline void updateAdvanceSteps(unsigned int v,uint8_t max_loops,bool accelerate)
{
#ifdef USE_ADVANCE
if(!Printer::isAdvanceActivated()) return;
#ifdef ENABLE_QUADRATIC_ADVANCE
long advanceTarget = Printer::advanceExecuted;
if(accelerate)
{
for(uint8_t loop = 0; loop<max_loops; loop++) advanceTarget += advanceRate;
if(advanceTarget>advanceFull)
advanceTarget = advanceFull;
}
else
{
for(uint8_t loop = 0; loop<max_loops; loop++) advanceTarget -= advanceRate;
if(advanceTarget<advanceEnd)
advanceTarget = advanceEnd;
}
long h = HAL::mulu16xu16to32(v,advanceL);
int tred = ((advanceTarget + h) >> 16);
HAL::forbidInterrupts();
Printer::extruderStepsNeeded += tred-Printer::advanceStepsSet;
if(tred>0 && Printer::advanceStepsSet<=0)
Printer::extruderStepsNeeded += Extruder::current->advanceBacklash;
else if(tred<0 && Printer::advanceStepsSet>=0)
Printer::extruderStepsNeeded -= Extruder::current->advanceBacklash;
Printer::advanceStepsSet = tred;
HAL::allowInterrupts();
Printer::advanceExecuted = advanceTarget;
#else
int tred = HAL::mulu6xu16shift16(v,advanceL);
HAL::forbidInterrupts();
Printer::extruderStepsNeeded += tred - Printer::advanceStepsSet;
if(tred>0 && Printer::advanceStepsSet<=0)
Printer::extruderStepsNeeded += (Extruder::current->advanceBacklash << 1);
else if(tred<0 && Printer::advanceStepsSet>=0)
Printer::extruderStepsNeeded -= (Extruder::current->advanceBacklash << 1);
Printer::advanceStepsSet = tred;
HAL::allowInterrupts();
#endif
#endif
}
inline bool moveDecelerating()
{
if(stepsRemaining <= decelSteps)
{
if (!(flags & FLAG_DECELERATING))
{
Printer::timer = 0;
flags |= FLAG_DECELERATING;
}
return true;
}
else return false;
}
inline bool moveAccelerating()
{
return Printer::stepNumber <= accelSteps;
}
inline bool isFullstepping()
{
return halfStep == 4;
}
inline bool startXStep()
{
ANALYZER_ON(ANALYZER_CH6);
#if DRIVE_SYSTEM==0 || !defined(XY_GANTRY)
ANALYZER_ON(ANALYZER_CH2);
WRITE(X_STEP_PIN,HIGH);
#if FEATURE_TWO_XSTEPPER
WRITE(X2_STEP_PIN,HIGH);
#endif
#else
#if DRIVE_SYSTEM==1
if(isXPositiveMove())
{
Printer::motorX++;
Printer::motorY++;
}
else
{
Printer::motorX--;
Printer::motorY--;
}
#endif
#if DRIVE_SYSTEM==2
if(isXPositiveMove())
{
Printer::motorX++;
Printer::motorY--;
}
else
{
Printer::motorX--;
Printer::motorY++;
}
#endif
#endif
#ifdef DEBUG_STEPCOUNT
totalStepsRemaining--;
#endif
}
inline bool startYStep()
{
ANALYZER_ON(ANALYZER_CH7);
#if DRIVE_SYSTEM==0 || !defined(XY_GANTRY)
ANALYZER_ON(ANALYZER_CH3);
WRITE(Y_STEP_PIN,HIGH);
#if FEATURE_TWO_YSTEPPER
WRITE(Y2_STEP_PIN,HIGH);
#endif
#else
#if DRIVE_SYSTEM==1
if(isYPositiveMove())
{
Printer::motorX++;
Printer::motorY--;
}
else
{
Printer::motorX--;
Printer::motorY++;
}
#endif
#if DRIVE_SYSTEM==2
if(isYPositiveMove())
{
Printer::motorX++;
Printer::motorY++;
}
else
{
Printer::motorX--;
Printer::motorY--;
}
#endif
#endif // XY_GANTRY
#ifdef DEBUG_STEPCOUNT
totalStepsRemaining--;
#endif
}
inline void startZStep()
{
WRITE(Z_STEP_PIN,HIGH);
#if FEATURE_TWO_ZSTEPPER
WRITE(Z2_STEP_PIN,HIGH);
#endif
}
void updateStepsParameter();
inline float safeSpeed();
void calculateMove(float axis_diff[],uint8_t pathOptimize);
void logLine();
inline long getWaitTicks()
{
return timeInTicks;
}
inline void setWaitTicks(long wait)
{
timeInTicks = wait;
}
static inline bool hasLines()
{
return linesCount;
}
static inline void setCurrentLine()
{
cur = &lines[linesPos];
#if CPU_ARCH==ARCH_ARM
PrintLine::nlFlag = true;
#endif
}
static inline void removeCurrentLineForbidInterrupt()
{
linesPos++;
if(linesPos>=MOVE_CACHE_SIZE) linesPos=0;
cur = NULL;
#if CPU_ARCH==ARCH_ARM
nlFlag = false;
#endif
HAL::forbidInterrupts();
--linesCount;
}
static inline void pushLine()
{
linesWritePos++;
if(linesWritePos>=MOVE_CACHE_SIZE) linesWritePos = 0;
BEGIN_INTERRUPT_PROTECTED
linesCount++;
END_INTERRUPT_PROTECTED
}
static PrintLine *getNextWriteLine()
{
return &lines[linesWritePos];
}
static inline void computeMaxJunctionSpeed(PrintLine *previous,PrintLine *current);
static long bresenhamStep();
static void waitForXFreeLines(uint8_t b=1);
static inline void forwardPlanner(uint8_t p);
static inline void backwardPlanner(uint8_t p,uint8_t last);
static void updateTrapezoids();
static uint8_t insertWaitMovesIfNeeded(uint8_t pathOptimize, uint8_t waitExtraLines);
static void queueCartesianMove(uint8_t check_endstops,uint8_t pathOptimize);
static void moveRelativeDistanceInSteps(long x,long y,long z,long e,float feedrate,bool waitEnd,bool check_endstop);
#if ARC_SUPPORT
static void arc(float *position, float *target, float *offset, float radius, uint8_t isclockwise);
#endif
static inline void previousPlannerIndex(uint8_t &p)
{
p = (p ? p-1 : MOVE_CACHE_SIZE-1);
}
static inline void nextPlannerIndex(uint8_t& p)
{
p = (p==MOVE_CACHE_SIZE-1?0:p+1);
}
#if NONLINEAR_SYSTEM
static void queueDeltaMove(uint8_t check_endstops,uint8_t pathOptimize, uint8_t softEndstop);
static inline void queueEMove(long e_diff,uint8_t check_endstops,uint8_t pathOptimize);
inline uint16_t calculateDeltaSubSegments(uint8_t softEndstop);
static inline void calculateDirectionAndDelta(long difference[], uint8_t *dir, long delta[]);
static inline uint8_t calculateDistance(float axis_diff[], uint8_t dir, float *distance);
#ifdef SOFTWARE_LEVELING && DRIVE_SYSTEM==3
static void calculatePlane(long factors[], long p1[], long p2[], long p3[]);
static float calcZOffset(long factors[], long pointX, long pointY);
#endif
#endif
};
#endif // MOTION_H_INCLUDED