fsensor.cpp 7.6 KB

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