@@ -340,49 +340,49 @@ void display_process(therm_settings_t* s
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_SETTEMPOFFSET;
}
else if(!HAL_GPIO_ReadPin(SW_UP)) {
set->temp_units = TEMP_UNITS_FAHRENHEIT;
else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
set->temp_units = TEMP_UNITS_CELSIUS;
// Event Handler
// N/A
} break;
case STATE_SETTEMPOFFSET:
{
// Write text to OLED
// [ therm :: set temp offset ]
// [ g = 12 ]
ssd1306_DrawString("Thermocouple Offset", 0, 40);
ssd1306_DrawString("Temp Cal Offset", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->temp_offset, tempstr, 10);
ssd1306_DrawString("O=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
save_settings(&set);
status->state = STATE_IDLE;
else {
user_input_signed(&set->temp_offset);
@@ -214,49 +214,49 @@ void update_temp() {
status.temp = temp_pre * signint;
status.temp += set.temp_offset;
// PID implementation
// TODO: Make struct that has the last_temp and i_state in it, pass by ref. Make struct that has other input values maybe.
int16_t last_pid_temp = 0;
uint8_t last_pid_temp_frac = 0;
int32_t i_state = 0;
int16_t update_pid(uint16_t k_p, uint16_t k_i, uint16_t k_d, int16_t temp, uint8_t temp_frac, int16_t setpoint)
// Calculate instantaneous error
int16_t error = setpoint - temp; // TODO: Use fixed point fraction
// Proportional component
int32_t p_term = k_p * error;
// Error accumulator (integrator)
i_state += error;
// to prevent the iTerm getting huge despite lots of
// to prevent the iTerm getting huge from lots of
// error, we use a "windup guard"
// (this happens when the machine is first turned on and
// it cant help be cold despite its best efforts)
// not necessary, but this makes windup guard values
// relative to the current iGain
int32_t windup_guard_res = set.windup_guard / k_i;
// Calculate integral term with windup guard
if (i_state > windup_guard_res)
i_state = windup_guard_res;
else if (i_state < -windup_guard_res)
i_state = -windup_guard_res;
int32_t i_term = k_i * i_state;
// Calculate differential term (slope since last iteration)
int32_t d_term = (k_d * (status.temp - last_pid_temp));
// Save temperature for next iteration
last_pid_temp = status.temp;
last_pid_temp_frac = status.temp_frac;
int16_t result = p_term + i_term - d_term;
#include "stm32f0xx_hal.h"
#include "states.h"
void save_settings(therm_settings_t *tosave)
// TODO: Rework with FLASH read/write
/*
Minimal_EEPROM_Unlock();
// Try programming a word at an address divisible by 4
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW, boottobrew);
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD, windup_guard);
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P, k_p);
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I, k_i);
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D, k_d);
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS, temp_units);
Minimal_EEPROM_Lock();
// TODO: Check for missing settings
*/
void save_setpoints(therm_settings_t *tosave)
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP, setpoint_brew);
Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP, setpoint_steam);
// TODO: Make a struct that has all settings in it. Pass by ref to this func in a library.
void restore_settings(therm_settings_t *tosave)
/* Minimal_EEPROM_Unlock();
while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
boottobrew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW));
windup_guard = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD));
k_p = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P));
k_i = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I));
k_d = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D));
setpoint_brew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP));
setpoint_steam = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP));
temp_units = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS));
Minimal_EEPROM_Lock(); */
Status change: