Changeset - 7fc667dd7f38
[Not reviewed]
default
0 6 0
Ethan Zonca - 8 years ago 2017-07-28 13:27:26
ez@ethanzonca.com
Decent working pid
6 files changed with 28 insertions and 45 deletions:
0 comments (0 inline, 0 general)
src/display.c
Show inline comments
 
@@ -452,158 +452,160 @@ void display_process(void)
 
            // [ g = 12         ]
 
            ssd1306_drawstring("Units: ", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            ssd1306_drawchar(updown(), 1, 52);
 

	
 
            if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT)
 
                ssd1306_drawstring("Fahrenheit", 1, 60);
 
            else
 
                ssd1306_drawstring("Celsius   ", 1, 60);
 

	
 
            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->val.temp_units = TEMP_UNITS_FAHRENHEIT;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
                set->val.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("Temp Cal Offset", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            ssd1306_drawchar(updown(), 1, 52);
 

	
 
            char tempstr[12];
 
            snprintf(tempstr, 12, "O=%d", set->val.temp_offset);
 
            ssd1306_drawstring(tempstr, 1, 60);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
//                flash_save(set);
 
            	flash_savesettings();
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input_signed(&set->val.temp_offset);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 

	
 
        case STATE_PREHEAT:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating brew ]
 
            // [ 30 => 120 C             ]
 
            if(set->val.plant_type == PLANT_HEATER)
 
                ssd1306_drawstring("Preheating...", 0, 0);
 
            else
 
                ssd1306_drawstring("Precooling...", 0, 0);
 

	
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 

	
 
            status->pid_enabled = 1;
 
            status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
                flash_savesettings();
 
            }
 
            else {
 
                user_input_signed(&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN;
 
            }
 
 
 
        } break;
 

	
 
        case STATE_MAINTAIN:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to brew ]
 
            // [ 30 => 120 C           ]
 

	
 
            if(set->val.plant_type == PLANT_HEATER)
 
                ssd1306_drawstring("Preheated!", 0, 0);
 
            else
 
                ssd1306_drawstring("Precooled!", 0, 0);
 

	
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
            status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
                flash_savesettings();
 
            }
 
            else {
 
                user_input_signed(&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        // Thermocouple error
 
        case STATE_TC_ERROR:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_drawstring("Error:              ", 0, 0);
 

	
 
            char tempstr[6];
 
            itoa(status->error_code, tempstr, 10);
 
            ssd1306_drawstring(tempstr, 0, 57);
 

	
 
            //TODO: add RTD error codes
 

	
 
            if(status->error_code == 1)
 
                ssd1306_drawstring("    TC Open Circuit", 1, 0);
 
            else if(status->error_code == 4)
 
                ssd1306_drawstring("    TC Short to GND", 1, 0);
 
            else if(status->error_code == 8)
 
                ssd1306_drawstring("    TC Short to VCC", 1, 0);
 
            else
 
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
 
            ssd1306_drawstring("                    ", 2, 0);
 

	
 
            ssd1306_drawstring("-> to ignore all or", 2, 0);
 
            ssd1306_drawstring("press to continue", 3, 0);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
				#ifdef MAX31865_RTD_SENSOR
 
				max31865_clear_errors(spi_get());
 
				#endif
 
            }
 
            else if(SW_RIGHT_PRESSED) {
 
                set->val.ignore_error = 1;
 
                status->state = STATE_IDLE;
 
            }
src/main.c
Show inline comments
 
@@ -7,107 +7,105 @@
 
#include "stm32f3xx_hal.h"
 
#include "config.h"
 
#include "watchdog.h"
 
#include "system.h"
 
#include "display.h"
 
#include "thermostat.h"
 
#include "gpio.h"
 
#include "tempsense.h"
 
#include "pid.h"
 
#include "error.h"
 
#include "flash.h"
 
#include "ssd1306/ssd1306.h"
 
#include "pwmout.h"
 
 
 
int main(void)
 
{
 
	sysclock_init();
 
	hal_init();
 
	gpio_init();
 
 
	ssd1306_init();
 
 
	// Startup screen
 
    display_startup_screen();
 
    HAL_Delay(2000);
 
    ssd1306_clearscreen();
 
	ssd1306_drawlogo();
 
 
    // Default status
 
	runtime_status()->temp = 0.0;
 
	runtime_status()->state_resume = 0;
 
	runtime_status()->state = STATE_IDLE;
 
	runtime_status()->setpoint = 70;
 
	runtime_status()->pid_enabled = 0;
 
 
    pid_init();
 
    pwmout_init();
 
    flash_init();
 
	watchdog_init();
 
	tempsense_init();
 
 
	// Soft timers
 
    uint32_t last_pid = 0;
 
    uint32_t last_thermostat = 0;
 
    uint32_t last_1hz = 0;
 
    uint32_t last_5hz = 0;
 
 
	float duty = 0.0;
 
	int16_t duty = 0;
 
 
	while (1)
 
	{
 
 
		if(HAL_GetTick() - last_1hz > 750)
 
		{
 
			display_1hz();
 
			last_1hz = HAL_GetTick();
 
		}
 
 
		if(HAL_GetTick() - last_5hz > 200)
 
		{
 
			tempsense_readtemp();
 
			runtime_status()->temp = tempsense_gettemp();
 
			last_5hz = HAL_GetTick();
 
		}
 
 
        if(flash_getsettings()->val.control_mode == MODE_PID && (HAL_GetTick() - last_pid > PID_PERIOD))
 
        {
 
//        	runtime_status()->temp = tempsense_readtemp();
 
        	duty = pid_process();
 
            last_pid = HAL_GetTick();
 
        }
 
 
        // Thermostatic control
 
        if(flash_getsettings()->val.control_mode == MODE_THERMOSTAT && HAL_GetTick() - last_thermostat > SSR_PERIOD)
 
        {
 
//        	runtime_status()->temp = tempsense_readtemp();
 
        	duty = thermostat_process();
 
            last_thermostat = HAL_GetTick();
 
        }
 
 
        pwmout_process((int16_t)duty);
 
        display_process();
 
        watchdog_feed();
 
 
 
//        // Transmit temperature over USB-CDC on a regular basis
 
//        if(HAL_GetTick() - last_vcp_tx > VCP_TX_FREQ)
 
//        {
 
//            // Print temp to cdc
 
//            char tempstr[16];
 
//            itoa_fp(status.temp, status.temp_frac, tempstr);
 
//            uint8_t numlen = strlen(tempstr);
 
//            tempstr[numlen] = '\r';
 
//            tempstr[numlen+1] = '\n';
 
//
 
//    //        if(set.val.usb_plugged)
 
//    //            CDC_Transmit_FS(tempstr, numlen+2);
 
//           // while(CDC_Transmit_FS("\r\n", 2) == USBD_BUSY);
 
//
 
//            last_vcp_tx = HAL_GetTick();
 
//        }
 
 
	}
 
}
 
 
src/pid.c
Show inline comments
 
//
 
// PID: proportional/integral/derivative controller
 
//
 

	
 
#include "pid.h"
 
#include "flash.h"
 

	
 
// PID implementation
 

	
 
static pid_state_t state;
 

	
 

	
 
void pid_init()
 
{
 
	state.i_state = 0;
 
	state.last_pid_temp = 0;
 
	state.last_pid_temp_frac = 0;
 
}
 

	
 
float pid_process(void)
 
{
 

	
 

	
 
//            #ifdef MAX31865_RTD_SENSOR
 
//            max31865_readtemp(spi_get(), &set, &status);
 
//			#else
 
//			max31855_readtemp(spi_get(), &set, &status); // Read MAX31855
 
//			#endif
 

	
 
	float ssr_output = 0;
 

	
 

	
 
	if(runtime_status()->pid_enabled)
 
	{
 
		// Get ssr output for next time
 
		int16_t power_percent = pid_update();
 

	
 
		if(flash_getsettings()->val.plant_type == PLANT_HEATER)
 
		if(flash_getsettings()->val.plant_type == PLANT_COOLER)
 
			power_percent *= -1;
 

	
 
		//power-percent is 0-1000?
 
		ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000;
 

	
 
		// put ssr output on display
 
		ssd1306_drawstring("      ", 0, 90); //fixme: this is bad, but I can't get the old digits to clear otherwise
 
		char tempstr[6];
 
		snprintf(tempstr, 6, "%g", ssr_output);
 
		ssd1306_drawstring(tempstr, 0, 90);
 
		char tempstr[8];
 
		snprintf(tempstr, 8, "%4.1f%%", ssr_output/10.0);
 
		ssd1306_drawstring(tempstr, 0, 85);
 
	}
 
	else
 
	{
 
		ssr_output = 0.0;
 
	}
 

	
 
	return ssr_output; //ssr_output;
 
}
 

	
 
int16_t pid_update(void)
 
{
 
	therm_status_t* status = runtime_status();
 
	therm_settings_t* set = flash_getsettings();
 

	
 
	// Convert temperature to fixed point number with 1/10th resolution
 
	float temp = status->temp;
 

	
 
	// Calculate instantaneous error
 
	int16_t error = status->setpoint - temp; // TODO: Use fixed point fraction
 
	// EMZ FIXME: was regulating to 1 degree below the setpoint! +1 accomadates for this
 
	int16_t error = (status->setpoint+1) - temp;
 

	
 
	// Proportional component
 
	int32_t p_term = set->val.k_p * error;
 

	
 
	// Error accumulator (integrator)
 
	state.i_state += error;
 

	
 
	// 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->val.windup_guard) / set->val.k_i;
 
	int32_t windup_guard_res = (set->val.windup_guard * 10) / set->val.k_i;
 

	
 
	// Calculate integral term with windup guard
 
	if (state.i_state > windup_guard_res)
 
	  state.i_state = windup_guard_res;
 
	else if (state.i_state < -windup_guard_res)
 
	  state.i_state = -windup_guard_res;
 

	
 
	// Discard I if we've achieved the setpoint (TODO: add setting disable/enable)
 
	if(error <= 0)
 
		state.i_state = 0;
 

	
 
	int32_t i_term = set->val.k_i * state.i_state;
 

	
 
	// Calculate differential term (slope since last iteration)
 
	int32_t d_term = (set->val.k_d * (temp - state.last_pid_temp));
 

	
 
	// Save temperature for next iteration
 
	state.last_pid_temp = temp;
 

	
 
	int16_t result = (p_term + i_term - d_term) / 10;
 

	
 
	// Put out tenths of percent, 0-1000.
 
	if(result > 1000)
 
	result = 1000;
 
	else if(result < -1000)
 
	result = -1000;
 

	
 
	// Return feedback
 
	return result;
 
}
 

	
 

	
src/pwmout.c
Show inline comments
 
@@ -47,90 +47,60 @@ void pwmout_init(void)
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
	sConfigOC.OCMode = TIM_OCMODE_TOGGLE; //TIM_OCMODE_PWM1;
 
	sConfigOC.Pulse = 200;
 
	sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 
	sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
 
	sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 
	sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
 
	sConfigOC.OCNIdleState = TIM_OCIDLESTATE_SET;
 
	if (HAL_TIM_OC_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 
	{
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
	sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
 
	sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
 
	sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
 
	sBreakDeadTimeConfig.DeadTime = 0;
 
	sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
 
	sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
 
	sBreakDeadTimeConfig.BreakFilter = 0;
 
	sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
 
	if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK)
 
	{
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
    HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, 0, 0);
 
    HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn);
 

	
 

	
 
	HAL_TIM_OC_Start_IT(&htim17, TIM_CHANNEL_1);
 
    __HAL_TIM_ENABLE_IT(&htim17, TIM_IT_UPDATE);
 

	
 
}
 

	
 

	
 
