| [20] | 1 | |
|---|
| 2 | |
|---|
| 3 | #include "sysdata.h" |
|---|
| 4 | #include "esr.h" |
|---|
| 5 | #include <stdlib.h> |
|---|
| [25] | 6 | #include "main.h" |
|---|
| 7 | #include "battery_voltage.h" |
|---|
| 8 | #include "fast_current.h" |
|---|
| [20] | 9 | |
|---|
| 10 | |
|---|
| [25] | 11 | |
|---|
| [20] | 12 | int32_t current_buffer[SAMPLE_ARRAY_SIZE]; |
|---|
| 13 | int32_t voltage_buffer[SAMPLE_ARRAY_SIZE]; |
|---|
| [25] | 14 | //int32_t current_buffer_fast[SAMPLE_ARRAY_SIZE]; |
|---|
| 15 | //int32_t voltage_buffer_fast[SAMPLE_ARRAY_SIZE]; |
|---|
| [20] | 16 | |
|---|
| [25] | 17 | extern uint16_t adc12Data[SAMPLE_ARRAY_SIZE][2]; |
|---|
| [20] | 18 | |
|---|
| [25] | 19 | |
|---|
| [20] | 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! |
|---|
| [26] | 106 | sys_data.s.values.esr = ( (double)dU / (double) dI) * 10000; |
|---|
| [20] | 107 | last_refresh = sys_data.s.values.onTime; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| 111 | { |
|---|
| [25] | 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]; |
|---|
| [20] | 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. |
|---|
| [26] | 131 | //Aktuell erfolgt nur die Anze der low speed Methode |
|---|
| 132 | //sys_data.s.values.esrCalcTime = sys_data.s.values.onTime - last_refresh; |
|---|
| [20] | 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | //Suche Min und max werte im Array |
|---|
| 137 | int32_t minU=INT32_MAX; |
|---|
| 138 | int32_t maxU=0; |
|---|
| 139 | int32_t minI=INT32_MAX; |
|---|
| 140 | int32_t maxI=0; |
|---|
| 141 | int32_t minIPos = -1; |
|---|
| 142 | int32_t maxdIPos = -1; |
|---|
| 143 | int32_t minUPos = -1; |
|---|
| 144 | int32_t maxUPos = -1; |
|---|
| 145 | |
|---|
| 146 | //Suche min und max werte |
|---|
| 147 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| 148 | { |
|---|
| [25] | 149 | if (adc12Data[x][0] < minI) { minI = adc12Data[x][0]; minIPos = x; } |
|---|
| 150 | if (adc12Data[x][0] >= maxI) { maxI = adc12Data[x][0]; maxdIPos = x; } |
|---|
| 151 | if (adc12Data[x][1] < minU) { minU = adc12Data[x][1]; minUPos = x; } |
|---|
| 152 | if (adc12Data[x][1] > maxU) { maxU = adc12Data[x][1]; maxUPos = x; } |
|---|
| [20] | 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| [26] | 157 | |
|---|
| [20] | 158 | //Delta berechnen |
|---|
| [25] | 159 | int32_t dI = maxI - minI; |
|---|
| [26] | 160 | |
|---|
| 161 | //Nehme nicht mehr die gesamte maximale Differenz der Spannungen, sondern nehme das delt U wo auch das Delta I gemessen wurde |
|---|
| 162 | //Funktioniert nur bei Synchroner Messug von Strom und Spannung |
|---|
| 163 | //int32_t dU = maxU - minU; |
|---|
| 164 | int32_t dU = adc12Data[maxdIPos][1] - adc12Data[minIPos][1]; |
|---|
| [20] | 165 | |
|---|
| [25] | 166 | //Umrechnung in mV / mA |
|---|
| [26] | 167 | dI = dI * ((double) VREF / FAST_CURRENT_SHUNT_RESISTOR / FAST_CURRENT_I_SENSE_GAIN / FAST_CURRENT_ADC_RESOLUTION); |
|---|
| [25] | 168 | dI = dI * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktor / 1000000.0); |
|---|
| 169 | |
|---|
| [26] | 170 | dU = dU * (double )VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER / BATTERY_VOLTAGE_ADC_RESOLUTION ; |
|---|
| [25] | 171 | |
|---|
| 172 | |
|---|
| [20] | 173 | //Minimale Belastung Prüfen ob es genügent Änderungen gab |
|---|
| 174 | // 1/20 des Nennstroms |
|---|
| 175 | // Bei 100Ah Batterie mit 0,5 Std discharge --> 50A --> /20 =2,5 A |
|---|
| 176 | int32_t min_dI; |
|---|
| 177 | min_dI = sys_data.s.parameter.cellCapacity / sys_data.s.parameter.cellRatedDischargeTime; //Nennlaststrom in mA |
|---|
| [25] | 178 | min_dI = min_dI / 10 ; |
|---|
| 179 | |
|---|
| [20] | 180 | |
|---|
| [25] | 181 | int32_t min_dU = 10; |
|---|
| [20] | 182 | |
|---|
| [25] | 183 | if( abs(dI) < min_dI) |
|---|
| [20] | 184 | { |
|---|
| 185 | |
|---|
| 186 | return -1; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | //printf("dI change!\r\n"); |
|---|
| 190 | |
|---|
| [25] | 191 | if (abs(dU) < min_dU) { |
|---|
| [20] | 192 | return -2; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | //printf("dU change!\r\n"); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | int32_t dIMax=-1; |
|---|
| 199 | int32_t dIx=-1;; |
|---|
| 200 | int32_t dIMaxPos=-1; |
|---|
| 201 | |
|---|
| [25] | 202 | |
|---|
| 203 | |
|---|
| 204 | //Finde Position der flanke |
|---|
| [20] | 205 | for (x=0; x < (SAMPLE_ARRAY_SIZE-1); x++) |
|---|
| 206 | { |
|---|
| [25] | 207 | dIx = adc12Data[x+1][0] - adc12Data[x][0]; |
|---|
| [20] | 208 | if (dIx > dIMax) { dIMax = dIx; dIMaxPos = x; } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| [26] | 211 | //if ((dIMaxPos < 5 ) || (dIMaxPos > (SAMPLE_ARRAY_SIZE-5) )) |
|---|
| 212 | //{ |
|---|
| 213 | // return -3; |
|---|
| 214 | // } |
|---|
| [20] | 215 | |
|---|
| 216 | |
|---|
| [25] | 217 | //ESR berechnen! |
|---|
| [26] | 218 | sys_data.s.values.esr_fast = ( (double)dU / (double) dI) * 10000; |
|---|
| [25] | 219 | last_refresh = sys_data.s.values.onTime; |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) |
|---|
| [20] | 223 | { |
|---|
| [25] | 224 | sys_data.s.values.current_buffer_fast[x] = (int32_t) adc12Data[x][0] - FAST_CURRENT_ADC_OFFSET ; |
|---|
| 225 | sys_data.s.values.voltage_buffer_fast[x] = (int32_t) adc12Data[x][1] - BATTERY_VOLTAGE_ADC_OFFSET ; |
|---|
| 226 | } |
|---|
| [20] | 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| [25] | 231 | |
|---|
| [20] | 232 | return 0; |
|---|
| 233 | } |
|---|