My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mesh_bed_leveling.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "../../../inc/MarlinConfig.h"
  23. #if ENABLED(MESH_BED_LEVELING)
  24. #include "mesh_bed_leveling.h"
  25. #include "../../../module/motion.h"
  26. #include "../../../feature/bedlevel/bedlevel.h"
  27. mesh_bed_leveling mbl;
  28. float mesh_bed_leveling::z_offset,
  29. mesh_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
  30. mesh_bed_leveling::index_to_xpos[GRID_MAX_POINTS_X],
  31. mesh_bed_leveling::index_to_ypos[GRID_MAX_POINTS_Y];
  32. mesh_bed_leveling::mesh_bed_leveling() {
  33. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; ++i)
  34. index_to_xpos[i] = MESH_MIN_X + i * (MESH_X_DIST);
  35. for (uint8_t i = 0; i < GRID_MAX_POINTS_Y; ++i)
  36. index_to_ypos[i] = MESH_MIN_Y + i * (MESH_Y_DIST);
  37. reset();
  38. }
  39. void mesh_bed_leveling::reset() {
  40. z_offset = 0;
  41. ZERO(z_values);
  42. #if ENABLED(EXTENSIBLE_UI)
  43. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  44. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  45. 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 float fr_mm_s, uint8_t x_splits, uint8_t y_splits) {
  54. // Get current and destination cells for this line
  55. int cx1 = cell_index_x(current_position[X_AXIS]),
  56. cy1 = cell_index_y(current_position[Y_AXIS]),
  57. cx2 = cell_index_x(destination[X_AXIS]),
  58. cy2 = cell_index_y(destination[Y_AXIS]);
  59. NOMORE(cx1, GRID_MAX_POINTS_X - 2);
  60. NOMORE(cy1, GRID_MAX_POINTS_Y - 2);
  61. NOMORE(cx2, GRID_MAX_POINTS_X - 2);
  62. NOMORE(cy2, GRID_MAX_POINTS_Y - 2);
  63. // Start and end in the same cell? No split needed.
  64. if (cx1 == cx2 && cy1 == cy2) {
  65. line_to_destination(fr_mm_s);
  66. set_current_from_destination();
  67. return;
  68. }
  69. #define MBL_SEGMENT_END(A) (current_position[_AXIS(A)] + (destination[_AXIS(A)] - current_position[_AXIS(A)]) * normalized_dist)
  70. float normalized_dist, end[XYZE];
  71. const int8_t gcx = MAX(cx1, cx2), gcy = MAX(cy1, cy2);
  72. // Crosses on the X and not already split on this X?
  73. // The x_splits flags are insurance against rounding errors.
  74. if (cx2 != cx1 && TEST(x_splits, gcx)) {
  75. // Split on the X grid line
  76. CBI(x_splits, gcx);
  77. COPY(end, destination);
  78. destination[X_AXIS] = index_to_xpos[gcx];
  79. normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]);
  80. destination[Y_AXIS] = MBL_SEGMENT_END(Y);
  81. }
  82. // Crosses on the Y and not already split on this Y?
  83. else if (cy2 != cy1 && TEST(y_splits, gcy)) {
  84. // Split on the Y grid line
  85. CBI(y_splits, gcy);
  86. COPY(end, destination);
  87. destination[Y_AXIS] = index_to_ypos[gcy];
  88. normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]);
  89. destination[X_AXIS] = MBL_SEGMENT_END(X);
  90. }
  91. else {
  92. // Must already have been split on these border(s)
  93. // This should be a rare case.
  94. line_to_destination(fr_mm_s);
  95. set_current_from_destination();
  96. return;
  97. }
  98. destination[Z_AXIS] = MBL_SEGMENT_END(Z);
  99. destination[E_AXIS] = MBL_SEGMENT_END(E);
  100. // Do the split and look for more borders
  101. line_to_destination(fr_mm_s, x_splits, y_splits);
  102. // Restore destination from stack
  103. COPY(destination, end);
  104. line_to_destination(fr_mm_s, x_splits, y_splits);
  105. }
  106. #endif // IS_CARTESIAN && !SEGMENT_LEVELED_MOVES
  107. void mesh_bed_leveling::report_mesh() {
  108. SERIAL_ECHOPAIR_F(STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_Y) " mesh. Z offset: ", z_offset, 5);
  109. SERIAL_ECHOLNPGM("\nMeasured points:");
  110. print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 5,
  111. [](const uint8_t ix, const uint8_t iy) { return z_values[ix][iy]; }
  112. );
  113. }
  114. #endif // MESH_BED_LEVELING