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

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