From 55d943e9d2913e0257dbac413ce9d4a9098ce501 Mon Sep 17 00:00:00 2001 From: Tom Brazier Date: Tue, 29 Nov 2022 08:53:47 +0000 Subject: [PATCH] Make it possible to override the shaping buffer size calculations in confg. Also potential SRAM reduction when not using ADAPTIVE_STEP_SMOOTHING on 32 bit MCUs. --- Marlin/Configuration_adv.h | 27 ++++++++++++++------------- Marlin/src/inc/SanityCheck.h | 17 ++++++++++++----- Marlin/src/module/stepper.h | 11 +++++++++-- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 74eb952abe..c1273ca9e4 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1107,14 +1107,13 @@ * * Zero Vibration (ZV) Input Shaping for X and/or Y movements. * - * This option uses a lot of SRAM for the step buffer, which is related to the - * largest step rate possible for the shaped axes. If the build fails due to - * low SRAM the buffer size may be reduced by setting smaller values for - * DEFAULT_AXIS_STEPS_PER_UNIT and/or DEFAULT_MAX_FEEDRATE. Disabling - * ADAPTIVE_STEP_SMOOTHING and reducing the step rate for non-shaped axes may - * also reduce the buffer sizes. Runtime editing of max feedrate (M203) or - * resonant frequency (M593) may result in input shaping losing effectiveness - * during high speed movements to prevent buffer overruns. + * This option uses a lot of SRAM for the step buffer. The buffer size is + * calculated automatically from SHAPING_FREQ_[XY], DEFAULT_AXIS_STEPS_PER_UNIT, + * DEFAULT_MAX_FEEDRATE and ADAPTIVE_STEP_SMOOTHING. The default calculation can + * be overridden by setting SHAPING_MIN_FREQ and/or SHAPING_MAX_FEEDRATE. + * The higher the frequency and the lower the feedrate, the smaller the buffer. + * If the buffer is too small at runtime, input shaping will have reduced + * effectiveness during high speed movements. * * Tune with M593 D F: * @@ -1128,14 +1127,16 @@ //#define INPUT_SHAPING_Y #if EITHER(INPUT_SHAPING_X, INPUT_SHAPING_Y) #if ENABLED(INPUT_SHAPING_X) - #define SHAPING_FREQ_X 40 // (Hz) The default dominant resonant frequency on the X axis. - #define SHAPING_ZETA_X 0.15f // Damping ratio of the X axis (range: 0.0 = no damping to 1.0 = critical damping). + #define SHAPING_FREQ_X 40 // (Hz) The default dominant resonant frequency on the X axis. + #define SHAPING_ZETA_X 0.15f // Damping ratio of the X axis (range: 0.0 = no damping to 1.0 = critical damping). #endif #if ENABLED(INPUT_SHAPING_Y) - #define SHAPING_FREQ_Y 40 // (Hz) The default dominant resonant frequency on the Y axis. - #define SHAPING_ZETA_Y 0.15f // Damping ratio of the Y axis (range: 0.0 = no damping to 1.0 = critical damping). + #define SHAPING_FREQ_Y 40 // (Hz) The default dominant resonant frequency on the Y axis. + #define SHAPING_ZETA_Y 0.15f // Damping ratio of the Y axis (range: 0.0 = no damping to 1.0 = critical damping). #endif - //#define SHAPING_MENU // Add a menu to the LCD to set shaping parameters. + //#define SHAPING_MIN_FREQ 20 // By default the minimum of the shaping frequencies. Override to affect SRAM usage. + //#define SHAPING_MAX_STEPRATE 10000 // By default the maximum total step rate of the shaped axes. Override to affect SRAM usage. + //#define SHAPING_MENU // Add a menu to the LCD to set shaping parameters. #endif #define AXIS_RELATIVE_MODES { false, false, false, false } diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 609d0029a4..f17ff40a34 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -4331,17 +4331,24 @@ static_assert(_PLUS_TEST(4), "HOMING_FEEDRATE_MM_M values must be positive."); // Check requirements for Input Shaping #if HAS_SHAPING && defined(__AVR__) + #ifdef SHAPING_MIN_FREQ + static_assert((SHAPING_MIN_FREQ) > 0, "SHAPING_MIN_FREQ must be > 0."); + #else + TERN_(INPUT_SHAPING_X, static_assert((SHAPING_FREQ_X) > 0, "SHAPING_FREQ_X must be > 0.")); + TERN_(INPUT_SHAPING_Y, static_assert((SHAPING_FREQ_Y) > 0, "SHAPING_FREQ_Y must be > 0.")); + #endif #if ENABLED(INPUT_SHAPING_X) #if F_CPU > 16000000 - static_assert((SHAPING_FREQ_X) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_X is below the minimum (20) for AVR 20MHz."); + static_assert((SHAPING_FREQ_X) == 0 || (SHAPING_FREQ_X) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_X is below the minimum (20) for AVR 20MHz."); #else - static_assert((SHAPING_FREQ_X) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_X is below the minimum (16) for AVR 16MHz."); + static_assert((SHAPING_FREQ_X) == 0 || (SHAPING_FREQ_X) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_X is below the minimum (16) for AVR 16MHz."); #endif - #elif ENABLED(INPUT_SHAPING_Y) + #endif + #if ENABLED(INPUT_SHAPING_Y) #if F_CPU > 16000000 - static_assert((SHAPING_FREQ_Y) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_Y is below the minimum (20) for AVR 20MHz."); + static_assert((SHAPING_FREQ_Y) == 0 || (SHAPING_FREQ_Y) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_Y is below the minimum (20) for AVR 20MHz."); #else - static_assert((SHAPING_FREQ_Y) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_Y is below the minimum (16) for AVR 16MHz."); + static_assert((SHAPING_FREQ_Y) == 0 || (SHAPING_FREQ_Y) * 2 * 0x10000 >= (STEPPER_TIMER_RATE), "SHAPING_FREQ_Y is below the minimum (16) for AVR 16MHz."); #endif #endif #endif diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index 6d9814b4df..731935ee8a 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -334,7 +334,9 @@ constexpr ena_mask_t enable_overlap[] = { constexpr xyze_float_t max_feedrate = DEFAULT_MAX_FEEDRATE; constexpr xyze_float_t steps_per_unit = DEFAULT_AXIS_STEPS_PER_UNIT; // MIN_STEP_ISR_FREQUENCY is known at compile time on AVRs and any reduction in SRAM is welcome - #ifdef __AVR__ + #ifdef SHAPING_MAX_STEPRATE + constexpr float max_step_rate = SHAPING_MAX_STEPRATE; + #elif defined(__AVR__) || !defined(ADAPTIVE_STEP_SMOOTHING) constexpr float max_isr_rate = _MAX( LOGICAL_AXIS_LIST( max_feedrate.e * steps_per_unit.e, @@ -358,7 +360,12 @@ constexpr ena_mask_t enable_overlap[] = { constexpr float max_step_rate = TERN0(INPUT_SHAPING_X, max_feedrate.x * steps_per_unit.x) + TERN0(INPUT_SHAPING_Y, max_feedrate.y * steps_per_unit.y); #endif - constexpr uint16_t shaping_echoes = max_step_rate / _MIN(0x7FFFFFFFL OPTARG(INPUT_SHAPING_X, SHAPING_FREQ_X) OPTARG(INPUT_SHAPING_Y, SHAPING_FREQ_Y)) / 2 + 3; + #ifdef SHAPING_MIN_FREQ + constexpr uint16_t shaping_min_freq = SHAPING_MIN_FREQ; + #else + constexpr uint16_t shaping_min_freq = _MIN(0x7FFFFFFFL OPTARG(INPUT_SHAPING_X, SHAPING_FREQ_X) OPTARG(INPUT_SHAPING_Y, SHAPING_FREQ_Y)); + #endif + constexpr uint16_t shaping_echoes = max_step_rate / shaping_min_freq / 2 + 3; typedef IF::type shaping_time_t; enum shaping_echo_t { ECHO_NONE = 0, ECHO_FWD = 1, ECHO_BWD = 2 };