mesh_bed_calibration.h 6.7 KB

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