From 408718d2bb507a1323c6910075feb2d2db73d080 Mon Sep 17 00:00:00 2001 From: Jim McGee Date: Fri, 13 May 2011 21:38:23 -0700 Subject: [PATCH] Limit effect on dda->c when computing reprap style acceleration. This fix is really a hack to protect from overflow/underflows in the acceleration code due to the limited precision of the fixed point calculations. It does, however, protect the code from accidently turning off the step timer or setting the timer interval value outside of the range defined by the current interval and the ultimate endpoint interval. This is a cleanup replacement for the code posted on the reprap forum: http://forums.reprap.org/read.php?147,33082,83467#msg-83467 --- dda.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/dda.c b/dda.c index 279a837..4f68d33 100644 --- a/dda.c +++ b/dda.c @@ -481,12 +481,23 @@ void dda_step(DDA *dda) { #ifdef ACCELERATION_REPRAP // linear acceleration magic, courtesy of http://www.embedded.com/columns/technicalinsights/56800129?printable=true if (dda->accel) { - if ( - ((dda->n > 0) && (dda->c > dda->end_c)) || - ((dda->n < 0) && (dda->c < dda->end_c)) - ) { - dda->c = (int32_t) dda->c - ((int32_t) (dda->c * 2) / dda->n); - dda->n += 4; + if ((dda->c > dda->end_c) && (dda->n > 0)) { + uint32_t new_c = dda->c - (dda->c * 2) / dda->n; + if (new_c <= dda->c && new_c > dda->end_c) { + dda->c = new_c; + dda->n += 4; + } + else + dda->c = dda->end_c; + } + else if ((dda->c < dda->end_c) && (dda->n < 0)) { + uint32_t new_c = dda->c + ((dda->c * 2) / -dda->n); + if (new_c >= dda->c && new_c < dda->end_c) { + dda->c = new_c; + dda->n += 4; + } + else + dda->c = dda->end_c; } else if (dda->c != dda->end_c) { dda->c = dda->end_c;