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.

runout.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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();
  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 &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 bool ran_out = response.has_run_out();
  103. TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, sei());
  104. if (ran_out) {
  105. filament_ran_out = true;
  106. event_filament_runout();
  107. planner.synchronize();
  108. }
  109. }
  110. }
  111. };
  112. /*************************** FILAMENT PRESENCE SENSORS ***************************/
  113. class FilamentSensorBase {
  114. protected:
  115. /**
  116. * Called by FilamentSensorSwitch::run when filament is detected.
  117. * Called by FilamentSensorEncoder::block_completed when motion is detected.
  118. */
  119. static inline void filament_present(const uint8_t extruder) {
  120. runout.filament_present(extruder); // ...which calls response.filament_present(extruder)
  121. }
  122. public:
  123. static inline void setup() {
  124. #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)
  125. #define INIT_RUNOUT_PIN(N) _INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN, FIL_RUNOUT##N##_STATE, FIL_RUNOUT##N##_PULLUP, FIL_RUNOUT##N##_PULLDOWN)
  126. #if NUM_RUNOUT_SENSORS >= 1
  127. INIT_RUNOUT_PIN(1);
  128. #endif
  129. #if NUM_RUNOUT_SENSORS >= 2
  130. INIT_RUNOUT_PIN(2);
  131. #endif
  132. #if NUM_RUNOUT_SENSORS >= 3
  133. INIT_RUNOUT_PIN(3);
  134. #endif
  135. #if NUM_RUNOUT_SENSORS >= 4
  136. INIT_RUNOUT_PIN(4);
  137. #endif
  138. #if NUM_RUNOUT_SENSORS >= 5
  139. INIT_RUNOUT_PIN(5);
  140. #endif
  141. #if NUM_RUNOUT_SENSORS >= 6
  142. INIT_RUNOUT_PIN(6);
  143. #endif
  144. #if NUM_RUNOUT_SENSORS >= 7
  145. INIT_RUNOUT_PIN(7);
  146. #endif
  147. #if NUM_RUNOUT_SENSORS >= 8
  148. INIT_RUNOUT_PIN(8);
  149. #endif
  150. #undef _INIT_RUNOUT_PIN
  151. #undef INIT_RUNOUT_PIN
  152. }
  153. // Return a bitmask of runout pin states
  154. static inline uint8_t poll_runout_pins() {
  155. #define _OR_RUNOUT(N) | (READ(FIL_RUNOUT##N##_PIN) ? _BV((N) - 1) : 0)
  156. return (0 REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _OR_RUNOUT));
  157. #undef _OR_RUNOUT
  158. }
  159. // Return a bitmask of runout flag states (1 bits always indicates runout)
  160. static inline uint8_t poll_runout_states() {
  161. return poll_runout_pins() ^ uint8_t(0
  162. #if NUM_RUNOUT_SENSORS >= 1
  163. | (FIL_RUNOUT1_STATE ? 0 : _BV(1 - 1))
  164. #endif
  165. #if NUM_RUNOUT_SENSORS >= 2
  166. | (FIL_RUNOUT2_STATE ? 0 : _BV(2 - 1))
  167. #endif
  168. #if NUM_RUNOUT_SENSORS >= 3
  169. | (FIL_RUNOUT3_STATE ? 0 : _BV(3 - 1))
  170. #endif
  171. #if NUM_RUNOUT_SENSORS >= 4
  172. | (FIL_RUNOUT4_STATE ? 0 : _BV(4 - 1))
  173. #endif
  174. #if NUM_RUNOUT_SENSORS >= 5
  175. | (FIL_RUNOUT5_STATE ? 0 : _BV(5 - 1))
  176. #endif
  177. #if NUM_RUNOUT_SENSORS >= 6
  178. | (FIL_RUNOUT6_STATE ? 0 : _BV(6 - 1))
  179. #endif
  180. #if NUM_RUNOUT_SENSORS >= 7
  181. | (FIL_RUNOUT7_STATE ? 0 : _BV(7 - 1))
  182. #endif
  183. #if NUM_RUNOUT_SENSORS >= 8
  184. | (FIL_RUNOUT8_STATE ? 0 : _BV(8 - 1))
  185. #endif
  186. );
  187. }
  188. };
  189. #if ENABLED(FILAMENT_MOTION_SENSOR)
  190. /**
  191. * This sensor uses a magnetic encoder disc and a Hall effect
  192. * sensor (or a slotted disc and optical sensor). The state
  193. * will toggle between 0 and 1 on filament movement. It can detect
  194. * filament runout and stripouts or jams.
  195. */
  196. class FilamentSensorEncoder : public FilamentSensorBase {
  197. private:
  198. static uint8_t motion_detected;
  199. static inline void poll_motion_sensor() {
  200. static uint8_t old_state;
  201. const uint8_t new_state = poll_runout_pins(),
  202. change = old_state ^ new_state;
  203. old_state = new_state;
  204. #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
  205. if (change) {
  206. SERIAL_ECHOPGM("Motion detected:");
  207. LOOP_L_N(e, NUM_RUNOUT_SENSORS)
  208. if (TEST(change, e)) SERIAL_CHAR(' ', '0' + e);
  209. SERIAL_EOL();
  210. }
  211. #endif
  212. motion_detected |= change;
  213. }
  214. public:
  215. static inline void block_completed(const block_t* const b) {
  216. // If the sensor wheel has moved since the last call to
  217. // this method reset the runout counter for the extruder.
  218. if (TEST(motion_detected, b->extruder))
  219. filament_present(b->extruder);
  220. // Clear motion triggers for next block
  221. motion_detected = 0;
  222. }
  223. static inline void run() { poll_motion_sensor(); }
  224. };
  225. #else
  226. /**
  227. * This is a simple endstop switch in the path of the filament.
  228. * It can detect filament runout, but not stripouts or jams.
  229. */
  230. class FilamentSensorSwitch : public FilamentSensorBase {
  231. private:
  232. static inline bool poll_runout_state(const uint8_t extruder) {
  233. const uint8_t runout_states = poll_runout_states();
  234. #if NUM_RUNOUT_SENSORS == 1
  235. UNUSED(extruder);
  236. #else
  237. if ( !TERN0(DUAL_X_CARRIAGE, idex_is_duplicating())
  238. && !TERN0(MULTI_NOZZLE_DUPLICATION, extruder_duplication_enabled)
  239. ) return TEST(runout_states, extruder); // A specific extruder ran out
  240. #endif
  241. return !!runout_states; // Any extruder ran out
  242. }
  243. public:
  244. static inline void block_completed(const block_t* const) {}
  245. static inline void run() {
  246. const bool out = poll_runout_state(active_extruder);
  247. if (!out) filament_present(active_extruder);
  248. #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
  249. static bool was_out = false;
  250. if (out != was_out) {
  251. was_out = out;
  252. SERIAL_ECHOPGM("Filament ");
  253. serialprintPGM(out ? PSTR("OUT\n") : PSTR("IN\n"));
  254. }
  255. #endif
  256. }
  257. };
  258. #endif // !FILAMENT_MOTION_SENSOR
  259. /********************************* RESPONSE TYPE *********************************/
  260. #if HAS_FILAMENT_RUNOUT_DISTANCE
  261. // RunoutResponseDelayed triggers a runout event only if the length
  262. // of filament specified by FILAMENT_RUNOUT_DISTANCE_MM has been fed
  263. // during a runout condition.
  264. class RunoutResponseDelayed {
  265. private:
  266. static volatile float runout_mm_countdown[EXTRUDERS];
  267. public:
  268. static float runout_distance_mm;
  269. static inline void reset() {
  270. LOOP_L_N(i, EXTRUDERS) filament_present(i);
  271. }
  272. static inline void run() {
  273. #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
  274. static millis_t t = 0;
  275. const millis_t ms = millis();
  276. if (ELAPSED(ms, t)) {
  277. t = millis() + 1000UL;
  278. LOOP_L_N(i, EXTRUDERS) {
  279. serialprintPGM(i ? PSTR(", ") : PSTR("Remaining mm: "));
  280. SERIAL_ECHO(runout_mm_countdown[i]);
  281. }
  282. SERIAL_EOL();
  283. }
  284. #endif
  285. }
  286. static inline bool has_run_out() {
  287. return runout_mm_countdown[active_extruder] < 0;
  288. }
  289. static inline void filament_present(const uint8_t extruder) {
  290. runout_mm_countdown[extruder] = runout_distance_mm;
  291. }
  292. static inline void block_completed(const block_t* const b) {
  293. if (b->steps.x || b->steps.y || b->steps.z || did_pause_print) { // Allow pause purge move to re-trigger runout state
  294. // Only trigger on extrusion with XYZ movement to allow filament change and retract/recover.
  295. const uint8_t e = b->extruder;
  296. const int32_t steps = b->steps.e;
  297. runout_mm_countdown[e] -= (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.steps_to_mm[E_AXIS_N(e)];
  298. }
  299. }
  300. };
  301. #else // !HAS_FILAMENT_RUNOUT_DISTANCE
  302. // RunoutResponseDebounced triggers a runout event after a runout
  303. // condition has been detected runout_threshold times in a row.
  304. class RunoutResponseDebounced {
  305. private:
  306. static constexpr int8_t runout_threshold = FILAMENT_RUNOUT_THRESHOLD;
  307. static int8_t runout_count;
  308. public:
  309. static inline void reset() { runout_count = runout_threshold; }
  310. static inline void run() { if (runout_count >= 0) runout_count--; }
  311. static inline bool has_run_out() { return runout_count < 0; }
  312. static inline void block_completed(const block_t* const) { }
  313. static inline void filament_present(const uint8_t) { runout_count = runout_threshold; }
  314. };
  315. #endif // !HAS_FILAMENT_RUNOUT_DISTANCE