// freaking integral term isn't compatible with both heating and cooling... due to the discard
 
// functionality. this is a problem.
 

	
 
// also duty cycling isn't working correctly...
 
void pwmout_process(int16_t duty)
 
{
 
	if(duty == 0)
 
	{
 
		HAL_GPIO_WritePin(SSR, 0);
 
		HAL_GPIO_WritePin(LED, 0);
 
		// TODO: Disable timer
 
	}
 
	if(duty < 0)
 
		duty = -duty;
 
		duty = 0;
 

	
 

	
 
	htim17.Instance->CCR1 = duty; //duty;
 
//	HAL_GPIO_TogglePin(SSR_GPIO_Port, SSR_PIN);
 

	
 
//	uint32_t ssr_output = duty; // meh
 
//
 
//	// Kill SSR once the desired on-time has elapsed
 
//	if(flash_getsettings()->val.control_mode == MODE_PID && ((HAL_GetTick() - last_ssr_on > ssr_output) || ssr_output <= 0))
 
//	{
 
//		HAL_GPIO_WritePin(SSR, 0);
 
//		HAL_GPIO_WritePin(LED, 0);
 
//	}
 
//
 
//
 
//	// Every 200ms, set the SSR on unless output is 0
 
//	if(flash_getsettings()->val.control_mode == MODE_PID && HAL_GetTick() - last_ssr_on > SSR_PERIOD)
 
//	{
 
//		// Heat or cool, if we need to
 
//		if(ssr_output > 0)
 
//		{
 
//			HAL_GPIO_WritePin(SSR, 1);
 
//			HAL_GPIO_WritePin(LED, 1);
 
//			last_ssr_on = HAL_GetTick();
 
//		}
 
//		else {
 
//			// Make sure everything is off
 
//			HAL_GPIO_WritePin(SSR, 0);
 
//			HAL_GPIO_WritePin(LED, 0);
 
//		}
 
//
 
//	}
 
}
 

	
 
