fsensor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #include "Marlin.h"
  2. #include "fsensor.h"
  3. #include <avr/pgmspace.h>
  4. #include "pat9125.h"
  5. #include "stepper.h"
  6. #include "planner.h"
  7. #include "fastio.h"
  8. #include "cmdqueue.h"
  9. #include "ultralcd.h"
  10. #include "ConfigurationStore.h"
  11. //Basic params
  12. #define FSENSOR_CHUNK_LEN 0.64F //filament sensor chunk length 0.64mm
  13. #define FSENSOR_ERR_MAX 17 //filament sensor maximum error count for runout detection
  14. //Optical quality meassurement params
  15. #define FSENSOR_OQ_MAX_ES 6 //maximum error sum while loading (length ~64mm = 100chunks)
  16. #define FSENSOR_OQ_MAX_EM 2 //maximum error counter value while loading
  17. #define FSENSOR_OQ_MIN_YD 2 //minimum yd per chunk (applied to avg value)
  18. #define FSENSOR_OQ_MAX_YD 200 //maximum yd per chunk (applied to avg value)
  19. #define FSENSOR_OQ_MAX_PD 4 //maximum positive deviation (= yd_max/yd_avg)
  20. #define FSENSOR_OQ_MAX_ND 5 //maximum negative deviation (= yd_avg/yd_min)
  21. #define FSENSOR_OQ_MAX_SH 13 //maximum shutter value
  22. const char ERRMSG_PAT9125_NOT_RESP[] PROGMEM = "PAT9125 not responding (%d)!\n";
  23. #define FSENSOR_INT_PIN 63 //filament sensor interrupt pin PK1
  24. #define FSENSOR_INT_PIN_MSK 0x02 //filament sensor interrupt pin mask (bit1)
  25. void fsensor_stop_and_save_print(void)
  26. {
  27. printf_P(PSTR("fsensor_stop_and_save_print\n"));
  28. stop_and_save_print_to_ram(0, 0); //XYZE - no change
  29. }
  30. void fsensor_restore_print_and_continue(void)
  31. {
  32. printf_P(PSTR("fsensor_restore_print_and_continue\n"));
  33. restore_print_from_ram_and_continue(0); //XYZ = orig, E - no change
  34. }
  35. //uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  36. uint8_t fsensor_int_pin_old = 0;
  37. int16_t fsensor_chunk_len = 0;
  38. //enabled = initialized and sampled every chunk event
  39. bool fsensor_enabled = true;
  40. //runout watching is done in fsensor_update (called from main loop)
  41. bool fsensor_watch_runout = true;
  42. //not responding - is set if any communication error occured durring initialization or readout
  43. bool fsensor_not_responding = false;
  44. //printing saved
  45. bool fsensor_printing_saved = false;
  46. //number of errors, updated in ISR
  47. uint8_t fsensor_err_cnt = 0;
  48. //variable for accumolating step count (updated callbacks from stepper and ISR)
  49. int16_t fsensor_st_cnt = 0;
  50. //last dy value from pat9125 sensor (used in ISR)
  51. int16_t fsensor_dy_old = 0;
  52. //log flag: 0=log disabled, 1=log enabled
  53. uint8_t fsensor_log = 1;
  54. ////////////////////////////////////////////////////////////////////////////////
  55. //filament autoload variables
  56. //autoload feature enabled
  57. bool fsensor_autoload_enabled = true;
  58. //autoload watching enable/disable flag
  59. bool fsensor_watch_autoload = false;
  60. //
  61. uint16_t fsensor_autoload_y;
  62. //
  63. uint8_t fsensor_autoload_c;
  64. //
  65. uint32_t fsensor_autoload_last_millis;
  66. //
  67. uint8_t fsensor_autoload_sum;
  68. ////////////////////////////////////////////////////////////////////////////////
  69. //filament optical quality meassurement variables
  70. //meassurement enable/disable flag
  71. bool fsensor_oq_meassure = false;
  72. //skip-chunk counter, for accurate meassurement is necesary to skip first chunk...
  73. uint8_t fsensor_oq_skipchunk;
  74. //number of samples from start of meassurement
  75. uint8_t fsensor_oq_samples;
  76. //sum of steps in positive direction movements
  77. uint16_t fsensor_oq_st_sum;
  78. //sum of deltas in positive direction movements
  79. uint16_t fsensor_oq_yd_sum;
  80. //sum of errors durring meassurement
  81. uint16_t fsensor_oq_er_sum;
  82. //max error counter value durring meassurement
  83. uint8_t fsensor_oq_er_max;
  84. //minimum delta value
  85. int16_t fsensor_oq_yd_min;
  86. //maximum delta value
  87. int16_t fsensor_oq_yd_max;
  88. //sum of shutter value
  89. uint16_t fsensor_oq_sh_sum;
  90. void fsensor_init(void)
  91. {
  92. uint8_t pat9125 = pat9125_init();
  93. printf_P(PSTR("PAT9125_init:%hhu\n"), pat9125);
  94. uint8_t fsensor = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
  95. fsensor_autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
  96. fsensor_chunk_len = (int16_t)(FSENSOR_CHUNK_LEN * cs.axis_steps_per_unit[E_AXIS]);
  97. if (!pat9125)
  98. {
  99. fsensor = 0; //disable sensor
  100. fsensor_not_responding = true;
  101. }
  102. else
  103. fsensor_not_responding = false;
  104. if (fsensor)
  105. fsensor_enable();
  106. else
  107. fsensor_disable();
  108. printf_P(PSTR("FSensor %S\n"), (fsensor_enabled?PSTR("ENABLED"):PSTR("DISABLED\n")));
  109. }
  110. bool fsensor_enable(void)
  111. {
  112. uint8_t pat9125 = pat9125_init();
  113. printf_P(PSTR("PAT9125_init:%hhu\n"), pat9125);
  114. if (pat9125)
  115. fsensor_not_responding = false;
  116. else
  117. fsensor_not_responding = true;
  118. fsensor_enabled = pat9125?true:false;
  119. fsensor_watch_runout = true;
  120. fsensor_oq_meassure = false;
  121. fsensor_err_cnt = 0;
  122. fsensor_dy_old = 0;
  123. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled?0x01:0x00);
  124. FSensorStateMenu = fsensor_enabled?1:0;
  125. return fsensor_enabled;
  126. }
  127. void fsensor_disable(void)
  128. {
  129. fsensor_enabled = false;
  130. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  131. FSensorStateMenu = 0;
  132. }
  133. void fsensor_autoload_set(bool State)
  134. {
  135. fsensor_autoload_enabled = State;
  136. eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, fsensor_autoload_enabled);
  137. }
  138. void pciSetup(byte pin)
  139. {
  140. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  141. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  142. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  143. }
  144. void fsensor_autoload_check_start(void)
  145. {
  146. // puts_P(_N("fsensor_autoload_check_start\n"));
  147. if (!fsensor_enabled) return;
  148. if (!fsensor_autoload_enabled) return;
  149. if (fsensor_watch_autoload) return;
  150. if (!pat9125_update_y()) //update sensor
  151. {
  152. fsensor_disable();
  153. fsensor_not_responding = true;
  154. fsensor_watch_autoload = false;
  155. printf_P(ERRMSG_PAT9125_NOT_RESP, 3);
  156. return;
  157. }
  158. puts_P(_N("fsensor_autoload_check_start - autoload ENABLED\n"));
  159. fsensor_autoload_y = pat9125_y; //save current y value
  160. fsensor_autoload_c = 0; //reset number of changes counter
  161. fsensor_autoload_sum = 0;
  162. fsensor_autoload_last_millis = millis();
  163. fsensor_watch_runout = false;
  164. fsensor_watch_autoload = true;
  165. fsensor_err_cnt = 0;
  166. }
  167. void fsensor_autoload_check_stop(void)
  168. {
  169. // puts_P(_N("fsensor_autoload_check_stop\n"));
  170. if (!fsensor_enabled) return;
  171. // puts_P(_N("fsensor_autoload_check_stop 1\n"));
  172. if (!fsensor_autoload_enabled) return;
  173. // puts_P(_N("fsensor_autoload_check_stop 2\n"));
  174. if (!fsensor_watch_autoload) return;
  175. puts_P(_N("fsensor_autoload_check_stop - autoload DISABLED\n"));
  176. fsensor_autoload_sum = 0;
  177. fsensor_watch_autoload = false;
  178. fsensor_watch_runout = true;
  179. fsensor_err_cnt = 0;
  180. }
  181. bool fsensor_check_autoload(void)
  182. {
  183. if (!fsensor_enabled) return false;
  184. if (!fsensor_autoload_enabled) return false;
  185. if (!fsensor_watch_autoload)
  186. {
  187. fsensor_autoload_check_start();
  188. return false;
  189. }
  190. #if 0
  191. uint8_t fsensor_autoload_c_old = fsensor_autoload_c;
  192. #endif
  193. if ((millis() - fsensor_autoload_last_millis) < 25) return false;
  194. fsensor_autoload_last_millis = millis();
  195. if (!pat9125_update_y()) //update sensor
  196. {
  197. fsensor_disable();
  198. fsensor_not_responding = true;
  199. printf_P(ERRMSG_PAT9125_NOT_RESP, 2);
  200. return false;
  201. }
  202. int16_t dy = pat9125_y - fsensor_autoload_y;
  203. if (dy) //? dy value is nonzero
  204. {
  205. if (dy > 0) //? delta-y value is positive (inserting)
  206. {
  207. fsensor_autoload_sum += dy;
  208. fsensor_autoload_c += 3; //increment change counter by 3
  209. }
  210. else if (fsensor_autoload_c > 1)
  211. fsensor_autoload_c -= 2; //decrement change counter by 2
  212. fsensor_autoload_y = pat9125_y; //save current value
  213. }
  214. else if (fsensor_autoload_c > 0)
  215. fsensor_autoload_c--;
  216. if (fsensor_autoload_c == 0) fsensor_autoload_sum = 0;
  217. #if 0
  218. puts_P(_N("fsensor_check_autoload\n"));
  219. if (fsensor_autoload_c != fsensor_autoload_c_old)
  220. printf_P(PSTR("fsensor_check_autoload dy=%d c=%d sum=%d\n"), dy, fsensor_autoload_c, fsensor_autoload_sum);
  221. #endif
  222. // if ((fsensor_autoload_c >= 15) && (fsensor_autoload_sum > 30))
  223. if ((fsensor_autoload_c >= 12) && (fsensor_autoload_sum > 20))
  224. {
  225. // puts_P(_N("fsensor_check_autoload = true !!!\n"));
  226. return true;
  227. }
  228. return false;
  229. }
  230. void fsensor_oq_meassure_start(uint8_t skip)
  231. {
  232. if (!fsensor_enabled) return;
  233. printf_P(PSTR("fsensor_oq_meassure_start\n"));
  234. fsensor_oq_skipchunk = skip;
  235. fsensor_oq_samples = 0;
  236. fsensor_oq_st_sum = 0;
  237. fsensor_oq_yd_sum = 0;
  238. fsensor_oq_er_sum = 0;
  239. fsensor_oq_er_max = 0;
  240. fsensor_oq_yd_min = FSENSOR_OQ_MAX_YD;
  241. fsensor_oq_yd_max = 0;
  242. fsensor_oq_sh_sum = 0;
  243. pat9125_update();
  244. pat9125_y = 0;
  245. fsensor_watch_runout = false;
  246. fsensor_oq_meassure = true;
  247. }
  248. void fsensor_oq_meassure_stop(void)
  249. {
  250. if (!fsensor_enabled) return;
  251. printf_P(PSTR("fsensor_oq_meassure_stop, %hhu samples\n"), fsensor_oq_samples);
  252. 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);
  253. 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));
  254. fsensor_oq_meassure = false;
  255. fsensor_watch_runout = true;
  256. fsensor_err_cnt = 0;
  257. }
  258. const char _OK[] PROGMEM = "OK";
  259. const char _NG[] PROGMEM = "NG!";
  260. bool fsensor_oq_result(void)
  261. {
  262. if (!fsensor_enabled) return true;
  263. printf_P(_N("fsensor_oq_result\n"));
  264. bool res_er_sum = (fsensor_oq_er_sum <= FSENSOR_OQ_MAX_ES);
  265. printf_P(_N(" er_sum = %u %S\n"), fsensor_oq_er_sum, (res_er_sum?_OK:_NG));
  266. bool res_er_max = (fsensor_oq_er_max <= FSENSOR_OQ_MAX_EM);
  267. printf_P(_N(" er_max = %hhu %S\n"), fsensor_oq_er_max, (res_er_max?_OK:_NG));
  268. uint8_t yd_avg = ((uint32_t)fsensor_oq_yd_sum * fsensor_chunk_len / fsensor_oq_st_sum);
  269. bool res_yd_avg = (yd_avg >= FSENSOR_OQ_MIN_YD) && (yd_avg <= FSENSOR_OQ_MAX_YD);
  270. printf_P(_N(" yd_avg = %hhu %S\n"), yd_avg, (res_yd_avg?_OK:_NG));
  271. bool res_yd_max = (fsensor_oq_yd_max <= (yd_avg * FSENSOR_OQ_MAX_PD));
  272. printf_P(_N(" yd_max = %u %S\n"), fsensor_oq_yd_max, (res_yd_max?_OK:_NG));
  273. bool res_yd_min = (fsensor_oq_yd_min >= (yd_avg / FSENSOR_OQ_MAX_ND));
  274. printf_P(_N(" yd_min = %u %S\n"), fsensor_oq_yd_min, (res_yd_min?_OK:_NG));
  275. uint16_t yd_dev = (fsensor_oq_yd_max - yd_avg) + (yd_avg - fsensor_oq_yd_min);
  276. uint16_t yd_qua = 10 * yd_avg / (yd_dev + 1);
  277. printf_P(_N(" yd_dev = %u\n"), yd_dev);
  278. printf_P(_N(" yd_qua = %u\n"), yd_qua);
  279. uint8_t sh_avg = (fsensor_oq_sh_sum / fsensor_oq_samples);
  280. bool res_sh_avg = (sh_avg <= FSENSOR_OQ_MAX_SH);
  281. if (yd_qua >= 8) res_sh_avg = true;
  282. printf_P(_N(" sh_avg = %hhu %S\n"), sh_avg, (res_sh_avg?_OK:_NG));
  283. bool res = res_er_sum && res_er_max && res_yd_avg && res_yd_max && res_yd_min && res_sh_avg;
  284. printf_P(_N("fsensor_oq_result %S\n"), (res?_OK:_NG));
  285. return res;
  286. }
  287. ISR(PCINT2_vect)
  288. {
  289. if (!((fsensor_int_pin_old ^ PINK) & FSENSOR_INT_PIN_MSK)) return;
  290. fsensor_int_pin_old = PINK;
  291. static bool _lock = false;
  292. if (_lock) return;
  293. _lock = true;
  294. int st_cnt = fsensor_st_cnt;
  295. fsensor_st_cnt = 0;
  296. sei();
  297. uint8_t old_err_cnt = fsensor_err_cnt;
  298. uint8_t pat9125_res = fsensor_oq_meassure?pat9125_update():pat9125_update_y();
  299. if (!pat9125_res)
  300. {
  301. fsensor_disable();
  302. fsensor_not_responding = true;
  303. printf_P(ERRMSG_PAT9125_NOT_RESP, 1);
  304. }
  305. if (st_cnt != 0)
  306. { //movement
  307. if (st_cnt > 0) //positive movement
  308. {
  309. if (pat9125_y < 0)
  310. {
  311. if (fsensor_err_cnt)
  312. fsensor_err_cnt += 2;
  313. else
  314. fsensor_err_cnt++;
  315. }
  316. else if (pat9125_y > 0)
  317. {
  318. if (fsensor_err_cnt)
  319. fsensor_err_cnt--;
  320. }
  321. else //(pat9125_y == 0)
  322. if (((fsensor_dy_old <= 0) || (fsensor_err_cnt)) && (st_cnt > (fsensor_chunk_len >> 1)))
  323. fsensor_err_cnt++;
  324. if (fsensor_oq_meassure)
  325. {
  326. if (fsensor_oq_skipchunk)
  327. {
  328. fsensor_oq_skipchunk--;
  329. fsensor_err_cnt = 0;
  330. }
  331. else
  332. {
  333. if (st_cnt == fsensor_chunk_len)
  334. {
  335. if (pat9125_y > 0) if (fsensor_oq_yd_min > pat9125_y) fsensor_oq_yd_min = (fsensor_oq_yd_min + pat9125_y) / 2;
  336. if (pat9125_y >= 0) if (fsensor_oq_yd_max < pat9125_y) fsensor_oq_yd_max = (fsensor_oq_yd_max + pat9125_y) / 2;
  337. }
  338. fsensor_oq_samples++;
  339. fsensor_oq_st_sum += st_cnt;
  340. if (pat9125_y > 0) fsensor_oq_yd_sum += pat9125_y;
  341. if (fsensor_err_cnt > old_err_cnt)
  342. fsensor_oq_er_sum += (fsensor_err_cnt - old_err_cnt);
  343. if (fsensor_oq_er_max < fsensor_err_cnt)
  344. fsensor_oq_er_max = fsensor_err_cnt;
  345. fsensor_oq_sh_sum += pat9125_s;
  346. }
  347. }
  348. }
  349. else //negative movement
  350. {
  351. }
  352. }
  353. else
  354. { //no movement
  355. }
  356. #ifdef DEBUG_FSENSOR_LOG
  357. if (fsensor_log)
  358. {
  359. 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"));
  360. 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);
  361. }
  362. #endif //DEBUG_FSENSOR_LOG
  363. fsensor_dy_old = pat9125_y;
  364. pat9125_y = 0;
  365. _lock = false;
  366. return;
  367. }
  368. void fsensor_st_block_begin(block_t* bl)
  369. {
  370. if (!fsensor_enabled) return;
  371. if (((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8)) ||
  372. ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8)))
  373. {
  374. if (_READ(63)) _WRITE(63, LOW);
  375. else _WRITE(63, HIGH);
  376. }
  377. }
  378. void fsensor_st_block_chunk(block_t* bl, int cnt)
  379. {
  380. if (!fsensor_enabled) return;
  381. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  382. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  383. {
  384. if (_READ(63)) _WRITE(63, LOW);
  385. else _WRITE(63, HIGH);
  386. }
  387. }
  388. //! update (perform M600 on filament runout)
  389. //!
  390. //! Works only if filament sensor is enabled.
  391. //! When the filament sensor error count is larger then FSENSOR_ERR_MAX, pauses print, tries to move filament back and forth.
  392. //! If there is still no plausible signal from filament sensor plans M600 (Filament change).
  393. void fsensor_update(void)
  394. {
  395. if (fsensor_enabled)
  396. {
  397. if (fsensor_printing_saved)
  398. {
  399. fsensor_restore_print_and_continue();
  400. fsensor_printing_saved = false;
  401. fsensor_watch_runout = true;
  402. fsensor_err_cnt = 0;
  403. }
  404. else if (fsensor_watch_runout && (fsensor_err_cnt > FSENSOR_ERR_MAX))
  405. {
  406. bool autoload_enabled_tmp = fsensor_autoload_enabled;
  407. fsensor_autoload_enabled = false;
  408. fsensor_stop_and_save_print();
  409. fsensor_printing_saved = true;
  410. fsensor_err_cnt = 0;
  411. fsensor_oq_meassure_start(0);
  412. enquecommand_front_P((PSTR("G1 E-3 F200")));
  413. process_commands();
  414. cmdqueue_pop_front();
  415. st_synchronize();
  416. enquecommand_front_P((PSTR("G1 E3 F200")));
  417. process_commands();
  418. cmdqueue_pop_front();
  419. st_synchronize();
  420. fsensor_oq_meassure_stop();
  421. bool err = false;
  422. err |= (fsensor_oq_er_sum > 1);
  423. err |= (fsensor_oq_yd_sum < (4 * FSENSOR_OQ_MIN_YD));
  424. if (!err)
  425. {
  426. printf_P(PSTR("fsensor_err_cnt = 0\n"));
  427. fsensor_restore_print_and_continue();
  428. fsensor_printing_saved = false;
  429. }
  430. else
  431. {
  432. printf_P(PSTR("fsensor_update - M600\n"));
  433. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  434. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  435. enquecommand_front_P((PSTR("M600")));
  436. fsensor_watch_runout = false;
  437. }
  438. fsensor_autoload_enabled = autoload_enabled_tmp;
  439. }
  440. }
  441. }
  442. void fsensor_setup_interrupt(void)
  443. {
  444. pinMode(FSENSOR_INT_PIN, OUTPUT);
  445. digitalWrite(FSENSOR_INT_PIN, LOW);
  446. fsensor_int_pin_old = 0;
  447. pciSetup(FSENSOR_INT_PIN);
  448. }