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

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