foxBMS  1.1.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_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 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...
 
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
2021-07-29 (date of last update)
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 71 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:85
BMS_STATEMACH_e state
Definition: bms.h:188
BMS_STATEMACH_e laststate
Definition: bms.h:190
BMS_STATEMACH_SUB_e substate
Definition: bms.h:189

Saves the last state and the last substate

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

◆ BMS_CheckOpenSenseWire()

static void BMS_CheckOpenSenseWire ( void  )
static

Check for any open voltage sense wire.

Definition at line 339 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 364 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 292 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 271 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 567 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 1272 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 477 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 1276 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 452 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 625 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 523 of file bms.c.

◆ BMS_GetMeasurementValues()

static void BMS_GetMeasurementValues ( void  )
static

Get latest database entries for static module variables

Definition at line 314 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 1317 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 629 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 547 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 390 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 409 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 1299 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 1308 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 1321 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 633 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 304 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.

Remove statements below once interlock module is working

Remove statements above once interlock module is working

Definition at line 647 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 576 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,
.remainingDelay_ms = BMS_NO_ACTIVE_DELAY_TIME_ms,
.minimumActiveDelay_ms = BMS_NO_ACTIVE_DELAY_TIME_ms,
.transitionToErrorState = false,
}
#define BS_RELAXATION_PERIOD_10ms
Wait time in 10ms before battery system is at rest. Balancing for example only starts if battery syst...
#define BMS_NO_ACTIVE_DELAY_TIME_ms
Definition: bms.c:71
@ BMS_RELAXATION
Definition: bms.h:68
@ BMS_STATE_NO_REQUEST
Definition: bms.h:158
@ BMS_ENTRY
Definition: bms.h:120
@ BMS_PL_OPEN
Definition: bms.h:175
@ BMS_STATEMACH_STANDBY
Definition: bms.h:93
@ BMS_STATEMACH_UNINITIALIZED
Definition: bms.h:88
@ STD_NOT_OK
Definition: fstd_types.h:73

contains the state of the bms state machine

Definition at line 85 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 117 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 118 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 119 of file bms.c.