| 1 | /**
|
|---|
| 2 | ******************************************************************************
|
|---|
| 3 | * @file meas.c
|
|---|
| 4 | * @author ECS, Zed Kazharov
|
|---|
| 5 | * @version V1.0.0
|
|---|
| 6 | * @date 15-Jan-2023
|
|---|
| 7 | * @brief Measurement Modul
|
|---|
| 8 | * Beschreibung in Header
|
|---|
| 9 | ******************************************************************************
|
|---|
| 10 | */
|
|---|
| 11 |
|
|---|
| 12 | // --- INCLUDES ----------------------------------------------------------------
|
|---|
| 13 | #include "meas.h"
|
|---|
| 14 | #include "main.h"
|
|---|
| 15 | #include "adc.h"
|
|---|
| 16 | #include "stdio.h"
|
|---|
| 17 | #include "sysdata.h"
|
|---|
| 18 | //--- GGF. EXTERNE VARIABLEN ---------------------------------------------------
|
|---|
| 19 |
|
|---|
| 20 | //--- LOKALE DEFINES - bitte hier dokumentieren --------------------------------
|
|---|
| 21 | #define ADC_CONVERTED_DATA_BUFFER_SIZE 4
|
|---|
| 22 | #define VREF 2500 //in mV
|
|---|
| 23 | #define CELL_VOLTAGE_DIVIDER 2
|
|---|
| 24 | #define ADC_RESOLUTION 65536 //16 bit AD Wandler durch Oversampling
|
|---|
| 25 |
|
|---|
| 26 | #define CELL_VOLTAGE_FILTER 8 // Filterlängen in 2er-Potenzen --> Compiler optimiert
|
|---|
| 27 | #define CELL_TEMPERATURE_FILTER 8
|
|---|
| 28 | #define BALANCER_CURRENT_FILTER 8
|
|---|
| 29 | //--- LOKALE TYPE DEFS - bitte hier dokumentieren-------------------------------
|
|---|
| 30 |
|
|---|
| 31 | //--- DEFINATIONEN GLOBALER VARIABLEN - Bitte in Header dokumentieren ----------
|
|---|
| 32 |
|
|---|
| 33 | //--- LOKALE VARIABLEN - bitte hier dokumentieren ------------------------------
|
|---|
| 34 | __IO uint16_t aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE]; /* ADC group regular conversion data (array of data) */
|
|---|
| 35 | __IO uint32_t newADCDataFlag;
|
|---|
| 36 | uint32_t currentOffset;
|
|---|
| 37 | uint32_t totalMeas;
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | //--- LOKALE FUNKTIONS PROTOTYPEN ----------------------------------------------
|
|---|
| 41 | uint32_t calcCellVoltageFiltered(void);
|
|---|
| 42 | uint32_t calcCellVoltageUnfiltered(void);
|
|---|
| 43 | int32_t calcCellTemperature(void);
|
|---|
| 44 | uint32_t calcBalancerCurrent(void);
|
|---|
| 45 | extern uint32_t initRefresh;
|
|---|
| 46 |
|
|---|
| 47 | //--- LOKALE FUNKTIONEN - bitte hier dokumentieren -----------------------------
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | //--- GLOBALE FUNKTIONEN - bitte in Header dokumentieren------------------------
|
|---|
| 51 | void MEAS_Init()
|
|---|
| 52 | {
|
|---|
| 53 | if (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK)
|
|---|
| 54 | {
|
|---|
| 55 | printf("HAL_ADCEx_Calibration_Start: ERROR\n");
|
|---|
| 56 | return;
|
|---|
| 57 | }
|
|---|
| 58 | printf("HAL_ADCEx_Calibration_Start: OK\n");
|
|---|
| 59 |
|
|---|
| 60 | if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)aADCxConvertedData, ADC_CONVERTED_DATA_BUFFER_SIZE ) != HAL_OK)
|
|---|
| 61 | {
|
|---|
| 62 | /* ADC conversion start error */
|
|---|
| 63 | printf("Error HAL_ADC_Start_DMA\n");
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 | printf("HAL_ADC_Start_DMA: OK\n");
|
|---|
| 67 |
|
|---|
| 68 | //---- Offset calibrierung ---
|
|---|
| 69 | //Warte bis Mittelwert gebildet
|
|---|
| 70 | while (totalMeas < 100)
|
|---|
| 71 | {
|
|---|
| 72 | MEAS_Exec();
|
|---|
| 73 | initRefresh = 1;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | currentOffset = calcBalancerCurrent();
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void MEAS_Exec()
|
|---|
| 83 | {
|
|---|
| 84 | if (newADCDataFlag)
|
|---|
| 85 | {
|
|---|
| 86 | totalMeas++;
|
|---|
| 87 | sysData.s.cellVoltage = calcCellVoltageFiltered();
|
|---|
| 88 | sysData.s.cellVoltageUnfiltered = calcCellVoltageUnfiltered();
|
|---|
| 89 | sysData.s.cellTemperature = calcCellTemperature();
|
|---|
| 90 | sysData.s.balancerCurrent = calcBalancerCurrent() - currentOffset;
|
|---|
| 91 | newADCDataFlag=0;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * @brief Conversion complete callback in non blocking mode
|
|---|
| 98 | * @param hadc: ADC handle
|
|---|
| 99 | * @note This example shows a simple way to report end of conversion
|
|---|
| 100 | * and get conversion result. You can add your own implementation.
|
|---|
| 101 | * @retval None
|
|---|
| 102 | */
|
|---|
| 103 | void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
|
|---|
| 104 | {
|
|---|
| 105 | newADCDataFlag = 1;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | uint32_t calcCellVoltageFiltered()
|
|---|
| 110 | {
|
|---|
| 111 | static unsigned long avgsum = 0;
|
|---|
| 112 |
|
|---|
| 113 | //ADC Wert gefiltert
|
|---|
| 114 | uint32_t adcValue;
|
|---|
| 115 |
|
|---|
| 116 | //Wert umgerechnet in Spannung [mV]
|
|---|
| 117 | uint32_t u;
|
|---|
| 118 |
|
|---|
| 119 | //Filter
|
|---|
| 120 | avgsum -= avgsum / CELL_VOLTAGE_FILTER;
|
|---|
| 121 | avgsum += aADCxConvertedData[1];;
|
|---|
| 122 | adcValue = avgsum / CELL_VOLTAGE_FILTER;
|
|---|
| 123 |
|
|---|
| 124 | //Umrechnung in Spannung
|
|---|
| 125 | u = (adcValue * VREF * CELL_VOLTAGE_DIVIDER) / ADC_RESOLUTION;
|
|---|
| 126 |
|
|---|
| 127 | return u;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | uint32_t calcCellVoltageUnfiltered()
|
|---|
| 131 | {
|
|---|
| 132 | static unsigned long avgsum = 0;
|
|---|
| 133 |
|
|---|
| 134 | //ADC Wert gefiltert
|
|---|
| 135 | uint32_t adcValue;
|
|---|
| 136 |
|
|---|
| 137 | //Wert umgerechnet in Spannung [mV]
|
|---|
| 138 | uint32_t u;
|
|---|
| 139 | adcValue = aADCxConvertedData[1];;
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | //Umrechnung in Spannung
|
|---|
| 143 | u = (adcValue * VREF * CELL_VOLTAGE_DIVIDER) / ADC_RESOLUTION;
|
|---|
| 144 |
|
|---|
| 145 | return u;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | int32_t calcCellTemperature()
|
|---|
| 151 | {
|
|---|
| 152 | static unsigned long avgsum = 0;
|
|---|
| 153 |
|
|---|
| 154 | //ADC Wert gefiltert
|
|---|
| 155 | uint32_t adcValue;
|
|---|
| 156 |
|
|---|
| 157 | //Wert umgerechnet in Spannung [mV]
|
|---|
| 158 | uint32_t u;
|
|---|
| 159 |
|
|---|
| 160 | //Wert umgerechnet in [°C ]
|
|---|
| 161 | int32_t temp;
|
|---|
| 162 |
|
|---|
| 163 | //Filter
|
|---|
| 164 | avgsum -= avgsum / CELL_TEMPERATURE_FILTER;
|
|---|
| 165 | avgsum += aADCxConvertedData[2];
|
|---|
| 166 | adcValue = avgsum / CELL_TEMPERATURE_FILTER;
|
|---|
| 167 |
|
|---|
| 168 | //Umrechnung in Spannung
|
|---|
| 169 | u = (adcValue * VREF ) / ADC_RESOLUTION;
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | //Umrechnung in Grade C
|
|---|
| 173 | temp = u;
|
|---|
| 174 | temp = temp - 600;
|
|---|
| 175 | temp = temp * 100;
|
|---|
| 176 | temp = temp / 100; //Von Milligrad in Grad * 10
|
|---|
| 177 | return temp;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | uint32_t calcBalancerCurrent()
|
|---|
| 181 | {
|
|---|
| 182 | static unsigned long avgsum = 0;
|
|---|
| 183 |
|
|---|
| 184 | //ADC Wert gefiltert
|
|---|
| 185 | uint32_t adcValue;
|
|---|
| 186 |
|
|---|
| 187 | //Wert umgerechnet in Spannung [mV]
|
|---|
| 188 | uint32_t u;
|
|---|
| 189 |
|
|---|
| 190 | //Wert umgerechnet in mA
|
|---|
| 191 | int32_t i;
|
|---|
| 192 |
|
|---|
| 193 | //Filter
|
|---|
| 194 | avgsum -= avgsum / BALANCER_CURRENT_FILTER;
|
|---|
| 195 | avgsum += aADCxConvertedData[0];
|
|---|
| 196 | adcValue = avgsum / BALANCER_CURRENT_FILTER;
|
|---|
| 197 |
|
|---|
| 198 | //Umrechnung in Spannung
|
|---|
| 199 | u = (adcValue * VREF ) / ADC_RESOLUTION;
|
|---|
| 200 |
|
|---|
| 201 | //Umrechnung in Strom
|
|---|
| 202 | //u = u / 100; //Verstärkunsfaktor INA180A3
|
|---|
| 203 | //i = u / 0.001; Shunt Widerstand
|
|---|
| 204 | //Die beiden letzten Zeilen wurden zusammengefasst in
|
|---|
| 205 | i = u * 10;
|
|---|
| 206 |
|
|---|
| 207 | return i;
|
|---|
| 208 |
|
|---|
| 209 | }
|
|---|