source: trunk/fw_g473rct/SES/src/esr.c

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