[3] | 1 | /** |
---|
| 2 | ****************************************************************************** |
---|
| 3 | * @file balancer.c |
---|
[8] | 4 | * @author ECS, Zed Kazharov |
---|
[3] | 5 | * @version V1.0.0 |
---|
[8] | 6 | * @date 03.01.2023 |
---|
[3] | 7 | * @brief BALANCER Modul |
---|
| 8 | * Beschreibung in Header |
---|
| 9 | ****************************************************************************** |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | // --- INCLUDES ---------------------------------------------------------------- |
---|
| 13 | #include <stdio.h> |
---|
| 14 | #include "balancer.h" |
---|
| 15 | #include "main.h" |
---|
| 16 | #include "sysdata.h" |
---|
| 17 | #include "tim.h" |
---|
| 18 | #include "dac.h" |
---|
| 19 | #include "comp.h" |
---|
| 20 | #include "led.h" |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | //--- GGF. EXTERNE VARIABLEN --------------------------------------------------- |
---|
| 24 | |
---|
| 25 | //--- LOKALE DEFINES - bitte hier dokumentieren -------------------------------- |
---|
| 26 | //Imax = 20A |
---|
| 27 | //Verstrkung = 100 |
---|
| 28 | //Shunt = 0,001 Ohm |
---|
| 29 | //U dac = Imax * Rshunt * Verstrkung --> 20 A * 0,001 Ohm * 100 = 2.0V |
---|
| 30 | //Dac Value = Udac / (Uref / 4096) |
---|
| 31 | //#define MAX_DAC_VALUE 3276 |
---|
| 32 | //Zur Zeit reduzieren wir den max. Wert weil der Strommessverstrker langsam ist und zu spt den max Wert ausgibt |
---|
| 33 | //Daher zur Zeit nur 1,7V statt 2.0V |
---|
| 34 | #define MAX_DAC_VALUE 1500 //2300 |
---|
| 35 | |
---|
| 36 | //MAX Timer Value um nicht mehr laufenden DC DC Wandler zu erkennen |
---|
| 37 | //gemessen 1500 Periode bei 5V/12 und Sysclock 64Mhz |
---|
| 38 | //um Faktor 10 vergrert |
---|
| 39 | #define MAX_TIMER_VALUE 500 |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #define MIN_OUTPUT_VOLTAGE 10000 //mV |
---|
| 44 | #define MAX_OUTPUT_VOLTAGE 68000 //mv //Messgenauikeit ca. +- 2-3V |
---|
| 45 | #define MAX_ERROR 100 // |
---|
| 46 | #define RESTART_TIMEOUT 100 //ms |
---|
| 47 | #define BALANCER_REGULATION_TIME 10 //Alle 10mS ein Schritt |
---|
| 48 | #define CONVERTER_MIN_POWER 300 //300 von 3000 = 10% |
---|
| 49 | //--- LOKALE TYPE DEFS - bitte hier dokumentieren------------------------------- |
---|
| 50 | |
---|
| 51 | //--- DEFINATIONEN GLOBALER VARIABLEN - Bitte in Header dokumentieren ---------- |
---|
| 52 | |
---|
| 53 | //--- LOKALE VARIABLEN - bitte hier dokumentieren ------------------------------ |
---|
| 54 | |
---|
| 55 | uint32_t restartCounter; |
---|
| 56 | uint32_t startImpulsCounter; |
---|
| 57 | uint32_t balancer_running; |
---|
| 58 | uint32_t secondCounter; |
---|
| 59 | uint32_t balancerRegulationCounter= BALANCER_REGULATION_TIME; |
---|
| 60 | //--- LOKALE FUNKTIONS PROTOTYPEN ---------------------------------------------- |
---|
| 61 | void BALANCER_SetStartImpulse(void); |
---|
| 62 | |
---|
| 63 | //--- LOKALE FUNKTIONEN - bitte hier dokumentieren ----------------------------- |
---|
| 64 | |
---|
| 65 | //--- GLOBALE FUNKTIONEN - bitte in Header dokumentieren------------------------ |
---|
| 66 | void BALANCER_Init(void) |
---|
| 67 | { |
---|
| 68 | |
---|
| 69 | // Starte timer zur Messung der Charge Time und der Charge Transfer Time |
---|
| 70 | // Charge Time bedeutet die Zeit zum Aufbau des Magnedfeldes. Also vom Einschalten |
---|
| 71 | // des Mosfets bis zum erreichen der Maximal Stroms. |
---|
| 72 | //HAL_TIM_Base_Start_IT(&htim16); |
---|
| 73 | //HAL_TIM_Base_Start_IT(&htim17); |
---|
| 74 | |
---|
| 75 | HAL_TIM_IC_Start(&htim1,TIM_CHANNEL_1); |
---|
| 76 | HAL_TIM_IC_Start(&htim1,TIM_CHANNEL_2); |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0) != HAL_OK) |
---|
| 83 | { |
---|
| 84 | /* Setting value Error */ |
---|
| 85 | printf("HAL_DAC_SetValue ERROR\n"); |
---|
| 86 | return; |
---|
| 87 | } |
---|
| 88 | printf("HAL_DAC_SetValue OK\n"); |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK) |
---|
| 92 | { |
---|
| 93 | /* Start Error */ |
---|
| 94 | printf("HAL_DAC_Start ERROR\n"); |
---|
| 95 | return; |
---|
| 96 | } |
---|
| 97 | printf("HAL_DAC_Start OK\n"); |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | //Comparator fr Strom |
---|
| 101 | if(HAL_COMP_Start(&hcomp1) != HAL_OK) |
---|
| 102 | { |
---|
| 103 | /* Initialization Error */ |
---|
| 104 | printf("HAL_COMP1_Start ERROR\n"); |
---|
| 105 | return; |
---|
| 106 | } |
---|
| 107 | printf("HAL_COMP1_Start OK\n"); |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | //Comparator fr Charge Transfer |
---|
| 111 | if(HAL_COMP_Start(&hcomp2) != HAL_OK) |
---|
| 112 | { |
---|
| 113 | /* Initialization Error */ |
---|
| 114 | printf("HAL_COMP2_Start ERROR\n"); |
---|
| 115 | return; |
---|
| 116 | } |
---|
| 117 | printf("HAL_COMP2_Start OK\n"); |
---|
| 118 | printf("BALANCER_Init OK\n"); |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | uint32_t counterValue; |
---|
| 122 | |
---|
| 123 | void BALANCER_Exec() |
---|
| 124 | { |
---|
| 125 | uint32_t val1 = HAL_TIM_ReadCapturedValue(&htim1,TIM_CHANNEL_1); |
---|
| 126 | uint32_t val2 = HAL_TIM_ReadCapturedValue(&htim1,TIM_CHANNEL_2); |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | //Val1 representiert die gesamte Periode von Ipeak Abschaltung zu Ipeak Abschaltung (falling edge comparator 1) |
---|
| 130 | //Val2 Periode von Ipeak Abschaltung (falling edge comparator 1) bis zu ende charge transfer (comparartor 2 rising edge ) |
---|
| 131 | uint32_t periodeTime = val1; |
---|
| 132 | uint32_t chargeTransferTime = val2; |
---|
| 133 | uint32_t chargeTime = periodeTime - chargeTransferTime; |
---|
| 134 | uint32_t trafo = 2;// n2 / n1; |
---|
| 135 | uint32_t usek = ((float)chargeTime / (float)chargeTransferTime) * trafo * sysData.s.cellVoltage; |
---|
| 136 | sysData.s.chargeTime = chargeTime; |
---|
| 137 | sysData.s.chargeTransferTime = chargeTransferTime; |
---|
| 138 | sysData.s.sekVoltage = usek / 10; |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | //Wenn Balancer eingeschaltet |
---|
| 142 | if (sysData.s.balancerPower > 0) |
---|
| 143 | { |
---|
| 144 | // ----- Prfe ob Flyback converter luft ----- |
---|
| 145 | counterValue = __HAL_TIM_GET_COUNTER(&htim1); |
---|
| 146 | if (( counterValue > MAX_TIMER_VALUE) && (sysData.s.converterError == CONVERTER_ERROR_NONE) ) |
---|
| 147 | { |
---|
| 148 | restartCounter++; |
---|
| 149 | balancer_running=0; |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | if (restartCounter>0) restartCounter--; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | if (restartCounter >= RESTART_TIMEOUT) |
---|
| 158 | { |
---|
| 159 | BALANCER_SetStartImpulse(); |
---|
| 160 | restartCounter = 0; |
---|
| 161 | balancer_running=1; |
---|
| 162 | startImpulsCounter++; |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | { |
---|
| 166 | |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | if (startImpulsCounter > 10) |
---|
| 170 | { |
---|
| 171 | sysData.s.balancerPower=0; |
---|
| 172 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0); |
---|
| 173 | sysData.s.converterError = CONVERTER_ERROR_STARTUP_ERROR; |
---|
| 174 | printf("CONVERTER ERROR: CONVERTER_STARTUP ERROR!\n"); |
---|
| 175 | startImpulsCounter = 0; |
---|
| 176 | |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | secondCounter++; |
---|
| 181 | if (secondCounter > 999) |
---|
| 182 | { |
---|
| 183 | if (startImpulsCounter > 0) startImpulsCounter--; |
---|
| 184 | secondCounter = 0; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | // ----- Prfe ob Flyback converter Ausgangsspannung zu hoch ----- |
---|
| 190 | if ((usek > MAX_OUTPUT_VOLTAGE) && (balancer_running == 1)) |
---|
| 191 | { |
---|
| 192 | sysData.s.SekHvErrorCounter++; |
---|
| 193 | } |
---|
| 194 | else |
---|
| 195 | { |
---|
| 196 | if (sysData.s.SekHvErrorCounter>0) sysData.s.SekHvErrorCounter--; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | if (sysData.s.SekHvErrorCounter > MAX_ERROR) |
---|
| 200 | { |
---|
| 201 | sysData.s.balancerPower=0; |
---|
| 202 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0); |
---|
| 203 | sysData.s.converterError = CONVERTER_ERROR_OUTPUT_VOLT_TOO_HIGH; |
---|
| 204 | printf("CONVERTER ERROR: CONVERTER_ERROR_OUTPUT_VOLT_TOO_HIGH\n"); |
---|
| 205 | sysData.s.SekHvErrorCounter=0; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // ----- Prfe ob Flyback converter Ausgangsspannung zu niedrig ----- |
---|
| 209 | if ((usek < MIN_OUTPUT_VOLTAGE) && (balancer_running == 1)) |
---|
| 210 | { |
---|
| 211 | sysData.s.SekLvErrorCounter++; |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | { |
---|
| 215 | if (sysData.s.SekLvErrorCounter>0) sysData.s.SekLvErrorCounter--; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if (sysData.s.SekLvErrorCounter > MAX_ERROR) |
---|
| 219 | { |
---|
| 220 | sysData.s.balancerPower=0; |
---|
| 221 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0); |
---|
| 222 | sysData.s.converterError = CONVERTER_ERROR_OUTPUT_VOLT_TOO_LOW; |
---|
| 223 | printf("CONVERTER ERROR: CONVERTER_ERROR_OUTPUT_VOLT_TOO_LOW\n"); |
---|
| 224 | sysData.s.SekLvErrorCounter=0; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | else |
---|
| 228 | { |
---|
| 229 | //Balancer ist ausgeschaltet |
---|
| 230 | sysData.s.sekVoltage=0; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | if (balancerRegulationCounter > 0) balancerRegulationCounter--; |
---|
| 234 | |
---|
| 235 | if ((sysData.s.mode == 0) && (balancerRegulationCounter == 0)) //AUTO MODE |
---|
| 236 | { |
---|
| 237 | balancerRegulationCounter = BALANCER_REGULATION_TIME; |
---|
| 238 | if (sysData.s.cellVoltageUnfiltered > sysData.s.balancerVoltage) |
---|
| 239 | { |
---|
| 240 | if ((sysData.s.balancerPower < MAX_DAC_VALUE) && (sysData.s.converterError == CONVERTER_ERROR_NONE )) |
---|
| 241 | { |
---|
| 242 | sysData.s.balancerPower++; |
---|
| 243 | if (sysData.s.balancerPower < CONVERTER_MIN_POWER ) |
---|
| 244 | { |
---|
| 245 | sysData.s.balancerPower = CONVERTER_MIN_POWER; |
---|
| 246 | startImpulsCounter = 0; |
---|
| 247 | } |
---|
| 248 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, sysData.s.balancerPower); |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | if (sysData.s.cellVoltageUnfiltered < sysData.s.balancerVoltage) |
---|
| 252 | { |
---|
| 253 | if (sysData.s.balancerPower > 0) |
---|
| 254 | { |
---|
| 255 | sysData.s.balancerPower--; |
---|
| 256 | if( sysData.s.balancerPower < CONVERTER_MIN_POWER) sysData.s.balancerPower = 0; |
---|
| 257 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, sysData.s.balancerPower); |
---|
| 258 | |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | if (sysData.s.mode == 1) |
---|
| 264 | { |
---|
| 265 | if ((sysData.s.balancerPower < MAX_DAC_VALUE) && (sysData.s.converterError == CONVERTER_ERROR_NONE )) |
---|
| 266 | { |
---|
| 267 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, sysData.s.balancerPower); |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | if (sysData.s.balancerPower > 0) |
---|
| 274 | { |
---|
| 275 | LED_FunctionSetTimes(900,100); |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | LED_FunctionSetTimes(100,900); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | void BALANCER_SetStartImpulse() |
---|
| 289 | { |
---|
| 290 | |
---|
| 291 | //Balancer arbeitet nicht. Schalte Ausgang von Comparator um auf normalen GPIO, |
---|
| 292 | //setze ausgang auf 0, und schalte dann wieder auf Comparator, dadurch wird ein neuer Setz Impuls auf das Flip Flop gegeben. |
---|
| 293 | printf("DC DC Wandler not running, start impuls\n"); |
---|
| 294 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
---|
| 295 | GPIO_InitStruct.Pin = COMP2_OUT_DISCHARGE_Pin; |
---|
| 296 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
---|
| 297 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
| 298 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
| 299 | HAL_GPIO_Init(COMP2_OUT_DISCHARGE_GPIO_Port, &GPIO_InitStruct); |
---|
| 300 | HAL_GPIO_WritePin(COMP2_OUT_DISCHARGE_GPIO_Port, COMP2_OUT_DISCHARGE_Pin, GPIO_PIN_RESET); |
---|
| 301 | |
---|
| 302 | //Ausgang zurck auf Comparator |
---|
| 303 | GPIO_InitStruct.Pin = COMP2_OUT_DISCHARGE_Pin; |
---|
| 304 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
---|
| 305 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
| 306 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
| 307 | GPIO_InitStruct.Alternate = GPIO_AF7_COMP2; |
---|
| 308 | HAL_GPIO_Init(COMP2_OUT_DISCHARGE_GPIO_Port, &GPIO_InitStruct); |
---|
| 309 | |
---|
| 310 | } |
---|