mesh_bed_leveling.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. void set_z(uint8_t ix, uint8_t iy, float z) { z_values[iy][ix] = z; }
  19. int select_x_index(float x) {
  20. int i = 1;
  21. while (x > get_x(i) && i < MESH_NUM_X_POINTS - 1) i++;
  22. return i - 1;
  23. }
  24. int select_y_index(float y) {
  25. int i = 1;
  26. while (y > get_y(i) && i < MESH_NUM_Y_POINTS - 1) i++;
  27. return i - 1;
  28. }
  29. float get_z(float x, float y) {
  30. int i, j;
  31. float s, t;
  32. #if MESH_NUM_X_POINTS==3 && MESH_NUM_Y_POINTS==3
  33. #define MESH_MID_X (0.5f*(MESH_MIN_X+MESH_MAX_X))
  34. #define MESH_MID_Y (0.5f*(MESH_MIN_Y+MESH_MAX_Y))
  35. if (x < MESH_MID_X) {
  36. i = 0;
  37. s = (x - MESH_MIN_X) / MESH_X_DIST;
  38. if (s > 1.f)
  39. s = 1.f;
  40. } else {
  41. i = 1;
  42. s = (x - MESH_MID_X) / MESH_X_DIST;
  43. if (s < 0)
  44. s = 0;
  45. }
  46. if (y < MESH_MID_Y) {
  47. j = 0;
  48. t = (y - MESH_MIN_Y) / MESH_Y_DIST;
  49. if (t > 1.f)
  50. t = 1.f;
  51. } else {
  52. j = 1;
  53. t = (y - MESH_MID_Y) / MESH_Y_DIST;
  54. if (t < 0)
  55. t = 0;
  56. }
  57. #else
  58. i = int(floor((x - MESH_MIN_X) / MESH_X_DIST));
  59. if (i < 0) {
  60. i = 0;
  61. s = (x - MESH_MIN_X) / MESH_X_DIST;
  62. if (s > 1.f)
  63. s = 1.f;
  64. }
  65. else if (i > MESH_NUM_X_POINTS - 2) {
  66. i = MESH_NUM_X_POINTS - 2;
  67. s = (x - get_x(i)) / MESH_X_DIST;
  68. if (s < 0)
  69. s = 0;
  70. } else {
  71. s = (x - get_x(i)) / MESH_X_DIST;
  72. if (s < 0)
  73. s = 0;
  74. else if (s > 1.f)
  75. s = 1.f;
  76. }
  77. j = int(floor((y - MESH_MIN_Y) / MESH_Y_DIST));
  78. if (j < 0) {
  79. j = 0;
  80. t = (y - MESH_MIN_Y) / MESH_Y_DIST;
  81. if (t > 1.f)
  82. t = 1.f;
  83. } else if (j > MESH_NUM_Y_POINTS - 2) {
  84. j = MESH_NUM_Y_POINTS - 2;
  85. t = (y - get_y(j)) / MESH_Y_DIST;
  86. if (t < 0)
  87. t = 0;
  88. } else {
  89. t = (y - get_y(j)) / MESH_Y_DIST;
  90. if (t < 0)
  91. t = 0;
  92. else if (t > 1.f)
  93. t = 1.f;
  94. }
  95. #endif /* MESH_NUM_X_POINTS==3 && MESH_NUM_Y_POINTS==3 */
  96. float si = 1.f-s;
  97. float z0 = si * z_values[j ][i] + s * z_values[j ][i+1];
  98. float z1 = si * z_values[j+1][i] + s * z_values[j+1][i+1];
  99. return (1.f-t) * z0 + t * z1;
  100. }
  101. };
  102. extern mesh_bed_leveling mbl;
  103. #endif // MESH_BED_LEVELING