Prechádzať zdrojové kódy

Move more IR sensor analog stuff to Filament_sensor.h

Alex Voinea 3 rokov pred
rodič
commit
fc49ba115a

+ 133 - 1
Firmware/Filament_sensor.h

@@ -14,6 +14,7 @@
 #include "eeprom.h"
 #include "pins.h"
 #include "fastio.h"
+#include "adc.h"
 
 class Filament_sensor {
 public:
@@ -152,7 +153,38 @@ public:
         bool event = IR_sensor::update();
         if (voltReady) {
             voltReady = false;
-            printf_P(PSTR("newVoltRaw:%u\n"), getVoltRaw() / OVERSAMPLENR);
+            uint16_t volt = getVoltRaw();
+            printf_P(PSTR("newVoltRaw:%u\n"), volt / OVERSAMPLENR);
+            
+            // detect min-max, some long term sliding window for filtration may be added
+            // avoiding floating point operations, thus computing in raw
+            if(volt > maxVolt) {
+                maxVolt = volt;
+            }
+            else if(volt < minVolt) {
+                minVolt = volt;
+            }
+            //! The trouble is, I can hold the filament in the hole in such a way, that it creates the exact voltage
+            //! to be detected as the new fsensor
+            //! We can either fake it by extending the detection window to a looooong time
+            //! or do some other countermeasures
+            
+            //! what we want to detect:
+            //! if minvolt gets below ~0.3V, it means there is an old fsensor
+            //! if maxvolt gets above 4.6V, it means we either have an old fsensor or broken cables/fsensor
+            //! So I'm waiting for a situation, when minVolt gets to range <0, 1.5> and maxVolt gets into range <3.0, 5>
+            //! If and only if minVolt is in range <0.3, 1.5> and maxVolt is in range <3.0, 4.6>, I'm considering a situation with the new fsensor
+            if(minVolt >= IRsensor_Ldiode_TRESHOLD && minVolt <= IRsensor_Lmax_TRESHOLD && maxVolt >= IRsensor_Hmin_TRESHOLD && maxVolt <= IRsensor_Hopen_TRESHOLD) {
+                IR_ANALOG_Check(SensorRevision::_Old, SensorRevision::_Rev04, _i("FS v0.4 or newer") ); ////MSG_FS_V_04_OR_NEWER c=18
+            }
+            //! If and only if minVolt is in range <0.0, 0.3> and maxVolt is in range  <4.6, 5.0V>, I'm considering a situation with the old fsensor
+            //! Note, we are not relying on one voltage here - getting just +5V can mean an old fsensor or a broken new sensor - that's why
+            //! we need to have both voltages detected correctly to allow switching back to the old fsensor.
+            else if( minVolt < IRsensor_Ldiode_TRESHOLD && maxVolt > IRsensor_Hopen_TRESHOLD && maxVolt <= IRsensor_VMax_TRESHOLD) {
+                IR_ANALOG_Check(SensorRevision::_Rev04, sensorRevision=SensorRevision::_Old, _i("FS v0.3 or older")); ////MSG_FS_V_03_OR_OLDER c=18
+            }
+            
+            
             ;//
         }
         
@@ -184,10 +216,110 @@ public:
         _Rev04 = 1,
         _Undef = EEPROM_EMPTY_VALUE
     };
