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

mesh_bed_leveling.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "Marlin.h"
  23. #if ENABLED(MESH_BED_LEVELING)
  24. enum MeshLevelingState {
  25. MeshReport,
  26. MeshStart,
  27. MeshNext,
  28. MeshSet,
  29. MeshSetZOffset,
  30. MeshReset
  31. };
  32. enum MBLStatus {
  33. MBL_STATUS_NONE = 0,
  34. MBL_STATUS_HAS_MESH_BIT = 0,
  35. MBL_STATUS_ACTIVE_BIT = 1,
  36. MBL_STATUS_REACTIVATE_BIT = 2
  37. };
  38. #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X))/(MESH_NUM_X_POINTS - 1))
  39. #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y))/(MESH_NUM_Y_POINTS - 1))
  40. class mesh_bed_leveling {
  41. public:
  42. uint8_t status; // Has Mesh and Is Active bits
  43. float z_offset;
  44. float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
  45. mesh_bed_leveling();
  46. void reset();
  47. static FORCE_INLINE float get_probe_x(const int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
  48. static FORCE_INLINE float get_probe_y(const int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
  49. void set_z(const int8_t px, const int8_t py, const float &z) { z_values[py][px] = z; }
  50. bool active() const { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
  51. void set_active(const bool onOff) { onOff ? SBI(status, MBL_STATUS_ACTIVE_BIT) : CBI(status, MBL_STATUS_ACTIVE_BIT); }
  52. bool has_mesh() const { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
  53. void set_has_mesh(const bool onOff) { onOff ? SBI(status, MBL_STATUS_HAS_MESH_BIT) : CBI(status, MBL_STATUS_HAS_MESH_BIT); }
  54. bool reactivate() { bool b = TEST(status, MBL_STATUS_REACTIVATE_BIT); CBI(status, MBL_STATUS_REACTIVATE_BIT); return b; }
  55. void set_reactivate(const bool onOff) { onOff ? SBI(status, MBL_STATUS_REACTIVATE_BIT) : CBI(status, MBL_STATUS_REACTIVATE_BIT); }
  56. inline void zigzag(const int8_t index, int8_t &px, int8_t &py) const {
  57. px = index % (MESH_NUM_X_POINTS);
  58. py = index / (MESH_NUM_X_POINTS);
  59. if (py & 1) px = (MESH_NUM_X_POINTS - 1) - px; // Zig zag
  60. }
  61. void set_zigzag_z(const int8_t index, const float &z) {
  62. int8_t px, py;
  63. zigzag(index, px, py);
  64. set_z(px, py, z);
  65. }
  66. int8_t cell_index_x(const float &x) const {
  67. int8_t cx = (x - (MESH_MIN_X)) * (1.0 / (MESH_X_DIST));
  68. return constrain(cx, 0, (MESH_NUM_X_POINTS) - 2);
  69. }
  70. int8_t cell_index_y(const float &y) const {
  71. int8_t cy = (y - (MESH_MIN_Y)) * (1.0 / (MESH_Y_DIST));
  72. return constrain(cy, 0, (MESH_NUM_Y_POINTS) - 2);
  73. }
  74. int8_t probe_index_x(const float &x) const {
  75. int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * (1.0 / (MESH_X_DIST));
  76. return (px >= 0 && px < (MESH_NUM_X_POINTS)) ? px : -1;
  77. }
  78. int8_t probe_index_y(const float &y) const {
  79. int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * (1.0 / (MESH_Y_DIST));
  80. return (py >= 0 && py < (MESH_NUM_Y_POINTS)) ? py : -1;
  81. }
  82. float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) const {
  83. const float delta_z = (z2 - z1) / (a2 - a1);
  84. const float delta_a = a0 - a1;
  85. return z1 + delta_a * delta_z;
  86. }
  87. float get_z(const float &x0, const float &y0
  88. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  89. , const float &factor
  90. #endif
  91. ) const {
  92. int8_t cx = cell_index_x(x0),
  93. cy = cell_index_y(y0);
  94. if (cx < 0 || cy < 0) return z_offset;
  95. float z1 = calc_z0(x0,
  96. get_probe_x(cx), z_values[cy][cx],
  97. get_probe_x(cx + 1), z_values[cy][cx + 1]);
  98. float z2 = calc_z0(x0,
  99. get_probe_x(cx), z_values[cy + 1][cx],
  100. get_probe_x(cx + 1), z_values[cy + 1][cx + 1]);
  101. float z0 = calc_z0(y0,
  102. get_probe_y(cy), z1,
  103. get_probe_y(cy + 1), z2);
  104. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  105. return z0 * factor + z_offset;
  106. #else
  107. return z0 + z_offset;
  108. #endif
  109. }
  110. };
  111. extern mesh_bed_leveling mbl;
  112. #endif // MESH_BED_LEVELING