foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
bal_strategy_voltage.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_voltage.c
44  * @author foxBMS Team
45  * @date 2020-05-29 (date of creation)
46  * @updated 2021-12-08 (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_voltage.h"
56 
57 #include "battery_cell_cfg.h"
58 
59 #include "bms.h"
60 #include "database.h"
61 #include "os.h"
62 
63 /*========== Macros and Definitions =========================================*/
64 
65 /*========== Static Constant and Variable Definitions =======================*/
66 /** local storage of the #DATA_BLOCK_BALANCING_CONTROL_s table */
68 
69 /**
70  * @brief contains the state of the contactor state machine
71  */
73  .timer = 0,
74  .stateRequest = BAL_STATE_NO_REQUEST,
76  .substate = BAL_ENTRY,
77  .lastState = BAL_STATEMACH_UNINITIALIZED,
78  .lastSubstate = 0,
79  .triggerEntry = 0,
80  .errorRequestCounter = 0,
81  .initializationFinished = STD_NOT_OK,
82  .active = false,
83  .balancingThreshold = BAL_THRESHOLD_mV + BAL_HYSTERESIS_mV,
84  .balancingAllowed = true,
85  .balancingGlobalAllowed = false,
86 };
87 
88 /*========== Extern Constant and Variable Definitions =======================*/
89 
90 /*========== Static Function Prototypes =====================================*/
91 /**
92  * @brief Activates voltage based balancing
93  * @details TODO
94  */
95 static bool BAL_ActivateBalancing(void);
96 
97 /**
98  * @brief Deactivates voltage based balancing
99  * @details The balancing state of all cells in all strings set to inactivate
100  * (that is 0) and the delta charge is set to 0 As. The balancing
101  * enable bit is deactivate (that is 0).
102  */
103 static void BAL_Deactivate(void);
104 
105 /**
106  * @brief State machine subfunction to check if balancing is allowed
107  * @details Checks if balancing is allowed. If it is it transfers in the actual
108  * balancing state.
109  */
110 static void BAL_ProcessStateCheckBalancing(BAL_STATE_REQUEST_e state_request);
111 
112 /**
113  * @brief State machine subfunction to balance the battery cell
114  * @details TODO
115  */
116 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request);
117 
118 /*========== Static Function Implementations ================================*/
119 static bool BAL_ActivateBalancing(void) {
120  bool finished = true;
122  /* Database entry is declared static, to place it in the data segment and not on the stack */
124 
125  DATA_READ_DATA(&cellvoltage, &minMax);
126 
127  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
128  int16_t min = minMax.minimumCellVoltage_mV[s];
129  uint16_t nrBalancedCells = 0u;
130  for (uint8_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
131  if (cellvoltage.cellVoltage_mV[s][c] > (min + bal_state.balancingThreshold)) {
132  bal_balancing.balancingState[s][c] = 1;
133  finished = false;
135  bal_state.active = true;
137  nrBalancedCells++;
138  } else {
139  bal_balancing.balancingState[s][c] = 0;
140  }
141  }
142  bal_balancing.nrBalancedCells[s] = nrBalancedCells;
143  }
145 
146  return finished;
147 }
148 
149 static void BAL_Deactivate(void) {
150  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
151  for (uint16_t i = 0u; i < BS_NR_OF_BAT_CELLS; i++) {
152  bal_balancing.balancingState[s][i] = 0;
153  bal_balancing.deltaCharge_mAs[s][i] = 0;
154  }
156  }
158  bal_state.active = false;
159 
161 }
162 
164  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
165  bal_state.balancingAllowed = false;
166  }
167  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
169  }
170 
172 
173  if ((bal_state.balancingAllowed == false) || (bal_state.balancingGlobalAllowed == false)) {
174  BAL_Deactivate();
175  bal_state.active = false;
176  } else {
180  }
181  }
182 }
183 
184 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request) {
185  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
186  bal_state.balancingAllowed = false;
187  }
188  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
190  }
191 
192  if (bal_state.balancingGlobalAllowed == false) {
193  if (bal_state.active == true) {
194  BAL_Deactivate();
195  }
196  bal_state.active = false;
200  return;
201  }
202 
203  if (bal_state.substate == BAL_ENTRY) {
204  if (bal_state.balancingAllowed == false) {
205  if (bal_state.active == true) {
206  BAL_Deactivate();
207  }
208  bal_state.active = false;
211  } else {
213  }
215  return;
219  DATA_READ_DATA(&checkMinMax);
220  /* stop balacing if minimum voltage is below minimum threshold or
221  * maximum cell temperature breached upper temperature limit */
222  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
223  if ((checkMinMax.minimumCellVoltage_mV[stringNumber] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
224  (checkMinMax.maximumTemperature_ddegC[stringNumber] >= BAL_UPPER_TEMPERATURE_LIMIT_ddegC)) {
225  if (bal_state.active == true) {
226  BAL_Deactivate();
227  }
230  }
231  }
233  return;
234  } else if (bal_state.substate == BAL_CHECK_CURRENT) {
237  } else {
238  if (bal_state.active == true) {
239  BAL_Deactivate();
240  }
243  }
245  return;
246  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
247  if (bal_state.balancingAllowed == false) {
248  if (bal_state.active == true) {
249  BAL_Deactivate();
250  }
251  bal_state.active = false;
254  } else {
255  if (true == BAL_ActivateBalancing()) {
259  } else {
262  }
263  }
265  return;
266  }
267 }
268 
269 /*========== Extern Function Implementations ================================*/
272 }
273 
275  BAL_RETURN_TYPE_e retVal = BAL_OK;
276 
278  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
279 
280  if (retVal == BAL_OK) {
281  bal_state.stateRequest = stateRequest;
282  }
284 
285  return retVal;
286 }
287 
288 extern void BAL_Trigger(void) {
290 
291  /* Check re-entrance of function */
292  if (BAL_CheckReEntrance(&bal_state) > 0u) {
293  return;
294  }
295 
296  if (bal_state.timer > 0u) {
297  if ((--bal_state.timer) > 0) {
299  return; /* handle state machine only if timer has elapsed */
300  }
301  }
302 
303  switch (bal_state.state) {
306  stateRequest = BAL_TransferStateRequest(&bal_state);
307  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
308  break;
313  break;
317  break;
320  stateRequest = BAL_TransferStateRequest(&bal_state);
321  BAL_ProcessStateCheckBalancing(stateRequest);
322  break;
325  /* Check if balancing is still allowed */
326  stateRequest = BAL_TransferStateRequest(&bal_state);
327  BAL_ProcessStateBalancing(stateRequest);
328  break;
329  default:
330  /* invalid state */
332  break;
333  }
335 }
336 
337 /*========== Externalized Static Function Implementations (Unit Test) =======*/
338 #ifdef UNITY_UNIT_TEST
339 extern BAL_STATEMACH_e BAL_GetState(void) {
340  return bal_state.state;
341 }
342 #endif
343 
344 /*================== Getter for static Variables (Unit Test) ==============*/
345 #ifdef UNITY_UNIT_TEST
346 extern DATA_BLOCK_BALANCING_CONTROL_s *TEST_BAL_GetBalancingControl(void) {
347  return &bal_balancing;
348 }
349 
350 extern BAL_STATE_s *TEST_BAL_GetBalancingState(void) {
351  return &bal_state;
352 }
353 #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_ENTRY
Definition: bal.h:88
@ BAL_CHECK_LOWEST_VOLTAGE
Definition: bal.h:92
@ BAL_CHECK_CURRENT
Definition: bal.h:93
@ BAL_STATE_NO_REQUEST
Definition: bal.h:106
@ BAL_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:103
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:102
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 void BAL_ProcessStateCheckBalancing(BAL_STATE_REQUEST_e state_request)
State machine subfunction to check if balancing is allowed.
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
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.
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
#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:1261
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:239
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:110
@ STD_NOT_OK
Definition: fstd_types.h:82
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
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:125
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:121
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:111
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:242
uint16_t nrBalancedCells[BS_NR_OF_STRINGS]
Definition: database_cfg.h:244
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:243
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:238
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:124
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:121
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:166
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:150
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:153