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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <http://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. #ifdef BACKLASH_DISTANCE_MM
  28. #if ENABLED(BACKLASH_GCODE)
  29. xyz_float_t Backlash::distance_mm = BACKLASH_DISTANCE_MM;
  30. #else
  31. const xyz_float_t Backlash::distance_mm = BACKLASH_DISTANCE_MM;
  32. #endif
  33. #endif
  34. #if ENABLED(BACKLASH_GCODE)
  35. uint8_t Backlash::correction = (BACKLASH_CORRECTION) * 0xFF;
  36. #ifdef BACKLASH_SMOOTHING_MM
  37. float Backlash::smoothing_mm = BACKLASH_SMOOTHING_MM;
  38. #endif
  39. #endif
  40. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  41. xyz_float_t Backlash::measured_mm{0};
  42. xyz_uint8_t Backlash::measured_count{0};
  43. #endif
  44. Backlash backlash;
  45. /**
  46. * To minimize seams in the printed part, backlash correction only adds
  47. * steps to the current segment (instead of creating a new segment, which
  48. * causes discontinuities and print artifacts).
  49. *
  50. * With a non-zero BACKLASH_SMOOTHING_MM value the backlash correction is
  51. * spread over multiple segments, smoothing out artifacts even more.
  52. */
  53. void Backlash::add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const uint8_t dm, block_t * const block) {
  54. static uint8_t last_direction_bits;
  55. uint8_t changed_dir = last_direction_bits ^ dm;
  56. // Ignore direction change if no steps are taken in that direction
  57. if (da == 0) CBI(changed_dir, X_AXIS);
  58. if (db == 0) CBI(changed_dir, Y_AXIS);
  59. if (dc == 0) CBI(changed_dir, Z_AXIS);
  60. last_direction_bits ^= changed_dir;
  61. if (correction == 0) return;
  62. #ifdef BACKLASH_SMOOTHING_MM
  63. // The segment proportion is a value greater than 0.0 indicating how much residual_error
  64. // is corrected for in this segment. The contribution is based on segment length and the
  65. // smoothing distance. Since the computation of this proportion involves a floating point
  66. // division, defer computation until needed.
  67. float segment_proportion = 0;
  68. // Residual error carried forward across multiple segments, so correction can be applied
  69. // to segments where there is no direction change.
  70. static xyz_long_t residual_error{0};
  71. #else
  72. // No direction change, no correction.
  73. if (!changed_dir) return;
  74. // No leftover residual error from segment to segment
  75. xyz_long_t residual_error{0};
  76. #endif
  77. const float f_corr = float(correction) / 255.0f;
  78. LOOP_XYZ(axis) {
  79. if (distance_mm[axis]) {
  80. const bool reversing = TEST(dm,axis);
  81. // When an axis changes direction, add axis backlash to the residual error
  82. if (TEST(changed_dir, axis))
  83. residual_error[axis] += (reversing ? -f_corr : f_corr) * distance_mm[axis] * planner.settings.axis_steps_per_mm[axis];
  84. // Decide how much of the residual error to correct in this segment
  85. int32_t error_correction = residual_error[axis];
  86. #ifdef BACKLASH_SMOOTHING_MM
  87. if (error_correction && smoothing_mm != 0) {
  88. // Take up a portion of the residual_error in this segment, but only when
  89. // the current segment travels in the same direction as the correction
  90. if (reversing == (error_correction < 0)) {
  91. if (segment_proportion == 0)
  92. segment_proportion = _MIN(1.0f, block->millimeters / smoothing_mm);
  93. error_correction = CEIL(segment_proportion * error_correction);
  94. }
  95. else
  96. error_correction = 0; // Don't take up any backlash in this segment, as it would subtract steps
  97. }
  98. #endif
  99. // Making a correction reduces the residual error and modifies delta_mm
  100. if (error_correction) {
  101. block->steps[axis] += ABS(error_correction);
  102. residual_error[axis] -= error_correction;
  103. }
  104. }
  105. }
  106. }
  107. #if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
  108. #if HAS_CUSTOM_PROBE_PIN
  109. #define TEST_PROBE_PIN (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING)
  110. #else
  111. #define TEST_PROBE_PIN (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)
  112. #endif
  113. // Measure Z backlash by raising nozzle in increments until probe deactivates
  114. void Backlash::measure_with_probe() {
  115. if (measured_count.z == 255) return;
  116. const float start_height = current_position.z;
  117. while (current_position.z < (start_height + BACKLASH_MEASUREMENT_LIMIT) && TEST_PROBE_PIN)
  118. do_blocking_move_to_z(current_position.z + BACKLASH_MEASUREMENT_RESOLUTION, MMM_TO_MMS(BACKLASH_MEASUREMENT_FEEDRATE));
  119. // The backlash from all probe points is averaged, so count the number of measurements
  120. measured_mm.z += current_position.z - start_height;
  121. measured_count.z++;
  122. }
  123. #endif
  124. #endif // BACKLASH_COMPENSATION