fsensor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. //! @file
  2. #include "Marlin.h"
  3. #include "fsensor.h"
  4. #include <avr/pgmspace.h>
  5. #include "pat9125.h"
  6. #include "stepper.h"
  7. #include "cmdqueue.h"
  8. #include "ultralcd.h"
  9. #include "mmu.h"
  10. #include "cardreader.h"
  11. #include "adc.h"
  12. #include "temperature.h"
  13. #include "config.h"
  14. #include "Filament_sensor.h" //temporary
  15. //! @name Basic parameters
  16. //! @{
  17. #define FSENSOR_CHUNK_LEN 1.25 //!< filament sensor chunk length (mm)
  18. #define FSENSOR_ERR_MAX 4 //!< filament sensor maximum error/chunk count for runout detection
  19. #define FSENSOR_SOFTERR_CMAX 3 //!< number of contiguous soft failures before a triggering a runout
  20. #define FSENSOR_SOFTERR_DELTA 30000 //!< maximum interval (ms) to consider soft failures contiguous
  21. //! @}
  22. //! @name Optical quality measurement parameters
  23. //! @{
  24. #define FSENSOR_OQ_MAX_ES 2 //!< maximum sum of error blocks during filament recheck
  25. #define FSENSOR_OQ_MIN_YD 2 //!< minimum yd sum during filament check (counts per inch)
  26. #define FSENSOR_OQ_MIN_BR 80 //!< minimum brightness value
  27. #define FSENSOR_OQ_MAX_SH 10 //!< maximum shutter value
  28. //! @}
  29. const char ERRMSG_PAT9125_NOT_RESP[] PROGMEM = "PAT9125 not responding (%d)!\n";
  30. //! enabled = initialized and sampled every chunk event
  31. bool fsensor_enabled = true;
  32. //! runout watching is done in fsensor_update (called from main loop)
  33. bool fsensor_watch_runout = true;
  34. //! not responding - is set if any communication error occurred during initialization or readout
  35. bool fsensor_not_responding = false;
  36. #ifdef PAT9125
  37. //! optical checking "chunk lenght" (already in steps)
  38. int16_t fsensor_chunk_len = 0;
  39. //! number of errors, updated in ISR
  40. uint8_t fsensor_err_cnt = 0;
  41. //! variable for accumulating step count (updated callbacks from stepper and ISR)
  42. int16_t fsensor_st_cnt = 0;
  43. //! count of total sensor "soft" failures (filament status checks)
  44. uint8_t fsensor_softfail = 0;
  45. //! timestamp of last soft failure
  46. unsigned long fsensor_softfail_last = 0;
  47. //! count of soft failures within the configured time
  48. uint8_t fsensor_softfail_ccnt = 0;
  49. #endif
  50. #ifdef DEBUG_FSENSOR_LOG
  51. //! log flag: 0=log disabled, 1=log enabled
  52. uint8_t fsensor_log = 1;
  53. #endif //DEBUG_FSENSOR_LOG
  54. //! @name filament autoload variables
  55. //! @{
  56. //! autoload feature enabled
  57. bool fsensor_autoload_enabled = true;
  58. //! @name filament optical quality measurement variables
  59. //! @{
  60. //! Measurement enable/disable flag
  61. bool fsensor_oq_meassure = false;
  62. //! skip-chunk counter, for accurate measurement is necessary to skip first chunk...
  63. uint8_t fsensor_oq_skipchunk;
  64. //! number of samples from start of measurement
  65. uint8_t fsensor_oq_samples;
  66. //! sum of steps in positive direction movements
  67. uint16_t fsensor_oq_st_sum;
  68. //! sum of deltas in positive direction movements
  69. uint16_t fsensor_oq_yd_sum;
  70. //! sum of errors during measurement
  71. uint16_t fsensor_oq_er_sum;
  72. //! max error counter value during measurement
  73. uint8_t fsensor_oq_er_max;
  74. //! minimum delta value
  75. int16_t fsensor_oq_yd_min;
  76. //! maximum delta value
  77. int16_t fsensor_oq_yd_max;
  78. //! sum of shutter value
  79. uint16_t fsensor_oq_sh_sum;
  80. //! @}
  81. #ifdef IR_SENSOR_ANALOG
  82. ClFsensorActionNA oFsensorActionNA;
  83. bool bIRsensorStateFlag=false;
  84. ShortTimer tIRsensorCheckTimer;
  85. #endif //IR_SENSOR_ANALOG
  86. #ifdef PAT9125
  87. // Reset all internal counters to zero, including stepper callbacks
  88. void fsensor_reset_err_cnt()
  89. {
  90. fsensor_err_cnt = 0;
  91. pat9125_y = 0;
  92. st_reset_fsensor();
  93. }
  94. void fsensor_set_axis_steps_per_unit(float u)
  95. {
  96. fsensor_chunk_len = (int16_t)(FSENSOR_CHUNK_LEN * u);
  97. }
  98. #endif
  99. // fsensor_checkpoint_print cuts the current print job at the current position,
  100. // allowing new instructions to be inserted in the middle
  101. void fsensor_checkpoint_print(void)
  102. {
  103. puts_P(PSTR("fsensor_checkpoint_print"));
  104. stop_and_save_print_to_ram(0, 0);
  105. restore_print_from_ram_and_continue(0);
  106. }
  107. #ifdef IR_SENSOR_ANALOG
  108. const char* FsensorIRVersionText()
  109. {
  110. switch(oFsensorPCB)
  111. {
  112. case ClFsensorPCB::_Old:
  113. return _T(MSG_IR_03_OR_OLDER);
  114. case ClFsensorPCB::_Rev04:
  115. return _T(MSG_IR_04_OR_NEWER);
  116. default:
  117. return _T(MSG_IR_UNKNOWN);
  118. }
  119. }
  120. #endif //IR_SENSOR_ANALOG
  121. void fsensor_init(void)
  122. {
  123. #ifdef PAT9125
  124. uint8_t pat9125 = pat9125_init();
  125. printf_P(PSTR("PAT9125_init:%u\n"), pat9125);
  126. #endif //PAT9125
  127. uint8_t fsensor_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
  128. fsensor_autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
  129. fsensor_not_responding = false;
  130. #ifdef PAT9125
  131. uint8_t oq_meassure_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENS_OQ_MEASS_ENABLED);
  132. fsensor_oq_meassure_enabled = (oq_meassure_enabled == 1)?true:false;
  133. fsensor_set_axis_steps_per_unit(cs.axis_steps_per_unit[E_AXIS]);
  134. if (!pat9125){
  135. fsensor_enabled = 0; //disable sensor
  136. fsensor_not_responding = true;
  137. }
  138. #endif //PAT9125
  139. #ifdef IR_SENSOR_ANALOG
  140. bIRsensorStateFlag=false;
  141. oFsensorPCB = (ClFsensorPCB)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_PCB);
  142. oFsensorActionNA = (ClFsensorActionNA)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_ACTION_NA);
  143. // If the fsensor is not responding even at the start of the printer,
  144. // set this flag accordingly to show N/A in Settings->Filament sensor.
  145. // This is even valid for both fsensor board revisions (0.3 or older and 0.4).
  146. // Must be done after reading what type of fsensor board we have
  147. fsensor_not_responding = ! fsensor_IR_check();
  148. #endif //IR_SENSOR_ANALOG
  149. if (fsensor_enabled){
  150. fsensor_enable(false); // (in this case) EEPROM update is not necessary
  151. } else {
  152. fsensor_disable(false); // (in this case) EEPROM update is not necessary
  153. }
  154. printf_P(PSTR("FSensor %S"), (fsensor_enabled?PSTR("ENABLED"):PSTR("DISABLED")));
  155. #ifdef IR_SENSOR_ANALOG
  156. printf_P(PSTR(" (sensor board revision:%S)\n"), FsensorIRVersionText());
  157. #else //IR_SENSOR_ANALOG
  158. MYSERIAL.println();
  159. #endif //IR_SENSOR_ANALOG
  160. if (check_for_ir_sensor()){
  161. ir_sensor_detected = true;
  162. }
  163. }
  164. bool fsensor_enable(bool bUpdateEEPROM)
  165. {
  166. #ifdef PAT9125
  167. (void)bUpdateEEPROM; // silence unused warning in this variant
  168. if (mmu_enabled == false) { //filament sensor is pat9125, enable only if it is working
  169. uint8_t pat9125 = pat9125_init();
  170. printf_P(PSTR("PAT9125_init:%u\n"), pat9125);
  171. if (pat9125)
  172. fsensor_not_responding = false;
  173. else
  174. fsensor_not_responding = true;
  175. fsensor_enabled = pat9125 ? true : false;
  176. fsensor_watch_runout = true;
  177. fsensor_oq_meassure = false;
  178. fsensor_reset_err_cnt();
  179. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled ? 0x01 : 0x00);
  180. FSensorStateMenu = fsensor_enabled ? 1 : 0;
  181. }
  182. else //filament sensor is FINDA, always enable
  183. {
  184. fsensor_enabled = true;
  185. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x01);
  186. FSensorStateMenu = 1;
  187. }
  188. #else // PAT9125
  189. #ifdef IR_SENSOR_ANALOG
  190. if(!fsensor.checkVoltage(fsensor.getVoltRaw()))
  191. {
  192. bUpdateEEPROM=true;
  193. fsensor_enabled=false;
  194. fsensor_not_responding=true;
  195. FSensorStateMenu=0;
  196. }
  197. else {
  198. #endif //IR_SENSOR_ANALOG
  199. fsensor_enabled=true;
  200. fsensor_not_responding=false;
  201. FSensorStateMenu=1;
  202. #ifdef IR_SENSOR_ANALOG
  203. }
  204. #endif //IR_SENSOR_ANALOG
  205. if(bUpdateEEPROM)
  206. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, FSensorStateMenu);
  207. #endif //PAT9125
  208. return fsensor_enabled;
  209. }
  210. void fsensor_disable(bool bUpdateEEPROM)
  211. {
  212. fsensor_enabled = false;
  213. FSensorStateMenu = 0;
  214. if(bUpdateEEPROM)
  215. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  216. }
  217. void fsensor_autoload_set(bool State)
  218. {
  219. #ifdef PAT9125
  220. if (!State) fsensor_autoload_check_stop();
  221. #endif //PAT9125
  222. fsensor_autoload_enabled = State;
  223. eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, fsensor_autoload_enabled);
  224. }
  225. void pciSetup(byte pin)
  226. {
  227. // !!! "digitalPinTo?????bit()" does not provide the correct results for some MCU pins
  228. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  229. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  230. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  231. }
  232. #ifdef PAT9125
  233. void fsensor_autoload_check_start(void)
  234. {
  235. // puts_P(_N("fsensor_autoload_check_start\n"));
  236. if (!fsensor_enabled) return;
  237. if (!fsensor_autoload_enabled) return;
  238. if (fsensor_watch_autoload) return;
  239. if (!pat9125_update()) //update sensor
  240. {
  241. fsensor_disable();
  242. fsensor_not_responding = true;
  243. fsensor_watch_autoload = false;
  244. printf_P(ERRMSG_PAT9125_NOT_RESP, 3);
  245. return;
  246. }
  247. puts_P(_N("fsensor_autoload_check_start - autoload ENABLED"));
  248. fsensor_autoload_y = pat9125_y; //save current y value
  249. fsensor_autoload_c = 0; //reset number of changes counter
  250. fsensor_autoload_sum = 0;
  251. fsensor_autoload_last_millis = _millis();
  252. fsensor_watch_runout = false;
  253. fsensor_watch_autoload = true;
  254. }
  255. void fsensor_autoload_check_stop(void)
  256. {
  257. // puts_P(_N("fsensor_autoload_check_stop\n"));
  258. if (!fsensor_enabled) return;
  259. // puts_P(_N("fsensor_autoload_check_stop 1\n"));
  260. if (!fsensor_autoload_enabled) return;
  261. // puts_P(_N("fsensor_autoload_check_stop 2\n"));
  262. if (!fsensor_watch_autoload) return;
  263. puts_P(_N("fsensor_autoload_check_stop - autoload DISABLED"));
  264. fsensor_autoload_sum = 0;
  265. fsensor_watch_autoload = false;
  266. fsensor_watch_runout = true;
  267. fsensor_reset_err_cnt();
  268. }
  269. #endif //PAT9125
  270. bool fsensor_check_autoload(void)
  271. {
  272. if (!fsensor_enabled) return false;
  273. if (!fsensor_autoload_enabled) return false;
  274. if (ir_sensor_detected) {
  275. if (READ(IR_SENSOR_PIN)) {
  276. fsensor_watch_autoload = true;
  277. }
  278. else if (fsensor_watch_autoload == true) {
  279. fsensor_watch_autoload = false;
  280. return true;
  281. }
  282. }
  283. #ifdef PAT9125
  284. if (!fsensor_watch_autoload)
  285. {
  286. fsensor_autoload_check_start();
  287. return false;
  288. }
  289. #if 0
  290. uint8_t fsensor_autoload_c_old = fsensor_autoload_c;
  291. #endif
  292. if ((_millis() - fsensor_autoload_last_millis) < 25) return false;
  293. fsensor_autoload_last_millis = _millis();
  294. if (!pat9125_update_y()) //update sensor
  295. {
  296. fsensor_disable();
  297. fsensor_not_responding = true;
  298. printf_P(ERRMSG_PAT9125_NOT_RESP, 2);
  299. return false;
  300. }
  301. int16_t dy = pat9125_y - fsensor_autoload_y;
  302. if (dy) //? dy value is nonzero
  303. {
  304. if (dy > 0) //? delta-y value is positive (inserting)
  305. {
  306. fsensor_autoload_sum += dy;
  307. fsensor_autoload_c += 3; //increment change counter by 3
  308. }
  309. else if (fsensor_autoload_c > 1)
  310. fsensor_autoload_c -= 2; //decrement change counter by 2
  311. fsensor_autoload_y = pat9125_y; //save current value
  312. }
  313. else if (fsensor_autoload_c > 0)
  314. fsensor_autoload_c--;
  315. if (fsensor_autoload_c == 0) fsensor_autoload_sum = 0;
  316. #if 0
  317. puts_P(_N("fsensor_check_autoload\n"));
  318. if (fsensor_autoload_c != fsensor_autoload_c_old)
  319. printf_P(PSTR("fsensor_check_autoload dy=%d c=%d sum=%d\n"), dy, fsensor_autoload_c, fsensor_autoload_sum);
  320. #endif
  321. // if ((fsensor_autoload_c >= 15) && (fsensor_autoload_sum > 30))
  322. if ((fsensor_autoload_c >= 12) && (fsensor_autoload_sum > 20))
  323. {
  324. // puts_P(_N("fsensor_check_autoload = true !!!\n"));
  325. return true;
  326. }
  327. #endif //PAT9125
  328. return false;
  329. }
  330. #ifdef PAT9125
  331. void fsensor_oq_meassure_set(bool State)
  332. {
  333. fsensor_oq_meassure_enabled = State;
  334. eeprom_update_byte((unsigned char *)EEPROM_FSENS_OQ_MEASS_ENABLED, fsensor_oq_meassure_enabled);
  335. }
  336. void fsensor_oq_meassure_start(uint8_t skip)
  337. {
  338. if (!fsensor_enabled) return;
  339. if (!fsensor_oq_meassure_enabled) return;
  340. puts_P(PSTR("fsensor_oq_meassure_start"));
  341. fsensor_oq_skipchunk = skip;
  342. fsensor_oq_samples = 0;
  343. fsensor_oq_st_sum = 0;
  344. fsensor_oq_yd_sum = 0;
  345. fsensor_oq_er_sum = 0;
  346. fsensor_oq_er_max = 0;
  347. fsensor_oq_yd_min = INT16_MAX;
  348. fsensor_oq_yd_max = 0;
  349. fsensor_oq_sh_sum = 0;
  350. pat9125_update();
  351. pat9125_y = 0;
  352. fsensor_oq_meassure = true;
  353. }
  354. void fsensor_oq_meassure_stop(void)
  355. {
  356. if (!fsensor_enabled) return;
  357. if (!fsensor_oq_meassure_enabled) return;
  358. printf_P(PSTR("fsensor_oq_meassure_stop, %u samples\n"), fsensor_oq_samples);
  359. printf_P(_N(" st_sum=%u yd_sum=%u er_sum=%u er_max=%u\n"), fsensor_oq_st_sum, fsensor_oq_yd_sum, fsensor_oq_er_sum, fsensor_oq_er_max);
  360. printf_P(_N(" yd_min=%u yd_max=%u yd_avg=%u sh_avg=%u\n"), fsensor_oq_yd_min, fsensor_oq_yd_max, (uint16_t)((uint32_t)fsensor_oq_yd_sum * fsensor_chunk_len / fsensor_oq_st_sum), (uint16_t)(fsensor_oq_sh_sum / fsensor_oq_samples));
  361. fsensor_oq_meassure = false;
  362. }
  363. #ifdef FSENSOR_QUALITY
  364. const char _OK[] PROGMEM = "OK";
  365. const char _NG[] PROGMEM = "NG!";
  366. bool fsensor_oq_result(void)
  367. {
  368. if (!fsensor_enabled) return true;
  369. if (!fsensor_oq_meassure_enabled) return true;
  370. puts_P(_N("fsensor_oq_result"));
  371. bool res_er_sum = (fsensor_oq_er_sum <= FSENSOR_OQ_MAX_ES);
  372. printf_P(_N(" er_sum = %u %S\n"), fsensor_oq_er_sum, (res_er_sum?_OK:_NG));
  373. bool res_er_max = (fsensor_oq_er_max <= FSENSOR_OQ_MAX_EM);
  374. printf_P(_N(" er_max = %u %S\n"), fsensor_oq_er_max, (res_er_max?_OK:_NG));
  375. uint8_t yd_avg = ((uint32_t)fsensor_oq_yd_sum * fsensor_chunk_len / fsensor_oq_st_sum);
  376. bool res_yd_avg = (yd_avg >= FSENSOR_OQ_MIN_YD) && (yd_avg <= FSENSOR_OQ_MAX_YD);
  377. printf_P(_N(" yd_avg = %u %S\n"), yd_avg, (res_yd_avg?_OK:_NG));
  378. bool res_yd_max = (fsensor_oq_yd_max <= (yd_avg * FSENSOR_OQ_MAX_PD));
  379. printf_P(_N(" yd_max = %u %S\n"), fsensor_oq_yd_max, (res_yd_max?_OK:_NG));
  380. bool res_yd_min = (fsensor_oq_yd_min >= (yd_avg / FSENSOR_OQ_MAX_ND));
  381. printf_P(_N(" yd_min = %u %S\n"), fsensor_oq_yd_min, (res_yd_min?_OK:_NG));
  382. uint16_t yd_dev = (fsensor_oq_yd_max - yd_avg) + (yd_avg - fsensor_oq_yd_min);
  383. printf_P(_N(" yd_dev = %u\n"), yd_dev);
  384. uint16_t yd_qua = 10 * yd_avg / (yd_dev + 1);
  385. printf_P(_N(" yd_qua = %u %S\n"), yd_qua, ((yd_qua >= 8)?_OK:_NG));
  386. uint8_t sh_avg = (fsensor_oq_sh_sum / fsensor_oq_samples);
  387. bool res_sh_avg = (sh_avg <= FSENSOR_OQ_MAX_SH);
  388. if (yd_qua >= 8) res_sh_avg = true;
  389. printf_P(_N(" sh_avg = %u %S\n"), sh_avg, (res_sh_avg?_OK:_NG));
  390. bool res = res_er_sum && res_er_max && res_yd_avg && res_yd_max && res_yd_min && res_sh_avg;
  391. printf_P(_N("fsensor_oq_result %S\n"), (res?_OK:_NG));
  392. return res;
  393. }
  394. #endif //FSENSOR_QUALITY
  395. FORCE_INLINE static void fsensor_isr(int st_cnt)
  396. {
  397. uint8_t old_err_cnt = fsensor_err_cnt;
  398. uint8_t pat9125_res = fsensor_oq_meassure?pat9125_update():pat9125_update_y();
  399. if (!pat9125_res)
  400. {
  401. fsensor_disable();
  402. fsensor_not_responding = true;
  403. printf_P(ERRMSG_PAT9125_NOT_RESP, 1);
  404. }
  405. if (st_cnt != 0)
  406. {
  407. // movement was planned, check for sensor movement
  408. int8_t st_dir = st_cnt >= 0;
  409. int8_t pat9125_dir = pat9125_y >= 0;
  410. if (pat9125_y == 0)
  411. {
  412. if (st_dir)
  413. {
  414. // no movement detected: we might be within a blind sensor range,
  415. // update the frame and shutter parameters we didn't earlier
  416. if (!fsensor_oq_meassure)
  417. pat9125_update_bs();
  418. // increment the error count only if underexposed: filament likely missing
  419. if ((pat9125_b < FSENSOR_OQ_MIN_BR) && (pat9125_s > FSENSOR_OQ_MAX_SH))
  420. {
  421. // check for a dark frame (<30% avg brightness) with long exposure
  422. ++fsensor_err_cnt;
  423. }
  424. else
  425. {
  426. // good frame, filament likely present
  427. if(fsensor_err_cnt) --fsensor_err_cnt;
  428. }
  429. }
  430. }
  431. else if (pat9125_dir != st_dir)
  432. {
  433. // detected direction opposite of motor movement
  434. if (st_dir) ++fsensor_err_cnt;
  435. }
  436. else if (pat9125_dir == st_dir)
  437. {
  438. // direction agreeing with planned movement
  439. if (fsensor_err_cnt) --fsensor_err_cnt;
  440. }
  441. if (st_dir && fsensor_oq_meassure)
  442. {
  443. // extruding with quality assessment
  444. if (fsensor_oq_skipchunk)
  445. {
  446. fsensor_oq_skipchunk--;
  447. fsensor_err_cnt = 0;
  448. }
  449. else
  450. {
  451. if (st_cnt == fsensor_chunk_len)
  452. {
  453. if (pat9125_y > 0) if (fsensor_oq_yd_min > pat9125_y) fsensor_oq_yd_min = (fsensor_oq_yd_min + pat9125_y) / 2;
  454. if (pat9125_y >= 0) if (fsensor_oq_yd_max < pat9125_y) fsensor_oq_yd_max = (fsensor_oq_yd_max + pat9125_y) / 2;
  455. }
  456. fsensor_oq_samples++;
  457. fsensor_oq_st_sum += st_cnt;
  458. if (pat9125_y > 0) fsensor_oq_yd_sum += pat9125_y;
  459. if (fsensor_err_cnt > old_err_cnt)
  460. fsensor_oq_er_sum += (fsensor_err_cnt - old_err_cnt);
  461. if (fsensor_oq_er_max < fsensor_err_cnt)
  462. fsensor_oq_er_max = fsensor_err_cnt;
  463. fsensor_oq_sh_sum += pat9125_s;
  464. }
  465. }
  466. }
  467. #ifdef DEBUG_FSENSOR_LOG
  468. if (fsensor_log)
  469. {
  470. printf_P(_N("FSENSOR cnt=%d dy=%d err=%u %S\n"), st_cnt, pat9125_y, fsensor_err_cnt, (fsensor_err_cnt > old_err_cnt)?_N("NG!"):_N("OK"));
  471. if (fsensor_oq_meassure) printf_P(_N("FSENSOR st_sum=%u yd_sum=%u er_sum=%u er_max=%u yd_max=%u\n"), fsensor_oq_st_sum, fsensor_oq_yd_sum, fsensor_oq_er_sum, fsensor_oq_er_max, fsensor_oq_yd_max);
  472. }
  473. #endif //DEBUG_FSENSOR_LOG
  474. pat9125_y = 0;
  475. }
  476. ISR(FSENSOR_INT_PIN_VECT)
  477. {
  478. if (mmu_enabled || ir_sensor_detected) return;
  479. if (!((fsensor_int_pin_old ^ FSENSOR_INT_PIN_PIN_REG) & FSENSOR_INT_PIN_MASK)) return;
  480. fsensor_int_pin_old = FSENSOR_INT_PIN_PIN_REG;
  481. // prevent isr re-entry
  482. static bool _lock = false;
  483. if (!_lock)
  484. {
  485. // fetch fsensor_st_cnt atomically
  486. int st_cnt = fsensor_st_cnt;
  487. fsensor_st_cnt = 0;
  488. _lock = true;
  489. sei();
  490. fsensor_isr(st_cnt);
  491. cli();
  492. _lock = false;
  493. }
  494. }
  495. void fsensor_setup_interrupt(void)
  496. {
  497. WRITE(FSENSOR_INT_PIN, 0);
  498. SET_OUTPUT(FSENSOR_INT_PIN);
  499. fsensor_int_pin_old = 0;
  500. //pciSetup(FSENSOR_INT_PIN);
  501. // !!! "pciSetup()" does not provide the correct results for some MCU pins
  502. // so interrupt registers settings:
  503. FSENSOR_INT_PIN_PCMSK_REG |= bit(FSENSOR_INT_PIN_PCMSK_BIT); // enable corresponding PinChangeInterrupt (individual pin)
  504. PCIFR |= bit(FSENSOR_INT_PIN_PCICR_BIT); // clear previous occasional interrupt (set of pins)
  505. PCICR |= bit(FSENSOR_INT_PIN_PCICR_BIT); // enable corresponding PinChangeInterrupt (set of pins)
  506. }
  507. void fsensor_st_block_chunk(int cnt)
  508. {
  509. if (!fsensor_enabled) return;
  510. fsensor_st_cnt += cnt;
  511. // !!! bit toggling (PINxn <- 1) (for PinChangeInterrupt) does not work for some MCU pins
  512. WRITE(FSENSOR_INT_PIN, !READ(FSENSOR_INT_PIN));
  513. }
  514. #endif //PAT9125
  515. //! Common code for enqueing M600 and supplemental codes into the command queue.
  516. //! Used both for the IR sensor and the PAT9125
  517. void fsensor_enque_M600(){
  518. puts_P(PSTR("fsensor_update - M600"));
  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. //! @brief filament sensor update (perform M600 on filament runout)
  524. //!
  525. //! Works only if filament sensor is enabled.
  526. //! When the filament sensor error count is larger then FSENSOR_ERR_MAX, pauses print, tries to move filament back and forth.
  527. //! If there is still no plausible signal from filament sensor plans M600 (Filament change).
  528. void fsensor_update(void)
  529. {
  530. #ifdef PAT9125
  531. if (fsensor_watch_runout && (fsensor_err_cnt > FSENSOR_ERR_MAX))
  532. {
  533. fsensor_stop_and_save_print();
  534. KEEPALIVE_STATE(IN_HANDLER);
  535. bool autoload_enabled_tmp = fsensor_autoload_enabled;
  536. fsensor_autoload_enabled = false;
  537. bool oq_meassure_enabled_tmp = fsensor_oq_meassure_enabled;
  538. fsensor_oq_meassure_enabled = true;
  539. // move the nozzle away while checking the filament
  540. current_position[Z_AXIS] += 0.8;
  541. if(current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  542. plan_buffer_line_curposXYZE(max_feedrate[Z_AXIS]);
  543. st_synchronize();
  544. // check the filament in isolation
  545. fsensor_reset_err_cnt();
  546. fsensor_oq_meassure_start(0);
  547. float e_tmp = current_position[E_AXIS];
  548. current_position[E_AXIS] -= 3;
  549. plan_buffer_line_curposXYZE(250/60);
  550. current_position[E_AXIS] = e_tmp;
  551. plan_buffer_line_curposXYZE(200/60);
  552. st_synchronize();
  553. fsensor_oq_meassure_stop();
  554. bool err = false;
  555. err |= (fsensor_err_cnt > 0); // final error count is non-zero
  556. err |= (fsensor_oq_er_sum > FSENSOR_OQ_MAX_ES); // total error count is above limit
  557. err |= (fsensor_oq_yd_sum < FSENSOR_OQ_MIN_YD); // total measured distance is below limit
  558. fsensor_restore_print_and_continue();
  559. fsensor_autoload_enabled = autoload_enabled_tmp;
  560. fsensor_oq_meassure_enabled = oq_meassure_enabled_tmp;
  561. unsigned long now = _millis();
  562. if (!err && (now - fsensor_softfail_last) > FSENSOR_SOFTERR_DELTA)
  563. fsensor_softfail_ccnt = 0;
  564. if (!err && fsensor_softfail_ccnt <= FSENSOR_SOFTERR_CMAX)
  565. {
  566. puts_P(PSTR("fsensor_err_cnt = 0"));
  567. ++fsensor_softfail;
  568. ++fsensor_softfail_ccnt;
  569. fsensor_softfail_last = now;
  570. }
  571. else
  572. {
  573. fsensor_softfail_ccnt = 0;
  574. fsensor_softfail_last = 0;
  575. fsensor_enque_M600();
  576. }
  577. }
  578. #else //PAT9125
  579. if (CHECK_FSENSOR && ir_sensor_detected)
  580. {
  581. if (READ(IR_SENSOR_PIN))
  582. { // IR_SENSOR_PIN ~ H
  583. fsensor_checkpoint_print();
  584. fsensor_enque_M600();
  585. }
  586. }
  587. #endif //PAT9125
  588. }
  589. #ifdef IR_SENSOR_ANALOG
  590. /// This is called only upon start of the printer or when switching the fsensor ON in the menu
  591. /// We cannot do temporal window checks here (aka the voltage has been in some range for a period of time)
  592. bool fsensor_IR_check(uint16_t raw){
  593. if( IRsensor_Lmax_TRESHOLD <= raw && raw <= IRsensor_Hmin_TRESHOLD ){
  594. /// If the voltage is in forbidden range, the fsensor is ok, but the lever is mounted improperly.
  595. /// Or the user is so creative so that he can hold a piece of fillament in the hole in such a genius way,
  596. /// that the IR fsensor reading is within 1.5 and 3V ... this would have been highly unusual
  597. /// and would have been considered more like a sabotage than normal printer operation
  598. puts_P(PSTR("fsensor in forbidden range 1.5-3V - check sensor"));
  599. return false;
  600. }
  601. if( oFsensorPCB == ClFsensorPCB::_Rev04 ){
  602. /// newer IR sensor cannot normally produce 4.6-5V, this is considered a failure/bad mount
  603. if( IRsensor_Hopen_TRESHOLD <= raw && raw <= IRsensor_VMax_TRESHOLD ){
  604. puts_P(PSTR("fsensor v0.4 in fault range 4.6-5V - unconnected"));
  605. return false;
  606. }
  607. /// newer IR sensor cannot normally produce 0-0.3V, this is considered a failure
  608. #if 0 //Disabled as it has to be decided if we gonna use this or not.
  609. if( IRsensor_Hopen_TRESHOLD <= raw && raw <= IRsensor_VMax_TRESHOLD ){
  610. puts_P(PSTR("fsensor v0.4 in fault range 0.0-0.3V - wrong IR sensor"));
  611. return false;
  612. }
  613. #endif
  614. }
  615. /// If IR sensor is "uknown state" and filament is not loaded > 1.5V return false
  616. #if 0
  617. if( (oFsensorPCB == ClFsensorPCB::_Undef) && ( raw > IRsensor_Lmax_TRESHOLD ) ){
  618. puts_P(PSTR("Unknown IR sensor version and no filament loaded detected."));
  619. return false;
  620. }
  621. #endif
  622. // otherwise the IR fsensor is considered working correctly
  623. return true;
  624. }
  625. #endif //IR_SENSOR_ANALOG