fsensor.cpp 7.4 KB

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