mesh_bed_calibration.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Resets the transformation to identity and update current_position[X,Y] from the servos.
  25. extern void world2machine_revert_to_uncorrected();
  26. // Loads the transformation from the EEPROM, if available.
  27. extern void world2machine_initialize();
  28. // When switching from absolute to corrected coordinates,
  29. // this will apply an inverse world2machine transformation
  30. // to current_position[x,y].
  31. extern void world2machine_update_current();
  32. inline void world2machine(const float &x, const float &y, float &out_x, float &out_y)
  33. {
  34. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  35. // No correction.
  36. out_x = x;
  37. out_y = y;
  38. } else {
  39. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  40. // Firs the skew & rotation correction.
  41. out_x = world2machine_rotation_and_skew[0][0] * x + world2machine_rotation_and_skew[0][1] * y;
  42. out_y = world2machine_rotation_and_skew[1][0] * x + world2machine_rotation_and_skew[1][1] * y;
  43. }
  44. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  45. // Then add the offset.
  46. out_x += world2machine_shift[0];
  47. out_y += world2machine_shift[1];
  48. }
  49. }
  50. }
  51. inline void world2machine(float &x, float &y)
  52. {
  53. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  54. // No correction.
  55. } else {
  56. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  57. // Firs the skew & rotation correction.
  58. float out_x = world2machine_rotation_and_skew[0][0] * x + world2machine_rotation_and_skew[0][1] * y;
  59. float out_y = world2machine_rotation_and_skew[1][0] * x + world2machine_rotation_and_skew[1][1] * y;
  60. x = out_x;
  61. y = out_y;
  62. }
  63. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  64. // Then add the offset.
  65. x += world2machine_shift[0];
  66. y += world2machine_shift[1];
  67. }
  68. }
  69. }
  70. inline void machine2world(float x, float y, float &out_x, float &out_y)
  71. {
  72. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  73. // No correction.
  74. out_x = x;
  75. out_y = y;
  76. } else {
  77. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  78. // Then add the offset.
  79. x -= world2machine_shift[0];
  80. y -= world2machine_shift[1];
  81. }
  82. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  83. // Firs the skew & rotation correction.
  84. out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  85. out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  86. }
  87. }
  88. }
  89. inline void machine2world(float &x, float &y)
  90. {
  91. if (world2machine_correction_mode == WORLD2MACHINE_CORRECTION_NONE) {
  92. // No correction.
  93. } else {
  94. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SHIFT) {
  95. // Then add the offset.
  96. x -= world2machine_shift[0];
  97. y -= world2machine_shift[1];
  98. }
  99. if (world2machine_correction_mode & WORLD2MACHINE_CORRECTION_SKEW) {
  100. // Firs the skew & rotation correction.
  101. float out_x = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
  102. float out_y = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
  103. x = out_x;
  104. y = out_y;
  105. }
  106. }
  107. }
  108. inline bool world2machine_clamp(float &x, float &y)
  109. {
  110. bool clamped = false;
  111. float tmpx, tmpy;
  112. world2machine(x, y, tmpx, tmpy);
  113. if (tmpx < X_MIN_POS) {
  114. tmpx = X_MIN_POS;
  115. clamped = true;
  116. }
  117. if (tmpy < Y_MIN_POS) {
  118. tmpy = Y_MIN_POS;
  119. clamped = true;
  120. }
  121. if (tmpx > X_MAX_POS) {
  122. tmpx = X_MAX_POS;
  123. clamped = true;
  124. }
  125. if (tmpy > Y_MAX_POS) {
  126. tmpy = Y_MAX_POS;
  127. clamped = true;
  128. }
  129. if (clamped)
  130. machine2world(tmpx, tmpy, x, y);
  131. return clamped;
  132. }
  133. extern bool find_bed_induction_sensor_point_z(float minimum_z = -10.f, uint8_t n_iter = 3);
  134. extern bool find_bed_induction_sensor_point_xy();
  135. extern void go_home_with_z_lift();
  136. // Positive or zero: ok
  137. // Negative: failed
  138. enum BedSkewOffsetDetectionResultType {
  139. // Detection failed, some point was not found.
  140. BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND = -1,
  141. BED_SKEW_OFFSET_DETECTION_FITTING_FAILED = -2,
  142. // Detection finished with success.
  143. BED_SKEW_OFFSET_DETECTION_PERFECT = 0,
  144. BED_SKEW_OFFSET_DETECTION_SKEW_MILD = 1,
  145. BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME = 2
  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, uint8_t &too_far_mask);
  149. extern bool sample_mesh_and_store_reference();
  150. extern void reset_bed_offset_and_skew();
  151. extern bool is_bed_z_jitter_data_valid();
  152. // Scan the mesh bed induction points one by one by a left-right zig-zag movement,
  153. // write the trigger coordinates to the serial line.
  154. // Useful for visualizing the behavior of the bed induction detector.
  155. extern bool scan_bed_induction_points(int8_t verbosity_level);
  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. #endif /* MESH_BED_CALIBRATION_H */