Avalon Firmware
 
Loading...
Searching...
No Matches
supercap_controllers.cpp File Reference

Macros

#define ALPHA_PID_POWER   0.98f
 Low-pass filter alpha constant for smoothing power signal.
 

Functions

static float low_pass_filter (float current_value, float previous_value, float alpha)
 First-order low pass filter.
 
static void update_pid_maxpow ()
 Updates the internal PID limits for chassis power.
 
static uint16_t moving_average (mov_avrg_filter &filter, uint16_t new_sample)
 Calculates a moving average on a circular buffer filter structure.
 
static void sample_adc ()
 Samples and filters ADC data from both ADC1 and ADC2.
 
static void stop_gates_pwm ()
 Disables all HRTIM PWM outputs driving the power gates.
 
static void stop_loop ()
 Internal version of loop stop (non-class variant).
 
void softwareReset ()
 Forces a software reset of the MCU.
 
static void idle_mode ()
 Transitions the system into idle mode safely.
 
static void update_dutyCycle (float dutyRatio)
 Updates the duty cycle for both half-bridges using the HRTIM peripheral.
 
static float get_PID (LoopCtrl_PID *pid_struct, float ref, float feedback, float ff_model)
 Executes a single PID computation and clamps the result.
 
static void all_safety_checks ()
 Runs all safety checks for voltage and current conditions.
 
static void loop_update ()
 Updates the control loop at each HRTIM callback cycle.
 
static void safety_change_level (uint8_t Item, uint8_t Level)
 Safely updates the level of a safety item if required.
 
static void safety_check_voltages ()
 Performs safety checks on both chassis and supercapacitor voltages.
 
static void safety_check_currents ()
 Checks for overcurrent conditions on both the chassis and the supercapacitor.
 
void AnalogSignal_ADCDMA_OVRStop (ADC_HandleTypeDef *hadc)
 Stops the ongoing ADC DMA conversion in case of overrun.
 
void AnalogSignal_ADCDMA_OVRRecovery (ADC_HandleTypeDef *hadc)
 Recovers ADC and DMA after an overrun (OVR) condition.
 
void HAL_HRTIM_RepetitionEventCallback (HRTIM_HandleTypeDef *hhrtim, uint32_t TimerIdx)
 Main control loop callback called at each HRTIM repetition event.
 

Variables

float Pchassis_output
 Output variables used in control and monitoring.
 
float icap_output
 PID-calculated reference current for supercap.
 
float duty_ratio_output
 Raw PID output for duty ratio.
 
float out_duty_ratio_output
 Clamped and validated duty ratio to apply.
 
uint8_t max_chassis_power = 45
 Max allowed chassis power under RMUC rules.
 
float temperature = 0.0f
 Global sensing variables updated during each loop.
 
float duty_ratio = 0.9f
 
float cap_voltage = 0.0f
 
float chassis_voltage = 0.0f
 Extern global values for chassis voltage and battery current (used across control loops and diagnostics).
 
float cap_current = 0.0f
 
float battery_current = 0.0f
 
static uint16_t ADC_sampled_data [5]
 Intermediate ADC buffer after averaging.
 
static uint8_t is_init = false
 Internal state variables for control loop execution.
 
static uint8_t in_loop = false
 True if control is actively running.
 
static uint8_t safetyChangeTrigger
 Tracks whether a safety level transition occurred.
 
static LoopCtrl_PID pid [5]
 Array of PID control structures.
 
static float I_supercap_last = 0.0f
 Stores last supercap current reference.
 
Mode_ModeTypedef current_mode = normal
 Current control mode (normal, idle, etc.)
 
uint8_t safetyItemLevel [8]
 Tracks safety status per monitored item.
 

Macro Definition Documentation

◆ ALPHA_PID_POWER

#define ALPHA_PID_POWER   0.98f

Low-pass filter alpha constant for smoothing power signal.

Function Documentation

◆ all_safety_checks()

static void all_safety_checks ( )
inlinestatic

Runs all safety checks for voltage and current conditions.

Combines the voltage and current safety check routines if the system is initialized.

