From ac157d2acb4420f8063bf9f73bc0d3c19dd499e3 Mon Sep 17 00:00:00 2001 From: Chris Giacofei Date: Wed, 1 May 2024 15:46:02 -0400 Subject: [PATCH] Switch over to PID control. --- boil_kettle/boil_kettle.ino | 101 +++++++++++++--------------- boil_kettle/src/Arduino-PID-Library | 2 +- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/boil_kettle/boil_kettle.ino b/boil_kettle/boil_kettle.ino index 0139330..f2fc52c 100644 --- a/boil_kettle/boil_kettle.ino +++ b/boil_kettle/boil_kettle.ino @@ -22,9 +22,9 @@ double KettleSetpoint = 70; double CurrentTemp; bool tuning = false; -//EEPROMStorage eepromKp(0, 2.0); // 9 bytes for doubles 8 + 1 for checksum -//EEPROMStorage eepromKi(9, 5.0); // Initialize at zero. -//EEPROMStorage eepromKd(18, 1.0); +EEPROMStorage eepromKp(0, 2.0); // 9 bytes for doubles 8 + 1 for checksum +EEPROMStorage eepromKi(9, 5.0); +EEPROMStorage eepromKd(18, 1.0); // User I/O objects. Button Enter; @@ -32,11 +32,7 @@ slowPWM boilPWM; MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK); LiquidCrystal_I2C lcd(0x27, 20, 4); Adafruit_MAX31865 thermoRTD = Adafruit_MAX31865(KettleRTD); -thermoControl Controller(&CurrentTemp, &KettleSetpoint, &KettleDuty, AUTOMATIC); - -//PID ControllerPID(&CurrentTemp, &KettleDuty, &KettleSetpoint, eepromKp, eepromKi, eepromKd, P_ON_M, DIRECT); - -unsigned long lastRun = 0; +Controller(&CurrentTemp, &KettleDuty, &KettleSetpoint, eepromKp, eepromKi, eepromKd, P_ON_M, DIRECT); // Return a character array to represent the // On/Off state of the kettle. @@ -74,6 +70,8 @@ LiquidMenu menu(lcd); // Increases/decreases the kettle output to a max // of 100% and minimum of 0%. void doEncoder() { + if (tuning) return; + uint8_t result = rotary.read(); uint8_t inc = 1; @@ -108,7 +106,6 @@ void doEncoder() { void setup() { - lastRun = millis() - UpdateInterval; Serial.begin(115200); rotary.begin(); thermoRTD.begin(MAX31865_3WIRE); @@ -116,7 +113,6 @@ void setup() { attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE); attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE); - pinMode(encoderCLK, INPUT_PULLUP); pinMode(encoderDT, INPUT_PULLUP); Enter.begin(encoderBTN); @@ -143,33 +139,48 @@ void run_kettle() { Controller.Compute(); } -/* + void run_tuning() { + if (Enter.pressed()) { - stopAutoTune(); + if(tuning) { //cancel autotune + Controller.Cancel(); + Controller.Mode(OFF); + tuning=false; + } + return; + } + + // Initial setup + if(!tuning) { + KettleDuty=50; + Controller.SetNoiseBand(0.5); + Controller.SetOutputStep(30); + Controller.SetLookbackSec(20); + Controller.Mode(AUTOMATIC); + tuning=true; + } + + if (Controller.ComputeTune() != 0) { + tuning = false; + } + if(!tuning) { + //we're done, set the tuning parameters + Controller.SetTunings(Controller.TunedKp(),Controller.TunedKi(),Controller.TunedKd()); + Controller.Mode(OFF); } - int result = ControllerPID.ComputeTune(); - if (result!=0) - { - tuning = false; - } - if(!tuning) - { //we're done, set the tuning parameters - ControllerPID.SetTunings(ControllerPID.TunedKp(),ControllerPID.TunedKi(),ControllerPID.TunedKd()); - ControllerPID.Mode(OFF); - } } -*/ + void loop() { unsigned long now = millis(); - static unsigned long lastTime=now-2000; + static unsigned long lastTime=now-250; unsigned long timeChange = (now - lastTime); static byte lastoutput = LOW; static double lastKettleDuty = 0; static double lastKettleSetpoint = 0; static byte lastTemperature; - if(timeChange >= 2000) { + if(timeChange >= 250) { CurrentTemp = 32 + 1.8 * thermoRTD.temperature(RNOMINAL_KETTLE, RREF_KETTLE); lastTime = now; } @@ -177,7 +188,8 @@ void loop() { if (!tuning) { run_kettle(); } else { - //run_tuning(); + startAutoTune(); + run_tuning(); } byte output = boilPWM.Compute(KettleDuty); @@ -195,31 +207,11 @@ void loop() { } } -/* -void startAutoTune() { - if(!tuning) { - //Set the output to the desired starting frequency. - KettleDuty=50; - ControllerPID.SetNoiseBand(1); - ControllerPID.SetOutputStep(30); - ControllerPID.SetLookbackSec(20); - ControllerPID.Mode(AUTOMATIC); - tuning=true; - } -} - -void stopAutoTune() { - if(tuning) { //cancel autotune - ControllerPID.Cancel(); - ControllerPID.Mode(OFF); - tuning=false; - } -} void SaveTunings() { - eepromKp = ControllerPID.GetKp(); - eepromKi = ControllerPID.GetKi(); - eepromKd = ControllerPID.GetKd(); + eepromKp = Controller.GetKp(); + eepromKi = Controller.GetKi(); + eepromKd = Controller.GetKd(); } void SerialSend() @@ -230,9 +222,9 @@ void SerialSend() if(tuning){ Serial.println("tuning mode"); } else { - Serial.print("kp: ");Serial.print(ControllerPID.GetKp());Serial.print(" "); - Serial.print("ki: ");Serial.print(ControllerPID.GetKi());Serial.print(" "); - Serial.print("kd: ");Serial.print(ControllerPID.GetKd());Serial.println(); + Serial.print("kp: ");Serial.print(Controller.GetKp());Serial.print(" "); + Serial.print("ki: ");Serial.print(Controller.GetKi());Serial.print(" "); + Serial.print("kd: ");Serial.print(Controller.GetKd());Serial.println(); } } @@ -242,13 +234,10 @@ void SerialReceive() String myinput; myinput = Serial.readString(); myinput.trim(); - if(myinput=="start"){ + if(myinput=="tune"){ startAutoTune(); - } else if(myinput=="stop") { - stopAutoTune(); } else if(myinput=="save") { SaveTunings(); } } } -*/ \ No newline at end of file diff --git a/boil_kettle/src/Arduino-PID-Library b/boil_kettle/src/Arduino-PID-Library index 7787498..b3255d9 160000 --- a/boil_kettle/src/Arduino-PID-Library +++ b/boil_kettle/src/Arduino-PID-Library @@ -1 +1 @@ -Subproject commit 7787498eda8955288e99a18d02581a425203bac5 +Subproject commit b3255d94d45c7a24acf39312f85c8fb69ef7278a