fsensor.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 fautoload_set(bool State)
  75. {
  76. filament_autoload_enabled = State;
  77. eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, filament_autoload_enabled);
  78. }
  79. void pciSetup(byte pin)
  80. {
  81. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  82. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  83. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  84. }
  85. void fsensor_setup_interrupt()
  86. {
  87. // uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  88. // uint8_t fsensor_int_pcmsk = digitalPinToPCMSKbit(pin);
  89. // uint8_t fsensor_int_pcicr = digitalPinToPCICRbit(pin);
  90. pinMode(FSENSOR_INT_PIN, OUTPUT);
  91. digitalWrite(FSENSOR_INT_PIN, LOW);
  92. fsensor_int_pin_old = 0;
  93. pciSetup(FSENSOR_INT_PIN);
  94. }
  95. void fsensor_autoload_check_start(void)
  96. {
  97. // puts_P(PSTR("fsensor_autoload_check_start\n"));
  98. if (!pat9125_update_y()) //update sensor
  99. {
  100. puts_P(PSTR("pat9125 not responding (3).\n"));
  101. fsensor_disable();
  102. fsensor_not_responding = true;
  103. fsensor_autoload_enabled = false;
  104. return;
  105. }
  106. fsensor_autoload_y = pat9125_y; //save current y value
  107. fsensor_autoload_c = 0; //reset number of changes counter
  108. fsensor_autoload_sum = 0;
  109. fsensor_autoload_last_millis = millis();
  110. fsensor_autoload_enabled = true;
  111. fsensor_err_cnt = 0;
  112. }
  113. void fsensor_autoload_check_stop(void)
  114. {
  115. // puts_P(PSTR("fsensor_autoload_check_stop\n"));
  116. fsensor_autoload_sum = 0;
  117. fsensor_autoload_enabled = false;
  118. fsensor_err_cnt = 0;
  119. }
  120. bool fsensor_check_autoload(void)
  121. {
  122. uint8_t fsensor_autoload_c_old = fsensor_autoload_c;
  123. if ((millis() - fsensor_autoload_last_millis) < 25) return false;
  124. fsensor_autoload_last_millis = millis();
  125. if (!pat9125_update_y())
  126. {
  127. puts_P(PSTR("pat9125 not responding (2).\n"));
  128. fsensor_disable();
  129. fsensor_not_responding = true;
  130. return false; //update sensor
  131. }
  132. int16_t dy = fsensor_autoload_y - pat9125_y;
  133. if (dy) //? y value is different
  134. {
  135. if (dy < 0) //? delta-y value is positive (inserting)
  136. {
  137. fsensor_autoload_sum -= dy;
  138. fsensor_autoload_c += 3; //increment change counter by 3
  139. }
  140. else if (fsensor_autoload_c > 1)
  141. fsensor_autoload_c -= 2; //decrement change counter by 2
  142. fsensor_autoload_y = pat9125_y; //save current value
  143. }
  144. else if (fsensor_autoload_c > 0)
  145. fsensor_autoload_c--;
  146. if (fsensor_autoload_c == 0) fsensor_autoload_sum = 0;
  147. // if (fsensor_autoload_c != fsensor_autoload_c_old)
  148. // printf_P(PSTR("fsensor_check_autoload dy=%d c=%d sum=%d\n"), dy, fsensor_autoload_c, fsensor_autoload_sum);
  149. if ((fsensor_autoload_c >= 15) && (fsensor_autoload_sum > 30))
  150. return true;
  151. return false;
  152. }
  153. ISR(PCINT2_vect)
  154. {
  155. // puts("PCINT2\n");
  156. if (!((fsensor_int_pin_old ^ PINK) & FSENSOR_INT_PIN_MSK)) return;
  157. fsensor_int_pin_old = PINK;
  158. static bool _lock = false;
  159. if (_lock) return;
  160. _lock = true;
  161. // return;
  162. int st_cnt = fsensor_st_cnt;
  163. fsensor_st_cnt = 0;
  164. sei();
  165. /* *digitalPinToPCMSK(fsensor_int_pin) &= ~bit(digitalPinToPCMSKbit(fsensor_int_pin));
  166. digitalWrite(fsensor_int_pin, HIGH);
  167. *digitalPinToPCMSK(fsensor_int_pin) |= bit(digitalPinToPCMSKbit(fsensor_int_pin));*/
  168. if (!pat9125_update_y())
  169. {
  170. //#ifdef DEBUG_FSENSOR_LOG
  171. puts_P(PSTR("pat9125 not responding (1).\n"));
  172. //#endif //DEBUG_FSENSOR_LOG
  173. fsensor_disable();
  174. fsensor_not_responding = true;
  175. }
  176. if (st_cnt != 0)
  177. {
  178. #ifdef DEBUG_FSENSOR_LOG
  179. if (fsensor_log)
  180. {
  181. MYSERIAL.print("cnt=");
  182. MYSERIAL.print(st_cnt, DEC);
  183. MYSERIAL.print(" dy=");
  184. MYSERIAL.print(pat9125_y, DEC);
  185. }
  186. #endif //DEBUG_FSENSOR_LOG
  187. if (st_cnt != 0)
  188. {
  189. if( (pat9125_y == 0) || ((pat9125_y > 0) && (st_cnt < 0)) || ((pat9125_y < 0) && (st_cnt > 0)))
  190. { //invalid movement
  191. if (st_cnt > 0) //only positive movements
  192. fsensor_err_cnt++;
  193. #ifdef DEBUG_FSENSOR_LOG
  194. if (fsensor_log)
  195. {
  196. MYSERIAL.print("\tNG ! err=");
  197. MYSERIAL.println(fsensor_err_cnt, DEC);
  198. }
  199. #endif //DEBUG_FSENSOR_LOG
  200. }
  201. else
  202. { //propper movement
  203. if (fsensor_err_cnt > 0)
  204. fsensor_err_cnt--;
  205. // fsensor_err_cnt = 0;
  206. #ifdef DEBUG_FSENSOR_LOG
  207. if (fsensor_log)
  208. {
  209. MYSERIAL.print("\tOK err=");
  210. MYSERIAL.println(fsensor_err_cnt, DEC);
  211. }
  212. #endif //DEBUG_FSENSOR_LOG
  213. }
  214. }
  215. else
  216. { //no movement
  217. #ifdef DEBUG_FSENSOR_LOG
  218. if (fsensor_log)
  219. MYSERIAL.println("\tOK 0");
  220. #endif //DEBUG_FSENSOR_LOG
  221. }
  222. }
  223. pat9125_y = 0;
  224. _lock = false;
  225. return;
  226. }
  227. void fsensor_st_block_begin(block_t* bl)
  228. {
  229. if (!fsensor_enabled) return;
  230. if (((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8)) ||
  231. ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8)))
  232. {
  233. if (_READ(63)) _WRITE(63, LOW);
  234. else _WRITE(63, HIGH);
  235. }
  236. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  237. // _WRITE(fsensor_int_pin, LOW);
  238. }
  239. void fsensor_st_block_chunk(block_t* bl, int cnt)
  240. {
  241. if (!fsensor_enabled) return;
  242. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  243. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  244. {
  245. if (_READ(63)) _WRITE(63, LOW);
  246. else _WRITE(63, HIGH);
  247. }
  248. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  249. // _WRITE(fsensor_int_pin, LOW);
  250. }
  251. void fsensor_update()
  252. {
  253. if (!fsensor_enabled) return;
  254. if (fsensor_err_cnt > FSENSOR_ERR_MAX)
  255. {
  256. fsensor_stop_and_save_print();
  257. fsensor_err_cnt = 0;
  258. enquecommand_front_P((PSTR("G1 E-3 F200")));
  259. process_commands();
  260. cmdqueue_pop_front();
  261. st_synchronize();
  262. enquecommand_front_P((PSTR("G1 E3 F200")));
  263. process_commands();
  264. cmdqueue_pop_front();
  265. st_synchronize();
  266. if (fsensor_err_cnt == 0)
  267. {
  268. fsensor_restore_print_and_continue();
  269. }
  270. else
  271. {
  272. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  273. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  274. enquecommand_front_P((PSTR("M600")));
  275. fsensor_M600 = true;
  276. fsensor_enabled = false;
  277. }
  278. }
  279. }
  280. #endif //PAT9125