TIM_HandleTypeDef* pwmout_get_tim(void)
 
{
 
	return &htim17;
 
}
src/system/flash.c
Show inline comments
 
@@ -24,69 +24,69 @@ void flash_savesettings()
 
{
 
	// Unlock flash memory
 
	HAL_FLASH_Unlock();
 
 
	// Initialize eraser to erase one page of flash
 
	FLASH_EraseInitTypeDef eraser =
 
	{
 
			.TypeErase = TYPEERASE_PAGES,
 
			.PageAddress = eeprom_emulation,
 
			.NbPages = 1,
 
	};
 
	uint32_t errvar = 0;
 
 
	// Erase flash page
 
	HAL_FLASHEx_Erase(&eraser, &errvar);
 
 
	// Write new flash data
 
	uint16_t writectr;
 
	for(writectr = 0; writectr < 128; writectr++)// 128 bytes data
 
	{
 
		HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+writectr,settings.data[writectr]);
 
	}
 
 
	// Write magic value to flash
 
	HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+FLASH_MAGIC_LOC,FLASH_MAGIC_VALUE);
 
 
	// Lock flash memory
 
	HAL_FLASH_Lock();
 
	HAL_Delay(2);
 
}
 
 
 
// Restore configuration from flash memory, if any was previously saved
 
