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.
pull/173/head
Yuri D'Elia 2022-05-18 02:05:52 +02:00 committed by DRracer
parent e99e7441e1
commit ea3b8e5c85
1 changed files with 6 additions and 1 deletions

View File

@ -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 just a 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;