mesh_bed_leveling.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "mesh_bed_leveling.h"
  2. #include "mesh_bed_calibration.h"
  3. #include "Configuration.h"
  4. #ifdef MESH_BED_LEVELING
  5. mesh_bed_leveling mbl;
  6. mesh_bed_leveling::mesh_bed_leveling() { reset(); }
  7. void mesh_bed_leveling::reset() {
  8. active = 0;
  9. for (int y = 0; y < MESH_NUM_Y_POINTS; y++)
  10. for (int x = 0; x < MESH_NUM_X_POINTS; x++)
  11. z_values[y][x] = 0;
  12. }
  13. static inline bool vec_undef(const float v[2])
  14. {
  15. const uint32_t *vx = (const uint32_t*)v;
  16. return vx[0] == 0x0FFFFFFFF || vx[1] == 0x0FFFFFFFF;
  17. }
  18. #if MESH_NUM_X_POINTS>=5 && MESH_NUM_Y_POINTS>=5 && (MESH_NUM_X_POINTS&1)==1 && (MESH_NUM_Y_POINTS&1)==1
  19. // Works for an odd number of MESH_NUM_X_POINTS and MESH_NUM_Y_POINTS
  20. // #define MBL_BILINEAR
  21. void mesh_bed_leveling::upsample_3x3()
  22. {
  23. int idx0 = 0;
  24. int idx1 = MESH_NUM_X_POINTS / 2;
  25. int idx2 = MESH_NUM_X_POINTS - 1;
  26. {
  27. // First interpolate the points in X axis.
  28. static const float x0 = MESH_MIN_X;
  29. static const float x1 = 0.5f * float(MESH_MIN_X + MESH_MAX_X);
  30. static const float x2 = MESH_MAX_X;
  31. for (int j = 0; j < 3; ++ j) {
  32. // 1) Copy the source points to their new destination.
  33. z_values[j][idx2] = z_values[j][2];
  34. z_values[j][idx1] = z_values[j][1];
  35. // 2) Interpolate the remaining values by Largrangian polynomials.
  36. for (int i = idx0 + 1; i < idx2; ++ i) {
  37. if (i == idx1)
  38. continue;
  39. float x = get_x(i);
  40. #ifdef MBL_BILINEAR
  41. z_values[j][i] = (x < x1) ?
  42. ((z_values[j][idx0] * (x - x0) + z_values[j][idx1] * (x1 - x)) / (x1 - x0)) :
  43. ((z_values[j][idx1] * (x - x1) + z_values[j][idx2] * (x2 - x)) / (x2 - x1));
  44. #else
  45. z_values[j][i] =
  46. z_values[j][idx0] * (x - x1) * (x - x2) / ((x0 - x1) * (x0 - x2)) +
  47. z_values[j][idx1] * (x - x0) * (x - x2) / ((x1 - x0) * (x1 - x2)) +
  48. z_values[j][idx2] * (x - x0) * (x - x1) / ((x2 - x0) * (x2 - x1));
  49. #endif
  50. }
  51. }
  52. }
  53. {
  54. // Second interpolate the points in Y axis.
  55. static const float y0 = MESH_MIN_Y;
  56. static const float y1 = 0.5f * float(MESH_MIN_Y + MESH_MAX_Y);
  57. static const float y2 = MESH_MAX_Y;
  58. for (int i = 0; i < MESH_NUM_X_POINTS; ++ i) {
  59. // 1) Copy the intermediate points to their new destination.
  60. z_values[idx2][i] = z_values[2][i];
  61. z_values[idx1][i] = z_values[1][i];
  62. // 2) Interpolate the remaining values by Largrangian polynomials.
  63. for (int j = 1; j + 1 < MESH_NUM_Y_POINTS; ++ j) {
  64. if (j == idx1)
  65. continue;
  66. float y = get_y(j);
  67. #ifdef MBL_BILINEAR
  68. z_values[j][i] = (y < y1) ?
  69. ((z_values[idx0][i] * (y - y0) + z_values[idx1][i] * (y1 - y)) / (y1 - y0)) :
  70. ((z_values[idx1][i] * (y - y1) + z_values[idx2][i] * (y2 - y)) / (y2 - y1));
  71. #else
  72. z_values[j][i] =
  73. z_values[idx0][i] * (y - y1) * (y - y2) / ((y0 - y1) * (y0 - y2)) +
  74. z_values[idx1][i] * (y - y0) * (y - y2) / ((y1 - y0) * (y1 - y2)) +
  75. z_values[idx2][i] * (y - y0) * (y - y1) / ((y2 - y0) * (y2 - y1));
  76. #endif
  77. }
  78. }
  79. }
  80. /*
  81. // Relax the non-measured points.
  82. const float weight = 0.2f;
  83. for (uint8_t iter = 0; iter < 20; ++ iter) {
  84. for (int8_t j = 1; j < 6; ++ j) {
  85. for (int8_t i = 1; i < 6; ++ i) {
  86. if (i == 3 || j == 3)
  87. continue;
  88. if ((i % 3) == 0 && (j % 3) == 0)
  89. continue;
  90. float avg = 0.25f * (z_values[j][i-1]+z_values[j][i+1]+z_values[j-1][i]+z_values[j+1][i]);
  91. z_values[j][i] = (1.f-weight)*z_values[j][i] + weight*avg;
  92. }
  93. }
  94. }
  95. */
  96. }
  97. #endif
  98. #endif // MESH_BED_LEVELING