Compare commits

..

2 Commits

Author SHA1 Message Date
interfisch 490d890368 add polynomial/linear ec calculation 2023-04-30 20:20:09 +02:00
interfisch 833a7fb578 add reference adc measurement 2023-04-30 19:00:12 +02:00
3 changed files with 96 additions and 29 deletions

View File

@ -3,7 +3,6 @@
#include <Arduino.h> #include <Arduino.h>
float ecEC_ADS_CHANNEL_mean=0;
bool ec_flag_measurement_available=false; bool ec_flag_measurement_available=false;
@ -17,13 +16,18 @@ bool ec_flag_measurement_available=false;
#define EC_RESOLUTION 8 #define EC_RESOLUTION 8
#define EC_FREQUENCY 5000 #define EC_FREQUENCY 5000
#define EC_CALIB_ARRAY_SIZE 64
uint16_t ec_calib_array[EC_CALIB_ARRAY_SIZE];
uint16_t ec_calib_array_pos=0;
#define EC_CALIB_READ_INTERVAL 100 //interval of reading adc value inside a measurement
#define EC_ARRAY_SIZE 64 #define EC_ARRAY_SIZE 64
uint16_t ec_array[EC_ARRAY_SIZE]; uint16_t ec_array[EC_ARRAY_SIZE];
uint16_t ec_array_pos=EC_ARRAY_SIZE; uint16_t ec_array_pos=EC_ARRAY_SIZE;
unsigned long last_measurement_ec=0; unsigned long last_measurement_ec=0;
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms #define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4 //One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
#define EC_READ_INTERVAL 2 //interval of reading adc value inside a measurement #define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement
#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
@ -36,16 +40,28 @@ enum ECState{IDLE,MEASURE};
ECState ecstate=IDLE; ECState ecstate=IDLE;
float ec_adc; float ec_adc;
float ec_adc_adjusted; //adjusted for reference resistor
float ec_calib_adc;
float ec; //ec value after adjustment for reference (at current temperature)
float ec25; //ec value but temperature adjusted for 25 degC
float ec_tempadjust_alpa=0.2; //TODO
float ec_reference_adc=5889; //adc reference value for the calibration resistor measurement
//x^0*p[0] + ... + x^n*p[n]
float ec_calibration_polynom[]={691.5992624638029,-1.4015367296761692,0.0008513503472324141,-2.2140576823179093e-07,2.8962580780180067e-11,-1.8577565383307114e-15,4.7162479484903865e-20};
float ec_calibration_linearize_below_adc=6000; //use linear approximation below this adc value
float ec_calibration_linear_lowADC=830; //x0
float ec_calibration_linear_lowEC=0; //y0
bool ec_measurementReady(); bool ec_measurementReady();
void ec_startMeasurement(); void ec_startMeasurement();
void ec_setRange(uint8_t range); void ec_setRange(uint8_t range);
void ec_connectProbe(bool); void ec_connectProbe(bool);
void ec_releaseRelay(); void ec_releaseRelay();
float ec_getECfromADC(float adc);
void ec_setup() { void ec_setup() {
//pinMode(EC_PIN_ADC,INPUT);
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION); ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
ledcWrite(EC_PWM_CH, 127); //50% duty cycle ledcWrite(EC_PWM_CH, 127); //50% duty cycle
@ -76,13 +92,14 @@ void ec_loop(unsigned long loopmillis) {
ec_releaseRelay(); ec_releaseRelay();
Serial.println("Finished EC"); Serial.println("Finished EC");
ec_adc=getMean(ec_array,EC_ARRAY_SIZE); ec_adc=getMean(ec_array,EC_ARRAY_SIZE);
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,0)){
ec_calib_adc=getMean(ec_calib_array,EC_CALIB_ARRAY_SIZE);
ec_adc_adjusted=mapf(ec_adc,0,ec_calib_adc,0,ec_reference_adc);
ec=ec_getECfromADC(ec_adc_adjusted);
}
ec_flag_measurement_available=true; ec_flag_measurement_available=true;
ecstate=IDLE; ecstate=IDLE;
} }
@ -91,27 +108,34 @@ void ec_loop(unsigned long loopmillis) {
} }
if (ec_array_pos<EC_ARRAY_SIZE) { //measurement running
if (loopmillis>last_read_ec+EC_READ_INTERVAL) { //take reading into array
last_read_ec=loopmillis;
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos<EC_ARRAY_SIZE) { //take reading into array if measurement running if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
last_read_ec=loopmillis; uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
//flag_print= ec_array_pos==EC_ARRAY_SIZE; ec_array[ec_array_pos]=value;
//ec_array_pos%=EC_ARRAY_SIZE;
ec_array_pos++;
}
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
//uint16_t value=analogRead(EC_PIN_ADC);
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
ec_array[ec_array_pos]=value;
ec_array_pos++;
} }
}else{ //measurement not running, then take calibration readings
} if (loopmillis>last_read_ec+EC_CALIB_READ_INTERVAL) { //take reading into array
last_read_ec=loopmillis;
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
ec_calib_array[ec_calib_array_pos]=value;
ec_calib_array_pos++;
ec_calib_array_pos%=EC_CALIB_ARRAY_SIZE;
}
}
}
} }
@ -143,5 +167,26 @@ void ec_releaseRelay() {
ec_last_change_relay=millis(); ec_last_change_relay=millis();
} }
float ec_getECfromADC(float adc) {
uint8_t polynom_order=sizeof(ec_calibration_polynom) / sizeof(ec_calibration_polynom[0]);
double _ec=0;
if (adc>=ec_calibration_linearize_below_adc) { //adc is in range where polynomial approximation fits well
for (uint8_t i=0;i<polynom_order;i++) {
_ec+=pow(adc,i)*ec_calibration_polynom[i];
}
}else{ //low ec region. linear approximation works better here
float x1=ec_calibration_linearize_below_adc;
float y1=0;
for (uint8_t i=0;i<polynom_order;i++) { //get y1 value from curve
y1+=pow(x1,i)*ec_calibration_polynom[i];
}
float x0=ec_calibration_linear_lowADC;
float y0=ec_calibration_linear_lowEC;
_ec=mapf(adc,x0,x1,y0,y1); //linear approximation
}
return _ec;
}
#endif #endif

