Avalon Firmware
 
Loading...
Searching...
No Matches
adc_utils.hpp File Reference

Header file for managing ADC sampling, compensation, and filtering for the supercapacitor control board. More...

#include <adc.h>
#include <dma.h>
#include <stdint.h>
#include <arm_math.h>
#include "supercap_def.h"
#include "main.h"

Go to the source code of this file.

Data Structures

struct  mov_avrg_filter
 Moving average filter structure. More...
 
struct  LoopCtrl_PID
 PID loop controller structure. More...
 
class  adc_manager
 Class to manage ADC hardware sampling and filtering. More...
 

Macros

#define ADC_RES   4095.0f
 ADC maximum value (12-bit)
 
#define INV_VOLT_DIV   11.0f
 Inverse of voltage divider ratio (used for voltage measurement scaling)
 
#define ADC_REF   3.3f
 Reference voltage for ADC (V)
 
#define INA186A2_GAIN   50.0f
 INA186A2 current amplifier gain (datasheet)
 
#define SHUNT_RESISTOR   0.004f
 Value of current sensing shunt resistor (Ohms)
 
#define VCAP_GAIN   ADC_REF * INV_VOLT_DIV / ADC_RES
 Voltage measurement gain: V = (ADC / 4095) * 3.3 * 11.
 
#define VBAT_GAIN   ADC_REF * INV_VOLT_DIV / ADC_RES
 
#define IBAT_GAIN   -(ADC_REF / SHUNT_RESISTOR / INA186A2_GAIN / ADC_RES)
 Current measurement gain from each source, sign indicates direction.
 
#define ISOURCE_GAIN   -(ADC_REF / SHUNT_RESISTOR / INA186A2_GAIN / ADC_RES)
 
#define ICAP_GAIN   -(ADC_REF / SHUNT_RESISTOR / INA186A2_GAIN / ADC_RES)
 
#define ADC1_CHANNELS   2
 
#define ADC2_CHANNELS   3
 
#define ADC_FIRST_BUFFER   0
 
#define ADC_SECOND_BUFFER   1
 
#define ADC_BUFFER_DEPTH   16
 
#define GET_COMPENSATED_ADC(ADC_VAL, NAME)
 Helper macro for applying gain + offset compensation on raw ADC readings.
 

Enumerations

enum  adc_names {
  I_cap , V_cap , V_bat , I_chassis ,
  I_bat
}
 Enum for identifying ADC channels. More...
 
enum  pid_names {
  P_bat , P_chassis , I_capa , V_cap_max ,
  V_cap_min
}
 Enum for identifying PID loops. More...
 

Variables

uint16_t adc1_samples [2][16][2]
 Raw buffer for ADC1.
 
uint16_t adc2_samples [2][16][3]
 Raw buffer for ADC2.
 
uint16_t ADC_filtered_data [5]
 Filtered data per channel.
 
mov_avrg_filter filters [5]
 Filter states per channel.
 
float adc_gains [5]
 Gain correction per channel.
 
float adc_offsets [5]
 Offset correction per channel.
 

Detailed Description

Header file for managing ADC sampling, compensation, and filtering for the supercapacitor control board.

Defines gain/offset macros, buffer sizes, filtering structures, and the ADC manager class.

Created on: Jul 5, 2024 Author: Yassine Bakkali

Macro Definition Documentation

◆ ADC1_CHANNELS

#define ADC1_CHANNELS   2

◆ ADC2_CHANNELS

#define ADC2_CHANNELS   3

◆ ADC_BUFFER_DEPTH

#define ADC_BUFFER_DEPTH   16

◆ ADC_FIRST_BUFFER

#define ADC_FIRST_BUFFER   0

◆ ADC_REF

#define ADC_REF   3.3f

Reference voltage for ADC (V)

◆ ADC_RES

#define ADC_RES   4095.0f

ADC maximum value (12-bit)

◆ ADC_SECOND_BUFFER

#define ADC_SECOND_BUFFER   1

◆ GET_COMPENSATED_ADC

#define GET_COMPENSATED_ADC ( ADC_VAL,
NAME )
Value:
(((float)(ADC_VAL)) * adc_gains[NAME]) + adc_offsets[NAME]
float adc_gains[5]
Gain correction per channel.
Definition adc_utils.cpp:26
float adc_offsets[5]
Offset correction per channel.
Definition adc_utils.cpp:27

Helper macro for applying gain + offset compensation on raw ADC readings.

◆ IBAT_GAIN

#define IBAT_GAIN   -(ADC_REF / SHUNT_RESISTOR / INA186A2_GAIN / ADC_RES)

Current measurement gain from each source, sign indicates direction.

◆ ICAP_GAIN

#define ICAP_GAIN   -(ADC_REF / SHUNT_RESISTOR / INA186A2_GAIN / ADC_RES)

◆ INA186A2_GAIN

#define INA186A2_GAIN   50.0f

INA186A2 current amplifier gain (datasheet)

◆ INV_VOLT_DIV

#define INV_VOLT_DIV   11.0f

Inverse of voltage divider ratio (used for voltage measurement scaling)

◆ ISOURCE_GAIN

#define ISOURCE_GAIN   -(ADC_REF / SHUNT_RESISTOR / INA186A2_GAIN / ADC_RES)

◆ SHUNT_RESISTOR

#define SHUNT_RESISTOR   0.004f

Value of current sensing shunt resistor (Ohms)

◆ VBAT_GAIN

#define VBAT_GAIN   ADC_REF * INV_VOLT_DIV / ADC_RES

◆ VCAP_GAIN

#define VCAP_GAIN   ADC_REF * INV_VOLT_DIV / ADC_RES

Voltage measurement gain: V = (ADC / 4095) * 3.3 * 11.

Enumeration Type Documentation

◆ adc_names

enum adc_names

Enum for identifying ADC channels.

Enumerator
I_cap 

Current from capacitor side.

V_cap 

Voltage on supercap bank.

V_bat 

Voltage from battery input.

I_chassis 

Current flowing to chassis.

I_bat 

Current from PMM to board.

◆ pid_names

enum pid_names

Enum for identifying PID loops.

Enumerator
P_bat 
P_chassis 
I_capa 
V_cap_max 
V_cap_min 

Variable Documentation

◆ adc1_samples

uint16_t adc1_samples[2][16][2]
extern

Raw buffer for ADC1.

◆ adc2_samples

uint16_t adc2_samples[2][16][3]
extern

Raw buffer for ADC2.

◆ ADC_filtered_data

uint16_t ADC_filtered_data[5]
extern

Filtered data per channel.

◆ adc_gains

float adc_gains[5]
extern

Gain correction per channel.

◆ adc_offsets

float adc_offsets[5]
extern

Offset correction per channel.

◆ filters

mov_avrg_filter filters[5]
extern

Filter states per channel.