212 lines
6.4 KiB
C
212 lines
6.4 KiB
C
#ifndef _EC_H_
|
|
#define _EC_H_
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
bool ec_flag_measurement_available=false;
|
|
|
|
#define EC_ADC_UNAVAILABLE 0
|
|
#define EC_UNAVAILABLE -1
|
|
|
|
|
|
#define EC_PIN_RELAY_PROBE 27
|
|
|
|
|
|
//#define EC_PIN_ADC 4
|
|
#define EC_ADS_CHANNEL 0
|
|
#define EC_PIN_FREQ 5
|
|
#define EC_PWM_CH 0
|
|
#define EC_RESOLUTION 8
|
|
#define EC_FREQUENCY 5000
|
|
|
|
#define EC_CALIB_ARRAY_SIZE 128
|
|
uint16_t ec_calib_array[EC_CALIB_ARRAY_SIZE];
|
|
uint16_t ec_calib_array_pos=0;
|
|
#define EC_CALIB_READ_INTERVAL 250 //interval of reading adc value inside a measurement
|
|
|
|
#define EC_ARRAY_SIZE 256
|
|
uint16_t ec_array[EC_ARRAY_SIZE];
|
|
uint16_t ec_array_pos=EC_ARRAY_SIZE;
|
|
unsigned long last_measurement_ec=0;
|
|
#define EC_MEASUREMENT_INTERVAL 60000 //complete filtered measurement every x ms
|
|
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
|
|
#define EC_READ_INTERVAL 10 //interval of reading adc value inside a measurement. one reading takes about 9-10ms
|
|
|
|
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
|
|
|
|
//const uint16_t ec_centerADCvalue=9026; //adc value when probe resistance is equal to the range resistor (mean of both)
|
|
//Range Resistor is two parallel 1k2 = 600 Ohm
|
|
|
|
unsigned long ec_last_change_relay=0; //millis of last relay change
|
|
|
|
enum ECState{IDLE,MEASURE};
|
|
|
|
ECState ecstate=IDLE;
|
|
|
|
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.02;
|
|
float ec_reference_adc=6016.88; //adc reference value for the calibration resistor measurement.
|
|
//EC short circuit adc value: 17497, 17861.4 (for connection resistance testing)
|
|
//EC open circuit adc value: 738, 730, 737.27
|
|
|
|
//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_polynom[]={1033.928052655456,-3.8909104921922895,0.005627541436014758,-4.103988840997024e-06,1.7231981870816133e-09,-4.433707707721975e-13,7.203892111369395e-17,-7.406549810844244e-21,4.667420606439905e-25,-1.6439457516812463e-29,2.477292190335455e-34};
|
|
float ec_calibration_polynom[]={212.6826331524675,-0.6043878865263305,0.000571551634082491,-1.827897106718841e-07,2.682337041246909e-11,-1.8368511021965982e-15,4.8269168538877025e-20};
|
|
float ec_calibration_linearize_below_adc=6500; //use linear approximation below this adc value. 0=disable
|
|
float ec_calibration_linear_lowADC=750; //x0
|
|
float ec_calibration_linear_lowEC=0; //y0
|
|
|
|
bool ec_measurementReady();
|
|
void ec_startMeasurement();
|
|
void ec_setRange(uint8_t range);
|
|
void ec_connectProbe(bool);
|
|
void ec_releaseRelay();
|
|
float ec_getECfromADC(float adc);
|
|
float ec_calculateEC25(float pEC,float pTemp);
|
|
|
|
void ec_setup() {
|
|
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
|
|
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
|
|
ledcWrite(EC_PWM_CH, 127); //50% duty cycle
|
|
|
|
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected
|
|
ec_releaseRelay();
|
|
|
|
}
|
|
|
|
void ec_loop(unsigned long loopmillis) {
|
|
|
|
static unsigned long last_read_ec=0;
|
|
|
|
switch (ecstate) {
|
|
case IDLE:
|
|
|
|
if (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL && ecstate==IDLE) { //start measurement if idle
|
|
last_measurement_ec=loopmillis;
|
|
ec_startMeasurement();
|
|
ec_connectProbe(true);
|
|
|
|
ecstate=MEASURE;
|
|
}
|
|
break;
|
|
|
|
case MEASURE:
|
|
if (ec_measurementReady()) {
|
|
ec_releaseRelay();
|
|
ec_adc=getMean(ec_array,EC_ARRAY_SIZE);
|
|
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)){
|
|
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);
|
|
ec25=ec_calculateEC25(ec,tempC_reservoir);
|
|
}else{
|
|
ec_calib_adc=EC_ADC_UNAVAILABLE;
|
|
ec_adc_adjusted=EC_ADC_UNAVAILABLE;
|
|
ec=EC_UNAVAILABLE;
|
|
ec25=EC_UNAVAILABLE;
|
|
}
|
|
|
|
ec_flag_measurement_available=true;
|
|
|
|
ecstate=IDLE;
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
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
|
|
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;
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_startMeasurement() {
|
|
ec_array_pos=0;
|
|
}
|
|
|
|
bool ec_measurementReady(){
|
|
if (ec_array_pos>=EC_ARRAY_SIZE) { //reached end of both arrays
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
void ec_connectProbe(bool relay) {
|
|
bool val=digitalRead(EC_PIN_RELAY_PROBE);
|
|
if (val!=relay) { //write only if different
|
|
digitalWrite(EC_PIN_RELAY_PROBE,relay);
|
|
ec_last_change_relay=millis();
|
|
}
|
|
}
|
|
|
|
void ec_releaseRelay() {
|
|
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
|
|
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;
|
|
}
|
|
|
|
float ec_calculateEC25(float pEC,float pTemp)
|
|
{
|
|
return pEC/(1.0+ec_tempadjust_alpa*(pTemp-25.0));
|
|
}
|
|
|
|
|
|
#endif |