+    
+    SensorRevision getSensorRevision() {
+        return sensorRevision;
+    }
+    
+    const char* getIRVersionText() {
+        switch(sensorRevision) {
+            case SensorRevision::_Old:
+                return _T(MSG_IR_03_OR_OLDER);
+            case SensorRevision::_Rev04:
+                return _T(MSG_IR_04_OR_NEWER);
+            default:
+                return _T(MSG_IR_UNKNOWN);
+        }
+    }
+    
+    void setSensorRevision(SensorRevision rev, bool updateEEPROM = false) {
+        sensorRevision = rev;
+        if (updateEEPROM) {
+            eeprom_update_byte((uint8_t *)EEPROM_FSENSOR_PCB, (uint8_t)rev);
+        }
+    }
+    
+    uint16_t Voltage2Raw(float V) {
+        return (V * 1023 * OVERSAMPLENR / VOLT_DIV_REF ) + 0.5F;
+    }
+    float Raw2Voltage(uint16_t raw) {
+        return VOLT_DIV_REF * (raw / (1023.F * OVERSAMPLENR));
+    }
+    
+    /// 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 checkVoltage(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
+            /// and would have been considered more like a sabotage than normal printer operation
+            puts_P(PSTR("fsensor in forbidden range 1.5-3V - check sensor"));
+            return false; 
+        }
+        if(sensorRevision == SensorRevision::_Rev04) {
+            /// newer IR sensor cannot normally produce 4.6-5V, this is considered a failure/bad mount
+            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 <= raw && raw <= IRsensor_VMax_TRESHOLD) {
+                puts_P(PSTR("fsensor v0.4 in fault range 0.0-0.3V - wrong IR sensor"));
+                return false;
+            }
+    #endif
+        }
+        /// If IR sensor is "uknown state" and filament is not loaded > 1.5V return false
+    #if 0
+        if((sensorRevision == SensorRevision::_Undef) && (raw > IRsensor_Lmax_TRESHOLD)) {
+            puts_P(PSTR("Unknown IR sensor version and no filament loaded detected."));
+            return false;
+        }
+    #endif
+        // otherwise the IR fsensor is considered working correctly
+        return true;
+    }
+    
+    // Voltage2Raw is not constexpr :/
+    const uint16_t IRsensor_Ldiode_TRESHOLD = Voltage2Raw(0.3f); // ~0.3V, raw value=982
+    const uint16_t IRsensor_Lmax_TRESHOLD = Voltage2Raw(1.5f); // ~1.5V (0.3*Vcc), raw value=4910
+    const uint16_t IRsensor_Hmin_TRESHOLD = Voltage2Raw(3.0f); // ~3.0V (0.6*Vcc), raw value=9821
+    const uint16_t IRsensor_Hopen_TRESHOLD = Voltage2Raw(4.6f); // ~4.6V (N.C. @ Ru~20-50k, Rd'=56k, Ru'=10k), raw value=15059
+    const uint16_t IRsensor_VMax_TRESHOLD = Voltage2Raw(5.f); // ~5V, raw value=16368
+    
 private:
     SensorRevision sensorRevision;
     volatile bool voltReady; //this gets set by the adc ISR
     volatile uint16_t voltRaw;
+    uint16_t minVolt = Voltage2Raw(6.f);
+    uint16_t maxVolt = 0;
+    uint16_t nFSCheckCount;
+
+    static constexpr uint16_t FS_CHECK_COUNT = 4;
+    /// Switching mechanism of the fsensor type.
+    /// Called from 2 spots which have a very similar behavior
+    /// 1: SensorRevision::_Old -> SensorRevision::_Rev04 and print _i("FS v0.4 or newer")
+    /// 2: SensorRevision::_Rev04 -> sensorRevision=SensorRevision::_Old and print _i("FS v0.3 or older")
+    void IR_ANALOG_Check(SensorRevision isVersion, SensorRevision switchTo, const char *statusLineTxt_P) {
+        bool bTemp = (!CHECK_ALL_HEATERS);
+        bTemp = bTemp && (menu_menu == lcd_status_screen);
+        bTemp = bTemp && ((sensorRevision == isVersion) || (sensorRevision == SensorRevision::_Undef));
+        bTemp = bTemp && ready;
+        if (bTemp) {
+            nFSCheckCount++;
+            if (nFSCheckCount > FS_CHECK_COUNT) {
+                nFSCheckCount = 0; // not necessary
+                setSensorRevision(switchTo, true);
+                printf_IRSensorAnalogBoardChange();
+                lcd_setstatuspgm(statusLineTxt_P);
+            }
+        }
+        else {
+            nFSCheckCount = 0;
+        }
+    }
 };
 
 extern IR_sensor_analog fsensor;

+ 1 - 27
Firmware/Marlin_main.cpp

@@ -876,7 +876,7 @@ static void check_if_fw_is_on_right_printer(){
 
     #ifdef PAT9125
       //will return 1 only if IR can detect filament in bondtech extruder so this may fail even when we have IR sensor
-      const uint8_t ir_detected = !READ(IR_SENSOR_PIN);
+      const uint8_t ir_detected = fsensor.getFilamentPresent();
       if (ir_detected){
         lcd_show_fullscreen_message_and_wait_P(_i("MK3 firmware detected on MK3S printer"));}////MSG_MK3_FIRMWARE_ON_MK3S c=20 r=4
     #endif //PAT9125
@@ -9406,32 +9406,6 @@ static void handleSafetyTimer()
 }
 #endif //SAFETYTIMER
 
