foxBMS  1.0.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 "bal.h"
#include "contactor.h"
#include "database.h"
#include "diag.h"
#include "foxmath.h"
#include "interlock.h"
#include "meas.h"
#include "mic.h"
#include "os.h"
#include "soa.h"
Include dependency graph for bms.c:

Go to the source code of this file.

Macros

#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 STD_RETURN_TYPE_e BMS_CheckAnyErrorFlagSet (void)
 Checks the error flags. 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...
 
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 SYS 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...
 

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

BSD 3-Clause License 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
2021-03-24 (date of last update)
Prefix
BMS

Definition in file bms.c.

Macro Definition Documentation

◆ BMS_SAVELASTSTATES

#define BMS_SAVELASTSTATES ( )
Value:
bms_state.lastsubstate = bms_state.substate

Saves the last state and the last substate

Definition at line 74 of file bms.c.

Function Documentation

◆ BMS_CheckAnyErrorFlagSet()

static STD_RETURN_TYPE_e BMS_CheckAnyErrorFlagSet ( void  )
static

Checks the error flags.

Checks all the error flags from the database and returns an error if at least one is set.

Returns
STD_OK if no error flag is set, otherwise STD_NOT_OK

Definition at line 372 of file bms.c.

◆ 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 300 of file bms.c.

◆ BMS_CheckOpenSenseWire()

static void BMS_CheckOpenSenseWire ( void  )
static

Check for any open voltage sense wire.

Definition at line 321 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 346 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 274 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 253 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 534 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 1230 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 444 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 1234 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 419 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 592 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 490 of file bms.c.

◆ BMS_GetMeasurementValues()

static void BMS_GetMeasurementValues ( void  )
static

Get latest database entries for static module variables

Definition at line 296 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 596 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 514 of file bms.c.

Here is the call graph for this function:

◆ 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 1257 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 1266 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 600 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 286 of file bms.c.

Here is the call graph for this function:

◆ BMS_Trigger()

void BMS_Trigger ( void  )

trigger function for the SYS driver state machine.

This function contains the sequence of events in the SYS state machine. It must be called time-triggered, every 10 milliseconds.

Definition at line 614 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 543 of file bms.c.

Variable Documentation

◆ bms_state

BMS_STATE_s bms_state
static
Initial value:
= {
.timer = 0,
.stateRequest = BMS_STATE_NO_REQUEST,
.substate = BMS_ENTRY,
.lastsubstate = BMS_ENTRY,
.triggerentry = 0u,
.ErrRequestCounter = 0u,
.initFinished = STD_NOT_OK,
.counter = 0u,
.OscillationTimeout = 0u,
.PrechargeTryCounter = 0u,
.powerline = BMS_PL_OPEN,
.closedStrings = {0u},
.closedPrechargeContactors = {0u},
.numberOfClosedStrings = 0u,
.deactivatedStrings = {0},
.firstClosedString = 0u,
.stringOpenTimeout = 0u,
.nextstringclosedtimer = 0u,
.stringCloseTimeout = 0u,
.nextstate = BMS_STATEMACH_STANDBY,
.restTimer_10ms = BS_RELAXATION_PERIOD_10ms,
.currentFlowState = BMS_RELAXATION,
}

contains the state of the bms state machine

Definition at line 83 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 112 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 113 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 114 of file bms.c.

BMS_STATE_NO_REQUEST
@ BMS_STATE_NO_REQUEST
Definition: bms.h:158
BMS_STATEMACH_UNINITIALIZED
@ BMS_STATEMACH_UNINITIALIZED
Definition: bms.h:88
BMS_STATE::state
BMS_STATEMACH_e state
Definition: bms.h:188
BMS_STATE::laststate
BMS_STATEMACH_e laststate
Definition: bms.h:190
BS_RELAXATION_PERIOD_10ms
#define BS_RELAXATION_PERIOD_10ms
Wait time in 10ms before battery system is at rest. Balancing for example only starts if battery syst...
Definition: battery_system_cfg.h:293
BMS_ENTRY
@ BMS_ENTRY
Definition: bms.h:120
bms_state
static BMS_STATE_s bms_state
Definition: bms.c:83
BMS_STATEMACH_STANDBY
@ BMS_STATEMACH_STANDBY
Definition: bms.h:93
BMS_STATE::substate
BMS_STATEMACH_SUB_e substate
Definition: bms.h:189
BMS_PL_OPEN
@ BMS_PL_OPEN
Definition: bms.h:175
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
BMS_RELAXATION
@ BMS_RELAXATION
Definition: bms.h:68