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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 MBLStatus { MBL_STATUS_NONE = 0, MBL_STATUS_HAS_MESH_BIT = 0, MBL_STATUS_ACTIVE_BIT = 1 };
  25. #define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X))/(MESH_NUM_X_POINTS - 1))
  26. #define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y))/(MESH_NUM_Y_POINTS - 1))
  27. class mesh_bed_leveling {
  28. public:
  29. uint8_t status; // Has Mesh and Is Active bits
  30. float z_offset;
  31. float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
  32. mesh_bed_leveling();
  33. void reset();
  34. static FORCE_INLINE float get_probe_x(int8_t i) { return MESH_MIN_X + (MESH_X_DIST) * i; }
  35. static FORCE_INLINE float get_probe_y(int8_t i) { return MESH_MIN_Y + (MESH_Y_DIST) * i; }
  36. void set_z(const int8_t px, const int8_t py, const float z) { z_values[py][px] = z; }
  37. bool active() { return TEST(status, MBL_STATUS_ACTIVE_BIT); }
  38. void set_active(bool onOff) { if (onOff) SBI(status, MBL_STATUS_ACTIVE_BIT); else CBI(status, MBL_STATUS_ACTIVE_BIT); }
  39. bool has_mesh() { return TEST(status, MBL_STATUS_HAS_MESH_BIT); }
  40. void set_has_mesh(bool onOff) { if (onOff) SBI(status, MBL_STATUS_HAS_MESH_BIT); else CBI(status, MBL_STATUS_HAS_MESH_BIT); }
  41. inline void zigzag(int8_t index, int8_t &px, int8_t &py) {
  42. px = index % (MESH_NUM_X_POINTS);
  43. py = index / (MESH_NUM_X_POINTS);
  44. if (py & 1) px = (MESH_NUM_X_POINTS - 1) - px; // Zig zag
  45. }
  46. void set_zigzag_z(int8_t index, float z) {
  47. int8_t px, py;
  48. zigzag(index, px, py);
  49. set_z(px, py, z);
  50. }
  51. int8_t cell_index_x(float x) {
  52. int8_t cx = int(x - (MESH_MIN_X)) / (MESH_X_DIST);
  53. return constrain(cx, 0, (MESH_NUM_X_POINTS) - 2);
  54. }
  55. int8_t cell_index_y(float y) {
  56. int8_t cy = int(y - (MESH_MIN_Y)) / (MESH_Y_DIST);
  57. return constrain(cy, 0, (MESH_NUM_Y_POINTS) - 2);
  58. }
  59. int8_t probe_index_x(float x) {
  60. int8_t px = int(x - (MESH_MIN_X) + (MESH_X_DIST) / 2) / (MESH_X_DIST);
  61. return (px >= 0 && px < (MESH_NUM_X_POINTS)) ? px : -1;
  62. }
  63. int8_t probe_index_y(float y) {
  64. int8_t py = int(y - (MESH_MIN_Y) + (MESH_Y_DIST) / 2) / (MESH_Y_DIST);
  65. return (py >= 0 && py < (MESH_NUM_Y_POINTS)) ? py : -1;
  66. }
  67. float calc_z0(float a0, float a1, float z1, float a2, float z2) {
  68. float delta_z = (z2 - z1) / (a2 - a1);
  69. float delta_a = a0 - a1;
  70. return z1 + delta_a * delta_z;
  71. }
  72. float get_z(float x0, float y0) {
  73. int8_t cx = cell_index_x(x0),
  74. cy = cell_index_y(y0);
  75. if (cx < 0 || cy < 0) return z_offset;
  76. float z1 = calc_z0(x0,
  77. get_probe_x(cx), z_values[cy][cx],
  78. get_probe_x(cx + 1), z_values[cy][cx + 1]);
  79. float z2 = calc_z0(x0,
  80. get_probe_x(cx), z_values[cy + 1][cx],
  81. get_probe_x(cx + 1), z_values[cy + 1][cx + 1]);
  82. float z0 = calc_z0(y0,
  83. get_probe_y(cy), z1,
  84. get_probe_y(cy + 1), z2);
  85. return z0 + z_offset;
  86. }
  87. };
  88. extern mesh_bed_leveling mbl;
  89. #endif // MESH_BED_LEVELING