fsensor.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "Marlin.h"
  2. #ifdef PAT9125
  3. #include "fsensor.h"
  4. #include "pat9125.h"
  5. #include "planner.h"
  6. #include "fastio.h"
  7. //#include "LiquidCrystal.h"
  8. //extern LiquidCrystal lcd;
  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 560 //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. void fsensor_block()
  40. {
  41. fsensor_enabled = false;
  42. }
  43. void fsensor_unblock() {
  44. fsensor_enabled = (eeprom_read_byte((uint8_t*)EEPROM_FSENSOR) == 0x01);
  45. }
  46. bool fsensor_enable()
  47. {
  48. puts_P(PSTR("fsensor_enable\n"));
  49. int pat9125 = pat9125_init(PAT9125_XRES, PAT9125_YRES);
  50. printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
  51. if (pat9125)
  52. fsensor_not_responding = false;
  53. else
  54. fsensor_not_responding = true;
  55. fsensor_enabled = pat9125?true:false;
  56. // fsensor_ignore_error = true;
  57. fsensor_M600 = false;
  58. fsensor_err_cnt = 0;
  59. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled?0x01:0x00);
  60. FSensorStateMenu = fsensor_enabled?1:0;
  61. return fsensor_enabled;
  62. }
  63. void fsensor_disable()
  64. {
  65. puts_P(PSTR("fsensor_disable\n"));
  66. fsensor_enabled = false;
  67. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  68. FSensorStateMenu = 0;
  69. }
  70. void pciSetup(byte pin)
  71. {
  72. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  73. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  74. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  75. }
  76. void fsensor_setup_interrupt()
  77. {
  78. // uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  79. // uint8_t fsensor_int_pcmsk = digitalPinToPCMSKbit(pin);
  80. // uint8_t fsensor_int_pcicr = digitalPinToPCICRbit(pin);
  81. pinMode(FSENSOR_INT_PIN, OUTPUT);
  82. digitalWrite(FSENSOR_INT_PIN, LOW);
  83. fsensor_int_pin_old = 0;
  84. pciSetup(FSENSOR_INT_PIN);
  85. }
  86. void fsensor_autoload_check_start(void)
  87. {
  88. puts_P(PSTR("fsensor_autoload_check_start\n"));
  89. pat9125_update_y(); //update sensor
  90. fsensor_autoload_y = pat9125_y; //save current y value
  91. fsensor_autoload_c = 0; //reset number of changes counter
  92. fsensor_autoload_last_millis = millis();
  93. fsensor_autoload_enabled = true;
  94. fsensor_err_cnt = 0;
  95. }
  96. void fsensor_autoload_check_stop(void)
  97. {
  98. puts_P(PSTR("fsensor_autoload_check_stop\n"));
  99. fsensor_autoload_enabled = false;
  100. fsensor_err_cnt = 0;
  101. }
  102. bool fsensor_check_autoload(void)
  103. {
  104. if ((millis() - fsensor_autoload_last_millis) < 50) return false;
  105. fsensor_autoload_last_millis = millis();
  106. pat9125_update_y(); //update sensor
  107. uint16_t dy = fsensor_autoload_y - pat9125_y;
  108. if (dy) //? y value is different
  109. {
  110. if (dy > 0) //? delta-y value is positive (inserting)
  111. {
  112. fsensor_autoload_c+=3; //increment change counter
  113. // printf_P(PSTR("fsensor_check_autoload dy=%d c=%d\n"), dy, fsensor_autoload_c);
  114. }
  115. else if (fsensor_autoload_c > 0)
  116. {
  117. fsensor_autoload_c--;
  118. // printf_P(PSTR("fsensor_check_autoload dy=%d c=%d\n"), dy, fsensor_autoload_c);
  119. }
  120. fsensor_autoload_y = pat9125_y; //save current value
  121. if (fsensor_autoload_c > 10) return true; //number of positive changes > 10, start loading
  122. }
  123. else if (fsensor_autoload_c > 0)
  124. {
  125. fsensor_autoload_c--;
  126. // printf_P(PSTR("fsensor_check_autoload dy=%d c=%d\n"), dy, fsensor_autoload_c);
  127. }
  128. return false;
  129. }
  130. ISR(PCINT2_vect)
  131. {
  132. // return;
  133. if (!((fsensor_int_pin_old ^ PINK) & FSENSOR_INT_PIN_MSK)) return;
  134. // puts("PCINT2\n");
  135. // return;
  136. int st_cnt = fsensor_st_cnt;
  137. fsensor_st_cnt = 0;
  138. sei();
  139. /* *digitalPinToPCMSK(fsensor_int_pin) &= ~bit(digitalPinToPCMSKbit(fsensor_int_pin));
  140. digitalWrite(fsensor_int_pin, HIGH);
  141. *digitalPinToPCMSK(fsensor_int_pin) |= bit(digitalPinToPCMSKbit(fsensor_int_pin));*/
  142. if (!pat9125_update_y())
  143. {
  144. puts_P(PSTR("pat9125 not responding.\n"));
  145. fsensor_disable();
  146. fsensor_not_responding = true;
  147. }
  148. if (st_cnt != 0)
  149. {
  150. #ifdef DEBUG_FSENSOR_LOG
  151. if (fsensor_log)
  152. {
  153. MYSERIAL.print("cnt=");
  154. MYSERIAL.print(st_cnt, DEC);
  155. MYSERIAL.print(" dy=");
  156. MYSERIAL.print(pat9125_y, DEC);
  157. }
  158. #endif //DEBUG_FSENSOR_LOG
  159. if (st_cnt != 0)
  160. {
  161. if( (pat9125_y == 0) || ((pat9125_y > 0) && (st_cnt < 0)) || ((pat9125_y < 0) && (st_cnt > 0)))
  162. { //invalid movement
  163. if (st_cnt > 0) //only positive movements
  164. fsensor_err_cnt++;
  165. #ifdef DEBUG_FSENSOR_LOG
  166. if (fsensor_log)
  167. {
  168. MYSERIAL.print("\tNG ! err=");
  169. MYSERIAL.println(fsensor_err_cnt, DEC);
  170. }
  171. #endif //DEBUG_FSENSOR_LOG
  172. }
  173. else
  174. { //propper movement
  175. if (fsensor_err_cnt > 0)
  176. fsensor_err_cnt--;
  177. // fsensor_err_cnt = 0;
  178. #ifdef DEBUG_FSENSOR_LOG
  179. if (fsensor_log)
  180. {
  181. MYSERIAL.print("\tOK err=");
  182. MYSERIAL.println(fsensor_err_cnt, DEC);
  183. }
  184. #endif //DEBUG_FSENSOR_LOG
  185. }
  186. }
  187. else
  188. { //no movement
  189. #ifdef DEBUG_FSENSOR_LOG
  190. if (fsensor_log)
  191. MYSERIAL.println("\tOK 0");
  192. #endif //DEBUG_FSENSOR_LOG
  193. }
  194. }
  195. pat9125_y = 0;
  196. return;
  197. }
  198. void fsensor_st_block_begin(block_t* bl)
  199. {
  200. if (!fsensor_enabled) return;
  201. if (((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8)) ||
  202. ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8)))
  203. {
  204. if (_READ(63)) _WRITE(63, LOW);
  205. else _WRITE(63, HIGH);
  206. }
  207. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  208. // _WRITE(fsensor_int_pin, LOW);
  209. }
  210. void fsensor_st_block_chunk(block_t* bl, int cnt)
  211. {
  212. if (!fsensor_enabled) return;
  213. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  214. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  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_update()
  223. {
  224. if (!fsensor_enabled) return;
  225. if (fsensor_err_cnt > FSENSOR_ERR_MAX)
  226. {
  227. MYSERIAL.println("fsensor_update (fsensor_err_cnt > FSENSOR_ERR_MAX)");
  228. /* if (fsensor_ignore_error)
  229. {
  230. MYSERIAL.println("fsensor_update - error ignored)");
  231. fsensor_ignore_error = false;
  232. }
  233. else*/
  234. {
  235. MYSERIAL.println("fsensor_update - ERROR!!!");
  236. fsensor_stop_and_save_print();
  237. enquecommand_front_P((PSTR("M600")));
  238. fsensor_M600 = true;
  239. fsensor_enabled = false;
  240. }
  241. }
  242. }
  243. #endif //PAT9125