| 1 | |
|---|
| 2 | |
|---|
| 3 | #include "sysdata.h" |
|---|
| 4 | #include "esr.h" |
|---|
| 5 | #include <stdlib.h> |
|---|
| 6 | #include "main.h" |
|---|
| 7 | #include "battery_voltage.h" |
|---|
| 8 | #include "fast_current.h" |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | int32_t current_buffer[SAMPLE_ARRAY_SIZE]; |
|---|
| 13 | int32_t voltage_buffer[SAMPLE_ARRAY_SIZE]; |
|---|
| 14 | //int32_t current_buffer_fast[SAMPLE_ARRAY_SIZE]; |
|---|
| 15 | //int32_t voltage_buffer_fast[SAMPLE_ARRAY_SIZE]; |
|---|
| 16 | |
|---|
| 17 | extern uint16_t adc12Data[SAMPLE_ARRAY_SIZE][2]; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | int16_t ESR_Exec(void) |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | static int32_t last_refresh; |
|---|
| 25 | int x; |
|---|
| 26 | |
|---|
| 27 | //Anzeige vor wieviel Sekunden zuletzt aktualisiert wurd. |
|---|
| 28 | sys_data.s.values.esrCalcTime = sys_data.s.values.onTime - last_refresh; |
|---|
| 29 | |
|---|
| 30 | for (x=SAMPLE_ARRAY_SIZE-1; x>0; x--) |
|---|
| 31 | { |
|---|
| 32 | current_buffer[x] = current_buffer[x-1]; |
|---|
| 33 | voltage_buffer[x] = voltage_buffer[x-1]; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | // Neue Werte ins array aufnehmen |
|---|
| 37 | current_buffer[0] = sys_data.s.values.batteryCurrent; |
|---|
| 38 | voltage_buffer[0] = sys_data.s.values.batteryVoltage; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | //Suche Min und max werte im Array |
|---|
| 42 | int32_t minU=INT32_MAX; |
|---|
| 43 | int32_t maxU=0; |
|---|
| 44 | int32_t minI=INT32_MAX; |
|---|
| 45 | int32_t maxI=0; |
|---|
| 46 | int32_t minIPos = -1; |
|---|
| 47 | int32_t maxdIPos = -1; |
|---|
| 48 | int32_t minUPos = -1; |
|---|
| 49 | int32_t maxUPos = -1; |
|---|
| 50 | |
|---|
| 51 | //Suche min und max werte |
|---|
| 52 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| 53 | { |
|---|
| 54 | if (abs(current_buffer[x]) < minI) { minI = abs(current_buffer[x]); minIPos = x; } |
|---|
| 55 | if (abs(current_buffer[x]) >= maxI) { maxI = abs(current_buffer[x]); maxdIPos = x; } |
|---|
| 56 | if (abs(voltage_buffer[x]) < minU) { minU = abs(voltage_buffer[x]); minUPos = x; } |
|---|
| 57 | if (abs(voltage_buffer[x]) > maxU) { maxU = abs(voltage_buffer[x]); maxUPos = x; } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | //Suche Zeitpunkt der größten Änderung in I |
|---|
| 62 | |
|---|
| 63 | //Delta berechnen |
|---|
| 64 | int32_t dI = abs (maxI - minI); |
|---|
| 65 | int32_t dU = abs (maxU - minU); |
|---|
| 66 | |
|---|
| 67 | //Minimale Belastung Prüfen ob es genügent Änderungen gab |
|---|
| 68 | // 1/20 des Nennstroms |
|---|
| 69 | // Bei 100Ah Batterie mit 0,5 Std discharge --> 50A --> /20 =2,5 A |
|---|
| 70 | int32_t min_dI; |
|---|
| 71 | min_dI = sys_data.s.parameter.cellCapacity / sys_data.s.parameter.cellRatedDischargeTime; //Nennlaststrom in mA |
|---|
| 72 | min_dI = min_dI / 20 ; |
|---|
| 73 | |
|---|
| 74 | int32_t min_dU = 25; |
|---|
| 75 | |
|---|
| 76 | if( dI < min_dI) |
|---|
| 77 | { |
|---|
| 78 | |
|---|
| 79 | return -1; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | //printf("dI change!\r\n"); |
|---|
| 83 | |
|---|
| 84 | if (dU < min_dU) { |
|---|
| 85 | return -2; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | //printf("dU change!\r\n"); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | int32_t dIMax=-1; |
|---|
| 92 | int32_t dIx=-1;; |
|---|
| 93 | int32_t dIMaxPos=-1; |
|---|
| 94 | |
|---|
| 95 | for (x=0; x < (SAMPLE_ARRAY_SIZE-1); x++) |
|---|
| 96 | { |
|---|
| 97 | dIx = abs(current_buffer[x+1] - current_buffer[x]); |
|---|
| 98 | if (dIx > dIMax) { dIMax = dIx; dIMaxPos = x; } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | if (dIMaxPos == SAMPLE_ARRAY_SIZE / 2) |
|---|
| 104 | { |
|---|
| 105 | //ESR berechnen! |
|---|
| 106 | sys_data.s.values.esr = ( (double)dU / (double) dI) * 1000; |
|---|
| 107 | last_refresh = sys_data.s.values.onTime; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| 111 | { |
|---|
| 112 | sys_data.s.values.current_buffer[(SAMPLE_ARRAY_SIZE-1)-x] = adc12Data[x][0]; |
|---|
| 113 | sys_data.s.values.voltage_buffer[(SAMPLE_ARRAY_SIZE-1)-x] = adc12Data[x][1]; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | } |
|---|
| 119 | return 0; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | int16_t ESR_FAST_Exec(void) |
|---|
| 124 | { |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | static int32_t last_refresh; |
|---|
| 128 | int x; |
|---|
| 129 | |
|---|
| 130 | //Anzeige vor wieviel Sekunden zuletzt aktualisiert wurd. |
|---|
| 131 | sys_data.s.values.esrCalcTime = sys_data.s.values.onTime - last_refresh; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | //Suche Min und max werte im Array |
|---|
| 136 | int32_t minU=INT32_MAX; |
|---|
| 137 | int32_t maxU=0; |
|---|
| 138 | int32_t minI=INT32_MAX; |
|---|
| 139 | int32_t maxI=0; |
|---|
| 140 | int32_t minIPos = -1; |
|---|
| 141 | int32_t maxdIPos = -1; |
|---|
| 142 | int32_t minUPos = -1; |
|---|
| 143 | int32_t maxUPos = -1; |
|---|
| 144 | |
|---|
| 145 | //Suche min und max werte |
|---|
| 146 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| 147 | { |
|---|
| 148 | if (adc12Data[x][0] < minI) { minI = adc12Data[x][0]; minIPos = x; } |
|---|
| 149 | if (adc12Data[x][0] >= maxI) { maxI = adc12Data[x][0]; maxdIPos = x; } |
|---|
| 150 | if (adc12Data[x][1] < minU) { minU = adc12Data[x][1]; minUPos = x; } |
|---|
| 151 | if (adc12Data[x][1] > maxU) { maxU = adc12Data[x][1]; maxUPos = x; } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | //Suche Zeitpunkt der größten Änderung in I |
|---|
| 156 | |
|---|
| 157 | //Delta berechnen |
|---|
| 158 | int32_t dI = maxI - minI; |
|---|
| 159 | int32_t dU = maxU - minU; |
|---|
| 160 | |
|---|
| 161 | //Umrechnung in mV / mA |
|---|
| 162 | dI = dI * ((int64_t) VREF / FAST_CURRENT_SHUNT_RESISTOR / FAST_CURRENT_I_SENSE_GAIN / FAST_CURRENT_ADC_RESOLUTION); |
|---|
| 163 | dI = dI * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktor / 1000000.0); |
|---|
| 164 | |
|---|
| 165 | dU = dU * VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER / BATTERY_VOLTAGE_ADC_RESOLUTION ; |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | //Minimale Belastung Prüfen ob es genügent Änderungen gab |
|---|
| 169 | // 1/20 des Nennstroms |
|---|
| 170 | // Bei 100Ah Batterie mit 0,5 Std discharge --> 50A --> /20 =2,5 A |
|---|
| 171 | int32_t min_dI; |
|---|
| 172 | min_dI = sys_data.s.parameter.cellCapacity / sys_data.s.parameter.cellRatedDischargeTime; //Nennlaststrom in mA |
|---|
| 173 | min_dI = min_dI / 10 ; |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | int32_t min_dU = 10; |
|---|
| 177 | |
|---|
| 178 | if( abs(dI) < min_dI) |
|---|
| 179 | { |
|---|
| 180 | |
|---|
| 181 | return -1; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | //printf("dI change!\r\n"); |
|---|
| 185 | |
|---|
| 186 | if (abs(dU) < min_dU) { |
|---|
| 187 | return -2; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | //printf("dU change!\r\n"); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | int32_t dIMax=-1; |
|---|
| 194 | int32_t dIx=-1;; |
|---|
| 195 | int32_t dIMaxPos=-1; |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | //Finde Position der flanke |
|---|
| 200 | for (x=0; x < (SAMPLE_ARRAY_SIZE-1); x++) |
|---|
| 201 | { |
|---|
| 202 | dIx = adc12Data[x+1][0] - adc12Data[x][0]; |
|---|
| 203 | if (dIx > dIMax) { dIMax = dIx; dIMaxPos = x; } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | if ((dIMaxPos < 5 ) || (dIMaxPos > (SAMPLE_ARRAY_SIZE-5) )) |
|---|
| 207 | { |
|---|
| 208 | return -3; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | //ESR berechnen! |
|---|
| 213 | sys_data.s.values.esr_fast = ( (double)dU / (double) dI) * 1000; |
|---|
| 214 | last_refresh = sys_data.s.values.onTime; |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| 218 | { |
|---|
| 219 | sys_data.s.values.current_buffer_fast[x] = (int32_t) adc12Data[x][0] - FAST_CURRENT_ADC_OFFSET ; |
|---|
| 220 | sys_data.s.values.voltage_buffer_fast[x] = (int32_t) adc12Data[x][1] - BATTERY_VOLTAGE_ADC_OFFSET ; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | return 0; |
|---|
| 228 | } |
|---|