# HG changeset patch # User Ethan Zonca # Date 2015-06-01 17:30:55 # Node ID 7527bab9ca743d88d943a547025b748f0232a6b1 # Parent 2025413db75952513539c472ecefc892d2eaaba4 Tweaks to PID word length, commenting diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -91,8 +91,9 @@ int main(void) // Load settings (if any) from EEPROM restore_settings(); + // Go to brew instead of idle if configured thusly if(set.boottobrew) - status.state = STATE_PREHEAT_BREW; // Go to brew instead of idle if configured thusly + status.state = STATE_PREHEAT_BREW; // Startup screen ssd1306_DrawString("therm v0.2", 1, 40); @@ -113,8 +114,7 @@ int main(void) } -/** System Clock Configuration -*/ +// Clock configuration void SystemClock_Config(void) { @@ -142,7 +142,7 @@ void SystemClock_Config(void) } - +// Grab temperature reading from MAX31855 void update_temp() { // Assert CS @@ -225,15 +225,15 @@ void update_temp() { // 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; -int16_t i_state = 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 = (int16_t)setpoint - (int16_t)temp; // TODO: Use fixed point fraction + int16_t error = setpoint - temp; // TODO: Use fixed point fraction // Proportional component - int16_t p_term = k_p * error; + int32_t p_term = k_p * error; // Error accumulator (integrator) i_state += error; @@ -244,17 +244,18 @@ int16_t update_pid(uint16_t k_p, uint16_ // it cant help be cold despite its best efforts) // not necessary, but this makes windup guard values // relative to the current iGain - int16_t windup_guard_res = set.windup_guard / k_i; + 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; - int16_t i_term = k_i * i_state; + + int32_t i_term = k_i * i_state; // Calculate differential term (slope since last iteration) - int16_t d_term = (k_d * (status.temp - last_pid_temp)); + int32_t d_term = (k_d * (status.temp - last_pid_temp)); // Save temperature for next iteration last_pid_temp = status.temp; @@ -278,12 +279,12 @@ uint32_t last_vcp_tx = 0; uint32_t last_led = 0; int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD -// Process things +// Turn SSR output on/off according to set duty cycle. +// TODO: Eventually maybe replace with a very slow timer or something. Double-check this code... void process() { update_temp(); // Read MAX31855 - // TODO: Add calibration offset (linear) uint32_t ticks = HAL_GetTick(); if(ticks - last_led > 400) @@ -343,6 +344,7 @@ void process() void save_settings() { + // TODO: Rework with FLASH read/write /* Minimal_EEPROM_Unlock(); // Try programming a word at an address divisible by 4 @@ -358,6 +360,7 @@ void save_settings() void save_setpoints() { + // TODO: Rework with FLASH read/write /* Minimal_EEPROM_Unlock(); @@ -371,6 +374,7 @@ void save_setpoints() // TODO: Make a struct that has all settings in it. Pass by ref to this func in a library. void restore_settings() { + // TODO: Rework with FLASH read/write /* Minimal_EEPROM_Unlock(); while(Minimal_FLASH_GetStatus()==FLASH_BUSY); boottobrew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW));