Browse Source

IR_sensor_analog voltage readings

Alex Voinea 3 years ago
parent
commit
43db24e4fe
5 changed files with 23 additions and 22 deletions
  1. 13 8
      Firmware/Filament_sensor.h
  2. 5 5
      Firmware/fsensor.cpp
  3. 1 1
      Firmware/fsensor.h
  4. 1 5
      Firmware/temperature.cpp
  5. 3 3
      Firmware/ultralcd.cpp

+ 13 - 8
Firmware/Filament_sensor.h

@@ -151,12 +151,9 @@ public:
     bool update() {
         bool event = IR_sensor::update();
         if (voltReady) {
-            uint16_t newVoltRaw;
-            ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
-                newVoltRaw = voltRaw;
-                voltReady = false;
-            }
-            printf_P(PSTR("newVoltRaw:%u\n"), newVoltRaw / OVERSAMPLENR);
+            voltReady = false;
+            printf_P(PSTR("newVoltRaw:%u\n"), getVoltRaw() / OVERSAMPLENR);
+            ;//
         }
         
         ;//
@@ -169,6 +166,14 @@ public:
         voltReady = true;
     }
     
+    uint16_t getVoltRaw() {
+        uint16_t newVoltRaw;
+        ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+            newVoltRaw = voltRaw;
+        }
+        return newVoltRaw;
+    }
+    
     void settings_init() {
         IR_sensor::settings_init();
         sensorRevision = (SensorRevision)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_PCB);
@@ -181,8 +186,8 @@ public:
     };
 private:
     SensorRevision sensorRevision;
-    bool voltReady; //this gets set by the adc ISR
-    uint16_t voltRaw;
+    volatile bool voltReady; //this gets set by the adc ISR
+    volatile uint16_t voltRaw;
 };
 
 extern IR_sensor_analog fsensor;

+ 5 - 5
Firmware/fsensor.cpp

@@ -696,8 +696,8 @@ void fsensor_update(void)
 #ifdef IR_SENSOR_ANALOG
 /// This is called only upon start of the printer or when switching the fsensor ON in the menu
 /// We cannot do temporal window checks here (aka the voltage has been in some range for a period of time)
