fsensor.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 "io_atmega2560.h"
  8. #include "cmdqueue.h"
  9. #include "ultralcd.h"
  10. #include "mmu.h"
  11. #include "cardreader.h"
  12. //! @name Basic parameters
  13. //! @{
  14. #define FSENSOR_CHUNK_LEN 0.64F //!< filament sensor chunk length 0.64mm
  15. #define FSENSOR_ERR_MAX 17 //!< filament sensor maximum error count for runout detection
  16. //! @}
  17. //! @name Optical quality measurement parameters
  18. //! @{
  19. #define FSENSOR_OQ_MAX_ES 6 //!< maximum error sum while loading (length ~64mm = 100chunks)
  20. #define FSENSOR_OQ_MAX_EM 2 //!< maximum error counter value while loading
  21. #define FSENSOR_OQ_MIN_YD 2 //!< minimum yd per chunk (applied to avg value)
  22. #define FSENSOR_OQ_MAX_YD 200 //!< maximum yd per chunk (applied to avg value)
  23. #define FSENSOR_OQ_MAX_PD 4 //!< maximum positive deviation (= yd_max/yd_avg)
  24. #define FSENSOR_OQ_MAX_ND 5 //!< maximum negative deviation (= yd_avg/yd_min)
  25. #define FSENSOR_OQ_MAX_SH 13 //!< maximum shutter value
  26. //! @}
  27. const char ERRMSG_PAT9125_NOT_RESP[] PROGMEM = "PAT9125 not responding (%d)!\n";
  28. // PJ7 can not be used (does not have PinChangeInterrupt possibility)
  29. #define FSENSOR_INT_PIN 75 //!< filament sensor interrupt pin PJ4
  30. #define FSENSOR_INT_PIN_MASK 0x10 //!< filament sensor interrupt pin mask (bit4)
  31. #define FSENSOR_INT_PIN_PIN_REG PINJ // PIN register @ PJ4
  32. #define FSENSOR_INT_PIN_VECT PCINT1_vect // PinChange ISR @ PJ4
  33. #define FSENSOR_INT_PIN_PCMSK_REG PCMSK1 // PinChangeMaskRegister @ PJ4
  34. #define FSENSOR_INT_PIN_PCMSK_BIT PCINT13 // PinChange Interrupt / PinChange Enable Mask @ PJ4
  35. #define FSENSOR_INT_PIN_PCICR_BIT PCIE1 // PinChange Interrupt Enable / Flag @ PJ4
  36. //uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  37. uint8_t fsensor_int_pin_old = 0;
  38. int16_t fsensor_chunk_len = 0;
  39. //! enabled = initialized and sampled every chunk event
  40. bool fsensor_enabled = true;
  41. //! runout watching is done in fsensor_update (called from main loop)
  42. bool fsensor_watch_runout = true;
  43. //! not responding - is set if any communication error occurred during initialization or readout
  44. bool fsensor_not_responding = false;
  45. //! printing saved
  46. bool fsensor_printing_saved = false;
  47. //! enable/disable quality meassurement
  48. bool fsensor_oq_meassure_enabled = false;
  49. //! as explained in the CHECK_FSENSOR macro: this flag is set to true when fsensor posts
  50. //! the M600 into the command queue, which elliminates the hazard of having posted multiple M600's
  51. //! before the first one gets read and started processing.
  52. //! Btw., the IR fsensor could do up to 6 posts before the command queue managed to start processing the first M600 ;)
  53. static bool fsensor_m600_enqueued = false;
  54. //! number of errors, updated in ISR
  55. uint8_t fsensor_err_cnt = 0;
  56. //! variable for accumulating step count (updated callbacks from stepper and ISR)
  57. int16_t fsensor_st_cnt = 0;
  58. //! last dy value from pat9125 sensor (used in ISR)
  59. int16_t fsensor_dy_old = 0;
  60. //! log flag: 0=log disabled, 1=log enabled
  61. uint8_t fsensor_log = 1;
  62. //! @name filament autoload variables
  63. //! @{
  64. //! autoload feature enabled
  65. bool fsensor_autoload_enabled = true;
  66. //! autoload watching enable/disable flag
  67. bool fsensor_watch_autoload = false;
  68. //
  69. uint16_t fsensor_autoload_y;
  70. //
  71. uint8_t fsensor_autoload_c;
  72. //
  73. uint32_t fsensor_autoload_last_millis;
  74. //
  75. uint8_t fsensor_autoload_sum;
  76. //! @}
  77. //! @name filament optical quality measurement variables
  78. //! @{
  79. //! Measurement enable/disable flag
  80. bool fsensor_oq_meassure = false;
  81. //! skip-chunk counter, for accurate measurement is necessary to skip first chunk...
  82. uint8_t fsensor_oq_skipchunk;
  83. //! number of samples from start of measurement
  84. uint8_t fsensor_oq_samples;
  85. //! sum of steps in positive direction movements
  86. uint16_t fsensor_oq_st_sum;
  87. //! sum of deltas in positive direction movements
  88. uint16_t fsensor_oq_yd_sum;
  89. //! sum of errors during measurement
  90. uint16_t fsensor_oq_er_sum;
  91. //! max error counter value during measurement
  92. uint8_t fsensor_oq_er_max;
  93. //! minimum delta value
  94. int16_t fsensor_oq_yd_min;
  95. //! maximum delta value
  96. int16_t fsensor_oq_yd_max;
  97. //! sum of shutter value
  98. uint16_t fsensor_oq_sh_sum;
  99. //! @}
  100. void fsensor_stop_and_save_print(void)
  101. {
  102. printf_P(PSTR("fsensor_stop_and_save_print\n"));
  103. stop_and_save_print_to_ram(0, 0); //XYZE - no change
  104. }
  105. void fsensor_restore_print_and_continue(void)
  106. {
  107. printf_P(PSTR("fsensor_restore_print_and_continue\n"));
  108. fsensor_watch_runout = true;
  109. fsensor_err_cnt = 0;
  110. fsensor_m600_enqueued = false;
  111. restore_print_from_ram_and_continue(0); //XYZ = orig, E - no change
  112. }
  113. void fsensor_init(void)
  114. {
  115. #ifdef PAT9125
  116. uint8_t pat9125 = pat9125_init();
  117. printf_P(PSTR("PAT9125_init:%hhu\n"), pat9125);
  118. #endif //PAT9125
  119. uint8_t fsensor = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
  120. fsensor_autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
  121. #ifdef PAT9125
  122. uint8_t oq_meassure_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENS_OQ_MEASS_ENABLED);
  123. fsensor_oq_meassure_enabled = (oq_meassure_enabled == 1)?true:false;
  124. fsensor_chunk_len = (int16_t)(FSENSOR_CHUNK_LEN * cs.axis_steps_per_unit[E_AXIS]);
  125. if (!pat9125)
  126. {
  127. fsensor = 0; //disable sensor
  128. fsensor_not_responding = true;
  129. }
  130. else
  131. fsensor_not_responding = false;
  132. #endif //PAT9125
  133. if (fsensor)
  134. fsensor_enable();
  135. else
  136. fsensor_disable();
  137. printf_P(PSTR("FSensor %S\n"), (fsensor_enabled?PSTR("ENABLED"):PSTR("DISABLED\n")));
  138. if (check_for_ir_sensor()) ir_sensor_detected = true;
  139. }
  140. bool fsensor_enable(void)
  141. {
  142. #ifdef PAT9125
  143. if (mmu_enabled == false) { //filament sensor is pat9125, enable only if it is working
  144. uint8_t pat9125 = pat9125_init();
  145. printf_P(PSTR("PAT9125_init:%hhu\n"), pat9125);
  146. if (pat9125)
  147. fsensor_not_responding = false;
  148. else
  149. fsensor_not_responding = true;
  150. fsensor_enabled = pat9125 ? true : false;
  151. fsensor_watch_runout = true;
  152. fsensor_oq_meassure = false;
  153. fsensor_err_cnt = 0;
  154. fsensor_dy_old = 0;
  155. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled ? 0x01 : 0x00);
  156. FSensorStateMenu = fsensor_enabled ? 1 : 0;
  157. }
  158. else //filament sensor is FINDA, always enable
  159. {
  160. fsensor_enabled = true;
  161. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x01);
  162. FSensorStateMenu = 1;
  163. }
  164. #else // PAT9125
  165. fsensor_enabled = true;
  166. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x01);
  167. FSensorStateMenu = 1;
  168. #endif // PAT9125
  169. return fsensor_enabled;
  170. }
  171. void fsensor_disable(void)
  172. {
  173. fsensor_enabled = false;
  174. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  175. FSensorStateMenu = 0;
  176. }
  177. void fsensor_autoload_set(bool State)
  178. {
  179. #ifdef PAT9125
  180. if (!State) fsensor_autoload_check_stop();
  181. #endif //PAT9125
  182. fsensor_autoload_enabled = State;
  183. eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, fsensor_autoload_enabled);
  184. }
  185. void pciSetup(byte pin)
  186. {
  187. // !!! "digitalPinTo?????bit()" does not provide the correct results for some MCU pins
  188. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  189. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  190. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  191. }
  192. #ifdef PAT9125
  193. void fsensor_autoload_check_start(void)
  194. {
  195. // puts_P(_N("fsensor_autoload_check_start\n"));
  196. if (!fsensor_enabled) return;
  197. if (!fsensor_autoload_enabled) return;
  198. if (fsensor_watch_autoload) return;
  199. if (!pat9125_update()) //update sensor
  200. {
  201. fsensor_disable();
  202. fsensor_not_responding = true;
  203. fsensor_watch_autoload = false;
  204. printf_P(ERRMSG_PAT9125_NOT_RESP, 3);
  205. return;
  206. }
  207. puts_P(_N("fsensor_autoload_check_start - autoload ENABLED\n"));
  208. fsensor_autoload_y = pat9125_y; //save current y value
  209. fsensor_autoload_c = 0; //reset number of changes counter
  210. fsensor_autoload_sum = 0;
  211. fsensor_autoload_last_millis = _millis();
  212. fsensor_watch_runout = false;
  213. fsensor_watch_autoload = true;
  214. fsensor_err_cnt = 0;
  215. }
  216. void fsensor_autoload_check_stop(void)
  217. {
  218. // puts_P(_N("fsensor_autoload_check_stop\n"));
  219. if (!fsensor_enabled) return;
  220. // puts_P(_N("fsensor_autoload_check_stop 1\n"));
  221. if (!fsensor_autoload_enabled) return;
  222. // puts_P(_N("fsensor_autoload_check_stop 2\n"));
  223. if (!fsensor_watch_autoload) return;
  224. puts_P(_N("fsensor_autoload_check_stop - autoload DISABLED\n"));
  225. fsensor_autoload_sum = 0;
  226. fsensor_watch_autoload = false;
  227. fsensor_watch_runout = true;
  228. fsensor_err_cnt = 0;
  229. }
  230. #endif //PAT9125
  231. bool fsensor_check_autoload(void)
  232. {
  233. if (!fsensor_enabled) return false;
  234. if (!fsensor_autoload_enabled) return false;
  235. if (ir_sensor_detected) {
  236. if (digitalRead(IR_SENSOR_PIN) == 1) {
  237. fsensor_watch_autoload = true;
  238. }
  239. else if (fsensor_watch_autoload == true) {
  240. fsensor_watch_autoload = false;
  241. return true;
  242. }
  243. }
  244. #ifdef PAT9125
  245. if (!fsensor_watch_autoload)
  246. {
  247. fsensor_autoload_check_start();
  248. return false;
  249. }
  250. #if 0
  251. uint8_t fsensor_autoload_c_old = fsensor_autoload_c;
  252. #endif
  253. if ((_millis() - fsensor_autoload_last_millis) < 25) return false;
  254. fsensor_autoload_last_millis = _millis();
  255. if (!pat9125_update_y()) //update sensor
  256. {
  257. fsensor_disable();
  258. fsensor_not_responding = true;
  259. printf_P(ERRMSG_PAT9125_NOT_RESP, 2);
  260. return false;
  261. }
  262. int16_t dy = pat9125_y - fsensor_autoload_y;
  263. if (dy) //? dy value is nonzero
  264. {
  265. if (dy > 0) //? delta-y value is positive (inserting)
  266. {
  267. fsensor_autoload_sum += dy;
  268. fsensor_autoload_c += 3; //increment change counter by 3
  269. }
  270. else if (fsensor_autoload_c > 1)
  271. fsensor_autoload_c -= 2; //decrement change counter by 2
  272. fsensor_autoload_y = pat9125_y; //save current value
  273. }
  274. else if (fsensor_autoload_c > 0)
  275. fsensor_autoload_c--;
  276. if (fsensor_autoload_c == 0) fsensor_autoload_sum = 0;
  277. #if 0
  278. puts_P(_N("fsensor_check_autoload\n"));
  279. if (fsensor_autoload_c != fsensor_autoload_c_old)
  280. printf_P(PSTR("fsensor_check_autoload dy=%d c=%d sum=%d\n"), dy, fsensor_autoload_c, fsensor_autoload_sum);
  281. #endif
  282. // if ((fsensor_autoload_c >= 15) && (fsensor_autoload_sum > 30))
  283. if ((fsensor_autoload_c >= 12) && (fsensor_autoload_sum > 20))
  284. {
  285. // puts_P(_N("fsensor_check_autoload = true !!!\n"));
  286. return true;
  287. }
  288. #endif //PAT9125
  289. return false;
  290. }
  291. void fsensor_oq_meassure_set(bool State)
  292. {
  293. fsensor_oq_meassure_enabled = State;
  294. eeprom_update_byte((unsigned char *)EEPROM_FSENS_OQ_MEASS_ENABLED, fsensor_oq_meassure_enabled);
  295. }
  296. void fsensor_oq_meassure_start(uint8_t skip)
  297. {
  298. if (!fsensor_enabled) return;
  299. if (!fsensor_oq_meassure_enabled) return;
  300. printf_P(PSTR("fsensor_oq_meassure_start\n"));
  301. fsensor_oq_skipchunk = skip;
  302. fsensor_oq_samples = 0;
  303. fsensor_oq_st_sum = 0;
  304. fsensor_oq_yd_sum = 0;
  305. fsensor_oq_er_sum = 0;
  306. fsensor_oq_er_max = 0;
  307. fsensor_oq_yd_min = FSENSOR_OQ_MAX_YD;
  308. fsensor_oq_yd_max = 0;
  309. fsensor_oq_sh_sum = 0;
  310. pat9125_update();
  311. pat9125_y = 0;
  312. fsensor_watch_runout = false;
  313. fsensor_oq_meassure = true;
  314. }
  315. void fsensor_oq_meassure_stop(void)
  316. {
  317. if (!fsensor_enabled) return;
  318. if (!fsensor_oq_meassure_enabled) return;
  319. printf_P(PSTR("fsensor_oq_meassure_stop, %hhu samples\n"), fsensor_oq_samples);
  320. printf_P(_N(" st_sum=%u yd_sum=%u er_sum=%u er_max=%hhu\n"), fsensor_oq_st_sum, fsensor_oq_yd_sum, fsensor_oq_er_sum, fsensor_oq_er_max);
  321. 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));
  322. fsensor_oq_meassure = false;
  323. fsensor_watch_runout = true;
  324. fsensor_err_cnt = 0;
  325. }
  326. const char _OK[] PROGMEM = "OK";
  327. const char _NG[] PROGMEM = "NG!";
  328. bool fsensor_oq_result(void)
  329. {
  330. if (!fsensor_enabled) return true;
  331. if (!fsensor_oq_meassure_enabled) return true;
  332. printf_P(_N("fsensor_oq_result\n"));
  333. bool res_er_sum = (fsensor_oq_er_sum <= FSENSOR_OQ_MAX_ES);
  334. printf_P(_N(" er_sum = %u %S\n"), fsensor_oq_er_sum, (res_er_sum?_OK:_NG));
  335. bool res_er_max = (fsensor_oq_er_max <= FSENSOR_OQ_MAX_EM);
  336. printf_P(_N(" er_max = %hhu %S\n"), fsensor_oq_er_max, (res_er_max?_OK:_NG));
  337. uint8_t yd_avg = ((uint32_t)fsensor_oq_yd_sum * fsensor_chunk_len / fsensor_oq_st_sum);
  338. bool res_yd_avg = (yd_avg >= FSENSOR_OQ_MIN_YD) && (yd_avg <= FSENSOR_OQ_MAX_YD);
  339. printf_P(_N(" yd_avg = %hhu %S\n"), yd_avg, (res_yd_avg?_OK:_NG));
  340. bool res_yd_max = (fsensor_oq_yd_max <= (yd_avg * FSENSOR_OQ_MAX_PD));
  341. printf_P(_N(" yd_max = %u %S\n"), fsensor_oq_yd_max, (res_yd_max?_OK:_NG));
  342. bool res_yd_min = (fsensor_oq_yd_min >= (yd_avg / FSENSOR_OQ_MAX_ND));
  343. printf_P(_N(" yd_min = %u %S\n"), fsensor_oq_yd_min, (res_yd_min?_OK:_NG));
  344. uint16_t yd_dev = (fsensor_oq_yd_max - yd_avg) + (yd_avg - fsensor_oq_yd_min);
  345. printf_P(_N(" yd_dev = %u\n"), yd_dev);
  346. uint16_t yd_qua = 10 * yd_avg / (yd_dev + 1);
  347. printf_P(_N(" yd_qua = %u %S\n"), yd_qua, ((yd_qua >= 8)?_OK:_NG));
  348. uint8_t sh_avg = (fsensor_oq_sh_sum / fsensor_oq_samples);
  349. bool res_sh_avg = (sh_avg <= FSENSOR_OQ_MAX_SH);
  350. if (yd_qua >= 8) res_sh_avg = true;
  351. printf_P(_N(" sh_avg = %hhu %S\n"), sh_avg, (res_sh_avg?_OK:_NG));
  352. bool res = res_er_sum && res_er_max && res_yd_avg && res_yd_max && res_yd_min && res_sh_avg;
  353. printf_P(_N("fsensor_oq_result %S\n"), (res?_OK:_NG));
  354. return res;
  355. }
  356. #ifdef PAT9125
  357. ISR(FSENSOR_INT_PIN_VECT)
  358. {
  359. if (mmu_enabled || ir_sensor_detected) return;
  360. if (!((fsensor_int_pin_old ^ FSENSOR_INT_PIN_PIN_REG) & FSENSOR_INT_PIN_MASK)) return;
  361. fsensor_int_pin_old = FSENSOR_INT_PIN_PIN_REG;
  362. static bool _lock = false;
  363. if (_lock) return;
  364. _lock = true;
  365. int st_cnt = fsensor_st_cnt;
  366. fsensor_st_cnt = 0;
  367. sei();
  368. uint8_t old_err_cnt = fsensor_err_cnt;
  369. uint8_t pat9125_res = fsensor_oq_meassure?pat9125_update():pat9125_update_y();
  370. if (!pat9125_res)
  371. {
  372. fsensor_disable();
  373. fsensor_not_responding = true;
  374. printf_P(ERRMSG_PAT9125_NOT_RESP, 1);
  375. }
  376. if (st_cnt != 0)
  377. { //movement
  378. if (st_cnt > 0) //positive movement
  379. {
  380. if (pat9125_y < 0)
  381. {
  382. if (fsensor_err_cnt)
  383. fsensor_err_cnt += 2;
  384. else
  385. fsensor_err_cnt++;
  386. }
  387. else if (pat9125_y > 0)
  388. {
  389. if (fsensor_err_cnt)
  390. fsensor_err_cnt--;
  391. }
  392. else //(pat9125_y == 0)
  393. if (((fsensor_dy_old <= 0) || (fsensor_err_cnt)) && (st_cnt > (fsensor_chunk_len >> 1)))
  394. fsensor_err_cnt++;
  395. if (fsensor_oq_meassure)
  396. {
  397. if (fsensor_oq_skipchunk)
  398. {
  399. fsensor_oq_skipchunk--;
  400. fsensor_err_cnt = 0;
  401. }
  402. else
  403. {
  404. if (st_cnt == fsensor_chunk_len)
  405. {
  406. if (pat9125_y > 0) if (fsensor_oq_yd_min > pat9125_y) fsensor_oq_yd_min = (fsensor_oq_yd_min + pat9125_y) / 2;
  407. if (pat9125_y >= 0) if (fsensor_oq_yd_max < pat9125_y) fsensor_oq_yd_max = (fsensor_oq_yd_max + pat9125_y) / 2;
  408. }
  409. fsensor_oq_samples++;
  410. fsensor_oq_st_sum += st_cnt;
  411. if (pat9125_y > 0) fsensor_oq_yd_sum += pat9125_y;
  412. if (fsensor_err_cnt > old_err_cnt)
  413. fsensor_oq_er_sum += (fsensor_err_cnt - old_err_cnt);
  414. if (fsensor_oq_er_max < fsensor_err_cnt)
  415. fsensor_oq_er_max = fsensor_err_cnt;
  416. fsensor_oq_sh_sum += pat9125_s;
  417. }
  418. }
  419. }
  420. else //negative movement
  421. {
  422. }
  423. }
  424. else
  425. { //no movement
  426. }
  427. #ifdef DEBUG_FSENSOR_LOG
  428. if (fsensor_log)
  429. {
  430. printf_P(_N("FSENSOR cnt=%d dy=%d err=%hhu %S\n"), st_cnt, pat9125_y, fsensor_err_cnt, (fsensor_err_cnt > old_err_cnt)?_N("NG!"):_N("OK"));
  431. if (fsensor_oq_meassure) printf_P(_N("FSENSOR st_sum=%u yd_sum=%u er_sum=%u er_max=%hhu yd_max=%u\n"), fsensor_oq_st_sum, fsensor_oq_yd_sum, fsensor_oq_er_sum, fsensor_oq_er_max, fsensor_oq_yd_max);
  432. }
  433. #endif //DEBUG_FSENSOR_LOG
  434. fsensor_dy_old = pat9125_y;
  435. pat9125_y = 0;
  436. _lock = false;
  437. return;
  438. }
  439. void fsensor_setup_interrupt(void)
  440. {
  441. pinMode(FSENSOR_INT_PIN, OUTPUT);
  442. digitalWrite(FSENSOR_INT_PIN, LOW);
  443. fsensor_int_pin_old = 0;
  444. //pciSetup(FSENSOR_INT_PIN);
  445. // !!! "pciSetup()" does not provide the correct results for some MCU pins
  446. // so interrupt registers settings:
  447. FSENSOR_INT_PIN_PCMSK_REG |= bit(FSENSOR_INT_PIN_PCMSK_BIT); // enable corresponding PinChangeInterrupt (individual pin)
  448. PCIFR |= bit(FSENSOR_INT_PIN_PCICR_BIT); // clear previous occasional interrupt (set of pins)
  449. PCICR |= bit(FSENSOR_INT_PIN_PCICR_BIT); // enable corresponding PinChangeInterrupt (set of pins)
  450. }
  451. #endif //PAT9125
  452. void fsensor_st_block_chunk(int cnt)
  453. {
  454. if (!fsensor_enabled) return;
  455. fsensor_st_cnt += cnt;
  456. if (abs(fsensor_st_cnt) >= fsensor_chunk_len)
  457. {
  458. // !!! bit toggling (PINxn <- 1) (for PinChangeInterrupt) does not work for some MCU pins
  459. if (PIN_GET(FSENSOR_INT_PIN)) {PIN_VAL(FSENSOR_INT_PIN, LOW);}
  460. else {PIN_VAL(FSENSOR_INT_PIN, HIGH);}
  461. }
  462. }
  463. //! This ensures generating z-position at least 25mm above the heat bed.
  464. //! Making this a template enables changing the computation data type easily at all spots where necessary.
  465. //! @param current_z current z-position
  466. //! @return z-position at least 25mm above the heat bed plus FILAMENTCHANGE_ZADD
  467. template <typename T>
  468. inline T fsensor_clamp_z(float current_z){
  469. T z( current_z );
  470. if(z < T(25)){ // make sure the compiler understands, that the constant 25 is of correct type
  471. // - necessary for uint8_t -> results in shorter code
  472. z = T(25); // move to at least 25mm above heat bed
  473. }
  474. return z + T(FILAMENTCHANGE_ZADD); // always move above the printout by FILAMENTCHANGE_ZADD (default 2mm)
  475. }
  476. //! Common code for enqueing M600 and supplemental codes into the command queue.
  477. //! Used both for the IR sensor and the PAT9125
  478. void fsensor_enque_M600(){
  479. printf_P(PSTR("fsensor_update - M600\n"));
  480. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  481. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  482. enquecommand_front_P(PSTR("PRUSA fsensor_recover"));
  483. fsensor_m600_enqueued = true;
  484. enquecommand_front_P((PSTR("M600")));
  485. #define xstr(a) str(a)
  486. #define str(a) #a
  487. static const char gcodeMove[] PROGMEM =
  488. "G1 X" xstr(FILAMENTCHANGE_XPOS)
  489. " Y" xstr(FILAMENTCHANGE_YPOS)
  490. " Z%u";
  491. #undef str
  492. #undef xstr
  493. char buf[32];
  494. // integer arithmetics is far shorter, I don't need a precise float position here, just move a bit above
  495. // 8bit arithmetics in fsensor_clamp_z is 10B shorter than 16bit (not talking about float ;) )
  496. // The compile-time static_assert here ensures, that the computation gets enough bits in case of Z-range too high,
  497. // i.e. makes the user change the data type, which also results in larger code
  498. static_assert(Z_MAX_POS < (255 - FILAMENTCHANGE_ZADD), "Z-range too high, change fsensor_clamp_z<uint8_t> to <uint16_t>");
  499. sprintf_P(buf, gcodeMove, fsensor_clamp_z<uint8_t>(current_position[Z_AXIS]) );
  500. enquecommand_front(buf, false);
  501. }
  502. //! @brief filament sensor update (perform M600 on filament runout)
  503. //!
  504. //! Works only if filament sensor is enabled.
  505. //! When the filament sensor error count is larger then FSENSOR_ERR_MAX, pauses print, tries to move filament back and forth.
  506. //! If there is still no plausible signal from filament sensor plans M600 (Filament change).
  507. void fsensor_update(void)
  508. {
  509. #ifdef PAT9125
  510. if (fsensor_enabled && fsensor_watch_runout && (fsensor_err_cnt > FSENSOR_ERR_MAX) && ( ! fsensor_m600_enqueued) )
  511. {
  512. bool autoload_enabled_tmp = fsensor_autoload_enabled;
  513. fsensor_autoload_enabled = false;
  514. bool oq_meassure_enabled_tmp = fsensor_oq_meassure_enabled;
  515. fsensor_oq_meassure_enabled = true;
  516. fsensor_stop_and_save_print();
  517. fsensor_err_cnt = 0;
  518. fsensor_oq_meassure_start(0);
  519. enquecommand_front_P((PSTR("G1 E-3 F200")));
  520. process_commands();
  521. KEEPALIVE_STATE(IN_HANDLER);
  522. cmdqueue_pop_front();
  523. st_synchronize();
  524. enquecommand_front_P((PSTR("G1 E3 F200")));
  525. process_commands();
  526. KEEPALIVE_STATE(IN_HANDLER);
  527. cmdqueue_pop_front();
  528. st_synchronize();
  529. uint8_t err_cnt = fsensor_err_cnt;
  530. fsensor_oq_meassure_stop();
  531. bool err = false;
  532. err |= (err_cnt > 1);
  533. err |= (fsensor_oq_er_sum > 2);
  534. err |= (fsensor_oq_yd_sum < (4 * FSENSOR_OQ_MIN_YD));
  535. if (!err)
  536. {
  537. printf_P(PSTR("fsensor_err_cnt = 0\n"));
  538. fsensor_restore_print_and_continue();
  539. }
  540. else
  541. {
  542. fsensor_enque_M600();
  543. fsensor_watch_runout = false;
  544. }
  545. fsensor_autoload_enabled = autoload_enabled_tmp;
  546. fsensor_oq_meassure_enabled = oq_meassure_enabled_tmp;
  547. }
  548. #else //PAT9125
  549. if ((digitalRead(IR_SENSOR_PIN) == 1) && CHECK_FSENSOR && fsensor_enabled && ir_sensor_detected && ( ! fsensor_m600_enqueued) )
  550. {
  551. fsensor_stop_and_save_print();
  552. fsensor_enque_M600();
  553. }
  554. #endif //PAT9125
  555. }