My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

runout.h 11KB

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