updates
This commit is contained in:
@@ -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;
|
||||
|
||||
// 3. Proportional Calculation
|
||||
/* Proportional Calculation */
|
||||
float p_term = KP * (float)error;
|
||||
|
||||
// 4. Adjust the Duty Cycle
|
||||
static float current_duty = 500.0f; // Start at 50%
|
||||
/* Adjust the Duty Cycle */
|
||||
static float current_duty = 32000.0f; // Start at 50%
|
||||
current_duty += p_term;
|
||||
|
||||
// 5. Anti-Windup / Saturation (Crucial for bidirectional)
|
||||
// Prevents the PWM from trying to go to -50% or 200%
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
../Core/Src/main.c:131:5:main 5
|
||||
../Core/Src/main.c:237:6:SystemClock_Config 3
|
||||
../Core/Src/main.c:284:13:MX_ADC1_Init 4
|
||||
../Core/Src/main.c:352:13:MX_ADC2_Init 4
|
||||
../Core/Src/main.c:420:13:MX_TIM2_Init 4
|
||||
../Core/Src/main.c:465:13:MX_TIM16_Init 5
|
||||
../Core/Src/main.c:528:13:MX_USART2_UART_Init 5
|
||||
../Core/Src/main.c:576:13:MX_GPIO_Init 1
|
||||
../Core/Src/main.c:603:6:Control_Loop_Update 5
|
||||
../Core/Src/main.c:630:6:MA_Init 2
|
||||
../Core/Src/main.c:641:10:MA_Update 1
|
||||
../Core/Src/main.c:662:10:get_actual_vdda 3
|
||||
../Core/Src/main.c:685:10:get_divider_input_mv 1
|
||||
../Core/Src/main.c:698:6:voltage_conversion_task 2
|
||||
../Core/Src/main.c:737:6:voltage_conversion_task_no_tx 1
|
||||
../Core/Src/main.c:743:6:serial_number_task 3
|
||||
../Core/Src/main.c:781:6:adc_task 1
|
||||
../Core/Src/main.c:795:6:power_switch 2
|
||||
../Core/Src/main.c:811:6:HAL_UART_TxCpltCallback 1
|
||||
../Core/Src/main.c:817:6:HAL_UART_RxCpltCallback 18
|
||||
../Core/Src/main.c:970:6:Error_Handler 1
|
||||
|
||||
BIN
Debug/Core/Src/main.o
Normal file
BIN
Debug/Core/Src/main.o
Normal file
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
../Core/Src/main.c:131:5:main 8 static
|
||||
../Core/Src/main.c:237:6:SystemClock_Config 88 static
|
||||
../Core/Src/main.c:284:13:MX_ADC1_Init 56 static
|
||||
../Core/Src/main.c:352:13:MX_ADC2_Init 40 static
|
||||
../Core/Src/main.c:420:13:MX_TIM2_Init 40 static
|
||||
../Core/Src/main.c:465:13:MX_TIM16_Init 88 static
|
||||
../Core/Src/main.c:528:13:MX_USART2_UART_Init 8 static
|
||||
../Core/Src/main.c:576:13:MX_GPIO_Init 40 static
|
||||
../Core/Src/main.c:603:6:Control_Loop_Update 24 static
|
||||
../Core/Src/main.c:630:6:MA_Init 24 static
|
||||
../Core/Src/main.c:641:10:MA_Update 16 static
|
||||
../Core/Src/main.c:662:10:get_actual_vdda 24 static
|
||||
../Core/Src/main.c:685:10:get_divider_input_mv 48 static
|
||||
../Core/Src/main.c:698:6:voltage_conversion_task 8 static
|
||||
../Core/Src/main.c:737:6:voltage_conversion_task_no_tx 8 static
|
||||
../Core/Src/main.c:743:6:serial_number_task 8 static
|
||||
../Core/Src/main.c:781:6:adc_task 8 static
|
||||
../Core/Src/main.c:795:6:power_switch 16 static
|
||||
../Core/Src/main.c:811:6:HAL_UART_TxCpltCallback 16 static
|
||||
../Core/Src/main.c:817:6:HAL_UART_RxCpltCallback 16 static
|
||||
../Core/Src/main.c:970:6:Error_Handler 4 static,ignoring_inline_asm
|
||||
|
||||
BIN
Debug/POWER_SWITCH.elf
Normal file
BIN
Debug/POWER_SWITCH.elf
Normal file
Binary file not shown.
26848
Debug/POWER_SWITCH.list
26848
Debug/POWER_SWITCH.list
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user