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