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