Switch over to PID control.

This commit is contained in:
Chris Giacofei 2024-05-01 15:46:02 -04:00
parent 9be0cabfe3
commit ac157d2acb
2 changed files with 46 additions and 57 deletions

View File

@ -22,9 +22,9 @@ double KettleSetpoint = 70;
double CurrentTemp; double CurrentTemp;
bool tuning = false; bool tuning = false;
//EEPROMStorage<double> eepromKp(0, 2.0); // 9 bytes for doubles 8 + 1 for checksum EEPROMStorage<double> eepromKp(0, 2.0); // 9 bytes for doubles 8 + 1 for checksum
//EEPROMStorage<double> eepromKi(9, 5.0); // Initialize at zero. EEPROMStorage<double> eepromKi(9, 5.0);
//EEPROMStorage<double> eepromKd(18, 1.0); EEPROMStorage<double> eepromKd(18, 1.0);
// User I/O objects. // User I/O objects.
Button Enter; Button Enter;
@ -32,11 +32,7 @@ slowPWM boilPWM;
MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK); MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27, 20, 4); LiquidCrystal_I2C lcd(0x27, 20, 4);
Adafruit_MAX31865 thermoRTD = Adafruit_MAX31865(KettleRTD); Adafruit_MAX31865 thermoRTD = Adafruit_MAX31865(KettleRTD);
thermoControl Controller(&CurrentTemp, &KettleSetpoint, &KettleDuty, AUTOMATIC); Controller(&CurrentTemp, &KettleDuty, &KettleSetpoint, eepromKp, eepromKi, eepromKd, P_ON_M, DIRECT);
//PID ControllerPID(&CurrentTemp, &KettleDuty, &KettleSetpoint, eepromKp, eepromKi, eepromKd, P_ON_M, DIRECT);
unsigned long lastRun = 0;
// Return a character array to represent the // Return a character array to represent the
// On/Off state of the kettle. // On/Off state of the kettle.
@ -74,6 +70,8 @@ LiquidMenu menu(lcd);
// Increases/decreases the kettle output to a max // Increases/decreases the kettle output to a max
// of 100% and minimum of 0%. // of 100% and minimum of 0%.
void doEncoder() { void doEncoder() {
if (tuning) return;
uint8_t result = rotary.read(); uint8_t result = rotary.read();
uint8_t inc = 1; uint8_t inc = 1;
@ -108,7 +106,6 @@ void doEncoder() {
void setup() { void setup() {
lastRun = millis() - UpdateInterval;
Serial.begin(115200); Serial.begin(115200);
rotary.begin(); rotary.begin();
thermoRTD.begin(MAX31865_3WIRE); thermoRTD.begin(MAX31865_3WIRE);
@ -116,7 +113,6 @@ void setup() {
attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE); attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE); attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE);
pinMode(encoderCLK, INPUT_PULLUP); pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP); pinMode(encoderDT, INPUT_PULLUP);
Enter.begin(encoderBTN); Enter.begin(encoderBTN);
@ -143,33 +139,48 @@ void run_kettle() {
Controller.Compute(); Controller.Compute();
} }
/*
void run_tuning() { void run_tuning() {
if (Enter.pressed()) { 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() { void loop() {
unsigned long now = millis(); unsigned long now = millis();
static unsigned long lastTime=now-2000; static unsigned long lastTime=now-250;
unsigned long timeChange = (now - lastTime); unsigned long timeChange = (now - lastTime);
static byte lastoutput = LOW; static byte lastoutput = LOW;
static double lastKettleDuty = 0; static double lastKettleDuty = 0;
static double lastKettleSetpoint = 0; static double lastKettleSetpoint = 0;
static byte lastTemperature; static byte lastTemperature;
if(timeChange >= 2000) { if(timeChange >= 250) {
CurrentTemp = 32 + 1.8 * thermoRTD.temperature(RNOMINAL_KETTLE, RREF_KETTLE); CurrentTemp = 32 + 1.8 * thermoRTD.temperature(RNOMINAL_KETTLE, RREF_KETTLE);
lastTime = now; lastTime = now;
} }
@ -177,7 +188,8 @@ void loop() {
if (!tuning) { if (!tuning) {
run_kettle(); run_kettle();
} else { } else {
//run_tuning(); startAutoTune();
run_tuning();
} }
byte output = boilPWM.Compute(KettleDuty); 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() { void SaveTunings() {
eepromKp = ControllerPID.GetKp(); eepromKp = Controller.GetKp();
eepromKi = ControllerPID.GetKi(); eepromKi = Controller.GetKi();
eepromKd = ControllerPID.GetKd(); eepromKd = Controller.GetKd();
} }
void SerialSend() void SerialSend()
@ -230,9 +222,9 @@ void SerialSend()
if(tuning){ if(tuning){
Serial.println("tuning mode"); Serial.println("tuning mode");
} else { } else {
Serial.print("kp: ");Serial.print(ControllerPID.GetKp());Serial.print(" "); Serial.print("kp: ");Serial.print(Controller.GetKp());Serial.print(" ");
Serial.print("ki: ");Serial.print(ControllerPID.GetKi());Serial.print(" "); Serial.print("ki: ");Serial.print(Controller.GetKi());Serial.print(" ");
Serial.print("kd: ");Serial.print(ControllerPID.GetKd());Serial.println(); Serial.print("kd: ");Serial.print(Controller.GetKd());Serial.println();
} }
} }
@ -242,13 +234,10 @@ void SerialReceive()
String myinput; String myinput;
myinput = Serial.readString(); myinput = Serial.readString();
myinput.trim(); myinput.trim();
if(myinput=="start"){ if(myinput=="tune"){
startAutoTune(); startAutoTune();
} else if(myinput=="stop") {
stopAutoTune();
} else if(myinput=="save") { } else if(myinput=="save") {
SaveTunings(); SaveTunings();
} }
} }
} }
*/

@ -1 +1 @@
Subproject commit 7787498eda8955288e99a18d02581a425203bac5 Subproject commit b3255d94d45c7a24acf39312f85c8fb69ef7278a