-#ifdef IR_SENSOR_ANALOG
-#define FS_CHECK_COUNT 16
-/// Switching mechanism of the fsensor type.
-/// Called from 2 spots which have a very similar behavior
-/// 1: ClFsensorPCB::_Old -> ClFsensorPCB::_Rev04 and print _i("FS v0.4 or newer")
-/// 2: ClFsensorPCB::_Rev04 -> oFsensorPCB=ClFsensorPCB::_Old and print _i("FS v0.3 or older")
-void manage_inactivity_IR_ANALOG_Check(uint16_t &nFSCheckCount, ClFsensorPCB isVersion, ClFsensorPCB switchTo, const char *statusLineTxt_P) {
-    bool bTemp = (!CHECK_ALL_HEATERS);
-    bTemp = bTemp && (menu_menu == lcd_status_screen);
-    bTemp = bTemp && ((oFsensorPCB == isVersion) || (oFsensorPCB == ClFsensorPCB::_Undef));
-    bTemp = bTemp && fsensor_enabled;
-    if (bTemp) {
-        nFSCheckCount++;
-        if (nFSCheckCount > FS_CHECK_COUNT) {
-            nFSCheckCount = 0; // not necessary
-            oFsensorPCB = switchTo;
-            eeprom_update_byte((uint8_t *)EEPROM_FSENSOR_PCB, (uint8_t)oFsensorPCB);
-            printf_IRSensorAnalogBoardChange();
-            lcd_setstatuspgm(statusLineTxt_P);
-        }
-    } else {
-        nFSCheckCount = 0;
-    }
-}
-#endif
-
 void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument set in Marlin.h
 {
 #ifdef FILAMENT_SENSOR

+ 1 - 2
Firmware/fsensor.cpp

@@ -111,7 +111,6 @@ uint16_t fsensor_oq_sh_sum;
 //! @}
 
 #ifdef IR_SENSOR_ANALOG
-ClFsensorPCB oFsensorPCB;
 ClFsensorActionNA oFsensorActionNA;
 bool bIRsensorStateFlag=false;
 ShortTimer tIRsensorCheckTimer;
@@ -230,7 +229,7 @@ bool fsensor_enable(bool bUpdateEEPROM)
 	}
 #else // PAT9125
 #ifdef IR_SENSOR_ANALOG
-     if(!fsensor_IR_check(fsensor.getVoltRaw()))
+     if(!fsensor.checkVoltage(fsensor.getVoltRaw()))
           {
           bUpdateEEPROM=true;
           fsensor_enabled=false;

+ 0 - 25
Firmware/fsensor.h

@@ -66,17 +66,7 @@ extern uint8_t fsensor_log;
 //! @}
 #endif //PAT9125
 
-#define VOLT_DIV_REF 5
-
 #ifdef IR_SENSOR_ANALOG
-#define IR_SENSOR_STEADY 10                       // [ms]
-
-enum class ClFsensorPCB:uint_least8_t
-{
-    _Old=0,
-    _Rev04=1,
-    _Undef=EEPROM_EMPTY_VALUE
-};
 
 enum class ClFsensorActionNA:uint_least8_t
 {
@@ -85,22 +75,7 @@ enum class ClFsensorActionNA:uint_least8_t
     _Undef=EEPROM_EMPTY_VALUE
 };
 
-extern ClFsensorPCB oFsensorPCB;
 extern ClFsensorActionNA oFsensorActionNA;
-extern const char* FsensorIRVersionText();
-
-extern bool fsensor_IR_check(uint16_t raw);
-constexpr uint16_t Voltage2Raw(float V){
-	return ( V * 1023 * OVERSAMPLENR / VOLT_DIV_REF ) + 0.5F;
-}
-constexpr float Raw2Voltage(uint16_t raw){
-	return VOLT_DIV_REF*(raw / (1023.F * OVERSAMPLENR) );
-}
-constexpr uint16_t IRsensor_Ldiode_TRESHOLD = Voltage2Raw(0.3F); // ~0.3V, raw value=982
-constexpr uint16_t IRsensor_Lmax_TRESHOLD = Voltage2Raw(1.5F); // ~1.5V (0.3*Vcc), raw value=4910
-constexpr uint16_t IRsensor_Hmin_TRESHOLD = Voltage2Raw(3.0F); // ~3.0V (0.6*Vcc), raw value=9821
-constexpr uint16_t IRsensor_Hopen_TRESHOLD = Voltage2Raw(4.6F); // ~4.6V (N.C. @ Ru~20-50k, Rd'=56k, Ru'=10k), raw value=15059
-constexpr uint16_t IRsensor_VMax_TRESHOLD = Voltage2Raw(5.F); // ~5V, raw value=16368
 
 #endif //IR_SENSOR_ANALOG
 

+ 0 - 2
Firmware/mmu.cpp

@@ -157,8 +157,6 @@ void mmu_init(void)
 	_delay_ms(10);                             //wait 10ms for sure
 	mmu_reset();                               //reset mmu (HW or SW), do not wait for response
 	mmu_state = S::Init;
-	SET_INPUT(IR_SENSOR_PIN); //input mode
-	WRITE(IR_SENSOR_PIN, 1); //pullup
 }
 
 //if IR_SENSOR defined, always returns true

