My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mesh_bed_leveling.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #include "../../../inc/MarlinConfig.h"
  24. enum MeshLevelingState : char {
  25. MeshReport, // G29 S0
  26. MeshStart, // G29 S1
  27. MeshNext, // G29 S2
  28. MeshSet, // G29 S3
  29. MeshSetZOffset, // G29 S4
  30. MeshReset // G29 S5
  31. };
  32. #define MESH_X_DIST (float(MESH_MAX_X - (MESH_MIN_X)) / float(GRID_MAX_POINTS_X - 1))
  33. #define MESH_Y_DIST (float(MESH_MAX_Y - (MESH_MIN_Y)) / float(GRID_MAX_POINTS_Y - 1))
  34. #define _GET_MESH_X(I) mbl.index_to_xpos[I]
  35. #define _GET_MESH_Y(J) mbl.index_to_ypos[J]
  36. #define Z_VALUES_ARR mbl.z_values
  37. class mesh_bed_leveling {
  38. public:
  39. static float z_offset,
  40. z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
  41. index_to_xpos[GRID_MAX_POINTS_X],
  42. index_to_ypos[GRID_MAX_POINTS_Y];
  43. mesh_bed_leveling();
  44. static void report_mesh();
  45. static void reset();
  46. FORCE_INLINE static bool has_mesh() {
  47. GRID_LOOP(x, y) if (z_values[x][y]) return true;
  48. return false;
  49. }
  50. static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
  51. static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
  52. px = index % (GRID_MAX_POINTS_X);
  53. py = index / (GRID_MAX_POINTS_X);
  54. if (py & 1) px = (GRID_MAX_POINTS_X - 1) - px; // Zig zag
  55. }
  56. static void set_zigzag_z(const int8_t index, const float &z) {
  57. int8_t px, py;
  58. zigzag(index, px, py);
  59. set_z(px, py, z);
  60. }
  61. static int8_t cell_index_x(const float &x) {
  62. int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
  63. return constrain(cx, 0, (GRID_MAX_POINTS_X) - 2);
  64. }
  65. static int8_t cell_index_y(const float &y) {
  66. int8_t cy = (y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST);
  67. return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 2);
  68. }
  69. static inline xy_int8_t cell_indexes(const float &x, const float &y) {
  70. return { cell_index_x(x), cell_index_y(y) };
  71. }
  72. static inline xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }
  73. static int8_t probe_index_x(const float &x) {
  74. int8_t px = (x - (MESH_MIN_X) + 0.5f * (MESH_X_DIST)) * RECIPROCAL(MESH_X_DIST);
  75. return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
  76. }
  77. static int8_t probe_index_y(const float &y) {
  78. int8_t py = (y - (MESH_MIN_Y) + 0.5f * (MESH_Y_DIST)) * RECIPROCAL(MESH_Y_DIST);
  79. return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
  80. }
  81. static inline xy_int8_t probe_indexes(const float &x, const float &y) {
  82. return { probe_index_x(x), probe_index_y(y) };
  83. }
  84. static inline xy_int8_t probe_indexes(const xy_pos_t &xy) { return probe_indexes(xy.x, xy.y); }
  85. static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
  86. const float delta_z = (z2 - z1) / (a2 - a1),
  87. delta_a = a0 - a1;
  88. return z1 + delta_a * delta_z;
  89. }
  90. static float get_z(const xy_pos_t &pos
  91. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  92. , const float &factor=1.0f
  93. #endif
  94. ) {
  95. #if DISABLED(ENABLE_LEVELING_FADE_HEIGHT)
  96. constexpr float factor = 1.0f;
  97. #endif
  98. const xy_int8_t ind = cell_indexes(pos);
  99. const float x1 = index_to_xpos[ind.x], x2 = index_to_xpos[ind.x+1],
  100. y1 = index_to_xpos[ind.y], y2 = index_to_xpos[ind.y+1],
  101. z1 = calc_z0(pos.x, x1, z_values[ind.x][ind.y ], x2, z_values[ind.x+1][ind.y ]),
  102. z2 = calc_z0(pos.x, x1, z_values[ind.x][ind.y+1], x2, z_values[ind.x+1][ind.y+1]);
  103. return z_offset + calc_z0(pos.y, y1, z1, y2, z2) * factor;
  104. }
  105. #if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
  106. static void line_to_destination(const feedRate_t &scaled_fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF);
  107. #endif
  108. };
  109. extern mesh_bed_leveling mbl;