Filament_sensor.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. #pragma once
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. #include <avr/pgmspace.h>
  5. #include <util/atomic.h>
  6. #include "Marlin.h"
  7. #include "ultralcd.h"
  8. #include "menu.h"
  9. #include "cardreader.h"
  10. #include "temperature.h"
  11. #include "cmdqueue.h"
  12. #include "eeprom.h"
  13. #include "pins.h"
  14. #include "fastio.h"
  15. #include "adc.h"
  16. #include "Timer.h"
  17. #include "pat9125.h"
  18. #define FSENSOR_IR 1
  19. #define FSENSOR_IR_ANALOG 2
  20. #define FSENSOR_PAT9125 3
  21. #ifdef FILAMENT_SENSOR
  22. class Filament_sensor {
  23. public:
  24. virtual void init() = 0;
  25. virtual void deinit() = 0;
  26. virtual bool update() = 0;
  27. virtual bool getFilamentPresent() = 0;
  28. #ifdef FSENSOR_PROBING
  29. virtual bool probeOtherType() = 0; //checks if the wrong fsensor type is detected.
  30. #endif
  31. enum class State : uint8_t {
  32. disabled = 0,
  33. initializing,
  34. ready,
  35. error,
  36. };
  37. enum class SensorActionOnError : uint8_t {
  38. _Continue = 0,
  39. _Pause = 1,
  40. _Undef = EEPROM_EMPTY_VALUE
  41. };
  42. void setEnabled(bool enabled) {
  43. eeprom_update_byte((uint8_t *)EEPROM_FSENSOR, enabled);
  44. if (enabled) {
  45. init();
  46. }
  47. else {
  48. deinit();
  49. }
  50. }
  51. void setAutoLoadEnabled(bool state, bool updateEEPROM = false) {
  52. autoLoadEnabled = state;
  53. if (updateEEPROM) {
  54. eeprom_update_byte((uint8_t *)EEPROM_FSENS_AUTOLOAD_ENABLED, state);
  55. }
  56. }
  57. bool getAutoLoadEnabled() {
  58. return autoLoadEnabled;
  59. }
  60. void setRunoutEnabled(bool state, bool updateEEPROM = false) {
  61. runoutEnabled = state;
  62. if (updateEEPROM) {
  63. eeprom_update_byte((uint8_t *)EEPROM_FSENS_RUNOUT_ENABLED, state);
  64. }
  65. }
  66. bool getRunoutEnabled() {
  67. return runoutEnabled;
  68. }
  69. void setActionOnError(SensorActionOnError state, bool updateEEPROM = false) {
  70. sensorActionOnError = state;
  71. if (updateEEPROM) {
  72. eeprom_update_byte((uint8_t *)EEPROM_FSENSOR_ACTION_NA, (uint8_t)state);
  73. }
  74. }
  75. SensorActionOnError getActionOnError() {
  76. return sensorActionOnError;
  77. }
  78. bool getFilamentLoadEvent() {
  79. return postponedLoadEvent;
  80. }
  81. bool isError() {
  82. return state == State::error;
  83. }
  84. bool isReady() {
  85. return state == State::ready;
  86. }
  87. bool isEnabled() {
  88. return state != State::disabled;
  89. }
  90. protected:
  91. void settings_init() {
  92. bool enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
  93. if ((state != State::disabled) != enabled) {
  94. state = enabled ? State::initializing : State::disabled;
  95. }
  96. autoLoadEnabled = eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
  97. runoutEnabled = eeprom_read_byte((uint8_t*)EEPROM_FSENS_RUNOUT_ENABLED);
  98. sensorActionOnError = (SensorActionOnError)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_ACTION_NA);
  99. if (sensorActionOnError == SensorActionOnError::_Undef) {
  100. sensorActionOnError = SensorActionOnError::_Continue;
  101. }
  102. }
  103. bool checkFilamentEvents() {
  104. if (state != State::ready)
  105. return false;
  106. if (eventBlankingTimer.running() && !eventBlankingTimer.expired(100)) {// event blanking for 100ms
  107. return false;
  108. }
  109. bool newFilamentPresent = getFilamentPresent();
  110. if (oldFilamentPresent != newFilamentPresent) {
  111. oldFilamentPresent = newFilamentPresent;
  112. eventBlankingTimer.start();
  113. if (newFilamentPresent) { //filament insertion
  114. puts_P(PSTR("filament inserted"));
  115. triggerFilamentInserted();
  116. postponedLoadEvent = true;
  117. }
  118. else { //filament removal
  119. puts_P(PSTR("filament removed"));
  120. triggerFilamentRemoved();
  121. }
  122. return true;
  123. }
  124. return false;
  125. };
  126. void triggerFilamentInserted() {
  127. if (autoLoadEnabled && (eFilamentAction == FilamentAction::None) && !(moves_planned() || IS_SD_PRINTING || usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal) || eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE))) {
  128. filAutoLoad();
  129. }
  130. }
  131. void triggerFilamentRemoved() {
  132. if (runoutEnabled && (eFilamentAction == FilamentAction::None) && !saved_printing && (moves_planned() || IS_SD_PRINTING || usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal) || eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE))) {
  133. filRunout();
  134. }
  135. }
  136. void filAutoLoad() {
  137. eFilamentAction = FilamentAction::AutoLoad;
  138. if(target_temperature[0] >= EXTRUDE_MINTEMP){
  139. bFilamentPreheatState = true;
  140. menu_submenu(mFilamentItemForce);
  141. }
  142. else {
  143. menu_submenu(lcd_generic_preheat_menu);
  144. lcd_timeoutToStatus.start();
  145. }
  146. }
  147. void filRunout() {
  148. runoutEnabled = false;
  149. autoLoadEnabled = false;
  150. stop_and_save_print_to_ram(0, 0);
  151. restore_print_from_ram_and_continue(0);
  152. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  153. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  154. enquecommand_front_P((PSTR("M600")));
  155. }
  156. void triggerError() {
  157. state = State::error;
  158. /// some message, idk
  159. ;//
  160. }
  161. State state;
  162. bool autoLoadEnabled;
  163. bool runoutEnabled;
  164. bool oldFilamentPresent; //for creating filament presence switching events.
  165. bool postponedLoadEvent; //this event lasts exactly one update cycle. It is long enough to be able to do polling for load event.
  166. ShortTimer eventBlankingTimer;
  167. SensorActionOnError sensorActionOnError;
  168. };
  169. #if (FILAMENT_SENSOR_TYPE == FSENSOR_IR) || (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  170. class IR_sensor: public Filament_sensor {
  171. public:
  172. void init() {
  173. if (state == State::error) {
  174. deinit(); //deinit first if there was an error.
  175. }
  176. puts_P(PSTR("fsensor::init()"));
  177. SET_INPUT(IR_SENSOR_PIN); //input mode
  178. WRITE(IR_SENSOR_PIN, 1); //pullup
  179. settings_init(); //also sets the state to State::initializing
  180. }
  181. void deinit() {
  182. puts_P(PSTR("fsensor::deinit()"));
  183. SET_INPUT(IR_SENSOR_PIN); //input mode
  184. WRITE(IR_SENSOR_PIN, 0); //no pullup
  185. state = State::disabled;
  186. }
  187. bool update() {
  188. switch (state) {
  189. case State::initializing:
  190. state = State::ready; //the IR sensor gets ready instantly as it's just a gpio read operation.
  191. oldFilamentPresent = getFilamentPresent(); //initialize the current filament state so that we don't create a switching event right after the sensor is ready.
  192. // fallthru
  193. case State::ready: {
  194. postponedLoadEvent = false;
  195. bool event = checkFilamentEvents();
  196. ;//
  197. return event;
  198. } break;
  199. case State::disabled:
  200. case State::error:
  201. default:
  202. return false;
  203. }
  204. return false;
  205. }
  206. bool getFilamentPresent() {
  207. return !READ(IR_SENSOR_PIN);
  208. }
  209. #ifdef FSENSOR_PROBING
  210. bool probeOtherType() {
  211. return pat9125_probe();
  212. }
  213. #endif
  214. void settings_init() {
  215. Filament_sensor::settings_init();
  216. }
  217. protected:
  218. };
  219. #if (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  220. class IR_sensor_analog: public IR_sensor {
  221. public:
  222. void init() {
  223. IR_sensor::init();
  224. settings_init();
  225. }
  226. void deinit() {
  227. IR_sensor::deinit();
  228. }
  229. bool update() {
  230. bool event = IR_sensor::update();
  231. if (state == State::ready) {
  232. if (voltReady) {
  233. voltReady = false;
  234. uint16_t volt = getVoltRaw();
  235. printf_P(PSTR("newVoltRaw:%u\n"), volt / OVERSAMPLENR);
  236. // detect min-max, some long term sliding window for filtration may be added
  237. // avoiding floating point operations, thus computing in raw
  238. if(volt > maxVolt) {
  239. maxVolt = volt;
  240. }
  241. else if(volt < minVolt) {
  242. minVolt = volt;
  243. }
  244. //! The trouble is, I can hold the filament in the hole in such a way, that it creates the exact voltage
  245. //! to be detected as the new fsensor
  246. //! We can either fake it by extending the detection window to a looooong time
  247. //! or do some other countermeasures
  248. //! what we want to detect:
  249. //! if minvolt gets below ~0.3V, it means there is an old fsensor
  250. //! if maxvolt gets above 4.6V, it means we either have an old fsensor or broken cables/fsensor
  251. //! So I'm waiting for a situation, when minVolt gets to range <0, 1.5> and maxVolt gets into range <3.0, 5>
  252. //! If and only if minVolt is in range <0.3, 1.5> and maxVolt is in range <3.0, 4.6>, I'm considering a situation with the new fsensor
  253. if(minVolt >= IRsensor_Ldiode_TRESHOLD && minVolt <= IRsensor_Lmax_TRESHOLD && maxVolt >= IRsensor_Hmin_TRESHOLD && maxVolt <= IRsensor_Hopen_TRESHOLD) {
  254. IR_ANALOG_Check(SensorRevision::_Old, SensorRevision::_Rev04);
  255. }
  256. //! If and only if minVolt is in range <0.0, 0.3> and maxVolt is in range <4.6, 5.0V>, I'm considering a situation with the old fsensor
  257. //! Note, we are not relying on one voltage here - getting just +5V can mean an old fsensor or a broken new sensor - that's why
  258. //! we need to have both voltages detected correctly to allow switching back to the old fsensor.
  259. else if( minVolt < IRsensor_Ldiode_TRESHOLD && maxVolt > IRsensor_Hopen_TRESHOLD && maxVolt <= IRsensor_VMax_TRESHOLD) {
  260. IR_ANALOG_Check(SensorRevision::_Rev04, SensorRevision::_Old);
  261. }
  262. if (!checkVoltage(volt)) {
  263. triggerError();
  264. }
  265. }
  266. }
  267. ;//
  268. return event;
  269. }
  270. void voltUpdate(uint16_t raw) { //to be called from the ADC ISR when a cycle is finished
  271. voltRaw = raw;
  272. voltReady = true;
  273. }
  274. uint16_t getVoltRaw() {
  275. uint16_t newVoltRaw;
  276. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  277. newVoltRaw = voltRaw;
  278. }
  279. return newVoltRaw;
  280. }
  281. void settings_init() {
  282. IR_sensor::settings_init();
  283. sensorRevision = (SensorRevision)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_PCB);
  284. }
  285. enum class SensorRevision : uint8_t {
  286. _Old = 0,
  287. _Rev04 = 1,
  288. _Undef = EEPROM_EMPTY_VALUE
  289. };
  290. SensorRevision getSensorRevision() {
  291. return sensorRevision;
  292. }
  293. const char* getIRVersionText() {
  294. switch(sensorRevision) {
  295. case SensorRevision::_Old:
  296. return _T(MSG_IR_03_OR_OLDER);
  297. case SensorRevision::_Rev04:
  298. return _T(MSG_IR_04_OR_NEWER);
  299. default:
  300. return _T(MSG_IR_UNKNOWN);
  301. }
  302. }
  303. void setSensorRevision(SensorRevision rev, bool updateEEPROM = false) {
  304. sensorRevision = rev;
  305. if (updateEEPROM) {
  306. eeprom_update_byte((uint8_t *)EEPROM_FSENSOR_PCB, (uint8_t)rev);
  307. }
  308. }
  309. uint16_t Voltage2Raw(float V) {
  310. return (V * 1023 * OVERSAMPLENR / VOLT_DIV_REF ) + 0.5F;
  311. }
  312. float Raw2Voltage(uint16_t raw) {
  313. return VOLT_DIV_REF * (raw / (1023.F * OVERSAMPLENR));
  314. }
  315. bool checkVoltage(uint16_t raw) {
  316. if(IRsensor_Lmax_TRESHOLD <= raw && raw <= IRsensor_Hmin_TRESHOLD) {
  317. /// If the voltage is in forbidden range, the fsensor is ok, but the lever is mounted improperly.
  318. /// Or the user is so creative so that he can hold a piece of fillament in the hole in such a genius way,
  319. /// that the IR fsensor reading is within 1.5 and 3V ... this would have been highly unusual
  320. /// and would have been considered more like a sabotage than normal printer operation
  321. if (voltageErrorCnt++ > 4) {
  322. puts_P(PSTR("fsensor in forbidden range 1.5-3V - check sensor"));
  323. return false;
  324. }
  325. }
  326. else {
  327. voltageErrorCnt = 0;
  328. }
  329. if(sensorRevision == SensorRevision::_Rev04) {
  330. /// newer IR sensor cannot normally produce 4.6-5V, this is considered a failure/bad mount
  331. if(IRsensor_Hopen_TRESHOLD <= raw && raw <= IRsensor_VMax_TRESHOLD) {
  332. puts_P(PSTR("fsensor v0.4 in fault range 4.6-5V - unconnected"));
  333. return false;
  334. }
  335. /// newer IR sensor cannot normally produce 0-0.3V, this is considered a failure
  336. #if 0 //Disabled as it has to be decided if we gonna use this or not.
  337. if(IRsensor_Hopen_TRESHOLD <= raw && raw <= IRsensor_VMax_TRESHOLD) {
  338. puts_P(PSTR("fsensor v0.4 in fault range 0.0-0.3V - wrong IR sensor"));
  339. return false;
  340. }
  341. #endif
  342. }
  343. /// If IR sensor is "uknown state" and filament is not loaded > 1.5V return false
  344. #if 0
  345. #error "I really think this code can't be enabled anymore because we are constantly checking this voltage."
  346. if((sensorRevision == SensorRevision::_Undef) && (raw > IRsensor_Lmax_TRESHOLD)) {
  347. puts_P(PSTR("Unknown IR sensor version and no filament loaded detected."));
  348. return false;
  349. }
  350. #endif
  351. // otherwise the IR fsensor is considered working correctly
  352. return true;
  353. }
  354. // Voltage2Raw is not constexpr :/
  355. const uint16_t IRsensor_Ldiode_TRESHOLD = Voltage2Raw(0.3f); // ~0.3V, raw value=982
  356. const uint16_t IRsensor_Lmax_TRESHOLD = Voltage2Raw(1.5f); // ~1.5V (0.3*Vcc), raw value=4910
  357. const uint16_t IRsensor_Hmin_TRESHOLD = Voltage2Raw(3.0f); // ~3.0V (0.6*Vcc), raw value=9821
  358. const uint16_t IRsensor_Hopen_TRESHOLD = Voltage2Raw(4.6f); // ~4.6V (N.C. @ Ru~20-50k, Rd'=56k, Ru'=10k), raw value=15059
  359. const uint16_t IRsensor_VMax_TRESHOLD = Voltage2Raw(5.f); // ~5V, raw value=16368
  360. private:
  361. SensorRevision sensorRevision;
  362. volatile bool voltReady; //this gets set by the adc ISR
  363. volatile uint16_t voltRaw;
  364. uint16_t minVolt = Voltage2Raw(6.f);
  365. uint16_t maxVolt = 0;
  366. uint16_t nFSCheckCount;
  367. uint8_t voltageErrorCnt;
  368. static constexpr uint16_t FS_CHECK_COUNT = 4;
  369. /// Switching mechanism of the fsensor type.
  370. /// Called from 2 spots which have a very similar behavior
  371. /// 1: SensorRevision::_Old -> SensorRevision::_Rev04 and print _i("FS v0.4 or newer")
  372. /// 2: SensorRevision::_Rev04 -> sensorRevision=SensorRevision::_Old and print _i("FS v0.3 or older")
  373. void IR_ANALOG_Check(SensorRevision isVersion, SensorRevision switchTo) {
  374. bool bTemp = (!CHECK_ALL_HEATERS);
  375. bTemp = bTemp && (menu_menu == lcd_status_screen);
  376. bTemp = bTemp && ((sensorRevision == isVersion) || (sensorRevision == SensorRevision::_Undef));
  377. bTemp = bTemp && (state == State::ready);
  378. if (bTemp) {
  379. nFSCheckCount++;
  380. if (nFSCheckCount > FS_CHECK_COUNT) {
  381. nFSCheckCount = 0; // not necessary
  382. setSensorRevision(switchTo, true);
  383. printf_IRSensorAnalogBoardChange();
  384. switch (switchTo) {
  385. case SensorRevision::_Old:
  386. lcd_setstatuspgm(_T(MSG_FS_V_03_OR_OLDER)); ////MSG_FS_V_03_OR_OLDER c=18
  387. break;
  388. case SensorRevision::_Rev04:
  389. lcd_setstatuspgm(_T(MSG_FS_V_04_OR_NEWER)); ////MSG_FS_V_04_OR_NEWER c=18
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. }
  396. else {
  397. nFSCheckCount = 0;
  398. }
  399. }
  400. };
  401. #endif //(FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  402. #endif //(FILAMENT_SENSOR_TYPE == FSENSOR_IR) || (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  403. #if (FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125)
  404. class PAT9125_sensor: public Filament_sensor {
  405. public:
  406. void init() {
  407. if (state == State::error) {
  408. deinit(); //deinit first if there was an error.
  409. }
  410. puts_P(PSTR("fsensor::init()"));
  411. settings_init(); //also sets the state to State::initializing
  412. calcChunkSteps(cs.axis_steps_per_unit[E_AXIS]); //for jam detection
  413. if (!pat9125_init()) {
  414. deinit();
  415. triggerError();
  416. ;//
  417. }
  418. #ifdef IR_SENSOR_PIN
  419. else if (!READ(IR_SENSOR_PIN)) {
  420. ;// MK3 fw on MK3S printer
  421. }
  422. #endif //IR_SENSOR_PIN
  423. }
  424. void deinit() {
  425. puts_P(PSTR("fsensor::deinit()"));
  426. ;//
  427. state = State::disabled;
  428. filter = 0;
  429. }
  430. bool update() {
  431. switch (state) {
  432. case State::initializing:
  433. if (!updatePAT9125()) {
  434. break; // still not stable. Stay in the initialization state.
  435. }
  436. oldFilamentPresent = getFilamentPresent(); //initialize the current filament state so that we don't create a switching event right after the sensor is ready.
  437. oldPos = pat9125_y;
  438. state = State::ready;
  439. break;
  440. case State::ready: {
  441. updatePAT9125();
  442. postponedLoadEvent = false;
  443. bool event = checkFilamentEvents();
  444. ;//
  445. return event;
  446. } break;
  447. case State::disabled:
  448. case State::error:
  449. default:
  450. return false;
  451. }
  452. return false;
  453. }
  454. bool getFilamentPresent() {
  455. return filterFilPresent;
  456. }
  457. #ifdef FSENSOR_PROBING
  458. bool probeOtherType() {
  459. SET_INPUT(IR_SENSOR_PIN); //input mode
  460. WRITE(IR_SENSOR_PIN, 1); //pullup
  461. _delay_us(100); //wait for the pullup to pull the line high (might be needed, not really sure. The internal pullups are quite weak and there might be a long wire attached).
  462. bool fsensorDetected = !READ(IR_SENSOR_PIN);
  463. WRITE(IR_SENSOR_PIN, 0); //no pullup
  464. return fsensorDetected;
  465. }
  466. #endif
  467. void setJamDetectionEnabled(bool state, bool updateEEPROM = false) {
  468. jamDetection = state;
  469. oldPos = pat9125_y;
  470. resetStepCount();
  471. jamErrCnt = 0;
  472. if (updateEEPROM) {
  473. eeprom_update_byte((uint8_t *)EEPROM_FSENSOR_JAM_DETECTION, state);
  474. }
  475. }
  476. bool getJamDetectionEnabled() {
  477. return jamDetection;
  478. }
  479. void stStep(bool rev) { //from stepper isr
  480. stepCount += rev ? -1 : 1;
  481. }
  482. void settings_init() {
  483. puts_P(PSTR("settings_init"));
  484. Filament_sensor::settings_init();
  485. setJamDetectionEnabled(eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_JAM_DETECTION));
  486. }
  487. private:
  488. static constexpr uint16_t pollingPeriod = 10; //[ms]
  489. static constexpr uint8_t filterCnt = 5; //how many checks need to be done in order to determine the filament presence precisely.
  490. ShortTimer pollingTimer;
  491. uint8_t filter;
  492. uint8_t filterFilPresent;
  493. bool jamDetection;
  494. int16_t oldPos;
  495. volatile int16_t stepCount;
  496. int16_t chunkSteps;
  497. uint8_t jamErrCnt;
  498. void calcChunkSteps(float u) {
  499. chunkSteps = (int16_t)(1.25 * u); //[mm]
  500. }
  501. int16_t getStepCount() {
  502. int16_t st_cnt;
  503. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  504. st_cnt = stepCount;
  505. }
  506. return st_cnt;
  507. }
  508. void resetStepCount() {
  509. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  510. stepCount = 0;
  511. }
  512. }
  513. void filJam() {
  514. runoutEnabled = false;
  515. autoLoadEnabled = false;
  516. jamDetection = false;
  517. stop_and_save_print_to_ram(0, 0);
  518. restore_print_from_ram_and_continue(0);
  519. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  520. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  521. enquecommand_front_P((PSTR("M600")));
  522. }
  523. bool updatePAT9125() {
  524. if (jamDetection) {
  525. int16_t _stepCount = getStepCount();
  526. if (abs(_stepCount) >= chunkSteps) { //end of chunk. Check distance
  527. resetStepCount();
  528. if (!pat9125_update()) { //get up to date data. reinit on error.
  529. init(); //try to reinit.
  530. }
  531. bool fsDir = (pat9125_y - oldPos) > 0;
  532. bool stDir = _stepCount > 0;
  533. if (fsDir != stDir) {
  534. jamErrCnt++;
  535. }
  536. else if (jamErrCnt) {
  537. jamErrCnt--;
  538. }
  539. oldPos = pat9125_y;
  540. }
  541. if (jamErrCnt > 10) {
  542. jamErrCnt = 0;
  543. filJam();
  544. }
  545. }
  546. if (!pollingTimer.running() || pollingTimer.expired(pollingPeriod)) {
  547. pollingTimer.start();
  548. if (!pat9125_update()) {
  549. init(); //try to reinit.
  550. }
  551. bool present = (pat9125_s < 17) || (pat9125_s >= 17 && pat9125_b >= 50);
  552. if (present != filterFilPresent) {
  553. filter++;
  554. }
  555. else if (filter) {
  556. filter--;
  557. }
  558. if (filter >= filterCnt) {
  559. filter = 0;
  560. filterFilPresent = present;
  561. }
  562. }
  563. return (filter == 0); //return stability
  564. }
  565. };
  566. #endif //(FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125)
  567. #if FILAMENT_SENSOR_TYPE == FSENSOR_IR
  568. extern IR_sensor fsensor;
  569. #elif FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG
  570. extern IR_sensor_analog fsensor;
  571. #elif FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125
  572. extern PAT9125_sensor fsensor;
  573. #endif
  574. #endif //FILAMENT_SENSOR