hydroponic-controller/include/ec.h

303 lines
10 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
2024-06-25 19:10:12 +00:00
//#define EC_PIN_RELAY_PROBE 27 //moved to platformio.ini
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
2024-06-25 19:10:12 +00:00
//#define EC_PIN_FREQ 5 //move to platformio.ini
2023-04-17 19:32:34 +00:00
#define EC_PWM_CH 0
#define EC_RESOLUTION 8
#define EC_FREQUENCY 5000
2024-04-23 22:13:14 +00:00
#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
2024-05-16 17:11:06 +00:00
#define EC_ARRAY_SIZE 64
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;
2024-04-23 22:13:14 +00:00
#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
2024-05-16 17:11:06 +00:00
#define EC_READ_INTERVAL 50 //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-06-25 19:10:12 +00:00
2024-04-23 21:39:18 +00:00
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
2024-06-25 19:10:12 +00:00
float ec_adc;
float ec_adc_adjusted; //adjusted for reference resistor
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.
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-06-25 19:10:12 +00:00
//float ec_calibration_polynom[]={8.718380956513695,-0.026463423062356713,3.425216464107108e-05,-4.069826379094172e-09,2.478900495960682e-13}; //20240423, graphite electrodes
2024-04-23 21:39:18 +00:00
2024-06-25 19:10:12 +00:00
// 20240423, graphite electrodes DB {8.718380956513695,-0.026463423062356713,3.425216464107108e-05,-4.069826379094172e-09,2.478900495960682e-13}
// 20240423, graphite electrodes NFT {18.785904241636743,-0.04069178351449846,3.528797358514823e-05,-4.214254847500995e-09,2.543662736303669e-13}
float ec_calibration_polynom[]=EC_CALIBRATION_POLYNOM;
2024-04-23 21:39:18 +00:00
2024-06-25 19:10:12 +00:00
float ec_calibration_linearize_below_adc=EC_CALIBRATION_LINEARIZE_BELOW_ADC; //use linear approximation below this adc value. 0=disable
float ec_calibration_linear_lowADC=EC_CALIBRATION_LINEAR_LOWADC; //x0
float ec_calibration_linear_lowEC=EC_CALIBRATION_LINEAR_LOWEC; //y0
/*
2024-05-13 17:42:34 +00:00
float ec_calibration_polynom_B[]={18.785904241636743,-0.04069178351449846,3.528797358514823e-05,-4.214254847500995e-09,2.543662736303669e-13}; //20240423, graphite electrodes
2024-04-23 21:39:18 +00:00
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
2024-06-25 19:10:12 +00:00
*/
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-06-25 19:10:12 +00:00
void ec_connectProbe(bool);
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);
2024-05-11 07:20:37 +00:00
bool ec_measurementRunning();
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
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-06-25 19:10:12 +00:00
ec_connectProbe(true);
2024-04-23 21:39:18 +00:00
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");
2024-06-25 19:41:12 +00:00
ec_releaseRelay();
2023-04-28 13:07:47 +00:00
ec_adc=getMean(ec_array,EC_ARRAY_SIZE);
2024-06-25 19:41:12 +00:00
//Serial.print("ec_adc="); Serial.println(ec_adc);
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);
2024-06-25 19:41:12 +00:00
//Serial.print("ec_calib_adc="); Serial.println(ec_calib_adc);
2023-04-30 17:00:12 +00:00
ec_adc_adjusted=mapf(ec_adc,0,ec_calib_adc,0,ec_reference_adc);
2024-06-25 19:41:12 +00:00
//Serial.print("ec_adc_adjusted="); Serial.println(ec_adc_adjusted);
2024-06-25 19:10:12 +00:00
ec=ec_getECfromADC(ec_adc_adjusted, ec_calibration_polynom, sizeof(ec_calibration_polynom), ec_calibration_linearize_below_adc, ec_calibration_linear_lowADC, ec_calibration_linear_lowEC);
2024-06-25 19:41:12 +00:00
//Serial.print("ec="); Serial.println(ec);
2024-06-25 19:10:12 +00:00
ec25=ec_calculateEC25(ec,tempC_reservoir);
2024-05-04 11:04:05 +00:00
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-06-25 19:10:12 +00:00
ec_flag_measurement_available=true;
ecstate=IDLE;
2023-04-20 21:16:04 +00:00
}
break;
}
2024-05-11 07:20:37 +00:00
if (ec_measurementRunning()) { //measurement running
2023-04-30 17:00:12 +00:00
if (loopmillis>last_read_ec+EC_READ_INTERVAL) { //take reading into array
last_read_ec=loopmillis;
2024-05-11 07:20:37 +00:00
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
2024-05-13 18:32:14 +00:00
//Serial.print("Get ADC Reading");
2023-04-30 17:00:12 +00:00
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
2024-05-13 18:32:14 +00:00
//Serial.print(". Write to pos ");
2024-06-25 19:41:12 +00:00
//Serial.print(ec_array_pos);
//Serial.print(" = ");
//Serial.println(value);
2024-05-11 07:20:37 +00:00
2023-04-30 17:00:12 +00:00
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-06-25 19:10:12 +00:00
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);
2024-04-23 21:39:18 +00:00
//Serial.print("DEBUG: Set Relay to "); Serial.println(relay);
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
//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));
}
2024-05-11 07:20:37 +00:00
bool ec_measurementRunning() {
return (ec_array_pos<EC_ARRAY_SIZE);
}
2023-04-17 19:32:34 +00:00
#endif