◆ AnalogSignal_ADCDMA_OVRRecovery()

void AnalogSignal_ADCDMA_OVRRecovery ( ADC_HandleTypeDef * hadc)

Recovers ADC and DMA after an overrun (OVR) condition.

Clears error flags and restarts the ADC and DMA modules to restore normal operation. Required to recover from transient ADC DMA overflows due to latency or missed callbacks.

Parameters
hadcPointer to the ADC handle that experienced overrun.

◆ AnalogSignal_ADCDMA_OVRStop()

void AnalogSignal_ADCDMA_OVRStop ( ADC_HandleTypeDef * hadc)

Stops the ongoing ADC DMA conversion in case of overrun.

This is a low-level emergency stop. It halts the ADC immediately to prevent further data corruption.

Parameters
hadcPointer to the ADC handle.

◆ get_PID()

static float get_PID ( LoopCtrl_PID * pid_struct,
float ref,
float feedback,
float ff_model )
inlinestatic

Executes a single PID computation and clamps the result.

Applies the PID algorithm using the CMSIS-DSP library. The output is adjusted with a feedforward term. Integral and output values are clamped to safe min/max bounds to prevent wind-up or instability.

Parameters
pid_structPointer to the PID controller structure.
refDesired reference value (setpoint).
feedbackCurrent feedback value (measurement).
ff_modelOptional feedforward term added to the output.
Returns
Final, clamped output value from the controller.

◆ HAL_HRTIM_RepetitionEventCallback()

void HAL_HRTIM_RepetitionEventCallback ( HRTIM_HandleTypeDef * hhrtim,
uint32_t TimerIdx )

Main control loop callback called at each HRTIM repetition event.

This function is tied to a high-frequency hardware timer (HRTIM). It:

  • Checks for ADC overruns and recovers if needed
  • Samples ADC data
  • Executes the main closed-loop control update

This is where the entire feedback control logic is anchored, providing real-time regulation of current, power, and duty cycles for the supercap management system.

Parameters
hhrtimPointer to the HRTIM handle (unused here).
TimerIdxIndex of the timer triggering the event (unused).

◆ idle_mode()

static void idle_mode ( )
inlinestatic

Transitions the system into idle mode safely.

Used during safety events or power-off conditions to gracefully exit the control loop. Ensures gates are off and no control loop logic executes.

◆ loop_update()

static void loop_update ( )
inlinestatic

Updates the control loop at each HRTIM callback cycle.

This function is the heart of the closed-loop control logic. It:

  • Runs safety checks
  • Reads filtered sensor values
  • Computes a desired supercap current to keep battery power below limits
  • Computes duty cycle through cascaded PID loops with voltage clamping
  • Updates PWM outputs accordingly

◆ low_pass_filter()

static float low_pass_filter ( float current_value,
float previous_value,
float alpha )
inlinestatic

First-order low pass filter.

Parameters
current_valueNew input value
previous_valuePreviously filtered output
alphaFilter constant (0 < alpha < 1)
Returns
Filtered result

◆ moving_average()

static uint16_t moving_average ( mov_avrg_filter & filter,
uint16_t new_sample )
inlinestatic

Calculates a moving average on a circular buffer filter structure.

The moving average filter stores a rolling window of past n samples and returns the average based on either the full window or current fill state. This is used for noise filtering of analog signals such as current and voltage before feeding them into PID loops.

Parameters
filterThe filter state structure (holds index, window buffer, etc.)
new_sampleThe latest ADC sample to be added to the buffer
Returns
Filtered (smoothed) 16-bit average value

◆ safety_change_level()

static void safety_change_level ( uint8_t Item,
uint8_t Level )
inlinestatic

Safely updates the level of a safety item if required.

Changes the level only if escalating or recovering from a serious state.

Parameters
ItemIndex of the safety item.
LevelNew safety level to apply.

◆ safety_check_currents()

static void safety_check_currents ( )
inlinestatic

Checks for overcurrent conditions on both the chassis and the supercapacitor.

Monitors the current flowing through the capacitor and the main gate. If the current exceeds the defined safe limits, the system transitions into idle mode.

