Browse Source

Fix logic of UBL::fade_scaling_factor_for_z

Scott Lahteine 8 years ago
parent
commit
7310110ec0
4 changed files with 17 additions and 26 deletions
  1. 2
    2
      Marlin/Marlin_main.cpp
  2. 11
    18
      Marlin/UBL.h
  3. 3
    4
      Marlin/UBL_Bed_Leveling.cpp
  4. 1
    2
      Marlin/UBL_G29.cpp

+ 2
- 2
Marlin/Marlin_main.cpp View File

10175
   /**
10175
   /**
10176
    * Prepare a linear move in a Cartesian setup.
10176
    * Prepare a linear move in a Cartesian setup.
10177
    * If Mesh Bed Leveling is enabled, perform a mesh move.
10177
    * If Mesh Bed Leveling is enabled, perform a mesh move.
10178
+   *
10179
+   * Returns true if the caller didn't update current_position.
10178
    */
10180
    */
10179
   inline bool prepare_move_to_destination_cartesian() {
10181
   inline bool prepare_move_to_destination_cartesian() {
10180
     // Do not use feedrate_percentage for E or Z only moves
10182
     // Do not use feedrate_percentage for E or Z only moves
10190
         else
10192
         else
10191
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
10193
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
10192
         if (ubl.state.active) {
10194
         if (ubl.state.active) {
10193
-
10194
           ubl_line_to_destination(MMS_SCALED(feedrate_mm_s), active_extruder);
10195
           ubl_line_to_destination(MMS_SCALED(feedrate_mm_s), active_extruder);
10195
-
10196
           return false;
10196
           return false;
10197
         }
10197
         }
10198
         else
10198
         else

+ 11
- 18
Marlin/UBL.h View File

98
         float g29_correction_fade_height = 10.0,
98
         float g29_correction_fade_height = 10.0,
99
               g29_fade_height_multiplier = 1.0 / 10.0; // It's cheaper to do a floating point multiply than divide,
99
               g29_fade_height_multiplier = 1.0 / 10.0; // It's cheaper to do a floating point multiply than divide,
100
                                                        // so keep this value and its reciprocal.
100
                                                        // so keep this value and its reciprocal.
101
-      #else
102
-        const float g29_correction_fade_height = 10.0,
103
-                    g29_fade_height_multiplier = 1.0 / 10.0;
104
       #endif
101
       #endif
105
 
102
 
106
       // If you change this struct, adjust TOTAL_STRUCT_SIZE
103
       // If you change this struct, adjust TOTAL_STRUCT_SIZE
118
     class unified_bed_leveling {
115
     class unified_bed_leveling {
119
       private:
116
       private:
120
 
117
 
121
-        static float last_specified_z,
122
-                     fade_scaling_factor_for_current_height;
118
+        static float last_specified_z;
123
 
119
 
124
       public:
120
       public:
125
 
121
 
307
         }
303
         }
308
 
304
 
309
         /**
305
         /**
310
-         * This routine is used to scale the Z correction depending upon the current nozzle height. It is
311
-         * optimized for speed. It avoids floating point operations by checking if the requested scaling
312
-         * factor is going to be the same as the last time the function calculated a value. If so, it just
313
-         * returns it.
306
+         * This function sets the Z leveling fade factor based on the given Z height,
307
+         * only re-calculating when necessary.
314
          *
308
          *
315
-         * It returns a scaling factor of 1.0 if UBL is inactive.
316
-         * It returns a scaling factor of 0.0 if Z is past the specified 'Fade Height'
309
+         *  Returns 1.0 if g29_correction_fade_height is 0.0.
310
+         *  Returns 0.0 if Z is past the specified 'Fade Height'.
317
          */
311
          */
318
         #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
312
         #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
319
 
313
 
320
           static FORCE_INLINE float fade_scaling_factor_for_z(const float &lz) {
314
           static FORCE_INLINE float fade_scaling_factor_for_z(const float &lz) {
315
+            if (state.g29_correction_fade_height == 0.0) return 1.0;
316
+
317
+            static float fade_scaling_factor = 1.0;
321
             const float rz = RAW_Z_POSITION(lz);
318
             const float rz = RAW_Z_POSITION(lz);
322
             if (last_specified_z != rz) {
319
             if (last_specified_z != rz) {
323
               last_specified_z = rz;
320
               last_specified_z = rz;
324
-              fade_scaling_factor_for_current_height =
325
-                state.active && rz < state.g29_correction_fade_height
321
+              fade_scaling_factor =
322
+                rz < state.g29_correction_fade_height
326
                   ? 1.0 - (rz * state.g29_fade_height_multiplier)
323
                   ? 1.0 - (rz * state.g29_fade_height_multiplier)
327
                   : 0.0;
324
                   : 0.0;
328
             }
325
             }
329
-            return fade_scaling_factor_for_current_height;
326
+            return fade_scaling_factor;
330
           }
327
           }
331
 
328
 
332
-        #else
333
-
334
-          static constexpr float fade_scaling_factor_for_z(const float &lz) { UNUSED(lz); return 1.0; }
335
-
336
         #endif
329
         #endif
337
 
330
 
338
     }; // class unified_bed_leveling
331
     }; // class unified_bed_leveling

