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
 
@@ -497,7 +497,7 @@ void display_process(void)
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
//                flash_save(set);
 
            	flash_savesettings();
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
@@ -529,6 +529,7 @@ void display_process(void)
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
                flash_savesettings();
 
            }
 
            else {
 
                user_input_signed(&set->val.setpoint_brew);
 
@@ -559,6 +560,7 @@ void display_process(void)
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
                flash_savesettings();
 
            }
 
            else {
 
                user_input_signed(&set->val.setpoint_brew);
src/main.c
Show inline comments
 
@@ -52,7 +52,7 @@ int main(void)
 
    uint32_t last_1hz = 0;
 
    uint32_t last_5hz = 0;
 
 
	float duty = 0.0;
 
	int16_t duty = 0;
 
 
	while (1)
 
	{
 
@@ -72,7 +72,6 @@ int main(void)
 
 
        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();
 
        }
 
@@ -80,7 +79,6 @@ int main(void)
 
        // 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();
 
        }
src/pid.c
Show inline comments
 
@@ -35,7 +35,7 @@ float pid_process(void)
 
		// 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?
 
@@ -43,9 +43,9 @@ float pid_process(void)
 

	
 
		// 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
 
	{
 
@@ -64,7 +64,8 @@ int16_t pid_update(void)
 
	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;
 
@@ -78,7 +79,7 @@ int16_t pid_update(void)
 
	// 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)
 
@@ -86,6 +87,10 @@ int16_t pid_update(void)
 
	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)
src/pwmout.c
Show inline comments
 
@@ -92,42 +92,12 @@ void pwmout_process(int16_t duty)
 
	{
 
		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)
src/system/flash.c
Show inline comments
 
@@ -69,8 +69,8 @@ void flash_restoresettings(void)
 
	// 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;
src/system/interrupts.c
Show inline comments
 
@@ -73,8 +73,16 @@ void HAL_TIM_PeriodElapsedCallback(TIM_H
 
{
 
	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)