Uses absolute values for current readings since direction doesn’t matter for safety. Only acts if the current error hasn't already reached its most critical state.

◆ safety_check_voltages()

static void safety_check_voltages ( )
inlinestatic

Performs safety checks on both chassis and supercapacitor voltages.

Uses counters for debounce behavior and disables the control loop if unsafe conditions persist (too high or low voltage on either side).

◆ sample_adc()

static void sample_adc ( )
inlinestatic

Samples and filters ADC data from both ADC1 and ADC2.

This function determines which half of the DMA double-buffer is ready for each ADC, averages multiple samples per channel (to reduce noise), and applies a moving average filter to smooth the data before it's used for PID control and safety checks.

◆ softwareReset()

void softwareReset ( )

Forces a software reset of the MCU.

Software-triggered MCU reset function (called on fatal error or remote command).

Used during critical fault recovery to completely reboot the board. Disables interrupts and enters an infinite loop until the system reset is triggered via NVIC.

◆ stop_gates_pwm()

static void stop_gates_pwm ( )
inlinestatic

Disables all HRTIM PWM outputs driving the power gates.

Forces an output disable through the HRTIM register. Used in safety shutdown and during idle transitions to prevent accidental switching activity.

◆ stop_loop()

static void stop_loop ( )
inlinestatic

Internal version of loop stop (non-class variant).

Exists in addition to the class method for situations where non-member access is needed.

◆ update_dutyCycle()

static void update_dutyCycle ( float dutyRatio)
inlinestatic

Updates the duty cycle for both half-bridges using the HRTIM peripheral.

Based on the current ratio, calculates the appropriate compare values for each timer output. The cap and chassis sides are controlled separately using a normalized (0.0–1.0+) input ratio.

Parameters
dutyRatioDesired duty ratio (V_cap / V_bat), expected to be in [0.0, 2.0]

◆ update_pid_maxpow()

static void update_pid_maxpow ( )
inlinestatic

Updates the internal PID limits for chassis power.

This function is used internally to update the maximum allowed output and integration range for the chassis power PID controller. It ensures that the controller stays within competition-legal limits by capping both the integral term and final output.

Variable Documentation

◆ ADC_sampled_data

uint16_t ADC_sampled_data[5]
static

Intermediate ADC buffer after averaging.

◆ battery_current

float battery_current = 0.0f

◆ cap_current

float cap_current = 0.0f

◆ cap_voltage

float cap_voltage = 0.0f

◆ chassis_voltage

float chassis_voltage = 0.0f

Extern global values for chassis voltage and battery current (used across control loops and diagnostics).

◆ current_mode

Mode_ModeTypedef current_mode = normal

Current control mode (normal, idle, etc.)

◆ duty_ratio

float duty_ratio = 0.9f

◆ duty_ratio_output

float duty_ratio_output

Raw PID output for duty ratio.

◆ I_supercap_last

float I_supercap_last = 0.0f
static

Stores last supercap current reference.

◆ icap_output

float icap_output

PID-calculated reference current for supercap.

◆ in_loop

uint8_t in_loop = false
static

True if control is actively running.

◆ is_init

uint8_t is_init = false
static

Internal state variables for control loop execution.

True if control loop is initialized

◆ max_chassis_power

uint8_t max_chassis_power = 45

Max allowed chassis power under RMUC rules.

◆ out_duty_ratio_output

float out_duty_ratio_output

Clamped and validated duty ratio to apply.

◆ Pchassis_output

float Pchassis_output

Output variables used in control and monitoring.

Output power estimation to the chassis

◆ pid

LoopCtrl_PID pid[5]
static

Array of PID control structures.

◆ safetyChangeTrigger

uint8_t safetyChangeTrigger
static

Tracks whether a safety level transition occurred.

◆ safetyItemLevel

uint8_t safetyItemLevel[8]

Tracks safety status per monitored item.

Global safety flags indexed by Safety_DetectItemTypedef.

◆ temperature

float temperature = 0.0f

Global sensing variables updated during each loop.