foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
bal_strategy_voltage.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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_voltage.c
44  * @author foxBMS Team
45  * @date 2020-05-29 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup APPLICATION
49  * @prefix BAL
50  *
51  * @brief Driver for the Balancing module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "bal_strategy_voltage.h"
57 
58 #include "battery_cell_cfg.h"
59 
60 #include "bms.h"
61 #include "database.h"
62 #include "os.h"
63 
64 #include <stdbool.h>
65 #include <stdint.h>
66 
67 /*========== Macros and Definitions =========================================*/
68 
69 /*========== Static Constant and Variable Definitions =======================*/
70 /** local storage of the #DATA_BLOCK_BALANCING_CONTROL_s table */
72 
73 /**
74  * @brief contains the state of the contactor state machine
75  */
77  .timer = 0,
78  .stateRequest = BAL_STATE_NO_REQUEST,
80  .substate = BAL_ENTRY,
81  .lastState = BAL_STATEMACH_UNINITIALIZED,
82  .lastSubstate = 0,
83  .triggerEntry = 0,
84  .errorRequestCounter = 0,
85  .initializationFinished = STD_NOT_OK,
86  .active = false,
87  .balancingThreshold = BAL_DEFAULT_THRESHOLD_mV + BAL_HYSTERESIS_mV,
88  .balancingAllowed = true,
89  .balancingGlobalAllowed = false,
90 };
91 
92 /*========== Extern Constant and Variable Definitions =======================*/
93 
94 /*========== Static Function Prototypes =====================================*/
95 /**
96  * @brief Activates voltage based balancing
97  * @details TODO
98  */
99 static bool BAL_ActivateBalancing(void);
100 
101 /**
102  * @brief Deactivates voltage based balancing
103  * @details The balancing state of all cells in all strings set to inactivate
104  * (that is 0) and the delta charge is set to 0 As. The balancing
105  * enable bit is deactivate (that is 0).
106  */
107 static void BAL_Deactivate(void);
108 
109 /**
110  * @brief State machine subfunction to check if balancing is allowed
111  * @details Checks if balancing is allowed. If it is it transfers in the actual
112  * balancing state.
113  */
114 static void BAL_ProcessStateCheckBalancing(BAL_STATE_REQUEST_e state_request);
115 
116 /**
117  * @brief State machine subfunction to balance the battery cell
118  * @details TODO
119  */
120 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request);
121 
122 /*========== Static Function Implementations ================================*/
123 static bool BAL_ActivateBalancing(void) {
124  bool finished = true;
126  /* Database entry is declared static, to place it in the data segment and not on the stack */
128 
129  DATA_READ_DATA(&cellVoltage, &minMax);
130 
131  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
132  int16_t min = minMax.minimumCellVoltage_mV[s];
133  uint16_t nrBalancedCells = 0u;
134  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
135  for (uint8_t cb = 0u; cb < BS_NR_OF_CELL_BLOCKS_PER_MODULE; cb++) {
136  if (cellVoltage.cellVoltage_mV[s][m][cb] > (min + bal_state.balancingThreshold)) {
138  finished = false;
139  /* set without hysteresis so that we now balance all cells that are below the initial threshold */
141  bal_state.active = true;
143  nrBalancedCells++;
144  } else {
146  }
147  }
148  }
149  bal_balancing.nrBalancedCells[s] = nrBalancedCells;
150  }
152 
153  return finished;
154 }
155 
156 static void BAL_Deactivate(void) {
157  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
158  for (uint16_t i = 0u; i < BS_NR_OF_CELL_BLOCKS_PER_STRING; i++) {
159  bal_balancing.balancingState[s][i] = 0;
160  bal_balancing.deltaCharge_mAs[s][i] = 0;
161  }
163  }
165  bal_state.active = false;
166 
168 }
169 
171  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
172  bal_state.balancingAllowed = false;
173  }
174  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
176  }
177 
179 
180  if ((bal_state.balancingAllowed == false) || (bal_state.balancingGlobalAllowed == false)) {
181  BAL_Deactivate();
182  bal_state.active = false;
183  } else {
187  }
188  }
189 }
190 
191 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request) {
192  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
193  bal_state.balancingAllowed = false;
194  }
195  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
197  }
198 
199  if (bal_state.balancingGlobalAllowed == false) {
200  if (bal_state.active == true) {
201  BAL_Deactivate();
202  }
203  bal_state.active = false;
207  return;
208  }
209 
210  if (bal_state.substate == BAL_ENTRY) {
211  if (bal_state.balancingAllowed == false) {
212  if (bal_state.active == true) {
213  BAL_Deactivate();
214  }
215  bal_state.active = false;
218  } else {
220  }
222  return;
226  DATA_READ_DATA(&checkMinMax);
227  /* stop balancing if minimum voltage is below minimum threshold or
228  * maximum cell temperature breached upper temperature limit */
229  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
230  if ((checkMinMax.minimumCellVoltage_mV[s] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
232  if (bal_state.active == true) {
233  BAL_Deactivate();
234  }
237  }
238  }
240  return;
241  } else if (bal_state.substate == BAL_CHECK_CURRENT) {
244  } else {
245  if (bal_state.active == true) {
246  BAL_Deactivate();
247  }
250  }
252  return;
253  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
254  if (bal_state.balancingAllowed == false) {
255  if (bal_state.active == true) {
256  BAL_Deactivate();
257  }
258  bal_state.active = false;
261  } else {
262  if (BAL_ActivateBalancing() == true) {
263  /* set threshold with hysteresis in order to prevent too early reenabling of balancing */
267  } else {
270  }
271  }
273  return;
274  }
275 }
276 
277 /*========== Extern Function Implementations ================================*/
280 }
281 
283  BAL_RETURN_TYPE_e retVal = BAL_OK;
284 
286  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
287 
288  if (retVal == BAL_OK) {
289  bal_state.stateRequest = stateRequest;
290  }
292 
293  return retVal;
294 }
295 
296 extern void BAL_Trigger(void) {
298 
299  /* Check re-entrance of function */
300  if (BAL_CheckReEntrance(&bal_state) > 0u) {
301  return;
302  }
303 
304  if (bal_state.timer > 0u) {
305  if ((--bal_state.timer) > 0) {
307  return; /* handle state machine only if timer has elapsed */
308  }
309  }
310 
311  switch (bal_state.state) {
314  stateRequest = BAL_TransferStateRequest(&bal_state);
315  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
316  break;
321  break;
325  break;
328  stateRequest = BAL_TransferStateRequest(&bal_state);
329  BAL_ProcessStateCheckBalancing(stateRequest);
330  break;
333  /* Check if balancing is still allowed */
334  stateRequest = BAL_TransferStateRequest(&bal_state);
335  BAL_ProcessStateBalancing(stateRequest);
336  break;
337  default:
338  /* invalid state */
340  break;
341  }
343 }
344 
345 /*================== Getter for static Variables (Unit Test) ==============*/
346 #ifdef UNITY_UNIT_TEST
348  return &bal_balancing;
349 }
350 
352  return &bal_state;
353 }
354 #endif
355 
356 /*========== Externalized Static Function Implementations (Unit Test) =======*/
357 #ifdef UNITY_UNIT_TEST
359  return bal_state.state;
360 }
361 #endif
STD_RETURN_TYPE_e BAL_Init(DATA_BLOCK_BALANCING_CONTROL_s *pControl)
Generic initialization function for the balancing module.
Definition: bal.c:151
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:73
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:174
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:160
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:103
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:182
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:87
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:116
BAL_RETURN_TYPE_e
Definition: bal.h:115
@ BAL_OK
Definition: bal.h:116
BAL_STATE_REQUEST_e
Definition: bal.h:102
@ BAL_STATE_NO_REQUEST
Definition: bal.h:109
@ BAL_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:106
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:105
BAL_STATEMACH_e
Definition: bal.h:71
@ BAL_STATEMACH_BALANCE
Definition: bal.h:77
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:73
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:74
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:75
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:76
@ BAL_ACTIVATE_BALANCING
Definition: bal.h:94
@ BAL_ENTRY
Definition: bal.h:91
@ BAL_CHECK_LOWEST_VOLTAGE
Definition: bal.h:95
@ BAL_CHECK_CURRENT
Definition: bal.h:96
int32_t BAL_GetBalancingThreshold_mV(void)
get balancing threshold
Definition: bal_cfg.c:88
#define BAL_LOWER_VOLTAGE_LIMIT_mV
Definition: bal_cfg.h:87
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:65
#define BAL_DEFAULT_THRESHOLD_mV
Definition: bal_cfg.h:75
#define BAL_UPPER_TEMPERATURE_LIMIT_ddegC
Definition: bal_cfg.h:90
#define BAL_STATEMACH_BALANCINGTIME_100ms
Definition: bal_cfg.h:72
#define BAL_HYSTERESIS_mV
Definition: bal_cfg.h:84
static void BAL_ProcessStateCheckBalancing(BAL_STATE_REQUEST_e state_request)
State machine subfunction to check if balancing is allowed.
DATA_BLOCK_BALANCING_CONTROL_s * TEST_BAL_GetBalancingControl(void)
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
static BAL_STATE_s bal_state
contains the state of the contactor state machine
BAL_STATEMACH_e BAL_GetState(void)
static bool BAL_ActivateBalancing(void)
Activates voltage based balancing.
BAL_RETURN_TYPE_e BAL_SetStateRequest(BAL_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable bal_state.
static DATA_BLOCK_BALANCING_CONTROL_s bal_balancing
static void BAL_Deactivate(void)
Deactivates voltage based balancing.
static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request)
State machine subfunction to balance the battery cell.
void BAL_Trigger(void)
trigger function for the BAL driver state machine.
BAL_STATE_s * TEST_BAL_GetBalancingState(void)
Header for the voltage-based balancing strategy module.
Configuration of the battery cell (e.g., minimum and maximum cell voltage)
#define BS_NR_OF_CELL_BLOCKS_PER_MODULE
number of cells per module
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define BS_NR_OF_CELL_BLOCKS_PER_STRING
#define BS_NR_OF_MODULES_PER_STRING
number of modules in a string
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1586
bms driver header
@ BMS_AT_REST
Definition: bms.h:76
Database module header.
#define DATA_READ_DATA(...)
Definition: database.h:86
#define DATA_WRITE_DATA(...)
Definition: database.h:96
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:83
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:99
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:89
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:251
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:129
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_NOT_OK
Definition: fstd_types.h:84
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:154
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:150
int32_t balancingThreshold
Definition: bal.h:142
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:133
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:140
uint8_t triggerEntry
Definition: bal.h:138
bool active
Definition: bal.h:141
bool balancingGlobalAllowed
Definition: bal.h:144
BAL_STATEMACH_SUB_e substate
Definition: bal.h:135
bool balancingAllowed
Definition: bal.h:143
uint16_t timer
Definition: bal.h:132
BAL_STATEMACH_e state
Definition: bal.h:134
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:262
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:255
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:260
uint16_t nrBalancedCells[BS_NR_OF_STRINGS]
Definition: database_cfg.h:263
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING][BS_NR_OF_CELL_BLOCKS_PER_MODULE]
Definition: database_cfg.h:139
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:135
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:125
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:183
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:167
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:170