Changeset - 7527bab9ca74
[Not reviewed]
cortex-f0
0 1 0
Ethan Zonca - 9 years ago 2015-06-01 17:30:55
ez@ethanzonca.com
Tweaks to PID word length, commenting
1 file changed with 16 insertions and 12 deletions:
main.c
16
12
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -88,14 +88,15 @@ int main(void)
 
    status.setpoint = 0;
 
    status.pid_enabled = 0;
 
 
    // 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);
 
    ssd1306_DrawString("protofusion.org/therm", 3, 0);
 
 
    HAL_Delay(1500);
 
@@ -110,14 +111,13 @@ int main(void)
 
        // Run state machine
 
        display_process(&set, &status); 
 
    }
 
 
}
 
 
/** System Clock Configuration
 
*/
 
// Clock configuration
 
void SystemClock_Config(void)
 
{
 
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
 
@@ -139,13 +139,13 @@ void SystemClock_Config(void)
 
 
  __SYSCFG_CLK_ENABLE();
 
 
}
 
 
 
 
// Grab temperature reading from MAX31855
 
void update_temp() {
 
 
    // Assert CS
 
    HAL_GPIO_WritePin(MAX_CS, 0);
 
 
    uint8_t rxdatah[1] = {0x00};
 
@@ -222,42 +222,43 @@ void update_temp() {
 
 
 
// 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;
 
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;
 
 
  // to prevent the iTerm getting huge despite 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
 
  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;
 
  last_pid_temp_frac = status.temp_frac;
 
 
  int16_t result = p_term + i_term - d_term;
 
@@ -275,18 +276,18 @@ int16_t update_pid(uint16_t k_p, uint16_
 
 
uint32_t last_ssr_on = 0;
 
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) 
 
    {
 
        HAL_GPIO_TogglePin(LED_POWER);
 
        last_led = ticks;
 
@@ -340,12 +341,13 @@ void process()
 
        last_vcp_tx = ticks;
 
    }
 
}
 
 
void save_settings()
 
{
 
    // 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);
 
@@ -355,12 +357,13 @@ void save_settings()
 
    Minimal_EEPROM_Lock();
 
*/
 
}
 
 
void save_setpoints()
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*
 
 
    Minimal_EEPROM_Unlock();
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP, setpoint_brew);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP, setpoint_steam); 
 
    Minimal_EEPROM_Lock();
 
@@ -368,12 +371,13 @@ 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));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    windup_guard = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD));
0 comments (0 inline, 0 general)