+ 31 - 26
Firmware/ultralcd.cpp

@@ -1439,7 +1439,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(fsensor.getVoltRaw()));
+    lcd_printf_P(PSTR("\n IR :       %3.1fV"), fsensor.Raw2Voltage(fsensor.getVoltRaw()));
 #endif //IR_SENSOR_ANALOG
     menu_back_if_clicked();
 }
@@ -1676,7 +1676,7 @@ static void lcd_support_menu()
 #ifdef IR_SENSOR_ANALOG
   MENU_ITEM_BACK_P(STR_SEPARATOR);
   MENU_ITEM_BACK_P(PSTR("Fil. sensor v.:"));
-  MENU_ITEM_BACK_P(FsensorIRVersionText());
+  MENU_ITEM_BACK_P(fsensor.getIRVersionText());
 #endif // IR_SENSOR_ANALOG
 
 	MENU_ITEM_BACK_P(STR_SEPARATOR);
@@ -3444,7 +3444,7 @@ static void lcd_show_sensors_state()
 	}
 	
 	if (ir_sensor_detected) {
-		idler_state = !READ(IR_SENSOR_PIN);
+		idler_state = fsensor.getFilamentPresent();
 		lcd_puts_at_P(0, 1, _T(MSG_FSENSOR));
 		lcd_set_cursor(LCD_WIDTH - 3, 1);
 		lcd_print_state(idler_state);
@@ -3845,7 +3845,7 @@ void lcd_v2_calibration()
 	    bool loaded = false;
 	    if (fsensor_enabled && ir_sensor_detected)
 	    {
-	        loaded = !READ(IR_SENSOR_PIN);
+	        loaded = fsensor.getFilamentPresent();
 	    }
 	    else
 	    {
@@ -6199,36 +6199,41 @@ void lcd_belttest()
 #ifdef IR_SENSOR_ANALOG
 // called also from marlin_main.cpp
 void printf_IRSensorAnalogBoardChange(){
-    printf_P(PSTR("Filament sensor board change detected: revision%S\n"), FsensorIRVersionText());
+    printf_P(PSTR("Filament sensor board change detected: revision%S\n"), fsensor.getIRVersionText());
 }
 
 static bool lcd_selftest_IRsensor(bool bStandalone)
 {
-    bool bPCBrev04;
-    uint16_t volt_IR_int;
-
-    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){
+    bool ret = false;
+    fsensor.setAutoLoadEnabled(false);
+    fsensor.setRunoutEnabled(false);
+    IR_sensor_analog::SensorRevision oldSensorRevision = fsensor.getSensorRevision();
+    IR_sensor_analog::SensorRevision newSensorRevision;
+    uint16_t volt_IR_int = fsensor.getVoltRaw();
+    
+    newSensorRevision = (volt_IR_int < fsensor.IRsensor_Hopen_TRESHOLD) ? IR_sensor_analog::SensorRevision::_Rev04 : IR_sensor_analog::SensorRevision::_Old;
+    printf_P(PSTR("Measured filament sensor high level: %4.2fV\n"), fsensor.Raw2Voltage(volt_IR_int) );
+    if(volt_IR_int < fsensor.IRsensor_Hmin_TRESHOLD){
         if(!bStandalone)
             lcd_selftest_error(TestError::FsensorLevel,"HIGH","");
-        return(false);
+        goto exit;
     }
     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 = fsensor.getVoltRaw();
-    printf_P(PSTR("Measured filament sensor low level: %4.2fV\n"), Raw2Voltage(volt_IR_int));
-    if(volt_IR_int > (IRsensor_Lmax_TRESHOLD)){
+    printf_P(PSTR("Measured filament sensor low level: %4.2fV\n"), fsensor.Raw2Voltage(volt_IR_int));
+    if(volt_IR_int > (fsensor.IRsensor_Lmax_TRESHOLD)){
         if(!bStandalone)
             lcd_selftest_error(TestError::FsensorLevel,"LOW","");
-        return(false);
+        goto exit;
     }
-    if((bPCBrev04 ? 1 : 0) != (uint8_t)oFsensorPCB){        // safer then "(uint8_t)bPCBrev04"
-        oFsensorPCB=bPCBrev04 ? ClFsensorPCB::_Rev04 : ClFsensorPCB::_Old;
+    if(newSensorRevision != oldSensorRevision) {
+        fsensor.setSensorRevision(newSensorRevision, true);
         printf_IRSensorAnalogBoardChange();
-        eeprom_update_byte((uint8_t*)EEPROM_FSENSOR_PCB,(uint8_t)oFsensorPCB);
     }
-    return(true);
+    ret = true;
+exit:
+    fsensor.settings_init();
+    return ret;
 }
 
 static void lcd_detect_IRsensor(){
@@ -6236,8 +6241,8 @@ static void lcd_detect_IRsensor(){
     bool loaded;
     /// Check if filament is loaded. If it is loaded stop detection.
     /// @todo Add autodetection with MMU2s
-    loaded = ! READ(IR_SENSOR_PIN);
-    if(loaded ){
+    loaded = fsensor.getFilamentPresent();
+    if(loaded){
         lcd_show_fullscreen_message_and_wait_P(_i("Please unload the filament first, then repeat this action."));////MSG_UNLOAD_FILAMENT_REPEAT c=20 r=4
         return;
     } else {
@@ -6269,12 +6274,12 @@ bool lcd_selftest()
 	//!   Check if IR sensor is in unknown state, if so run Fsensor Detection
 	//!   As the Fsensor Detection isn't yet ready for the mmu2s we set temporarily the IR sensor 0.3 or older for mmu2s
 	//! @todo Don't forget to remove this as soon Fsensor Detection works with mmu
-	if( oFsensorPCB == ClFsensorPCB::_Undef) {
+	if(fsensor.getSensorRevision() == IR_sensor_analog::SensorRevision::_Undef) {
 		if (!mmu_enabled) {
 			lcd_detect_IRsensor();
 		}
 		else {
-			eeprom_update_byte((uint8_t*)EEPROM_FSENSOR_PCB,0);
+			fsensor.setSensorRevision(IR_sensor_analog::SensorRevision::_Old, true);
 		}
 	}
 #endif //IR_SENSOR_ANALOG
@@ -7066,7 +7071,7 @@ static bool lcd_selftest_fsensor(void)
 //!  * Pre-heat to PLA extrude temperature.
 //!  * Unload filament possibly present.
 //!  * Move extruder idler same way as during filament load
-//!    and sample IR_SENSOR_PIN.
+//!    and sample the filament sensor.
 //!  * Check that pin doesn't go low.
 //!
 //! @retval true passed
@@ -7103,7 +7108,7 @@ static bool selftest_irsensor()
         mmu_load_step(false);
         while (blocks_queued())
         {
-            if (READ(IR_SENSOR_PIN) == 0)
+            if (fsensor.getFilamentPresent())
             {
                 lcd_selftest_error(TestError::TriggeringFsensor, "", "");
                 return false;