View File

@ -13,6 +13,7 @@ float getMinf(float *parray, uint16_t psize);
bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck); bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck);
bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck); bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck);
float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff); float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff);
float mapf(float x, float in_min, float in_max, float out_min, float out_max);
float getMean(uint16_t *parray,uint16_t psize) { float getMean(uint16_t *parray,uint16_t psize) {
@ -113,5 +114,10 @@ float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff) {
return mean/(psize-2*pcutOff); return mean/(psize-2*pcutOff);
} }
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#endif #endif

View File

@ -61,7 +61,17 @@ void setup() {
//Serial.println("Setup finished"); //Serial.println("Setup finished");
delay(200); delay(200);
Serial.println("time,tempReservoir,EC"); //Test adc to ec function output
Serial.println();
for (int i=830;i<13300;i+=100) {
float _ec=ec_getECfromADC(i);
Serial.print(i); Serial.print(","); Serial.print(_ec); Serial.println();
}
delay(100000);
Serial.println("time,tempReservoir,ECadcCalib,ECadc,ECadcAdjusted,EC,EC25");
} }
@ -108,7 +118,13 @@ void loop() {
Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(","); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(",");
//Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(","); //Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(",");
Serial.print(ec_adc); Serial.print(ec_calib_adc); Serial.print(",");
Serial.print(ec_adc); Serial.print(",");
Serial.print(ec_adc_adjusted); Serial.print(",");
Serial.print(ec); Serial.print(",");
Serial.print(ec25);
Serial.println(); Serial.println();