My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #include "../module/planner.h"
  25. #include "../module/thermistor/thermistors.h"
  26. class FilamentWidthSensor {
  27. public:
  28. static constexpr int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
  29. static bool enabled; // (M405-M406) Filament Width Sensor ON/OFF.
  30. static uint32_t accum; // ADC accumulator
  31. static uint16_t raw; // Measured filament diameter - one extruder only
  32. static float nominal_mm, // (M104) Nominal filament width
  33. measured_mm, // Measured filament diameter
  34. e_count, delay_dist;
  35. static uint8_t meas_delay_cm; // Distance delay setting
  36. static int8_t ratios[MMD_CM], // Ring buffer to delay measurement. (Extruder factor minus 100)
  37. index_r, index_w; // Indexes into ring buffer
  38. FilamentWidthSensor() { init(); }
  39. static void init();
  40. static inline void enable(const bool ena) { enabled = ena; }
  41. static inline void set_delay_cm(const uint8_t cm) {
  42. meas_delay_cm = _MIN(cm, MAX_MEASUREMENT_DELAY);
  43. }
  44. /**
  45. * Convert Filament Width (mm) to an extrusion ratio
  46. * and reduce to an 8 bit value.
  47. *
  48. * A nominal width of 1.75 and measured width of 1.73
  49. * gives (100 * 1.75 / 1.73) for a ratio of 101 and
  50. * a return value of 1.
  51. */
  52. static int8_t sample_to_size_ratio() {
  53. return ABS(nominal_mm - measured_mm) <= FILWIDTH_ERROR_MARGIN
  54. ? int(100.0f * nominal_mm / measured_mm) - 100 : 0;
  55. }
  56. // Apply a single ADC reading to the raw value
  57. static void accumulate(const uint16_t adc) {
  58. if (adc > 102) // Ignore ADC under 0.5 volts
  59. accum += (uint32_t(adc) << 7) - (accum >> 7);
  60. }
  61. // Convert raw measurement to mm
  62. static inline float raw_to_mm(const uint16_t v) { return v * 5.0f * RECIPROCAL(float(MAX_RAW_THERMISTOR_VALUE)); }
  63. static inline float raw_to_mm() { return raw_to_mm(raw); }
  64. // A scaled reading is ready
  65. // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  66. static inline void reading_ready() { raw = accum >> 10; }
  67. // Update mm from the raw measurement
  68. static inline void update_measured_mm() { measured_mm = raw_to_mm(); }
  69. // Update ring buffer used to delay filament measurements
  70. static inline void advance_e(const float &e_move) {
  71. // Increment counters with the E distance
  72. e_count += e_move;
  73. delay_dist += e_move;
  74. // Only get new measurements on forward E movement
  75. if (!UNEAR_ZERO(e_count)) {
  76. // Loop the delay distance counter (modulus by the mm length)
  77. while (delay_dist >= MMD_MM) delay_dist -= MMD_MM;
  78. // Convert into an index (cm) into the measurement array
  79. index_r = int8_t(delay_dist * 0.1f);
  80. // If the ring buffer is not full...
  81. if (index_r != index_w) {
  82. e_count = 0; // Reset the E movement counter
  83. const int8_t meas_sample = sample_to_size_ratio();
  84. do {
  85. if (++index_w >= MMD_CM) index_w = 0; // The next unused slot
  86. ratios[index_w] = meas_sample; // Store the measurement
  87. } while (index_r != index_w); // More slots to fill?
  88. }
  89. }
  90. }
  91. // Dynamically set the volumetric multiplier based on the delayed width measurement.
  92. static inline void update_volumetric() {
  93. if (enabled) {
  94. int8_t read_index = index_r - meas_delay_cm;
  95. if (read_index < 0) read_index += MMD_CM; // Loop around buffer if needed
  96. LIMIT(read_index, 0, MAX_MEASUREMENT_DELAY);
  97. planner.apply_filament_width_sensor(ratios[read_index]);
  98. }
  99. }
  100. };
  101. extern FilamentWidthSensor filwidth;