fsensor.cpp 4.7 KB

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