fsensor.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "Marlin.h"
  2. #ifdef PAT9125
  3. #include "fsensor.h"
  4. #include "pat9125.h"
  5. #include "stepper.h"
  6. #include "planner.h"
  7. #include "fastio.h"
  8. #include "cmdqueue.h"
  9. const char ERRMSG_PAT9125_NOT_RESP[] PROGMEM = "PAT9125 not responding (%d)!\n";
  10. #define FSENSOR_INT_PIN 63 //filament sensor interrupt pin PK1
  11. #define FSENSOR_INT_PIN_MSK 0x02 //filament sensor interrupt pin mask (bit1)
  12. extern void stop_and_save_print_to_ram(float z_move, float e_move);
  13. extern void restore_print_from_ram_and_continue(float e_move);
  14. extern int8_t FSensorStateMenu;
  15. void fsensor_init(void)
  16. {
  17. int pat9125 = pat9125_init();
  18. printf_P(_N("PAT9125_init:%d\n"), pat9125);
  19. uint8_t fsensor = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
  20. fsensor_autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
  21. if (!pat9125)
  22. {
  23. fsensor = 0; //disable sensor
  24. fsensor_not_responding = true;
  25. }
  26. else {
  27. fsensor_not_responding = false;
  28. }
  29. puts_P(PSTR("FSensor "));
  30. if (fsensor)
  31. {
  32. puts_P(PSTR("ENABLED\n"));
  33. fsensor_enable();
  34. }
  35. else
  36. {
  37. puts_P(PSTR("DISABLED\n"));
  38. fsensor_disable();
  39. }
  40. #ifdef DEBUG_DISABLE_FSENSORCHECK
  41. fsensor_autoload_enabled = false;
  42. fsensor_disable();
  43. #endif //DEBUG_DISABLE_FSENSORCHECK
  44. }
  45. void fsensor_stop_and_save_print(void)
  46. {
  47. stop_and_save_print_to_ram(0, 0); //XYZE - no change
  48. }
  49. void fsensor_restore_print_and_continue(void)
  50. {
  51. restore_print_from_ram_and_continue(0); //XYZ = orig, E - no change
  52. }
  53. //uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  54. uint8_t fsensor_int_pin_old = 0;
  55. int16_t fsensor_chunk_len = FSENSOR_CHUNK_LEN;
  56. bool fsensor_enabled = true;
  57. bool fsensor_watch_runout = true;
  58. bool fsensor_not_responding = false;
  59. uint8_t fsensor_err_cnt = 0;
  60. int16_t fsensor_st_cnt = 0;
  61. uint8_t fsensor_log = 1;
  62. //autoload enable/disable flag
  63. bool fsensor_watch_autoload = false;
  64. uint16_t fsensor_autoload_y = 0;
  65. uint8_t fsensor_autoload_c = 0;
  66. uint32_t fsensor_autoload_last_millis = 0;
  67. uint8_t fsensor_autoload_sum = 0;
  68. //filament optical quality meassurement
  69. bool fsensor_oq_meassure = false;
  70. uint8_t fsensor_oq_skipchunk;
  71. uint32_t fsensor_oq_st_sum;
  72. uint32_t fsensor_oq_yd_sum;
  73. uint16_t fsensor_oq_er_sum;
  74. uint8_t fsensor_oq_er_max;
  75. uint16_t fsensor_oq_yd_min;
  76. uint16_t fsensor_oq_yd_max;
  77. bool fsensor_enable(void)
  78. {
  79. int pat9125 = pat9125_init();
  80. printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
  81. if (pat9125)
  82. fsensor_not_responding = false;
  83. else
  84. fsensor_not_responding = true;
  85. fsensor_enabled = pat9125?true:false;
  86. fsensor_watch_runout = true;
  87. fsensor_oq_meassure = false;
  88. fsensor_err_cnt = 0;
  89. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled?0x01:0x00);
  90. FSensorStateMenu = fsensor_enabled?1:0;
  91. return fsensor_enabled;
  92. }
  93. void fsensor_disable(void)
  94. {
  95. // puts_P(PSTR("fsensor_disable\n"));
  96. fsensor_enabled = false;
  97. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  98. FSensorStateMenu = 0;
  99. }
  100. void fsensor_autoload_set(bool State)
  101. {
  102. fsensor_autoload_enabled = State;
  103. eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, fsensor_autoload_enabled);
  104. }
  105. void pciSetup(byte pin)
  106. {
  107. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  108. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  109. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  110. }
  111. void fsensor_autoload_check_start(void)
  112. {
  113. // puts_P(PSTR("fsensor_autoload_check_start\n"));
  114. if (!pat9125_update_y()) //update sensor
  115. {
  116. printf_P(ERRMSG_PAT9125_NOT_RESP, 3);
  117. fsensor_disable();
  118. fsensor_not_responding = true;
  119. fsensor_watch_autoload = false;
  120. return;
  121. }
  122. fsensor_autoload_y = pat9125_y; //save current y value
  123. fsensor_autoload_c = 0; //reset number of changes counter
  124. fsensor_autoload_sum = 0;
  125. fsensor_autoload_last_millis = millis();
  126. fsensor_watch_autoload = true;
  127. fsensor_err_cnt = 0;
  128. }
  129. void fsensor_autoload_check_stop(void)
  130. {
  131. // puts_P(PSTR("fsensor_autoload_check_stop\n"));
  132. fsensor_autoload_sum = 0;
  133. fsensor_watch_autoload = false;
  134. fsensor_err_cnt = 0;
  135. }
  136. bool fsensor_check_autoload(void)
  137. {
  138. uint8_t fsensor_autoload_c_old = fsensor_autoload_c;
  139. if ((millis() - fsensor_autoload_last_millis) < 25) return false;
  140. fsensor_autoload_last_millis = millis();
  141. if (!pat9125_update_y())
  142. {
  143. printf_P(ERRMSG_PAT9125_NOT_RESP, 2);
  144. fsensor_disable();
  145. fsensor_not_responding = true;
  146. return false; //update sensor
  147. }
  148. int16_t dy = fsensor_autoload_y - pat9125_y;
  149. if (dy) //? y value is different
  150. {
  151. if (dy < 0) //? delta-y value is positive (inserting)
  152. {
  153. fsensor_autoload_sum -= dy;
  154. fsensor_autoload_c += 3; //increment change counter by 3
  155. }
  156. else if (fsensor_autoload_c > 1)
  157. fsensor_autoload_c -= 2; //decrement change counter by 2
  158. fsensor_autoload_y = pat9125_y; //save current value
  159. }
  160. else if (fsensor_autoload_c > 0)
  161. fsensor_autoload_c--;
  162. if (fsensor_autoload_c == 0) fsensor_autoload_sum = 0;
  163. // if (fsensor_autoload_c != fsensor_autoload_c_old)
  164. // printf_P(PSTR("fsensor_check_autoload dy=%d c=%d sum=%d\n"), dy, fsensor_autoload_c, fsensor_autoload_sum);
  165. if ((fsensor_autoload_c >= 15) && (fsensor_autoload_sum > 30))
  166. return true;
  167. return false;
  168. }
  169. void fsensor_oq_meassure_start(void)
  170. {
  171. fsensor_oq_skipchunk = 1;
  172. fsensor_oq_st_sum = 0;
  173. fsensor_oq_yd_sum = 0;
  174. fsensor_oq_er_sum = 0;
  175. fsensor_oq_er_max = 0;
  176. fsensor_oq_yd_min = FSENSOR_OQ_MAX_YD;
  177. fsensor_oq_yd_max = 0;
  178. pat9125_update_y();
  179. pat9125_y = 0;
  180. fsensor_watch_runout = false;
  181. fsensor_oq_meassure = true;
  182. }
  183. void fsensor_oq_meassure_stop(void)
  184. {
  185. fsensor_oq_meassure = false;
  186. }
  187. bool fsensor_oq_result(void)
  188. {
  189. printf(_N("fsensor_oq_result\n"));
  190. if (fsensor_oq_er_sum > FSENSOR_OQ_MAX_ER) return false;
  191. printf(_N(" er_sum OK\n"));
  192. uint8_t yd_avg = fsensor_oq_yd_sum * FSENSOR_CHUNK_LEN / fsensor_oq_st_sum;
  193. if ((yd_avg < FSENSOR_OQ_MIN_YD) || (yd_avg > FSENSOR_OQ_MAX_YD)) return false;
  194. printf(_N(" yd_avg OK\n"));
  195. if (fsensor_oq_yd_max > (yd_avg * FSENSOR_OQ_MAX_PD)) return false;
  196. printf(_N(" yd_max OK\n"));
  197. if (fsensor_oq_yd_min < (yd_avg / FSENSOR_OQ_MAX_ND)) return false;
  198. printf(_N(" yd_min OK\n"));
  199. return true;
  200. }
  201. ISR(PCINT2_vect)
  202. {
  203. if (!((fsensor_int_pin_old ^ PINK) & FSENSOR_INT_PIN_MSK)) return;
  204. fsensor_int_pin_old = PINK;
  205. static bool _lock = false;
  206. if (_lock) return;
  207. _lock = true;
  208. int st_cnt = fsensor_st_cnt;
  209. fsensor_st_cnt = 0;
  210. sei();
  211. uint8_t old_err_cnt = fsensor_err_cnt;
  212. if (!pat9125_update_y())
  213. {
  214. printf_P(ERRMSG_PAT9125_NOT_RESP, 1);
  215. fsensor_disable();
  216. fsensor_not_responding = true;
  217. }
  218. if (st_cnt != 0)
  219. { //movement
  220. if (st_cnt > 0) //positive movement
  221. {
  222. if (pat9125_y <= 0)
  223. {
  224. fsensor_err_cnt++;
  225. }
  226. else
  227. {
  228. if (fsensor_err_cnt)
  229. fsensor_err_cnt--;
  230. }
  231. if (fsensor_oq_meassure)
  232. {
  233. if (fsensor_oq_skipchunk)
  234. fsensor_oq_skipchunk--;
  235. else
  236. {
  237. if (st_cnt == FSENSOR_CHUNK_LEN)
  238. {
  239. if (fsensor_oq_yd_min > pat9125_y) fsensor_oq_yd_min = (fsensor_oq_yd_min + pat9125_y) / 2;
  240. if (fsensor_oq_yd_max < pat9125_y) fsensor_oq_yd_max = (fsensor_oq_yd_max + pat9125_y) / 2;
  241. }
  242. fsensor_oq_st_sum += st_cnt;
  243. fsensor_oq_yd_sum += pat9125_y;
  244. if (fsensor_err_cnt > old_err_cnt)
  245. fsensor_oq_er_sum += (fsensor_err_cnt - old_err_cnt);
  246. if (fsensor_oq_er_max < fsensor_err_cnt)
  247. fsensor_oq_er_max = fsensor_err_cnt;
  248. }
  249. }
  250. }
  251. else //negative movement
  252. {
  253. }
  254. }
  255. else
  256. { //no movement
  257. }
  258. #ifdef DEBUG_FSENSOR_LOG
  259. if (fsensor_log)
  260. {
  261. printf_P(_N("FSENSOR cnt=%d dy=%d err=%d %S\n"), st_cnt, pat9125_y, fsensor_err_cnt, (fsensor_err_cnt > old_err_cnt)?_N("NG!"):_N("OK"));
  262. printf_P(_N("FSENSOR st_sum=%lu yd_sum=%lu er_sum=%u er_max=%u\n"), fsensor_oq_st_sum, fsensor_oq_yd_sum, fsensor_oq_er_sum, fsensor_oq_er_max);
  263. }
  264. #endif //DEBUG_FSENSOR_LOG
  265. pat9125_y = 0;
  266. _lock = false;
  267. return;
  268. }
  269. void fsensor_st_block_begin(block_t* bl)
  270. {
  271. if (!fsensor_enabled) return;
  272. if (((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8)) ||
  273. ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8)))
  274. {
  275. if (_READ(63)) _WRITE(63, LOW);
  276. else _WRITE(63, HIGH);
  277. }
  278. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  279. // _WRITE(fsensor_int_pin, LOW);
  280. }
  281. void fsensor_st_block_chunk(block_t* bl, int cnt)
  282. {
  283. if (!fsensor_enabled) return;
  284. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  285. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  286. {
  287. if (_READ(63)) _WRITE(63, LOW);
  288. else _WRITE(63, HIGH);
  289. }
  290. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  291. // _WRITE(fsensor_int_pin, LOW);
  292. }
  293. void fsensor_update(void)
  294. {
  295. if (fsensor_enabled && fsensor_watch_runout)
  296. if (fsensor_err_cnt > FSENSOR_ERR_MAX)
  297. {
  298. fsensor_stop_and_save_print();
  299. fsensor_err_cnt = 0;
  300. enquecommand_front_P((PSTR("G1 E-3 F200")));
  301. process_commands();
  302. cmdqueue_pop_front();
  303. st_synchronize();
  304. enquecommand_front_P((PSTR("G1 E3 F200")));
  305. process_commands();
  306. cmdqueue_pop_front();
  307. st_synchronize();
  308. if (fsensor_err_cnt == 0)
  309. {
  310. fsensor_restore_print_and_continue();
  311. }
  312. else
  313. {
  314. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  315. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  316. enquecommand_front_P((PSTR("M600")));
  317. fsensor_watch_runout = false;
  318. }
  319. }
  320. }
  321. void fsensor_setup_interrupt(void)
  322. {
  323. // uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  324. // uint8_t fsensor_int_pcmsk = digitalPinToPCMSKbit(pin);
  325. // uint8_t fsensor_int_pcicr = digitalPinToPCICRbit(pin);
  326. pinMode(FSENSOR_INT_PIN, OUTPUT);
  327. digitalWrite(FSENSOR_INT_PIN, LOW);
  328. fsensor_int_pin_old = 0;
  329. pciSetup(FSENSOR_INT_PIN);
  330. }
  331. #endif //PAT9125