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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. mesh_bed_leveling mbl;
  27. uint8_t mesh_bed_leveling::status;
  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. status = MBL_STATUS_NONE;
  41. z_offset = 0;
  42. ZERO(z_values);
  43. }
  44. /**
  45. * Prepare a mesh-leveled linear move in a Cartesian setup,
  46. * splitting the move where it crosses mesh borders.
  47. */
  48. void mesh_line_to_destination(const float fr_mm_s, uint8_t x_splits, uint8_t y_splits) {
  49. int cx1 = mbl.cell_index_x(RAW_CURRENT_POSITION(X)),
  50. cy1 = mbl.cell_index_y(RAW_CURRENT_POSITION(Y)),
  51. cx2 = mbl.cell_index_x(RAW_X_POSITION(destination[X_AXIS])),
  52. cy2 = mbl.cell_index_y(RAW_Y_POSITION(destination[Y_AXIS]));
  53. NOMORE(cx1, GRID_MAX_POINTS_X - 2);
  54. NOMORE(cy1, GRID_MAX_POINTS_Y - 2);
  55. NOMORE(cx2, GRID_MAX_POINTS_X - 2);
  56. NOMORE(cy2, GRID_MAX_POINTS_Y - 2);
  57. if (cx1 == cx2 && cy1 == cy2) {
  58. // Start and end on same mesh square
  59. line_to_destination(fr_mm_s);
  60. set_current_to_destination();
  61. return;
  62. }
  63. #define MBL_SEGMENT_END(A) (current_position[A ##_AXIS] + (destination[A ##_AXIS] - current_position[A ##_AXIS]) * normalized_dist)
  64. float normalized_dist, end[XYZE];
  65. // Split at the left/front border of the right/top square
  66. const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2);
  67. if (cx2 != cx1 && TEST(x_splits, gcx)) {
  68. COPY(end, destination);
  69. destination[X_AXIS] = LOGICAL_X_POSITION(mbl.index_to_xpos[gcx]);
  70. normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]);
  71. destination[Y_AXIS] = MBL_SEGMENT_END(Y);
  72. CBI(x_splits, gcx);
  73. }
  74. else if (cy2 != cy1 && TEST(y_splits, gcy)) {
  75. COPY(end, destination);
  76. destination[Y_AXIS] = LOGICAL_Y_POSITION(mbl.index_to_ypos[gcy]);
  77. normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]);
  78. destination[X_AXIS] = MBL_SEGMENT_END(X);
  79. CBI(y_splits, gcy);
  80. }
  81. else {
  82. // Already split on a border
  83. line_to_destination(fr_mm_s);
  84. set_current_to_destination();
  85. return;
  86. }
  87. destination[Z_AXIS] = MBL_SEGMENT_END(Z);
  88. destination[E_AXIS] = MBL_SEGMENT_END(E);
  89. // Do the split and look for more borders
  90. mesh_line_to_destination(fr_mm_s, x_splits, y_splits);
  91. // Restore destination from stack
  92. COPY(destination, end);
  93. mesh_line_to_destination(fr_mm_s, x_splits, y_splits);
  94. }
  95. #endif // MESH_BED_LEVELING