+ 3
- 4
Marlin/UBL_Bed_Leveling.cpp View File

61
 
61
 
62
   float unified_bed_leveling::z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS],
62
   float unified_bed_leveling::z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS],
63
         unified_bed_leveling::last_specified_z,
63
         unified_bed_leveling::last_specified_z,
64
-        unified_bed_leveling::fade_scaling_factor_for_current_height,
65
         unified_bed_leveling::mesh_index_to_xpos[UBL_MESH_NUM_X_POINTS + 1], // +1 safety margin for now, until determinism prevails
64
         unified_bed_leveling::mesh_index_to_xpos[UBL_MESH_NUM_X_POINTS + 1], // +1 safety margin for now, until determinism prevails
66
         unified_bed_leveling::mesh_index_to_ypos[UBL_MESH_NUM_Y_POINTS + 1];
65
         unified_bed_leveling::mesh_index_to_ypos[UBL_MESH_NUM_Y_POINTS + 1];
67
 
66
 
102
        * updated, but until then, we try to ease the transition
101
        * updated, but until then, we try to ease the transition
103
        * for our Beta testers.
102
        * for our Beta testers.
104
        */
103
        */
105
-      if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) {
106
-        ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
104
+      const float recip = ubl.state.g29_correction_fade_height ? 1.0 / ubl.state.g29_correction_fade_height : 1.0;
105
+      if (ubl.state.g29_fade_height_multiplier != recip) {
106
+        ubl.state.g29_fade_height_multiplier = recip;
107
         store_state();
107
         store_state();
108
       }
108
       }
109
     #endif
109
     #endif
160
     ZERO(z_values);
160
     ZERO(z_values);
161
 
161
 
162
     last_specified_z = -999.9;
162
     last_specified_z = -999.9;
163
-    fade_scaling_factor_for_current_height = 0.0;
164
   }
163
   }
165
 
164
 
166
   void unified_bed_leveling::invalidate() {
165
   void unified_bed_leveling::invalidate() {

+ 1
- 2
Marlin/UBL_G29.cpp View File

1132
     safe_delay(50);
1132
     safe_delay(50);
1133
 
1133
 
1134
     #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1134
     #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1135
-      SERIAL_PROTOCOLPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
1136
-      SERIAL_EOL;
1135
+      SERIAL_PROTOCOLLNPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
1137
     #endif
1136
     #endif
1138
 
1137
 
1139
     SERIAL_PROTOCOLPGM("z_offset: ");
1138
     SERIAL_PROTOCOLPGM("z_offset: ");

Loading…
Cancel
Save