foxBMS  1.0.0
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
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
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  for (uint8_t c = 0u; c < BS_NR_OF_BAT_CELLS; c++) {
129  if (cellvoltage.cellVoltage_mV[s][c] > (min + bal_state.balancingThreshold)) {
130  bal_balancing.balancingState[s][c] = 1;
131  finished = false;
133  bal_state.active = true;
135  } else {
136  bal_balancing.balancingState[s][c] = 0;
137  }
138  }
139  }
141 
142  return finished;
143 }
144 
145 static void BAL_Deactivate(void) {
146  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
147  for (uint16_t i = 0u; i < BS_NR_OF_BAT_CELLS; i++) {
148  bal_balancing.balancingState[s][i] = 0;
149  bal_balancing.deltaCharge_mAs[s][i] = 0;
150  }
151  }
153  bal_state.active = false;
154 
156 }
157 
159  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
160  bal_state.balancingAllowed = false;
161  }
162  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
164  }
165 
167 
168  if ((bal_state.balancingAllowed == false) || (bal_state.balancingGlobalAllowed == false)) {
169  BAL_Deactivate();
170  bal_state.active = false;
171  } else {
175  }
176  }
177 }
178 
179 static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request) {
180  if (state_request == BAL_STATE_NO_BALANCING_REQUEST) {
181  bal_state.balancingAllowed = false;
182  }
183  if (state_request == BAL_STATE_ALLOWBALANCING_REQUEST) {
185  }
186 
187  if (bal_state.balancingGlobalAllowed == false) {
188  if (bal_state.active == true) {
189  BAL_Deactivate();
190  }
191  bal_state.active = false;
195  return;
196  }
197 
198  if (bal_state.substate == BAL_ENTRY) {
199  if (bal_state.balancingAllowed == false) {
200  if (bal_state.active == true) {
201  BAL_Deactivate();
202  }
203  bal_state.active = false;
206  } else {
208  }
210  return;
214  DATA_READ_DATA(&checkMinMax);
215  /* stop balacing if minimum voltage is below minimum threshold or
216  * maximum cell temperature breached upper temperature limit */
217  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
218  if ((checkMinMax.minimumCellVoltage_mV[stringNumber] <= BAL_LOWER_VOLTAGE_LIMIT_mV) ||
219  (checkMinMax.maximumTemperature_ddegC[stringNumber] >= BAL_UPPER_TEMPERATURE_LIMIT_ddegC)) {
220  if (bal_state.active == true) {
221  BAL_Deactivate();
222  }
225  }
226  }
228  return;
229  } else if (bal_state.substate == BAL_CHECK_CURRENT) {
232  } else {
233  if (bal_state.active == true) {
234  BAL_Deactivate();
235  }
238  }
240  return;
241  } else if (bal_state.substate == BAL_ACTIVATE_BALANCING) {
242  if (bal_state.balancingAllowed == false) {
243  if (bal_state.active == true) {
244  BAL_Deactivate();
245  }
246  bal_state.active = false;
249  } else {
250  if (true == BAL_ActivateBalancing()) {
254  } else {
257  }
258  }
260  return;
261  }
262 }
263 
264 /*========== Extern Function Implementations ================================*/
267 }
268 
270  BAL_RETURN_TYPE_e retVal = BAL_OK;
271 
273  retVal = BAL_CheckStateRequest(&bal_state, stateRequest);
274 
275  if (retVal == BAL_OK) {
276  bal_state.stateRequest = stateRequest;
277  }
279 
280  return retVal;
281 }
282 
283 extern void BAL_Trigger(void) {
285 
286  /* Check re-entrance of function */
287  if (BAL_CheckReEntrance(&bal_state) > 0u) {
288  return;
289  }
290 
291  if (bal_state.timer > 0u) {
292  if ((--bal_state.timer) > 0) {
294  return; /* handle state machine only if timer has elapsed */
295  }
296  }
297 
298  switch (bal_state.state) {
301  stateRequest = BAL_TransferStateRequest(&bal_state);
302  BAL_ProcessStateUninitalized(&bal_state, stateRequest);
303  break;
308  break;
312  break;
315  stateRequest = BAL_TransferStateRequest(&bal_state);
316  BAL_ProcessStateCheckBalancing(stateRequest);
317  break;
320  /* Check if balancing is still allowed */
321  stateRequest = BAL_TransferStateRequest(&bal_state);
322  BAL_ProcessStateBalancing(stateRequest);
323  break;
324  default:
325  /* invalid state */
327  break;
328  }
330 }
331 
332 /*========== Externalized Static Function Implementations (Unit Test) =======*/
333 #ifdef UNITY_UNIT_TEST
334 extern BAL_STATEMACH_e BAL_GetState(void) {
335  return bal_state.state;
336 }
337 #endif
338 
339 /*================== Getter for static Variables (Unit Test) ==============*/
340 #ifdef UNITY_UNIT_TEST
341 extern DATA_BLOCK_BALANCING_CONTROL_s *TEST_BAL_GetBalancingControl(void) {
342  return &bal_balancing;
343 }
344 
345 extern BAL_STATE_s *TEST_BAL_GetBalancingState(void) {
346  return &bal_state;
347 }
348 #endif
os.h
Implementation of the tasks used by the system, headers.
BAL_STATE::balancingThreshold
int32_t balancingThreshold
Definition: bal.h:139
BAL_STATE::balancingGlobalAllowed
bool balancingGlobalAllowed
Definition: bal.h:141
BAL_STATE::triggerEntry
uint8_t triggerEntry
Definition: bal.h:135
BS_NR_OF_BAT_CELLS
#define BS_NR_OF_BAT_CELLS
Definition: battery_system_cfg.h:156
BAL_STATE_NO_REQUEST
@ BAL_STATE_NO_REQUEST
Definition: bal.h:106
BAL_ENTRY
@ BAL_ENTRY
Definition: bal.h:88
BAL_TransferStateRequest
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:97
DATA_BLOCK_MIN_MAX::maximumTemperature_ddegC
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:178
BAL_ProcessStateCheckBalancing
static void BAL_ProcessStateCheckBalancing(BAL_STATE_REQUEST_e state_request)
State machine subfunction to check if balancing is allowed.
Definition: bal_strategy_voltage.c:158
DATA_BLOCK_BALANCING_CONTROL::enableBalancing
uint8_t enableBalancing
Definition: database_cfg.h:250
BAL_STATE
Definition: bal.h:128
BAL_ACTIVATE_BALANCING
@ BAL_ACTIVATE_BALANCING
Definition: bal.h:91
DATA_BLOCK_MIN_MAX::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:162
BAL_STATEMACH_e
enum BAL_STATEMACH BAL_STATEMACH_e
STD_RETURN_TYPE_e
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
BAL_ActivateBalancing
static bool BAL_ActivateBalancing(void)
Activates voltage based balancing.
Definition: bal_strategy_voltage.c:119
bms.h
bms driver header
BAL_LOWER_VOLTAGE_LIMIT_mV
#define BAL_LOWER_VOLTAGE_LIMIT_mV
Definition: bal_cfg.h:79
DATA_WRITE_DATA
#define DATA_WRITE_DATA(...)
Definition: database.h:82
BAL_STATEMACH_CHECK_BALANCING
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:73
BAL_STATEMACH_BALANCE
@ BAL_STATEMACH_BALANCE
Definition: bal.h:74
bal_strategy_voltage.h
Header for the voltage-based balancing strategy module.
BAL_CheckReEntrance
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:82
BAL_STATEMACH_INITIALIZATION
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:71
BAL_STATE_NO_BALANCING_REQUEST
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:102
BAL_Trigger
void BAL_Trigger(void)
trigger function for the BAL driver state machine.
Definition: bal_strategy_voltage.c:283
BAL_STATE::timer
uint16_t timer
Definition: bal.h:129
BAL_CHECK_CURRENT
@ BAL_CHECK_CURRENT
Definition: bal.h:93
BAL_ProcessStateInitialization
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:164
BAL_ProcessStateInitialized
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
DATA_BLOCK_BALANCING_CONTROL::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:249
FAS_ASSERT
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
OS_ExitTaskCritical
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
BAL_STATE::state
BAL_STATEMACH_e state
Definition: bal.h:131
BAL_STATE::initializationFinished
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:137
DATA_BLOCK_ID_MIN_MAX
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
DATA_BLOCK_BALANCING_CONTROL::deltaCharge_mAs
uint32_t deltaCharge_mAs[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:254
BAL_STATEMACH_UNINITIALIZED
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:70
bal_balancing
static DATA_BLOCK_BALANCING_CONTROL_s bal_balancing
Definition: bal_strategy_voltage.c:67
BAL_THRESHOLD_mV
#define BAL_THRESHOLD_mV
Definition: bal_cfg.h:73
BAL_UPPER_TEMPERATURE_LIMIT_ddegC
#define BAL_UPPER_TEMPERATURE_LIMIT_ddegC
Definition: bal_cfg.h:82
OS_EnterTaskCritical
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
BMS_AT_REST
@ BMS_AT_REST
Definition: bms.h:69
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
BMS_GetBatterySystemState
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1230
battery_cell_cfg.h
Configuration of the battery cell (e.g., minimum and maximum cell voltage)
DATA_BLOCK_CELL_VOLTAGE::cellVoltage_mV
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:122
BAL_Init
STD_RETURN_TYPE_e BAL_Init(DATA_BLOCK_BALANCING_CONTROL_s *pControl)
Generic initialization function for the balancing module.
Definition: bal.c:143
BAL_CHECK_LOWEST_VOLTAGE
@ BAL_CHECK_LOWEST_VOLTAGE
Definition: bal.h:92
BAL_SetStateRequest
BAL_RETURN_TYPE_e BAL_SetStateRequest(BAL_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable bal_state.
Definition: bal_strategy_voltage.c:269
BAL_Deactivate
static void BAL_Deactivate(void)
Deactivates voltage based balancing.
Definition: bal_strategy_voltage.c:145
BAL_GetInitializationState
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
Definition: bal_strategy_voltage.c:265
DATA_BLOCK_ID_BALANCING_CONTROL
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:77
DATA_BLOCK_BALANCING_CONTROL::balancingState
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:253
DATA_BLOCK_CELL_VOLTAGE
Definition: database_cfg.h:115
DATA_BLOCK_MIN_MAX
Definition: database_cfg.h:158
DATA_READ_DATA
#define DATA_READ_DATA(...)
Definition: database.h:72
BAL_ProcessStateBalancing
static void BAL_ProcessStateBalancing(BAL_STATE_REQUEST_e state_request)
State machine subfunction to balance the battery cell.
Definition: bal_strategy_voltage.c:179
BAL_RETURN_TYPE_e
enum BAL_RETURN_TYPE BAL_RETURN_TYPE_e
database.h
Database module header.
BAL_SaveLastStates
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:69
BAL_STATE::active
bool active
Definition: bal.h:138
BAL_STATEMACH_SHORTTIME_100ms
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:63
BAL_STATEMACH_INITIALIZED
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:72
bal_state
static BAL_STATE_s bal_state
contains the state of the contactor state machine
Definition: bal_strategy_voltage.c:72
DATA_BLOCK_ID_CELL_VOLTAGE
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:73
BAL_STATE::substate
BAL_STATEMACH_SUB_e substate
Definition: bal.h:132
DATA_BLOCK_MIN_MAX::minimumCellVoltage_mV
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:165
BAL_CheckStateRequest
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
BAL_STATE::stateRequest
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:130
BAL_STATE_REQUEST_e
enum BAL_STATE_REQUEST BAL_STATE_REQUEST_e
BAL_HYSTERESIS_mV
#define BAL_HYSTERESIS_mV
Definition: bal_cfg.h:76
FAS_TRAP
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
BAL_STATE::balancingAllowed
bool balancingAllowed
Definition: bal.h:140
DATA_BLOCK_BALANCING_CONTROL
Definition: database_cfg.h:245
DATA_BLOCK_CELL_VOLTAGE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:119
BAL_ProcessStateUninitalized
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:151
BAL_STATEMACH_BALANCINGTIME_100ms
#define BAL_STATEMACH_BALANCINGTIME_100ms
Definition: bal_cfg.h:70
BS_NR_OF_STRINGS
#define BS_NR_OF_STRINGS
Definition: battery_system_cfg.h:89
BAL_OK
@ BAL_OK
Definition: bal.h:113
BAL_STATE_ALLOWBALANCING_REQUEST
@ BAL_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:103
DATA_BLOCKHEADER::uniqueId
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:109