mesh_bed_calibration.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef MESH_BED_CALIBRATION_H
  2. #define MESH_BED_CALIBRATION_H
  3. // Exact positions of the print head above the bed reference points, in the world coordinates.
  4. // The world coordinates match the machine coordinates only in case, when the machine
  5. // is built properly, the end stops are at the correct positions and the axes are perpendicular.
  6. extern const float bed_ref_points[] PROGMEM;
  7. // Is the world2machine correction activated?
  8. enum World2MachineCorrectionMode
  9. {
  10. WORLD2MACHINE_CORRECTION_NONE = 0,
  11. WORLD2MACHINE_CORRECTION_SHIFT = 1,
  12. WORLD2MACHINE_CORRECTION_SKEW = 2,
  13. };
  14. extern uint8_t world2machine_correction_mode;
  15. // 2x2 transformation matrix from the world coordinates to the machine coordinates.
  16. // Corrects for the rotation and skew of the machine axes.
  17. // Used by the planner's plan_buffer_line() and plan_set_position().
  18. extern float world2machine_rotation_and_skew[2][2];
  19. extern float world2machine_rotation_and_skew_inv[2][2];
  20. // Shift of the machine zero point, in the machine coordinates.
  21. extern float world2machine_shift[2];
  22. // Resets the transformation to identity.
  23. extern void world2machine_reset();
  24. // Loads the transformation from the EEPROM, if available.
  25. extern void world2machine_initialize();
  26. // When switching from absolute to corrected coordinates,
  27. // this will apply an inverse world2machine transformation
  28. // to current_position[x,y].
  29. extern void world2machine_update_current();
  30. inline void world2machine(const float &x, const float &y, float &out_x, float &out_y)
  31. {
  32. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  33. // No correction.
  34. out_x = x;
  35. out_y = y;
  36. } else {
  37. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  38. // Firs the skew & rotation correction.
  39. out_x = world2machine_rotation_and_skew[0][0] * x + world2machine_rotation_and_skew[0][1] * y;
  40. out_y = world2machine_rotation_and_skew[1][0] * x + world2machine_rotation_and_skew[1][1] * y;
  41. }
  42. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  43. // Then add the offset.
  44. out_x += world2machine_shift[0];
  45. out_y += world2machine_shift[1];
  46. }
  47. }
  48. }
  49. inline void world2machine(float &x, float &y)
  50. {
  51. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  52. // No correction.
  53. } else {
  54. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  55. // Firs the skew & rotation correction.
  56. float out_x = world2machine_rotation_and_skew[0][0] * x + world2machine_rotation_and_skew[0][1] * y;
  57. float out_y = world2machine_rotation_and_skew[1][0] * x + world2machine_rotation_and_skew[1][1] * y;
  58. x = out_x;
  59. y = out_y;
  60. }
  61. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  62. // Then add the offset.
  63. x += world2machine_shift[0];
  64. y += world2machine_shift[1];
  65. }
  66. }
  67. }
  68. inline void machine2world(float x, float y, float &out_x, float &out_y)
  69. {
  70. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  71. // No correction.
  72. out_x = x;
  73. out_y = y;
  74. } else {
  75. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  76. // Then add the offset.
  77. x -= world2machine_shift[0];
  78. y -= world2machine_shift[1];
  79. }
  80. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  81. // Firs the skew & rotation correction.
  82. out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  83. out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  84. }
  85. }
  86. }
  87. inline void machine2world(float &x, float &y)
  88. {
  89. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  90. // No correction.
  91. } else {
  92. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  93. // Then add the offset.
  94. x -= world2machine_shift[0];
  95. y -= world2machine_shift[1];
  96. }
  97. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  98. // Firs the skew & rotation correction.
  99. float out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  100. float out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  101. x = out_x;
  102. y = out_y;
  103. }
  104. }
  105. }
  106. inline bool world2machine_clamp(float &x, float &y)
  107. {
  108. bool clamped = false;
  109. float tmpx, tmpy;
  110. world2machine(x, y, tmpx, tmpy);
  111. if (tmpx < X_MIN_POS) {
  112. tmpx = X_MIN_POS;
  113. clamped = true;
  114. }
  115. if (tmpy < Y_MIN_POS) {
  116. tmpy = Y_MIN_POS;
  117. clamped = true;
  118. }
  119. if (tmpx > X_MAX_POS) {
  120. tmpx = X_MAX_POS;
  121. clamped = true;
  122. }
  123. if (tmpy > Y_MAX_POS) {
  124. tmpy = Y_MAX_POS;
  125. clamped = true;
  126. }
  127. if (clamped)
  128. machine2world(tmpx, tmpy, x, y);
  129. return clamped;
  130. }
  131. extern bool find_bed_induction_sensor_point_z(float minimum_z = -10.f, uint8_t n_iter = 3);
  132. extern bool find_bed_induction_sensor_point_xy();
  133. // Positive or zero: ok
  134. // Negative: failed
  135. enum BedSkewOffsetDetectionResultType {
  136. // Detection failed, some point was not found.
  137. BED_SKEW_OFFSET_DETECTION_FAILED = -1,
  138. // Detection finished with success.
  139. BED_SKEW_OFFSET_DETECTION_PERFECT = 0,
  140. BED_SKEW_OFFSET_DETECTION_SKEW_MILD = 1,
  141. BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME = 2,
  142. // Detection finished with success, but it is recommended to fix the printer mechanically.
  143. BED_SKEW_OFFSET_DETECTION_FRONT_LEFT_FAR = 4,
  144. BED_SKEW_OFFSET_DETECTION_FRONT_RIGHT_FAR = 8,
  145. BED_SKEW_OFFSET_DETECTION_FRONT_BOTH_FAR = BED_SKEW_OFFSET_DETECTION_FRONT_LEFT_FAR | BED_SKEW_OFFSET_DETECTION_FRONT_RIGHT_FAR,
  146. };
  147. extern BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level);
  148. extern BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level);
  149. extern void reset_bed_offset_and_skew();
  150. extern bool is_bed_z_jitter_data_valid();
  151. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  152. // write the trigger coordinates to the serial line.
  153. // Useful for visualizing the behavior of the bed induction detector.
  154. extern bool scan_bed_induction_points(int8_t verbosity_level);
  155. #endif /* MESH_BED_CALIBRATION_H */