hydroponic-controller/include/ec.h

211 lines
6.2 KiB
C
Raw Normal View History

2023-04-17 19:32:34 +00:00
#ifndef _EC_H_
#define _EC_H_
#include <Arduino.h>
bool ec_flag_measurement_available=false;
2023-04-20 21:16:04 +00:00
#define EC_ADC_UNAVAILABLE 0
#define EC_UNAVAILABLE -1
2023-04-17 19:33:05 +00:00
#define EC_PIN_RELAY_PROBE 27
2023-04-17 19:32:34 +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
#define EC_CALIB_ARRAY_SIZE 128
2023-04-30 17:00:12 +00:00
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
2023-04-30 17:00:12 +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;
unsigned long last_measurement_ec=0;
#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
#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
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
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:53:41 +00:00
ECState ecstate=IDLE;
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
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 (for connection restistance testing)
//EC open circuit adc value: 738
2023-04-30 18:20:09 +00:00
//x^0*p[0] + ... + x^n*p[n]
2023-05-05 22:53:32 +00:00
//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_linearize_below_adc=0; //use linear approximation below this adc value. 0=disable
2023-04-30 18:20:09 +00:00
float ec_calibration_linear_lowADC=830; //x0
float ec_calibration_linear_lowEC=0; //y0
bool ec_measurementReady();
void ec_startMeasurement();
2023-04-20 21:16:04 +00:00
void ec_setRange(uint8_t range);
void ec_connectProbe(bool);
void ec_releaseRelay();
2023-04-30 18:20:09 +00:00
float ec_getECfromADC(float adc);
float ec_calculateEC25(float pEC,float pTemp);
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);
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
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);
2023-05-02 23:47:42 +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);
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)){
2023-04-30 17:00:12 +00:00
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);
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;
2023-04-30 17:00:12 +00:00
}
ec_flag_measurement_available=true;
2023-04-20 21:16:04 +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-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-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-30 17:00:12 +00:00
}
}
2023-04-30 17:00:12 +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
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);
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;
}
float ec_calculateEC25(float pEC,float pTemp)
{
return pEC/(1.0+ec_tempadjust_alpa*(pTemp-25.0));
}
2023-04-17 19:32:34 +00:00
#endif