mesh_bed_calibration.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. extern const float bed_ref_points_4[] PROGMEM;
  8. extern const float bed_skew_angle_mild;
  9. extern const float bed_skew_angle_extreme;
  10. // Is the world2machine correction activated?
  11. enum World2MachineCorrectionMode
  12. {
  13. WORLD2MACHINE_CORRECTION_NONE = 0,
  14. WORLD2MACHINE_CORRECTION_SHIFT = 1,
  15. WORLD2MACHINE_CORRECTION_SKEW = 2,
  16. };
  17. extern uint8_t world2machine_correction_mode;
  18. // 2x2 transformation matrix from the world coordinates to the machine coordinates.
  19. // Corrects for the rotation and skew of the machine axes.
  20. // Used by the planner's plan_buffer_line() and plan_set_position().
  21. extern float world2machine_rotation_and_skew[2][2];
  22. extern float world2machine_rotation_and_skew_inv[2][2];
  23. // Shift of the machine zero point, in the machine coordinates.
  24. extern float world2machine_shift[2];
  25. extern void world2machine_reset();
  26. extern void world2machine_revert_to_uncorrected();
  27. extern void world2machine_initialize();
  28. extern void world2machine_read_valid(float vec_x[2], float vec_y[2], float cntr[2]);
  29. extern void world2machine_update_current();
  30. inline void world2machine(float &x, float &y)
  31. {
  32. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  33. // No correction.
  34. } else {
  35. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  36. // Firs the skew & rotation correction.
  37. float out_x = world2machine_rotation_and_skew[0][0] * x + world2machine_rotation_and_skew[0][1] * y;
  38. float out_y = world2machine_rotation_and_skew[1][0] * x + world2machine_rotation_and_skew[1][1] * y;
  39. x = out_x;
  40. y = out_y;
  41. }
  42. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  43. // Then add the offset.
  44. x += world2machine_shift[0];
  45. y += world2machine_shift[1];
  46. }
  47. }
  48. }
  49. inline void world2machine(const float &x, const float &y, float &out_x, float &out_y)
  50. {
  51. out_x = x;
  52. out_y = y;
  53. world2machine(out_x, out_y);
  54. }
  55. inline void machine2world(float x, float y, float &out_x, float &out_y)
  56. {
  57. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  58. // No correction.
  59. out_x = x;
  60. out_y = y;
  61. } else {
  62. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  63. // Then add the offset.
  64. x -= world2machine_shift[0];
  65. y -= world2machine_shift[1];
  66. }
  67. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  68. // Firs the skew & rotation correction.
  69. out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  70. out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  71. }
  72. }
  73. }
  74. inline void machine2world(float &x, float &y)
  75. {
  76. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  77. // No correction.
  78. } else {
  79. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  80. // Then add the offset.
  81. x -= world2machine_shift[0];
  82. y -= world2machine_shift[1];
  83. }
  84. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  85. // Firs the skew & rotation correction.
  86. float out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  87. float out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  88. x = out_x;
  89. y = out_y;
  90. }
  91. }
  92. }
  93. inline bool world2machine_clamp(float &x, float &y)
  94. {
  95. bool clamped = false;
  96. float tmpx, tmpy;
  97. world2machine(x, y, tmpx, tmpy);
  98. if (tmpx < X_MIN_POS) {
  99. tmpx = X_MIN_POS;
  100. clamped = true;
  101. }
  102. if (tmpy < Y_MIN_POS) {
  103. tmpy = Y_MIN_POS;
  104. clamped = true;
  105. }
  106. if (tmpx > X_MAX_POS) {
  107. tmpx = X_MAX_POS;
  108. clamped = true;
  109. }
  110. if (tmpy > Y_MAX_POS) {
  111. tmpy = Y_MAX_POS;
  112. clamped = true;
  113. }
  114. if (clamped)
  115. machine2world(tmpx, tmpy, x, y);
  116. return clamped;
  117. }
  118. extern bool find_bed_induction_sensor_point_z(float minimum_z = -10.f, uint8_t n_iter = 3, int verbosity_level = 0);
  119. extern bool find_bed_induction_sensor_point_xy(int verbosity_level = 0);
  120. extern void go_home_with_z_lift();
  121. /**
  122. * @brief Bed skew and offest detection result
  123. *
  124. * Positive or zero: ok
  125. * Negative: failed
  126. */
  127. enum BedSkewOffsetDetectionResultType {
  128. // Detection failed, some point was not found.
  129. BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND = -1, //!< Point not found.
  130. BED_SKEW_OFFSET_DETECTION_FITTING_FAILED = -2, //!< Fitting failed
  131. // Detection finished with success.
  132. BED_SKEW_OFFSET_DETECTION_PERFECT = 0, //!< Perfect.
  133. BED_SKEW_OFFSET_DETECTION_SKEW_MILD = 1, //!< Mildly skewed.
  134. BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME = 2 //!< Extremely skewed.
  135. };
  136. extern BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level, uint8_t &too_far_mask);
  137. #ifndef NEW_XYZCAL
  138. extern BedSkewOffsetDetectionResultType improve_bed_offset_and_skew(int8_t method, int8_t verbosity_level, uint8_t &too_far_mask);
  139. #endif //NEW_XYZCAL
  140. extern bool sample_mesh_and_store_reference();
  141. extern void reset_bed_offset_and_skew();
  142. extern bool is_bed_z_jitter_data_valid();
  143. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  144. // write the trigger coordinates to the serial line.
  145. // Useful for visualizing the behavior of the bed induction detector.
  146. extern bool scan_bed_induction_points(int8_t verbosity_level);
  147. // Load Z babystep value from the EEPROM into babystepLoadZ,
  148. // but don't apply it through the planner. This is useful on wake up
  149. // after power panic, when it is expected, that the baby step has been already applied.
  150. extern void babystep_load();
  151. // Apply Z babystep value from the EEPROM through the planner.
  152. extern void babystep_apply();
  153. // Undo the current Z babystep value.
  154. extern void babystep_undo();
  155. // Reset the current babystep counter without moving the axes.
  156. extern void babystep_reset();
  157. extern void count_xyz_details(float (&distanceMin)[2]);
  158. extern bool sample_z();
  159. #endif /* MESH_BED_CALIBRATION_H */