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