From 38b31059003110ae0cb2dcc3270620e69954a3d7 Mon Sep 17 00:00:00 2001 From: XDA-Bam <1209896+XDA-Bam@users.noreply.github.com> Date: Fri, 9 Sep 2022 22:51:11 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Minor=20planner=20optimiza?= =?UTF-8?q?tion=20(#24737)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/module/planner.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index 1c9601632d..6ef1ed6c28 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -793,19 +793,21 @@ void Planner::calculate_trapezoid_for_block(block_t * const block, const_float_t uint32_t cruise_rate = block->nominal_rate; #endif - const int32_t accel = block->acceleration_steps_per_s2; - // Steps for acceleration, plateau and deceleration int32_t plateau_steps = block->step_event_count; uint32_t accelerate_steps = 0, decelerate_steps = 0; + const int32_t accel = block->acceleration_steps_per_s2; + float inverse_accel = 0.0f; if (accel != 0) { - // Steps required for acceleration, deceleration to/from nominal rate - const float nominal_rate_sq = sq(float(block->nominal_rate)); - float accelerate_steps_float = (nominal_rate_sq - sq(float(initial_rate))) * (0.5f / accel); + inverse_accel = 1.0f / accel; + const float half_inverse_accel = 0.5f * inverse_accel, + nominal_rate_sq = sq(float(block->nominal_rate)), + // Steps required for acceleration, deceleration to/from nominal rate + decelerate_steps_float = half_inverse_accel * (nominal_rate_sq - sq(float(final_rate))); + float accelerate_steps_float = half_inverse_accel * (nominal_rate_sq - sq(float(initial_rate))); accelerate_steps = CEIL(accelerate_steps_float); - const float decelerate_steps_float = (nominal_rate_sq - sq(float(final_rate))) * (0.5f / accel); decelerate_steps = FLOOR(decelerate_steps_float); // Steps between acceleration and deceleration, if any @@ -828,9 +830,10 @@ void Planner::calculate_trapezoid_for_block(block_t * const block, const_float_t } #if ENABLED(S_CURVE_ACCELERATION) + const float rate_factor = inverse_accel * (STEPPER_TIMER_RATE); // Jerk controlled speed requires to express speed versus time, NOT steps - uint32_t acceleration_time = (float(cruise_rate - initial_rate) / accel) * (STEPPER_TIMER_RATE), - deceleration_time = (float(cruise_rate - final_rate) / accel) * (STEPPER_TIMER_RATE), + uint32_t acceleration_time = rate_factor * float(cruise_rate - initial_rate), + deceleration_time = rate_factor * float(cruise_rate - final_rate), // And to offload calculations from the ISR, we also calculate the inverse of those times here acceleration_time_inverse = get_period_inverse(acceleration_time), deceleration_time_inverse = get_period_inverse(deceleration_time);