From 6fae5a8b7cb7738be2dfa58af0c6dd703cd99ff9 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Fri, 6 Dec 2013 01:57:31 +0100 Subject: [PATCH] DDA: make macro ACCELERATE_RAMP_LEN_SPM() a function. This macro is pretty expensive (700 bytes, well, stuff is now calculated at runtime), so there's no chance to use it in multiple places and we likely also need this in dda_lookahead.c to achieve full 4 axis compatibility there. --- dda.c | 4 ++-- dda_maths.c | 18 ++++++++++++++++++ dda_maths.h | 11 +++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/dda.c b/dda.c index e946c95..868fe3c 100644 --- a/dda.c +++ b/dda.c @@ -360,8 +360,8 @@ void dda_create(DDA *dda, TARGET *target) { // Acceleration ramps are based on the fast axis, not the combined speed. dda->rampup_steps = - ACCELERATE_RAMP_LEN_SPM(muldiv(dda->fast_um, dda->endpoint.F, distance), - dda->fast_spm); + acc_ramp_len(muldiv(dda->fast_um, dda->endpoint.F, distance), + dda->fast_spm); if (dda->rampup_steps > dda->total_steps / 2) dda->rampup_steps = dda->total_steps / 2; diff --git a/dda_maths.c b/dda_maths.c index 6e66f61..908ce59 100644 --- a/dda_maths.c +++ b/dda_maths.c @@ -235,3 +235,21 @@ const uint8_t msbloc (uint32_t v) { } return 0; } + +/*! Acceleration ramp length in steps. + * \param feedrate Target feedrate of the accelerateion. + * \param steps_per_m Steps/m of the axis. + * \return Accelerating steps neccessary to achieve target feedrate. + * + * 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: this function has shown to be accurate between 10 and 10'000 mm/s2 and + * 2000 to 4096000 steps/m (and higher). The numbers are a few percent + * too high at very low acceleration. Test code see commit message. + */ +uint32_t acc_ramp_len(uint32_t feedrate, uint32_t steps_per_m) { + return (feedrate * feedrate) / + (((uint32_t)7200000UL * ACCELERATION) / steps_per_m); +} + diff --git a/dda_maths.h b/dda_maths.h index 192ccd1..901e648 100644 --- a/dda_maths.h +++ b/dda_maths.h @@ -66,14 +66,9 @@ uint16_t int_inv_sqrt(uint16_t a); // 2 ^ msbloc(v) >= v const uint8_t msbloc (uint32_t v); -// 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: this macro has shown to be accurate between 10 and 10'000 mm/s2 and -// 2000 to 4096000 steps/m (and higher). The numbers are a few percent -// too high at very low acceleration. Test code see commit message. -#define ACCELERATE_RAMP_LEN_SPM(speed, spm) \ - (((speed) * (speed)) / \ - (uint32_t)((7200000UL * ACCELERATION) / (spm))) +// Calculates acceleration ramp length in steps. +uint32_t acc_ramp_len(uint32_t feedrate, uint32_t steps_per_m); + // For X axis only, should become obsolete: #define ACCELERATE_RAMP_LEN(speed) (((speed)*(speed)) / (uint32_t)((7200000.0f * ACCELERATION) / (float)STEPS_PER_M_X))