From 46548dba479321c5181de306af8362e144f5681a Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Thu, 5 Dec 2013 21:33:30 +0100 Subject: [PATCH] DDA: make a huge comment compact and move it to where it belongs. Also clarify the acceleration formula. --- dda.c | 26 +++++--------------------- dda_maths.h | 4 +++- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/dda.c b/dda.c index afdb72e..6804dbe 100644 --- a/dda.c +++ b/dda.c @@ -339,25 +339,9 @@ void dda_create(DDA *dda, TARGET *target) { if (dda->c_min < c_limit) dda->c_min = c_limit; - /** - Assuming: F is in mm/min, STEPS_PER_M_X is in steps/m, ACCELERATION is in mm/s² - Given: - - Velocity v at time t given acceleration a: v(t) = a*t - - Displacement s at time t given acceleration a: s(t) = 1/2 * a * t² - - Displacement until reaching target velocity v: s = 1/2 * (v² / a) - - Final result: steps needed to reach velocity v given acceleration a: - steps = (STEPS_PER_M_X * F^2) / (7200000 * ACCELERATION) - To keep precision, break up in floating point and integer part: - F^2 * (int)(STEPS_PER_M_X / (7200000 * ACCELERATION)) - Note: the floating point part is static so its calculated during compilation. - Note 2: the floating point part will be smaller than one, invert it: - steps = F^2 / (int)((7200000 * ACCELERATION) / STEPS_PER_M_X) - Note 3: As mentioned, setting F to 65535 or larger will overflow the - calculation. Make sure this does not happen. - Note 4: Anyone trying to run their machine at 65535 mm/min > 1m/s is nuts - */ - if (target->F > 65534) - target->F = 65534; + // Lookahead can deal with 16 bits ( = 1092 mm/s), only. + if (target->F > 65535) + target->F = 65535; // Note: this is inaccurate for several reasons: // - target->F isn't reverse-calculated from c_limit, so speed @@ -864,8 +848,8 @@ void dda_clock() { if (dda->n == 0) move_c = C0; else - // Explicit formula: sqrt(n + 1) - sqrt(n), - // approximation here: 1 / (2 * sqrt(n)). + // Explicit formula: c0 * (sqrt(n + 1) - sqrt(n)), + // approximation here: c0 * (1 / (2 * sqrt(n))). move_c = ((C0 >> 8) * int_inv_sqrt(dda->n)) >> 5; if (move_c < dda->c_min) { diff --git a/dda_maths.h b/dda_maths.h index fa0c84c..14ea1cd 100644 --- a/dda_maths.h +++ b/dda_maths.h @@ -66,7 +66,9 @@ uint16_t int_inv_sqrt(uint16_t a); // 2 ^ msbloc(v) >= v const uint8_t msbloc (uint32_t v); -// Note: the floating point bit is optimized away during compilation +// s = 1/2 * a * t^2, v = a * t ==> s = v^2 / (2 * a) +// 7200000 = 60 * 60 * 1000 * 2 (mm/min -> mm/s, steps/m -> steps/mm, factor 2) +// Note: the floating point bit is optimized away during compilation. #define ACCELERATE_RAMP_LEN(speed) (((speed)*(speed)) / (uint32_t)((7200000.0f * ACCELERATION) / (float)STEPS_PER_M_X)) // Initialization constant for the ramping algorithm.