mesh_bed_calibration.h 6.4 KB

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