fsensor.cpp 7.9 KB

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