#include "supercap_controllers.hpp"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. | |
| #define ALPHA_PID_POWER 0.98f |
Low-pass filter alpha constant for smoothing power signal.
|
inlinestatic |
Runs all safety checks for voltage and current conditions.
Combines the voltage and current safety check routines if the system is initialized.
| 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.
| hadc | Pointer to the ADC handle that experienced overrun. |
| 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.
| hadc | Pointer to the ADC handle. |
|
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.
| pid_struct | Pointer to the PID controller structure. |
| ref | Desired reference value (setpoint). |
| feedback | Current feedback value (measurement). |
| ff_model | Optional feedforward term added to the output. |
| 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:
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.
| hhrtim | Pointer to the HRTIM handle (unused here). |
| TimerIdx | Index of the timer triggering the event (unused). |
|
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.
|
inlinestatic |
Updates the control loop at each HRTIM callback cycle.
This function is the heart of the closed-loop control logic. It:
|
inlinestatic |
First-order low pass filter.
| current_value | New input value |
| previous_value | Previously filtered output |
| alpha | Filter constant (0 < alpha < 1) |
|
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.
| filter | The filter state structure (holds index, window buffer, etc.) |
| new_sample | The latest ADC sample to be added to the buffer |
|
inlinestatic |
Safely updates the level of a safety item if required.
Changes the level only if escalating or recovering from a serious state.
| Item | Index of the safety item. |
| Level | New safety level to apply. |
|
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.
|
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).
|
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.
| 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.
|
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.
|
inlinestatic |
Internal version of loop stop (non-class variant).
Exists in addition to the class method for situations where non-member access is needed.
|
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.
| dutyRatio | Desired duty ratio (V_cap / V_bat), expected to be in [0.0, 2.0] |
|
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.
|
static |
Intermediate ADC buffer after averaging.
| float battery_current = 0.0f |
| float cap_current = 0.0f |
| 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).
| Mode_ModeTypedef current_mode = normal |
Current control mode (normal, idle, etc.)
| float duty_ratio = 0.9f |
| float duty_ratio_output |
Raw PID output for duty ratio.
|
static |
Stores last supercap current reference.
| float icap_output |
PID-calculated reference current for supercap.
|
static |
True if control is actively running.
|
static |
Internal state variables for control loop execution.
True if control loop is initialized
| uint8_t max_chassis_power = 45 |
Max allowed chassis power under RMUC rules.
| float out_duty_ratio_output |
Clamped and validated duty ratio to apply.
| float Pchassis_output |
Output variables used in control and monitoring.
Output power estimation to the chassis
|
static |
Array of PID control structures.
|
static |
Tracks whether a safety level transition occurred.
| uint8_t safetyItemLevel[8] |
Tracks safety status per monitored item.
Global safety flags indexed by Safety_DetectItemTypedef.
| float temperature = 0.0f |
Global sensing variables updated during each loop.