foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
bal_strategy_history.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, 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 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup APPLICATION
49  * @prefix BAL
50  *
51  * @brief Driver for the Balancing module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "bal_strategy_history.h"
57 
58 #include "battery_cell_cfg.h"
59 
60 #include "bms.h"
61 #include "database.h"
62 #include "os.h"
63 #include "state_estimation.h"
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 /** local storage of the #DATA_BLOCK_BALANCING_CONTROL_s table */
70 /** local storage of the #DATA_BLOCK_CELL_VOLTAGE_s table */
72 
73 /** contains the state of the contactor state machine */
75  .timer = 0,
76  .stateRequest = BAL_STATE_NO_REQUEST,
78  .substate = BAL_ENTRY,
79  .lastState = BAL_STATEMACH_UNINITIALIZED,
80  .lastSubstate = 0,
81  .triggerEntry = 0,
82  .errorRequestCounter = 0,
83  .initializationFinished = STD_NOT_OK,
84  .active = false,
85  .balancingThreshold = BAL_DEFAULT_THRESHOLD_mV + BAL_HYSTERESIS_mV,
86  .balancingAllowed = true,
87  .balancingGlobalAllowed = false,
88 };
89 
90 /*========== Extern Constant and Variable Definitions =======================*/
91 
92 /*========== Static Function Prototypes =====================================*/
93 /** Activates history based balancing */
94 static void BAL_ActivateBalancing(void);
95 
96 /**
97  * @brief Deactivates history based balancing
98  * @details The balancing state of all cells in all strings set to inactivate
99  * (that is 0) and the delta charge is set to 0 As. The balancing
100  * enable bit is deactivate (that is 0).
101  */
102 static void BAL_Deactivate(void);
103 
104 /**
105  * @brief State machine subfunction to check if balancing is allowed
106  * @details Checks if balancing is allowed. If it is it transfers in the actual
107  * balancing state.
108  */
109 static void BAL_ProcessStateCheckBalancing(void);
110 
111 /** State machine subfunction to balance the battery cell */
112 static void BAL_ProcessStateBalancing(void);
113 
114 /** State machine subfunction to check for voltage imbalances */
115 static bool BAL_CheckImbalances(void);
116 
117 /** State machine subfunction to compute the imbalance of all cells */
118 static void BAL_ComputeImbalances(void);
119 
120 /*========== Static Function Implementations ================================*/
121 
122 static void BAL_ActivateBalancing(void) {
123  float cellBalancingCurrent = 0.0f;
124  uint32_t difference = 0;
125 
127 
128  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
129  uint16_t nrBalancedCells = 0u;
130  for (uint8_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_STRING; c++) {
131  if (bal_state.balancingAllowed == false) {
132  bal_balancing.balancingState[s][c] = 0;
133  } else {
134  if (bal_balancing.deltaCharge_mAs[s][c] > 0) {
135  bal_balancing.balancingState[s][c] = 1;
136  nrBalancedCells++;
137  cellBalancingCurrent = ((float)(bal_cellVoltage.cellVoltage_mV[s][c])) /
139  difference = (BAL_STATEMACH_BALANCINGTIME_100ms / 10) * (uint32_t)(cellBalancingCurrent);
140  bal_state.active = true;
142  /* we are working with unsigned integers */
143  if (difference > bal_balancing.deltaCharge_mAs[s][c]) {
144  bal_balancing.deltaCharge_mAs[s][c] = 0;
145  } else {
146  bal_balancing.deltaCharge_mAs[s][c] -= difference;
147  }
148  } else {
149  bal_balancing.balancingState[s][c] = 0;
150  }
151  }
152  }
153  bal_balancing.nrBalancedCells[s] = nrBalancedCells;
154  }
155 
157 }
158 
159 static void BAL_Deactivate(void) {
160  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
161  for (uint16_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_STRING; c++) {
162  bal_balancing.balancingState[s][c] = 0;
163  bal_balancing.deltaCharge_mAs[s][c] = 0;
164  }
166  }
168  bal_state.active = false;
169 
171 }
172 
174  if (bal_state.substate == BAL_ENTRY) {
175  if (bal_state.balancingGlobalAllowed == false) {
176  if (bal_state.active == true) {
177  BAL_Deactivate();
178  }
179  bal_state.active = false;
181  } else {
183  }
184 
186  return;
187  } else if (bal_state.substate == BAL_CHECK_IMBALANCES) {
188  if (bal_state.active == true) {
189  BAL_Deactivate();
190  }
191  if (BAL_CheckImbalances() == true) {
194  } else {
196  }
198  return;
199  } else if (bal_state.substate == BAL_COMPUTE_IMBALANCES) {
204  } else {
206  }
208  return;
209  }
210 }
211 
212 static void BAL_ProcessStateBalancing(void) {
213  bool activateBalancing = true;
214 
215  if (bal_state.substate == BAL_ENTRY) {
216  if (bal_state.balancingGlobalAllowed == false) {
217  if (bal_state.active == true) {
218  BAL_Deactivate();
219  }
220  bal_state.active = false;
222  } else {
224  }
226  return;
227  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
229  DATA_READ_DATA(&bal_minmax);
231  /* do not balance under a certain voltage level */
232  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
233  if ((bal_minmax.minimumCellVoltage_mV[s] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
235  (BAL_CheckImbalances() == false) || (bal_state.balancingGlobalAllowed == false)) {
236  activateBalancing = false;
237  if (bal_state.active == true) {
238  BAL_Deactivate();
239  }
242  return;
243  }
244  }
245 
246  if (activateBalancing == true) {
248  }
249  return;
250  }
251 }
252 static bool BAL_CheckImbalances(void) {
253  bool retVal = false;
254 
255  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
256  for (uint16_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_STRING; c++) {
257  if (bal_balancing.deltaCharge_mAs[s][c] > 0) {
258  retVal = true;
259  }
260  }
261  }
262 
263  return retVal;
264 }
265 
266 static void BAL_ComputeImbalances(void) {
267  uint16_t voltageMin = 0;
268  uint16_t minVoltageIndex = 0;
269  float SOC = 0.0f;
270  uint32_t DOD = 0;
271  uint32_t maxDOD = 0;
272 
274 
275  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
276  voltageMin = bal_cellVoltage.cellVoltage_mV[s][0];
277  minVoltageIndex = 0;
278 
279  for (uint16_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_STRING; c++) {
280  if (bal_cellVoltage.cellVoltage_mV[s][c] <= voltageMin) {
281  voltageMin = bal_cellVoltage.cellVoltage_mV[s][c];
282  minVoltageIndex = c;
283  }
284  }
285 
286  SOC = SE_GetStateOfChargeFromVoltage(((float)(bal_cellVoltage.cellVoltage_mV[s][minVoltageIndex])) / 1000.0f);
287  maxDOD = BC_CAPACITY_mAh * (uint32_t)((1.0f - SOC) * 3600.0f);
288  bal_balancing.deltaCharge_mAs[s][minVoltageIndex] = 0;
289 
290  /* update balancing threshold */
292 
293  for (uint16_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_STRING; c++) {
294  if (c != minVoltageIndex) {
295  if (bal_cellVoltage.cellVoltage_mV[s][c] >= (voltageMin + bal_state.balancingThreshold)) {
296  SOC = SE_GetStateOfChargeFromVoltage(((float)(bal_cellVoltage.cellVoltage_mV[s][c])) / 1000.0f);
297  DOD = BC_CAPACITY_mAh * (uint32_t)((1.0f - SOC) * 3600.0f);
298  bal_balancing.deltaCharge_mAs[s][c] = (maxDOD - DOD);
299  }
300  }
301  }
302  }
303 
305 }
306 
307 /*========== Extern Function Implementations ================================*/
310 }
311 
313  BAL_RETURN_TYPE_e retVal = BAL_OK;
314 
316  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
317 
318  if (retVal == BAL_OK) {
319  bal_state.stateRequest = stateRequest;
320  }
322 
323  return retVal;
324 }
325 
326 extern void BAL_Trigger(void) {
328 
329  /* Check re-entrance of function */
330  if (BAL_CheckReEntrance(&bal_state) > 0u) {
331  return;
332  }
333 
334  if (bal_state.timer > 0u) {
335  if ((--bal_state.timer) > 0) {
337  return; /* handle state machine only if timer has elapsed */
338  }
339  }
340 
341  switch (bal_state.state) {
344  stateRequest = BAL_TransferStateRequest(&bal_state);
345  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
346  break;
351  break;
355  break;
359  break;
363  break;
364  default:
365  /* invalid state */
367  break;
368  }
370 }
371 
372 /*========== Externalized Static Function Implementations (Unit Test) =======*/
373 #ifdef UNITY_UNIT_TEST
375  return bal_state.state;
376 }
377 #endif
378 
379 /*================== Getter for static Variables (Unit Test) ==============*/
380 #ifdef UNITY_UNIT_TEST
382  return &bal_balancing;
383 }
384 
386  return &bal_state;
387 }
388 #endif
STD_RETURN_TYPE_e BAL_Init(DATA_BLOCK_BALANCING_CONTROL_s *pControl)
Generic initialization function for the balancing module.
Definition: bal.c:148
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:70
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:171
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:157
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:100
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:179
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:84
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:113
BAL_RETURN_TYPE_e
Definition: bal.h:113
@ BAL_OK
Definition: bal.h:114
BAL_STATE_REQUEST_e
Definition: bal.h:100
@ BAL_STATE_NO_REQUEST
Definition: bal.h:107
BAL_STATEMACH_e
Definition: bal.h:69
@ BAL_STATEMACH_BALANCE
Definition: bal.h:75
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:71
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:72
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:73
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:74
BAL_STATEMACH_SUB_e
Definition: bal.h:88
@ BAL_ACTIVATE_BALANCING
Definition: bal.h:92
@ BAL_CHECK_IMBALANCES
Definition: bal.h:90
@ BAL_ENTRY
Definition: bal.h:89
@ BAL_COMPUTE_IMBALANCES
Definition: bal.h:91
int32_t BAL_GetBalancingThreshold_mV(void)
get balancing threshold
Definition: bal_cfg.c:86
#define BAL_LOWER_VOLTAGE_LIMIT_mV
Definition: bal_cfg.h:86
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:64
#define BAL_DEFAULT_THRESHOLD_mV
Definition: bal_cfg.h:74
#define BAL_UPPER_TEMPERATURE_LIMIT_ddegC
Definition: bal_cfg.h:89
#define BAL_STATEMACH_BALANCINGTIME_100ms
Definition: bal_cfg.h:71
#define BAL_HYSTERESIS_mV
Definition: bal_cfg.h:83
DATA_BLOCK_BALANCING_CONTROL_s * TEST_BAL_GetBalancingControl(void)
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
static void BAL_ActivateBalancing(void)
static BAL_STATE_s bal_state
BAL_STATEMACH_e BAL_GetState(void)
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 DATA_BLOCK_CELL_VOLTAGE_s bal_cellVoltage
static void BAL_ComputeImbalances(void)
BAL_STATE_s * TEST_BAL_GetBalancingState(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
Number of parallel strings in the battery pack.
#define BS_NR_OF_CELL_BLOCKS_PER_STRING
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1569
bms driver header
@ BMS_AT_REST
Definition: bms.h:72
Database module header.
#define DATA_READ_DATA(...)
Definition: database.h:83
#define DATA_WRITE_DATA(...)
Definition: database.h:93
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:80
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:78
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:76
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:248
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:126
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
@ STD_NOT_OK
Definition: fstd_types.h:83
float SE_GetStateOfChargeFromVoltage(int16_t voltage_mV)
look-up table for SOC initialization
Definition: soc_counting.c:359
Declaration of the OS wrapper interface.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:135
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:131
Header for state-estimation module responsible for the estimation of state-of-charge (SOC),...
int32_t balancingThreshold
Definition: bal.h:140
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:131
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:138
uint8_t triggerEntry
Definition: bal.h:136
bool active
Definition: bal.h:139
bool balancingGlobalAllowed
Definition: bal.h:142
BAL_STATEMACH_SUB_e substate
Definition: bal.h:133
bool balancingAllowed
Definition: bal.h:141
uint16_t timer
Definition: bal.h:130
BAL_STATEMACH_e state
Definition: bal.h:132
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:254
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:247
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:252
uint16_t nrBalancedCells[BS_NR_OF_STRINGS]
Definition: database_cfg.h:255
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:129
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:132
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:175
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:159
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:162