From eb7b5e578b5f904a29c9865e3f85222c20fc11a4 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Wed, 18 May 2022 02:05:52 +0200 Subject: [PATCH] pulse_gen: Fix acceleration_rate block calculations The acceleration_rate should really by a premultiplication by 1<<24 so that the division in Step() (while calculating the acc_step_rate) can be computed again with a right shift. This was incorrectly changed to F_CPU, which was close enough but would cause the acceleration to be always slighly slower than expected. Fix the ratio, but keep the multiplication in fixed-point to avoid a float conversion. --- src/modules/pulse_gen.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/modules/pulse_gen.cpp b/src/modules/pulse_gen.cpp index d06d1d0..79776c9 100644 --- a/src/modules/pulse_gen.cpp +++ b/src/modules/pulse_gen.cpp @@ -109,7 +109,12 @@ bool PulseGen::PlanMoveTo(pos_t target, steps_t feed_rate, steps_t end_rate) { // Acceleration of the segment, in steps/sec^2 block->acceleration = acceleration; - block->acceleration_rate = block->acceleration * (rate_t)((float)F_CPU / (F_CPU / config::stepTimerFrequencyDivider)); + + // Calculate the ratio to 2^24 so that the rate division in Step() can be a just right shift + constexpr float ratio = (float)(1lu << 24) / (F_CPU / config::stepTimerFrequencyDivider); + constexpr rate_t mul = 8; // pre-multiply to increase the integer division resolution + static_assert(!(mul & (mul - 1)), "mul must be a power of two"); + block->acceleration_rate = block->acceleration * (rate_t)(ratio * mul) / mul; // Simplified forward jerk: do not handle reversals steps_t entry_speed;