Prusa MINI Firmware overview
unified_bed_leveling Class Reference

#include <ubl.h>

Collaboration diagram for unified_bed_leveling:

Public Member Functions

 unified_bed_leveling ()
 

Static Public Member Functions

static void echo_name ()
 
static void report_current_mesh ()
 
static void report_state ()
 
static void save_ubl_active_state_and_disable ()
 
static void restore_ubl_active_state_and_leave ()
 
static void display_map (const int) _O0
 
static mesh_index_pair find_closest_mesh_point_of_type (const MeshPointType, const xy_pos_t &, const bool=false, MeshFlags *done_flags=nullptr) _O0
 
static mesh_index_pair find_furthest_invalid_mesh_point () _O0
 
static void reset ()
 
static void invalidate ()
 
static void set_all_mesh_points_to_value (const float value)
 
static void adjust_mesh_to_mean (const bool cflag, const float value)
 
static bool sanity_check ()
 
static void G29 () _O0
 
static void smart_fill_wlsf (const float &) _O2
 
static FORCE_INLINE void set_z (const int8_t px, const int8_t py, const float &z)
 
static int8_t cell_index_x (const float &x)
 
static int8_t cell_index_y (const float &y)
 
static xy_int8_t cell_indexes (const float &x, const float &y)
 
static xy_int8_t cell_indexes (const xy_pos_t &xy)
 
static int8_t closest_x_index (const float &x)
 
static int8_t closest_y_index (const float &y)
 
static xy_int8_t closest_indexes (const xy_pos_t &xy)
 
static FORCE_INLINE float calc_z0 (const float &a0, const float &a1, const float &z1, const float &a2, const float &z2)
 
static float z_correction_for_x_on_horizontal_mesh_line (const float &rx0, const int x1_i, const int yi)
 
static float z_correction_for_y_on_vertical_mesh_line (const float &ry0, const int xi, const int y1_i)
 
static float get_z_correction (const float &rx0, const float &ry0)
 
static float get_z_correction (const xy_pos_t &pos)
 
static float mesh_index_to_xpos (const uint8_t i)
 
static float mesh_index_to_ypos (const uint8_t i)
 
static void line_to_destination_cartesian (const feedRate_t &scaled_fr_mm_s, const uint8_t e)
 
static bool mesh_is_valid ()
 

Static Public Attributes

static int8_t storage_slot
 
static bed_mesh_t z_values
 
static const float _mesh_index_to_xpos [GRID_MAX_POINTS_X]
 
static const float _mesh_index_to_ypos [GRID_MAX_POINTS_Y]
 
static volatile int encoder_diff
 

Constructor & Destructor Documentation

◆ unified_bed_leveling()

unified_bed_leveling::unified_bed_leveling ( )

Member Function Documentation

◆ echo_name()

static void unified_bed_leveling::echo_name ( )
static

◆ report_current_mesh()

static void unified_bed_leveling::report_current_mesh ( )
static

◆ report_state()

static void unified_bed_leveling::report_state ( )
static

◆ save_ubl_active_state_and_disable()

static void unified_bed_leveling::save_ubl_active_state_and_disable ( )
static

◆ restore_ubl_active_state_and_leave()

static void unified_bed_leveling::restore_ubl_active_state_and_leave ( )
static

◆ display_map()

static void unified_bed_leveling::display_map ( const int  )
static

◆ find_closest_mesh_point_of_type()

static mesh_index_pair unified_bed_leveling::find_closest_mesh_point_of_type ( const  MeshPointType,
const xy_pos_t ,
const bool  = false,
MeshFlags *  done_flags = nullptr 
)
static

◆ find_furthest_invalid_mesh_point()

static mesh_index_pair unified_bed_leveling::find_furthest_invalid_mesh_point ( )
static

◆ reset()

static void unified_bed_leveling::reset ( )
static

◆ invalidate()

static void unified_bed_leveling::invalidate ( )
static

◆ set_all_mesh_points_to_value()

static void unified_bed_leveling::set_all_mesh_points_to_value ( const float  value)
static

◆ adjust_mesh_to_mean()

static void unified_bed_leveling::adjust_mesh_to_mean ( const bool  cflag,
const float  value 
)
static

◆ sanity_check()

static bool unified_bed_leveling::sanity_check ( )
static

◆ G29()

static void unified_bed_leveling::G29 ( )
static

◆ smart_fill_wlsf()

static void unified_bed_leveling::smart_fill_wlsf ( const float &  )
static

◆ set_z()

static FORCE_INLINE void unified_bed_leveling::set_z ( const int8_t  px,
const int8_t  py,
const float &  z 
)
static
120 { z_values[px][py] = z; }

◆ cell_index_x()

