foxBMS  1.0.0
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
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
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  for (uint8_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
129  if (bal_state.balancingAllowed == false) {
130  bal_balancing.balancingState[s][c] = 0;
131  } else {
132  if (bal_balancing.deltaCharge_mAs[s][c] > 0) {
133  bal_balancing.balancingState[s][c] = 1;
134  cellBalancingCurrent = ((float)(bal_cellvoltage.cellVoltage_mV[s][c])) /
136  difference = (BAL_STATEMACH_BALANCINGTIME_100ms / 10) * (uint32_t)(cellBalancingCurrent);
137  bal_state.active = true;
139  /* we are working with unsigned integers */
140  if (difference > bal_balancing.deltaCharge_mAs[s][c]) {
141  bal_balancing.deltaCharge_mAs[s][c] = 0;
142  } else {
143  bal_balancing.deltaCharge_mAs[s][c] -= difference;
144  }
145  } else {
146  bal_balancing.balancingState[s][c] = 0;
147  }
148  }
149  }
150  }
151 
153 }
154 
155 static void BAL_Deactivate(void) {
156  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
157  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
158  bal_balancing.balancingState[s][c] = 0;
159  bal_balancing.deltaCharge_mAs[s][c] = 0;
160  }
161  }
163  bal_state.active = false;
164 
166 }
167 
169  if (bal_state.substate == BAL_ENTRY) {
170  if (bal_state.balancingGlobalAllowed == false) {
171  if (bal_state.active == true) {
172  BAL_Deactivate();
173  }
174  bal_state.active = false;
176  } else {
178  }
179 
181  return;
182  } else if (bal_state.substate == BAL_CHECK_IMBALANCES) {
183  if (bal_state.active == true) {
184  BAL_Deactivate();
185  }
186  if (BAL_CheckImbalances() == true) {
189  } else {
191  }
193  return;
194  } else if (bal_state.substate == BAL_COMPUTE_IMBALANCES) {
199  } else {
201  }
203  return;
204  }
205 }
206 
207 static void BAL_ProcessStateBalancing(void) {
208  bool activateBalancing = true;
209 
210  if (bal_state.substate == BAL_ENTRY) {
211  if (bal_state.balancingGlobalAllowed == false) {
212  if (bal_state.active == true) {
213  BAL_Deactivate();
214  }
215  bal_state.active = false;
217  } else {
219  }
221  return;
222  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
224  DATA_READ_DATA(&bal_minmax);
226  /* do not balance under a certain voltage level */
227  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
228  if ((bal_minmax.minimumCellVoltage_mV[s] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
230  (BAL_CheckImbalances() == false) || (bal_state.balancingGlobalAllowed == false)) {
231  activateBalancing = false;
232  if (bal_state.active == true) {
233  BAL_Deactivate();
234  }
237  return;
238  }
239  }
240 
241  if (activateBalancing == true) {
243  }
244  return;
245  }
246 }
247 static bool BAL_CheckImbalances(void) {
248  bool retVal = false;
249 
250  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
251  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
252  if (bal_balancing.deltaCharge_mAs[s][c] > 0) {
253  retVal = true;
254  }
255  }
256  }
257 
258  return retVal;
259 }
260 
261 static void BAL_ComputeImbalances(void) {
262  uint16_t voltageMin = 0;
263  uint16_t minVoltageIndex = 0;
264  float SOC = 0.0f;
265  uint32_t DOD = 0;
266  uint32_t maxDOD = 0;
267 
269 
270  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
271  voltageMin = bal_cellvoltage.cellVoltage_mV[s][0];
272  minVoltageIndex = 0;
273 
274  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
275  if (bal_cellvoltage.cellVoltage_mV[s][c] <= voltageMin) {
276  voltageMin = bal_cellvoltage.cellVoltage_mV[s][c];
277  minVoltageIndex = c;
278  }
279  }
280 
281  SOC = SOC_GetFromVoltage(((float)(bal_cellvoltage.cellVoltage_mV[s][minVoltageIndex])) / 1000.0f);
282  maxDOD = BC_CAPACITY_mAh * (uint32_t)((1.0f - SOC) * 3600.0f);
283  bal_balancing.deltaCharge_mAs[s][minVoltageIndex] = 0;
284 
285  for (uint16_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
286  if (c != minVoltageIndex) {
287  if (bal_cellvoltage.cellVoltage_mV[s][c] >= (voltageMin + bal_state.balancingThreshold)) {
288  SOC = SOC_GetFromVoltage(((float)(bal_cellvoltage.cellVoltage_mV[s][c])) / 1000.0f);
289  DOD = BC_CAPACITY_mAh * (uint32_t)((1.0f - SOC) * 3600.0f);
290  bal_balancing.deltaCharge_mAs[s][c] = (maxDOD - DOD);
291  }
292  }
293  }
294  }
295 
297 }
298 
299 /*========== Extern Function Implementations ================================*/
302 }
303 
305  BAL_RETURN_TYPE_e retVal = BAL_OK;
306 
308  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
309 
310  if (retVal == BAL_OK) {
311  bal_state.stateRequest = stateRequest;
312  }
314 
315  return retVal;
316 }
317 
318 extern void BAL_Trigger(void) {
320 
321  /* Check re-entrance of function */
322  if (BAL_CheckReEntrance(&bal_state) > 0u) {
323  return;
324  }
325 
326  if (bal_state.timer > 0u) {
327  if ((--bal_state.timer) > 0) {
329  return; /* handle state machine only if timer has elapsed */
330  }
331  }
332 
333  switch (bal_state.state) {
336  stateRequest = BAL_TransferStateRequest(&bal_state);
337  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
338  break;
343  break;
347  break;
351  break;
355  break;
356  default:
357  /* invalid state */
359  break;
360  }
362 }
363 
364 /*========== Externalized Static Function Implementations (Unit Test) =======*/
365 #ifdef UNITY_UNIT_TEST
366 extern BAL_STATEMACH_e BAL_GetState(void) {
367  return bal_state.state;
368 }
369 #endif
370 
371 /*================== Getter for static Variables (Unit Test) ==============*/
372 #ifdef UNITY_UNIT_TEST
373 extern DATA_BLOCK_BALANCING_CONTROL_s *TEST_BAL_GetBalancingControl(void) {
374  return &bal_balancing;
375 }
376 
377 extern BAL_STATE_s *TEST_BAL_GetBalancingState(void) {
378  return &bal_state;
379 }
380 #endif
os.h
Implementation of the tasks used by the system, headers.
BAL_STATE::balancingThreshold
int32_t balancingThreshold
Definition: bal.h:139
BAL_STATE::balancingGlobalAllowed
bool balancingGlobalAllowed
Definition: bal.h:141
BAL_STATE::triggerEntry
uint8_t triggerEntry
Definition: bal.h:135
BS_NR_OF_BAT_CELLS
#define BS_NR_OF_BAT_CELLS
Definition: battery_system_cfg.h:156
BS_BALANCING_RESISTANCE_ohm
#define BS_BALANCING_RESISTANCE_ohm
Definition: battery_system_cfg.h:134
BAL_STATE_NO_REQUEST
@ BAL_STATE_NO_REQUEST
Definition: bal.h:106
BAL_ENTRY
@ BAL_ENTRY
Definition: bal.h:88
BAL_TransferStateRequest
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:97
DATA_BLOCK_MIN_MAX::maximumTemperature_ddegC
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:178
bal_strategy_history.h
Header for the history-based balancing strategy module.
BAL_GetInitializationState
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
Definition: bal_strategy_history.c:300
BAL_CheckImbalances
static bool BAL_CheckImbalances(void)
Definition: bal_strategy_history.c:247
DATA_BLOCK_BALANCING_CONTROL::enableBalancing
uint8_t enableBalancing
Definition: database_cfg.h:250
BAL_STATE
Definition: bal.h:128
BAL_ACTIVATE_BALANCING
@ BAL_ACTIVATE_BALANCING
Definition: bal.h:91
DATA_BLOCK_MIN_MAX::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:162
BAL_STATEMACH_e
enum BAL_STATEMACH BAL_STATEMACH_e
BAL_Deactivate
static void BAL_Deactivate(void)
Deactivates history based balancing.
Definition: bal_strategy_history.c:155
BAL_ActivateBalancing
static void BAL_ActivateBalancing(void)
Definition: bal_strategy_history.c:121
STD_RETURN_TYPE_e
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
bms.h
bms driver header
BAL_LOWER_VOLTAGE_LIMIT_mV
#define BAL_LOWER_VOLTAGE_LIMIT_mV
Definition: bal_cfg.h:79
DATA_WRITE_DATA
#define DATA_WRITE_DATA(...)
Definition: database.h:82
bal_cellvoltage
static DATA_BLOCK_CELL_VOLTAGE_s bal_cellvoltage
Definition: bal_strategy_history.c:70
BAL_STATEMACH_CHECK_BALANCING
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:73
BAL_STATEMACH_BALANCE
@ BAL_STATEMACH_BALANCE
Definition: bal.h:74
SOC_GetFromVoltage
float SOC_GetFromVoltage(int16_t voltage_mV)
look-up table for SOC initialization (average, min and max).
Definition: soc_counting.c:352
BAL_CheckReEntrance
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:82
BAL_STATEMACH_INITIALIZATION
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:71
BAL_STATE::timer
uint16_t timer
Definition: bal.h:129
bal_balancing
static DATA_BLOCK_BALANCING_CONTROL_s bal_balancing
Definition: bal_strategy_history.c:68
BAL_ProcessStateInitialization
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:164
BAL_ProcessStateInitialized
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
BAL_SetStateRequest
BAL_RETURN_TYPE_e BAL_SetStateRequest(BAL_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable bal_state.
Definition: bal_strategy_history.c:304
DATA_BLOCK_BALANCING_CONTROL::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:249
FAS_ASSERT
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
OS_ExitTaskCritical
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
BAL_STATE::state
BAL_STATEMACH_e state
Definition: bal.h:131
BAL_STATE::initializationFinished
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:137
DATA_BLOCK_ID_MIN_MAX
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
DATA_BLOCK_BALANCING_CONTROL::deltaCharge_mAs
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:254
BAL_STATEMACH_UNINITIALIZED
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:70
BAL_THRESHOLD_mV
#define BAL_THRESHOLD_mV
Definition: bal_cfg.h:73
BAL_UPPER_TEMPERATURE_LIMIT_ddegC
#define BAL_UPPER_TEMPERATURE_LIMIT_ddegC
Definition: bal_cfg.h:82
OS_EnterTaskCritical
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
BMS_AT_REST
@ BMS_AT_REST
Definition: bms.h:69
state_estimation.h
Header for state-estimation module responsible for the estimation of state-of-charge (SOC),...
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
BMS_GetBatterySystemState
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1230
battery_cell_cfg.h
Configuration of the battery cell (e.g., minimum and maximum cell voltage)
DATA_BLOCK_CELL_VOLTAGE::cellVoltage_mV
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:122
BAL_Init
STD_RETURN_TYPE_e BAL_Init(DATA_BLOCK_BALANCING_CONTROL_s *pControl)
Generic initialization function for the balancing module.
Definition: bal.c:143
DATA_BLOCK_ID_BALANCING_CONTROL
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:77
DATA_BLOCK_BALANCING_CONTROL::balancingState
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:253
BAL_Trigger
void BAL_Trigger(void)
trigger function for the BAL driver state machine.
Definition: bal_strategy_history.c:318
DATA_BLOCK_CELL_VOLTAGE
Definition: database_cfg.h:115
DATA_BLOCK_MIN_MAX
Definition: database_cfg.h:158
DATA_READ_DATA
#define DATA_READ_DATA(...)
Definition: database.h:72
BAL_ProcessStateBalancing
static void BAL_ProcessStateBalancing(void)
Definition: bal_strategy_history.c:207
BAL_RETURN_TYPE_e
enum BAL_RETURN_TYPE BAL_RETURN_TYPE_e
database.h
Database module header.
BAL_SaveLastStates
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:69
BAL_STATE::active
bool active
Definition: bal.h:138
BC_CAPACITY_mAh
#define BC_CAPACITY_mAh
Cell capacity used for SOC calculation.
Definition: battery_cell_cfg.h:220
BAL_ComputeImbalances
static void BAL_ComputeImbalances(void)
Definition: bal_strategy_history.c:261
BAL_STATEMACH_SHORTTIME_100ms
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:63
BAL_STATEMACH_INITIALIZED
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:72
DATA_BLOCK_ID_CELL_VOLTAGE
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:73
BAL_ProcessStateCheckBalancing
static void BAL_ProcessStateCheckBalancing(void)
State machine subfunction to check if balancing is allowed.
Definition: bal_strategy_history.c:168
BAL_STATE::substate
BAL_STATEMACH_SUB_e substate
Definition: bal.h:132
DATA_BLOCK_MIN_MAX::minimumCellVoltage_mV
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:165
BAL_CheckStateRequest
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
BAL_STATE::stateRequest
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:130
BAL_STATEMACH_SUB_e
enum BAL_STATEMACH_SUB BAL_STATEMACH_SUB_e
BAL_STATE_REQUEST_e
enum BAL_STATE_REQUEST BAL_STATE_REQUEST_e
BAL_HYSTERESIS_mV
#define BAL_HYSTERESIS_mV
Definition: bal_cfg.h:76
BAL_COMPUTE_IMBALANCES
@ BAL_COMPUTE_IMBALANCES
Definition: bal.h:90
FAS_TRAP
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
BAL_STATE::balancingAllowed
bool balancingAllowed
Definition: bal.h:140
DATA_BLOCK_BALANCING_CONTROL
Definition: database_cfg.h:245
BAL_CHECK_IMBALANCES
@ BAL_CHECK_IMBALANCES
Definition: bal.h:89
DATA_BLOCK_CELL_VOLTAGE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:119
BAL_ProcessStateUninitalized
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:151
BAL_STATEMACH_BALANCINGTIME_100ms
#define BAL_STATEMACH_BALANCINGTIME_100ms
Definition: bal_cfg.h:70
BS_NR_OF_STRINGS
#define BS_NR_OF_STRINGS
Definition: battery_system_cfg.h:89
bal_state
static BAL_STATE_s bal_state
Definition: bal_strategy_history.c:73
BAL_OK
@ BAL_OK
Definition: bal.h:113
DATA_BLOCKHEADER::uniqueId
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:109