Skip to content

Commit

Permalink
Merge pull request #13 from AllanMar/feature-Larger-PID-Gains
Browse files Browse the repository at this point in the history
Convert PID gains to 16bit
  • Loading branch information
mattreba committed Jul 12, 2015
2 parents 0050da6 + ffcdd38 commit 25d1f6a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
4 changes: 4 additions & 0 deletions BrewTroller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ const char BTVER[] PROGMEM = "2.7";
#ERROR
#endif

#define PIDGAIN_DIV 100
#define PIDGAIN_DEC 2
#define PIDGAIN_LIM 65535

struct ProgramThread {
byte activeStep;
byte recipe;
Expand Down
67 changes: 36 additions & 31 deletions EEPROM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void loadPWMOutput(byte i) {

pid[i].SetInputLimits(0, 25500);
pid[i].SetOutputLimits(0, (unsigned long)pwmResolution * pidLimit / 100);
pid[i].SetTunings(getPIDp(i), getPIDi(i), getPIDd(i));
pid[i].SetTunings((double)getPIDp(i)/PIDGAIN_DIV, (double)getPIDi(i)/PIDGAIN_DIV, (double)getPIDd(i)/PIDGAIN_DIV);
pid[i].SetMode(AUTO);
pid[i].SetSampleTime(PID_UPDATE_INTERVAL);
}
Expand Down Expand Up @@ -238,37 +238,9 @@ void setTSAddr(byte sensor, byte addr[8]) {
}

//**********************************************************************************
//OPEN (72)
//OPEN (72-90)
//**********************************************************************************


//**********************************************************************************
//PIDp HLT (73), Mash (78), Kettle (83), RESERVED (88)
//**********************************************************************************
void setPIDp(byte vessel, byte value) {
pid[vessel].SetTunings(value, pid[vessel].GetI_Param(), pid[vessel].GetD_Param());
EEPROM.write(73 + vessel * 5, value);
}
byte getPIDp(byte vessel) { return EEPROM.read(73 + vessel * 5); }

//**********************************************************************************
//PIDi HLT (74), Mash (79), Kettle (84), RESERVED (89)
//**********************************************************************************
void setPIDi(byte vessel, byte value) {
pid[vessel].SetTunings(pid[vessel].GetP_Param(), value, pid[vessel].GetD_Param());
EEPROM.write(74 + vessel * 5, value);
}
byte getPIDi(byte vessel) { return EEPROM.read(74 + vessel * 5); }

//**********************************************************************************
//PIDd HLT (75), Mash (80), Kettle (85), RESERVED (90)
//**********************************************************************************
void setPIDd(byte vessel, byte value) {
pid[vessel].SetTunings(pid[vessel].GetP_Param(), pid[vessel].GetI_Param(), value);
EEPROM.write(75 + vessel * 5, value);
}
byte getPIDd(byte vessel) { return EEPROM.read(75 + vessel * 5); }

//**********************************************************************************
//PWM Period HLT (76), Mash (81), Kettle (86), RESERVED (91)
//**********************************************************************************
Expand Down Expand Up @@ -820,6 +792,33 @@ void initializeBrewStepConfiguration() {
brewStepConfiguration.flySpargeHysteresis = 0;
}

//**********************************************************************************
//PIDp HLT (2225-2226), Mash (2231-2232), Kettle (2237-2238), RESERVED (2243-2244)
//**********************************************************************************
void setPIDp(byte vessel, unsigned int value) {
pid[vessel].SetTunings((double)value/PIDGAIN_DIV, pid[vessel].GetI_Param(), pid[vessel].GetD_Param());
EEPROMwriteInt(2225 + vessel * 6, value);
}
unsigned int getPIDp(byte vessel) { return EEPROMreadInt(2225 + vessel * 6); }

//**********************************************************************************
//PIDi HLT (2227-2228), Mash (2233-2234), Kettle (2239-2240), RESERVED (2245-2246)
//**********************************************************************************
void setPIDi(byte vessel, unsigned int value) {
pid[vessel].SetTunings(pid[vessel].GetP_Param(), (double)value/PIDGAIN_DIV, pid[vessel].GetD_Param());
EEPROMwriteInt(2227 + vessel * 6, value);
}
unsigned int getPIDi(byte vessel) { return EEPROMreadInt(2227 + vessel * 6); }

//**********************************************************************************
//PIDd HLT (2229-2230), Mash (2035-2036), Kettle (2241-2242), RESERVED (2247-2248)
//**********************************************************************************
void setPIDd(byte vessel, unsigned int value) {
pid[vessel].SetTunings(pid[vessel].GetP_Param(), pid[vessel].GetI_Param(), (double)value/PIDGAIN_DIV);
EEPROMwriteInt(2229 + vessel * 6, value);
}
unsigned int getPIDd(byte vessel) { return EEPROMreadInt(2229 + vessel * 6); }

//*****************************************************************************************************************************
// Check/Update/Format EEPROM
//*****************************************************************************************************************************
Expand Down Expand Up @@ -926,7 +925,13 @@ boolean checkConfig() {
case 9:
setMashTunHeatCapacity(0);
setMinimumSpargeVolume(0);
EEPROM.write(2047, 10);
case 10:
for (byte vessel = VS_HLT; vessel <= VS_KETTLE; vessel++) {
EEPROMwriteInt(2225 + vessel * 6, EEPROM.read(73 + vessel * 5) * PIDGAIN_DIV); //P Gain
EEPROMwriteInt(2227 + vessel * 6, EEPROM.read(74 + vessel * 5) * PIDGAIN_DIV); //I Gain
EEPROMwriteInt(2229 + vessel * 6, EEPROM.read(75 + vessel * 5) * PIDGAIN_DIV); //D Gain
}
EEPROM.write(2047, 11);
}
return 0;
}
Expand Down
12 changes: 6 additions & 6 deletions UI_Menu.ino
Original file line number Diff line number Diff line change
Expand Up @@ -806,13 +806,13 @@ void menuVesselSettings(byte vessel) {
vesselMenu.appendItem(itoa(getPWMResolution(vessel), buf, 10), 2);

vesselMenu.setItem_P(PSTR("P Gain: "), 3);
vesselMenu.appendItem(itoa(getPIDp(vessel), buf, 10), 3);
vesselMenu.appendItem(vftoa(getPIDp(vessel), buf, PIDGAIN_DIV, PIDGAIN_DEC), 3);

vesselMenu.setItem_P(PSTR("I Gain: "), 4);
vesselMenu.appendItem(itoa(getPIDi(vessel), buf, 10), 4);
vesselMenu.appendItem(vftoa(getPIDi(vessel), buf, PIDGAIN_DIV, PIDGAIN_DEC), 4);

vesselMenu.setItem_P(PSTR("D Gain: "), 5);
vesselMenu.appendItem(itoa(getPIDd(vessel), buf, 10), 5);
vesselMenu.appendItem(vftoa(getPIDd(vessel), buf, PIDGAIN_DIV, PIDGAIN_DEC), 5);

vesselMenu.setItem_P(PSTR("PID Limit: "), 6);
vesselMenu.appendItem(itoa(getPIDLimit(vessel), buf, 10), 6);
Expand Down Expand Up @@ -842,11 +842,11 @@ void menuVesselSettings(byte vessel) {
else if (lastOption == 2)
setPWMResolution(vessel, getValue_P(PSTR("PWM Resolution"), getPWMResolution(vessel), 1, 255, PSTR("")));
else if (lastOption == 3)
setPIDp(vessel, getValue_P(PSTR("P Gain"), getPIDp(vessel), 1, 255, PSTR("")));
setPIDp(vessel, getValue_P(PSTR("P Gain"), getPIDp(vessel), PIDGAIN_DIV, PIDGAIN_LIM, PSTR("")));
else if (lastOption == 4)
setPIDi(vessel, getValue_P(PSTR("I Gain"), getPIDi(vessel), 1, 255, PSTR("")));
setPIDi(vessel, getValue_P(PSTR("I Gain"), getPIDi(vessel), PIDGAIN_DIV, PIDGAIN_LIM, PSTR("")));
else if (lastOption == 5)
setPIDd(vessel, getValue_P(PSTR("D Gain"), getPIDd(vessel), 1, 255, PSTR("")));
setPIDd(vessel, getValue_P(PSTR("D Gain"), getPIDd(vessel), PIDGAIN_DIV, PIDGAIN_LIM, PSTR("")));
else if (lastOption == 6)
setPIDLimit(vessel, getValue_P(PSTR("PID Limit"), getPIDLimit(vessel), 1, 100, PSTR("")));
else if (lastOption == 7)
Expand Down

0 comments on commit 25d1f6a

Please sign in to comment.