void flash_restoresettings(void)
 
{
 
	// Check for magic flash value
 
	if(eeprom_emulation[FLASH_MAGIC_LOC] == FLASH_MAGIC_VALUE)
 
	{
 
		// Read page of flash into settings structure
 
		uint16_t readctr = 0;
 
		for(readctr = 0; readctr < 128; readctr++)
 
		{
 
			settings.data[readctr] = eeprom_emulation[readctr];
 
		}
 
	}
 
	// No data in flash! Set defaults here
 
	else
 
	{
 
		settings.val.k_p = 10;
 
		settings.val.k_i = 0;
 
		settings.val.k_p = 100;
 
		settings.val.k_i = 2;
 
		settings.val.k_d = 0;
 
		settings.val.windup_guard = 300;
 
		settings.val.sensor_type = 1;
 
		//torestore.values.can_id = 22;
 
	}
 
}
 
 
 
// Accessor to retrieve settings structure
 
inline therm_settings_t* flash_getsettings(void)
 
{
 
	return &settings;
 
}
 
 
inline therm_status_t* runtime_status(void)
 
{
 
	return &status;
 
}
 
src/system/interrupts.c
Show inline comments
 
@@ -28,53 +28,61 @@ void EXTI15_10_IRQHandler(void)
 
{
 
  HAL_GPIO_EXTI_IRQHandler(SW_BTN_Pin);
 
}
 
 
uint32_t last_button_press = 0;
 
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 
{
 
	switch(GPIO_Pin)
 
	{
 
		case SW_BTN_Pin:
 
		{
 
//			if(HAL_GetTick() > last_button_press + 100)
 
//			{
 
//				HAL_GPIO_TogglePin(LED_RED);
 
//				HAL_GPIO_TogglePin(GATE_DRIVE);
 
//				last_button_press = HAL_GetTick();
 
//			}
 
		} break;
 
	}
 
}
 
 
void TIM1_TRG_COM_TIM17_IRQHandler(void)
 
{
 
  /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 0 */
 
 
  /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 0 */
 
 
  HAL_TIM_IRQHandler(pwmout_get_tim());
 
  /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 1 */
 
 
  /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 1 */
 
}
 
 
 
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
 
{
 
	if(htim == pwmout_get_tim())
 
	{
 
		HAL_GPIO_WritePin(SSR, 0);
 
		HAL_GPIO_WritePin(LED, 0);
 
	}
 
}
 
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
 
{
 
	if(htim == pwmout_get_tim())
 
	{
 
		HAL_GPIO_WritePin(LED, 1);
 
		HAL_GPIO_WritePin(SSR, 1);
 
		if(htim->Instance->CCR1 == 0)
 
		{
 
			HAL_GPIO_WritePin(LED, 0);
 
			HAL_GPIO_WritePin(SSR, 0);
 
		}
 
		else
 
		{
 
			HAL_GPIO_WritePin(LED, 1);
 
			HAL_GPIO_WritePin(SSR, 1);
 
		}
 
	}
 
}
 
0 comments (0 inline, 0 general)