diff --git a/boil_kettle/src/Arduino-PID-Library b/boil_kettle/src/Arduino-PID-Library new file mode 160000 index 0000000..9b4ca0e --- /dev/null +++ b/boil_kettle/src/Arduino-PID-Library @@ -0,0 +1 @@ +Subproject commit 9b4ca0e5b6d7bab9c6ac023e249d6af2446d99bb diff --git a/boil_kettle/src/button.h b/boil_kettle/src/button/button.h similarity index 100% rename from boil_kettle/src/button.h rename to boil_kettle/src/button/button.h diff --git a/boil_kettle/src/functions.h b/boil_kettle/src/functions.h index 7e0f41c..0990bf2 100644 --- a/boil_kettle/src/functions.h +++ b/boil_kettle/src/functions.h @@ -7,28 +7,24 @@ void doEncoder() { uint8_t result = rotary.read(); - uint8_t inc; + int inc; if (result) { uint8_t speed = rotary.speed(); speed >= 10 ? inc = 5 : inc = 1; if (result == DIR_CCW) inc = inc * -1; - } - SettingChanged = true; - - if (KettleController.Mode() == MANUAL) { - - uint8_t KettleDuty = (uint8_t)KettleController.Power(); - KettleDuty = max(0, min((KettleDuty / inc) * inc + inc, 100)); - KettleController.Power((double)KettleDuty); - - } else if (KettleController.Mode() == AUTOMATIC) { - - uint8_t KettleTemp = (uint8_t)KettleController.Setpoint(); - KettleTemp = max(0, min((KettleTemp / inc) * inc + inc, 220)); - KettleController.Setpoint((double)KettleTemp); + SettingChanged = true; + if (KettleController.Mode() == MANUAL) { + uint8_t KettleDuty = (uint8_t)KettleController.Power(); + KettleDuty = max(0, min((KettleDuty / inc) * inc + inc, 100)); + KettleController.Power((double)KettleDuty); + } else if (KettleController.Mode() == AUTOMATIC) { + uint8_t KettleTemp = (uint8_t)KettleController.Setpoint(); + KettleTemp = max(0, min((KettleTemp / inc) * inc + inc, 220)); + KettleController.Setpoint((double)KettleTemp); + } } else { SettingChanged = false; } @@ -38,11 +34,11 @@ void doEncoder() // state of the kettle. char* ShowKettleState() { if (KettleController.Mode() == MANUAL) { - return (char*)F("Kettle: Manual"); + return (char*)"Kettle: Manual"; } else if (KettleController.Mode() == AUTOMATIC) { - return (char*)F("Kettle: Auto"); + return (char*)"Kettle: Auto"; } else { - return (char*)F("Kettle: Off"); + return (char*)"Kettle: Off"; } } @@ -50,26 +46,27 @@ char* ShowKettleSetting() { static char LCD_Line[21]; char setting[4]; if (KettleController.Mode() == MANUAL) { - strcpy(LCD_Line, (char*)F("Kettle Power: ")); + strcpy(LCD_Line, (char*)"Kettle Power: "); itoa(KettleController.Power(), setting, 10); strcat(LCD_Line, setting); - strcat(LCD_Line, (char*)F("%")); + strcat(LCD_Line, (char*)"%"); return LCD_Line; } else if (KettleController.Mode() == AUTOMATIC) { - strcpy(LCD_Line, (char*)F("Kettle Temp: ")); + strcpy(LCD_Line, (char*)"Kettle Temp: "); itoa(KettleController.Setpoint(), setting, 10); strcat(LCD_Line, setting); - strcat(LCD_Line, (char*)F("F")); + strcat(LCD_Line, (char*)"F"); return LCD_Line; } else { - return (char*)""; + return (char*)"It's Off stoopid"; } } void UpdateBoilKettle(){ if (Enter.pressed()) { + Serial.println("Pressed"); KettleController.CycleMode(); SettingChanged = true; } diff --git a/boil_kettle/src/slowPWM.h b/boil_kettle/src/slowPWM/slowPWM.h similarity index 96% rename from boil_kettle/src/slowPWM.h rename to boil_kettle/src/slowPWM/slowPWM.h index 94b9a38..50aad9d 100644 --- a/boil_kettle/src/slowPWM.h +++ b/boil_kettle/src/slowPWM/slowPWM.h @@ -19,7 +19,7 @@ class slowPWM { pinMode(pin, OUTPUT); } - byte compute(uint8_t duty) { + byte Compute(double duty) { unsigned long onTime = (duty * period) / 1000; unsigned long offTime = period - onTime; unsigned long currentTime = millis(); diff --git a/boil_kettle/src/thermoControl.cpp b/boil_kettle/src/thermoControl.cpp deleted file mode 100644 index 2ad0fd6..0000000 --- a/boil_kettle/src/thermoControl.cpp +++ /dev/null @@ -1,118 +0,0 @@ - -#include -#include -#include "thermoControl.h" - - -void thermoControl::begin(int pinRTD) { - RTD = new Adafruit_MAX31865(pinRTD); - r_ref = 430.0; - r_nominal = 100.0; - outMax = 100; - outMin = 10; - OpMode = OFF; - SampleInterval = 100; - lastTime = millis()-SampleInterval; - output_pwm = 0; - control_temp = 0; - hysteresis = 1.0; - max_pwr_threshold = 5.0; -} - -void thermoControl::begin(int pinRTD, double temp, double duty, double mpt, double hyst) { - RTD = new Adafruit_MAX31865(pinRTD); - r_ref = 430.0; - r_nominal = 100.0; - outMax = 100; - outMin = 10; - OpMode = OFF; - SampleInterval = 100; - lastTime = millis()-SampleInterval; - - output_pwm = duty; - control_temp = temp; - hysteresis = hyst; - max_pwr_threshold = mpt; -} - -bool thermoControl::Compute() { - unsigned long now = millis(); - unsigned long timeChange = (now - lastTime); - - double temp = RTD->temperature(r_nominal, r_ref); - - if(timeChange>=SampleInterval && OpMode == AUTOMATIC) { - double output; - double error = control_temp - temp; - - if (error >= max_pwr_threshold) { - output = outMax; - } else if (error > hysteresis) { - output = 100 * error / max_pwr_threshold; - output = max(output, outMin); - output = min(output, outMax); - } else { - output = 0; - } - output_pwm = output; - lastTime = now; - - return true; - } else { - return false; - } -} - -double thermoControl::Power() { - return output_pwm; -} - -void thermoControl::Power(double pwr) { - output_pwm = pwr; -} - -double thermoControl::Setpoint() { - return control_temp; -} - -void thermoControl::Setpoint(double temp) { - control_temp = temp; -} - -void thermoControl::SampleTime(int NewSampleTime) { - if (NewSampleTime > 0) { - SampleInterval = (unsigned long)NewSampleTime; - } -} - -void thermoControl::PowerLimits(double Max, double Min) { - if(Min >= Max) return; - outMax = Max; - outMin = Min; -} - -void thermoControl::Hysteresis(double hys) { - hysteresis = hys; -} - -void thermoControl::ThreshPWR(double thresh) { - max_pwr_threshold = thresh; -} - -void thermoControl::Mode(modes newMode) { - OpMode = newMode; -} - -modes thermoControl::Mode() { - return OpMode; -} - -modes thermoControl::CycleMode() { - if (OpMode + 1 == OVERFLOW) { - OpMode = (modes)(0); - } else { - OpMode = (modes)(OpMode + 1); - } - return OpMode; -} - diff --git a/boil_kettle/src/thermoControl/thermoControl.cpp b/boil_kettle/src/thermoControl/thermoControl.cpp new file mode 100644 index 0000000..8d39082 --- /dev/null +++ b/boil_kettle/src/thermoControl/thermoControl.cpp @@ -0,0 +1,82 @@ + +#include +#include +#include "thermoControl.h" + +thermoControl::thermoControl(double* current_temp, double* setpoint, double* power, modes mode) { + outMax = 100; + outMin = 10; + OpMode = mode; + SampleInterval = 100; + lastTime = millis()-SampleInterval; + output_pwm = power; + control_temp = setpoint; + actual_temp = current_temp; + hysteresis = 1.0; + max_pwr_threshold = 5.0; + Serial.println("Controller Started"); +} + +bool thermoControl::Compute() { + unsigned long now = millis(); + unsigned long timeChange = (now - lastTime); + + if(timeChange >= SampleInterval && OpMode == AUTOMATIC) { + double error = *control_temp - *actual_temp; + + if (error >= max_pwr_threshold) { + *output_pwm = outMax; + } else if (error > hysteresis) { + *output_pwm = 100 * error / max_pwr_threshold; + *output_pwm = max(*output_pwm, outMin); + *output_pwm = min(*output_pwm, outMax); + } else { + *output_pwm = 0; + } + + lastTime = now; + + return true; + } else { + return false; + } +} + +void thermoControl::SampleTime(int NewSampleTime) { + if (NewSampleTime > 0) { + SampleInterval = NewSampleTime; + } +} + +void thermoControl::PowerLimits(double Max, double Min) { + if(Min >= Max) return; + outMax = Max; + outMin = Min; +} + +void thermoControl::Hysteresis(double hys) { + hysteresis = hys; +} + +void thermoControl::ThreshPWR(double thresh) { + max_pwr_threshold = thresh; +} + +void thermoControl::Mode(modes newMode) { + Serial.println(newMode); + OpMode = newMode; +} + +modes thermoControl::Mode() { + return OpMode; +} + +modes thermoControl::CycleMode() { + if (OpMode + 1 == OVERFLOW) { + OpMode = (modes)(0); + } else { + OpMode = (modes)(OpMode + 1); + } + return OpMode; +} + diff --git a/boil_kettle/src/thermoControl.h b/boil_kettle/src/thermoControl/thermoControl.h similarity index 74% rename from boil_kettle/src/thermoControl.h rename to boil_kettle/src/thermoControl/thermoControl.h index 86af5ea..6bf6ebc 100644 --- a/boil_kettle/src/thermoControl.h +++ b/boil_kettle/src/thermoControl/thermoControl.h @@ -3,28 +3,28 @@ #include +#ifndef enum modes enum modes : uint8_t {OFF, AUTOMATIC, MANUAL, OVERFLOW}; +#endif + class thermoControl { private: - Adafruit_MAX31865* RTD; - double r_nominal; - double r_ref; - double output_pwm; - double control_temp; + double *output_pwm; + double *control_temp; + double * actual_temp; + double current_temp; double hysteresis; double max_pwr_threshold; int outMax; int outMin; - + int SampleInterval; + unsigned long lastTime; modes OpMode; - unsigned long SampleInterval; - unsigned long lastTime; - public: - void begin(int, double, double, double, double); - void begin(int); + thermoControl(double*, double*, double*, modes); + void nope(); bool Compute(); double Power(); void Power(double);