2023-04-17 19:32:34 +00:00
|
|
|
#ifndef _EC_H_
|
|
|
|
#define _EC_H_
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
|
|
|
|
bool ec_flag_measurement_available=false;
|
2023-04-20 21:16:04 +00:00
|
|
|
|
2023-04-17 19:33:05 +00:00
|
|
|
#define EC_PIN_RELAY_PROBE 27
|
|
|
|
|
2023-04-17 19:32:34 +00:00
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
//#define EC_PIN_ADC 4
|
|
|
|
#define EC_ADS_CHANNEL 0
|
2023-04-17 19:32:34 +00:00
|
|
|
#define EC_PIN_FREQ 5
|
|
|
|
#define EC_PWM_CH 0
|
|
|
|
#define EC_RESOLUTION 8
|
|
|
|
#define EC_FREQUENCY 5000
|
|
|
|
|
2023-05-05 22:08:39 +00:00
|
|
|
#define EC_CALIB_ARRAY_SIZE 256
|
2023-04-30 17:00:12 +00:00
|
|
|
uint16_t ec_calib_array[EC_CALIB_ARRAY_SIZE];
|
|
|
|
uint16_t ec_calib_array_pos=0;
|
2023-05-05 22:08:39 +00:00
|
|
|
#define EC_CALIB_READ_INTERVAL 250 //interval of reading adc value inside a measurement
|
2023-04-30 17:00:12 +00:00
|
|
|
|
2023-05-05 22:08:39 +00:00
|
|
|
#define EC_ARRAY_SIZE 256
|
2023-04-28 13:07:47 +00:00
|
|
|
uint16_t ec_array[EC_ARRAY_SIZE];
|
|
|
|
uint16_t ec_array_pos=EC_ARRAY_SIZE;
|
2023-04-22 01:14:51 +00:00
|
|
|
unsigned long last_measurement_ec=0;
|
2023-05-05 22:08:39 +00:00
|
|
|
#define EC_MEASUREMENT_INTERVAL 30000 //complete filtered measurement every x ms
|
2023-04-20 21:16:04 +00:00
|
|
|
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
|
2023-05-05 22:08:39 +00:00
|
|
|
#define EC_READ_INTERVAL 10 //interval of reading adc value inside a measurement. one reading takes about 9-10ms
|
2023-04-20 19:52:22 +00:00
|
|
|
|
|
|
|
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
|
|
|
|
|
2023-05-02 23:47:42 +00:00
|
|
|
//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
|
2023-04-20 21:16:04 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
unsigned long ec_last_change_relay=0; //millis of last relay change
|
|
|
|
|
2023-04-20 19:53:41 +00:00
|
|
|
enum ECState{IDLE,MEASURE};
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-20 19:53:41 +00:00
|
|
|
ECState ecstate=IDLE;
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-28 13:07:47 +00:00
|
|
|
float ec_adc;
|
2023-04-30 17:00:12 +00:00
|
|
|
float ec_adc_adjusted; //adjusted for reference resistor
|
|
|
|
float ec_calib_adc;
|
2023-04-30 18:20:09 +00:00
|
|
|
float ec; //ec value after adjustment for reference (at current temperature)
|
|
|
|
float ec25; //ec value but temperature adjusted for 25 degC
|
2023-04-30 17:00:12 +00:00
|
|
|
|
2023-04-30 18:20:09 +00:00
|
|
|
float ec_tempadjust_alpa=0.2; //TODO
|
2023-05-05 22:08:39 +00:00
|
|
|
float ec_reference_adc=6016.88; //adc reference value for the calibration resistor measurement.
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-30 18:20:09 +00:00
|
|
|
//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
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
bool ec_measurementReady();
|
|
|
|
void ec_startMeasurement();
|
2023-04-20 21:16:04 +00:00
|
|
|
void ec_setRange(uint8_t range);
|
2023-04-20 19:52:22 +00:00
|
|
|
void ec_connectProbe(bool);
|
|
|
|
void ec_releaseRelay();
|
2023-04-30 18:20:09 +00:00
|
|
|
float ec_getECfromADC(float adc);
|
2023-04-17 19:32:34 +00:00
|
|
|
|
|
|
|
void ec_setup() {
|
|
|
|
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
|
|
|
|
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
|
2023-04-20 19:52:22 +00:00
|
|
|
ledcWrite(EC_PWM_CH, 127); //50% duty cycle
|
2023-04-17 19:32:34 +00:00
|
|
|
|
2023-04-17 19:33:05 +00:00
|
|
|
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected
|
2023-04-20 19:52:22 +00:00
|
|
|
ec_releaseRelay();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_loop(unsigned long loopmillis) {
|
2023-04-22 01:14:51 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
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);
|
2023-05-02 23:47:42 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
ecstate=MEASURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEASURE:
|
|
|
|
if (ec_measurementReady()) {
|
|
|
|
ec_releaseRelay();
|
2023-04-28 13:07:47 +00:00
|
|
|
ec_adc=getMean(ec_array,EC_ARRAY_SIZE);
|
2023-04-30 17:00:12 +00:00
|
|
|
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);
|
2023-04-30 18:20:09 +00:00
|
|
|
ec=ec_getECfromADC(ec_adc_adjusted);
|
2023-04-30 17:00:12 +00:00
|
|
|
}
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
ec_flag_measurement_available=true;
|
2023-04-20 21:16:04 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
ecstate=IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-30 17:00:12 +00:00
|
|
|
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;
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-30 17:00:12 +00:00
|
|
|
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;
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-30 17:00:12 +00:00
|
|
|
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;
|
2023-04-22 01:14:51 +00:00
|
|
|
|
2023-04-30 17:00:12 +00:00
|
|
|
|
|
|
|
}
|
2023-04-20 19:52:22 +00:00
|
|
|
}
|
2023-04-30 17:00:12 +00:00
|
|
|
}
|
2023-04-20 19:52:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ec_startMeasurement() {
|
|
|
|
ec_array_pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ec_measurementReady(){
|
2023-04-28 13:07:47 +00:00
|
|
|
if (ec_array_pos>=EC_ARRAY_SIZE) { //reached end of both arrays
|
2023-04-20 19:52:22 +00:00
|
|
|
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() {
|
2023-04-17 19:33:05 +00:00
|
|
|
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
|
2023-04-20 19:52:22 +00:00
|
|
|
ec_last_change_relay=millis();
|
2023-04-17 19:32:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 18:20:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-17 19:32:34 +00:00
|
|
|
#endif
|