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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #pragma once
  23. /**
  24. * feature/runout.h - Runout sensor support
  25. */
  26. #include "../sd/cardreader.h"
  27. #include "../module/printcounter.h"
  28. #include "../module/planner.h"
  29. #include "../module/stepper.h" // for block_t
  30. #include "../gcode/queue.h"
  31. #include "../feature/pause.h"
  32. #include "../inc/MarlinConfig.h"
  33. #if ENABLED(EXTENSIBLE_UI)
  34. #include "../lcd/extui/ui_api.h"
  35. #endif
  36. //#define FILAMENT_RUNOUT_SENSOR_DEBUG
  37. #ifndef FILAMENT_RUNOUT_THRESHOLD
  38. #define FILAMENT_RUNOUT_THRESHOLD 5
  39. #endif
  40. void event_filament_runout(const uint8_t extruder);
  41. template<class RESPONSE_T, class SENSOR_T>
  42. class TFilamentMonitor;
  43. class FilamentSensorEncoder;
  44. class FilamentSensorSwitch;
  45. class RunoutResponseDelayed;
  46. class RunoutResponseDebounced;
  47. /********************************* TEMPLATE SPECIALIZATION *********************************/
  48. typedef TFilamentMonitor<
  49. TERN(HAS_FILAMENT_RUNOUT_DISTANCE, RunoutResponseDelayed, RunoutResponseDebounced),
  50. TERN(FILAMENT_MOTION_SENSOR, FilamentSensorEncoder, FilamentSensorSwitch)
  51. > FilamentMonitor;
  52. extern FilamentMonitor runout;
  53. /*******************************************************************************************/
  54. class FilamentMonitorBase {
  55. public:
  56. static bool enabled, filament_ran_out;
  57. #if ENABLED(HOST_ACTION_COMMANDS)
  58. static bool host_handling;
  59. #else
  60. static constexpr bool host_handling = false;
  61. #endif
  62. };
  63. template<class RESPONSE_T, class SENSOR_T>
  64. class TFilamentMonitor : public FilamentMonitorBase {
  65. private:
  66. typedef RESPONSE_T response_t;
  67. typedef SENSOR_T sensor_t;
  68. static response_t response;
  69. static sensor_t sensor;
  70. public:
  71. static inline void setup() {
  72. sensor.setup();
  73. reset();
  74. }
  75. static inline void reset() {
  76. filament_ran_out = false;
  77. response.reset();
  78. }
  79. // Call this method when filament is present,
  80. // so the response can reset its counter.
  81. static inline void filament_present(const uint8_t extruder) {
  82. response.filament_present(extruder);
  83. }
  84. #if HAS_FILAMENT_RUNOUT_DISTANCE
  85. static inline float& runout_distance() { return response.runout_distance_mm; }
  86. static inline void set_runout_distance(const_float_t mm) { response.runout_distance_mm = mm; }
  87. #endif
  88. // Handle a block completion. RunoutResponseDelayed uses this to
  89. // add up the length of filament moved while the filament is out.
  90. static inline void block_completed(const block_t * const b) {
  91. if (enabled) {
  92. response.block_completed(b);
  93. sensor.block_completed(b);
  94. }
  95. }
  96. // Give the response a chance to update its counter.
  97. static inline void run() {
  98. if (enabled && !filament_ran_out && (printingIsActive() || did_pause_print)) {
  99. TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, cli()); // Prevent RunoutResponseDelayed::block_completed from accumulating here
  100. response.run();
  101. sensor.run();
  102. const uint8_t runout_flags = response.has_run_out();
  103. TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, sei());
  104. #if MULTI_FILAMENT_SENSOR
  105. #if ENABLED(WATCH_ALL_RUNOUT_SENSORS)
  106. const bool ran_out = !!runout_flags; // any sensor triggers
  107. uint8_t extruder = 0;
  108. if (ran_out) {
  109. uint8_t bitmask = runout_flags;
  110. while (!(bitmask & 1)) {
  111. bitmask >>= 1;
  112. extruder++;
  113. }
  114. }
  115. #else
  116. const bool ran_out = TEST(runout_flags, active_extruder); // suppress non active extruders
  117. uint8_t extruder = active_extruder;
  118. #endif
  119. #else
  120. const bool ran_out = !!runout_flags;
  121. uint8_t extruder = active_extruder;
  122. #endif
  123. #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
  124. if (runout_flags) {
  125. SERIAL_ECHOPGM("Runout Sensors: ");
  126. LOOP_L_N(i, 8) SERIAL_ECHO('0' + TEST(runout_flags, i));
  127. SERIAL_ECHOPGM(" -> ", extruder);
  128. if (ran_out) SERIAL_ECHOPGM(" RUN OUT");
  129. SERIAL_EOL();
  130. }
  131. #endif
  132. if (ran_out) {
  133. filament_ran_out = true;
  134. event_filament_runout(extruder);
  135. planner.synchronize();
  136. }
  137. }
  138. }
  139. };
  140. /*************************** FILAMENT PRESENCE SENSORS ***************************/
  141. class FilamentSensorBase {
  142. protected:
  143. /**
  144. * Called by FilamentSensorSwitch::run when filament is detected.
  145. * Called by FilamentSensorEncoder::block_completed when motion is detected.
  146. */
  147. static inline void filament_present(const uint8_t extruder) {
  148. runout.filament_present(extruder); // ...which calls response.filament_present(extruder)
  149. }
  150. public:
  151. static inline void setup() {
  152. #define _INIT_RUNOUT_PIN(P,S,U,D) do{ if (ENABLED(U)) SET_INPUT_PULLUP(P); else if (ENABLED(D)) SET_INPUT_PULLDOWN(P); else SET_INPUT(P); }while(0)
  153. #define INIT_RUNOUT_PIN(N) _INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN, FIL_RUNOUT##N##_STATE, FIL_RUNOUT##N##_PULLUP, FIL_RUNOUT##N##_PULLDOWN)
  154. #if NUM_RUNOUT_SENSORS >= 1
  155. INIT_RUNOUT_PIN(1);
  156. #endif
  157. #if NUM_RUNOUT_SENSORS >= 2
  158. INIT_RUNOUT_PIN(2);
  159. #endif
  160. #if NUM_RUNOUT_SENSORS >= 3
  161. INIT_RUNOUT_PIN(3);
  162. #endif
  163. #if NUM_RUNOUT_SENSORS >= 4
  164. INIT_RUNOUT_PIN(4);
  165. #endif
  166. #if NUM_RUNOUT_SENSORS >= 5
  167. INIT_RUNOUT_PIN(5);
  168. #endif
  169. #if NUM_RUNOUT_SENSORS >= 6
  170. INIT_RUNOUT_PIN(6);
  171. #endif
  172. #if NUM_RUNOUT_SENSORS >= 7
  173. INIT_RUNOUT_PIN(7);
  174. #endif
  175. #if NUM_RUNOUT_SENSORS >= 8
  176. INIT_RUNOUT_PIN(8);
  177. #endif
  178. #undef _INIT_RUNOUT_PIN
  179. #undef INIT_RUNOUT_PIN
  180. }
  181. // Return a bitmask of runout pin states
  182. static inline uint8_t poll_runout_pins() {
  183. #define _OR_RUNOUT(N) | (READ(FIL_RUNOUT##N##_PIN) ? _BV((N) - 1) : 0)
  184. return (0 REPEAT_1(NUM_RUNOUT_SENSORS, _OR_RUNOUT));
  185. #undef _OR_RUNOUT
  186. }
  187. // Return a bitmask of runout flag states (1 bits always indicates runout)
  188. static inline uint8_t poll_runout_states() {
  189. return poll_runout_pins() ^ uint8_t(0
  190. #if NUM_RUNOUT_SENSORS >= 1
  191. | (FIL_RUNOUT1_STATE ? 0 : _BV(1 - 1))
  192. #endif
  193. #if NUM_RUNOUT_SENSORS >= 2
  194. | (FIL_RUNOUT2_STATE ? 0 : _BV(2 - 1))
  195. #endif
  196. #if NUM_RUNOUT_SENSORS >= 3
  197. | (FIL_RUNOUT3_STATE ? 0 : _BV(3 - 1))
  198. #endif
  199. #if NUM_RUNOUT_SENSORS >= 4
  200. | (FIL_RUNOUT4_STATE ? 0 : _BV(4 - 1))
  201. #endif
  202. #if NUM_RUNOUT_SENSORS >= 5
  203. | (FIL_RUNOUT5_STATE ? 0 : _BV(5 - 1))
  204. #endif
  205. #if NUM_RUNOUT_SENSORS >= 6
  206. | (FIL_RUNOUT6_STATE ? 0 : _BV(6 - 1))
  207. #endif
  208. #if NUM_RUNOUT_SENSORS >= 7
  209. | (FIL_RUNOUT7_STATE ? 0 : _BV(7 - 1))
  210. #endif
  211. #if NUM_RUNOUT_SENSORS >= 8
  212. | (FIL_RUNOUT8_STATE ? 0 : _BV(8 - 1))
  213. #endif
  214. );
  215. }
  216. };
  217. #if ENABLED(FILAMENT_MOTION_SENSOR)
  218. /**
  219. * This sensor uses a magnetic encoder disc and a Hall effect
  220. * sensor (or a slotted disc and optical sensor). The state
  221. * will toggle between 0 and 1 on filament movement. It can detect
  222. * filament runout and stripouts or jams.
  223. */
  224. class FilamentSensorEncoder : public FilamentSensorBase {
  225. private:
  226. static uint8_t motion_detected;
  227. static inline void poll_motion_sensor() {
  228. static uint8_t old_state;
  229. const uint8_t new_state = poll_runout_pins(),
  230. change = old_state ^ new_state;
  231. old_state = new_state;
  232. #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
  233. if (change) {
  234. SERIAL_ECHOPGM("Motion detected:");
  235. LOOP_L_N(e, NUM_RUNOUT_SENSORS)
  236. if (TEST(change, e)) SERIAL_CHAR(' ', '0' + e);
  237. SERIAL_EOL();
  238. }
  239. #endif
  240. motion_detected |= change;
  241. }
  242. public:
  243. static inline void block_completed(const block_t * const b) {
  244. // If the sensor wheel has moved since the last call to
  245. // this method reset the runout counter for the extruder.
  246. if (TEST(motion_detected, b->extruder))
  247. filament_present(b->extruder);
  248. // Clear motion triggers for next block
  249. motion_detected = 0;
  250. }
  251. static inline void run() { poll_motion_sensor(); }
  252. };
  253. #else
  254. /**
  255. * This is a simple endstop switch in the path of the filament.
  256. * It can detect filament runout, but not stripouts or jams.
  257. */
  258. class FilamentSensorSwitch : public FilamentSensorBase {
  259. private:
  260. static inline bool poll_runout_state(const uint8_t extruder) {
  261. const uint8_t runout_states = poll_runout_states();
  262. #if MULTI_FILAMENT_SENSOR
  263. if ( !TERN0(DUAL_X_CARRIAGE, idex_is_duplicating())
  264. && !TERN0(MULTI_NOZZLE_DUPLICATION, extruder_duplication_enabled)
  265. ) return TEST(runout_states, extruder); // A specific extruder ran out
  266. #else
  267. UNUSED(extruder);
  268. #endif
  269. return !!runout_states; // Any extruder ran out
  270. }
  271. public:
  272. static inline void block_completed(const block_t * const) {}
  273. static inline void run() {
  274. LOOP_L_N(s, NUM_RUNOUT_SENSORS) {
  275. const bool out = poll_runout_state(s);
  276. if (!out) filament_present(s);
  277. #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
  278. static uint8_t was_out; // = 0
  279. if (out != TEST(was_out, s)) {
  280. TBI(was_out, s);
  281. SERIAL_ECHOLNF(F("Filament Sensor "), AS_DIGIT(s), out ? F(" OUT") : F(" IN"));
  282. }
  283. #endif
  284. }
  285. }
  286. };
  287. #endif // !FILAMENT_MOTION_SENSOR
  288. /********************************* RESPONSE TYPE *********************************/
  289. #if HAS_FILAMENT_RUNOUT_DISTANCE
  290. // RunoutResponseDelayed triggers a runout event only if the length
  291. // of filament specified by FILAMENT_RUNOUT_DISTANCE_MM has been fed
  292. // during a runout condition.
  293. class RunoutResponseDelayed {
  294. private:
  295. static volatile float runout_mm_countdown[NUM_RUNOUT_SENSORS];
  296. public:
  297. static float runout_distance_mm;
  298. static inline void reset() {
  299. LOOP_L_N(i, NUM_RUNOUT_SENSORS) filament_present(i);
  300. }
  301. static inline void run() {
  302. #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
  303. static millis_t t = 0;
  304. const millis_t ms = millis();
  305. if (ELAPSED(ms, t)) {
  306. t = millis() + 1000UL;
  307. LOOP_L_N(i, NUM_RUNOUT_SENSORS)
  308. SERIAL_ECHOF(i ? F(", ") : F("Remaining mm: "), runout_mm_countdown[i]);
  309. SERIAL_EOL();
  310. }
  311. #endif
  312. }
  313. static inline uint8_t has_run_out() {
  314. uint8_t runout_flags = 0;
  315. LOOP_L_N(i, NUM_RUNOUT_SENSORS) if (runout_mm_countdown[i] < 0) SBI(runout_flags, i);
  316. return runout_flags;
  317. }
  318. static inline void filament_present(const uint8_t extruder) {
  319. runout_mm_countdown[extruder] = runout_distance_mm;
  320. }
  321. static inline void block_completed(const block_t * const b) {
  322. if (b->steps.x || b->steps.y || b->steps.z || did_pause_print) { // Allow pause purge move to re-trigger runout state
  323. // Only trigger on extrusion with XYZ movement to allow filament change and retract/recover.
  324. const uint8_t e = b->extruder;
  325. const int32_t steps = b->steps.e;
  326. runout_mm_countdown[e] -= (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.mm_per_step[E_AXIS_N(e)];
  327. }
  328. }
  329. };
  330. #else // !HAS_FILAMENT_RUNOUT_DISTANCE
  331. // RunoutResponseDebounced triggers a runout event after a runout
  332. // condition has been detected runout_threshold times in a row.
  333. class RunoutResponseDebounced {
  334. private:
  335. static constexpr int8_t runout_threshold = FILAMENT_RUNOUT_THRESHOLD;
  336. static int8_t runout_count[NUM_RUNOUT_SENSORS];
  337. public:
  338. static inline void reset() {
  339. LOOP_L_N(i, NUM_RUNOUT_SENSORS) filament_present(i);
  340. }
  341. static inline void run() {
  342. LOOP_L_N(i, NUM_RUNOUT_SENSORS) if (runout_count[i] >= 0) runout_count[i]--;
  343. }
  344. static inline uint8_t has_run_out() {
  345. uint8_t runout_flags = 0;
  346. LOOP_L_N(i, NUM_RUNOUT_SENSORS) if (runout_count[i] < 0) SBI(runout_flags, i);
  347. return runout_flags;
  348. }
  349. static inline void block_completed(const block_t * const) { }
  350. static inline void filament_present(const uint8_t extruder) {
  351. runout_count[extruder] = runout_threshold;
  352. }
  353. };
  354. #endif // !HAS_FILAMENT_RUNOUT_DISTANCE