mesh_bed_leveling.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "Marlin.h"
  2. #ifdef MESH_BED_LEVELING
  3. #define MEAS_NUM_X_DIST (float(MESH_MAX_X - MESH_MIN_X)/float(MESH_MEAS_NUM_X_POINTS - 1))
  4. #define MEAS_NUM_Y_DIST (float(MESH_MAX_Y - MESH_MIN_Y)/float(MESH_MEAS_NUM_Y_POINTS - 1))
  5. #define MESH_X_DIST (float(MESH_MAX_X - MESH_MIN_X)/float(MESH_NUM_X_POINTS - 1))
  6. #define MESH_Y_DIST (float(MESH_MAX_Y - MESH_MIN_Y)/float(MESH_NUM_Y_POINTS - 1))
  7. class mesh_bed_leveling {
  8. public:
  9. uint8_t active;
  10. float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
  11. mesh_bed_leveling();
  12. void reset();
  13. #if MESH_NUM_X_POINTS>=5 && MESH_NUM_Y_POINTS>=5 && (MESH_NUM_X_POINTS&1)==1 && (MESH_NUM_Y_POINTS&1)==1
  14. void upsample_3x3();
  15. #endif
  16. static float get_x(int i) { return float(MESH_MIN_X) + float(MESH_X_DIST) * float(i); }
  17. static float get_y(int i) { return float(MESH_MIN_Y) + float(MESH_Y_DIST) * float(i); }
  18. // Measurement point for the Z probe.
  19. // If use_default=true, then the default positions for a correctly built printer are used.
  20. // Otherwise a correction matrix is pulled from the EEPROM if available.
  21. static void get_meas_xy(int ix, int iy, float &x, float &y, bool use_default);
  22. void set_z(int ix, int iy, float z) { z_values[iy][ix] = z; }
  23. int select_x_index(float x) {
  24. int i = 1;
  25. while (x > get_x(i) && i < MESH_NUM_X_POINTS - 1) i++;
  26. return i - 1;
  27. }
  28. int select_y_index(float y) {
  29. int i = 1;
  30. while (y > get_y(i) && i < MESH_NUM_Y_POINTS - 1) i++;
  31. return i - 1;
  32. }
  33. float get_z(float x, float y) {
  34. int i, j;
  35. float s, t;
  36. #if MESH_NUM_X_POINTS==3 && MESH_NUM_Y_POINTS==3
  37. #define MESH_MID_X (0.5f*(MESH_MIN_X+MESH_MAX_X))
  38. #define MESH_MID_Y (0.5f*(MESH_MIN_Y+MESH_MAX_Y))
  39. if (x < MESH_MID_X) {
  40. i = 0;
  41. s = (x - MESH_MIN_X) / MESH_X_DIST;
  42. if (s > 1.f)
  43. s = 1.f;
  44. } else {
  45. i = 1;
  46. s = (x - MESH_MID_X) / MESH_X_DIST;
  47. if (s < 0)
  48. s = 0;
  49. }
  50. if (y < MESH_MID_Y) {
  51. j = 0;
  52. t = (y - MESH_MIN_Y) / MESH_Y_DIST;
  53. if (t > 1.f)
  54. t = 1.f;
  55. } else {
  56. j = 1;
  57. t = (y - MESH_MID_Y) / MESH_Y_DIST;
  58. if (t < 0)
  59. t = 0;
  60. }
  61. #else
  62. i = int(floor((x - MESH_MIN_X) / MESH_X_DIST));
  63. if (i < 0) {
  64. i = 0;
  65. s = (x - MESH_MIN_X) / MESH_X_DIST;
  66. if (s > 1.f)
  67. s = 1.f;
  68. }
  69. else if (i > MESH_NUM_X_POINTS - 2) {
  70. i = MESH_NUM_X_POINTS - 2;
  71. s = (x - get_x(i)) / MESH_X_DIST;
  72. if (s < 0)
  73. s = 0;
  74. } else {
  75. s = (x - get_x(i)) / MESH_X_DIST;
  76. if (s < 0)
  77. s = 0;
  78. else if (s > 1.f)
  79. s = 1.f;
  80. }
  81. j = int(floor((y - MESH_MIN_Y) / MESH_Y_DIST));
  82. if (j < 0) {
  83. j = 0;
  84. t = (y - MESH_MIN_Y) / MESH_Y_DIST;
  85. if (t > 1.f)
  86. t = 1.f;
  87. } else if (j > MESH_NUM_Y_POINTS - 2) {
  88. j = MESH_NUM_Y_POINTS - 2;
  89. t = (y - get_y(j)) / MESH_Y_DIST;
  90. if (t < 0)
  91. t = 0;
  92. } else {
  93. t = (y - get_y(j)) / MESH_Y_DIST;
  94. if (t < 0)
  95. t = 0;
  96. else if (t > 1.f)
  97. t = 1.f;
  98. }
  99. #endif /* MESH_NUM_X_POINTS==3 && MESH_NUM_Y_POINTS==3 */
  100. float si = 1.f-s;
  101. float z0 = si * z_values[j ][i] + s * z_values[j ][i+1];
  102. float z1 = si * z_values[j+1][i] + s * z_values[j+1][i+1];
  103. return (1.f-t) * z0 + t * z1;
  104. }
  105. };
  106. extern mesh_bed_leveling mbl;
  107. #endif // MESH_BED_LEVELING