This commit is contained in:
David Rice
2026-01-21 15:40:46 +00:00
parent 7990f118dd
commit c01006628f
7 changed files with 14283 additions and 14095 deletions

View File

@@ -21,6 +21,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32g4xx_hal.h"
#include <stdlib.h>
#define FILTER_SIZE 128 /* Must be a power of 2 for optimal bit-shifting (e.g., 8, 16, 32, 64) */
/* USER CODE END Includes */
@@ -40,6 +41,10 @@ typedef struct
#define IN_SYNC_BYTE_1 'A'
#define IN_SYNC_BYTE_2 'R'
#define MAX_PWM 63999
#define KP 0.15f /* Start small and increase slowly */
#define DEADBAND_MV 25 /* Ignore errors smaller than 25mV */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@@ -110,6 +115,7 @@ uint32_t get_divider_input_mv(uint32_t raw_adc_value, uint32_t vdda_mv);
void serial_number_task (void);
void MA_Init(MovingAverageFilter *filter);
uint16_t MA_Update(MovingAverageFilter *filter, uint16_t new_sample);
void Control_Loop_Update(uint32_t setpoint_mv, uint32_t measured_mv);
/* USER CODE END PFP */
@@ -205,10 +211,15 @@ int main(void)
if (vset_task_flag == 0xff)
{
__HAL_TIM_SET_COMPARE(&htim16, TIM_CHANNEL_1, 300);
adc_task();
vout_adc_val_av = MA_Update (&movavFilter, vout_adc_val);
voltage_conversion_task_no_tx();
Control_Loop_Update(v_target, vout_val);
}
else
{
__HAL_TIM_SET_COMPARE(&htim16, TIM_CHANNEL_1, 0);
}
/* USER CODE END WHILE */
@@ -589,34 +600,31 @@ static void MX_GPIO_Init(void)
}
/* USER CODE BEGIN 4 */
void Control_Loop_Update(uint32_t )
void Control_Loop_Update(uint32_t setpoint_mv, uint32_t measured_mv)
{
// 1. Get filtered ADC raw and convert to mV
uint16_t raw_adc = HAL_ADC_GetValue(&hadc1);
uint16_t filtered_raw = MA_Update(filter, raw_adc);
/* Positive error = need more power */
/* Negative error = need less power */
int32_t error = (int32_t)setpoint_mv - (int32_t)measured_mv;
// Conversion (Example: 12-bit ADC on 3.3V Vref)
uint16_t measured_mv = (uint16_t)(((uint32_t)filtered_raw * 3300) / 4095);
if (abs(error) < DEADBAND_MV)
{
error = 0; /* Don't change PWM if we are "close enough" */
}
// 2. Calculate Bidirectional Error
// Positive error = need more power
// Negative error = need less power
int32_t error = (int32_t)SETPOINT_MV - (int32_t)measured_mv;
/* Proportional Calculation */
float p_term = KP * (float)error;
// 3. Proportional Calculation
float p_term = KP * (float)error;
/* Adjust the Duty Cycle */
static float current_duty = 32000.0f; // Start at 50%
current_duty += p_term;
// 4. Adjust the Duty Cycle
static float current_duty = 500.0f; // Start at 50%
current_duty += p_term;
/* Anti-Windup / Saturation (Crucial for bidirectional) */
/* Prevents the PWM from trying to go to -50% or 200% */
if (current_duty > MAX_PWM) current_duty = (float)MAX_PWM;
if (current_duty < 0.0f) current_duty = 0.0f;
// 5. Anti-Windup / Saturation (Crucial for bidirectional)
// Prevents the PWM from trying to go to -50% or 200%
if (current_duty > MAX_PWM) current_duty = (float)MAX_PWM;
if (current_duty < 0.0f) current_duty = 0.0f;
// 6. Update STM32 Hardware
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (uint32_t)current_duty);
/* Update PWM */
__HAL_TIM_SET_COMPARE(&htim16, TIM_CHANNEL_1, (uint32_t)current_duty);
}
void MA_Init(MovingAverageFilter *filter)
@@ -795,7 +803,6 @@ void power_switch (uint8_t state)
else
{
vset_task_flag = 0x00;
__HAL_TIM_SET_COMPARE(&htim16, TIM_CHANNEL_1, 0);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
}
}