From 7310110ec0fa49fd014506cc1d0672c62cbbeb30 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 1 Apr 2017 22:29:35 -0500 Subject: [PATCH] Fix logic of UBL::fade_scaling_factor_for_z --- Marlin/Marlin_main.cpp | 4 ++-- Marlin/UBL.h | 29 +++++++++++------------------ Marlin/UBL_Bed_Leveling.cpp | 7 +++---- Marlin/UBL_G29.cpp | 3 +-- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 7d4a95506..322448e01 100755 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -10175,6 +10175,8 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { /** * Prepare a linear move in a Cartesian setup. * If Mesh Bed Leveling is enabled, perform a mesh move. + * + * Returns true if the caller didn't update current_position. */ inline bool prepare_move_to_destination_cartesian() { // Do not use feedrate_percentage for E or Z only moves @@ -10190,9 +10192,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { else #elif ENABLED(AUTO_BED_LEVELING_UBL) if (ubl.state.active) { - ubl_line_to_destination(MMS_SCALED(feedrate_mm_s), active_extruder); - return false; } else diff --git a/Marlin/UBL.h b/Marlin/UBL.h index ea726b457..f96d23e66 100644 --- a/Marlin/UBL.h +++ b/Marlin/UBL.h @@ -98,9 +98,6 @@ float g29_correction_fade_height = 10.0, g29_fade_height_multiplier = 1.0 / 10.0; // It's cheaper to do a floating point multiply than divide, // so keep this value and its reciprocal. - #else - const float g29_correction_fade_height = 10.0, - g29_fade_height_multiplier = 1.0 / 10.0; #endif // If you change this struct, adjust TOTAL_STRUCT_SIZE @@ -118,8 +115,7 @@ class unified_bed_leveling { private: - static float last_specified_z, - fade_scaling_factor_for_current_height; + static float last_specified_z; public: @@ -307,32 +303,29 @@ } /** - * This routine is used to scale the Z correction depending upon the current nozzle height. It is - * optimized for speed. It avoids floating point operations by checking if the requested scaling - * factor is going to be the same as the last time the function calculated a value. If so, it just - * returns it. + * This function sets the Z leveling fade factor based on the given Z height, + * only re-calculating when necessary. * - * It returns a scaling factor of 1.0 if UBL is inactive. - * It returns a scaling factor of 0.0 if Z is past the specified 'Fade Height' + * Returns 1.0 if g29_correction_fade_height is 0.0. + * Returns 0.0 if Z is past the specified 'Fade Height'. */ #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) static FORCE_INLINE float fade_scaling_factor_for_z(const float &lz) { + if (state.g29_correction_fade_height == 0.0) return 1.0; + + static float fade_scaling_factor = 1.0; const float rz = RAW_Z_POSITION(lz); if (last_specified_z != rz) { last_specified_z = rz; - fade_scaling_factor_for_current_height = - state.active && rz < state.g29_correction_fade_height + fade_scaling_factor = + rz < state.g29_correction_fade_height ? 1.0 - (rz * state.g29_fade_height_multiplier) : 0.0; } - return fade_scaling_factor_for_current_height; + return fade_scaling_factor; } - #else - - static constexpr float fade_scaling_factor_for_z(const float &lz) { UNUSED(lz); return 1.0; } - #endif }; // class unified_bed_leveling diff --git a/Marlin/UBL_Bed_Leveling.cpp b/Marlin/UBL_Bed_Leveling.cpp index e173247fe..414099b76 100644 --- a/Marlin/UBL_Bed_Leveling.cpp +++ b/Marlin/UBL_Bed_Leveling.cpp @@ -61,7 +61,6 @@ float unified_bed_leveling::z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS], unified_bed_leveling::last_specified_z, - unified_bed_leveling::fade_scaling_factor_for_current_height, unified_bed_leveling::mesh_index_to_xpos[UBL_MESH_NUM_X_POINTS + 1], // +1 safety margin for now, until determinism prevails unified_bed_leveling::mesh_index_to_ypos[UBL_MESH_NUM_Y_POINTS + 1]; @@ -102,8 +101,9 @@ * updated, but until then, we try to ease the transition * for our Beta testers. */ - if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) { - ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height; + const float recip = ubl.state.g29_correction_fade_height ? 1.0 / ubl.state.g29_correction_fade_height : 1.0; + if (ubl.state.g29_fade_height_multiplier != recip) { + ubl.state.g29_fade_height_multiplier = recip; store_state(); } #endif @@ -160,7 +160,6 @@ ZERO(z_values); last_specified_z = -999.9; - fade_scaling_factor_for_current_height = 0.0; } void unified_bed_leveling::invalidate() { diff --git a/Marlin/UBL_G29.cpp b/Marlin/UBL_G29.cpp index 9b7ccadd9..9512f61b2 100644 --- a/Marlin/UBL_G29.cpp +++ b/Marlin/UBL_G29.cpp @@ -1132,8 +1132,7 @@ safe_delay(50); #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - SERIAL_PROTOCOLPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height); - SERIAL_EOL; + SERIAL_PROTOCOLLNPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height); #endif SERIAL_PROTOCOLPGM("z_offset: ");