My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

backlash.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #if ENABLED(BACKLASH_COMPENSATION)
  24. #include "backlash.h"
  25. #include "../module/motion.h"
  26. #include "../module/planner.h"
  27. axis_bits_t Backlash::last_direction_bits;
  28. xyz_long_t Backlash::residual_error{0};
  29. #ifdef BACKLASH_DISTANCE_MM
  30. #if ENABLED(BACKLASH_GCODE)
  31. xyz_float_t Backlash::distance_mm = BACKLASH_DISTANCE_MM;
  32. #else
  33. const xyz_float_t Backlash::distance_mm = BACKLASH_DISTANCE_MM;
  34. #endif
  35. #endif
  36. #if ENABLED(BACKLASH_GCODE)
  37. uint8_t Backlash::correction = (BACKLASH_CORRECTION) * all_on;
  38. #ifdef BACKLASH_SMOOTHING_MM
  39. float Backlash::smoothing_mm = BACKLASH_SMOOTHING_MM;
  40. #endif
  41. #endif
  42. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  43. xyz_float_t Backlash::measured_mm{0};
  44. xyz_uint8_t Backlash::measured_count{0};
  45. #endif
  46. Backlash backlash;
  47. /**
  48. * To minimize seams in the printed part, backlash correction only adds
  49. * steps to the current segment (instead of creating a new segment, which
  50. * causes discontinuities and print artifacts).
  51. *
  52. * With a non-zero BACKLASH_SMOOTHING_MM value the backlash correction is
  53. * spread over multiple segments, smoothing out artifacts even more.
  54. */
  55. void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const axis_bits_t dm, block_t * const block) {
  56. axis_bits_t changed_dir = last_direction_bits ^ dm;
  57. // Ignore direction change unless steps are taken in that direction
  58. #if DISABLED(CORE_BACKLASH) || EITHER(MARKFORGED_XY, MARKFORGED_YX)
  59. if (!da) CBI(changed_dir, X_AXIS);
  60. if (!db) CBI(changed_dir, Y_AXIS);
  61. if (!dc) CBI(changed_dir, Z_AXIS);
  62. #elif CORE_IS_XY
  63. if (!(da + db)) CBI(changed_dir, X_AXIS);
  64. if (!(da - db)) CBI(changed_dir, Y_AXIS);
  65. if (!dc) CBI(changed_dir, Z_AXIS);
  66. #elif CORE_IS_XZ
  67. if (!(da + dc)) CBI(changed_dir, X_AXIS);
  68. if (!(da - dc)) CBI(changed_dir, Z_AXIS);
  69. if (!db) CBI(changed_dir, Y_AXIS);
  70. #elif CORE_IS_YZ
  71. if (!(db + dc)) CBI(changed_dir, Y_AXIS);
  72. if (!(db - dc)) CBI(changed_dir, Z_AXIS);
  73. if (!da) CBI(changed_dir, X_AXIS);
  74. #endif
  75. last_direction_bits ^= changed_dir;
  76. if (!correction && !residual_error) return;
  77. #ifdef BACKLASH_SMOOTHING_MM
  78. // The segment proportion is a value greater than 0.0 indicating how much residual_error
  79. // is corrected for in this segment. The contribution is based on segment length and the
  80. // smoothing distance. Since the computation of this proportion involves a floating point
  81. // division, defer computation until needed.
  82. float segment_proportion = 0;
  83. #endif
  84. const float f_corr = float(correction) / all_on;
  85. LOOP_NUM_AXES(axis) {
  86. if (distance_mm[axis]) {
  87. const bool reverse = TEST(dm, axis);
  88. // When an axis changes direction, add axis backlash to the residual error
  89. if (TEST(changed_dir, axis))
  90. residual_error[axis] += (reverse ? -f_corr : f_corr) * distance_mm[axis] * planner.settings.axis_steps_per_mm[axis];
  91. // Decide how much of the residual error to correct in this segment
  92. int32_t error_correction = residual_error[axis];
  93. if (reverse != (error_correction < 0))
  94. error_correction = 0; // Don't take up any backlash in this segment, as it would subtract steps
  95. #ifdef BACKLASH_SMOOTHING_MM
  96. if (error_correction && smoothing_mm != 0) {
  97. // Take up a portion of the residual_error in this segment
  98. if (segment_proportion == 0) segment_proportion = _MIN(1.0f, block->millimeters / smoothing_mm);
  99. error_correction = CEIL(segment_proportion * error_correction);
  100. }
  101. #endif
  102. // This correction reduces the residual error and adds block steps
  103. if (error_correction) {
  104. block->steps[axis] += ABS(error_correction);
  105. #if ENABLED(CORE_BACKLASH)
  106. switch (axis) {
  107. case CORE_AXIS_1:
  108. //block->steps[CORE_AXIS_2] += influence_distance_mm[axis] * planner.settings.axis_steps_per_mm[CORE_AXIS_2];
  109. //SERIAL_ECHOLNPGM("CORE_AXIS_1 dir change. distance=", distance_mm[axis], " r.err=", residual_error[axis],
  110. // " da=", da, " db=", db, " block->steps[axis]=", block->steps[axis], " err_corr=", error_correction);
  111. break;
  112. case CORE_AXIS_2:
  113. //block->steps[CORE_AXIS_1] += influence_distance_mm[axis] * planner.settings.axis_steps_per_mm[CORE_AXIS_1];;
  114. //SERIAL_ECHOLNPGM("CORE_AXIS_2 dir change. distance=", distance_mm[axis], " r.err=", residual_error[axis],
  115. // " da=", da, " db=", db, " block->steps[axis]=", block->steps[axis], " err_corr=", error_correction);
  116. break;
  117. case NORMAL_AXIS: break;
  118. }
  119. residual_error[axis] = 0; // No residual_error needed for next CORE block, I think...
  120. #else
  121. residual_error[axis] -= error_correction;
  122. #endif
  123. }
  124. }
  125. }
  126. }
  127. int32_t Backlash::get_applied_steps(const AxisEnum axis) {
  128. if (axis >= NUM_AXES) return 0;
  129. const bool reverse = TEST(last_direction_bits, axis);
  130. const int32_t residual_error_axis = residual_error[axis];
  131. // At startup it is assumed the last move was forwards. So the applied
  132. // steps will always be a non-positive number.
  133. if (!reverse) return -residual_error_axis;
  134. const float f_corr = float(correction) / all_on;
  135. const int32_t full_error_axis = -f_corr * distance_mm[axis] * planner.settings.axis_steps_per_mm[axis];
  136. return full_error_axis - residual_error_axis;
  137. }
  138. class Backlash::StepAdjuster {
  139. private:
  140. xyz_long_t applied_steps;
  141. public:
  142. StepAdjuster() {
  143. LOOP_NUM_AXES(axis) applied_steps[axis] = backlash.get_applied_steps((AxisEnum)axis);
  144. }
  145. ~StepAdjuster() {
  146. // after backlash compensation parameter changes, ensure applied step count does not change
  147. LOOP_NUM_AXES(axis) residual_error[axis] += backlash.get_applied_steps((AxisEnum)axis) - applied_steps[axis];
  148. }
  149. };
  150. #if ENABLED(BACKLASH_GCODE)
  151. void Backlash::set_correction_uint8(const uint8_t v) {
  152. StepAdjuster adjuster;
  153. correction = v;
  154. }
  155. void Backlash::set_distance_mm(const AxisEnum axis, const float v) {
  156. StepAdjuster adjuster;
  157. distance_mm[axis] = v;
  158. }
  159. #ifdef BACKLASH_SMOOTHING_MM
  160. void Backlash::set_smoothing_mm(const float v) {
  161. StepAdjuster adjuster;
  162. smoothing_mm = v;
  163. }
  164. #endif
  165. #endif
  166. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  167. #include "../module/probe.h"
  168. // Measure Z backlash by raising nozzle in increments until probe deactivates
  169. void Backlash::measure_with_probe() {
  170. if (measured_count.z == 255) return;
  171. const float start_height = current_position.z;
  172. while (current_position.z < (start_height + BACKLASH_MEASUREMENT_LIMIT) && PROBE_TRIGGERED())
  173. do_blocking_move_to_z(current_position.z + BACKLASH_MEASUREMENT_RESOLUTION, MMM_TO_MMS(BACKLASH_MEASUREMENT_FEEDRATE));
  174. // The backlash from all probe points is averaged, so count the number of measurements
  175. measured_mm.z += current_position.z - start_height;
  176. measured_count.z++;
  177. }
  178. #endif
  179. #endif // BACKLASH_COMPENSATION