//
// PWM Out: generate PWM waveform to control factory
//
#include "pwmout.h"
#include "gpio.h"
#include "flash.h"
static uint32_t last_ssr_on = 0;
void pwmout_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
// Configure LED GPIO pins
GPIO_InitStruct.Pin = SSR_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(SSR_GPIO_Port, &GPIO_InitStruct);
}
// 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(float duty)
{
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);
}
}
}