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.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "../../../inc/MarlinConfig.h"
  23. #if ENABLED(MESH_BED_LEVELING)
  24. #include "../bedlevel.h"
  25. #include "../../../module/motion.h"
  26. #if ENABLED(EXTENSIBLE_UI)
  27. #include "../../../lcd/extui/ui_api.h"
  28. #endif
  29. mesh_bed_leveling mbl;
  30. float mesh_bed_leveling::z_offset,
  31. mesh_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
  32. mesh_bed_leveling::index_to_xpos[GRID_MAX_POINTS_X],
  33. mesh_bed_leveling::index_to_ypos[GRID_MAX_POINTS_Y];
  34. mesh_bed_leveling::mesh_bed_leveling() {
  35. LOOP_L_N(i, GRID_MAX_POINTS_X)
  36. index_to_xpos[i] = MESH_MIN_X + i * (MESH_X_DIST);
  37. LOOP_L_N(i, GRID_MAX_POINTS_Y)
  38. index_to_ypos[i] = MESH_MIN_Y + i * (MESH_Y_DIST);
  39. reset();
  40. }
  41. void mesh_bed_leveling::reset() {
  42. z_offset = 0;
  43. ZERO(z_values);
  44. #if ENABLED(EXTENSIBLE_UI)
  45. GRID_LOOP(x, y) ExtUI::onMeshUpdate(x, y, 0);
  46. #endif
  47. }
  48. #if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
  49. /**
  50. * Prepare a mesh-leveled linear move in a Cartesian setup,
  51. * splitting the move where it crosses mesh borders.
  52. */
  53. void mesh_bed_leveling::line_to_destination(const feedRate_t &scaled_fr_mm_s, uint8_t x_splits, uint8_t y_splits) {
  54. // Get current and destination cells for this line
  55. xy_int8_t scel = cell_indexes(current_position), ecel = cell_indexes(destination);
  56. NOMORE(scel.x, GRID_MAX_POINTS_X - 2);
  57. NOMORE(scel.y, GRID_MAX_POINTS_Y - 2);
  58. NOMORE(ecel.x, GRID_MAX_POINTS_X - 2);
  59. NOMORE(ecel.y, GRID_MAX_POINTS_Y - 2);
  60. // Start and end in the same cell? No split needed.
  61. if (scel == ecel) {
  62. line_to_destination(scaled_fr_mm_s);
  63. current_position = destination;
  64. return;
  65. }
  66. #define MBL_SEGMENT_END(A) (current_position.A + (destination.A - current_position.A) * normalized_dist)
  67. float normalized_dist;
  68. xyze_pos_t dest;
  69. const int8_t gcx = _MAX(scel.x, ecel.x), gcy = _MAX(scel.y, ecel.y);
  70. // Crosses on the X and not already split on this X?
  71. // The x_splits flags are insurance against rounding errors.
  72. if (ecel.x != scel.x && TEST(x_splits, gcx)) {
  73. // Split on the X grid line
  74. CBI(x_splits, gcx);
  75. dest = destination;
  76. destination.x = index_to_xpos[gcx];
  77. normalized_dist = (destination.x - current_position.x) / (dest.x - current_position.x);
  78. destination.y = MBL_SEGMENT_END(y);
  79. }
  80. // Crosses on the Y and not already split on this Y?
  81. else if (ecel.y != scel.y && TEST(y_splits, gcy)) {
  82. // Split on the Y grid line
  83. CBI(y_splits, gcy);
  84. dest = destination;
  85. destination.y = index_to_ypos[gcy];
  86. normalized_dist = (destination.y - current_position.y) / (dest.y - current_position.y);
  87. destination.x = MBL_SEGMENT_END(x);
  88. }
  89. else {
  90. // Must already have been split on these border(s)
  91. // This should be a rare case.
  92. line_to_destination(scaled_fr_mm_s);
  93. current_position = destination;
  94. return;
  95. }
  96. destination.z = MBL_SEGMENT_END(z);
  97. destination.e = MBL_SEGMENT_END(e);
  98. // Do the split and look for more borders
  99. line_to_destination(scaled_fr_mm_s, x_splits, y_splits);
  100. // Restore destination from stack
  101. destination = dest;
  102. line_to_destination(scaled_fr_mm_s, x_splits, y_splits);
  103. }
  104. #endif // IS_CARTESIAN && !SEGMENT_LEVELED_MOVES
  105. void mesh_bed_leveling::report_mesh() {
  106. SERIAL_ECHOPAIR_F(STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_Y) " mesh. Z offset: ", z_offset, 5);
  107. SERIAL_ECHOLNPGM("\nMeasured points:");
  108. print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 5,
  109. [](const uint8_t ix, const uint8_t iy) { return z_values[ix][iy]; }
  110. );
  111. }
  112. #endif // MESH_BED_LEVELING