foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
bms.c File Reference

bms driver implementation More...

#include "bms.h"
#include "battery_cell_cfg.h"
#include "afe.h"
#include "bal.h"
#include "database.h"
#include "diag.h"
#include "foxmath.h"
#include "imd.h"
#include "led.h"
#include "meas.h"
#include "os.h"
#include "soa.h"
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for bms.c:

Go to the source code of this file.

Macros

#define BMS_NO_ACTIVE_DELAY_TIME_ms   (UINT32_MAX)
 
#define BMS_SAVELASTSTATES()
 

Functions

static BMS_RETURN_TYPE_e BMS_CheckStateRequest (BMS_STATE_REQUEST_e statereq)
 checks the state requests that are made. More...
 
static BMS_STATE_REQUEST_e BMS_TransferStateRequest (void)
 transfers the current state request to the state machine. More...
 
static uint8_t BMS_CheckReEntrance (void)
 re-entrance check of SYS state machine trigger function More...
 
static uint8_t BMS_CheckCanRequests (void)
 Checks the state requests made to the BMS state machine. More...
 
static bool BMS_IsAnyFatalErrorFlagSet (void)
 Checks all the error flags from diagnosis module with a severity of DIAG_FATAL_ERROR. More...
 
static STD_RETURN_TYPE_e BMS_IsBatterySystemStateOkay (void)
 Checks if any error flag is set and handles delay until contactors need to be opened. More...
 
static bool BMS_IsContactorFeedbackValid (uint8_t stringNumber, CONT_TYPE_e contactorType)
 Checks if the contactor feedback for a specific contactor is valid need to be opened. More...
 
static void BMS_GetMeasurementValues (void)
 
static void BMS_CheckOpenSenseWire (void)
 Check for any open voltage sense wire. More...
 