-bool fsensor_IR_check(){
-    if( IRsensor_Lmax_TRESHOLD <= current_voltage_raw_IR && current_voltage_raw_IR <= IRsensor_Hmin_TRESHOLD ){
+bool fsensor_IR_check(uint16_t raw){
+    if( IRsensor_Lmax_TRESHOLD <= raw && raw <= IRsensor_Hmin_TRESHOLD ){
         /// If the voltage is in forbidden range, the fsensor is ok, but the lever is mounted improperly.
         /// Or the user is so creative so that he can hold a piece of fillament in the hole in such a genius way,
         /// that the IR fsensor reading is within 1.5 and 3V ... this would have been highly unusual
@@ -707,13 +707,13 @@ bool fsensor_IR_check(){
     }
     if( oFsensorPCB == ClFsensorPCB::_Rev04 ){
         /// newer IR sensor cannot normally produce 4.6-5V, this is considered a failure/bad mount
-        if( IRsensor_Hopen_TRESHOLD <= current_voltage_raw_IR && current_voltage_raw_IR <= IRsensor_VMax_TRESHOLD ){
+        if( IRsensor_Hopen_TRESHOLD <= raw && raw <= IRsensor_VMax_TRESHOLD ){
             puts_P(PSTR("fsensor v0.4 in fault range 4.6-5V - unconnected"));
             return false;
         }
         /// newer IR sensor cannot normally produce 0-0.3V, this is considered a failure 
 #if 0	//Disabled as it has to be decided if we gonna use this or not.
-        if( IRsensor_Hopen_TRESHOLD <= current_voltage_raw_IR && current_voltage_raw_IR <= IRsensor_VMax_TRESHOLD ){
+        if( IRsensor_Hopen_TRESHOLD <= raw && raw <= IRsensor_VMax_TRESHOLD ){
             puts_P(PSTR("fsensor v0.4 in fault range 0.0-0.3V - wrong IR sensor"));
             return false;
         }
@@ -721,7 +721,7 @@ bool fsensor_IR_check(){
     }
     /// If IR sensor is "uknown state" and filament is not loaded > 1.5V return false
 #if 0
-    if( (oFsensorPCB == ClFsensorPCB::_Undef) && ( current_voltage_raw_IR > IRsensor_Lmax_TRESHOLD ) ){
+    if( (oFsensorPCB == ClFsensorPCB::_Undef) && ( raw > IRsensor_Lmax_TRESHOLD ) ){
         puts_P(PSTR("Unknown IR sensor version and no filament loaded detected."));
         return false;
     }

+ 1 - 1
Firmware/fsensor.h

@@ -95,7 +95,7 @@ extern ClFsensorPCB oFsensorPCB;
 extern ClFsensorActionNA oFsensorActionNA;
 extern const char* FsensorIRVersionText();
 
-extern bool fsensor_IR_check();
+extern bool fsensor_IR_check(uint16_t raw);
 constexpr uint16_t Voltage2Raw(float V){
 	return ( V * 1023 * OVERSAMPLENR / VOLT_DIV_REF ) + 0.5F;
 }

+ 1 - 5
Firmware/temperature.cpp

@@ -125,10 +125,6 @@ int current_voltage_raw_pwr = 0;
 int current_voltage_raw_bed = 0;
 #endif
 
-#ifdef IR_SENSOR_ANALOG
-uint16_t current_voltage_raw_IR = 0;
-#endif //IR_SENSOR_ANALOG
-
 int current_temperature_bed_raw = 0;
 float current_temperature_bed = 0.0;
   
@@ -1188,7 +1184,7 @@ FORCE_INLINE static void applyBabysteps() {
     int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
 
     if(curTodo>0)
-    {
+{
       ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
         babystep(axis,/*fwd*/true);
         babystepsTodo[axis]--; //less to do next time

+ 3 - 3
Firmware/ultralcd.cpp

@@ -1443,7 +1443,7 @@ static void lcd_menu_voltages()
     lcd_home();
     lcd_printf_P(PSTR(" PWR:      %4.1fV\n" " BED:      %4.1fV"), volt_pwr, volt_bed);
 #ifdef IR_SENSOR_ANALOG
-    lcd_printf_P(PSTR("\n IR :       %3.1fV"), Raw2Voltage(current_voltage_raw_IR));
+    lcd_printf_P(PSTR("\n IR :       %3.1fV"), Raw2Voltage(fsensor.getVoltRaw()));
 #endif //IR_SENSOR_ANALOG
     menu_back_if_clicked();
 }
@@ -6211,7 +6211,7 @@ static bool lcd_selftest_IRsensor(bool bStandalone)
     bool bPCBrev04;
     uint16_t volt_IR_int;
 
-    volt_IR_int = current_voltage_raw_IR;
+    volt_IR_int = fsensor.getVoltRaw();
     bPCBrev04=(volt_IR_int < IRsensor_Hopen_TRESHOLD);
     printf_P(PSTR("Measured filament sensor high level: %4.2fV\n"), Raw2Voltage(volt_IR_int) );
     if(volt_IR_int < IRsensor_Hmin_TRESHOLD){
@@ -6220,7 +6220,7 @@ static bool lcd_selftest_IRsensor(bool bStandalone)
         return(false);
     }
     lcd_show_fullscreen_message_and_wait_P(_i("Insert the filament (do not load it) into the extruder and then press the knob."));////MSG_INSERT_FIL c=20 r=6
-    volt_IR_int = current_voltage_raw_IR;
+    volt_IR_int = fsensor.getVoltRaw();
     printf_P(PSTR("Measured filament sensor low level: %4.2fV\n"), Raw2Voltage(volt_IR_int));
     if(volt_IR_int > (IRsensor_Lmax_TRESHOLD)){
         if(!bStandalone)