fsensor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "Marlin.h"
  2. #ifdef PAT9125
  3. #include "fsensor.h"
  4. #include "pat9125.h"
  5. #include "planner.h"
  6. //#include "LiquidCrystal.h"
  7. //extern LiquidCrystal lcd;
  8. #define FSENSOR_ERR_MAX 5 //filament sensor max error count
  9. #define FSENSOR_INT_PIN 63 //filament sensor interrupt pin
  10. #define FSENSOR_CHUNK_LEN 560 //filament sensor chunk length in steps
  11. extern void stop_and_save_print_to_ram(float z_move, float e_move);
  12. extern void restore_print_from_ram_and_continue(float e_move);
  13. extern int8_t FSensorStateMenu;
  14. void fsensor_stop_and_save_print()
  15. {
  16. stop_and_save_print_to_ram(0, 0); //XYZE - no change
  17. }
  18. void fsensor_restore_print_and_continue()
  19. {
  20. restore_print_from_ram_and_continue(0); //XYZ = orig, E - no change
  21. }
  22. uint8_t fsensor_int_pin = FSENSOR_INT_PIN;
  23. int16_t fsensor_chunk_len = FSENSOR_CHUNK_LEN;
  24. bool fsensor_enabled = true;
  25. //bool fsensor_ignore_error = true;
  26. bool fsensor_M600 = false;
  27. uint8_t fsensor_err_cnt = 0;
  28. int16_t fsensor_st_cnt = 0;
  29. uint8_t fsensor_log = 0;
  30. void fsensor_enable()
  31. {
  32. MYSERIAL.println("fsensor_enable");
  33. fsensor_enabled = true;
  34. // fsensor_ignore_error = true;
  35. fsensor_M600 = false;
  36. fsensor_err_cnt = 0;
  37. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0xFF);
  38. FSensorStateMenu = 1;
  39. }
  40. void fsensor_disable()
  41. {
  42. MYSERIAL.println("fsensor_disable");
  43. fsensor_enabled = false;
  44. eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
  45. FSensorStateMenu = 0;
  46. }
  47. void pciSetup(byte pin)
  48. {
  49. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  50. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  51. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  52. }
  53. void fsensor_setup_interrupt()
  54. {
  55. uint8_t fsensor_int_pin = 63;
  56. pinMode(fsensor_int_pin, OUTPUT);
  57. digitalWrite(fsensor_int_pin, HIGH);
  58. pciSetup(fsensor_int_pin);
  59. }
  60. ISR(PCINT2_vect)
  61. {
  62. // return;
  63. int st_cnt = fsensor_st_cnt;
  64. fsensor_st_cnt = 0;
  65. sei();
  66. *digitalPinToPCMSK(fsensor_int_pin) &= ~bit(digitalPinToPCMSKbit(fsensor_int_pin));
  67. digitalWrite(fsensor_int_pin, HIGH);
  68. *digitalPinToPCMSK(fsensor_int_pin) |= bit(digitalPinToPCMSKbit(fsensor_int_pin));
  69. pat9125_update_y();
  70. if (st_cnt != 0)
  71. {
  72. #ifdef DEBUG_FSENSOR_LOG
  73. if (fsensor_log)
  74. {
  75. MYSERIAL.print("cnt=");
  76. MYSERIAL.print(st_cnt, DEC);
  77. MYSERIAL.print(" dy=");
  78. MYSERIAL.print(pat9125_y, DEC);
  79. }
  80. #endif //DEBUG_FSENSOR_LOG
  81. if (st_cnt != 0)
  82. {
  83. if( (pat9125_y == 0) || ((pat9125_y > 0) && (st_cnt < 0)) || ((pat9125_y < 0) && (st_cnt > 0)))
  84. { //invalid movement
  85. if (st_cnt > 0) //only positive movements
  86. fsensor_err_cnt++;
  87. #ifdef DEBUG_FSENSOR_LOG
  88. if (fsensor_log)
  89. {
  90. MYSERIAL.print("\tNG ! err=");
  91. MYSERIAL.println(fsensor_err_cnt, DEC);
  92. }
  93. #endif //DEBUG_FSENSOR_LOG
  94. }
  95. else
  96. { //propper movement
  97. if (fsensor_err_cnt > 0)
  98. fsensor_err_cnt--;
  99. // fsensor_err_cnt = 0;
  100. #ifdef DEBUG_FSENSOR_LOG
  101. if (fsensor_log)
  102. {
  103. MYSERIAL.print("\tOK err=");
  104. MYSERIAL.println(fsensor_err_cnt, DEC);
  105. }
  106. #endif //DEBUG_FSENSOR_LOG
  107. }
  108. }
  109. else
  110. { //no movement
  111. #ifdef DEBUG_FSENSOR_LOG
  112. if (fsensor_log)
  113. MYSERIAL.println("\tOK 0");
  114. #endif //DEBUG_FSENSOR_LOG
  115. }
  116. }
  117. pat9125_y = 0;
  118. return;
  119. }
  120. void fsensor_st_block_begin(block_t* bl)
  121. {
  122. if (!fsensor_enabled) return;
  123. if ((fsensor_st_cnt > 0) && (bl->direction_bits & 0x8))
  124. digitalWrite(fsensor_int_pin, LOW);
  125. if ((fsensor_st_cnt < 0) && !(bl->direction_bits & 0x8))
  126. digitalWrite(fsensor_int_pin, LOW);
  127. }
  128. void fsensor_st_block_chunk(block_t* bl, int cnt)
  129. {
  130. if (!fsensor_enabled) return;
  131. fsensor_st_cnt += (bl->direction_bits & 0x8)?-cnt:cnt;
  132. if ((fsensor_st_cnt >= fsensor_chunk_len) || (fsensor_st_cnt <= -fsensor_chunk_len))
  133. digitalWrite(fsensor_int_pin, LOW);
  134. }
  135. void fsensor_update()
  136. {
  137. if (!fsensor_enabled) return;
  138. if (fsensor_err_cnt > FSENSOR_ERR_MAX)
  139. {
  140. MYSERIAL.println("fsensor_update (fsensor_err_cnt > FSENSOR_ERR_MAX)");
  141. /* if (fsensor_ignore_error)
  142. {
  143. MYSERIAL.println("fsensor_update - error ignored)");
  144. fsensor_ignore_error = false;
  145. }
  146. else*/
  147. {
  148. MYSERIAL.println("fsensor_update - ERROR!!!");
  149. fsensor_stop_and_save_print();
  150. enquecommand_front_P((PSTR("M600")));
  151. fsensor_M600 = true;
  152. fsensor_enabled = false;
  153. }
  154. }
  155. }
  156. #endif //PAT9125