Filament_sensor.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include <inttypes.h>
  3. #include "cmdqueue.h"
  4. #include "pins.h"
  5. #include "fastio.h"
  6. #include "adc.h"
  7. #include "pat9125.h"
  8. #define FSENSOR_IR 1
  9. #define FSENSOR_IR_ANALOG 2
  10. #define FSENSOR_PAT9125 3
  11. #ifdef FILAMENT_SENSOR
  12. class Filament_sensor {
  13. public:
  14. virtual void init() = 0;
  15. virtual void deinit() = 0;
  16. virtual bool update() = 0;
  17. virtual bool getFilamentPresent() = 0;
  18. #ifdef FSENSOR_PROBING
  19. virtual bool probeOtherType() = 0; //checks if the wrong fsensor type is detected.
  20. #endif
  21. enum class State : uint8_t {
  22. disabled = 0,
  23. initializing,
  24. ready,
  25. error,
  26. };
  27. enum class SensorActionOnError : uint8_t {
  28. _Continue = 0,
  29. _Pause = 1,
  30. _Undef = EEPROM_EMPTY_VALUE
  31. };
  32. void setEnabled(bool enabled);
  33. void setAutoLoadEnabled(bool state, bool updateEEPROM = false);
  34. bool getAutoLoadEnabled() {
  35. return autoLoadEnabled;
  36. }
  37. void setRunoutEnabled(bool state, bool updateEEPROM = false);
  38. bool getRunoutEnabled() {
  39. return runoutEnabled;
  40. }
  41. void setActionOnError(SensorActionOnError state, bool updateEEPROM = false);
  42. SensorActionOnError getActionOnError() {
  43. return sensorActionOnError;
  44. }
  45. bool getFilamentLoadEvent() {
  46. return postponedLoadEvent;
  47. }
  48. bool isError() {
  49. return state == State::error;
  50. }
  51. bool isReady() {
  52. return state == State::ready;
  53. }
  54. bool isEnabled() {
  55. return state != State::disabled;
  56. }
  57. protected:
  58. void settings_init_common();
  59. bool checkFilamentEvents();
  60. void triggerFilamentInserted();
  61. void triggerFilamentRemoved();
  62. void filAutoLoad();
  63. void filRunout();
  64. void triggerError();
  65. State state;
  66. bool autoLoadEnabled;
  67. bool runoutEnabled;
  68. bool oldFilamentPresent; //for creating filament presence switching events.
  69. bool postponedLoadEvent; //this event lasts exactly one update cycle. It is long enough to be able to do polling for load event.
  70. ShortTimer eventBlankingTimer;
  71. SensorActionOnError sensorActionOnError;
  72. };
  73. #if (FILAMENT_SENSOR_TYPE == FSENSOR_IR) || (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  74. class IR_sensor: public Filament_sensor {
  75. public:
  76. void init() override;
  77. void deinit() override;
  78. bool update()override ;
  79. bool getFilamentPresent()override;
  80. #ifdef FSENSOR_PROBING
  81. bool probeOtherType()override;
  82. #endif
  83. void settings_init();
  84. };
  85. #if (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  86. constexpr static uint16_t Voltage2Raw(float V) {
  87. return (V * 1023 * OVERSAMPLENR / VOLT_DIV_REF ) + 0.5F;
  88. }
  89. constexpr static float Raw2Voltage(uint16_t raw) {
  90. return VOLT_DIV_REF * (raw / (1023.F * OVERSAMPLENR));
  91. }
  92. class IR_sensor_analog: public IR_sensor {
  93. public:
  94. void init()override;
  95. void deinit()override;
  96. bool update()override;
  97. void voltUpdate(uint16_t raw);
  98. uint16_t getVoltRaw();
  99. void settings_init();
  100. enum class SensorRevision : uint8_t {
  101. _Old = 0,
  102. _Rev04 = 1,
  103. _Undef = EEPROM_EMPTY_VALUE
  104. };
  105. SensorRevision getSensorRevision() {
  106. return sensorRevision;
  107. }
  108. const char* getIRVersionText();
  109. void setSensorRevision(SensorRevision rev, bool updateEEPROM = false);
  110. bool checkVoltage(uint16_t raw);
  111. // Voltage2Raw is not constexpr :/
  112. constexpr static uint16_t IRsensor_Ldiode_TRESHOLD = Voltage2Raw(0.3F); // ~0.3V, raw value=982
  113. constexpr static uint16_t IRsensor_Lmax_TRESHOLD = Voltage2Raw(1.5F); // ~1.5V (0.3*Vcc), raw value=4910
  114. constexpr static uint16_t IRsensor_Hmin_TRESHOLD = Voltage2Raw(3.0F); // ~3.0V (0.6*Vcc), raw value=9821
  115. constexpr static uint16_t IRsensor_Hopen_TRESHOLD = Voltage2Raw(4.6F); // ~4.6V (N.C. @ Ru~20-50k, Rd'=56k, Ru'=10k), raw value=15059
  116. constexpr static uint16_t IRsensor_VMax_TRESHOLD = Voltage2Raw(5.F); // ~5V, raw value=16368
  117. private:
  118. SensorRevision sensorRevision;
  119. volatile bool voltReady; //this gets set by the adc ISR
  120. volatile uint16_t voltRaw;
  121. uint16_t minVolt = Voltage2Raw(6.F);
  122. uint16_t maxVolt = 0;
  123. uint16_t nFSCheckCount;
  124. uint8_t voltageErrorCnt;
  125. static constexpr uint16_t FS_CHECK_COUNT = 4;
  126. /// Switching mechanism of the fsensor type.
  127. /// Called from 2 spots which have a very similar behavior
  128. /// 1: SensorRevision::_Old -> SensorRevision::_Rev04 and print _i("FS v0.4 or newer")
  129. /// 2: SensorRevision::_Rev04 -> sensorRevision=SensorRevision::_Old and print _i("FS v0.3 or older")
  130. void IR_ANALOG_Check(SensorRevision isVersion, SensorRevision switchTo);
  131. };
  132. #endif //(FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  133. #endif //(FILAMENT_SENSOR_TYPE == FSENSOR_IR) || (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
  134. #if (FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125)
  135. class PAT9125_sensor: public Filament_sensor {
  136. public:
  137. void init()override;
  138. void deinit()override;
  139. bool update()override;
  140. bool getFilamentPresent() override{
  141. return filterFilPresent;
  142. }
  143. #ifdef FSENSOR_PROBING
  144. bool probeOtherType() override;
  145. #endif
  146. void setJamDetectionEnabled(bool state, bool updateEEPROM = false);
  147. bool getJamDetectionEnabled() {
  148. return jamDetection;
  149. }
  150. void stStep(bool rev) { //from stepper isr
  151. stepCount += rev ? -1 : 1;
  152. }
  153. void settings_init();
  154. private:
  155. static constexpr uint16_t pollingPeriod = 10; //[ms]
  156. static constexpr uint8_t filterCnt = 5; //how many checks need to be done in order to determine the filament presence precisely.
  157. ShortTimer pollingTimer;
  158. uint8_t filter;
  159. uint8_t filterFilPresent;
  160. bool jamDetection;
  161. int16_t oldPos;
  162. volatile int16_t stepCount;
  163. int16_t chunkSteps;
  164. uint8_t jamErrCnt;
  165. constexpr void calcChunkSteps(float u) {
  166. chunkSteps = (int16_t)(1.25 * u); //[mm]
  167. }
  168. int16_t getStepCount();
  169. void resetStepCount();
  170. void filJam();
  171. bool updatePAT9125();
  172. };
  173. #endif //(FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125)
  174. #if FILAMENT_SENSOR_TYPE == FSENSOR_IR
  175. extern IR_sensor fsensor;
  176. #elif FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG
  177. extern IR_sensor_analog fsensor;
  178. #elif FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125
  179. extern PAT9125_sensor fsensor;
  180. #endif
  181. #endif //FILAMENT_SENSOR