[2.0.x] 6th-order jerk-controlled motion planning in real-time for AVR (#10373)

This commit is contained in:
Eduardo José Tagle
2018-04-11 20:13:42 -03:00
committed by Scott Lahteine
parent 5676b187b4
commit 57a899a412
5 changed files with 1265 additions and 126 deletions

View File

@ -56,6 +56,10 @@
*
* IntersectionDistance[s1_, s2_, a_, d_] := (2 a d - s1^2 + s2^2)/(4 a)
*
* --
*
* The fast inverse function needed for Bézier interpolation for AVR
* was designed, written and tested by Eduardo José Tagle on April/2018
*/
#include "planner.h"
@ -215,6 +219,523 @@ void Planner::init() {
#endif
}
#if ENABLED(BEZIER_JERK_CONTROL)
#ifdef __AVR__
// This routine, for AVR, returns 0x1000000 / d, but trying to get the inverse as
// fast as possible. A fast converging iterative Newton-Raphson method is able to
// reach full precision in just 1 iteration, and takes 211 cycles (worst case, mean
// case is less, up to 30 cycles for small divisors), instead of the 500 cycles a
// normal division would take.
//
// Inspired by the following page,
// https://stackoverflow.com/questions/27801397/newton-raphson-division-with-big-integers
//
// Suppose we want to calculate
// floor(2 ^ k / B) where B is a positive integer
// Then
// B must be <= 2^k, otherwise, the quotient is 0.
//
// The Newton - Raphson iteration for x = B / 2 ^ k yields:
// q[n + 1] = q[n] * (2 - q[n] * B / 2 ^ k)
//
// We can rearrange it as:
// q[n + 1] = q[n] * (2 ^ (k + 1) - q[n] * B) >> k
//
// Each iteration of this kind requires only integer multiplications
// and bit shifts.
// Does it converge to floor(2 ^ k / B) ?: Not necessarily, but, in
// the worst case, it eventually alternates between floor(2 ^ k / B)
// and ceiling(2 ^ k / B)).
// So we can use some not-so-clever test to see if we are in this
// case, and extract floor(2 ^ k / B).
// Lastly, a simple but important optimization for this approach is to
// truncate multiplications (i.e.calculate only the higher bits of the
// product) in the early iterations of the Newton - Raphson method.The
// reason to do so, is that the results of the early iterations are far
// from the quotient, and it doesn't matter to perform them inaccurately.
// Finally, we should pick a good starting value for x. Knowing how many
// digits the divisor has, we can estimate it:
//
// 2^k / x = 2 ^ log2(2^k / x)
// 2^k / x = 2 ^(log2(2^k)-log2(x))
// 2^k / x = 2 ^(k*log2(2)-log2(x))
// 2^k / x = 2 ^ (k-log2(x))
// 2^k / x >= 2 ^ (k-floor(log2(x)))
// floor(log2(x)) simply is the index of the most significant bit set.
//
// If we could improve this estimation even further, then the number of
// iterations can be dropped quite a bit, thus saving valuable execution time.
// The paper "Software Integer Division" by Thomas L.Rodeheffer, Microsoft
// Research, Silicon Valley,August 26, 2008, that is available at
// https://www.microsoft.com/en-us/research/wp-content/uploads/2008/08/tr-2008-141.pdf
// suggests , for its integer division algorithm, that using a table to supply the
// first 8 bits of precision, and due to the quadratic convergence nature of the
// Newton-Raphon iteration, then just 2 iterations should be enough to get
// maximum precision of the division.
// If we precompute values of inverses for small denominator values, then
// just one Newton-Raphson iteration is enough to reach full precision
// We will use the top 9 bits of the denominator as index.
//
// The AVR assembly function is implementing the following C code, included
// here as reference:
//
// uint32_t get_period_inverse(uint32_t d) {
// static const uint8_t inv_tab[256] = {
// 255,253,252,250,248,246,244,242,240,238,236,234,233,231,229,227,
// 225,224,222,220,218,217,215,213,212,210,208,207,205,203,202,200,
// 199,197,195,194,192,191,189,188,186,185,183,182,180,179,178,176,
// 175,173,172,170,169,168,166,165,164,162,161,160,158,157,156,154,
// 153,152,151,149,148,147,146,144,143,142,141,139,138,137,136,135,
// 134,132,131,130,129,128,127,126,125,123,122,121,120,119,118,117,
// 116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,
// 100,99,98,97,96,95,94,93,92,91,90,89,88,88,87,86,
// 85,84,83,82,81,80,80,79,78,77,76,75,74,74,73,72,
// 71,70,70,69,68,67,66,66,65,64,63,62,62,61,60,59,
// 59,58,57,56,56,55,54,53,53,52,51,50,50,49,48,48,
// 47,46,46,45,44,43,43,42,41,41,40,39,39,38,37,37,
// 36,35,35,34,33,33,32,32,31,30,30,29,28,28,27,27,
// 26,25,25,24,24,23,22,22,21,21,20,19,19,18,18,17,
// 17,16,15,15,14,14,13,13,12,12,11,10,10,9,9,8,
// 8,7,7,6,6,5,5,4,4,3,3,2,2,1,0,0
// };
//
// // For small denominators, it is cheaper to directly store the result,
// // because those denominators would require 2 Newton-Raphson iterations
// // to converge to the required result precision. For bigger ones, just
// // ONE Newton-Raphson iteration is enough to get maximum precision!
// static const uint32_t small_inv_tab[111] PROGMEM = {
// 16777216,16777216,8388608,5592405,4194304,3355443,2796202,2396745,2097152,1864135,1677721,1525201,1398101,1290555,1198372,1118481,
// 1048576,986895,932067,883011,838860,798915,762600,729444,699050,671088,645277,621378,599186,578524,559240,541200,
// 524288,508400,493447,479349,466033,453438,441505,430185,419430,409200,399457,390167,381300,372827,364722,356962,
// 349525,342392,335544,328965,322638,316551,310689,305040,299593,294337,289262,284359,279620,275036,270600,266305,
// 262144,258111,254200,250406,246723,243148,239674,236298,233016,229824,226719,223696,220752,217885,215092,212369,
// 209715,207126,204600,202135,199728,197379,195083,192841,190650,188508,186413,184365,182361,180400,178481,176602,
// 174762,172960,171196,169466,167772,166111,164482,162885,161319,159783,158275,156796,155344,153919,152520
// };
//
// // For small divisors, it is best to directly retrieve the results
// if (d <= 110)
// return pgm_read_dword(&small_inv_tab[d]);
//
// // Compute initial estimation of 0x1000000/x -
// // Get most significant bit set on divider
// uint8_t idx = 0;
// uint32_t nr = d;
// if (!(nr & 0xff0000)) {
// nr <<= 8;
// idx += 8;
// if (!(nr & 0xff0000)) {
// nr <<= 8;
// idx += 8;
// }
// }
// if (!(nr & 0xf00000)) {
// nr <<= 4;
// idx += 4;
// }
// if (!(nr & 0xc00000)) {
// nr <<= 2;
// idx += 2;
// }
// if (!(nr & 0x800000)) {
// nr <<= 1;
// idx += 1;
// }
//
// // Isolate top 9 bits of the denominator, to be used as index into the initial estimation table
// uint32_t tidx = nr >> 15; // top 9 bits. bit8 is always set
// uint32_t ie = inv_tab[tidx & 0xFF] + 256; // Get the table value. bit9 is always set
// uint32_t x = idx <= 8 ? (ie >> (8 - idx)) : (ie << (idx - 8)); // Position the estimation at the proper place
//
// // Now, refine estimation by newton-raphson. 1 iteration is enough
// x = uint32_t((x * uint64_t((1 << 25) - x * d)) >> 24);
//
// // Estimate remainder
// uint32_t r = (1 << 24) - x * d;
//
// // Check if we must adjust result
// if (r >= d) x++;
//
// // x holds the proper estimation
// return uint32_t(x);
// }
//
static uint32_t get_period_inverse(uint32_t d) {
static const uint8_t inv_tab[256] PROGMEM = {
255,253,252,250,248,246,244,242,240,238,236,234,233,231,229,227,
225,224,222,220,218,217,215,213,212,210,208,207,205,203,202,200,
199,197,195,194,192,191,189,188,186,185,183,182,180,179,178,176,
175,173,172,170,169,168,166,165,164,162,161,160,158,157,156,154,
153,152,151,149,148,147,146,144,143,142,141,139,138,137,136,135,
134,132,131,130,129,128,127,126,125,123,122,121,120,119,118,117,
116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,
100,99,98,97,96,95,94,93,92,91,90,89,88,88,87,86,
85,84,83,82,81,80,80,79,78,77,76,75,74,74,73,72,
71,70,70,69,68,67,66,66,65,64,63,62,62,61,60,59,
59,58,57,56,56,55,54,53,53,52,51,50,50,49,48,48,
47,46,46,45,44,43,43,42,41,41,40,39,39,38,37,37,
36,35,35,34,33,33,32,32,31,30,30,29,28,28,27,27,
26,25,25,24,24,23,22,22,21,21,20,19,19,18,18,17,
17,16,15,15,14,14,13,13,12,12,11,10,10,9,9,8,
8,7,7,6,6,5,5,4,4,3,3,2,2,1,0,0
};
// For small denominators, it is cheaper to directly store the result.
// For bigger ones, just ONE Newton-Raphson iteration is enough to get
// maximum precision we need
static const uint32_t small_inv_tab[111] PROGMEM = {
16777216,16777216,8388608,5592405,4194304,3355443,2796202,2396745,2097152,1864135,1677721,1525201,1398101,1290555,1198372,1118481,
1048576,986895,932067,883011,838860,798915,762600,729444,699050,671088,645277,621378,599186,578524,559240,541200,
524288,508400,493447,479349,466033,453438,441505,430185,419430,409200,399457,390167,381300,372827,364722,356962,
349525,342392,335544,328965,322638,316551,310689,305040,299593,294337,289262,284359,279620,275036,270600,266305,
262144,258111,254200,250406,246723,243148,239674,236298,233016,229824,226719,223696,220752,217885,215092,212369,
209715,207126,204600,202135,199728,197379,195083,192841,190650,188508,186413,184365,182361,180400,178481,176602,
174762,172960,171196,169466,167772,166111,164482,162885,161319,159783,158275,156796,155344,153919,152520
};
// For small divisors, it is best to directly retrieve the results
if (d <= 110)
return pgm_read_dword(&small_inv_tab[d]);
register uint8_t r8 = d & 0xFF;
register uint8_t r9 = (d >> 8) & 0xFF;
register uint8_t r10 = (d >> 16) & 0xFF;
register uint8_t r2,r3,r4,r5,r6,r7,r11,r12,r13,r14,r15,r16,r17,r18;
register const uint8_t* ptab = inv_tab;
__asm__ __volatile__(
/* %8:%7:%6 = interval*/
/* r31:r30: MUST be those registers, and they must point to the inv_tab */
" clr %13" "\n\t" /* %13 = 0 */
/* Now we must compute */
/* result = 0xFFFFFF / d */
/* %8:%7:%6 = interval*/
/* %16:%15:%14 = nr */
/* %13 = 0*/
/* A plain division of 24x24 bits should take 388 cycles to complete. We will */
/* use Newton-Raphson for the calculation, and will strive to get way less cycles*/
/* for the same result - Using C division, it takes 500cycles to complete .*/
" clr %3" "\n\t" /* idx = 0 */
" mov %14,%6" "\n\t"
" mov %15,%7" "\n\t"
" mov %16,%8" "\n\t" /* nr = interval */
" tst %16" "\n\t" /* nr & 0xFF0000 == 0 ? */
" brne 2f" "\n\t" /* No, skip this */
" mov %16,%15" "\n\t"
" mov %15,%14" "\n\t" /* nr <<= 8, %14 not needed */
" subi %3,-8" "\n\t" /* idx += 8 */
" tst %16" "\n\t" /* nr & 0xFF0000 == 0 ? */
" brne 2f" "\n\t" /* No, skip this */
" mov %16,%15" "\n\t" /* nr <<= 8, %14 not needed */
" clr %15" "\n\t" /* We clear %14 */
" subi %3,-8" "\n\t" /* idx += 8 */
/* here %16 != 0 and %16:%15 contains at least 9 MSBits, or both %16:%15 are 0 */
"2:" "\n\t"
" cpi %16,0x10" "\n\t" /* (nr & 0xf00000) == 0 ? */
" brcc 3f" "\n\t" /* No, skip this */
" swap %15" "\n\t" /* Swap nibbles */
" swap %16" "\n\t" /* Swap nibbles. Low nibble is 0 */
" mov %14, %15" "\n\t"
" andi %14,0x0f" "\n\t" /* Isolate low nibble */
" andi %15,0xf0" "\n\t" /* Keep proper nibble in %15 */
" or %16, %14" "\n\t" /* %16:%15 <<= 4 */
" subi %3,-4" "\n\t" /* idx += 4 */
"3:" "\n\t"
" cpi %16,0x40" "\n\t" /* (nr & 0xc00000) == 0 ? */
" brcc 4f" "\n\t" /* No, skip this*/
" add %15,%15" "\n\t"
" adc %16,%16" "\n\t"
" add %15,%15" "\n\t"
" adc %16,%16" "\n\t" /* %16:%15 <<= 2 */
" subi %3,-2" "\n\t" /* idx += 2 */
"4:" "\n\t"
" cpi %16,0x80" "\n\t" /* (nr & 0x800000) == 0 ? */
" brcc 5f" "\n\t" /* No, skip this */
" add %15,%15" "\n\t"
" adc %16,%16" "\n\t" /* %16:%15 <<= 1 */
" inc %3" "\n\t" /* idx += 1 */
/* Now %16:%15 contains its MSBit set to 1, or %16:%15 is == 0. We are now absolutely sure*/
/* we have at least 9 MSBits available to enter the initial estimation table*/
"5:" "\n\t"
" add %15,%15" "\n\t"
" adc %16,%16" "\n\t" /* %16:%15 = tidx = (nr <<= 1), we lose the top MSBit (always set to 1, %16 is the index into the inverse table)*/
" add r30,%16" "\n\t" /* Only use top 8 bits */
" adc r31,%13" "\n\t" /* r31:r30 = inv_tab + (tidx) */
" lpm %14, Z" "\n\t" /* %14 = inv_tab[tidx] */
" ldi %15, 1" "\n\t" /* %15 = 1 %15:%14 = inv_tab[tidx] + 256 */
/* We must scale the approximation to the proper place*/
" clr %16" "\n\t" /* %16 will always be 0 here */
" subi %3,8" "\n\t" /* idx == 8 ? */
" breq 6f" "\n\t" /* yes, no need to scale*/
" brcs 7f" "\n\t" /* If C=1, means idx < 8, result was negative!*/
/* idx > 8, now %3 = idx - 8. We must perform a left shift. idx range:[1-8]*/
" sbrs %3,0" "\n\t" /* shift by 1bit position?*/
" rjmp 8f" "\n\t" /* No*/
" add %14,%14" "\n\t"
" adc %15,%15" "\n\t" /* %15:16 <<= 1*/
"8:" "\n\t"
" sbrs %3,1" "\n\t" /* shift by 2bit position?*/
" rjmp 9f" "\n\t" /* No*/
" add %14,%14" "\n\t"
" adc %15,%15" "\n\t"
" add %14,%14" "\n\t"
" adc %15,%15" "\n\t" /* %15:16 <<= 1*/
"9:" "\n\t"
" sbrs %3,2" "\n\t" /* shift by 4bits position?*/
" rjmp 16f" "\n\t" /* No*/
" swap %15" "\n\t" /* Swap nibbles. lo nibble of %15 will always be 0*/
" swap %14" "\n\t" /* Swap nibbles*/
" mov %12,%14" "\n\t"
" andi %12,0x0f" "\n\t" /* isolate low nibble*/
" andi %14,0xf0" "\n\t" /* and clear it*/
" or %15,%12" "\n\t" /* %15:%16 <<= 4*/
"16:" "\n\t"
" sbrs %3,3" "\n\t" /* shift by 8bits position?*/
" rjmp 6f" "\n\t" /* No, we are done */
" mov %16,%15" "\n\t"
" mov %15,%14" "\n\t"
" clr %14" "\n\t"
" jmp 6f" "\n\t"
/* idx < 8, now %3 = idx - 8. Get the count of bits */
"7:" "\n\t"
" neg %3" "\n\t" /* %3 = -idx = count of bits to move right. idx range:[1...8]*/
" sbrs %3,0" "\n\t" /* shift by 1 bit position ?*/
" rjmp 10f" "\n\t" /* No, skip it*/
" asr %15" "\n\t" /* (bit7 is always 0 here)*/
" ror %14" "\n\t"
"10:" "\n\t"
" sbrs %3,1" "\n\t" /* shift by 2 bit position ?*/
" rjmp 11f" "\n\t" /* No, skip it*/
" asr %15" "\n\t" /* (bit7 is always 0 here)*/
" ror %14" "\n\t"
" asr %15" "\n\t" /* (bit7 is always 0 here)*/
" ror %14" "\n\t"
"11:" "\n\t"
" sbrs %3,2" "\n\t" /* shift by 4 bit position ?*/
" rjmp 12f" "\n\t" /* No, skip it*/
" swap %15" "\n\t" /* Swap nibbles*/
" andi %14, 0xf0" "\n\t" /* Lose the lowest nibble*/
" swap %14" "\n\t" /* Swap nibbles. Upper nibble is 0*/
" or %14,%15" "\n\t" /* Pass nibble from upper byte*/
" andi %15, 0x0f" "\n\t" /* And get rid of that nibble*/
"12:" "\n\t"
" sbrs %3,3" "\n\t" /* shift by 8 bit position ?*/
" rjmp 6f" "\n\t" /* No, skip it*/
" mov %14,%15" "\n\t"
" clr %15" "\n\t"
"6:" "\n\t" /* %16:%15:%14 = initial estimation of 0x1000000 / d*/
/* Now, we must refine the estimation present on %16:%15:%14 using 1 iteration*/
/* of Newton-Raphson. As it has a quadratic convergence, 1 iteration is enough*/
/* to get more than 18bits of precision (the initial table lookup gives 9 bits of*/
/* precision to start from). 18bits of precision is all what is needed here for result */
/* %8:%7:%6 = d = interval*/
/* %16:%15:%14 = x = initial estimation of 0x1000000 / d*/
/* %13 = 0*/
/* %3:%2:%1:%0 = working accumulator*/
/* Compute 1<<25 - x*d. Result should never exceed 25 bits and should always be positive*/
" clr %0" "\n\t"
" clr %1" "\n\t"
" clr %2" "\n\t"
" ldi %3,2" "\n\t" /* %3:%2:%1:%0 = 0x2000000*/
" mul %6,%14" "\n\t" /* r1:r0 = LO(d) * LO(x)*/
" sub %0,r0" "\n\t"
" sbc %1,r1" "\n\t"
" sbc %2,%13" "\n\t"
" sbc %3,%13" "\n\t" /* %3:%2:%1:%0 -= LO(d) * LO(x)*/
" mul %7,%14" "\n\t" /* r1:r0 = MI(d) * LO(x)*/
" sub %1,r0" "\n\t"
" sbc %2,r1" "\n\t"
" sbc %3,%13" "\n\t" /* %3:%2:%1:%0 -= MI(d) * LO(x) << 8*/
" mul %8,%14" "\n\t" /* r1:r0 = HI(d) * LO(x)*/
" sub %2,r0" "\n\t"
" sbc %3,r1" "\n\t" /* %3:%2:%1:%0 -= MIL(d) * LO(x) << 16*/
" mul %6,%15" "\n\t" /* r1:r0 = LO(d) * MI(x)*/
" sub %1,r0" "\n\t"
" sbc %2,r1" "\n\t"
" sbc %3,%13" "\n\t" /* %3:%2:%1:%0 -= LO(d) * MI(x) << 8*/
" mul %7,%15" "\n\t" /* r1:r0 = MI(d) * MI(x)*/
" sub %2,r0" "\n\t"
" sbc %3,r1" "\n\t" /* %3:%2:%1:%0 -= MI(d) * MI(x) << 16*/
" mul %8,%15" "\n\t" /* r1:r0 = HI(d) * MI(x)*/
" sub %3,r0" "\n\t" /* %3:%2:%1:%0 -= MIL(d) * MI(x) << 24*/
" mul %6,%16" "\n\t" /* r1:r0 = LO(d) * HI(x)*/
" sub %2,r0" "\n\t"
" sbc %3,r1" "\n\t" /* %3:%2:%1:%0 -= LO(d) * HI(x) << 16*/
" mul %7,%16" "\n\t" /* r1:r0 = MI(d) * HI(x)*/
" sub %3,r0" "\n\t" /* %3:%2:%1:%0 -= MI(d) * HI(x) << 24*/
/* %3:%2:%1:%0 = (1<<25) - x*d [169]*/
/* We need to multiply that result by x, and we are only interested in the top 24bits of that multiply*/
/* %16:%15:%14 = x = initial estimation of 0x1000000 / d*/
/* %3:%2:%1:%0 = (1<<25) - x*d = acc*/
/* %13 = 0 */
/* result = %11:%10:%9:%5:%4*/
" mul %14,%0" "\n\t" /* r1:r0 = LO(x) * LO(acc)*/
" mov %4,r1" "\n\t"
" clr %5" "\n\t"
" clr %9" "\n\t"
" clr %10" "\n\t"
" clr %11" "\n\t" /* %11:%10:%9:%5:%4 = LO(x) * LO(acc) >> 8*/
" mul %15,%0" "\n\t" /* r1:r0 = MI(x) * LO(acc)*/
" add %4,r0" "\n\t"
" adc %5,r1" "\n\t"
" adc %9,%13" "\n\t"
" adc %10,%13" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * LO(acc) */
" mul %16,%0" "\n\t" /* r1:r0 = HI(x) * LO(acc)*/
" add %5,r0" "\n\t"
" adc %9,r1" "\n\t"
" adc %10,%13" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * LO(acc) << 8*/
" mul %14,%1" "\n\t" /* r1:r0 = LO(x) * MIL(acc)*/
" add %4,r0" "\n\t"
" adc %5,r1" "\n\t"
" adc %9,%13" "\n\t"
" adc %10,%13" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 = LO(x) * MIL(acc)*/
" mul %15,%1" "\n\t" /* r1:r0 = MI(x) * MIL(acc)*/
" add %5,r0" "\n\t"
" adc %9,r1" "\n\t"
" adc %10,%13" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * MIL(acc) << 8*/
" mul %16,%1" "\n\t" /* r1:r0 = HI(x) * MIL(acc)*/
" add %9,r0" "\n\t"
" adc %10,r1" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * MIL(acc) << 16*/
" mul %14,%2" "\n\t" /* r1:r0 = LO(x) * MIH(acc)*/
" add %5,r0" "\n\t"
" adc %9,r1" "\n\t"
" adc %10,%13" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 = LO(x) * MIH(acc) << 8*/
" mul %15,%2" "\n\t" /* r1:r0 = MI(x) * MIH(acc)*/
" add %9,r0" "\n\t"
" adc %10,r1" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * MIH(acc) << 16*/
" mul %16,%2" "\n\t" /* r1:r0 = HI(x) * MIH(acc)*/
" add %10,r0" "\n\t"
" adc %11,r1" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * MIH(acc) << 24*/
" mul %14,%3" "\n\t" /* r1:r0 = LO(x) * HI(acc)*/
" add %9,r0" "\n\t"
" adc %10,r1" "\n\t"
" adc %11,%13" "\n\t" /* %11:%10:%9:%5:%4 = LO(x) * HI(acc) << 16*/
" mul %15,%3" "\n\t" /* r1:r0 = MI(x) * HI(acc)*/
" add %10,r0" "\n\t"
" adc %11,r1" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * HI(acc) << 24*/
" mul %16,%3" "\n\t" /* r1:r0 = HI(x) * HI(acc)*/
" add %11,r0" "\n\t" /* %11:%10:%9:%5:%4 += MI(x) * HI(acc) << 32*/
/* At this point, %11:%10:%9 contains the new estimation of x. */
/* Finally, we must correct the result. Estimate remainder as*/
/* (1<<24) - x*d*/
/* %11:%10:%9 = x*/
/* %8:%7:%6 = d = interval" "\n\t" /* */
" ldi %3,1" "\n\t"
" clr %2" "\n\t"
" clr %1" "\n\t"
" clr %0" "\n\t" /* %3:%2:%1:%0 = 0x1000000*/
" mul %6,%9" "\n\t" /* r1:r0 = LO(d) * LO(x)*/
" sub %0,r0" "\n\t"
" sbc %1,r1" "\n\t"
" sbc %2,%13" "\n\t"
" sbc %3,%13" "\n\t" /* %3:%2:%1:%0 -= LO(d) * LO(x)*/
" mul %7,%9" "\n\t" /* r1:r0 = MI(d) * LO(x)*/
" sub %1,r0" "\n\t"
" sbc %2,r1" "\n\t"
" sbc %3,%13" "\n\t" /* %3:%2:%1:%0 -= MI(d) * LO(x) << 8*/
" mul %8,%9" "\n\t" /* r1:r0 = HI(d) * LO(x)*/
" sub %2,r0" "\n\t"
" sbc %3,r1" "\n\t" /* %3:%2:%1:%0 -= MIL(d) * LO(x) << 16*/
" mul %6,%10" "\n\t" /* r1:r0 = LO(d) * MI(x)*/
" sub %1,r0" "\n\t"
" sbc %2,r1" "\n\t"
" sbc %3,%13" "\n\t" /* %3:%2:%1:%0 -= LO(d) * MI(x) << 8*/
" mul %7,%10" "\n\t" /* r1:r0 = MI(d) * MI(x)*/
" sub %2,r0" "\n\t"
" sbc %3,r1" "\n\t" /* %3:%2:%1:%0 -= MI(d) * MI(x) << 16*/
" mul %8,%10" "\n\t" /* r1:r0 = HI(d) * MI(x)*/
" sub %3,r0" "\n\t" /* %3:%2:%1:%0 -= MIL(d) * MI(x) << 24*/
" mul %6,%11" "\n\t" /* r1:r0 = LO(d) * HI(x)*/
" sub %2,r0" "\n\t"
" sbc %3,r1" "\n\t" /* %3:%2:%1:%0 -= LO(d) * HI(x) << 16*/
" mul %7,%11" "\n\t" /* r1:r0 = MI(d) * HI(x)*/
" sub %3,r0" "\n\t" /* %3:%2:%1:%0 -= MI(d) * HI(x) << 24*/
/* %3:%2:%1:%0 = r = (1<<24) - x*d*/
/* %8:%7:%6 = d = interval */
/* Perform the final correction*/
" sub %0,%6" "\n\t"
" sbc %1,%7" "\n\t"
" sbc %2,%8" "\n\t" /* r -= d*/
" brcs 14f" "\n\t" /* if ( r >= d) */
/* %11:%10:%9 = x */
" ldi %3,1" "\n\t"
" add %9,%3" "\n\t"
" adc %10,%13" "\n\t"
" adc %11,%13" "\n\t" /* x++*/
"14:" "\n\t"
/* Estimation is done. %11:%10:%9 = x */
" clr __zero_reg__" "\n\t" /* Make C runtime happy */
/* [211 cycles total]*/
: "=r" (r2),
"=r" (r3),
"=r" (r4),
"=d" (r5),
"=r" (r6),
"=r" (r7),
"+r" (r8),
"+r" (r9),
"+r" (r10),
"=d" (r11),
"=r" (r12),
"=r" (r13),
"=d" (r14),
"=d" (r15),
"=d" (r16),
"=d" (r17),
"=d" (r18),
"+z" (ptab)
:
: "r0", "r1", "cc"
);
// Return the result
return r11 | (uint16_t(r12) << 8) | (uint32_t(r13) << 16);
}
#else
// All the other 32 CPUs can easily perform the inverse using hardware division,
// so we don´t need to reduce precision or to use assembly language at all.
// This routine, for all the other archs, returns 0x100000000 / d ~= 0xFFFFFFFF / d
static FORCE_INLINE uint32_t get_period_inverse(uint32_t d) {
return 0xFFFFFFFF / d;
}
#endif
#endif
#define MINIMAL_STEP_RATE 120
/**
@ -266,8 +787,13 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e
#if ENABLED(BEZIER_JERK_CONTROL)
// Jerk controlled speed requires to express speed versus time, NOT steps
int32_t acceleration_time = ((float)(cruise_rate - initial_rate) / accel) * HAL_STEPPER_TIMER_RATE,
deceleration_time = ((float)(cruise_rate - final_rate) / accel) * HAL_STEPPER_TIMER_RATE;
uint32_t acceleration_time = ((float)(cruise_rate - initial_rate) / accel) * HAL_STEPPER_TIMER_RATE,
deceleration_time = ((float)(cruise_rate - final_rate) / accel) * HAL_STEPPER_TIMER_RATE;
// And to offload calculations from the ISR, we also calculate the inverse of those times here
uint32_t acceleration_time_inverse = get_period_inverse(acceleration_time);
uint32_t deceleration_time_inverse = get_period_inverse(deceleration_time);
#endif
CRITICAL_SECTION_START; // Fill variables used by the stepper in a critical section
@ -278,6 +804,8 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e
#if ENABLED(BEZIER_JERK_CONTROL)
block->acceleration_time = acceleration_time;
block->deceleration_time = deceleration_time;
block->acceleration_time_inverse = acceleration_time_inverse;
block->deceleration_time_inverse = deceleration_time_inverse;
block->cruise_rate = cruise_rate;
#endif
block->final_rate = final_rate;