fsensor.cpp 6.7 KB

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