foxBMS  1.1.2
The foxBMS Battery Management System API Documentation
bal_strategy_history.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file bal_strategy_history.c
44  * @author foxBMS Team
45  * @date 2020-05-29 (date of creation)
46  * @updated 2020-07-31 (date of last update)
47  * @ingroup APPLICATION
48  * @prefix BAL
49  *
50  * @brief Driver for the Balancing module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "bal_strategy_history.h"
56 
57 #include "battery_cell_cfg.h"
58 
59 #include "bms.h"
60 #include "database.h"
61 #include "os.h"
62 #include "state_estimation.h"
63 
64 /*========== Macros and Definitions =========================================*/
65 
66 /*========== Static Constant and Variable Definitions =======================*/
67 /** local storage of the #DATA_BLOCK_BALANCING_CONTROL_s table */
69 /** local storage of the #DATA_BLOCK_CELL_VOLTAGE_s table */
71 
72 /** contains the state of the contactor state machine */
74  .timer = 0,
75  .stateRequest = BAL_STATE_NO_REQUEST,
77  .substate = BAL_ENTRY,
78  .lastState = BAL_STATEMACH_UNINITIALIZED,
79  .lastSubstate = 0,
80  .triggerEntry = 0,
81  .errorRequestCounter = 0,
82  .initializationFinished = STD_NOT_OK,
83  .active = false,
84  .balancingThreshold = BAL_THRESHOLD_mV + BAL_HYSTERESIS_mV,
85  .balancingAllowed = true,
86  .balancingGlobalAllowed = false,
87 };
88 
89 /*========== Extern Constant and Variable Definitions =======================*/
90 
91 /*========== Static Function Prototypes =====================================*/
92 /** Activates history based balancing */
93 static void BAL_ActivateBalancing(void);
94 
95 /**
96  * @brief Deactivates history based balancing
97  * @details The balancing state of all cells in all strings set to inactivate
98  * (that is 0) and the delta charge is set to 0 As. The balancing
99  * enable bit is deactivate (that is 0).
100  */
101 static void BAL_Deactivate(void);
102 
103 /**
104  * @brief State machine subfunction to check if balancing is allowed
105  * @details Checks if balancing is allowed. If it is it transfers in the actual
106  * balancing state.
107  */
108 static void BAL_ProcessStateCheckBalancing(void);
109 
110 /** State machine subfunction to balance the battery cell */
111 static void BAL_ProcessStateBalancing(void);
112 
113 /** State machine subfunction to check for voltage imbalances */
114 static bool BAL_CheckImbalances(void);
115 
116 /** State machine subfunction to compute the imbalance of all cells */
117 static void BAL_ComputeImbalances(void);
118 
119 /*========== Static Function Implementations ================================*/
120 
121 static void BAL_ActivateBalancing(void) {
122  float cellBalancingCurrent = 0.0f;
123  uint32_t difference = 0;
124 
126 
127  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
128  uint16_t nrBalancedCells = 0u;
129  for (uint8_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
130  if (bal_state.balancingAllowed == false) {
131  bal_balancing.balancingState[s][c] = 0;
132  } else {
133  if (bal_balancing.deltaCharge_mAs[s][c] > 0) {
134  bal_balancing.balancingState[s][c] = 1;
135  nrBalancedCells++;
136  cellBalancingCurrent = ((float)(bal_cellvoltage.cellVoltage_mV[s][c])) /
138  difference = (BAL_STATEMACH_BALANCINGTIME_100ms / 10) * (uint32_t)(cellBalancingCurrent);
139  bal_state.active = true;
141  /* we are working with unsigned integers */
142  if (difference > bal_balancing.deltaCharge_mAs[s][c]) {
143  bal_balancing.deltaCharge_mAs[s][c] = 0;
144  } else {
145  bal_balancing.deltaCharge_mAs[s][c] -= difference;
146  }
147  } else {
148  bal_balancing.balancingState[s][c] = 0;
149  }
150  }
151  }
152  bal_balancing.nrBalancedCells[s] = nrBalancedCells;
153  }
154 
156 }
157 
158 static void BAL_Deactivate(void) {
159  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
160  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
161  bal_balancing.balancingState[s][c] = 0;
162  bal_balancing.deltaCharge_mAs[s][c] = 0;
163  }
165  }
167  bal_state.active = false;
168 
170 }
171 
173  if (bal_state.substate == BAL_ENTRY) {
174  if (bal_state.balancingGlobalAllowed == false) {
175  if (bal_state.active == true) {
176  BAL_Deactivate();
177  }
178  bal_state.active = false;
180  } else {
182  }
183 
185  return;
186  } else if (bal_state.substate == BAL_CHECK_IMBALANCES) {
187  if (bal_state.active == true) {
188  BAL_Deactivate();
189  }
190  if (BAL_CheckImbalances() == true) {
193  } else {
195  }
197  return;
198  } else if (bal_state.substate == BAL_COMPUTE_IMBALANCES) {
203  } else {
205  }
207  return;
208  }
209 }
210 
211 static void BAL_ProcessStateBalancing(void) {
212  bool activateBalancing = true;
213 
214  if (bal_state.substate == BAL_ENTRY) {
215  if (bal_state.balancingGlobalAllowed == false) {
216  if (bal_state.active == true) {
217  BAL_Deactivate();
218  }
219  bal_state.active = false;
221  } else {
223  }
225  return;
226  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
228  DATA_READ_DATA(&bal_minmax);
230  /* do not balance under a certain voltage level */
231  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
232  if ((bal_minmax.minimumCellVoltage_mV[s] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
234  (BAL_CheckImbalances() == false) || (bal_state.balancingGlobalAllowed == false)) {
235  activateBalancing = false;
236  if (bal_state.active == true) {
237  BAL_Deactivate();
238  }
241  return;
242  }
243  }
244 
245  if (activateBalancing == true) {
247  }
248  return;
249  }
250 }
251 static bool BAL_CheckImbalances(void) {
252  bool retVal = false;
253 
254  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
255  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
256  if (bal_balancing.deltaCharge_mAs[s][c] > 0) {
257  retVal = true;
258  }
259  }
260  }
261 
262  return retVal;
263 }
264 
265 static void BAL_ComputeImbalances(void) {
266  uint16_t voltageMin = 0;
267  uint16_t minVoltageIndex = 0;
268  float SOC = 0.0f;
269  uint32_t DOD = 0;
270  uint32_t maxDOD = 0;
271 
273 
274  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
275  voltageMin = bal_cellvoltage.cellVoltage_mV[s][0];
276  minVoltageIndex = 0;
277 
278  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
279  if (bal_cellvoltage.cellVoltage_mV[s][c] <= voltageMin) {
280  voltageMin = bal_cellvoltage.cellVoltage_mV[s][c];
281  minVoltageIndex = c;
282  }
283  }
284 
285  SOC = SOC_GetFromVoltage(((float)(bal_cellvoltage.cellVoltage_mV[s][minVoltageIndex])) / 1000.0f);
286  maxDOD = BC_CAPACITY_mAh * (uint32_t)((1.0f - SOC) * 3600.0f);
287  bal_balancing.deltaCharge_mAs[s][minVoltageIndex] = 0;
288 
289  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
290  if (c != minVoltageIndex) {
291  if (bal_cellvoltage.cellVoltage_mV[s][c] >= (voltageMin + bal_state.balancingThreshold)) {
292  SOC = SOC_GetFromVoltage(((float)(bal_cellvoltage.cellVoltage_mV[s][c])) / 1000.0f);
293  DOD = BC_CAPACITY_mAh * (uint32_t)((1.0f - SOC) * 3600.0f);
294  bal_balancing.deltaCharge_mAs[s][c] = (maxDOD - DOD);
295  }
296  }
297  }
298  }
299 
301 }
302 
303 /*========== Extern Function Implementations ================================*/
306 }
307 
309  BAL_RETURN_TYPE_e retVal = BAL_OK;
310 
312  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
313 
314  if (retVal == BAL_OK) {
315  bal_state.stateRequest = stateRequest;
316  }
318 
319  return retVal;
320 }
321 
322 extern void BAL_Trigger(void) {
324 
325  /* Check re-entrance of function */
326  if (BAL_CheckReEntrance(&bal_state) > 0u) {
327  return;
328  }
329 
330  if (bal_state.timer > 0u) {
331  if ((--bal_state.timer) > 0) {
333  return; /* handle state machine only if timer has elapsed */
334  }
335  }
336 
337  switch (bal_state.state) {
340  stateRequest = BAL_TransferStateRequest(&bal_state);
341  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
342  break;
347  break;
351  break;
355  break;
359  break;
360  default:
361  /* invalid state */
363  break;
364  }
366 }
367 
368 /*========== Externalized Static Function Implementations (Unit Test) =======*/
369 #ifdef UNITY_UNIT_TEST
370 extern BAL_STATEMACH_e BAL_GetState(void) {
371  return bal_state.state;
372 }
373 #endif
374 
375 /*================== Getter for static Variables (Unit Test) ==============*/
376 #ifdef UNITY_UNIT_TEST
377 extern DATA_BLOCK_BALANCING_CONTROL_s *TEST_BAL_GetBalancingControl(void) {
378  return &bal_balancing;
379 }
380 
381 extern BAL_STATE_s *TEST_BAL_GetBalancingState(void) {
382  return &bal_state;
383 }
384 #endif
STD_RETURN_TYPE_e BAL_Init(DATA_BLOCK_BALANCING_CONTROL_s *pControl)
Generic initialization function for the balancing module.
Definition: bal.c:143
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:69
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:164
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:151
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:97
void BAL_ProcessStateInitialized(BAL_STATE_s *currentState)
State machine subfunction to transfer from an initalized state to "running" states of th state machin...
Definition: bal.c:171
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:82
BAL_RETURN_TYPE_e BAL_CheckStateRequest(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
checks the state requests that are made.
Definition: bal.c:109
enum BAL_STATEMACH BAL_STATEMACH_e
@ BAL_STATEMACH_BALANCE
Definition: bal.h:74
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:70
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:71
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:72
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:73
enum BAL_STATE_REQUEST BAL_STATE_REQUEST_e
@ BAL_OK
Definition: bal.h:113
@ BAL_ACTIVATE_BALANCING
Definition: bal.h:91
@ BAL_CHECK_IMBALANCES
Definition: bal.h:89
@ BAL_ENTRY
Definition: bal.h:88
@ BAL_COMPUTE_IMBALANCES
Definition: bal.h:90
@ BAL_STATE_NO_REQUEST
Definition: bal.h:106
enum BAL_STATEMACH_SUB BAL_STATEMACH_SUB_e
enum BAL_RETURN_TYPE BAL_RETURN_TYPE_e
#define BAL_LOWER_VOLTAGE_LIMIT_mV
Definition: bal_cfg.h:79
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:63
#define BAL_THRESHOLD_mV
Definition: bal_cfg.h:73
#define BAL_UPPER_TEMPERATURE_LIMIT_ddegC
Definition: bal_cfg.h:82
#define BAL_STATEMACH_BALANCINGTIME_100ms
Definition: bal_cfg.h:70
#define BAL_HYSTERESIS_mV
Definition: bal_cfg.h:76
static DATA_BLOCK_CELL_VOLTAGE_s bal_cellvoltage
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
static void BAL_ActivateBalancing(void)
static BAL_STATE_s bal_state
static void BAL_ProcessStateBalancing(void)
BAL_RETURN_TYPE_e BAL_SetStateRequest(BAL_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable bal_state.
static bool BAL_CheckImbalances(void)
static DATA_BLOCK_BALANCING_CONTROL_s bal_balancing
static void BAL_Deactivate(void)
Deactivates history based balancing.
void BAL_Trigger(void)
trigger function for the BAL driver state machine.
static void BAL_ComputeImbalances(void)
static void BAL_ProcessStateCheckBalancing(void)
State machine subfunction to check if balancing is allowed.
Header for the history-based balancing strategy module.
Configuration of the battery cell (e.g., minimum and maximum cell voltage)
#define BC_CAPACITY_mAh
Cell capacity used for SOC calculation.
#define BS_BALANCING_RESISTANCE_ohm
#define BS_NR_OF_STRINGS
#define BS_NR_OF_BAT_CELLS
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1272
bms driver header
@ BMS_AT_REST
Definition: bms.h:69
Database module header.
#define DATA_READ_DATA(...)
Definition: database.h:76
#define DATA_WRITE_DATA(...)
Definition: database.h:86
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:77
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:73
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
@ STD_NOT_OK
Definition: fstd_types.h:73
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
Implementation of the tasks used by the system, headers.
float SOC_GetFromVoltage(int16_t voltage_mV)
look-up table for SOC initialization
Definition: soc_counting.c:368
Header for state-estimation module responsible for the estimation of state-of-charge (SOC),...
Definition: bal.h:128
BAL_STATEMACH_SUB_e substate
Definition: bal.h:132
uint8_t triggerEntry
Definition: bal.h:135
bool balancingGlobalAllowed
Definition: bal.h:141
uint16_t timer
Definition: bal.h:129
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:130
bool balancingAllowed
Definition: bal.h:140
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:137
BAL_STATEMACH_e state
Definition: bal.h:131
bool active
Definition: bal.h:138
int32_t balancingThreshold
Definition: bal.h:139
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:110
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:255
uint16_t nrBalancedCells[BS_NR_OF_STRINGS]
Definition: database_cfg.h:257
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:256
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:251
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:123
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:120
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:179
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:163
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:166