static STD_RETURN_TYPE_e BMS_CheckPrecharge (uint8_t stringNumber, const DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Checks if the current limitations are violated. More...
 
static uint8_t BMS_GetHighestString (BMS_CONSIDER_PRECHARGE_e precharge, DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Returns ID of string with highest total voltage. More...
 
static uint8_t BMS_GetClosestString (BMS_CONSIDER_PRECHARGE_e precharge, DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Returns ID of string with voltage closest to first closed string voltage. More...
 
static uint8_t BMS_GetLowestString (BMS_CONSIDER_PRECHARGE_e precharge, DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Returns ID of string with lowest total voltage. More...
 
static int32_t BMS_GetStringVoltageDifference (uint8_t string, const DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Returns voltage difference between first closed string and string ID. More...
 
static int32_t BMS_GetAverageStringCurrent (DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Returns the average current flowing through all strings. More...
 
static void BMS_UpdateBatsysState (DATA_BLOCK_PACK_VALUES_s *pPackValues)
 Updates battery system state variable depending on measured/recent current values. More...
 
static CONT_TYPE_e BMS_GetFirstContactorToBeOpened (uint8_t stringNumber, BMS_CURRENT_FLOW_STATE_e flowDirection)
 Get first string contactor that should be opened depending on the actual current flow direction. More...
 
static CONT_TYPE_e BMS_GetSecondContactorToBeOpened (uint8_t stringNumber, CONT_TYPE_e firstOpenedContactorType)
 Get second string contactor that should be opened. More...
 
STD_RETURN_TYPE_e BMS_GetInitializationState (void)
 Gets the initialization state. More...
 
BMS_STATEMACH_e BMS_GetState (void)
 Returns the current state. More...
 
BMS_RETURN_TYPE_e BMS_SetStateRequest (BMS_STATE_REQUEST_e statereq)
 sets the current state request of the state variable bms_state. More...
 
void BMS_Trigger (void)
 trigger function for the BMS driver state machine. More...
 
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState (void)
 Returns current battery system state (charging/discharging, resting or in relaxation phase) More...
 
BMS_CURRENT_FLOW_STATE_e BMS_GetCurrentFlowDirection (int32_t current_mA)
 Get current flow direction, current value as function parameter. More...
 
bool BMS_IsStringClosed (uint8_t stringNumber)
 Returns string state (closed or open) More...
 
bool BMS_IsStringPrecharging (uint8_t stringNumber)
 Returns if string is currently precharging or not. More...
 
uint8_t BMS_GetNumberOfConnectedStrings (void)
 Returns number of connected strings. More...
 
bool BMS_IsTransitionToErrorStateActive (void)
 Check if transition in to error state is active. More...
 

Variables

static BMS_STATE_s bms_state
 
static DATA_BLOCK_MIN_MAX_s bms_tableMinMax = {.header.uniqueId = DATA_BLOCK_ID_MIN_MAX}
 
static DATA_BLOCK_OPEN_WIRE_s bms_tableOpenWire = {.header.uniqueId = DATA_BLOCK_ID_OPEN_WIRE_BASE}
 
static DATA_BLOCK_PACK_VALUES_s bms_tablePackValues = {.header.uniqueId = DATA_BLOCK_ID_PACK_VALUES}
 

Detailed Description

bms driver implementation

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2020-02-24 (date of creation)
Updated
2023-10-12 (date of last update)
Version
v1.6.0
Prefix
BMS

Definition in file bms.c.

Macro Definition Documentation

◆ BMS_NO_ACTIVE_DELAY_TIME_ms

#define BMS_NO_ACTIVE_DELAY_TIME_ms   (UINT32_MAX)

default value for unset "active delay time"

Definition at line 75 of file bms.c.

◆ BMS_SAVELASTSTATES

#define BMS_SAVELASTSTATES ( )
Value:
bms_state.lastsubstate = bms_state.substate
static BMS_STATE_s bms_state
Definition: bms.c:89
BMS_STATEMACH_e laststate
Definition: bms.h:194
BMS_STATEMACH_e state
Definition: bms.h:192
BMS_STATEMACH_SUB_e substate
Definition: bms.h:193

Saves the last state and the last substate

Definition at line 80 of file bms.c.

Function Documentation

◆ BMS_CheckCanRequests()

static uint8_t BMS_CheckCanRequests ( void  )
static

Checks the state requests made to the BMS state machine.

Checks of the state request in the database and sets this value as return value.

Returns
requested state

Definition at line 360 of file bms.c.

◆ BMS_CheckOpenSenseWire()

static void BMS_CheckOpenSenseWire ( void  )
static

Check for any open voltage sense wire.

Definition at line 381 of file bms.c.

Here is the call graph for this function:

◆ BMS_CheckPrecharge()

static STD_RETURN_TYPE_e BMS_CheckPrecharge ( uint8_t  stringNumber,
const DATA_BLOCK_PACK_VALUES_s pPackValues 
)
static

Checks if the current limitations are violated.

Parameters
[in]stringNumberstring addressed
[in]pPackValuespointer to pack values database entry
Returns
STD_OK if the current limitations are NOT violated, else STD_NOT_OK (type: STD_RETURN_TYPE_e)

Definition at line 406 of file bms.c.

Here is the call graph for this function:

◆ BMS_CheckReEntrance()

static uint8_t BMS_CheckReEntrance ( void  )
static

re-entrance check of SYS state machine trigger function

This function is not re-entrant and should only be called time- or event-triggered. It increments the triggerentry counter from the state variable ltc_state. It should never be called by two different processes, so if it is the case, triggerentry should never be higher than 0 when this function is called.

Returns
retval 0 if no further instance of the function is active, 0xff else

Definition at line 334 of file bms.c.

Here is the call graph for this function:

◆ BMS_CheckStateRequest()

static BMS_RETURN_TYPE_e BMS_CheckStateRequest ( BMS_STATE_REQUEST_e  statereq)
static

checks the state requests that are made.

This function checks the validity of the state requests. The results of the checked is returned immediately.

Parameters
[in]statereqstate request to be checked
Returns
result of the state request that was made

Definition at line 313 of file bms.c.

◆ BMS_GetAverageStringCurrent()

static int32_t BMS_GetAverageStringCurrent ( DATA_BLOCK_PACK_VALUES_s pPackValues)
static

Returns the average current flowing through all strings.

This function is used when closing strings.

Parameters
[in]pPackValuespointer to pack values database entry
Returns
average current taking all strings into account in mA. INT32_MAX if there is no valid current measurement

Definition at line 654 of file bms.c.

◆ BMS_GetBatterySystemState()

BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState ( void  )

Returns current battery system state (charging/discharging, resting or in relaxation phase)

Returns
BMS_CURRENT_FLOW_STATE_e

Definition at line 1586 of file bms.c.

◆ BMS_GetClosestString()

static uint8_t BMS_GetClosestString ( BMS_CONSIDER_PRECHARGE_e  precharge,
DATA_BLOCK_PACK_VALUES_s pPackValues 
)
static

Returns ID of string with voltage closest to first closed string voltage.

This is used to close further strings in drive.

Parameters
[in]prechargeIf BMS_DO_NOT_TAKE_PRECHARGE_INTO_ACCCOUNT, precharge availability for string is ignored. if BMS_TAKE_PRECHARGE_INTO_ACCCOUNT, only select string that has precharge available.
[in]pPackValuespointer to pack values database entry
Returns
index of string with voltage closest to the first closed string voltage. If no string is available, returns BMS_NO_STRING_AVAILABLE.

Definition at line 564 of file bms.c.

Here is the call graph for this function:

◆ BMS_GetCurrentFlowDirection()

BMS_CURRENT_FLOW_STATE_e BMS_GetCurrentFlowDirection ( int32_t  current_mA)

Get current flow direction, current value as function parameter.

Parameters
[in]current_mAcurrent that is flowing
Returns
BMS_DISCHARGING or BMS_CHARGING depending on current direction. Return BMS_AT_REST. ((type: BMS_CURRENT_FLOW_STATE_e)

Definition at line 1590 of file bms.c.

◆ BMS_GetFirstContactorToBeOpened()

static CONT_TYPE_e BMS_GetFirstContactorToBeOpened ( uint8_t  stringNumber,
BMS_CURRENT_FLOW_STATE_e  flowDirection 
)
static

Get first string contactor that should be opened depending on the actual current flow direction.

Check the mounting direction of the contactors and open the contactor that is mounted in the preferred current flow direction. Open the plus contactor first if, there is no contactor in preferred direction to the curren flow to open available. This may be either because both contactors are installed in the same direction or because the contactors are bidirectional.

Parameters
stringNumberstring that will be opened
flowDirectioncurrent flow direction (charging or discharging)
Returns
CONT_TYPE_e contactor that should be opened

Definition at line 710 of file bms.c.

◆ BMS_GetHighestString()

static uint8_t BMS_GetHighestString ( BMS_CONSIDER_PRECHARGE_e  precharge,
DATA_BLOCK_PACK_VALUES_s pPackValues 
)
static

Returns ID of string with highest total voltage.

This is used to close the first string when drive-off is requested.

Parameters
[in]prechargeIf BMS_DO_NOT_TAKE_PRECHARGE_INTO_ACCCOUNT, precharge availability for string is ignored. if BMS_TAKE_PRECHARGE_INTO_ACCCOUNT, only select string that has precharge available.
[in]pPackValuespointer to pack values database entry
Returns
index of string with highest voltage If no string is available, returns BMS_NO_STRING_AVAILABLE.

Definition at line 539 of file bms.c.

◆ BMS_GetInitializationState()

STD_RETURN_TYPE_e BMS_GetInitializationState ( void  )

Gets the initialization state.

This function is used for getting the BMS initialization state.

Returns
STD_OK if initialized, otherwise STD_NOT_OK

Definition at line 803 of file bms.c.

◆ BMS_GetLowestString()

static uint8_t BMS_GetLowestString ( BMS_CONSIDER_PRECHARGE_e  precharge,
DATA_BLOCK_PACK_VALUES_s pPackValues 
)
static

Returns ID of string with lowest total voltage.

This is used to close the first string when charge-off is requested.

Parameters
[in]prechargeIf 0, precharge availability for string is ignored. If 1, only selects a string that has precharge available.
[in]pPackValuespointer to pack values database entry
Returns
index of string with lowest voltage. If no string is available, returns BMS_NO_STRING_AVAILABLE.

Definition at line 610 of file bms.c.

◆ BMS_GetMeasurementValues()

static void BMS_GetMeasurementValues ( void  )
static

Get latest database entries for static module variables

Definition at line 356 of file bms.c.

◆ BMS_GetNumberOfConnectedStrings()

uint8_t BMS_GetNumberOfConnectedStrings ( void  )

Returns number of connected strings.

Returns
Returns number of connected strings

Definition at line 1632 of file bms.c.

◆ BMS_GetSecondContactorToBeOpened()

static CONT_TYPE_e BMS_GetSecondContactorToBeOpened ( uint8_t  stringNumber,
CONT_TYPE_e  firstOpenedContactorType 
)
static

Get second string contactor that should be opened.

Mounting direction of the contactor does not need to be checked for the second contactor as the current has already been interrupted opening the first contactor.

Parameters
stringNumberstring that will be opened
firstOpenedContactorTypetype of first contactor that has been opened
Returns
CONT_TYPE_e contactor that should be opened

Definition at line 773 of file bms.c.

◆ BMS_GetState()

BMS_STATEMACH_e BMS_GetState ( void  )

Returns the current state.

This function is used in the functioning of the SYS state machine.

Returns
current state, taken from BMS_STATEMACH_e

Definition at line 807 of file bms.c.

◆ BMS_GetStringVoltageDifference()

static int32_t BMS_GetStringVoltageDifference ( uint8_t  string,
const DATA_BLOCK_PACK_VALUES_s pPackValues 
)
static

Returns voltage difference between first closed string and string ID.

This function is used to check voltage when trying to close further strings.

Parameters
[in]stringID of string that must be compared with first closed string
[in]pPackValuespointer to pack values database entry
Returns
voltage difference in mV, will return INT32_MAX if voltages are invalid and difference can not be calculated

Definition at line 634 of file bms.c.

Here is the call graph for this function:

◆ BMS_IsAnyFatalErrorFlagSet()

static bool BMS_IsAnyFatalErrorFlagSet ( void  )
static

Checks all the error flags from diagnosis module with a severity of DIAG_FATAL_ERROR.

Checks all the error flags from diagnosis module with a severity of DIAG_FATAL_ERROR. Furthermore, sets parameter minimumActiveDelay_ms of bms_state variable.

Returns
true if error flag is set, otherwise false

Definition at line 446 of file bms.c.

Here is the call graph for this function:

◆ BMS_IsBatterySystemStateOkay()

static STD_RETURN_TYPE_e BMS_IsBatterySystemStateOkay ( void  )
static

Checks if any error flag is set and handles delay until contactors need to be opened.

Checks all the diagnosis entries with severity of DIAG_FATAL_ERROR and handles the configured delay until the contactors need to be opened. The shortest delay is used, if multiple errors are active at once.

Returns
STD_NOT_OK if error detected and delay time elapsed, otherwise STD_OK

Check if a fatal error has been detected previously. If yes, check delay

Set previous timestamp for next call

Definition at line 465 of file bms.c.

Here is the call graph for this function:

◆ BMS_IsContactorFeedbackValid()

static bool BMS_IsContactorFeedbackValid ( uint8_t  stringNumber,
CONT_TYPE_e  contactorType 
)
static

Checks if the contactor feedback for a specific contactor is valid need to be opened.

Reads error flag database entry and checks if the feedback for this specific contactor is valid or not.

Returns
true if no error detected feedback is valid, otherwise false

Definition at line 508 of file bms.c.

◆ BMS_IsStringClosed()

bool BMS_IsStringClosed ( uint8_t  stringNumber)

Returns string state (closed or open)

Parameters
[in]stringNumberstring addressed
Returns
false if string is open, true if string is closed

Definition at line 1614 of file bms.c.

◆ BMS_IsStringPrecharging()

bool BMS_IsStringPrecharging ( uint8_t  stringNumber)

Returns if string is currently precharging or not.

Parameters
[in]stringNumberstring addressed
Returns
false if precharge contactor is open, true if closed and string is precharging

Definition at line 1623 of file bms.c.

◆ BMS_IsTransitionToErrorStateActive()

bool BMS_IsTransitionToErrorStateActive ( void  )

Check if transition in to error state is active.

Returns
True, if transition into error state is ongoing, otherwise false

Definition at line 1636 of file bms.c.

◆ BMS_SetStateRequest()

BMS_RETURN_TYPE_e BMS_SetStateRequest ( BMS_STATE_REQUEST_e  statereq)

sets the current state request of the state variable bms_state.

This function is used to make a state request to the state machine, e.g, start voltage measurement, read result of voltage measurement, re-initialization. It calls BMS_CheckStateRequest() to check if the request is valid. The state request is rejected if is not valid. The result of the check is returned immediately, so that the requester can act in case it made a non-valid state request.

Parameters
statereqstate request to set
Returns
current state request

Definition at line 811 of file bms.c.

Here is the call graph for this function:

◆ BMS_TransferStateRequest()

static BMS_STATE_REQUEST_e BMS_TransferStateRequest ( void  )
static

transfers the current state request to the state machine.

This function takes the current state request from bms_state transfers it to the state machine. It resets the value from bms_state to BMS_STATE_NO_REQUEST

Returns
current state request

Definition at line 346 of file bms.c.

Here is the call graph for this function:

◆ BMS_Trigger()

void BMS_Trigger ( void  )

trigger function for the BMS driver state machine.

This function contains the sequence of events in the BMS state machine. It must be called time-triggered, every 10 milliseconds. This function needs to be adapted to be adapted to the behavior the batter system shall provide to the target application.

Definition at line 825 of file bms.c.

Here is the call graph for this function:

◆ BMS_UpdateBatsysState()

static void BMS_UpdateBatsysState ( DATA_BLOCK_PACK_VALUES_s pPackValues)
static

Updates battery system state variable depending on measured/recent current values.

Parameters
[in]pPackValuesrecent measured values from current sensor

Definition at line 663 of file bms.c.

Variable Documentation

◆ bms_state

BMS_STATE_s bms_state
static

contains the state of the bms state machine

Definition at line 89 of file bms.c.

◆ bms_tableMinMax

DATA_BLOCK_MIN_MAX_s bms_tableMinMax = {.header.uniqueId = DATA_BLOCK_ID_MIN_MAX}
static

local copies of database tables

Definition at line 124 of file bms.c.

◆ bms_tableOpenWire

DATA_BLOCK_OPEN_WIRE_s bms_tableOpenWire = {.header.uniqueId = DATA_BLOCK_ID_OPEN_WIRE_BASE}
static

local copies of database tables

Definition at line 125 of file bms.c.

◆ bms_tablePackValues

DATA_BLOCK_PACK_VALUES_s bms_tablePackValues = {.header.uniqueId = DATA_BLOCK_ID_PACK_VALUES}
static

local copies of database tables

Definition at line 126 of file bms.c.