add polynomial/linear ec calculation
This commit is contained in:
parent
833a7fb578
commit
490d890368
32
include/ec.h
32
include/ec.h
|
@ -42,14 +42,24 @@ ECState ecstate=IDLE;
|
||||||
float ec_adc;
|
float ec_adc;
|
||||||
float ec_adc_adjusted; //adjusted for reference resistor
|
float ec_adc_adjusted; //adjusted for reference resistor
|
||||||
float ec_calib_adc;
|
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
|
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() {
|
||||||
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
|
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
|
||||||
|
@ -85,6 +95,7 @@ void ec_loop(unsigned long loopmillis) {
|
||||||
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,0)){
|
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,0)){
|
||||||
ec_calib_adc=getMean(ec_calib_array,EC_CALIB_ARRAY_SIZE);
|
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_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;
|
||||||
|
@ -156,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
|
17
src/main.cpp
17
src/main.cpp
|
@ -61,7 +61,17 @@ void setup() {
|
||||||
//Serial.println("Setup finished");
|
//Serial.println("Setup finished");
|
||||||
delay(200);
|
delay(200);
|
||||||
|
|
||||||
Serial.println("time,tempReservoir,EC,ECadjusted,ECcalib");
|
//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,10 +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_calib_adc); Serial.print(",");
|
||||||
Serial.print(ec_adc); Serial.print(",");
|
Serial.print(ec_adc); Serial.print(",");
|
||||||
Serial.print(ec_adc_adjusted); Serial.print(",");
|
Serial.print(ec_adc_adjusted); Serial.print(",");
|
||||||
|
Serial.print(ec); Serial.print(",");
|
||||||
|
Serial.print(ec25);
|
||||||
|
|
||||||
|
|
||||||
Serial.print(ec_calib_adc);
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue