mesh_bed_calibration.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(float &x, float &y)
  35. {
  36. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  37. // No correction.
  38. } else {
  39. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  40. // Firs the skew & rotation correction.
  41. float out_x = world2machine_rotation_and_skew[0][0] * x + world2machine_rotation_and_skew[0][1] * y;
  42. float out_y = world2machine_rotation_and_skew[1][0] * x + world2machine_rotation_and_skew[1][1] * y;
  43. x = out_x;
  44. y = out_y;
  45. }
  46. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  47. // Then add the offset.
  48. x += world2machine_shift[0];
  49. y += world2machine_shift[1];
  50. }
  51. }
  52. }
  53. inline void world2machine(const float &x, const float &y, float &out_x, float &out_y)
  54. {
  55. out_x = x;
  56. out_y = y;
  57. world2machine(out_x, out_y);
  58. }
  59. inline void machine2world(float x, float y, float &out_x, float &out_y)
  60. {
  61. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  62. // No correction.
  63. out_x = x;
  64. out_y = y;
  65. } else {
  66. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  67. // Then add the offset.
  68. x -= world2machine_shift[0];
  69. y -= world2machine_shift[1];
  70. }
  71. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  72. // Firs the skew & rotation correction.
  73. out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  74. out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  75. }
  76. }
  77. }
  78. inline void machine2world(float &x, float &y)
  79. {
  80. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  81. // No correction.
  82. } else {
  83. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  84. // Then add the offset.
  85. x -= world2machine_shift[0];
  86. y -= world2machine_shift[1];
  87. }
  88. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  89. // Firs the skew & rotation correction.
  90. float out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  91. float out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  92. x = out_x;
  93. y = out_y;
  94. }
  95. }
  96. }
  97. inline bool world2machine_clamp(float &x, float &y)
  98. {
  99. bool clamped = false;
  100. float tmpx, tmpy;
  101. world2machine(x, y, tmpx, tmpy);
  102. if (tmpx < X_MIN_POS) {
  103. tmpx = X_MIN_POS;
  104. clamped = true;
  105. }
  106. if (tmpy < Y_MIN_POS) {
  107. tmpy = Y_MIN_POS;
  108. clamped = true;
  109. }
  110. if (tmpx > X_MAX_POS) {
  111. tmpx = X_MAX_POS;
  112. clamped = true;
  113. }
  114. if (tmpy > Y_MAX_POS) {
  115. tmpy = Y_MAX_POS;
  116. clamped = true;
  117. }
  118. if (clamped)
  119. machine2world(tmpx, tmpy, x, y);
  120. return clamped;
  121. }
  122. extern bool find_bed_induction_sensor_point_z(float minimum_z = -10.f, uint8_t n_iter = 3, int verbosity_level = 0);
  123. extern bool find_bed_induction_sensor_point_xy(int verbosity_level = 0);
  124. extern void go_home_with_z_lift();
  125. // Positive or zero: ok
  126. // Negative: failed
  127. enum BedSkewOffsetDetectionResultType {
  128. // Detection failed, some point was not found.
  129. BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND = -1,
  130. BED_SKEW_OFFSET_DETECTION_FITTING_FAILED = -2,
  131. // Detection finished with success.
  132. BED_SKEW_OFFSET_DETECTION_PERFECT = 0,
  133. BED_SKEW_OFFSET_DETECTION_SKEW_MILD = 1,
  134. BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME = 2
  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();
  158. extern bool sample_z();
  159. #endif /* MESH_BED_CALIBRATION_H */