static int8_t unified_bed_leveling::cell_index_x ( const float &  x)
static
122  {
123  const int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
124  return constrain(cx, 0, (GRID_MAX_POINTS_X) - 1); // -1 is appropriate if we want all movement to the X_MAX
125  } // position. But with this defined this way, it is possible
Here is the caller graph for this function:

◆ cell_index_y()

static int8_t unified_bed_leveling::cell_index_y ( const float &  y)
static
129  {
130  const int8_t cy = (y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST);
131  return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 1); // -1 is appropriate if we want all movement to the Y_MAX
132  } // position. But with this defined this way, it is possible
Here is the caller graph for this function:

◆ cell_indexes() [1/2]

static xy_int8_t unified_bed_leveling::cell_indexes ( const float &  x,
const float &  y 
)
static
137  {
138  return { cell_index_x(x), cell_index_y(y) };
139  }
Here is the call graph for this function:

◆ cell_indexes() [2/2]

static xy_int8_t unified_bed_leveling::cell_indexes ( const xy_pos_t xy)
static
140 { return cell_indexes(xy.x, xy.y); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ closest_x_index()

static int8_t unified_bed_leveling::closest_x_index ( const float &  x)
static
142  {
143  const int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * RECIPROCAL(MESH_X_DIST);
144  return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
145  }
Here is the caller graph for this function:

◆ closest_y_index()

static int8_t unified_bed_leveling::closest_y_index ( const float &  y)
static
146  {
147  const int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * RECIPROCAL(MESH_Y_DIST);
148  return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
149  }
Here is the caller graph for this function:

◆ closest_indexes()

static xy_int8_t unified_bed_leveling::closest_indexes ( const xy_pos_t xy)
static
150  {
151  return { closest_x_index(xy.x), closest_y_index(xy.y) };
152  }
Here is the call graph for this function:

◆ calc_z0()

static FORCE_INLINE float unified_bed_leveling::calc_z0 ( const float &  a0,
const float &  a1,
const float &  z1,
const float &  a2,
const float &  z2 
)
static
                      z2   --|
            z0        |      |
             |        |      + (z2-z1)

z1 | | | —+----------—+-----—+– –| a1 a0 a2 |<—delta_a-------—>|

calc_z0 is the basis for all the Mesh Based correction. It is used to find the expected Z Height at a position between two known Z-Height locations.

It is fairly expensive with its 4 floating point additions and 2 floating point multiplications.

169  {
170  return z1 + (z2 - z1) * (a0 - a1) / (a2 - a1);
171  }
Here is the caller graph for this function:

◆ z_correction_for_x_on_horizontal_mesh_line()

static float unified_bed_leveling::z_correction_for_x_on_horizontal_mesh_line ( const float &  rx0,
const int  x1_i,
const int  yi 
)
static

z_correction_for_x_on_horizontal_mesh_line is an optimization for the case where the printer is making a vertical line that only crosses horizontal mesh lines.

177  {
178  if (!WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(yi, 0, GRID_MAX_POINTS_Y - 1)) {
179 
180  if (DEBUGGING(LEVELING)) {
181  if (WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1)) DEBUG_ECHOPGM("yi"); else DEBUG_ECHOPGM("x1_i");
182  DEBUG_ECHOLNPAIR(" out of bounds in z_correction_for_x_on_horizontal_mesh_line(rx0=", rx0, ",x1_i=", x1_i, ",yi=", yi, ")");
183  }
184 
185  // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
186  return (
187  #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
188  UBL_Z_RAISE_WHEN_OFF_MESH
189  #else
190  NAN
191  #endif
192  );
193  }
194 
195  const float xratio = (rx0 - mesh_index_to_xpos(x1_i)) * RECIPROCAL(MESH_X_DIST),
196  z1 = z_values[x1_i][yi];
197 
198  return z1 + xratio * (z_values[_MIN(x1_i, GRID_MAX_POINTS_X - 2) + 1][yi] - z1); // Don't allow x1_i+1 to be past the end of the array
199  // If it is, it is clamped to the last element of the
200  // z_values[][] array and no correction is applied.
201  }
Here is the call graph for this function:

◆ z_correction_for_y_on_vertical_mesh_line()

