remove automatic calibration

This commit is contained in:
interfisch 2023-04-20 21:53:41 +02:00
parent 78cc0e5c5a
commit 5f3e8cc10c
1 changed files with 4 additions and 149 deletions

View File

@ -4,7 +4,6 @@
#include <Arduino.h> #include <Arduino.h>
#define EC_PIN_RELAY_PROBE 27 #define EC_PIN_RELAY_PROBE 27
#define EC_PIN_RELAY_CALIBRATION 26
#define EC_PIN_RELAY_RANGE 25 #define EC_PIN_RELAY_RANGE 25
@ -22,35 +21,20 @@ uint16_t ec_array_pos=EC_ARRAY_SIZE*2;
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*2 //One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*2
#define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement #define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement
float ec_calib_rangeLow_Rlow=0; //adc value for low value resistor on low resistor value range
float ec_calib_rangeLow_Rhigh=0; //adc value for high value resistor on low resistor value range
float ec_calib_rangeHigh_Rlow=0; //adc value for low value resistor on high resistor value range
float ec_calib_rangeHigh_Rhigh=0; //adc value for high value resistor on high resistor value range
const float ec_calibresistor_low=990; //value of low value calibration resistor. Low is Relay NO
const float ec_calibresistor_high=9943; //value of high value calibration resistor. HIGH is Relay NC
unsigned long ec_last_calibration=0; //millis of last calibration
#define EC_CALIBRATION_VALID_TIME 120000 //time in ms a calibration is valid for
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled #define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
unsigned long ec_last_change_relay=0; //millis of last relay change unsigned long ec_last_change_relay=0; //millis of last relay change
enum ECState{IDLE,CALIBRATELOW,CALIBRATEHIGH,MEASURE}; enum ECState{IDLE,MEASURE};
ECState ecstate=CALIBRATELOW; ECState ecstate=IDLE;
bool ec_measurementReady(); bool ec_measurementReady();
void ec_startMeasurement(); void ec_startMeasurement();
void ec_setRange(bool); void ec_setRange(bool);
void ec_connectProbe(bool); void ec_connectProbe(bool);
void ec_setCalibration(bool calib);
void ec_releaseRelay(); void ec_releaseRelay();
void ec_startCalibration();
void ec_checkIfSettleTimeOK();
float ec_getResistance(float adc,float caliblow,float resistorlow,float calibhigh,float resistorhigh);
void ec_setup() { void ec_setup() {
pinMode(EC_PIN_ADC,INPUT); pinMode(EC_PIN_ADC,INPUT);
@ -60,12 +44,9 @@ void ec_setup() {
ledcWrite(EC_PWM_CH, 127); //50% duty cycle ledcWrite(EC_PWM_CH, 127); //50% duty cycle
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected
pinMode(EC_PIN_RELAY_CALIBRATION,OUTPUT); //LOW=NC Calibration Resistor, HIGH=NO Calib. Res.
pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor
ec_releaseRelay(); ec_releaseRelay();
ec_startCalibration();
} }
void ec_loop(unsigned long loopmillis) { void ec_loop(unsigned long loopmillis) {
@ -75,14 +56,6 @@ void ec_loop(unsigned long loopmillis) {
switch (ecstate) { switch (ecstate) {
case IDLE: case IDLE:
if (loopmillis>ec_last_calibration+EC_CALIBRATION_VALID_TIME) { //calibration needed
ec_last_calibration=loopmillis;
ecstate=CALIBRATELOW;
ec_startCalibration();
}
if (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL && ecstate==IDLE) { //start measurement if idle if (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL && ecstate==IDLE) { //start measurement if idle
last_measurement_ec=loopmillis; last_measurement_ec=loopmillis;
ec_startMeasurement(); ec_startMeasurement();
@ -92,69 +65,14 @@ void ec_loop(unsigned long loopmillis) {
} }
break; break;
case CALIBRATELOW:
if (ec_measurementReady()) {
//Serial.println("EC CALIBRATELOW measurement ready");
//save measurement
ec_calib_rangeLow_Rlow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
ec_calib_rangeHigh_Rlow=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);
//ec_checkIfSettleTimeOK();
//Switch to High calibration
ecstate=CALIBRATEHIGH;
ec_setCalibration(HIGH);
ec_setRange(LOW);
ec_startMeasurement();
//Serial.println("EC Start calibration high");
}
break;
case CALIBRATEHIGH:
if (ec_measurementReady()) {
//Serial.println("EC CALIBRATEHIGH measurement ready");
//save measurement
ec_calib_rangeLow_Rhigh=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
ec_calib_rangeHigh_Rhigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);
//ec_checkIfSettleTimeOK();
//Serial.println("EC Release Relay");
ec_releaseRelay();
ecstate=IDLE;
/*
Serial.println("EC Calibration done");
Serial.print("ec_calib_rangeLow_Rlow="); Serial.println(ec_calib_rangeLow_Rlow);
Serial.print("ec_calib_rangeHigh_Rlow="); Serial.println(ec_calib_rangeHigh_Rlow);
Serial.print("ec_calib_rangeLow_Rhigh="); Serial.println(ec_calib_rangeLow_Rhigh);
Serial.print("ec_calib_rangeHigh_Rhigh="); Serial.println(ec_calib_rangeHigh_Rhigh);
*/
Serial.println("EC Calibration Result: ");
Serial.print(ec_calib_rangeLow_Rlow);
Serial.print(", "); Serial.print(ec_calib_rangeHigh_Rlow);
Serial.print(", "); Serial.print(ec_calib_rangeLow_Rhigh);
Serial.print(", "); Serial.println(ec_calib_rangeHigh_Rhigh);
}
break;
case MEASURE: case MEASURE:
if (ec_measurementReady()) { if (ec_measurementReady()) {
ec_releaseRelay(); ec_releaseRelay();
float adc_rangelow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE); float adc_rangelow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
float adc_rangehigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE); float adc_rangehigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);
Serial.println(); Serial.print("EC ADC: Low="); Serial.print(adc_rangelow); Serial.print(" High="); Serial.println(adc_rangehigh);
float resistance_rangelow=ec_getResistance(adc_rangelow,ec_calib_rangeLow_Rlow,ec_calibresistor_low,ec_calib_rangeLow_Rhigh,ec_calibresistor_high);
Serial.print("Range Low: ADC="); Serial.print(adc_rangelow); Serial.print(", resistance="); Serial.println(resistance_rangelow);
Serial.println();
float resistance_rangehigh=ec_getResistance(adc_rangehigh,ec_calib_rangeHigh_Rlow,ec_calibresistor_low,ec_calib_rangeHigh_Rhigh,ec_calibresistor_high);
Serial.print("Range High: ADC="); Serial.print(adc_rangehigh); Serial.print(", resistance="); Serial.println(resistance_rangehigh);
ecstate=IDLE; ecstate=IDLE;
} }
@ -210,14 +128,6 @@ void ec_loop(unsigned long loopmillis) {
void ec_startCalibration() {
//Switch to Low calibration
ec_setCalibration(LOW);
ec_setRange(LOW);
ec_startMeasurement();
Serial.println("EC Started Calibration");
}
void ec_startMeasurement() { void ec_startMeasurement() {
ec_array_pos=0; ec_array_pos=0;
} }
@ -247,66 +157,11 @@ void ec_connectProbe(bool relay) {
} }
} }
void ec_setCalibration(bool calib) {
//calib low means low resistor value -> NO -> relay high
ec_connectProbe(false);
bool val=digitalRead(EC_PIN_RELAY_CALIBRATION);
if (val!=!calib) { //write only if different
digitalWrite(EC_PIN_RELAY_CALIBRATION,!calib);
ec_last_change_relay=millis();
}
}
void ec_releaseRelay() { void ec_releaseRelay() {
digitalWrite(EC_PIN_RELAY_PROBE,LOW); digitalWrite(EC_PIN_RELAY_PROBE,LOW);
digitalWrite(EC_PIN_RELAY_CALIBRATION,LOW);
digitalWrite(EC_PIN_RELAY_RANGE,LOW); digitalWrite(EC_PIN_RELAY_RANGE,LOW);
ec_last_change_relay=millis(); ec_last_change_relay=millis();
} }
void ec_checkIfSettleTimeOK() {
/*
Serial.print("ec_array_rangeLow[0]="); Serial.println(ec_array_rangeLow[0]);
Serial.print("rangeLow min="); Serial.println(getMin(ec_array_rangeLow,EC_ARRAY_SIZE));
Serial.print("rangeLow max="); Serial.println(getMax(ec_array_rangeLow,EC_ARRAY_SIZE));
*/
if (ec_array_rangeLow[0]<=getMin(ec_array_rangeLow,EC_ARRAY_SIZE) || ec_array_rangeLow[0]>=getMax(ec_array_rangeLow,EC_ARRAY_SIZE)){
//is first value the highest or lowest?
Serial.println("Warning: EC_RELAY_SWITCH_SETTLETIME might be too low! (ec_calib_rangeLow_Rlow)");
}
/*
Serial.print("ec_array_rangeHigh[0]="); Serial.println(ec_array_rangeHigh[0]);
Serial.print("rangeHigh min="); Serial.println(getMin(ec_array_rangeHigh,EC_ARRAY_SIZE));
Serial.print("rangeHigh max="); Serial.println(getMax(ec_array_rangeHigh,EC_ARRAY_SIZE));
*/
if (ec_array_rangeHigh[0]<=getMin(ec_array_rangeHigh,EC_ARRAY_SIZE) || ec_array_rangeHigh[0]>=getMax(ec_array_rangeHigh,EC_ARRAY_SIZE)){
//is first value the highest or lowest?
Serial.println("Warning: EC_RELAY_SWITCH_SETTLETIME might be too low! (ec_array_rangeHigh)");
}
}
float ec_getResistance(float adc,float caliblow,float resistorlow,float calibhigh,float resistorhigh)
{
//adc = adc reading to calculate resistance for
//caliblow = adc value from calibration. Low resistance
//resistorlow = actual resistor value. Low resistance
//calibhjgh = adc value from calibration. High resistance
//resistorhigh = actual resistor value. High resistance
//y=mx+a;
//resistorlow=m*caliblow+a;
//resistorhigh=m*calibhigh+a;
//linear interpolation interpolate
double m=(resistorhigh-resistorlow)/(calibhigh-caliblow);
float a=resistorlow-m*caliblow;
Serial.print("m="); Serial.println(m);
Serial.print("a="); Serial.println(a);
return m*adc+a;
}
#endif #endif