| 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) * 10000; | 
|---|
| 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 | //Aktuell erfolgt nur die Anze der low speed Methode | 
|---|
| 132 | //sys_data.s.values.esrCalcTime = sys_data.s.values.onTime - last_refresh; | 
|---|
| 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 | { | 
|---|
| 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; } | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | //Delta berechnen | 
|---|
| 159 | int32_t dI = maxI - minI; | 
|---|
| 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]; | 
|---|
| 165 |  | 
|---|
| 166 | //Umrechnung in mV / mA | 
|---|
| 167 | dI = dI * ((double) VREF / FAST_CURRENT_SHUNT_RESISTOR /  FAST_CURRENT_I_SENSE_GAIN /  FAST_CURRENT_ADC_RESOLUTION); | 
|---|
| 168 | dI = dI * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktor / 1000000.0); | 
|---|
| 169 |  | 
|---|
| 170 | dU = dU  * (double )VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER / BATTERY_VOLTAGE_ADC_RESOLUTION ; | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 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 | 
|---|
| 178 | min_dI = min_dI / 10 ; | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 | int32_t min_dU = 10; | 
|---|
| 182 |  | 
|---|
| 183 | if( abs(dI) < min_dI) | 
|---|
| 184 | { | 
|---|
| 185 |  | 
|---|
| 186 | return -1; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | //printf("dI change!\r\n"); | 
|---|
| 190 |  | 
|---|
| 191 | if (abs(dU) < min_dU) { | 
|---|
| 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 |  | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | //Finde Position der flanke | 
|---|
| 205 | for (x=0; x < (SAMPLE_ARRAY_SIZE-1); x++) | 
|---|
| 206 | { | 
|---|
| 207 | dIx = adc12Data[x+1][0] - adc12Data[x][0]; | 
|---|
| 208 | if (dIx > dIMax) { dIMax = dIx; dIMaxPos = x; } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | //if ((dIMaxPos < 5 ) || (dIMaxPos > (SAMPLE_ARRAY_SIZE-5) )) | 
|---|
| 212 | //{ | 
|---|
| 213 | //      return -3; | 
|---|
| 214 | // } | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 | //ESR berechnen! | 
|---|
| 218 | sys_data.s.values.esr_fast = ( (double)dU / (double) dI) * 10000; | 
|---|
| 219 | last_refresh = sys_data.s.values.onTime; | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 | for (x=0; x < SAMPLE_ARRAY_SIZE; x++) | 
|---|
| 223 | { | 
|---|
| 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 | } | 
|---|
| 227 |  | 
|---|
| 228 |  | 
|---|
| 229 |  | 
|---|
| 230 |  | 
|---|
| 231 |  | 
|---|
| 232 | return 0; | 
|---|
| 233 | } | 
|---|