fsensor.cpp 4.0 KB

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