fsensor.cpp 7.7 KB

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