From ca1e47375c71e6ddd4973a63a7a8cbf125a439bd Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 13 Oct 2017 10:39:11 -0500 Subject: [PATCH] Move fade_scaling_factor_for_z to Planner --- Marlin/src/feature/bedlevel/bedlevel.cpp | 24 ++++------ Marlin/src/feature/bedlevel/ubl/ubl.h | 25 ---------- .../src/feature/bedlevel/ubl/ubl_motion.cpp | 12 ++--- Marlin/src/module/planner.cpp | 48 +++++++------------ Marlin/src/module/planner.h | 32 +++++++++++++ 5 files changed, 65 insertions(+), 76 deletions(-) diff --git a/Marlin/src/feature/bedlevel/bedlevel.cpp b/Marlin/src/feature/bedlevel/bedlevel.cpp index e8a123c6b6..7be24d6ecd 100644 --- a/Marlin/src/feature/bedlevel/bedlevel.cpp +++ b/Marlin/src/feature/bedlevel/bedlevel.cpp @@ -134,20 +134,16 @@ void set_bed_leveling_enabled(const bool enable/*=true*/) { const bool level_active = LEVELING_IS_ACTIVE(); #if ENABLED(AUTO_BED_LEVELING_UBL) + if (level_active) set_bed_leveling_enabled(false); // turn off before changing fade height for proper apply/unapply leveling to maintain current_position + #endif - if (level_active) - set_bed_leveling_enabled(false); // turn off before changing fade height for proper apply/unapply leveling to maintain current_position - planner.z_fade_height = zfh; - planner.inverse_z_fade_height = RECIPROCAL(zfh); - if (level_active) + planner.z_fade_height = zfh; + planner.inverse_z_fade_height = RECIPROCAL(zfh); + + if (level_active) { + #if ENABLED(AUTO_BED_LEVELING_UBL) set_bed_leveling_enabled(true); // turn back on after changing fade height - - #else - - planner.z_fade_height = zfh; - planner.inverse_z_fade_height = RECIPROCAL(zfh); - - if (level_active) { + #else set_current_from_steppers_for_axis( #if ABL_PLANAR ALL_AXES @@ -155,8 +151,8 @@ void set_bed_leveling_enabled(const bool enable/*=true*/) { Z_AXIS #endif ); - } - #endif + #endif + } } #endif // ENABLE_LEVELING_FADE_HEIGHT diff --git a/Marlin/src/feature/bedlevel/ubl/ubl.h b/Marlin/src/feature/bedlevel/ubl/ubl.h index 1a83d7f17b..19bc8633e9 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl.h +++ b/Marlin/src/feature/bedlevel/ubl/ubl.h @@ -367,31 +367,6 @@ class unified_bed_leveling { return z0; } - /** - * This function sets the Z leveling fade factor based on the given Z height, - * only re-calculating when necessary. - * - * Returns 1.0 if planner.z_fade_height is 0.0. - * Returns 0.0 if Z is past the specified 'Fade Height'. - */ - #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - static inline float fade_scaling_factor_for_z(const float &lz) { - if (planner.z_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 = - rz < planner.z_fade_height - ? 1.0 - (rz * planner.inverse_z_fade_height) - : 0.0; - } - return fade_scaling_factor; - } - #else - FORCE_INLINE static float fade_scaling_factor_for_z(const float &lz) { return 1.0; } - #endif - FORCE_INLINE static float mesh_index_to_xpos(const uint8_t i) { return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : UBL_MESH_MIN_X + i * (MESH_X_DIST); } diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp index 1fa97b43c2..c2a74e8282 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp @@ -173,7 +173,7 @@ // are going to apply the Y-Distance into the cell to interpolate the final Z correction. const float yratio = (RAW_Y_POSITION(end[Y_AXIS]) - mesh_index_to_ypos(cell_dest_yi)) * (1.0 / (MESH_Y_DIST)); - float z0 = cell_dest_yi < GRID_MAX_POINTS_Y - 1 ? (z1 + (z2 - z1) * yratio) * fade_scaling_factor_for_z(end[Z_AXIS]) : 0.0; + float z0 = cell_dest_yi < GRID_MAX_POINTS_Y - 1 ? (z1 + (z2 - z1) * yratio) * planner.fade_scaling_factor_for_z(end[Z_AXIS]) : 0.0; /** * If part of the Mesh is undefined, it will show up as NAN @@ -259,7 +259,7 @@ float z0 = z_correction_for_x_on_horizontal_mesh_line(x, current_xi, current_yi); - z0 *= fade_scaling_factor_for_z(end[Z_AXIS]); + z0 *= planner.fade_scaling_factor_for_z(end[Z_AXIS]); /** * If part of the Mesh is undefined, it will show up as NAN @@ -324,7 +324,7 @@ float z0 = z_correction_for_y_on_vertical_mesh_line(y, current_xi, current_yi); - z0 *= fade_scaling_factor_for_z(end[Z_AXIS]); + z0 *= planner.fade_scaling_factor_for_z(end[Z_AXIS]); /** * If part of the Mesh is undefined, it will show up as NAN @@ -397,7 +397,7 @@ // Yes! Crossing a Y Mesh Line next float z0 = z_correction_for_x_on_horizontal_mesh_line(x, current_xi - left_flag, current_yi + dyi); - z0 *= fade_scaling_factor_for_z(end[Z_AXIS]); + z0 *= planner.fade_scaling_factor_for_z(end[Z_AXIS]); /** * If part of the Mesh is undefined, it will show up as NAN @@ -425,7 +425,7 @@ // Yes! Crossing a X Mesh Line next float z0 = z_correction_for_y_on_vertical_mesh_line(y, current_xi + dxi, current_yi - down_flag); - z0 *= fade_scaling_factor_for_z(end[Z_AXIS]); + z0 *= planner.fade_scaling_factor_for_z(end[Z_AXIS]); /** * If part of the Mesh is undefined, it will show up as NAN @@ -616,7 +616,7 @@ // Otherwise perform per-segment leveling #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - const float fade_scaling_factor = fade_scaling_factor_for_z(ltarget[Z_AXIS]); + const float fade_scaling_factor = planner.fade_scaling_factor_for_z(ltarget[Z_AXIS]); #endif // increment to first segment destination diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index a7fed29e15..57bd730f20 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -131,7 +131,8 @@ float Planner::min_feedrate_mm_s, #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) float Planner::z_fade_height, // Initialized by settings.load() - Planner::inverse_z_fade_height; + Planner::inverse_z_fade_height, + Planner::last_raw_lz; #endif #if ENABLED(AUTOTEMP) @@ -557,40 +558,29 @@ void Planner::calculate_volumetric_multipliers() { if (!LEVELING_IS_ACTIVE()) return; - #if ENABLED(AUTO_BED_LEVELING_UBL) - #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - // if z_fade_height enabled (nonzero) and raw_z above it, no leveling required - if (planner.z_fade_height && planner.z_fade_height <= RAW_Z_POSITION(lz)) return; - lz += ubl.get_z_correction(lx, ly) * ubl.fade_scaling_factor_for_z(lz); - #else // no fade - lz += ubl.get_z_correction(lx, ly); - #endif // FADE - #endif // UBL - - #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) && DISABLED(AUTO_BED_LEVELING_UBL) - static float z_fade_factor = 1.0, last_raw_lz = -999.0; - if (z_fade_height) { - const float raw_lz = RAW_Z_POSITION(lz); - if (raw_lz >= z_fade_height) return; - if (last_raw_lz != raw_lz) { - last_raw_lz = raw_lz; - z_fade_factor = 1.0 - raw_lz * inverse_z_fade_height; - } - } - else - z_fade_factor = 1.0; + #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + const float fade_scaling_factor = fade_scaling_factor_for_z(lz); + if (!fade_scaling_factor) return; + #else + constexpr float fade_scaling_factor = 1.0; #endif - #if ENABLED(MESH_BED_LEVELING) + #if ENABLED(AUTO_BED_LEVELING_UBL) + + lz += ubl.get_z_correction(lx, ly) * fade_scaling_factor; + + #elif ENABLED(MESH_BED_LEVELING) lz += mbl.get_z(RAW_X_POSITION(lx), RAW_Y_POSITION(ly) #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - , z_fade_factor + , fade_scaling_factor #endif - ); + ); #elif ABL_PLANAR + UNUSED(fade_scaling_factor); + float dx = RAW_X_POSITION(lx) - (X_TILT_FULCRUM), dy = RAW_Y_POSITION(ly) - (Y_TILT_FULCRUM), dz = RAW_Z_POSITION(lz); @@ -604,11 +594,7 @@ void Planner::calculate_volumetric_multipliers() { #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) float tmp[XYZ] = { lx, ly, 0 }; - lz += bilinear_z_offset(tmp) - #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - * z_fade_factor - #endif - ; + lz += bilinear_z_offset(tmp) * fade_scaling_factor; #endif } diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index 85f35f35b8..3d7ec53e00 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -263,6 +263,38 @@ class Planner { if (!filament_size[i]) filament_size[i] = DEFAULT_NOMINAL_FILAMENT_DIA; } + #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + + /** + * Get the Z leveling fade factor based on the given Z height, + * re-calculating only when needed. + * + * Returns 1.0 if planner.z_fade_height is 0.0. + * Returns 0.0 if Z is past the specified 'Fade Height'. + */ + inline static float fade_scaling_factor_for_z(const float &lz) { + static float z_fade_factor = 1.0, last_raw_lz = -999.0; + if (z_fade_height) { + const float raw_lz = RAW_Z_POSITION(lz); + if (raw_lz >= z_fade_height) return 0.0; + if (last_raw_lz != raw_lz) { + last_raw_lz = raw_lz; + z_fade_factor = 1.0 - raw_lz * inverse_z_fade_height; + } + return z_fade_factor; + } + return 1.0; + } + + #else + + FORCE_INLINE static float fade_scaling_factor_for_z(const float &lz) { + UNUSED(lz); + return 1.0; + } + + #endif + #if PLANNER_LEVELING #define ARG_X float lx