fsensor.cpp 8.3 KB

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