static float unified_bed_leveling::z_correction_for_y_on_vertical_mesh_line ( const float &  ry0,
const int  xi,
const int  y1_i 
)
static
206  {
207  if (!WITHIN(xi, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(y1_i, 0, GRID_MAX_POINTS_Y - 1)) {
208 
209  if (DEBUGGING(LEVELING)) {
210  if (WITHIN(xi, 0, GRID_MAX_POINTS_X - 1)) DEBUG_ECHOPGM("y1_i"); else DEBUG_ECHOPGM("xi");
211  DEBUG_ECHOLNPAIR(" out of bounds in z_correction_for_y_on_vertical_mesh_line(ry0=", ry0, ", xi=", xi, ", y1_i=", y1_i, ")");
212  }
213 
214  // The requested location is off the mesh. Return UBL_Z_RAISE_WHEN_OFF_MESH or NAN.
215  return (
216  #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
217  UBL_Z_RAISE_WHEN_OFF_MESH
218  #else
219  NAN
220  #endif
221  );
222  }
223 
224  const float yratio = (ry0 - mesh_index_to_ypos(y1_i)) * RECIPROCAL(MESH_Y_DIST),
225  z1 = z_values[xi][y1_i];
226 
227  return z1 + yratio * (z_values[xi][_MIN(y1_i, GRID_MAX_POINTS_Y - 2) + 1] - z1); // Don't allow y1_i+1 to be past the end of the array
228  // If it is, it is clamped to the last element of the
229  // z_values[][] array and no correction is applied.
230  }
Here is the call graph for this function:

◆ get_z_correction() [1/2]

static float unified_bed_leveling::get_z_correction ( const float &  rx0,
const float &  ry0 
)
static

This is the generic Z-Correction. It works anywhere within a Mesh Cell. It first does a linear interpolation along both of the bounding X-Mesh-Lines to find the Z-Height at both ends. Then it does a linear interpolation of these heights based on the Y position within the cell.

Check if the requested location is off the mesh. If so, and UBL_Z_RAISE_WHEN_OFF_MESH is specified, that value is returned.

238  {
239  const int8_t cx = cell_index_x(rx0), cy = cell_index_y(ry0); // return values are clamped
240 
241  /**
242  * Check if the requested location is off the mesh. If so, and
243  * UBL_Z_RAISE_WHEN_OFF_MESH is specified, that value is returned.
244  */
245  #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
246  if (!WITHIN(rx0, MESH_MIN_X, MESH_MAX_X) || !WITHIN(ry0, MESH_MIN_Y, MESH_MAX_Y))
247  return UBL_Z_RAISE_WHEN_OFF_MESH;
248  #endif
249 
250  const float z1 = calc_z0(rx0,
251  mesh_index_to_xpos(cx), z_values[cx][cy],
252  mesh_index_to_xpos(cx + 1), z_values[_MIN(cx, GRID_MAX_POINTS_X - 2) + 1][cy]);
253 
254  const float z2 = calc_z0(rx0,
255  mesh_index_to_xpos(cx), z_values[cx][_MIN(cy, GRID_MAX_POINTS_Y - 2) + 1],
256  mesh_index_to_xpos(cx + 1), z_values[_MIN(cx, GRID_MAX_POINTS_X - 2) + 1][_MIN(cy, GRID_MAX_POINTS_Y - 2) + 1]);
257 
258  float z0 = calc_z0(ry0,
259  mesh_index_to_ypos(cy), z1,
260  mesh_index_to_ypos(cy + 1), z2);
261 
262  if (DEBUGGING(MESH_ADJUST)) {
263  DEBUG_ECHOPAIR(" raw get_z_correction(", rx0);
264  DEBUG_CHAR(','); DEBUG_ECHO(ry0);
265  DEBUG_ECHOPAIR_F(") = ", z0, 6);
266  DEBUG_ECHOLNPAIR_F(" >>>---> ", z0, 6);
267  }
268 
269  if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
270  z0 = 0.0; // in ubl.z_values[][] and propagate through the
271  // calculations. If our correction is NAN, we throw it out
272  // because part of the Mesh is undefined and we don't have the
273  // information we need to complete the height correction.
274 
275  if (DEBUGGING(MESH_ADJUST)) {
276  DEBUG_ECHOPAIR("??? Yikes! NAN in get_z_correction(", rx0);
277  DEBUG_CHAR(',');
278  DEBUG_ECHO(ry0);
279  DEBUG_CHAR(')');
280  DEBUG_EOL();
281  }
282  }
283  return z0;
284  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_z_correction() [2/2]

static float unified_bed_leveling::get_z_correction ( const xy_pos_t pos)
static
285 { return get_z_correction(pos.x, pos.y); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mesh_index_to_xpos()

static float unified_bed_leveling::mesh_index_to_xpos ( const uint8_t  i)
static
287  {
288  return i < GRID_MAX_POINTS_X ? pgm_read_float(&_mesh_index_to_xpos[i]) : MESH_MIN_X + i * (MESH_X_DIST);
289  }
Here is the caller graph for this function:

◆ mesh_index_to_ypos()

static float unified_bed_leveling::mesh_index_to_ypos ( const uint8_t  i)
static
290  {
291  return i < GRID_MAX_POINTS_Y ? pgm_read_float(&_mesh_index_to_ypos[i]) : MESH_MIN_Y + i * (MESH_Y_DIST);
292  }
Here is the caller graph for this function:

◆ line_to_destination_cartesian()

static void unified_bed_leveling::line_to_destination_cartesian ( const feedRate_t scaled_fr_mm_s,
const uint8_t  e 
)
static
Here is the caller graph for this function:

◆ mesh_is_valid()

static bool unified_bed_leveling::mesh_is_valid ( )
static
300  {
301  for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
302  for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
303  if (isnan(z_values[x][y])) return false;
304  return true;
305  }

Member Data Documentation

◆ storage_slot

int8_t unified_bed_leveling::storage_slot
static

◆ z_values

bed_mesh_t unified_bed_leveling::z_values
static

◆ _mesh_index_to_xpos

const float unified_bed_leveling::_mesh_index_to_xpos[GRID_MAX_POINTS_X]
static

◆ _mesh_index_to_ypos

const float unified_bed_leveling::_mesh_index_to_ypos[GRID_MAX_POINTS_Y]
static

◆ encoder_diff

volatile int unified_bed_leveling::encoder_diff
static
WITHIN
#define WITHIN(N, L, H)
Definition: macros.h:195
unified_bed_leveling::mesh_index_to_ypos
static float mesh_index_to_ypos(const uint8_t i)
Definition: ubl.h:290
RECIPROCAL
#define RECIPROCAL(x)
Definition: macros.h:273
DEBUG_EOL
#define DEBUG_EOL()
Definition: debug_out.h:86
i
uint8_t i
Definition: screen_test_graph.c:72
_MIN
#define _MIN(V...)
Definition: macros.h:333
DEBUG_ECHOLNPAIR
#define DEBUG_ECHOLNPAIR(...)
Definition: debug_out.h:82
unified_bed_leveling::mesh_index_to_xpos
static float mesh_index_to_xpos(const uint8_t i)
Definition: ubl.h:287
unified_bed_leveling::closest_x_index
static int8_t closest_x_index(const float &x)
Definition: ubl.h:142
DEBUG_ECHOPGM
#define DEBUG_ECHOPGM(...)
Definition: debug_out.h:78
unified_bed_leveling::calc_z0
static FORCE_INLINE float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2)
Definition: ubl.h:169
MESH_Y_DIST
#define MESH_Y_DIST
Definition: ubl.h:42
DEBUG_ECHOLNPAIR_F
#define DEBUG_ECHOLNPAIR_F(...)
Definition: debug_out.h:83
MESH_X_DIST
#define MESH_X_DIST
Definition: ubl.h:41
unified_bed_leveling::_mesh_index_to_xpos
static const float _mesh_index_to_xpos[GRID_MAX_POINTS_X]
Definition: ubl.h:109
unified_bed_leveling::_mesh_index_to_ypos
static const float _mesh_index_to_ypos[GRID_MAX_POINTS_Y]
Definition: ubl.h:109
unified_bed_leveling::cell_index_x
static int8_t cell_index_x(const float &x)
Definition: ubl.h:122
isnan
#define isnan
Definition: Arduino.h:55
unified_bed_leveling::cell_indexes
static xy_int8_t cell_indexes(const float &x, const float &y)
Definition: ubl.h:137
constrain
#define constrain(amt, low, high)
Definition: wiring_constants.h:79
pgm_read_float
#define pgm_read_float(addr)
Definition: pgmspace.h:109
unified_bed_leveling::get_z_correction
static float get_z_correction(const float &rx0, const float &ry0)
Definition: ubl.h:238
XYval::x
T x
Definition: types.h:185
uint8_t
const uint8_t[]
Definition: 404_html.c:3
unified_bed_leveling::closest_y_index
static int8_t closest_y_index(const float &y)
Definition: ubl.h:146
DEBUG_ECHO
#define DEBUG_ECHO(...)
Definition: debug_out.h:75
DEBUG_ECHOPAIR_F
#define DEBUG_ECHOPAIR_F(...)
Definition: debug_out.h:81
DEBUGGING
#define DEBUGGING(F)
Definition: serial.h:47
DEBUG_ECHOPAIR
#define DEBUG_ECHOPAIR(...)
Definition: debug_out.h:80
XYval::y
T y
Definition: types.h:185
DEBUG_CHAR
#define DEBUG_CHAR(...)
Definition: debug_out.h:74
unified_bed_leveling::cell_index_y
static int8_t cell_index_y(const float &y)
Definition: ubl.h:129
unified_bed_leveling::z_values
static bed_mesh_t z_values
Definition: ubl.h:108