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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #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. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  48. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  49. if (z_values[x][y]) return true;
  50. return false;
  51. }
  52. static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
  53. static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
  54. px = index % (GRID_MAX_POINTS_X);
  55. py = index / (GRID_MAX_POINTS_X);
  56. if (py & 1) px = (GRID_MAX_POINTS_X - 1) - px; // Zig zag
  57. }
  58. static void set_zigzag_z(const int8_t index, const float &z) {
  59. int8_t px, py;
  60. zigzag(index, px, py);
  61. set_z(px, py, z);
  62. }
  63. static int8_t cell_index_x(const float &x) {
  64. int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
  65. return constrain(cx, 0, (GRID_MAX_POINTS_X) - 2);
  66. }
  67. static int8_t cell_index_y(const float &y) {
  68. int8_t cy = (y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST);
  69. return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 2);
  70. }
  71. static int8_t probe_index_x(const float &x) {
  72. int8_t px = (x - (MESH_MIN_X) + 0.5f * (MESH_X_DIST)) * RECIPROCAL(MESH_X_DIST);
  73. return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
  74. }
  75. static int8_t probe_index_y(const float &y) {
  76. int8_t py = (y - (MESH_MIN_Y) + 0.5f * (MESH_Y_DIST)) * RECIPROCAL(MESH_Y_DIST);
  77. return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
  78. }
  79. static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
  80. const float delta_z = (z2 - z1) / (a2 - a1),
  81. delta_a = a0 - a1;
  82. return z1 + delta_a * delta_z;
  83. }
  84. static float get_z(const float &x0, const float &y0
  85. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  86. , const float &factor
  87. #endif
  88. ) {
  89. const int8_t cx = cell_index_x(x0), cy = cell_index_y(y0);
  90. const float z1 = calc_z0(x0, index_to_xpos[cx], z_values[cx][cy], index_to_xpos[cx + 1], z_values[cx + 1][cy]),
  91. z2 = calc_z0(x0, index_to_xpos[cx], z_values[cx][cy + 1], index_to_xpos[cx + 1], z_values[cx + 1][cy + 1]),
  92. z0 = calc_z0(y0, index_to_ypos[cy], z1, index_to_ypos[cy + 1], z2);
  93. return z_offset + z0
  94. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  95. * factor
  96. #endif
  97. ;
  98. }
  99. #if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
  100. static void line_to_destination(const float fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF);
  101. #endif
  102. };
  103. extern mesh_bed_leveling mbl;