|
@@ -40,91 +40,83 @@
|
40
|
40
|
MBL_STATUS_REACTIVATE_BIT = 2
|
41
|
41
|
};
|
42
|
42
|
|
43
|
|
- #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X))/(MESH_NUM_X_POINTS - 1))
|
44
|
|
- #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y))/(MESH_NUM_Y_POINTS - 1))
|
|
43
|
+ #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X)) / (MESH_NUM_X_POINTS - 1))
|
|
44
|
+ #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y)) / (MESH_NUM_Y_POINTS - 1))
|
45
|
45
|
|
46
|
46
|
class mesh_bed_leveling {
|
47
|
47
|
public:
|
48
|
|
- uint8_t status; // Has Mesh and Is Active bits
|
49
|
|
- float z_offset;
|
50
|
|
- float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
|
|
48
|
+ static uint8_t status; // Has Mesh and Is Active bits
|
|
49
|
+ static float z_offset,
|
|
50
|
+ z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS],
|
|
51
|
+ index_to_xpos[MESH_NUM_X_POINTS],
|
|
52
|
+ index_to_ypos[MESH_NUM_Y_POINTS];
|
51
|
53
|
|
52
|
54
|
mesh_bed_leveling();
|
53
|
55
|
|
54
|
|
- void reset();
|
|
56
|
+ static void reset();
|
55
|
57
|
|
56
|
|
- static FORCE_INLINE float get_probe_x(const int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
|
57
|
|
- static FORCE_INLINE float get_probe_y(const int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
|
58
|
|
- void set_z(const int8_t px, const int8_t py, const float &z) { z_values[py][px] = z; }
|
|
58
|
+ static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[py][px] = z; }
|
59
|
59
|
|
60
|
|
- bool active() const { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
|
61
|
|
- void set_active(const bool onOff) { onOff ? SBI(status, MBL_STATUS_ACTIVE_BIT) : CBI(status, MBL_STATUS_ACTIVE_BIT); }
|
62
|
|
- bool has_mesh() const { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
|
63
|
|
- void set_has_mesh(const bool onOff) { onOff ? SBI(status, MBL_STATUS_HAS_MESH_BIT) : CBI(status, MBL_STATUS_HAS_MESH_BIT); }
|
64
|
|
- bool reactivate() { bool b = TEST(status, MBL_STATUS_REACTIVATE_BIT); CBI(status, MBL_STATUS_REACTIVATE_BIT); return b; }
|
65
|
|
- void set_reactivate(const bool onOff) { onOff ? SBI(status, MBL_STATUS_REACTIVATE_BIT) : CBI(status, MBL_STATUS_REACTIVATE_BIT); }
|
|
60
|
+ static bool active() { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
|
|
61
|
+ static void set_active(const bool onOff) { onOff ? SBI(status, MBL_STATUS_ACTIVE_BIT) : CBI(status, MBL_STATUS_ACTIVE_BIT); }
|
|
62
|
+ static bool has_mesh() { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
|
|
63
|
+ static void set_has_mesh(const bool onOff) { onOff ? SBI(status, MBL_STATUS_HAS_MESH_BIT) : CBI(status, MBL_STATUS_HAS_MESH_BIT); }
|
|
64
|
+ static bool reactivate() { bool b = TEST(status, MBL_STATUS_REACTIVATE_BIT); CBI(status, MBL_STATUS_REACTIVATE_BIT); return b; }
|
|
65
|
+ static void set_reactivate(const bool onOff) { onOff ? SBI(status, MBL_STATUS_REACTIVATE_BIT) : CBI(status, MBL_STATUS_REACTIVATE_BIT); }
|
66
|
66
|
|
67
|
|
- inline void zigzag(const int8_t index, int8_t &px, int8_t &py) const {
|
|
67
|
+ static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
|
68
|
68
|
px = index % (MESH_NUM_X_POINTS);
|
69
|
69
|
py = index / (MESH_NUM_X_POINTS);
|
70
|
70
|
if (py & 1) px = (MESH_NUM_X_POINTS - 1) - px; // Zig zag
|
71
|
71
|
}
|
72
|
72
|
|
73
|
|
- void set_zigzag_z(const int8_t index, const float &z) {
|
|
73
|
+ static void set_zigzag_z(const int8_t index, const float &z) {
|
74
|
74
|
int8_t px, py;
|
75
|
75
|
zigzag(index, px, py);
|
76
|
76
|
set_z(px, py, z);
|
77
|
77
|
}
|
78
|
78
|
|
79
|
|
- int8_t cell_index_x(const float &x) const {
|
|
79
|
+ static int8_t cell_index_x(const float &x) {
|
80
|
80
|
int8_t cx = (x - (MESH_MIN_X)) * (1.0 / (MESH_X_DIST));
|
81
|
81
|
return constrain(cx, 0, (MESH_NUM_X_POINTS) - 2);
|
82
|
82
|
}
|
83
|
83
|
|
84
|
|
- int8_t cell_index_y(const float &y) const {
|
|
84
|
+ static int8_t cell_index_y(const float &y) {
|
85
|
85
|
int8_t cy = (y - (MESH_MIN_Y)) * (1.0 / (MESH_Y_DIST));
|
86
|
86
|
return constrain(cy, 0, (MESH_NUM_Y_POINTS) - 2);
|
87
|
87
|
}
|
88
|
88
|
|
89
|
|
- int8_t probe_index_x(const float &x) const {
|
|
89
|
+ static int8_t probe_index_x(const float &x) {
|
90
|
90
|
int8_t px = (x - (MESH_MIN_X) + 0.5 * (MESH_X_DIST)) * (1.0 / (MESH_X_DIST));
|
91
|
91
|
return (px >= 0 && px < (MESH_NUM_X_POINTS)) ? px : -1;
|
92
|
92
|
}
|
93
|
93
|
|
94
|
|
- int8_t probe_index_y(const float &y) const {
|
|
94
|
+ static int8_t probe_index_y(const float &y) {
|
95
|
95
|
int8_t py = (y - (MESH_MIN_Y) + 0.5 * (MESH_Y_DIST)) * (1.0 / (MESH_Y_DIST));
|
96
|
96
|
return (py >= 0 && py < (MESH_NUM_Y_POINTS)) ? py : -1;
|
97
|
97
|
}
|
98
|
98
|
|
99
|
|
- float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) const {
|
|
99
|
+ static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
|
100
|
100
|
const float delta_z = (z2 - z1) / (a2 - a1);
|
101
|
101
|
const float delta_a = a0 - a1;
|
102
|
102
|
return z1 + delta_a * delta_z;
|
103
|
103
|
}
|
104
|
104
|
|
105
|
|
- float get_z(const float &x0, const float &y0
|
|
105
|
+ static float get_z(const float &x0, const float &y0
|
106
|
106
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
107
|
107
|
, const float &factor
|
108
|
108
|
#endif
|
109
|
|
- ) const {
|
110
|
|
- int8_t cx = cell_index_x(x0),
|
111
|
|
- cy = cell_index_y(y0);
|
112
|
|
- if (cx < 0 || cy < 0) return z_offset;
|
113
|
|
- float z1 = calc_z0(x0,
|
114
|
|
- get_probe_x(cx), z_values[cy][cx],
|
115
|
|
- get_probe_x(cx + 1), z_values[cy][cx + 1]);
|
116
|
|
- float z2 = calc_z0(x0,
|
117
|
|
- get_probe_x(cx), z_values[cy + 1][cx],
|
118
|
|
- get_probe_x(cx + 1), z_values[cy + 1][cx + 1]);
|
119
|
|
- float z0 = calc_z0(y0,
|
120
|
|
- get_probe_y(cy), z1,
|
121
|
|
- get_probe_y(cy + 1), z2);
|
122
|
|
-
|
123
|
|
- #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
124
|
|
- return z0 * factor + z_offset;
|
125
|
|
- #else
|
126
|
|
- return z0 + z_offset;
|
127
|
|
- #endif
|
|
109
|
+ ) {
|
|
110
|
+ const int8_t cx = cell_index_x(x0), cy = cell_index_y(y0);
|
|
111
|
+ const float z1 = calc_z0(x0, index_to_xpos[cx], z_values[cy][cx], index_to_xpos[cx + 1], z_values[cy][cx + 1]),
|
|
112
|
+ z2 = calc_z0(x0, index_to_xpos[cx], z_values[cy + 1][cx], index_to_xpos[cx + 1], z_values[cy + 1][cx + 1]),
|
|
113
|
+ z0 = calc_z0(y0, index_to_ypos[cy], z1, index_to_ypos[cy + 1], z2);
|
|
114
|
+
|
|
115
|
+ return z_offset + z0
|
|
116
|
+ #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
|
117
|
+ * factor
|
|
118
|
+ #endif
|
|
119
|
+ ;
|
128
|
120
|
}
|
129
|
121
|
};
|
130
|
122
|
|