mesh_bed_calibration.h 7.2 KB

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