Compare commits
No commits in common. "490d890368e3be21435f2007abf7f12ee7801e31" and "390aaeaeb71e2a0cb992c2276f02a9376b38fe09" have entirely different histories.
490d890368
...
390aaeaeb7
91
include/ec.h
91
include/ec.h
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
float ecEC_ADS_CHANNEL_mean=0;
|
||||||
|
|
||||||
bool ec_flag_measurement_available=false;
|
bool ec_flag_measurement_available=false;
|
||||||
|
|
||||||
|
@ -16,18 +17,13 @@ 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 5 //interval of reading adc value inside a measurement
|
#define EC_READ_INTERVAL 2 //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
|
||||||
|
|
||||||
|
@ -40,28 +36,16 @@ 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
|
||||||
|
@ -92,14 +76,13 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,33 +91,26 @@ 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>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
|
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos<EC_ARRAY_SIZE) { //take reading into array if measurement running
|
||||||
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
|
last_read_ec=loopmillis;
|
||||||
|
|
||||||
ec_array[ec_array_pos]=value;
|
//flag_print= ec_array_pos==EC_ARRAY_SIZE;
|
||||||
|
//ec_array_pos%=EC_ARRAY_SIZE;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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++;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -167,26 +143,5 @@ 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
|
|
@ -13,7 +13,6 @@ 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) {
|
||||||
|
@ -114,10 +113,5 @@ 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
|
20
src/main.cpp
20
src/main.cpp
|
@ -61,17 +61,7 @@ void setup() {
|
||||||
//Serial.println("Setup finished");
|
//Serial.println("Setup finished");
|
||||||
delay(200);
|
delay(200);
|
||||||
|
|
||||||
//Test adc to ec function output
|
Serial.println("time,tempReservoir,EC");
|
||||||
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");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,13 +108,7 @@ 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(ec_adc); Serial.print(",");
|
|
||||||
Serial.print(ec_adc_adjusted); Serial.print(",");
|
|
||||||
Serial.print(ec); Serial.print(",");
|
|
||||||
Serial.print(ec25);
|
|
||||||
|
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue