Filament_sensor.h 21 KB

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