foxBMS  1.1.2
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 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_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;
123 
124  DATA_READ_DATA(&cellvoltage, &minMax);
125 
126  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
127  int16_t min = minMax.minimumCellVoltage_mV[s];
128  uint16_t nrBalancedCells = 0u;
129  for (uint8_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
130  if (cellvoltage.cellVoltage_mV[s][c] > (min + bal_state.balancingThreshold)) {
131  bal_balancing.balancingState[s][c] = 1;
132  finished = false;
134  bal_state.active = true;
136  nrBalancedCells++;
137  } else {
138  bal_balancing.balancingState[s][c] = 0;
139  }
140  }
141  bal_balancing.nrBalancedCells[s] = nrBalancedCells;
142  }
144 
145  return finished;
146 }
147 
148 static void BAL_Deactivate(void) {
149  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
150  for (uint16_t i = 0u; i < BS_NR_OF_BAT_CELLS; i++) {
151  bal_balancing.balancingState[s][i] = 0;
152  bal_balancing.deltaCharge_mAs[s][i] = 0;
153  }
155  }
157  bal_state.active = false;
158 
160 }
161 
163  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
164  bal_state.balancingAllowed = false;
165  }
166  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
168  }
169 
171 
172  if ((bal_state.balancingAllowed == false) || (bal_state.balancingGlobalAllowed == false)) {
173  BAL_Deactivate();
174  bal_state.active = false;
175  } else {
179  }
180  }
181 }
182 
183 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request) {
184  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
185  bal_state.balancingAllowed = false;
186  }
187  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
189  }
190 
191  if (bal_state.balancingGlobalAllowed == false) {
192  if (bal_state.active == true) {
193  BAL_Deactivate();
194  }
195  bal_state.active = false;
199  return;
200  }
201 
202  if (bal_state.substate == BAL_ENTRY) {
203  if (bal_state.balancingAllowed == false) {
204  if (bal_state.active == true) {
205  BAL_Deactivate();
206  }
207  bal_state.active = false;
210  } else {
212  }
214  return;
218  DATA_READ_DATA(&checkMinMax);
219  /* stop balacing if minimum voltage is below minimum threshold or
220  * maximum cell temperature breached upper temperature limit */
221  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
222  if ((checkMinMax.minimumCellVoltage_mV[stringNumber] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
223  (checkMinMax.maximumTemperature_ddegC[stringNumber] >= BAL_UPPER_TEMPERATURE_LIMIT_ddegC)) {
224  if (bal_state.active == true) {
225  BAL_Deactivate();
226  }
229  }
230  }
232  return;
233  } else if (bal_state.substate == BAL_CHECK_CURRENT) {
236  } else {
237  if (bal_state.active == true) {
238  BAL_Deactivate();
239  }
242  }
244  return;
245  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
246  if (bal_state.balancingAllowed == false) {
247  if (bal_state.active == true) {
248  BAL_Deactivate();
249  }
250  bal_state.active = false;
253  } else {
254  if (true == BAL_ActivateBalancing()) {
258  } else {
261  }
262  }
264  return;
265  }
266 }
267 
268 /*========== Extern Function Implementations ================================*/
271 }
272 
274  BAL_RETURN_TYPE_e retVal = BAL_OK;
275 
277  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
278 
279  if (retVal == BAL_OK) {
280  bal_state.stateRequest = stateRequest;
281  }
283 
284  return retVal;
285 }
286 
287 extern void BAL_Trigger(void) {
289 
290  /* Check re-entrance of function */
291  if (BAL_CheckReEntrance(&bal_state) > 0u) {
292  return;
293  }
294 
295  if (bal_state.timer > 0u) {
296  if ((--bal_state.timer) > 0) {
298  return; /* handle state machine only if timer has elapsed */
299  }
300  }
301 
302  switch (bal_state.state) {
305  stateRequest = BAL_TransferStateRequest(&bal_state);
306  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
307  break;
312  break;
316  break;
319  stateRequest = BAL_TransferStateRequest(&bal_state);
320  BAL_ProcessStateCheckBalancing(stateRequest);
321  break;
324  /* Check if balancing is still allowed */
325  stateRequest = BAL_TransferStateRequest(&bal_state);
326  BAL_ProcessStateBalancing(stateRequest);
327  break;
328  default:
329  /* invalid state */
331  break;
332  }
334 }
335 
336 /*========== Externalized Static Function Implementations (Unit Test) =======*/
337 #ifdef UNITY_UNIT_TEST
338 extern BAL_STATEMACH_e BAL_GetState(void) {
339  return bal_state.state;
340 }
341 #endif
342 
343 /*================== Getter for static Variables (Unit Test) ==============*/
344 #ifdef UNITY_UNIT_TEST
345 extern DATA_BLOCK_BALANCING_CONTROL_s *TEST_BAL_GetBalancingControl(void) {
346  return &bal_balancing;
347 }
348 
349 extern BAL_STATE_s *TEST_BAL_GetBalancingState(void) {
350  return &bal_state;
351 }
352 #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:1272
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:237
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
@ STD_NOT_OK
Definition: fstd_types.h:73
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
Implementation of the tasks used by the system, headers.
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:110
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:255
uint16_t nrBalancedCells[BS_NR_OF_STRINGS]
Definition: database_cfg.h:257
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:256
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:251
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:123
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:120
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:179
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:163
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:166