fsensor.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #define FSENSOR_ERR_MAX 5 //filament sensor max error count
  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. #define FSENSOR_CHUNK_LEN 280 //filament sensor chunk length in steps
  13. extern void stop_and_save_print_to_ram(float z_move, float e_move);
  14. extern void restore_print_from_ram_and_continue(float e_move);
  15. extern int8_t FSensorStateMenu;
  16. void fsensor_stop_and_save_print()
  17. {
  18. stop_and_save_print_to_ram(0, 0); //XYZE - no change
  19. }
  20. void fsensor_restore_print_and_continue()
  21. {
  22. restore_print_from_ram_and_continue(0); //XYZ = orig, E - no change
  23. }
  24. //uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  25. uint8_t fsensor_int_pin_old = 0;
  26. int16_t fsensor_chunk_len = FSENSOR_CHUNK_LEN;
  27. bool fsensor_enabled = true;
  28. bool fsensor_not_responding = false;
  29. //bool fsensor_ignore_error = true;
  30. bool fsensor_M600 = 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. void fsensor_block()
  41. {
  42. fsensor_enabled = false;
  43. }
  44. void fsensor_unblock() {
  45. fsensor_enabled = (eeprom_read_byte((uint8_t*)EEPROM_FSENSOR) == 0x01);
  46. }
  47. bool fsensor_enable()
  48. {
  49. // puts_P(PSTR("fsensor_enable\n"));
  50. int pat9125 = pat9125_init();
  51. printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
  52. if (pat9125)
  53. fsensor_not_responding = false;
  54. else
  55. fsensor_not_responding = true;
  56. fsensor_enabled = pat9125?true:false;
  57. // fsensor_ignore_error = true;
  58. fsensor_M600 = false;
  59. fsensor_err_cnt = 0;
  60. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled?0x01:0x00);
  61. FSensorStateMenu = fsensor_enabled?1:0;
  62. // printf_P(PSTR("fsensor_enable - end %d\n"), fsensor_enabled?1: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 fautoload_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. puts_P(PSTR("pat9125 not responding (3).\n"));
  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. puts_P(PSTR("pat9125 not responding (2).\n"));
  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. // puts("PCINT2\n");
  154. if (!((fsensor_int_pin_old ^ PINK) & FSENSOR_INT_PIN_MSK)) return;
  155. fsensor_int_pin_old = PINK;
  156. static bool _lock = false;
  157. if (_lock) return;
  158. _lock = true;
  159. // return;
  160. int st_cnt = fsensor_st_cnt;
  161. fsensor_st_cnt = 0;
  162. sei();
  163. /* *digitalPinToPCMSK(fsensor_int_pin) &= ~bit(digitalPinToPCMSKbit(fsensor_int_pin));
  164. digitalWrite(fsensor_int_pin, HIGH);
  165. *digitalPinToPCMSK(fsensor_int_pin) |= bit(digitalPinToPCMSKbit(fsensor_int_pin));*/
  166. if (!pat9125_update_y())
  167. {
  168. //#ifdef DEBUG_FSENSOR_LOG
  169. puts_P(PSTR("pat9125 not responding (1).\n"));
  170. //#endif //DEBUG_FSENSOR_LOG
  171. fsensor_disable();
  172. fsensor_not_responding = true;
  173. }
  174. if (st_cnt != 0)
  175. {
  176. #ifdef DEBUG_FSENSOR_LOG
  177. if (fsensor_log)
  178. {
  179. MYSERIAL.print("cnt=");
  180. MYSERIAL.print(st_cnt, DEC);
  181. MYSERIAL.print(" dy=");
  182. MYSERIAL.print(pat9125_y, DEC);
  183. }
  184. #endif //DEBUG_FSENSOR_LOG
  185. if (st_cnt != 0)
  186. {
  187. if( (pat9125_y == 0) || ((pat9125_y > 0) && (st_cnt < 0)) || ((pat9125_y < 0) && (st_cnt > 0)))
  188. { //invalid movement
  189. if (st_cnt > 0) //only positive movements
  190. fsensor_err_cnt++;
  191. #ifdef DEBUG_FSENSOR_LOG
  192. if (fsensor_log)
  193. {
  194. MYSERIAL.print("\tNG ! err=");
  195. MYSERIAL.println(fsensor_err_cnt, DEC);
  196. }
  197. #endif //DEBUG_FSENSOR_LOG
  198. }
  199. else
  200. { //propper movement
  201. if (fsensor_err_cnt > 0)
  202. fsensor_err_cnt--;
  203. // fsensor_err_cnt = 0;
  204. #ifdef DEBUG_FSENSOR_LOG
  205. if (fsensor_log)
  206. {
  207. MYSERIAL.print("\tOK err=");
  208. MYSERIAL.println(fsensor_err_cnt, DEC);
  209. }
  210. #endif //DEBUG_FSENSOR_LOG
  211. }
  212. }
  213. else
  214. { //no movement
  215. #ifdef DEBUG_FSENSOR_LOG
  216. if (fsensor_log)
  217. MYSERIAL.println("\tOK 0");
  218. #endif //DEBUG_FSENSOR_LOG
  219. }
  220. }
  221. pat9125_y = 0;
  222. _lock = false;
  223. return;
  224. }
  225. void fsensor_st_block_begin(block_t* bl)
  226. {
  227. if (!fsensor_enabled) return;
  228. if (((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8)) ||
  229. ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8)))
  230. {
  231. if (_READ(63)) _WRITE(63, LOW);
  232. else _WRITE(63, HIGH);
  233. }
  234. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  235. // _WRITE(fsensor_int_pin, LOW);
  236. }
  237. void fsensor_st_block_chunk(block_t* bl, int cnt)
  238. {
  239. if (!fsensor_enabled) return;
  240. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  241. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  242. {
  243. if (_READ(63)) _WRITE(63, LOW);
  244. else _WRITE(63, HIGH);
  245. }
  246. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  247. // _WRITE(fsensor_int_pin, LOW);
  248. }
  249. void fsensor_update()
  250. {
  251. if (!fsensor_enabled) return;
  252. if (fsensor_err_cnt > FSENSOR_ERR_MAX)
  253. {
  254. fsensor_stop_and_save_print();
  255. fsensor_err_cnt = 0;
  256. enquecommand_front_P((PSTR("G1 E-3 F200")));
  257. process_commands();
  258. cmdqueue_pop_front();
  259. st_synchronize();
  260. enquecommand_front_P((PSTR("G1 E3 F200")));
  261. process_commands();
  262. cmdqueue_pop_front();
  263. st_synchronize();
  264. if (fsensor_err_cnt == 0)
  265. {
  266. fsensor_restore_print_and_continue();
  267. }
  268. else
  269. {
  270. eeprom_update_byte((uint8_t*)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) + 1);
  271. eeprom_update_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) + 1);
  272. enquecommand_front_P((PSTR("M600")));
  273. fsensor_M600 = true;
  274. fsensor_enabled = false;
  275. }
  276. }
  277. }
  278. #endif //PAT9125