Prusa MINI Firmware overview
mesh_bed_leveling.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 #include "../../../inc/MarlinConfig.h"
25 
26 enum MeshLevelingState : char {
27  MeshReport, // G29 S0
28  MeshStart, // G29 S1
29  MeshNext, // G29 S2
30  MeshSet, // G29 S3
31  MeshSetZOffset, // G29 S4
32  MeshReset // G29 S5
33 };
34 
35 #define MESH_X_DIST (float(MESH_MAX_X - (MESH_MIN_X)) / float(GRID_MAX_POINTS_X - 1))
36 #define MESH_Y_DIST (float(MESH_MAX_Y - (MESH_MIN_Y)) / float(GRID_MAX_POINTS_Y - 1))
37 #define _GET_MESH_X(I) mbl.index_to_xpos[I]
38 #define _GET_MESH_Y(J) mbl.index_to_ypos[J]
39 #define Z_VALUES_ARR mbl.z_values
40 
42 public:
43  static float z_offset,
44  z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
45  index_to_xpos[GRID_MAX_POINTS_X],
46  index_to_ypos[GRID_MAX_POINTS_Y];
47 
49 
50  static void report_mesh();
51 
52  static void reset();
53 
54  FORCE_INLINE static bool has_mesh() {
55  for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
56  for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
57  if (z_values[x][y]) return true;
58  return false;
59  }
60 
61  static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
62 
63  static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
64  px = index % (GRID_MAX_POINTS_X);
65  py = index / (GRID_MAX_POINTS_X);
66  if (py & 1) px = (GRID_MAX_POINTS_X - 1) - px; // Zig zag
67  }
68 
69  static void set_zigzag_z(const int8_t index, const float &z) {
70  int8_t px, py;
71  zigzag(index, px, py);
72  set_z(px, py, z);
73  }
74 
75  static int8_t cell_index_x(const float &x) {
76  int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
77  return constrain(cx, 0, (GRID_MAX_POINTS_X) - 2);
78  }
79  static int8_t cell_index_y(const float &y) {
80  int8_t cy = (y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST);
81  return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 2);
82  }
83  static inline xy_int8_t cell_indexes(const float &x, const float &y) {
84  return { cell_index_x(x), cell_index_y(y) };
85  }
86  static inline xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }
87 
88  static int8_t probe_index_x(const float &x) {
89  int8_t px = (x - (MESH_MIN_X) + 0.5f * (MESH_X_DIST)) * RECIPROCAL(MESH_X_DIST);
90  return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
91  }
92  static int8_t probe_index_y(const float &y) {
93  int8_t py = (y - (MESH_MIN_Y) + 0.5f * (MESH_Y_DIST)) * RECIPROCAL(MESH_Y_DIST);
94  return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
95  }
96  static inline xy_int8_t probe_indexes(const float &x, const float &y) {
97  return { probe_index_x(x), probe_index_y(y) };
98  }
99  static inline xy_int8_t probe_indexes(const xy_pos_t &xy) { return probe_indexes(xy.x, xy.y); }
100 
101  static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
102  const float delta_z = (z2 - z1) / (a2 - a1),
103  delta_a = a0 - a1;
104  return z1 + delta_a * delta_z;
105  }
106 
107  static float get_z(const xy_pos_t &pos
108  #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
109  , const float &factor=1.0f
110  #endif
111  ) {
112  #if DISABLED(ENABLE_LEVELING_FADE_HEIGHT)
113  constexpr float factor = 1.0f;
114  #endif
115  const xy_int8_t ind = cell_indexes(pos);
116  const float x1 = index_to_xpos[ind.x], x2 = index_to_xpos[ind.x+1],
117  y1 = index_to_xpos[ind.y], y2 = index_to_xpos[ind.y+1],
118  z1 = calc_z0(pos.x, x1, z_values[ind.x][ind.y ], x2, z_values[ind.x+1][ind.y ]),
119  z2 = calc_z0(pos.x, x1, z_values[ind.x][ind.y+1], x2, z_values[ind.x+1][ind.y+1]);
120 
121  return z_offset + calc_z0(pos.y, y1, z1, y2, z2) * factor;
122  }
123 
124  #if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
125  static void line_to_destination(const feedRate_t &scaled_fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF);
126  #endif
127 };
128 
129 extern mesh_bed_leveling mbl;
WITHIN
#define WITHIN(N, L, H)
Definition: macros.h:195
mesh_bed_leveling::cell_indexes
static xy_int8_t cell_indexes(const xy_pos_t &xy)
Definition: mesh_bed_leveling.h:86
mesh_bed_leveling::mesh_bed_leveling
mesh_bed_leveling()
XYZEval
Definition: types.h:101
XYZEval::z
T z
Definition: types.h:383
mbl
mesh_bed_leveling mbl
mesh_bed_leveling::index_to_xpos
static float index_to_xpos[GRID_MAX_POINTS_X]
Definition: mesh_bed_leveling.h:43
mesh_bed_leveling::get_z
static float get_z(const xy_pos_t &pos)
Definition: mesh_bed_leveling.h:107
RECIPROCAL
#define RECIPROCAL(x)
Definition: macros.h:273
mesh_bed_leveling::probe_indexes
static xy_int8_t probe_indexes(const xy_pos_t &xy)
Definition: mesh_bed_leveling.h:99
_MAX
#define _MAX(V...)
Definition: macros.h:346
mesh_bed_leveling::probe_index_x
static int8_t probe_index_x(const float &x)
Definition: mesh_bed_leveling.h:88
mesh_bed_leveling::zigzag
static void zigzag(const int8_t index, int8_t &px, int8_t &py)
Definition: mesh_bed_leveling.h:63
XYZEval::e
T e
Definition: types.h:383
mesh_bed_leveling::reset
static void reset()
i
uint8_t i
Definition: screen_test_graph.c:72
STRINGIFY
#define STRINGIFY(M)
Definition: macros.h:73
mesh_bed_leveling::cell_index_y
static int8_t cell_index_y(const float &y)
Definition: mesh_bed_leveling.h:79
destination
xyze_pos_t destination
Definition: motion.cpp:110
mesh_bed_leveling::probe_indexes
static xy_int8_t probe_indexes(const float &x, const float &y)
Definition: mesh_bed_leveling.h:96
feedRate_t
float feedRate_t
Definition: types.h:80
mesh_bed_leveling::has_mesh
static FORCE_INLINE bool has_mesh()
Definition: mesh_bed_leveling.h:54
mesh_bed_leveling::report_mesh
static void report_mesh()
mesh_bed_leveling::z_values
static float z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y]
Definition: mesh_bed_leveling.h:43
NOMORE
#define NOMORE(v, n)
Definition: macros.h:133
ZERO
#define ZERO(a)
Definition: macros.h:201
mesh_bed_leveling::cell_index_x
static int8_t cell_index_x(const float &x)
Definition: mesh_bed_leveling.h:75
FORCE_INLINE
#define FORCE_INLINE
Definition: macros.h:40
current_position
xyze_pos_t current_position
Definition: motion.cpp:102
MeshReport
Definition: mesh_bed_leveling.h:27
MeshSetZOffset
Definition: mesh_bed_leveling.h:31
MeshNext
Definition: mesh_bed_leveling.h:29
ExtUI::onMeshUpdate
void onMeshUpdate(const uint8_t xpos, const uint8_t ypos, const float zval)
Definition: marlin_server.cpp:945
XYZEval::x
T x
Definition: types.h:383
constrain
#define constrain(amt, low, high)
Definition: wiring_constants.h:79
mesh_bed_leveling::set_z
static void set_z(const int8_t px, const int8_t py, const float &z)
Definition: mesh_bed_leveling.h:61
XYval
Definition: types.h:99
MESH_X_DIST
#define MESH_X_DIST
Definition: mesh_bed_leveling.h:35
XYval::x
T x
Definition: types.h:185
MESH_Y_DIST
#define MESH_Y_DIST
Definition: mesh_bed_leveling.h:36
uint8_t
const uint8_t[]
Definition: 404_html.c:3
mesh_bed_leveling::z_offset
static float z_offset
Definition: mesh_bed_leveling.h:43
MeshLevelingState
MeshLevelingState
Definition: mesh_bed_leveling.h:26
MeshSet
Definition: mesh_bed_leveling.h:30
mesh_bed_leveling::index_to_ypos
static float index_to_ypos[GRID_MAX_POINTS_Y]
Definition: mesh_bed_leveling.h:43
CBI
#define CBI(A, B)
Definition: macros.h:89
mesh_bed_leveling::probe_index_y
static int8_t probe_index_y(const float &y)
Definition: mesh_bed_leveling.h:92
SERIAL_ECHOPAIR_F
#define SERIAL_ECHOPAIR_F(S, V...)
Definition: serial.h:176
mesh_bed_leveling::calc_z0
static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2)
Definition: mesh_bed_leveling.h:101
mesh_bed_leveling::set_zigzag_z
static void set_zigzag_z(const int8_t index, const float &z)
Definition: mesh_bed_leveling.h:69
TEST
#define TEST(n, b)
Definition: macros.h:81
XYval::y
T y
Definition: types.h:185
mesh_bed_leveling::cell_indexes
static xy_int8_t cell_indexes(const float &x, const float &y)
Definition: mesh_bed_leveling.h:83
mesh_bed_leveling
Definition: mesh_bed_leveling.h:41
MeshStart
Definition: mesh_bed_leveling.h:28
MeshReset
Definition: mesh_bed_leveling.h:32
SERIAL_ECHOLNPGM
#define SERIAL_ECHOLNPGM(S)
Definition: serial.h:174
mesh_bed_leveling::line_to_destination
static void line_to_destination(const feedRate_t &scaled_fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF)
ENABLED
#define ENABLED(V...)
Definition: macros.h:177
XYZEval::y
T y
Definition: types.h:383