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 11KB

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