fsensor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_ignore_error = true;
  29. bool fsensor_M600 = false;
  30. uint8_t fsensor_err_cnt = 0;
  31. int16_t fsensor_st_cnt = 0;
  32. uint8_t fsensor_log = 1;
  33. bool fsensor_enable()
  34. {
  35. puts_P(PSTR("fsensor_enable\n"));
  36. int pat9125 = pat9125_init(PAT9125_XRES, PAT9125_YRES);
  37. printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
  38. fsensor_enabled = pat9125?true:false;
  39. // fsensor_ignore_error = true;
  40. fsensor_M600 = false;
  41. fsensor_err_cnt = 0;
  42. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled?0x01:0x00);
  43. FSensorStateMenu = fsensor_enabled?1:0;
  44. return fsensor_enabled;
  45. }
  46. void fsensor_disable()
  47. {
  48. puts_P(PSTR("fsensor_disable\n"));
  49. fsensor_enabled = false;
  50. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  51. FSensorStateMenu = 0;
  52. }
  53. void pciSetup(byte pin)
  54. {
  55. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  56. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  57. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  58. }
  59. void fsensor_setup_interrupt()
  60. {
  61. // uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  62. // uint8_t fsensor_int_pcmsk = digitalPinToPCMSKbit(pin);
  63. // uint8_t fsensor_int_pcicr = digitalPinToPCICRbit(pin);
  64. pinMode(FSENSOR_INT_PIN, OUTPUT);
  65. digitalWrite(FSENSOR_INT_PIN, LOW);
  66. fsensor_int_pin_old = 0;
  67. pciSetup(FSENSOR_INT_PIN);
  68. }
  69. ISR(PCINT2_vect)
  70. {
  71. // return;
  72. if (!((fsensor_int_pin_old ^ PINK) & FSENSOR_INT_PIN_MSK)) return;
  73. // puts("PCINT2\n");
  74. // return;
  75. int st_cnt = fsensor_st_cnt;
  76. fsensor_st_cnt = 0;
  77. sei();
  78. /* *digitalPinToPCMSK(fsensor_int_pin) &= ~bit(digitalPinToPCMSKbit(fsensor_int_pin));
  79. digitalWrite(fsensor_int_pin, HIGH);
  80. *digitalPinToPCMSK(fsensor_int_pin) |= bit(digitalPinToPCMSKbit(fsensor_int_pin));*/
  81. pat9125_update_y();
  82. if (st_cnt != 0)
  83. {
  84. #ifdef DEBUG_FSENSOR_LOG
  85. if (fsensor_log)
  86. {
  87. MYSERIAL.print("cnt=");
  88. MYSERIAL.print(st_cnt, DEC);
  89. MYSERIAL.print(" dy=");
  90. MYSERIAL.print(pat9125_y, DEC);
  91. }
  92. #endif //DEBUG_FSENSOR_LOG
  93. if (st_cnt != 0)
  94. {
  95. if( (pat9125_y == 0) || ((pat9125_y > 0) && (st_cnt < 0)) || ((pat9125_y < 0) && (st_cnt > 0)))
  96. { //invalid movement
  97. if (st_cnt > 0) //only positive movements
  98. fsensor_err_cnt++;
  99. #ifdef DEBUG_FSENSOR_LOG
  100. if (fsensor_log)
  101. {
  102. MYSERIAL.print("\tNG ! err=");
  103. MYSERIAL.println(fsensor_err_cnt, DEC);
  104. }
  105. #endif //DEBUG_FSENSOR_LOG
  106. }
  107. else
  108. { //propper movement
  109. if (fsensor_err_cnt > 0)
  110. fsensor_err_cnt--;
  111. // fsensor_err_cnt = 0;
  112. #ifdef DEBUG_FSENSOR_LOG
  113. if (fsensor_log)
  114. {
  115. MYSERIAL.print("\tOK err=");
  116. MYSERIAL.println(fsensor_err_cnt, DEC);
  117. }
  118. #endif //DEBUG_FSENSOR_LOG
  119. }
  120. }
  121. else
  122. { //no movement
  123. #ifdef DEBUG_FSENSOR_LOG
  124. if (fsensor_log)
  125. MYSERIAL.println("\tOK 0");
  126. #endif //DEBUG_FSENSOR_LOG
  127. }
  128. }
  129. pat9125_y = 0;
  130. return;
  131. }
  132. void fsensor_st_block_begin(block_t* bl)
  133. {
  134. if (!fsensor_enabled) return;
  135. if (((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8)) ||
  136. ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8)))
  137. {
  138. if (_READ(63)) _WRITE(63, LOW);
  139. else _WRITE(63, HIGH);
  140. }
  141. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  142. // _WRITE(fsensor_int_pin, LOW);
  143. }
  144. void fsensor_st_block_chunk(block_t* bl, int cnt)
  145. {
  146. if (!fsensor_enabled) return;
  147. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  148. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  149. {
  150. if (_READ(63)) _WRITE(63, LOW);
  151. else _WRITE(63, HIGH);
  152. }
  153. // PINK |= FSENSOR_INT_PIN_MSK; //toggle pin
  154. // _WRITE(fsensor_int_pin, LOW);
  155. }
  156. void fsensor_update()
  157. {
  158. if (!fsensor_enabled) return;
  159. if (fsensor_err_cnt > FSENSOR_ERR_MAX)
  160. {
  161. MYSERIAL.println("fsensor_update (fsensor_err_cnt > FSENSOR_ERR_MAX)");
  162. /* if (fsensor_ignore_error)
  163. {
  164. MYSERIAL.println("fsensor_update - error ignored)");
  165. fsensor_ignore_error = false;
  166. }
  167. else*/
  168. {
  169. MYSERIAL.println("fsensor_update - ERROR!!!");
  170. fsensor_stop_and_save_print();
  171. enquecommand_front_P((PSTR("M600")));
  172. fsensor_M600 = true;
  173. fsensor_enabled = false;
  174. }
  175. }
  176. }
  177. #endif //PAT9125