hydroponic-controller/include/ec.h

329 lines
11 KiB
C
Raw Normal View History

2023-04-17 19:32:34 +00:00
#ifndef _EC_H_
#define _EC_H_
#include <Arduino.h>
2024-04-23 21:39:18 +00:00
/*
mqttValueTiming timing_ec_adc;
mqttValueTiming timing_ec_calibadc;
mqttValueTiming timing_ec_adcadjusted;
mqttValueTiming timing_ec_ec;
mqttValueTiming timing_ec_sc;
2024-04-23 21:39:18 +00:00
*/
2023-04-17 19:32:34 +00:00
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
2024-04-23 21:39:18 +00:00
#define EC_PIN_RELAY_PROBESELECT 26
2023-04-17 19:33:05 +00:00
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
2024-04-23 21:39:18 +00:00
//#define EC_CALIB_ARRAY_SIZE 128
#define EC_CALIB_ARRAY_SIZE 32 //temporarily changed
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 10*60*1000 //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};
2024-04-23 21:39:18 +00:00
uint8_t probeselect=0;
const uint8_t num_probes=2;
2023-04-20 19:53:41 +00:00
ECState ecstate=IDLE;
2023-04-30 17:00:12 +00:00
float ec_calib_adc;
2024-04-23 21:39:18 +00:00
float ec_adc_A;
float ec_adc_adjusted_A; //adjusted for reference resistor
float ec_A; //ec value after adjustment for reference (at current temperature)
float ec25_A; //ec value but temperature adjusted for 25 degC
float ec_adc_B;
float ec_adc_adjusted_B; //adjusted for reference resistor
float ec_B; //ec value after adjustment for reference (at current temperature)
float ec25_B; //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.
2023-05-09 21:20:47 +00:00
//EC short circuit adc value: 17497, 17861.4 (for connection resistance testing)
//EC open circuit adc value: 738, 730, 737.27
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};
2023-05-09 22:13:29 +00:00
//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}; //20220505
2023-06-20 20:30:29 +00:00
//float ec_calibration_polynom[]={-323.68589929771457,0.5836096440900665,-0.000279737392438965,5.98673062873e-08,-5.4460235093798435e-12,1.8535134644431135e-16}; //20230509
2023-05-09 22:13:29 +00:00
//float ec_calibration_polynom[]={212.6826331524675,-0.6043878865263305,0.000571551634082491,-1.827897106718841e-07,2.682337041246909e-11,-1.8368511021965982e-15,4.8269168538877025e-20}; //20230509 manuell
2024-04-23 21:39:18 +00:00
float ec_calibration_polynom_A[]={18.953002079376887,-0.057529180570340156,7.44612274805891e-05,-8.847448650204697e-09,5.388914121653647e-13}; //20240423, graphite electrodes
float ec_calibration_linearize_below_adc_A=2000; //use linear approximation below this adc value. 0=disable
float ec_calibration_linear_lowADC_A=728; //x0
float ec_calibration_linear_lowEC_A=0; //y0
float ec_calibration_polynom_B[]={40.838922264426685,-0.08846039894456156,7.671298605466989e-05,-9.161423581523883e-09,5.529701600660142e-13}; //20240423, graphite electrodes
float ec_calibration_linearize_below_adc_B=2000; //use linear approximation below this adc value. 0=disable
float ec_calibration_linear_lowADC_B=728; //x0
float ec_calibration_linear_lowEC_B=0; //y0
2023-04-30 18:20:09 +00:00
bool ec_measurementReady();
void ec_startMeasurement();
2023-04-20 21:16:04 +00:00
void ec_setRange(uint8_t range);
2024-04-23 21:39:18 +00:00
void ec_connectProbe(bool, uint8_t);
void ec_releaseRelay();
2024-04-23 21:39:18 +00:00
float ec_getECfromADC(float adc, float ec_calibration_polynom[], size_t len_ec_calibration_polynom, float ec_calibration_linearize_below_adc, float ec_calibration_linear_lowADC, float ec_calibration_linear_lowEC);
float ec_calculateEC25(float pEC,float pTemp);
2023-04-17 19:32:34 +00:00
void ec_setup() {
2024-04-23 21:39:18 +00:00
/*
timing_ec_adc.minchange=0.0;
timing_ec_adc.maxchange=250;
timing_ec_adc.mintime=10*000;
timing_ec_adc.maxtime=60*60*1000;
timing_ec_calibadc.minchange=0.0;
timing_ec_calibadc.maxchange=250;
timing_ec_calibadc.mintime=10*000;
timing_ec_calibadc.maxtime=60*60*1000;
timing_ec_adcadjusted.minchange=0.0;
timing_ec_adcadjusted.maxchange=2.0;
timing_ec_adcadjusted.mintime=10*000;
timing_ec_adcadjusted.maxtime=30*60*1000;
timing_ec_ec.minchange=0.0;
timing_ec_ec.maxchange=50;
timing_ec_ec.mintime=10*000;
timing_ec_ec.maxtime=60*60*1000;
timing_ec_sc.minchange=0.0;
timing_ec_sc.maxchange=50;
timing_ec_sc.mintime=10*000;
timing_ec_sc.maxtime=60*60*1000;
2024-04-23 21:39:18 +00:00
*/
2023-04-17 19:32:34 +00:00
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
2024-04-23 21:39:18 +00:00
pinMode(EC_PIN_RELAY_PROBESELECT,OUTPUT); //LOW=Probe A, HIGH=Probe B
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 || force_ec_measurement) { //start measurement if idle
2024-04-23 21:39:18 +00:00
//Serial.println("DEBUG: Start measurement");
last_measurement_ec=loopmillis;
force_ec_measurement=false;
ec_startMeasurement();
2024-04-23 21:39:18 +00:00
ec_connectProbe(true,0); //Probe A
2023-05-02 23:47:42 +00:00
ecstate=MEASURE;
}
break;
case MEASURE:
if (ec_measurementReady()) {
2024-04-23 21:39:18 +00:00
//Serial.println("DEBUG: Measurement Ready");
float ec_adc;
float ec_adc_adjusted;
float ec;
float ec25;
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);
2024-04-23 21:39:18 +00:00
if (probeselect==0) {
ec=ec_getECfromADC(ec_adc_adjusted, ec_calibration_polynom_A, sizeof(ec_calibration_polynom_A), ec_calibration_linearize_below_adc_A, ec_calibration_linear_lowADC_A, ec_calibration_linear_lowEC_A);
}else if (probeselect==1) {
ec=ec_getECfromADC(ec_adc_adjusted, ec_calibration_polynom_B, sizeof(ec_calibration_polynom_B), ec_calibration_linearize_below_adc_B, ec_calibration_linear_lowADC_B, ec_calibration_linear_lowEC_B);
}
ec25=ec_calculateEC25(ec,tempC_reservoir);
2024-04-23 21:39:18 +00:00
//Serial.println("DEBUG: EC OK");
}else{
ec_calib_adc=EC_ADC_UNAVAILABLE;
ec_adc_adjusted=EC_ADC_UNAVAILABLE;
ec=EC_UNAVAILABLE;
ec25=EC_UNAVAILABLE;
2024-04-23 21:39:18 +00:00
//Serial.println("DEBUG: EC unavailable");
2023-04-30 17:00:12 +00:00
}
2024-04-23 21:39:18 +00:00
if (probeselect==0) {
//Serial.println("DEBUG: Assigning to A");
ec_adc_A=ec_adc;
ec_adc_adjusted_A=ec_adc_adjusted;
ec_A=ec;
ec25_A=ec25;
last_measurement_ec=loopmillis;
force_ec_measurement=false;
ec_startMeasurement();
probeselect=1; //Select Probe B
ec_connectProbe(true,probeselect); //Probe B
ecstate=MEASURE;
}else if(probeselect==1) {
//Serial.println("DEBUG: Assigning to B");
ec_adc_B=ec_adc;
ec_adc_adjusted_B=ec_adc_adjusted;
ec_B=ec;
ec25_B=ec25;
probeselect=0; //Reset to Probe A
ec_flag_measurement_available=true;
ecstate=IDLE;
}
2023-04-20 21:16:04 +00:00
}
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-06-20 20:30:29 +00:00
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
2023-04-30 17:00:12 +00:00
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
ec_array[ec_array_pos]=value;
ec_array_pos++;
}
2023-04-30 17:00:12 +00:00
}
}else{ //measurement not running, then take calibration readings
2023-06-20 20:30:29 +00:00
if (loopmillis>last_read_ec+EC_CALIB_READ_INTERVAL) { //take reading into arraysdf
2023-04-30 17:00:12 +00:00
last_read_ec=loopmillis;
2023-06-20 20:30:29 +00:00
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
2023-04-30 17:00:12 +00:00
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;
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)){
ec_calib_adc=getMean(ec_calib_array,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;
}
}
2024-04-23 21:39:18 +00:00
void ec_connectProbe(bool relay, uint8_t probeselect) {
bool val=digitalRead(EC_PIN_RELAY_PROBE);
2024-04-23 21:39:18 +00:00
bool valsel=digitalRead(EC_PIN_RELAY_PROBESELECT);
if (val!=relay) { //write only if different
digitalWrite(EC_PIN_RELAY_PROBE,relay);
2024-04-23 21:39:18 +00:00
//Serial.print("DEBUG: Set Relay to "); Serial.println(relay);
ec_last_change_relay=millis();
}
if (valsel!=probeselect) { //write only if different
if (probeselect==0) {
digitalWrite(EC_PIN_RELAY_PROBESELECT,false);
//Serial.println("DEBUG: Selected Probe A");
}else if (probeselect==1) {
digitalWrite(EC_PIN_RELAY_PROBESELECT,true);
//Serial.println("DEBUG: Selected Probe B");
}
ec_last_change_relay=millis();
}
}
void ec_releaseRelay() {
2023-04-17 19:33:05 +00:00
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
2024-04-23 21:39:18 +00:00
digitalWrite(EC_PIN_RELAY_PROBESELECT,LOW);
//Serial.println("DEBUG: Released Relays");
ec_last_change_relay=millis();
2023-04-17 19:32:34 +00:00
}
2024-04-23 21:39:18 +00:00
float ec_getECfromADC(float adc, float ec_calibration_polynom[], size_t len_ec_calibration_polynom, float ec_calibration_linearize_below_adc, float ec_calibration_linear_lowADC, float ec_calibration_linear_lowEC) {
//uint8_t polynom_order=sizeof(ec_calibration_polynom) / sizeof(ec_calibration_polynom[0]);
uint8_t polynom_order=len_ec_calibration_polynom / sizeof(ec_calibration_polynom[0]);
2023-04-30 18:20:09 +00:00
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
}
if (_ec>=0) {
return _ec;
}else{
return 0;
}
2023-04-30 18:20:09 +00:00
}
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