foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
bal_strategy_voltage.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_voltage.c
44  * @author foxBMS Team
45  * @date 2020-05-29 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.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 /*========== Macros and Definitions =========================================*/
65 
66 /*========== Static Constant and Variable Definitions =======================*/
67 /** local storage of the #DATA_BLOCK_BALANCING_CONTROL_s table */
69 
70 /**
71  * @brief contains the state of the contactor state machine
72  */
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_DEFAULT_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 /**
93  * @brief Activates voltage based balancing
94  * @details TODO
95  */
96 static bool BAL_ActivateBalancing(void);
97 
98 /**
99  * @brief Deactivates voltage based balancing
100  * @details The balancing state of all cells in all strings set to inactivate
101  * (that is 0) and the delta charge is set to 0 As. The balancing
102  * enable bit is deactivate (that is 0).
103  */
104 static void BAL_Deactivate(void);
105 
106 /**
107  * @brief State machine subfunction to check if balancing is allowed
108  * @details Checks if balancing is allowed. If it is it transfers in the actual
109  * balancing state.
110  */
111 static void BAL_ProcessStateCheckBalancing(BAL_STATE_REQUEST_e state_request);
112 
113 /**
114  * @brief State machine subfunction to balance the battery cell
115  * @details TODO
116  */
117 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request);
118 
119 /*========== Static Function Implementations ================================*/
120 static bool BAL_ActivateBalancing(void) {
121  bool finished = true;
123  /* Database entry is declared static, to place it in the data segment and not on the stack */
125 
126  DATA_READ_DATA(&cellvoltage, &minMax);
127 
128  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
129  int16_t min = minMax.minimumCellVoltage_mV[s];
130  uint16_t nrBalancedCells = 0u;
131  for (uint8_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_STRING; c++) {
132  if (cellvoltage.cellVoltage_mV[s][c] > (min + bal_state.balancingThreshold)) {
133  bal_balancing.balancingState[s][c] = 1;
134  finished = false;
135  /* set without hysteresis so that we now balance all cells that are below the initial threshold */
137  bal_state.active = true;
139  nrBalancedCells++;
140  } else {
141  bal_balancing.balancingState[s][c] = 0;
142  }
143  }
144  bal_balancing.nrBalancedCells[s] = nrBalancedCells;
145  }
147 
148  return finished;
149 }
150 
151 static void BAL_Deactivate(void) {
152  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
153  for (uint16_t i = 0u; i < BS_NR_OF_CELL_BLOCKS_PER_STRING; i++) {
154  bal_balancing.balancingState[s][i] = 0;
155  bal_balancing.deltaCharge_mAs[s][i] = 0;
156  }
158  }
160  bal_state.active = false;
161 
163 }
164 
166  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
167  bal_state.balancingAllowed = false;
168  }
169  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
171  }
172 
174 
175  if ((bal_state.balancingAllowed == false) || (bal_state.balancingGlobalAllowed == false)) {
176  BAL_Deactivate();
177  bal_state.active = false;
178  } else {
182  }
183  }
184 }
185 
186 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request) {
187  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
188  bal_state.balancingAllowed = false;
189  }
190  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
192  }
193 
194  if (bal_state.balancingGlobalAllowed == false) {
195  if (bal_state.active == true) {
196  BAL_Deactivate();
197  }
198  bal_state.active = false;
202  return;
203  }
204 
205  if (bal_state.substate == BAL_ENTRY) {
206  if (bal_state.balancingAllowed == false) {
207  if (bal_state.active == true) {
208  BAL_Deactivate();
209  }
210  bal_state.active = false;
213  } else {
215  }
217  return;
221  DATA_READ_DATA(&checkMinMax);
222  /* stop balancing if minimum voltage is below minimum threshold or
223  * maximum cell temperature breached upper temperature limit */
224  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
225  if ((checkMinMax.minimumCellVoltage_mV[s] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
227  if (bal_state.active == true) {
228  BAL_Deactivate();
229  }
232  }
233  }
235  return;
236  } else if (bal_state.substate == BAL_CHECK_CURRENT) {
239  } else {
240  if (bal_state.active == true) {
241  BAL_Deactivate();
242  }
245  }
247  return;
248  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
249  if (bal_state.balancingAllowed == false) {
250  if (bal_state.active == true) {
251  BAL_Deactivate();
252  }
253  bal_state.active = false;
256  } else {
257  if (BAL_ActivateBalancing() == true) {
258  /* set threshold with hysteresis in order to prevent too early reenabling of balancing */
262  } else {
265  }
266  }
268  return;
269  }
270 }
271 
272 /*========== Extern Function Implementations ================================*/
275 }
276 
278  BAL_RETURN_TYPE_e retVal = BAL_OK;
279 
281  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
282 
283  if (retVal == BAL_OK) {
284  bal_state.stateRequest = stateRequest;
285  }
287 
288  return retVal;
289 }
290 
291 extern void BAL_Trigger(void) {
293 
294  /* Check re-entrance of function */
295  if (BAL_CheckReEntrance(&bal_state) > 0u) {
296  return;
297  }
298 
299  if (bal_state.timer > 0u) {
300  if ((--bal_state.timer) > 0) {
302  return; /* handle state machine only if timer has elapsed */
303  }
304  }
305 
306  switch (bal_state.state) {
309  stateRequest = BAL_TransferStateRequest(&bal_state);
310  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
311  break;
316  break;
320  break;
323  stateRequest = BAL_TransferStateRequest(&bal_state);
324  BAL_ProcessStateCheckBalancing(stateRequest);
325  break;
328  /* Check if balancing is still allowed */
329  stateRequest = BAL_TransferStateRequest(&bal_state);
330  BAL_ProcessStateBalancing(stateRequest);
331  break;
332  default:
333  /* invalid state */
335  break;
336  }
338 }
339 
340 /*========== Externalized Static Function Implementations (Unit Test) =======*/
341 #ifdef UNITY_UNIT_TEST
343  return bal_state.state;
344 }
345 #endif
346 
347 /*================== Getter for static Variables (Unit Test) ==============*/
348 #ifdef UNITY_UNIT_TEST
350  return &bal_balancing;
351 }
352 
354  return &bal_state;
355 }
356 #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_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:104
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:103
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_ACTIVATE_BALANCING
Definition: bal.h:92
@ BAL_ENTRY
Definition: bal.h:89
@ BAL_CHECK_LOWEST_VOLTAGE
Definition: bal.h:93
@ BAL_CHECK_CURRENT
Definition: bal.h:94
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
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_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:1274
bms driver header
@ BMS_AT_REST
Definition: bms.h:70
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:237
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:115
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
@ STD_NOT_OK
Definition: fstd_types.h:83
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
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