|
@@ -1,2029 +1,2029 @@
|
|
|
-//##############################################################################
|
|
|
-//
|
|
|
-// FILE: BMS_Master.c
|
|
|
-//
|
|
|
-// TITLE: Functions of BMS_Master
|
|
|
-// void BMS_Init_BSD ( void );
|
|
|
-// void BMS_Init_BSE ( void );
|
|
|
-// void SwitchRelais ( uint8_t, uint8_t );
|
|
|
-// void SetBalancer ( void );
|
|
|
-// uint16_t BMS_Do ( void );
|
|
|
-//
|
|
|
-//
|
|
|
-//##############################################################################
|
|
|
-
|
|
|
-//==============================================================================
|
|
|
-// Historie:
|
|
|
-//==============================================================================
|
|
|
-// Datum: | Name | Version:| Aenderungsgrund: | rev.:
|
|
|
-//------------------------------------------------------------------------------
|
|
|
-// | SB | 2.1 | Implementaion of Balancing and SoC Estimator | 003
|
|
|
-//------------------------------------------------------------------------------
|
|
|
-// | SB | 2.0 | Adaptation for RCT | 002
|
|
|
-//------------------------------------------------------------------------------
|
|
|
-// 01.07.13 | VR | 1.1 | Rearrange Code for IAA Bus | 001
|
|
|
-//------------------------------------------------------------------------------
|
|
|
-// 20.04.10 | TM | 1.0 | Start Code for Master Test | 000
|
|
|
-//==============================================================================
|
|
|
-
|
|
|
-#include "BMS_Master.h"
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// ***** Global Data ***********************************************************
|
|
|
-extern uint32_t Global_1msCounter;
|
|
|
-extern BSE_t gBSE;
|
|
|
-extern BSD_t gBSD;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// ***** SwitchRelais **********************************************************
|
|
|
-void SwitchRelais( uint8_t Relais, uint8_t OnOff )
|
|
|
-{
|
|
|
- if( Relais == LS_RELAIS )
|
|
|
- {
|
|
|
- if(OnOff)
|
|
|
- SET_OUTPIN( PIN_REGNR_RELAIS_SLAVE );
|
|
|
- else
|
|
|
- CLEAR_OUTPIN( PIN_REGNR_RELAIS_SLAVE);
|
|
|
- }
|
|
|
-
|
|
|
- if( Relais == HS_RELAIS)
|
|
|
- {
|
|
|
- if(OnOff) {
|
|
|
- SET_OUTPIN( PIN_REGNR_RELAIS_PLUS );
|
|
|
- SET_OUTPIN(PIN_REGNR_LED3);
|
|
|
- }
|
|
|
- else{
|
|
|
- CLEAR_OUTPIN( PIN_REGNR_RELAIS_PLUS );
|
|
|
- CLEAR_OUTPIN(PIN_REGNR_LED3);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if( Relais == PRE_CHARGE_RELAIS )
|
|
|
- {
|
|
|
- if(OnOff)
|
|
|
- SET_OUTPIN( PIN_REGNR_RELAIS_PRECHA );
|
|
|
- else
|
|
|
- CLEAR_OUTPIN( PIN_REGNR_RELAIS_PRECHA );
|
|
|
- }
|
|
|
- if( Relais ==PWR_SUPPLY ) {
|
|
|
- if(OnOff)
|
|
|
- SET_OUTPIN( PIN_REGNR_PWR_SUPPLY );
|
|
|
- else
|
|
|
- CLEAR_OUTPIN( PIN_REGNR_PWR_SUPPLY );
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-uint16_t init_master_CAN0_fsm(MASTER_CAN0_STRUCT_t* s,BMS_SLAVE_CONFIGURATION_t* cellConfig,BMS_UI_CONFIGURATION_t* uiConfig) {
|
|
|
- uint8_t slaveNr;
|
|
|
- uint8_t i;
|
|
|
- uint16_t stateActive;
|
|
|
- //go thought all connected Slaves
|
|
|
- for(slaveNr=0;slaveNr<CAN0_MAX_NR_OF_SLAVES-1;slaveNr++) {
|
|
|
-
|
|
|
- // set slave Type
|
|
|
- s->Slave[slaveNr].SlaveType=cellConfig[slaveNr].type;
|
|
|
- s->Slave[slaveNr].TxMailbox_ptr=MB_Container[slaveNr];
|
|
|
- s->Slave[slaveNr].TxTelegram_ptr=&TelegramTxContainer[slaveNr];
|
|
|
- s->Slave[slaveNr].SlaveConnectionState=cellConfig[slaveNr].connectionSate;
|
|
|
-
|
|
|
- // set initial Voltage and Temp value to 0xffff = not initialzied
|
|
|
-
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
- s->Slave[slaveNr].CellVoltage[i]=0xffff;
|
|
|
- s->Slave[slaveNr].CellTemp[i]=-51;
|
|
|
- s->Slave[slaveNr].CellConnectionState[i]=cellConfig[slaveNr].cellConnectionState[i];
|
|
|
- s->Slave[slaveNr].TempSensConnectionState[i]=cellConfig[slaveNr].tempSensConnectionState[i];
|
|
|
- }
|
|
|
- s->Slave[slaveNr].HeatSinkTemp=-51;
|
|
|
-
|
|
|
- // set slave alive cnt to 0xff fot not initialized
|
|
|
- for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
- s->Slave[slaveNr].SlaveAliveCnt[i]=0xff;
|
|
|
- }
|
|
|
-
|
|
|
- // set slave Status to 0xff for Not initialized
|
|
|
- s->Slave[slaveNr].SlaveMode=0xff;
|
|
|
- // no Errors
|
|
|
- s->Slave[slaveNr].SlaveError =0 ;
|
|
|
-
|
|
|
- // first State Running mode
|
|
|
- s->Slave[slaveNr].Set_Mode = BMS_SLAVE_RUN ;
|
|
|
- // No Balancing
|
|
|
- s->Slave[slaveNr].Balance_Cell_0_7=0;
|
|
|
- s->Slave[slaveNr].Balance_Cell_8_15=0;
|
|
|
- s->Slave[slaveNr].Balance_Cell_16_23=0;
|
|
|
-
|
|
|
- // set Slave Error to 0
|
|
|
- s->Slave[slaveNr].SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
- s->Slave[slaveNr].SlaveCanCommuniationError.SlaveErrorCounterRegister=0;
|
|
|
- s->Slave[slaveNr].SlaveCanCommuniationError.SlaveErrorStateRegister=0;
|
|
|
- s->Slave[slaveNr].SlaveCanCommuniationError.WrongAliveCnt=0;
|
|
|
-
|
|
|
- s->Slave[slaveNr].MasterAliveCnt=0;
|
|
|
- s->Slave[slaveNr].maxCellTemp=0;
|
|
|
- s->Slave[slaveNr].minCellTemp=0;
|
|
|
- }
|
|
|
-
|
|
|
- // init UI
|
|
|
-
|
|
|
- s->Slave[15].SlaveType=uiConfig->type;
|
|
|
- s->Slave[15].TxMailbox_ptr=MB_Container[15];
|
|
|
- s->Slave[15].TxTelegram_ptr=&TelegramTxContainer[15];
|
|
|
- s->Slave[15].SlaveConnectionState=uiConfig->connectionSate;
|
|
|
-
|
|
|
- s->Slave[15].SlaveMode=0xff;
|
|
|
- // no Errors
|
|
|
- s->Slave[15].SlaveError =0 ;
|
|
|
-
|
|
|
- // first State Running mode
|
|
|
- s->Slave[15].Set_Mode = BMS_SLAVE_RUN ;
|
|
|
-
|
|
|
-
|
|
|
- // set Slave Error to 0
|
|
|
- s->Slave[15].SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
- s->Slave[15].SlaveCanCommuniationError.SlaveErrorCounterRegister=0;
|
|
|
- s->Slave[15].SlaveCanCommuniationError.SlaveErrorStateRegister=0;
|
|
|
- s->Slave[15].SlaveCanCommuniationError.WrongAliveCnt=0;
|
|
|
-
|
|
|
-
|
|
|
- // set State to INIT
|
|
|
- s->FsmState=INIT;
|
|
|
- s->slaveSelect=0;
|
|
|
- s->transmission_pending=FALSE;
|
|
|
- s->cycleCounter=0;
|
|
|
- s->cycleTimestamp=0;
|
|
|
- s->StateOfCharge=40*60*60*1000; // 40Ah in mAs
|
|
|
- s->allValuesInitialized = FALSE;
|
|
|
- s->startCan1Comm=FALSE;
|
|
|
- s->balancerState=BMS_BALANCE_INIT;
|
|
|
-
|
|
|
-
|
|
|
- s->SoC_initialized=FALSE;
|
|
|
- s->SoC_outside=0;
|
|
|
- s->maxHeatSinkTemp=0;
|
|
|
- s->NrOfSlaves= get_nr_of_connected_slaves(s);
|
|
|
-
|
|
|
- s->CAN1_fsmStruct.fsmState=CAN1_FSM_INIT;
|
|
|
- s->CAN1_fsmStruct.highPrioMsgNr=0;
|
|
|
- s->CAN1_fsmStruct.lowPrioMsgNr=0;
|
|
|
- s->CAN1_fsmStruct.requestTelegramNr=0;
|
|
|
- s->CAN1_fsmStruct.timeoutCyclesCnt=0;
|
|
|
- s->CAN1_fsmStruct.receivedTelegrams=0;
|
|
|
- s->CAN1_fsmStruct.fastRequestFsmRunning=FALSE;
|
|
|
- s->CAN1_fsmStruct.slowRequestFsmRunning=FALSE;
|
|
|
- s->CAN1_fsmStruct.fastRxState=CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
- s->CAN1_fsmStruct.nrOfRecCurrentSamples=0;
|
|
|
-
|
|
|
- s->FsmErrorState=ERROR_INIT;
|
|
|
- //s->ErrorStack.ErrorNr=0;
|
|
|
- //s->RunMode=RUN_MODE_INIT;
|
|
|
- s->ErrorFlags=0;
|
|
|
- // celar Error Buffers
|
|
|
- ErrorStackClearBuffer(s);
|
|
|
-
|
|
|
- // set Operation fsms to idle
|
|
|
- s->RunMode.OperationMode=OP_MODE_INIT;
|
|
|
- s->RunMode.ErrorState1fsm=ES1_FSM_INIT;
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_INIT;
|
|
|
- s->RunMode.ErrorState3fsm=ES3_FSM_INIT;
|
|
|
- // init master temp sensor
|
|
|
- s->masterTemp=18;
|
|
|
- s->MasterTempSensState=BMS_MASTER_TEMP_SENSOR_INITIATE_MEASUREMENT;
|
|
|
- s->inverterState.state=RCT_INV_INACTIVE;
|
|
|
- s->inverterState.inverterCanOnline=FALSE;
|
|
|
- s->inverterState.inverterIsCharging=FALSE;
|
|
|
- s->inverterState.startupPwr=0;
|
|
|
-
|
|
|
- // assign impossible values to be able to check if values have been initialized
|
|
|
- s->inverter.rxStruct.expectedInputPower=-1;
|
|
|
- s->inverter.rxStruct.DCinputA_power=-1;
|
|
|
- s->inverter.rxStruct.DCinputB_power=-1;
|
|
|
-
|
|
|
- BMS_Clear_Error_init_recovery_struct(s);//clear recovery struct
|
|
|
- s->ErrorBuffer.ES2_New_Error=0;
|
|
|
- s->relayState.HS_closed=0;
|
|
|
- s->relayState.LS_closed=0;
|
|
|
- s->relayState.PRECHARGE_closed=0;
|
|
|
- s->relayState.reseved=0;
|
|
|
-
|
|
|
- s->reset_test_timestamp=0;
|
|
|
-
|
|
|
- BMS_RCT_init_fsm(s);
|
|
|
- initUIFifo(s);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-void set_slave_cell_connection_state(MASTER_CAN0_STRUCT_t* s,uint8_t slaveNr,CELL_STATE_t* cell_state){
|
|
|
- uint8_t i;
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
- s->Slave[slaveNr].CellConnectionState[i]=cell_state[i];
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void set_slave_temp_connection_state(MASTER_CAN0_STRUCT_t* s,uint8_t slaveNr,TEMP_SENSOR_STATE_t* temp_state){
|
|
|
- uint8_t i;
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
- s->Slave[slaveNr].TempSensConnectionState[i]=temp_state[i];
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-uint8_t check_slave_data(MASTER_CAN0_STRUCT_t* s,
|
|
|
- BMS_CAN0_SLAVE_t* Slave,
|
|
|
- BMS_CAN0_SLAVE_t* tempSlave,
|
|
|
- int8_t overTemp_charge,
|
|
|
- int8_t overTemp_discharge,
|
|
|
- int8_t underTemp_charge,
|
|
|
- int8_t underTemp_discharge,
|
|
|
- uint16_t overVoltage,uint16_t underVoltage) {
|
|
|
- uint8_t i;
|
|
|
- uint8_t error_status=BMS_SLAVE_DATA_OK;
|
|
|
- uint16_t current=s->UI_Board.Ibatt*10;
|
|
|
-
|
|
|
- // Check Slave Mode
|
|
|
- if(tempSlave->SlaveMode==BMS_SLAVE_RUN) {
|
|
|
- // everything OK
|
|
|
-
|
|
|
-
|
|
|
- //check Slave Alive Counter
|
|
|
-
|
|
|
- //if Failed communication counter is grater than 0 the communication
|
|
|
- //attempt last Cycle failed and the alive counters are out of sync
|
|
|
- if(tempSlave->SlaveCanCommuniationError.FailedComCnt>0) {
|
|
|
- //
|
|
|
- for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
- Slave->SlaveAliveCnt[i]=tempSlave->SlaveAliveCnt[i];
|
|
|
- }
|
|
|
- tempSlave->SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
- }
|
|
|
- //compare all Slave Alive Counter
|
|
|
- else{
|
|
|
- for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
- if( (tempSlave->SlaveAliveCnt[i] == Slave->SlaveAliveCnt[i] + 1) ||
|
|
|
- (Slave->SlaveAliveCnt[i] ==0x7 && tempSlave->SlaveAliveCnt[i] ==0) ) {
|
|
|
- // Slave Alive Counter == OK
|
|
|
- Slave->SlaveAliveCnt[i]=tempSlave->SlaveAliveCnt[i];
|
|
|
- Slave->SlaveCanCommuniationError.WrongAliveCnt=0;
|
|
|
- }
|
|
|
- else {
|
|
|
- // slave Alive Counter didn't change or made bigger steps
|
|
|
-
|
|
|
- Slave->SlaveCanCommuniationError.WrongAliveCnt++;
|
|
|
- if(Slave->SlaveCanCommuniationError.WrongAliveCnt>=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
- error_status|=BMS_SLAVE_DATA_ERROR_ALIVE_TIMEOUT;
|
|
|
- // set CAN ERROR
|
|
|
- ErrorStackPushMasterError(s,BMS_ERROR_STACK_SLAVE_CAN_ERROR,BMS_ERROR_CLASS_1);
|
|
|
- return error_status;
|
|
|
- }
|
|
|
- for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
- Slave->SlaveAliveCnt[i]=tempSlave->SlaveAliveCnt[i];
|
|
|
- }
|
|
|
- error_status |=BMS_SALVE_DATA_ERROR_ALIVE_CNT;
|
|
|
- return error_status;
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // check voltages
|
|
|
- // discard uninitialized values 0xff and Bypassed cells
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++){
|
|
|
- if(tempSlave->CellConnectionState[i]==CELL_BYPASSED) {
|
|
|
- // no cell == no voltage
|
|
|
- tempSlave->CellVoltage[i]=0;
|
|
|
- }
|
|
|
- else if(tempSlave->CellConnectionState[i]==CELL_CONNECTED && tempSlave->CellVoltage[i]>= 0xfff8 ) {
|
|
|
- // not initialized cells
|
|
|
- //TODO decide upon signed unsigned representation
|
|
|
- // use old value
|
|
|
- tempSlave->CellVoltage[i]= Slave->CellVoltage[i];
|
|
|
-
|
|
|
- }
|
|
|
- if( (tempSlave->CellVoltage[i] >= overVoltage || tempSlave->CellVoltage[i] <= underVoltage)
|
|
|
- && tempSlave->CellConnectionState[i]==CELL_CONNECTED) {
|
|
|
- //copy Slave to record current Errors
|
|
|
- *Slave=*tempSlave;
|
|
|
- error_status |=BMS_SLAVE_DATA_ERROR_VOLTAGE_LIMIT;
|
|
|
-
|
|
|
- // Write Error Stack
|
|
|
- if(tempSlave->CellVoltage[i] >= overVoltage ) {
|
|
|
-
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_OVER_VOLTAGE,i,BMS_ERROR_CLASS_3);
|
|
|
- error_status |=BMS_SLAVE_DATA_ERROR_OVER_VOLTAGE;
|
|
|
- }
|
|
|
- else {
|
|
|
-
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_UNDER_VOLTAGE,i,BMS_ERROR_CLASS_3);
|
|
|
- error_status |=BMS_SLAVE_DATA_ERROR_UNDER_VOLTAGE;
|
|
|
- }
|
|
|
-
|
|
|
- return error_status;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if(tempSlave->HeatSinkTemp >= BMS_ERROR_THRESHOLD_T_HEATSINK_MAX || tempSlave->HeatSinkTemp <= BMS_ERROR_THRESHOLD_T_HEATSINK_MIN){
|
|
|
- //copy Slave to record current Errors
|
|
|
- *Slave=*tempSlave;
|
|
|
- error_status |=BMS_SLAVE_DATA_ERROR_HEAT_SINK_LIMIT;
|
|
|
- // Write Error Stack
|
|
|
- if(tempSlave->HeatSinkTemp >= BMS_ERROR_THRESHOLD_T_HEATSINK_MAX ) {
|
|
|
- // Has to be defined
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- // has to be defined
|
|
|
- }
|
|
|
- return error_status;
|
|
|
- }
|
|
|
-
|
|
|
- //check cell temperatures
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++){
|
|
|
-
|
|
|
- if (tempSlave->CellTemp[i] > -40) {
|
|
|
- //toDo: QUICKFIX FOR INITIALIZED SENSORS
|
|
|
-
|
|
|
- if(current < -100 ) {
|
|
|
- // battery is charged
|
|
|
- if( (tempSlave->CellTemp[i] >= overTemp_charge || tempSlave->CellTemp[i] <= underTemp_charge)
|
|
|
- && tempSlave->TempSensConnectionState[i] ==TEMP_SENSOR_CONNECTED) {
|
|
|
- //copy Slave to record current Errors
|
|
|
- *Slave=*tempSlave;
|
|
|
- error_status |=BMS_SLAVE_DATA_ERROR_TEMP_LIMIT;
|
|
|
-
|
|
|
- // Write Error Stack
|
|
|
- if(tempSlave->CellTemp[i] >= overTemp_charge ) {
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_OVER_TEMP_CHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_UNDER_TEMP_CHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
- }
|
|
|
- return error_status;
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- // battery is discharged
|
|
|
- if( (tempSlave->CellTemp[i] >= overTemp_discharge || tempSlave->CellTemp[i] <= underTemp_discharge)
|
|
|
- && tempSlave->TempSensConnectionState[i] ==TEMP_SENSOR_CONNECTED) {
|
|
|
- //copy Slave to record current Errors
|
|
|
- *Slave=*tempSlave;
|
|
|
- error_status |=BMS_SLAVE_DATA_ERROR_TEMP_LIMIT;
|
|
|
-
|
|
|
- // Write Error Stack
|
|
|
- if(tempSlave->CellTemp[i] >= overTemp_discharge ) {
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_OVER_TEMP_DISCHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_UNDER_TEMP_DISCHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
- }
|
|
|
- return error_status;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- *Slave=*tempSlave;
|
|
|
- return error_status; //everything should be ok
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-uint8_t check_UI_data(BMS_CAN0_SLAVE_t* Slave, BMS_CAN0_SLAVE_t* tempSlave,BMS_CAN0_UI_t* ui,BMS_CAN0_UI_t* tempUI) {
|
|
|
- // todo Check Alive Cnt
|
|
|
- // Check Over current etc
|
|
|
- uint8_t i=0;
|
|
|
-
|
|
|
- if(tempUI->Ibatt > 50) {
|
|
|
- i++;
|
|
|
- }
|
|
|
-
|
|
|
- ui->Ubatt=tempUI->Ubatt;
|
|
|
- ui->Ibatt=tempUI->Ibatt;
|
|
|
- ui->Checksum=tempUI->Checksum;
|
|
|
- return BMS_SLAVE_DATA_OK;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief calculate Voltage of Slave Board By adding cell voltages
|
|
|
- */
|
|
|
-uint32_t calc_block_voltage(BMS_CAN0_SLAVE_t* Slave) {
|
|
|
- uint32_t blockVoltage=0;
|
|
|
- uint8_t i=0;
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
- if(Slave->CellConnectionState[i]==CELL_CONNECTED) {
|
|
|
- blockVoltage += Slave->CellVoltage[i];
|
|
|
- }
|
|
|
- }
|
|
|
- return blockVoltage;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief calculate min and Max Voltage of Block
|
|
|
- */
|
|
|
-uint32_t calc_min_max_voltage_slave(BMS_CAN0_SLAVE_t* Slave) {
|
|
|
- uint16_t minV=Slave->CellVoltage[0];
|
|
|
- uint16_t maxV=Slave->CellVoltage[0];
|
|
|
- uint8_t i=0;
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
- if(Slave->CellConnectionState[i]==CELL_CONNECTED) {
|
|
|
- if(minV >= Slave->CellVoltage[i]) {
|
|
|
- minV = Slave->CellVoltage[i];
|
|
|
- }
|
|
|
- if(maxV <= Slave->CellVoltage[i]) {
|
|
|
- maxV = Slave->CellVoltage[i];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- Slave->minCellVoltage=minV;
|
|
|
- Slave->maxCellVoltage=maxV;
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief calculate min and Max Voltage of System
|
|
|
- */
|
|
|
-uint32_t calc_min_max_voltage_system(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t slave_nr=0;
|
|
|
- uint16_t minV=BMS_SLAVE_MAX_CELL_VOLTAGE;
|
|
|
- uint16_t maxV=BMS_SLAVE_MIN_CELL_VOLTAGE;
|
|
|
- BMS_CAN0_SLAVE_t* Slave;
|
|
|
- for(slave_nr=0;slave_nr<CAN0_MAX_NR_OF_SLAVES -1 ;slave_nr++) {
|
|
|
- if(s->Slave[slave_nr].SlaveConnectionState != NOT_CONNECTED ) {
|
|
|
- //slave is connected
|
|
|
- Slave=&(s->Slave[slave_nr]);
|
|
|
- if(Slave->maxCellVoltage >=maxV ) {
|
|
|
- maxV = Slave->maxCellVoltage;
|
|
|
- }
|
|
|
- if(Slave->minCellVoltage <= minV) {
|
|
|
- minV = Slave->minCellVoltage;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- s->minCellVoltage=minV;
|
|
|
- s->maxCellVoltage=maxV;
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * set balancing register for cell_nr
|
|
|
- */
|
|
|
-uint32_t balance_cell(BMS_CAN0_SLAVE_t* Slave,uint8_t cell_nr) {
|
|
|
- if( cell_nr <8) {
|
|
|
- Slave->Balance_Cell_0_7|= (1<<cell_nr);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(cell_nr <16) {
|
|
|
- Slave->Balance_Cell_8_15|=(1<<(cell_nr-8));
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- Slave->Balance_Cell_16_23|=(1<<(cell_nr-16));
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- return FALSE;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * go though all slaves, destinguish slaves to balance and set Balancearray
|
|
|
- */
|
|
|
-uint32_t set_balancer(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t slave_nr=0;
|
|
|
- uint8_t cell_nr=0;
|
|
|
- BMS_CAN0_SLAVE_t* Slave;
|
|
|
-
|
|
|
- for(slave_nr=0;slave_nr < CAN0_MAX_NR_OF_SLAVES -1 ;slave_nr++) { // -1 because slave 15 is UI
|
|
|
- if(s->Slave[slave_nr].SlaveConnectionState != NOT_CONNECTED) {
|
|
|
- // slave is connected
|
|
|
- Slave=&(s->Slave[slave_nr]);
|
|
|
-
|
|
|
-
|
|
|
- if(Slave->maxCellVoltage - s->minCellVoltage > SLAVE_BALANCE_MIN_DELTA_U_MV) {
|
|
|
- // blancing neccessay
|
|
|
- for(cell_nr=0;cell_nr <MAX_SLAVE_CELLS;cell_nr++) {
|
|
|
- if(Slave->CellConnectionState[cell_nr]==CELL_CONNECTED) {
|
|
|
- // cell is connected
|
|
|
- if(Slave->CellVoltage[cell_nr] - s->minCellVoltage > SLAVE_BALANCE_MIN_DELTA_U_MV) {
|
|
|
- // cell has to be balanced
|
|
|
- balance_cell(Slave,cell_nr);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- // no need to balance
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * set all balance registers to off
|
|
|
- */
|
|
|
-uint32_t set_balancer_off(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t slave_nr=0;
|
|
|
- BMS_CAN0_SLAVE_t* Slave;
|
|
|
- for(slave_nr=0;slave_nr < CAN0_MAX_NR_OF_SLAVES -1 ;slave_nr++) {
|
|
|
- Slave=&(s->Slave[slave_nr]);
|
|
|
- Slave->Balance_Cell_0_7=0;
|
|
|
- Slave->Balance_Cell_16_23=0;
|
|
|
- Slave->Balance_Cell_8_15=0;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief set min and Max Cell Temperature
|
|
|
- * */
|
|
|
-
|
|
|
-uint32_t set_block_min_max_temp(BMS_CAN0_SLAVE_t* Slave){
|
|
|
- uint8_t i;
|
|
|
- int8_t maxTemp=25;
|
|
|
- int8_t minTemp=25;
|
|
|
- for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
- if(Slave->TempSensConnectionState[i]==TEMP_SENSOR_CONNECTED){
|
|
|
- if(Slave->CellTemp[i] > maxTemp) {
|
|
|
- maxTemp=Slave->CellTemp[i];
|
|
|
- }
|
|
|
- if(Slave->CellTemp[i] < minTemp) {
|
|
|
- minTemp=Slave->CellTemp[i];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- Slave->maxCellTemp=maxTemp;
|
|
|
- Slave->minCellTemp=minTemp;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief set min and Max Cell Temperature of System
|
|
|
- * */
|
|
|
-
|
|
|
-uint32_t set_system_min_max_temp(MASTER_CAN0_STRUCT_t* s){
|
|
|
- uint8_t i;
|
|
|
- int8_t maxTemp=25;
|
|
|
- int8_t minTemp=25;
|
|
|
- BMS_CAN0_SLAVE_t* Slave;
|
|
|
-
|
|
|
- for(i=0;i<CAN0_MAX_NR_OF_SLAVES;i++) {
|
|
|
- if(s->Slave[i].SlaveConnectionState!=NOT_CONNECTED && s->Slave[i].SlaveType == SLAVE) {
|
|
|
- Slave=&(s->Slave[i]);
|
|
|
- if(Slave->maxCellTemp > maxTemp) {
|
|
|
- maxTemp=Slave->maxCellTemp;
|
|
|
- }
|
|
|
- if(Slave->minCellTemp < minTemp) {
|
|
|
- minTemp=Slave->minCellTemp;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- s->maxCellTemp=maxTemp;
|
|
|
- s->minCellTemp=minTemp;
|
|
|
-}
|
|
|
-
|
|
|
-uint32_t set_system_min_max_heatsink_temp(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t i;
|
|
|
- int8_t maxTemp=25;
|
|
|
- int8_t minTemp=25;
|
|
|
- BMS_CAN0_SLAVE_t* Slave;
|
|
|
-
|
|
|
- for(i=0;i<CAN0_MAX_NR_OF_SLAVES;i++) {
|
|
|
- if(s->Slave[i].SlaveConnectionState!=NOT_CONNECTED &&s->Slave[i].SlaveType == SLAVE) {
|
|
|
- Slave=&(s->Slave[i]);
|
|
|
- if(Slave->HeatSinkTemp > maxTemp) {
|
|
|
- maxTemp=Slave->HeatSinkTemp;
|
|
|
- }
|
|
|
- if(Slave->HeatSinkTemp < minTemp) {
|
|
|
- minTemp=Slave->HeatSinkTemp;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- s->maxHeatSinkTemp=maxTemp;
|
|
|
- s->minHeatSinkTemp=minTemp;
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief calculate Voltage of System by adding block voltages
|
|
|
- */
|
|
|
-uint32_t calc_system_voltage(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint32_t systemVoltage=0;
|
|
|
- uint8_t i=0;
|
|
|
- for(i=0;i<CAN0_MAX_NR_OF_SLAVES;i++) {
|
|
|
- if(s->Slave[i].SlaveConnectionState!=NOT_CONNECTED && s->Slave[i].SlaveType == SLAVE ) {
|
|
|
- systemVoltage += s->Slave[i].BlockVoltage;
|
|
|
- }
|
|
|
- }
|
|
|
- return systemVoltage;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @bief calculate SOC by simple coulomb counting
|
|
|
- */
|
|
|
-void calc_system_SoC(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- s->StateOfCharge = s->StateOfCharge - s->UI_Board.Ibatt*10 *200; // t=200ms ibatt in 10mA steps
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief convert inverter data into correct data types
|
|
|
- */
|
|
|
-void refresh_inverter_tx_data(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- BMS_CAN1_INVERTER_TX* inv=&(s->inverter.txStruct);
|
|
|
- inv->Values.batteryCapacity.value=36.0;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryCapacity.value));
|
|
|
- inv->Values.batteryCurrent.value=( (float)(s->UI_Board.Ibatt) )/100 ;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryCurrent.value));
|
|
|
- inv->Values.batterySOC.value=s->SoC_estimator.SoC_percentage_smooth;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batterySOC.value));
|
|
|
- inv->Values.batterySOCtarget.value=(float)-1.0;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batterySOCtarget.value));
|
|
|
- inv->Values.batterySOH.value=(float)1.0;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batterySOH.value));
|
|
|
- inv->Values.batteryStatus.byte6=0;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryStatus.byte6));
|
|
|
- inv->Values.batteryTemperature.value=(float)s->maxCellTemp;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryTemperature.value));
|
|
|
- inv->Values.batteryVoltage.value=( (float)(s->systemVoltage)) /1000.0;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryVoltage.value));
|
|
|
- inv->Values.maxBatteryChargeCurrent.value=s->SoC_estimator.MaxBatteryChargeCurrent;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.maxBatteryChargeCurrent.value));
|
|
|
- inv->Values.maxBatteryChargeVoltage.value=s->startupConfig.maxBatteryVoltage;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.maxBatteryChargeVoltage.value));
|
|
|
- inv->Values.maxBatteryDischargeCurrent.value=s->SoC_estimator.MaxBatteryDischargeCurrent;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.maxBatteryDischargeCurrent.value));
|
|
|
- inv->Values.minBatteryDischargeVoltage.value=s->startupConfig.minBatteryVoltage;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.minBatteryDischargeVoltage.value));
|
|
|
-
|
|
|
- if(s->relayState.HS_closed == TRUE && s->relayState.LS_closed == TRUE && s->relayState.PRECHARGE_closed == TRUE) {
|
|
|
- inv->Values.batteryModeExtra.value = 0;
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- inv->Values.batteryModeExtra.value = 1;
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryModeExtra.value));
|
|
|
- }
|
|
|
- //inv->Values.batteryMode.value=(uint32_t)(s->RunMode);
|
|
|
- //shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryMode.value));
|
|
|
-}
|
|
|
-
|
|
|
-void shuffle_lsb_msb_can1(uint8_t* value) {
|
|
|
- uint8_t tempValue[4];
|
|
|
- tempValue[3]=value[0];
|
|
|
- tempValue[2]=value[1];
|
|
|
- tempValue[1]=value[2];
|
|
|
- tempValue[0]=value[3];
|
|
|
-
|
|
|
- value[0]=tempValue[0];
|
|
|
- value[1]=tempValue[1];
|
|
|
- value[2]=tempValue[2];
|
|
|
- value[3]=tempValue[3];
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-uint8_t check_if_all_values_are_initialized(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t slaveNr;
|
|
|
- uint8_t cellNr;
|
|
|
- for(slaveNr=0;slaveNr<CAN0_MAX_NR_OF_SLAVES-1;slaveNr++){
|
|
|
- if(s->Slave[slaveNr].SlaveConnectionState != NOT_CONNECTED) {
|
|
|
- for(cellNr=0;cellNr<MAX_SLAVE_CELLS;cellNr++) {
|
|
|
- if(s->Slave[slaveNr].CellConnectionState[cellNr]==CELL_CONNECTED) {
|
|
|
- if(s->Slave[slaveNr].CellVoltage[cellNr] == 0xffff) {
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- if(s->Slave[slaveNr].TempSensConnectionState[cellNr] == TEMP_SENSOR_CONNECTED) {
|
|
|
- if(s->Slave[slaveNr].CellTemp[cellNr] == -1) {
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief Handles communication with Slave-Boards over CAN0 interface
|
|
|
- * @param s pointer to state variable
|
|
|
- * @param time current timestamp in ms
|
|
|
- *
|
|
|
- * Master_CAN0_fsm is a finite state machine which handles the commuinication between Master and Slave boards.
|
|
|
- * It requests Data from the Slaves by sending an Request telegram, and saves the received data in the
|
|
|
- * provided MASTER_CAN0_STRUCT. It records CAN BUS Errors and hands these information to the Master_CAN0_ERROR_fsm.
|
|
|
- */
|
|
|
-uint16_t Master_CAN0_fsm(MASTER_CAN0_STRUCT_t* s,uint32_t time) {
|
|
|
- CAN_CONFIG *config;
|
|
|
- int8_t can_state=CAN_OK;
|
|
|
- uint8_t data_state=0;
|
|
|
- uint8_t ui_state=FALSE;
|
|
|
- switch(s->FsmState) {
|
|
|
- case INIT:
|
|
|
- s->slaveSelect=0;
|
|
|
- s->cycleTimestamp=time;
|
|
|
- s->FsmState=CHECK_IF_SLAVE_ACTIVE;
|
|
|
- s->cycleCounter++;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case CHECK_IF_SLAVE_ACTIVE:
|
|
|
- // transmitt data to Inverter
|
|
|
- if(s->SoC_initialized==TRUE && s->allValuesInitialized ==TRUE) {
|
|
|
- //Master_CAN1_Inverter_fsm( s,&(s->CAN1_fsmStruct)) ;
|
|
|
- Master_CAN1_select_comm_mode ( s,&(s->CAN1_fsmStruct)) ;
|
|
|
- }
|
|
|
- // Update Master temp sensor
|
|
|
- readMasterTempSensorFsm(s);
|
|
|
- s->timestamp=Global_1msCounter;
|
|
|
- if(s->Slave[s->slaveSelect].SlaveConnectionState == NOT_CONNECTED) {
|
|
|
- s->FsmState=WAIT_FOR_NEXT_SLAVE_TIMESLOT;
|
|
|
- s->slaveSelect++; // ignore and go to next slave
|
|
|
- if(s->slaveSelect>=CAN0_MAX_NR_OF_SLAVES) {
|
|
|
- s->slaveSelect=0;
|
|
|
- s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else{
|
|
|
- s->Slave[s->slaveSelect].SlaveTelegramsRecFlag=0; // no Telegrams recoredet yet
|
|
|
- s->FsmState=SEND_REQUEST_TELEGRAM;
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case SEND_REQUEST_TELEGRAM:
|
|
|
- s->transmission_pending=TRUE;
|
|
|
- CAN0_tx_send_request_telegram(&(s->Slave[s->slaveSelect]),s->Slave[s->slaveSelect].MasterAliveCnt);
|
|
|
- s->Slave[s->slaveSelect].MasterAliveCnt++;
|
|
|
- if(s->Slave[s->slaveSelect].MasterAliveCnt >=CAN0_NR_OF_TELEGRAMS) {
|
|
|
- s->Slave[s->slaveSelect].MasterAliveCnt=0;
|
|
|
- }
|
|
|
-
|
|
|
- if(s->Slave[s->slaveSelect].SlaveType==SLAVE) {
|
|
|
- s->tempSlave=s->Slave[s->slaveSelect];
|
|
|
- }
|
|
|
- else {
|
|
|
- s->tempSlave=s->Slave[s->slaveSelect];
|
|
|
- s->temp_UI_Board=s->UI_Board;
|
|
|
- }
|
|
|
- s->FsmState=WAIT_FOR_SLAVE_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case WAIT_FOR_SLAVE_RESPONSE:
|
|
|
- config= s->tempSlave.TxMailbox_ptr->p_CAN_Config;
|
|
|
- //can_state=CAN_Check_error_Register(config);
|
|
|
- if(s->tempSlave.SlaveType==SLAVE) {
|
|
|
- if(s->tempSlave.SlaveConnectionState == CONNECTED) {
|
|
|
- // real slave with real Sensors
|
|
|
- CAN0_check_if_slave_rec(&(s->tempSlave));
|
|
|
- }
|
|
|
- else if(s->tempSlave.SlaveConnectionState == DEBUG_DATA_SIMULATED) {
|
|
|
- // real slave with simulated Sensors
|
|
|
- CAN0_DEBUG_data_check_if_slave_rec((&s->tempSlave),&(gDUMMY_data_struct.Slave[s->slaveSelect]) );
|
|
|
- }
|
|
|
- else{
|
|
|
- // simulated Slave
|
|
|
- CAN0_DEBUG_slave_check_if_slave_rec((&s->tempSlave),&(gDUMMY_data_struct.Slave[s->slaveSelect]) );
|
|
|
- }
|
|
|
-
|
|
|
- //all Telegrams received
|
|
|
- if(s->tempSlave.SlaveTelegramsRecFlag==CAN0_ALL_TELEGRAMS_REC ) {
|
|
|
- //s->slaveSelect++;
|
|
|
- if(can_state == CAN_OK) {
|
|
|
- // all telegrams received => reset error counter
|
|
|
- if(s->slaveSelect == 3) {
|
|
|
- s->FsmState= CHECK_CAN_DATA;
|
|
|
- }
|
|
|
- s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
- s->FsmState= CHECK_CAN_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- s->FsmState =HANDLE_CAN_ERROR;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //timeout
|
|
|
- else if(s->timestamp + CAN0_TIMEOUT_MS <=Global_1msCounter) {
|
|
|
- s->FsmState =HANDLE_CAN_ERROR;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- // wait
|
|
|
- else {
|
|
|
- s->FsmState =WAIT_FOR_SLAVE_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
-
|
|
|
-
|
|
|
- if(s->tempSlave.SlaveConnectionState == CONNECTED) {
|
|
|
- // real UI with real Sensors
|
|
|
- ui_state=CAN0_check_if_UI_Board_rec( &(s->tempSlave) ,&(s->temp_UI_Board));
|
|
|
- }
|
|
|
- else if(s->tempSlave.SlaveConnectionState == DEBUG_DATA_SIMULATED) {
|
|
|
- // real UI with simulated Sensors
|
|
|
- ui_state=CAN0_DEBUG_data_check_if_UI_Board_rec(&(s->tempSlave),
|
|
|
- &(gDUMMY_data_struct.Slave[s->slaveSelect]),
|
|
|
- &(s->temp_UI_Board),
|
|
|
- &(gDUMMY_data_struct.UI_Board));
|
|
|
- }
|
|
|
- else{
|
|
|
- // simulated UI
|
|
|
- ui_state=CAN0_DEBUG_slave_check_if_UI_Board_rec(&(s->tempSlave),
|
|
|
- &(gDUMMY_data_struct.Slave[s->slaveSelect]),
|
|
|
- &(s->temp_UI_Board),
|
|
|
- &(gDUMMY_data_struct.UI_Board));
|
|
|
- }
|
|
|
-
|
|
|
- if(ui_state==TRUE) {
|
|
|
- //s->slaveSelect++;
|
|
|
- if(can_state==CAN_OK) {
|
|
|
- s->FsmState= CHECK_CAN_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- s->FsmState =HANDLE_CAN_ERROR;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- //return TRUE;
|
|
|
- }
|
|
|
- //timeout
|
|
|
- else if(s->timestamp + CAN0_TIMEOUT_MS <=time) {
|
|
|
- s->FsmState =HANDLE_CAN_ERROR;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- // wait
|
|
|
- else {
|
|
|
- s->FsmState =WAIT_FOR_SLAVE_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case CHECK_CAN_DATA:
|
|
|
- // check if rx data is correct
|
|
|
-
|
|
|
- if(s->tempSlave.SlaveType ==SLAVE ) {
|
|
|
-
|
|
|
- s->ErrorFlags=check_slave_data(s,&(s->Slave[s->slaveSelect]),
|
|
|
- &(s->tempSlave),
|
|
|
- BMS_SLAVE_MAX_TEMP,
|
|
|
- BMS_ERROR_THRESHOLD_T_DISCHARGE_MAX,
|
|
|
- BMS_SLAVE_MIN_TEMP,
|
|
|
- BMS_ERROR_THRESHOLD_T_DISCHARGE_MIN,
|
|
|
- BMS_SLAVE_MAX_CELL_VOLTAGE,
|
|
|
- BMS_SLAVE_MIN_CELL_VOLTAGE);
|
|
|
-
|
|
|
- if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_ALIVE_TIMEOUT) {
|
|
|
- s->FsmState =SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_OVER_VOLTAGE) {
|
|
|
- s->FsmState =SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_UNDER_VOLTAGE) {
|
|
|
- s->FsmState =SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- if((s->ErrorFlags & BMS_SLAVE_DATA_ERROR_TEMP_LIMIT) && (s->allValuesInitialized == TRUE)) {
|
|
|
- s->FsmState =SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_HEAT_SINK_LIMIT) {
|
|
|
- s->FsmState =SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_BOARD_ELECTRONIC) {
|
|
|
- s->FsmState =SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // everything ok
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- data_state=check_UI_data(&(s->Slave[s->slaveSelect]),
|
|
|
- &(s->tempSlave),
|
|
|
- &(s->UI_Board),
|
|
|
- &(s->temp_UI_Board));
|
|
|
-
|
|
|
- if(s->allValuesInitialized==FALSE) {
|
|
|
- s->allValuesInitialized=check_if_all_values_are_initialized(s);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // Check for "Soft" Errors and set Error states
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- s->transmission_pending=FALSE;
|
|
|
-
|
|
|
- s->FsmState= DO_CALCULATIONS;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case DO_CALCULATIONS:
|
|
|
- // calculate SoC SoH ?
|
|
|
- if(s->Slave[s->slaveSelect].SlaveType==SLAVE) {
|
|
|
- // calculate Slave Voltage
|
|
|
- s->Slave[s->slaveSelect].BlockVoltage=calc_block_voltage(&(s->Slave[s->slaveSelect]));
|
|
|
- // set min max Temp
|
|
|
- set_block_min_max_temp(&(s->Slave[s->slaveSelect]));
|
|
|
- // set min max Voltage
|
|
|
- calc_min_max_voltage_slave(&(s->Slave[s->slaveSelect]));
|
|
|
-
|
|
|
- s->slaveSelect++;
|
|
|
- s->FsmState=WAIT_FOR_NEXT_SLAVE_TIMESLOT;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // UI-Platine
|
|
|
- // calc SoC
|
|
|
- s->systemVoltage=calc_system_voltage(s);
|
|
|
- calc_min_max_voltage_system(s);
|
|
|
- set_system_min_max_temp(s);
|
|
|
- // update UI FIFO
|
|
|
- popUIFiFo(s);
|
|
|
-
|
|
|
- BMS_Set_Error_Check_voltage_inconsitency(s);
|
|
|
-
|
|
|
- // check if System voltage is too high
|
|
|
- if(s->allValuesInitialized ==TRUE) {
|
|
|
- // Check Umin 1 Umin2 Umin3
|
|
|
- BMS_Set_Error_check_Voltage_level_min(s);
|
|
|
- // Check Umax 1 and Umax 2
|
|
|
- BMS_Set_Error_check_Voltage_level_max(s) ;
|
|
|
- BMS_Set_Error_check_System_voltage_level_max(s);
|
|
|
-
|
|
|
- // check if CHECK Imax
|
|
|
- BMS_Set_Error_Check_derating(s);
|
|
|
-
|
|
|
-
|
|
|
- // make balancing decisions
|
|
|
- Master_Balancer_fsm(s);
|
|
|
- }
|
|
|
- // if in Winter MOde get SoC from FRAM
|
|
|
-// if(s->SoC_initialized==TRUE && s->allValuesInitialized ==FALSE && s->RunMode== RUN_MODE_WINTER) {
|
|
|
-// bms_SoC_init_estimator_FRAM(&(s->SoC_estimator), s->maxCellVoltage);
|
|
|
-// }
|
|
|
- // calculate SoC of higest and Lowest Cell
|
|
|
- if(s->SoC_initialized==FALSE && s->allValuesInitialized ==TRUE) {
|
|
|
- set_system_min_max_heatsink_temp(s);
|
|
|
- //set initial SoC
|
|
|
- if(s->startupConfig.state.Bit.SOC_Initialized) {
|
|
|
- // get initial SOC From FRAM
|
|
|
- bms_SoC_init_estimator_FRAM(&(s->SoC_estimator), s->maxCellVoltage);
|
|
|
- }
|
|
|
- else {
|
|
|
- bms_SoC_init_estimator(&(s->SoC_estimator), s->maxCellVoltage) ;
|
|
|
- }
|
|
|
- initSoCFifo(s);
|
|
|
- s->SoC_initialized=TRUE;
|
|
|
- s->FsmState=RUNNING_MODE_FSM;
|
|
|
- }
|
|
|
- else if(s->SoC_initialized==TRUE && s->allValuesInitialized ==TRUE) {
|
|
|
- set_system_min_max_heatsink_temp(s);
|
|
|
-
|
|
|
- // calc SoC
|
|
|
-
|
|
|
- while(s->SoC_estimator.state != BMS_SOC_READY) {
|
|
|
- bms_SoC_running_fsm( &(s->SoC_estimator),s->UI_Board.Ibatt*10 ,s->maxCellVoltage,s->minCellVoltage,s->minCellTemp,s->maxCellTemp);
|
|
|
- }
|
|
|
- s->SoC_estimator.state = BMS_SOC_IDLE ;
|
|
|
-
|
|
|
- // if soc is at 100% set SoC initialized Flag
|
|
|
- if(s->SoC_estimator.SoC_percentage_smooth > 0.9999) {
|
|
|
- s->startupConfig.state.Bit.SOC_Initialized=1;
|
|
|
- write_fram_set_startup_state(s);
|
|
|
- }
|
|
|
- // write SoC to FRAM
|
|
|
- else if(s->startupConfig.state.Bit.SOC_Initialized==1) {
|
|
|
- write_fram_set_SoC(s->SoC_estimator.SoC_percentage_smooth);
|
|
|
- }
|
|
|
- s->slaveSelect=0;
|
|
|
-
|
|
|
- // transmitt data to Inverter
|
|
|
- if(s->SoC_initialized==TRUE && s->allValuesInitialized ==TRUE) {
|
|
|
- refresh_inverter_tx_data(s);
|
|
|
- //Master_CAN1_Inverter_fsm( s,&(s->CAN1_fsmStruct)) ;
|
|
|
- }
|
|
|
- // set running Mode
|
|
|
-
|
|
|
- // update inverter state
|
|
|
- BMS_RCT_Inverter_fsm (s);
|
|
|
- s->FsmState=RUNNING_MODE_FSM;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // nothing happend yet
|
|
|
- s->FsmState=RUNNING_MODE_FSM;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case RUNNING_MODE_FSM:
|
|
|
- // set running Mode
|
|
|
- RunningModeFSM(s) ;
|
|
|
- s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case HANDLE_CAN_ERROR:
|
|
|
- // delete interrupt flags
|
|
|
- CAN0_clear_all_interrupt_flags();
|
|
|
- // handle CAN Errors
|
|
|
- config= s->tempSlave.TxMailbox_ptr->p_CAN_Config;
|
|
|
- //can_state=CAN_Check_error_Register(config);
|
|
|
- // all telegrams received, can telegrams disturbed
|
|
|
- if(s->tempSlave.SlaveTelegramsRecFlag==CAN0_ALL_TELEGRAMS_REC ) {
|
|
|
- // Telegram values could be disturbed => reject all received Data
|
|
|
- // Don't use newly received data => ignore tempSlave
|
|
|
- s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt++;
|
|
|
- if(s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt >=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
- s->FsmState=SHUT_DOWN_BMS;
|
|
|
-
|
|
|
- //write Error
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_CAN_ERROR,0,BMS_ERROR_CLASS_2);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- s->FsmState=DO_CALCULATIONS;
|
|
|
- return TRUE;
|
|
|
-
|
|
|
- }
|
|
|
- // telegrams missing, CAN Channel Ok
|
|
|
- else{
|
|
|
- if(can_state == CAN_OK) {
|
|
|
- // CAN BUS OK, Slave is not reacting
|
|
|
- // Don't use newly received data
|
|
|
- s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt++;
|
|
|
- if(s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt >=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
- //write Error
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_CAN_ERROR,0,BMS_ERROR_CLASS_2);
|
|
|
- s->FsmState=SHUT_DOWN_BMS;
|
|
|
- s->reset_test_timestamp=Global_1msCounter;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- s->FsmState=DO_CALCULATIONS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else{
|
|
|
- // CAN BUS ERROR, telegrams not coming thought
|
|
|
- // Don't use newly received data
|
|
|
- s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt++;
|
|
|
- if(s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt >=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
- //write Error
|
|
|
- ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_CAN_ERROR,0,BMS_ERROR_CLASS_2);
|
|
|
- s->reset_test_timestamp=Global_1msCounter;
|
|
|
- s->FsmState=SHUT_DOWN_BMS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- s->FsmState=DO_CALCULATIONS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case WAIT_FOR_NEXT_SLAVE_TIMESLOT:
|
|
|
- // next timesolt ready
|
|
|
-
|
|
|
- if(s->timestamp +CAN0_RASTER_MS <= time) {
|
|
|
- s->FsmState= CHECK_IF_SLAVE_ACTIVE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- //wait
|
|
|
- else{
|
|
|
- s->FsmState=WAIT_FOR_NEXT_SLAVE_TIMESLOT;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case SEND_RECEIVE_INVERTER_DATA:
|
|
|
- // send data to inverter and Wait for response
|
|
|
- //CAN1_tx_Data_to_Inverter(time,&(s->inverter.txStruct));
|
|
|
- //request DC INPUT VOLTAGE
|
|
|
- //CAN1_request_float_value(Global_1msCounter,&(s->inverter.rxStruct.DCinputA_power),CAN1_RX_DC_INPUT_A_VOLTAGE);
|
|
|
- s->startCan1Comm=TRUE;
|
|
|
- s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case WAIT_FOR_NEXT_COMMUNICATION_CYCLE:
|
|
|
- if(s->cycleTimestamp +CAN0_COMM_CYCLE_MS <= time) {
|
|
|
- s->FsmState= INIT;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case SHUT_DOWN_BMS:
|
|
|
- // Something went horribly wrong, turn off system
|
|
|
- SwitchRelais( LS_RELAIS, 0);
|
|
|
- SwitchRelais( PRE_CHARGE_RELAIS, 0);
|
|
|
- SwitchRelais( HS_RELAIS, 0);
|
|
|
- s->relayState.HS_closed=FALSE;
|
|
|
- s->relayState.LS_closed=FALSE;
|
|
|
- s->relayState.PRECHARGE_closed=FALSE;
|
|
|
- CLEAR_OUTPIN(PIN_REGNR_LED4); // set LED4 to 0 to indicate that an Error has occured
|
|
|
-
|
|
|
- //write_fram_word(BMS_STARTUP_ERROR_MODE_3,3,BMS_STARTUP_MODE_ADDR);
|
|
|
- // go to Error Mode
|
|
|
-
|
|
|
- // continue communication cycles
|
|
|
- s->FsmState=DO_CALCULATIONS;
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- default:
|
|
|
- return FALSE;
|
|
|
- break;
|
|
|
- }
|
|
|
- return FALSE;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-uint16_t init_master_operation_fsm(BMS_MASTER_OPERATION_t* opFsm) {
|
|
|
- opFsm->FsmState=MASTER_OPERATION_INIT;
|
|
|
- opFsm->timestamp=0;
|
|
|
-
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-uint16_t Master_CAN1_fsm_init(MASTER_CAN1_STRUCT_t* can1Fsm) {
|
|
|
- can1Fsm->delay =3;
|
|
|
- can1Fsm->fsmState=CAN1_INIT;
|
|
|
- can1Fsm->txMsgNr=0;
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- *
|
|
|
- */
|
|
|
-uint32_t Master_CAN1_select_comm_mode (MASTER_CAN0_STRUCT_t* s,MASTER_CAN1_INVERTER_STRUCT_t* fsmStruct) {
|
|
|
- if(fsmStruct->slowRequestFsmRunning == TRUE) {
|
|
|
- // slow requests are running => wait till requests are complete
|
|
|
- Master_CAN1_Inverter_fsm(s,fsmStruct);
|
|
|
- // set fast request running flag for faast request asap
|
|
|
- fsmStruct->fastRequestFsmRunning=TRUE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(s->Slave[s->slaveSelect].SlaveType== UI || fsmStruct->fastRequestFsmRunning==TRUE) {
|
|
|
- // commuincation with UI is about to begin or fast request flag is set
|
|
|
- // this means 250ms tick is reached => initiate fast request if possible
|
|
|
- Master_CAN1_Fast_request_fsm (s,fsmStruct);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // nothing special run normal fsm
|
|
|
- Master_CAN1_Inverter_fsm(s,fsmStruct);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-}
|
|
|
-/*
|
|
|
- * handles request of battery current and battery voltage from inverter
|
|
|
- * which have to be sampled regulatly
|
|
|
- */
|
|
|
-uint32_t Master_CAN1_Fast_request_fsm (MASTER_CAN0_STRUCT_t* s,MASTER_CAN1_INVERTER_STRUCT_t* fsmStruct) {
|
|
|
- switch(fsmStruct->fastRxState) {
|
|
|
- case CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT:
|
|
|
- // request battery current from inverter
|
|
|
- fsmStruct->nrOfRecCurrentSamples++;
|
|
|
- CAN1_send_request_telegram(CAN1_RX_BATTERY_CURRENT);
|
|
|
- fsmStruct->fastRequestFsmRunning=TRUE;
|
|
|
- fsmStruct->fastRxState=CAN1_FAST_RX_FSM_REQUEST_BATTERY_VOLTAGE;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case CAN1_FAST_RX_FSM_REQUEST_BATTERY_VOLTAGE:
|
|
|
- // readout battery current and request battery voltage
|
|
|
- if(CAN1_wait_for_response(&(s->inverter.rxStruct.batteryCurrent)) == TRUE ) {
|
|
|
- // battery current received
|
|
|
- pushInverterCurrentFIFO(s);
|
|
|
- // compare UI Current and Inverter Current
|
|
|
- if(fsmStruct->nrOfRecCurrentSamples > UI_CURRENT_FIFO_SIZE) {
|
|
|
- // BMS_Set_Error_Check_current_consitency(s); //compare current of UI with Inverter
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- // no response in 10ms => timeout
|
|
|
- // go to normal communication fsm
|
|
|
- fsmStruct->fastRxState = CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
- fsmStruct->fastRequestFsmRunning=FALSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- // request battery voltage
|
|
|
- CAN1_send_request_telegram(CAN1_RX_BATTERY_VOLTAGE);
|
|
|
- fsmStruct->fastRxState =CAN1_FAST_RX_FSM_REQUEST_COMPLETE;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case CAN1_FAST_RX_FSM_REQUEST_COMPLETE:
|
|
|
- // readout voltage
|
|
|
- if(CAN1_wait_for_response(&(s->inverter.rxStruct.batteryVoltage)) == TRUE ) {
|
|
|
- // battery voltage received
|
|
|
- }
|
|
|
- else {
|
|
|
- // no response in 10ms => timeout
|
|
|
- // go to normal communication fsm
|
|
|
- fsmStruct->fastRxState = CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
- fsmStruct->fastRequestFsmRunning=FALSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- // fast requests done
|
|
|
- fsmStruct->fastRxState = CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
- fsmStruct->fastRequestFsmRunning=FALSE;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-uint32_t Master_CAN1_Inverter_fsm(MASTER_CAN0_STRUCT_t* s,MASTER_CAN1_INVERTER_STRUCT_t* fsmStruct) {
|
|
|
- BMS_CAN1_INVERTER_TX* data = &(s->inverter.txStruct);
|
|
|
- BMS_CAN1_INVERTER_CELL_DATA_t txPkg;
|
|
|
- float* rx_ptr=&(s->inverter.rxStruct.expectedInputPower); // points to rx_address to sotre inverter value
|
|
|
- uint8_t eightBitBuff[8]= {0,0,0,0,0,0,0,0} ;
|
|
|
- switch(fsmStruct->fsmState) {
|
|
|
- case CAN1_FSM_INIT:
|
|
|
- // reset Message counters
|
|
|
- fsmStruct->highPrioMsgNr=0;
|
|
|
- fsmStruct->lowPrioMsgNr=0;
|
|
|
- fsmStruct->requestTelegramNr=0;
|
|
|
- fsmStruct->timeoutCyclesCnt=0;
|
|
|
-
|
|
|
-
|
|
|
- // check if all Values are initialized
|
|
|
- // if yes start transmitting
|
|
|
- if(s->allValuesInitialized == TRUE) {
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- // not all values are initialized
|
|
|
- //=> stay in INIT state
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case CAN1_FSM_SEND_HIGH_PRIO_DATA:
|
|
|
- // rx process complete
|
|
|
- fsmStruct->slowRequestFsmRunning=FALSE;
|
|
|
-
|
|
|
- // transmit high priority data, which is
|
|
|
- // maximum charge Current
|
|
|
- // maximum Charge Voltage
|
|
|
- // maximum Discharge Current
|
|
|
- // minimum Discharge Voltage
|
|
|
- // battery Current
|
|
|
- // battery Voltage
|
|
|
- // Battery SoC
|
|
|
- // Battery Capacity
|
|
|
- // Battery Temperature
|
|
|
- // Battery SoH
|
|
|
- // Battery Status
|
|
|
- // Battery SoC Target
|
|
|
-
|
|
|
- if(fsmStruct->highPrioMsgNr == 0) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.maxBatteryChargeCurrent),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 1) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.maxBatteryChargeVoltage),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 2) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.maxBatteryDischargeCurrent),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 3) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.minBatteryDischargeVoltage),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 4) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryCurrent),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 5) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryVoltage),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 7) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batterySOC),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 8) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryCapacity),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 9) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryTemperature),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 10) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batterySOH),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 11) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batterySOCtarget),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 12) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryMode),Global_1msCounter);
|
|
|
- }
|
|
|
- else if(fsmStruct->highPrioMsgNr == 13) {
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryModeExtra),Global_1msCounter);
|
|
|
- }
|
|
|
-
|
|
|
- // increment high prio msg cnt
|
|
|
- fsmStruct->highPrioMsgNr++ ;
|
|
|
-
|
|
|
- // if all high prio msg are transmitted start transmitting low prio msg
|
|
|
- if(fsmStruct->highPrioMsgNr > 13) {
|
|
|
- fsmStruct->highPrioMsgNr=0;
|
|
|
- // reset error cnt to start with error 1 again
|
|
|
- fsmStruct->transmitErrorNrEs1=0;
|
|
|
- fsmStruct->transmitErrorNrEs2=0;
|
|
|
- fsmStruct->transmitErrorNrEs3=0;
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_ES_1;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // continue Transmitting high Prio Msg
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case CAN1_FSM_SEND_ES_1:
|
|
|
- // send Error Calls 1 Errors
|
|
|
-
|
|
|
- if( !ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1) && !ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2) && ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_3) ) {
|
|
|
- // no Error has occured Transmitt everything ok
|
|
|
- CLEAR_OUTPIN(PIN_REGNR_LED4);
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryStatus),Global_1msCounter);
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_LOW_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_3) && fsmStruct->transmitErrorNrEs3 < BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
- // transmitt ES 3 Errors
|
|
|
- // check if Error is active
|
|
|
- SET_OUTPIN(PIN_REGNR_LED4);
|
|
|
- while(s->ErrorBuffer.ES3_Error[fsmStruct->transmitErrorNrEs3].Master.active == 0){
|
|
|
- // Error no ,longer Active go to next entry
|
|
|
- fsmStruct->transmitErrorNrEs3++;
|
|
|
- if(fsmStruct->transmitErrorNrEs3 >=BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
- ErrorStackGenerateStatusPkg(eightBitBuff,(uint8_t*)&(s->ErrorBuffer.ES3_Error[fsmStruct->transmitErrorNrEs3]));
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, eightBitBuff,Global_1msCounter);
|
|
|
- fsmStruct->transmitErrorNrEs3++;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2) && fsmStruct->transmitErrorNrEs2 < BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
- // transmitt ES 2 Errors
|
|
|
- // check if Error is active
|
|
|
- SET_OUTPIN(PIN_REGNR_LED4);
|
|
|
- while(s->ErrorBuffer.ES2_Error[fsmStruct->transmitErrorNrEs2].Master.active == 0){
|
|
|
- // Error no ,longer Active go to next entry
|
|
|
- fsmStruct->transmitErrorNrEs2++;
|
|
|
- if(fsmStruct->transmitErrorNrEs2 >=BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
- ErrorStackGenerateStatusPkg(eightBitBuff,(uint8_t*)&(s->ErrorBuffer.ES2_Error[fsmStruct->transmitErrorNrEs2]));
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, eightBitBuff,Global_1msCounter);
|
|
|
- fsmStruct->transmitErrorNrEs2++;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1) && fsmStruct->transmitErrorNrEs1 < BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
- // transmitt ES 1 Errors
|
|
|
- // check if Error is active
|
|
|
- SET_OUTPIN(PIN_REGNR_LED4);
|
|
|
- while(s->ErrorBuffer.ES1_Error[fsmStruct->transmitErrorNrEs1].Master.active == 0){
|
|
|
- // Error no ,longer Active go to next entry
|
|
|
- fsmStruct->transmitErrorNrEs1++;
|
|
|
- if(fsmStruct->transmitErrorNrEs1 >=BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
- ErrorStackGenerateStatusPkg(eightBitBuff,(uint8_t*)&(s->ErrorBuffer.ES1_Error[fsmStruct->transmitErrorNrEs1]));
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, eightBitBuff,Global_1msCounter);
|
|
|
- fsmStruct->transmitErrorNrEs1++;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_LOW_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case CAN1_FSM_SEND_LOW_PRIO_DATA:
|
|
|
- // send low Priority data which is
|
|
|
- // Cell temperature and Cell Voltages
|
|
|
- generateCellInfoPkg(s, fsmStruct->lowPrioMsgNr,&txPkg);
|
|
|
- CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(txPkg),Global_1msCounter);
|
|
|
- fsmStruct->lowPrioMsgNr++ ;
|
|
|
- if(fsmStruct->lowPrioMsgNr >= (s->NrOfSlaves)*MAX_SLAVE_CELLS) {
|
|
|
- // start with cell nr 0 again
|
|
|
- fsmStruct->lowPrioMsgNr=0;
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_REQUEST_DATA;
|
|
|
- fsmStruct->requestTelegramNr=0;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_LOW_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case CAN1_FSM_SEND_REQUEST_DATA:
|
|
|
- // request data from RCT Inverter
|
|
|
- // Set flag so request process cant be interrupted
|
|
|
- fsmStruct->slowRequestFsmRunning=TRUE;
|
|
|
- if(fsmStruct->requestTelegramNr==0) {
|
|
|
- CAN1_send_request_telegram(CAN1_RX_TOTAL_DC_PWR);
|
|
|
- fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==1) {
|
|
|
- CAN1_send_request_telegram(CAN1_RX_DC_INPUT_A_POWER);
|
|
|
- fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==2) {
|
|
|
- CAN1_send_request_telegram(CAN1_RX_DC_INPUT_B_POWER);
|
|
|
- fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==3) {
|
|
|
- CAN1_send_request_telegram(CAN1_RX_DC_INPUT_A_POWER);
|
|
|
- fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==4) {
|
|
|
- CAN1_send_request_telegram(CAN1_RX_DC_INPUT_B_POWER);
|
|
|
- fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // you should not be here
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case CAN1_FSM_WAIT_FOR_RESPONSE:
|
|
|
- // to be updateted for more values
|
|
|
- if(fsmStruct->requestTelegramNr==0) {
|
|
|
- rx_ptr=&(s->inverter.rxStruct.expectedInputPower);
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==1) {
|
|
|
- rx_ptr=&(s->inverter.rxStruct.DCinputA_power);
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==2) {
|
|
|
- rx_ptr=&(s->inverter.rxStruct.DCinputB_power);
|
|
|
- }
|
|
|
- else if(fsmStruct->requestTelegramNr==3) {
|
|
|
- // not valid anymore
|
|
|
- rx_ptr=&(s->inverter.rxStruct.DCinputA_power);
|
|
|
- }
|
|
|
- else {
|
|
|
- // not valid anymore
|
|
|
- rx_ptr=&(s->inverter.rxStruct.DCinputB_power);
|
|
|
- }
|
|
|
- if(CAN1_wait_for_response(rx_ptr) == TRUE ) {
|
|
|
- fsmStruct->requestTelegramNr++;
|
|
|
- fsmStruct->receivedTelegrams++;
|
|
|
- if(fsmStruct->requestTelegramNr >=CAN1_NR_OF_REQUEST_TELEGRAMS) {
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
- fsmStruct->timeoutCyclesCnt=0;
|
|
|
- s->inverterState.inverterCanOnline= TRUE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_REQUEST_DATA;
|
|
|
- fsmStruct->timeoutCyclesCnt=0;
|
|
|
- s->inverterState.inverterCanOnline= TRUE;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- }
|
|
|
- else{
|
|
|
- fsmStruct->timeoutCyclesCnt++;
|
|
|
- }
|
|
|
-
|
|
|
- if(fsmStruct->timeoutCyclesCnt >5) {
|
|
|
- // timeout
|
|
|
- fsmStruct->receivedTelegrams=0;
|
|
|
- //fsmStruct->timeoutCyclesCnt=0;
|
|
|
-
|
|
|
- // set Error
|
|
|
- BMS_Set_Error_CAN1_Timeout(s) ;
|
|
|
- // set inverter fsm to can out
|
|
|
- s->inverterState.inverterCanOnline= FALSE;
|
|
|
- fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- // wait another 10ms for response
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- default :
|
|
|
- // you should not be here
|
|
|
- return FALSE;
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * @brief generate a pkg with value ID (32bit) Cell Index (8-bit) voltage (16 bit) temperature (8-bit)
|
|
|
- */
|
|
|
-uint32_t generateCellInfoPkg(MASTER_CAN0_STRUCT_t* s, uint8_t cellIndex,BMS_CAN1_INVERTER_CELL_DATA_t* txPkg) {
|
|
|
- // collect information
|
|
|
- int8_t cellTemp;
|
|
|
- uint16_t cellVoltage;
|
|
|
- uint8_t SlaveNr;
|
|
|
- uint8_t cellNr;
|
|
|
- uint8_t payload[4];
|
|
|
-
|
|
|
-
|
|
|
- uint32_t valueId=CAN1_TX_CELL_STATUS;
|
|
|
-
|
|
|
- // shuffle Bits for correct Order
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&valueId);
|
|
|
-
|
|
|
- SlaveNr = cellIndex/ (MAX_SLAVE_CELLS); // Slave Nr 0...14
|
|
|
- cellNr = cellIndex - SlaveNr * MAX_SLAVE_CELLS ;// cellNr 0... 24
|
|
|
-
|
|
|
- cellVoltage = s->Slave[SlaveNr].CellVoltage[cellNr];
|
|
|
-
|
|
|
- if(s->Slave[SlaveNr].TempSensConnectionState[cellNr] == TEMP_SENSOR_CONNECTED) {
|
|
|
- cellTemp = s->Slave[SlaveNr].CellTemp[cellNr] ;
|
|
|
- }
|
|
|
- else {
|
|
|
- // if temp sensor not connected transmit -51 deg
|
|
|
- cellTemp = -51 ;
|
|
|
- }
|
|
|
- payload[0]=cellIndex;
|
|
|
- payload[1]= cellVoltage >> 8;
|
|
|
- payload[2]= cellVoltage & 0xFF;
|
|
|
- payload[3]= (uint8_t)cellTemp;
|
|
|
-
|
|
|
- shuffle_lsb_msb_can1((uint8_t*)&(payload[0]));
|
|
|
-
|
|
|
- txPkg->valueId=valueId;
|
|
|
- txPkg->payload= (payload[0] << 24) + (payload[1] << 16) + (payload[2] << 8) + payload[3] ;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-uint32_t Master_Balancer_fsm(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- switch(s->balancerState) {
|
|
|
- case BMS_BALANCE_INIT:
|
|
|
- if(s->allValuesInitialized== TRUE && (s->maxCellVoltage >=BMS_SLAVE_MAX_BALANCE_VOLTAGE)) {
|
|
|
- // cellvoltage of system too high => no Balancing
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- else if(s->SoC_estimator.SoC_percentage_smooth <= BMS_SLAVE_MIN_BALANCE_SOC) {
|
|
|
- // SoC under 80% do not balance
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- else if(s->maxHeatSinkTemp >= BMS_SLAVE_MAX_BALANCE_HEATSINK_TEMP) {
|
|
|
- // Heatsink too hot => do not balance
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- else if(s->allValuesInitialized== TRUE && (s->UI_Board.Ibatt > BMS_SLAVE_BATTERY_CHARGE_THERESOLD) ) {
|
|
|
- // battery discharging => no Balancing
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- else if(s->allValuesInitialized== TRUE && (s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD)) {
|
|
|
- // ready for balancing
|
|
|
- s->balancerState=BMS_BALANCE_GET_VOLTAGE;
|
|
|
- }
|
|
|
-
|
|
|
- else {
|
|
|
- // do nothing
|
|
|
- }
|
|
|
-
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case BMS_BALANCE_GET_VOLTAGE:
|
|
|
- // decide Which cells to balance
|
|
|
- if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
- set_balancer(s);
|
|
|
- s->balancerState=BMS_BALANCE_BALANCE_CELLS;
|
|
|
- }
|
|
|
- else {
|
|
|
- set_balancer_off(s) ;
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case BMS_BALANCE_BALANCE_CELLS:
|
|
|
- // turn off balancing to obtain uneffected cell voltages
|
|
|
- if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
- set_balancer_off(s) ;
|
|
|
- s->balancerState=BMS_BALANCE_COOL;
|
|
|
- }
|
|
|
- else {
|
|
|
- set_balancer_off(s) ;
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case BMS_BALANCE_COOL:
|
|
|
- // uneffected cell voltages are now available
|
|
|
- if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
- s->balancerState=BMS_BALANCE_INIT;
|
|
|
- }
|
|
|
- else {
|
|
|
- set_balancer_off(s) ;
|
|
|
- s->balancerState=BMS_BALANCE_OFF;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case BMS_BALANCE_OFF:
|
|
|
- set_balancer_off(s) ;
|
|
|
- if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
- s->balancerState=BMS_BALANCE_INIT;
|
|
|
- }
|
|
|
- s->balancerState=BMS_BALANCE_INIT;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-uint8_t get_nr_of_connected_slaves(MASTER_CAN0_STRUCT_t* s){
|
|
|
- uint8_t i=0;
|
|
|
- uint8_t nr=0;
|
|
|
- for(i=0;i<CAN0_MAX_NR_OF_SLAVES-1;i++) {
|
|
|
- if(s->Slave[i].SlaveConnectionState== CONNECTED) {
|
|
|
- nr++;
|
|
|
- }
|
|
|
- }
|
|
|
- return nr;
|
|
|
-}
|
|
|
-
|
|
|
-uint32_t RunningModeFSM(MASTER_CAN0_STRUCT_t* s){
|
|
|
-
|
|
|
-
|
|
|
- // check for active Error States
|
|
|
- if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_3) || s->RunMode.ErrorState3fsm != ES3_FSM_INIT) {
|
|
|
- // handle Error Class 3
|
|
|
- BMS_Master_ES3_fsm(s);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2) || s->RunMode.ErrorState2fsm != ES2_FSM_INIT) {
|
|
|
- // handle Error Class 2
|
|
|
- BMS_Master_ES2_fsm(s);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1) || s->RunMode.ErrorState1fsm != ES1_FSM_INIT) {
|
|
|
- // handle Error Class 1
|
|
|
- BMS_Master_ES1_fsm(s);
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
- else {
|
|
|
- // normal operation
|
|
|
- OPModeFSM( s );
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-uint32_t OPModeFSM(MASTER_CAN0_STRUCT_t* s){
|
|
|
- float SoC=s->SoC_estimator.SoC_percentage_smooth;
|
|
|
- switch(s->RunMode.OperationMode) {
|
|
|
- case OP_MODE_INIT:
|
|
|
- // just jump to checkup startup conditions
|
|
|
- s->RunMode.OperationMode=OP_MODE_CHECK_STARTUP_CONDITIONS;
|
|
|
-
|
|
|
- CLEAR_OUTPIN(PIN_REGNR_LED4);
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case OP_MODE_CHECK_STARTUP_CONDITIONS:
|
|
|
- if(s->allValuesInitialized==TRUE) {
|
|
|
- // all values initalized and no error occured
|
|
|
- // => Startup conditions apply => start startup
|
|
|
- s->RunMode.OperationModeTimestamp=Global_1msCounter;
|
|
|
- s->RunMode.OperationMode=OP_MODE_SET_PRECHARGE_RELAY;
|
|
|
- s->RunMode.onCounter=0;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // wait till all values are initialiezd
|
|
|
- s->RunMode.OperationMode=OP_MODE_CHECK_STARTUP_CONDITIONS;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case OP_MODE_SET_PRECHARGE_RELAY:
|
|
|
- // set Low Side and Precharge Relais
|
|
|
- s->relayState.LS_closed=TRUE;
|
|
|
- s->relayState.PRECHARGE_closed=TRUE;
|
|
|
- SwitchRelais( LS_RELAIS, 1);
|
|
|
- SwitchRelais( PRE_CHARGE_RELAIS, 1);
|
|
|
- if(s->RunMode.OperationModeTimestamp + 3000 <= Global_1msCounter) {
|
|
|
- s->RunMode.OperationMode=OP_MODE_SET_MAIN_RELAY;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- s->RunMode.OperationMode= OP_MODE_SET_PRECHARGE_RELAY;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- case OP_MODE_SET_MAIN_RELAY:
|
|
|
- // Switch Highside relais
|
|
|
- SwitchRelais( HS_RELAIS, 1);
|
|
|
- s->relayState.HS_closed=TRUE;
|
|
|
- s->RunMode.OperationMode=OP_MODE_NORMAL;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case OP_MODE_NORMAL:
|
|
|
- // do nothing until SoC becomes low
|
|
|
- s->RunMode.onCounter++;
|
|
|
- if ( SoC < BMS_SOC_LOW_MARK) {
|
|
|
- s->RunMode.OperationMode=OP_MODE_SOC_LOW;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- s->RunMode.OperationMode=OP_MODE_NORMAL;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case OP_MODE_SOC_LOW:
|
|
|
- s->RunMode.onCounter++;
|
|
|
- // do nothing until SoC becomes high again
|
|
|
- if ( SoC > BMS_SOC_LOW_MARK) {
|
|
|
- s->RunMode.OperationMode=OP_MODE_NORMAL;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- s->RunMode.OperationMode=OP_MODE_SOC_LOW;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- // something went horribly wrong
|
|
|
- return FALSE;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-/*
|
|
|
- * @brief Statemachine which handles Class 1 Errors
|
|
|
- */
|
|
|
-
|
|
|
-uint32_t BMS_Master_ES1_fsm(MASTER_CAN0_STRUCT_t* s){
|
|
|
- switch (s->RunMode.ErrorState1fsm) {
|
|
|
- case ES1_FSM_INIT:
|
|
|
- s->RunMode.ErrorState1fsm=ES1_FSM_CHECK_IF_ERROR_VALID;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES1_FSM_CHECK_IF_ERROR_VALID:
|
|
|
- // Try to clear Error
|
|
|
- BMS_Clear_Error_Buffer(s,BMS_ERROR_CLASS_1);
|
|
|
-
|
|
|
-
|
|
|
- if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1)) {
|
|
|
- // Error still active
|
|
|
- // Don't do anything
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- // No Error
|
|
|
- s->RunMode.ErrorState1fsm=ES1_FSM_ERROR_REVOKED;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES1_FSM_ERROR_REVOKED:
|
|
|
- // No More Errors apply startup system again
|
|
|
- s->RunMode.OperationMode=OP_MODE_INIT;
|
|
|
- // set Fsm Back to INIT
|
|
|
- s->RunMode.ErrorState1fsm=ES1_FSM_INIT;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-uint32_t BMS_Master_ES2_fsm(MASTER_CAN0_STRUCT_t* s){
|
|
|
- switch(s->RunMode.ErrorState2fsm) {
|
|
|
- case ES2_FSM_INIT:
|
|
|
- // save time for Timeout timer
|
|
|
- s->RunMode.ErrorState2Timestamp=Global_1msCounter;
|
|
|
- if(s->ErrorBuffer.ES2_New_Error==FALSE) {
|
|
|
- // System had power cycle, Try to fix Error
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_CHECK_IF_ERROR_VALID;
|
|
|
- }
|
|
|
- else {
|
|
|
- // no restart => wait timeout then kill system
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_WAIT_FOR_SHUTDOWN;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES2_FSM_CHECK_IF_ERROR_VALID:
|
|
|
- // Try to clear Error
|
|
|
- BMS_Clear_Error_Buffer(s,BMS_ERROR_CLASS_2);
|
|
|
- if(!ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2)) {
|
|
|
- // all Errors cleared
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_ERROR_REVOKED;
|
|
|
- }
|
|
|
- else if(s->RunMode.ErrorState2Timestamp + BMS_ERROR_FSM_ES2_TIMEOUT < Global_1msCounter ) {
|
|
|
- // Timeout shutdown system
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_SYSTEM_SHUTDOWN;
|
|
|
- }
|
|
|
- else{
|
|
|
- // do nothing an wait for timeout or error to be cleared
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES2_FSM_WAIT_FOR_SHUTDOWN:
|
|
|
- // wait timeout
|
|
|
- if(s->RunMode.ErrorState2Timestamp + BMS_ERROR_FSM_ES2_TIMEOUT < Global_1msCounter ) {
|
|
|
- // timeout
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_SYSTEM_SHUTDOWN;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- // wait and do nothing
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES2_FSM_ERROR_REVOKED:
|
|
|
- // no more Errors in the system => Start normal Operation again
|
|
|
- s->RunMode.ErrorState2fsm=ES2_FSM_INIT;
|
|
|
- s->RunMode.OperationMode=OP_MODE_INIT;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES2_FSM_SYSTEM_SHUTDOWN:
|
|
|
- // Turn off power Supply
|
|
|
- SwitchRelais( PWR_SUPPLY, 1);
|
|
|
- // now System should be dead
|
|
|
- while (1) {
|
|
|
- // loop for ever
|
|
|
- SwitchRelais( PWR_SUPPLY, 1);
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * @brief handle error class 3
|
|
|
- * Todo System repair tool not implemented yet
|
|
|
- */
|
|
|
-uint32_t BMS_Master_ES3_fsm(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- switch(s->RunMode.ErrorState3fsm){
|
|
|
- case ES3_FSM_INIT:
|
|
|
- // save timestamp for timeout
|
|
|
- s->RunMode.ErrorState3Timestamp=Global_1msCounter;
|
|
|
- s->RunMode.ErrorState3fsm=ES3_FSM_CONNECT_TO_SERVICE_TOOL;
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES3_FSM_CONNECT_TO_SERVICE_TOOL:
|
|
|
- // not fully implemented yet
|
|
|
- // just shut down system after timeout
|
|
|
- if(s->RunMode.ErrorState3Timestamp + BMS_ERROR_FSM_ES3_TIMEOUT < Global_1msCounter ) {
|
|
|
- // Shut down System
|
|
|
- s->RunMode.ErrorState3fsm=ES3_FSM_SYSTEM_SHUTDOWN;
|
|
|
- }
|
|
|
- else {
|
|
|
- // wait
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case ES3_FSM_SYSTEM_SHUTDOWN:
|
|
|
- // Turn off power Supply
|
|
|
- SwitchRelais( PWR_SUPPLY, 1);
|
|
|
- // now System should be dead
|
|
|
- while (1) {
|
|
|
- // loop for ever
|
|
|
- SwitchRelais( PWR_SUPPLY, 1);
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * @brief check if connected Inverter has enough power to charge battery and recover from Wintermode
|
|
|
- */
|
|
|
-
|
|
|
-int32_t checkIfInverterHasPower(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- if((s->inverter.rxStruct.expectedInputPower) > BMS_WINTER_MODE_RECOVER_PWR) {
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else {
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * @brief read Temp sensor on master Board
|
|
|
- */
|
|
|
-uint32_t readMasterTempSensorFsm(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- switch(s->MasterTempSensState) {
|
|
|
- case BMS_MASTER_TEMP_SENSOR_INITIATE_MEASUREMENT:
|
|
|
- // trigger measurement
|
|
|
- if(TempMess_triggerRead() == 0) {
|
|
|
- // measurement triggered got to next state
|
|
|
- s->MasterTempSensState=BMS_MASTER_TEMP_SENSOR_UPDATE_MEASUREMENT;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- case BMS_MASTER_TEMP_SENSOR_UPDATE_MEASUREMENT:
|
|
|
- TempMess_update();
|
|
|
- if(TempMess_poll_Value(&(s->masterTemp)) == 0) {
|
|
|
- // readout succesfull go back to triggering measurement
|
|
|
- s->MasterTempSensState=BMS_MASTER_TEMP_SENSOR_INITIATE_MEASUREMENT;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * @brief clear fifos
|
|
|
- */
|
|
|
-
|
|
|
-uint32_t initUIFifo(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t i;
|
|
|
- for(i=0;i<UI_VOLTAGE_FIFO_SIZE;i++) {
|
|
|
- s->UI_Board.UbattFiFo[i]=0;
|
|
|
- s->UI_Board.SystemVoltageFiFo[i]=0;
|
|
|
- }
|
|
|
- for(i=0;i<UI_CURRENT_FIFO_SIZE;i++) {
|
|
|
- s->UI_Board.IbattFiFo[i]=0;
|
|
|
- s->UI_Board.Ibatt_Inverter_FIFO[i]=0;
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-uint32_t popUIFiFo(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t i;
|
|
|
-
|
|
|
- for(i=UI_VOLTAGE_FIFO_SIZE-1;i>0;i--) {
|
|
|
- s->UI_Board.UbattFiFo[i]=s->UI_Board.UbattFiFo[i-1];
|
|
|
- }
|
|
|
- s->UI_Board.UbattFiFo[0]=s->UI_Board.Ubatt;
|
|
|
-
|
|
|
- for(i=UI_VOLTAGE_FIFO_SIZE-1;i>0;i--) {
|
|
|
- s->UI_Board.SystemVoltageFiFo[i]=s->UI_Board.SystemVoltageFiFo[i-1];
|
|
|
- }
|
|
|
- s->UI_Board.SystemVoltageFiFo[0]=(s->systemVoltage);
|
|
|
-
|
|
|
- for(i=UI_CURRENT_FIFO_SIZE-1;i>0;i--) {
|
|
|
- s->UI_Board.IbattFiFo[i]=s->UI_Board.IbattFiFo[i-1];
|
|
|
- }
|
|
|
- s->UI_Board.IbattFiFo[0]=s->UI_Board.Ibatt;
|
|
|
-}
|
|
|
-
|
|
|
-uint32_t pushInverterCurrentFIFO(MASTER_CAN0_STRUCT_t* s) {
|
|
|
- uint8_t i;
|
|
|
- for(i=UI_CURRENT_FIFO_SIZE-1;i>0;i--) {
|
|
|
- s->UI_Board.Ibatt_Inverter_FIFO[i]=s->UI_Board.Ibatt_Inverter_FIFO[i-1];
|
|
|
- }
|
|
|
- s->UI_Board.Ibatt_Inverter_FIFO[0]=s->inverter.rxStruct.batteryCurrent;
|
|
|
-
|
|
|
-// if(s->inverter.rxStruct.batteryCurrent > 100 || s->inverter.rxStruct.batteryCurrent < -100) {
|
|
|
-// // fake value
|
|
|
-// i++ ; // break here
|
|
|
-// }
|
|
|
- return TRUE;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-// *** End BMS_Master.c ******************************************************
|
|
|
+//##############################################################################
|
|
|
+//
|
|
|
+// FILE: BMS_Master.c
|
|
|
+//
|
|
|
+// TITLE: Functions of BMS_Master
|
|
|
+// void BMS_Init_BSD ( void );
|
|
|
+// void BMS_Init_BSE ( void );
|
|
|
+// void SwitchRelais ( uint8_t, uint8_t );
|
|
|
+// void SetBalancer ( void );
|
|
|
+// uint16_t BMS_Do ( void );
|
|
|
+//
|
|
|
+//
|
|
|
+//##############################################################################
|
|
|
+
|
|
|
+//==============================================================================
|
|
|
+// Historie:
|
|
|
+//==============================================================================
|
|
|
+// Datum: | Name | Version:| Aenderungsgrund: | rev.:
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+// | SB | 2.1 | Implementaion of Balancing and SoC Estimator | 003
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+// | SB | 2.0 | Adaptation for RCT | 002
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+// 01.07.13 | VR | 1.1 | Rearrange Code for IAA Bus | 001
|
|
|
+//------------------------------------------------------------------------------
|
|
|
+// 20.04.10 | TM | 1.0 | Start Code for Master Test | 000
|
|
|
+//==============================================================================
|
|
|
+
|
|
|
+#include "BMS_Master.h"
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// ***** Global Data ***********************************************************
|
|
|
+extern uint64_t Global_1msCounter;
|
|
|
+extern BSE_t gBSE;
|
|
|
+extern BSD_t gBSD;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// ***** SwitchRelais **********************************************************
|
|
|
+void SwitchRelais( uint8_t Relais, uint8_t OnOff )
|
|
|
+{
|
|
|
+ if( Relais == LS_RELAIS )
|
|
|
+ {
|
|
|
+ if(OnOff)
|
|
|
+ SET_OUTPIN( PIN_REGNR_RELAIS_SLAVE );
|
|
|
+ else
|
|
|
+ CLEAR_OUTPIN( PIN_REGNR_RELAIS_SLAVE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if( Relais == HS_RELAIS)
|
|
|
+ {
|
|
|
+ if(OnOff) {
|
|
|
+ SET_OUTPIN( PIN_REGNR_RELAIS_PLUS );
|
|
|
+ SET_OUTPIN(PIN_REGNR_LED3);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ CLEAR_OUTPIN( PIN_REGNR_RELAIS_PLUS );
|
|
|
+ CLEAR_OUTPIN(PIN_REGNR_LED3);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if( Relais == PRE_CHARGE_RELAIS )
|
|
|
+ {
|
|
|
+ if(OnOff)
|
|
|
+ SET_OUTPIN( PIN_REGNR_RELAIS_PRECHA );
|
|
|
+ else
|
|
|
+ CLEAR_OUTPIN( PIN_REGNR_RELAIS_PRECHA );
|
|
|
+ }
|
|
|
+ if( Relais ==PWR_SUPPLY ) {
|
|
|
+ if(OnOff)
|
|
|
+ SET_OUTPIN( PIN_REGNR_PWR_SUPPLY );
|
|
|
+ else
|
|
|
+ CLEAR_OUTPIN( PIN_REGNR_PWR_SUPPLY );
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+uint16_t init_master_CAN0_fsm(MASTER_CAN0_STRUCT_t* s,BMS_SLAVE_CONFIGURATION_t* cellConfig,BMS_UI_CONFIGURATION_t* uiConfig) {
|
|
|
+ uint8_t slaveNr;
|
|
|
+ uint8_t i;
|
|
|
+ uint16_t stateActive;
|
|
|
+ //go thought all connected Slaves
|
|
|
+ for(slaveNr=0;slaveNr<CAN0_MAX_NR_OF_SLAVES-1;slaveNr++) {
|
|
|
+
|
|
|
+ // set slave Type
|
|
|
+ s->Slave[slaveNr].SlaveType=cellConfig[slaveNr].type;
|
|
|
+ s->Slave[slaveNr].TxMailbox_ptr=MB_Container[slaveNr];
|
|
|
+ s->Slave[slaveNr].TxTelegram_ptr=&TelegramTxContainer[slaveNr];
|
|
|
+ s->Slave[slaveNr].SlaveConnectionState=cellConfig[slaveNr].connectionSate;
|
|
|
+
|
|
|
+ // set initial Voltage and Temp value to 0xffff = not initialzied
|
|
|
+
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
+ s->Slave[slaveNr].CellVoltage[i]=0xffff;
|
|
|
+ s->Slave[slaveNr].CellTemp[i]=-51;
|
|
|
+ s->Slave[slaveNr].CellConnectionState[i]=cellConfig[slaveNr].cellConnectionState[i];
|
|
|
+ s->Slave[slaveNr].TempSensConnectionState[i]=cellConfig[slaveNr].tempSensConnectionState[i];
|
|
|
+ }
|
|
|
+ s->Slave[slaveNr].HeatSinkTemp=-51;
|
|
|
+
|
|
|
+ // set slave alive cnt to 0xff fot not initialized
|
|
|
+ for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
+ s->Slave[slaveNr].SlaveAliveCnt[i]=0xff;
|
|
|
+ }
|
|
|
+
|
|
|
+ // set slave Status to 0xff for Not initialized
|
|
|
+ s->Slave[slaveNr].SlaveMode=0xff;
|
|
|
+ // no Errors
|
|
|
+ s->Slave[slaveNr].SlaveError =0 ;
|
|
|
+
|
|
|
+ // first State Running mode
|
|
|
+ s->Slave[slaveNr].Set_Mode = BMS_SLAVE_RUN ;
|
|
|
+ // No Balancing
|
|
|
+ s->Slave[slaveNr].Balance_Cell_0_7=0;
|
|
|
+ s->Slave[slaveNr].Balance_Cell_8_15=0;
|
|
|
+ s->Slave[slaveNr].Balance_Cell_16_23=0;
|
|
|
+
|
|
|
+ // set Slave Error to 0
|
|
|
+ s->Slave[slaveNr].SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
+ s->Slave[slaveNr].SlaveCanCommuniationError.SlaveErrorCounterRegister=0;
|
|
|
+ s->Slave[slaveNr].SlaveCanCommuniationError.SlaveErrorStateRegister=0;
|
|
|
+ s->Slave[slaveNr].SlaveCanCommuniationError.WrongAliveCnt=0;
|
|
|
+
|
|
|
+ s->Slave[slaveNr].MasterAliveCnt=0;
|
|
|
+ s->Slave[slaveNr].maxCellTemp=0;
|
|
|
+ s->Slave[slaveNr].minCellTemp=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // init UI
|
|
|
+
|
|
|
+ s->Slave[15].SlaveType=uiConfig->type;
|
|
|
+ s->Slave[15].TxMailbox_ptr=MB_Container[15];
|
|
|
+ s->Slave[15].TxTelegram_ptr=&TelegramTxContainer[15];
|
|
|
+ s->Slave[15].SlaveConnectionState=uiConfig->connectionSate;
|
|
|
+
|
|
|
+ s->Slave[15].SlaveMode=0xff;
|
|
|
+ // no Errors
|
|
|
+ s->Slave[15].SlaveError =0 ;
|
|
|
+
|
|
|
+ // first State Running mode
|
|
|
+ s->Slave[15].Set_Mode = BMS_SLAVE_RUN ;
|
|
|
+
|
|
|
+
|
|
|
+ // set Slave Error to 0
|
|
|
+ s->Slave[15].SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
+ s->Slave[15].SlaveCanCommuniationError.SlaveErrorCounterRegister=0;
|
|
|
+ s->Slave[15].SlaveCanCommuniationError.SlaveErrorStateRegister=0;
|
|
|
+ s->Slave[15].SlaveCanCommuniationError.WrongAliveCnt=0;
|
|
|
+
|
|
|
+
|
|
|
+ // set State to INIT
|
|
|
+ s->FsmState=INIT;
|
|
|
+ s->slaveSelect=0;
|
|
|
+ s->transmission_pending=FALSE;
|
|
|
+ s->cycleCounter=0;
|
|
|
+ s->cycleTimestamp=0;
|
|
|
+ s->StateOfCharge=40*60*60*1000; // 40Ah in mAs
|
|
|
+ s->allValuesInitialized = FALSE;
|
|
|
+ s->startCan1Comm=FALSE;
|
|
|
+ s->balancerState=BMS_BALANCE_INIT;
|
|
|
+
|
|
|
+
|
|
|
+ s->SoC_initialized=FALSE;
|
|
|
+ s->SoC_outside=0;
|
|
|
+ s->maxHeatSinkTemp=0;
|
|
|
+ s->NrOfSlaves= get_nr_of_connected_slaves(s);
|
|
|
+
|
|
|
+ s->CAN1_fsmStruct.fsmState=CAN1_FSM_INIT;
|
|
|
+ s->CAN1_fsmStruct.highPrioMsgNr=0;
|
|
|
+ s->CAN1_fsmStruct.lowPrioMsgNr=0;
|
|
|
+ s->CAN1_fsmStruct.requestTelegramNr=0;
|
|
|
+ s->CAN1_fsmStruct.timeoutCyclesCnt=0;
|
|
|
+ s->CAN1_fsmStruct.receivedTelegrams=0;
|
|
|
+ s->CAN1_fsmStruct.fastRequestFsmRunning=FALSE;
|
|
|
+ s->CAN1_fsmStruct.slowRequestFsmRunning=FALSE;
|
|
|
+ s->CAN1_fsmStruct.fastRxState=CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
+ s->CAN1_fsmStruct.nrOfRecCurrentSamples=0;
|
|
|
+
|
|
|
+ s->FsmErrorState=ERROR_INIT;
|
|
|
+ //s->ErrorStack.ErrorNr=0;
|
|
|
+ //s->RunMode=RUN_MODE_INIT;
|
|
|
+ s->ErrorFlags=0;
|
|
|
+ // celar Error Buffers
|
|
|
+ ErrorStackClearBuffer(s);
|
|
|
+
|
|
|
+ // set Operation fsms to idle
|
|
|
+ s->RunMode.OperationMode=OP_MODE_INIT;
|
|
|
+ s->RunMode.ErrorState1fsm=ES1_FSM_INIT;
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_INIT;
|
|
|
+ s->RunMode.ErrorState3fsm=ES3_FSM_INIT;
|
|
|
+ // init master temp sensor
|
|
|
+ s->masterTemp=18;
|
|
|
+ s->MasterTempSensState=BMS_MASTER_TEMP_SENSOR_INITIATE_MEASUREMENT;
|
|
|
+ s->inverterState.state=RCT_INV_INACTIVE;
|
|
|
+ s->inverterState.inverterCanOnline=FALSE;
|
|
|
+ s->inverterState.inverterIsCharging=FALSE;
|
|
|
+ s->inverterState.startupPwr=0;
|
|
|
+
|
|
|
+ // assign impossible values to be able to check if values have been initialized
|
|
|
+ s->inverter.rxStruct.expectedInputPower=-1;
|
|
|
+ s->inverter.rxStruct.DCinputA_power=-1;
|
|
|
+ s->inverter.rxStruct.DCinputB_power=-1;
|
|
|
+
|
|
|
+ BMS_Clear_Error_init_recovery_struct(s);//clear recovery struct
|
|
|
+ s->ErrorBuffer.ES2_New_Error=0;
|
|
|
+ s->relayState.HS_closed=0;
|
|
|
+ s->relayState.LS_closed=0;
|
|
|
+ s->relayState.PRECHARGE_closed=0;
|
|
|
+ s->relayState.reseved=0;
|
|
|
+
|
|
|
+ s->reset_test_timestamp=0;
|
|
|
+
|
|
|
+ BMS_RCT_init_fsm(s);
|
|
|
+ initUIFifo(s);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void set_slave_cell_connection_state(MASTER_CAN0_STRUCT_t* s,uint8_t slaveNr,CELL_STATE_t* cell_state){
|
|
|
+ uint8_t i;
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
+ s->Slave[slaveNr].CellConnectionState[i]=cell_state[i];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void set_slave_temp_connection_state(MASTER_CAN0_STRUCT_t* s,uint8_t slaveNr,TEMP_SENSOR_STATE_t* temp_state){
|
|
|
+ uint8_t i;
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
+ s->Slave[slaveNr].TempSensConnectionState[i]=temp_state[i];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t check_slave_data(MASTER_CAN0_STRUCT_t* s,
|
|
|
+ BMS_CAN0_SLAVE_t* Slave,
|
|
|
+ BMS_CAN0_SLAVE_t* tempSlave,
|
|
|
+ int8_t overTemp_charge,
|
|
|
+ int8_t overTemp_discharge,
|
|
|
+ int8_t underTemp_charge,
|
|
|
+ int8_t underTemp_discharge,
|
|
|
+ uint16_t overVoltage,uint16_t underVoltage) {
|
|
|
+ uint8_t i;
|
|
|
+ uint8_t error_status=BMS_SLAVE_DATA_OK;
|
|
|
+ uint16_t current=s->UI_Board.Ibatt*10;
|
|
|
+
|
|
|
+ // Check Slave Mode
|
|
|
+ if(tempSlave->SlaveMode==BMS_SLAVE_RUN) {
|
|
|
+ // everything OK
|
|
|
+
|
|
|
+
|
|
|
+ //check Slave Alive Counter
|
|
|
+
|
|
|
+ //if Failed communication counter is grater than 0 the communication
|
|
|
+ //attempt last Cycle failed and the alive counters are out of sync
|
|
|
+ if(tempSlave->SlaveCanCommuniationError.FailedComCnt>0) {
|
|
|
+ //
|
|
|
+ for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
+ Slave->SlaveAliveCnt[i]=tempSlave->SlaveAliveCnt[i];
|
|
|
+ }
|
|
|
+ tempSlave->SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
+ }
|
|
|
+ //compare all Slave Alive Counter
|
|
|
+ else{
|
|
|
+ for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
+ if( (tempSlave->SlaveAliveCnt[i] == Slave->SlaveAliveCnt[i] + 1) ||
|
|
|
+ (Slave->SlaveAliveCnt[i] ==0x7 && tempSlave->SlaveAliveCnt[i] ==0) ) {
|
|
|
+ // Slave Alive Counter == OK
|
|
|
+ Slave->SlaveAliveCnt[i]=tempSlave->SlaveAliveCnt[i];
|
|
|
+ Slave->SlaveCanCommuniationError.WrongAliveCnt=0;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // slave Alive Counter didn't change or made bigger steps
|
|
|
+
|
|
|
+ Slave->SlaveCanCommuniationError.WrongAliveCnt++;
|
|
|
+ if(Slave->SlaveCanCommuniationError.WrongAliveCnt>=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
+ error_status|=BMS_SLAVE_DATA_ERROR_ALIVE_TIMEOUT;
|
|
|
+ // set CAN ERROR
|
|
|
+ ErrorStackPushMasterError(s,BMS_ERROR_STACK_SLAVE_CAN_ERROR,BMS_ERROR_CLASS_1);
|
|
|
+ return error_status;
|
|
|
+ }
|
|
|
+ for(i=0;i<CAN0_NR_OF_TELEGRAMS;i++) {
|
|
|
+ Slave->SlaveAliveCnt[i]=tempSlave->SlaveAliveCnt[i];
|
|
|
+ }
|
|
|
+ error_status |=BMS_SALVE_DATA_ERROR_ALIVE_CNT;
|
|
|
+ return error_status;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // check voltages
|
|
|
+ // discard uninitialized values 0xff and Bypassed cells
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++){
|
|
|
+ if(tempSlave->CellConnectionState[i]==CELL_BYPASSED) {
|
|
|
+ // no cell == no voltage
|
|
|
+ tempSlave->CellVoltage[i]=0;
|
|
|
+ }
|
|
|
+ else if(tempSlave->CellConnectionState[i]==CELL_CONNECTED && tempSlave->CellVoltage[i]>= 0xfff8 ) {
|
|
|
+ // not initialized cells
|
|
|
+ //TODO decide upon signed unsigned representation
|
|
|
+ // use old value
|
|
|
+ tempSlave->CellVoltage[i]= Slave->CellVoltage[i];
|
|
|
+
|
|
|
+ }
|
|
|
+ if( (tempSlave->CellVoltage[i] >= overVoltage || tempSlave->CellVoltage[i] <= underVoltage)
|
|
|
+ && tempSlave->CellConnectionState[i]==CELL_CONNECTED) {
|
|
|
+ //copy Slave to record current Errors
|
|
|
+ *Slave=*tempSlave;
|
|
|
+ error_status |=BMS_SLAVE_DATA_ERROR_VOLTAGE_LIMIT;
|
|
|
+
|
|
|
+ // Write Error Stack
|
|
|
+ if(tempSlave->CellVoltage[i] >= overVoltage ) {
|
|
|
+
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_OVER_VOLTAGE,i,BMS_ERROR_CLASS_3);
|
|
|
+ error_status |=BMS_SLAVE_DATA_ERROR_OVER_VOLTAGE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_UNDER_VOLTAGE,i,BMS_ERROR_CLASS_3);
|
|
|
+ error_status |=BMS_SLAVE_DATA_ERROR_UNDER_VOLTAGE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return error_status;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(tempSlave->HeatSinkTemp >= BMS_ERROR_THRESHOLD_T_HEATSINK_MAX || tempSlave->HeatSinkTemp <= BMS_ERROR_THRESHOLD_T_HEATSINK_MIN){
|
|
|
+ //copy Slave to record current Errors
|
|
|
+ *Slave=*tempSlave;
|
|
|
+ error_status |=BMS_SLAVE_DATA_ERROR_HEAT_SINK_LIMIT;
|
|
|
+ // Write Error Stack
|
|
|
+ if(tempSlave->HeatSinkTemp >= BMS_ERROR_THRESHOLD_T_HEATSINK_MAX ) {
|
|
|
+ // Has to be defined
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // has to be defined
|
|
|
+ }
|
|
|
+ return error_status;
|
|
|
+ }
|
|
|
+
|
|
|
+ //check cell temperatures
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++){
|
|
|
+
|
|
|
+ if (tempSlave->CellTemp[i] > -40) {
|
|
|
+ //toDo: QUICKFIX FOR INITIALIZED SENSORS
|
|
|
+
|
|
|
+ if(current < -100 ) {
|
|
|
+ // battery is charged
|
|
|
+ if( (tempSlave->CellTemp[i] >= overTemp_charge || tempSlave->CellTemp[i] <= underTemp_charge)
|
|
|
+ && tempSlave->TempSensConnectionState[i] ==TEMP_SENSOR_CONNECTED) {
|
|
|
+ //copy Slave to record current Errors
|
|
|
+ *Slave=*tempSlave;
|
|
|
+ error_status |=BMS_SLAVE_DATA_ERROR_TEMP_LIMIT;
|
|
|
+
|
|
|
+ // Write Error Stack
|
|
|
+ if(tempSlave->CellTemp[i] >= overTemp_charge ) {
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_OVER_TEMP_CHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_UNDER_TEMP_CHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
+ }
|
|
|
+ return error_status;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // battery is discharged
|
|
|
+ if( (tempSlave->CellTemp[i] >= overTemp_discharge || tempSlave->CellTemp[i] <= underTemp_discharge)
|
|
|
+ && tempSlave->TempSensConnectionState[i] ==TEMP_SENSOR_CONNECTED) {
|
|
|
+ //copy Slave to record current Errors
|
|
|
+ *Slave=*tempSlave;
|
|
|
+ error_status |=BMS_SLAVE_DATA_ERROR_TEMP_LIMIT;
|
|
|
+
|
|
|
+ // Write Error Stack
|
|
|
+ if(tempSlave->CellTemp[i] >= overTemp_discharge ) {
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_OVER_TEMP_DISCHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_UNDER_TEMP_DISCHARGE,i,BMS_ERROR_CLASS_3);
|
|
|
+ }
|
|
|
+ return error_status;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ *Slave=*tempSlave;
|
|
|
+ return error_status; //everything should be ok
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t check_UI_data(BMS_CAN0_SLAVE_t* Slave, BMS_CAN0_SLAVE_t* tempSlave,BMS_CAN0_UI_t* ui,BMS_CAN0_UI_t* tempUI) {
|
|
|
+ // todo Check Alive Cnt
|
|
|
+ // Check Over current etc
|
|
|
+ uint8_t i=0;
|
|
|
+
|
|
|
+ if(tempUI->Ibatt > 50) {
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ ui->Ubatt=tempUI->Ubatt;
|
|
|
+ ui->Ibatt=tempUI->Ibatt;
|
|
|
+ ui->Checksum=tempUI->Checksum;
|
|
|
+ return BMS_SLAVE_DATA_OK;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief calculate Voltage of Slave Board By adding cell voltages
|
|
|
+ */
|
|
|
+uint32_t calc_block_voltage(BMS_CAN0_SLAVE_t* Slave) {
|
|
|
+ uint32_t blockVoltage=0;
|
|
|
+ uint8_t i=0;
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
+ if(Slave->CellConnectionState[i]==CELL_CONNECTED) {
|
|
|
+ blockVoltage += Slave->CellVoltage[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return blockVoltage;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief calculate min and Max Voltage of Block
|
|
|
+ */
|
|
|
+uint32_t calc_min_max_voltage_slave(BMS_CAN0_SLAVE_t* Slave) {
|
|
|
+ uint16_t minV=Slave->CellVoltage[0];
|
|
|
+ uint16_t maxV=Slave->CellVoltage[0];
|
|
|
+ uint8_t i=0;
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
+ if(Slave->CellConnectionState[i]==CELL_CONNECTED) {
|
|
|
+ if(minV >= Slave->CellVoltage[i]) {
|
|
|
+ minV = Slave->CellVoltage[i];
|
|
|
+ }
|
|
|
+ if(maxV <= Slave->CellVoltage[i]) {
|
|
|
+ maxV = Slave->CellVoltage[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ Slave->minCellVoltage=minV;
|
|
|
+ Slave->maxCellVoltage=maxV;
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief calculate min and Max Voltage of System
|
|
|
+ */
|
|
|
+uint32_t calc_min_max_voltage_system(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t slave_nr=0;
|
|
|
+ uint16_t minV=BMS_SLAVE_MAX_CELL_VOLTAGE;
|
|
|
+ uint16_t maxV=BMS_SLAVE_MIN_CELL_VOLTAGE;
|
|
|
+ BMS_CAN0_SLAVE_t* Slave;
|
|
|
+ for(slave_nr=0;slave_nr<CAN0_MAX_NR_OF_SLAVES -1 ;slave_nr++) {
|
|
|
+ if(s->Slave[slave_nr].SlaveConnectionState != NOT_CONNECTED ) {
|
|
|
+ //slave is connected
|
|
|
+ Slave=&(s->Slave[slave_nr]);
|
|
|
+ if(Slave->maxCellVoltage >=maxV ) {
|
|
|
+ maxV = Slave->maxCellVoltage;
|
|
|
+ }
|
|
|
+ if(Slave->minCellVoltage <= minV) {
|
|
|
+ minV = Slave->minCellVoltage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ s->minCellVoltage=minV;
|
|
|
+ s->maxCellVoltage=maxV;
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * set balancing register for cell_nr
|
|
|
+ */
|
|
|
+uint32_t balance_cell(BMS_CAN0_SLAVE_t* Slave,uint8_t cell_nr) {
|
|
|
+ if( cell_nr <8) {
|
|
|
+ Slave->Balance_Cell_0_7|= (1<<cell_nr);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(cell_nr <16) {
|
|
|
+ Slave->Balance_Cell_8_15|=(1<<(cell_nr-8));
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Slave->Balance_Cell_16_23|=(1<<(cell_nr-16));
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return FALSE;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * go though all slaves, destinguish slaves to balance and set Balancearray
|
|
|
+ */
|
|
|
+uint32_t set_balancer(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t slave_nr=0;
|
|
|
+ uint8_t cell_nr=0;
|
|
|
+ BMS_CAN0_SLAVE_t* Slave;
|
|
|
+
|
|
|
+ for(slave_nr=0;slave_nr < CAN0_MAX_NR_OF_SLAVES -1 ;slave_nr++) { // -1 because slave 15 is UI
|
|
|
+ if(s->Slave[slave_nr].SlaveConnectionState != NOT_CONNECTED) {
|
|
|
+ // slave is connected
|
|
|
+ Slave=&(s->Slave[slave_nr]);
|
|
|
+
|
|
|
+
|
|
|
+ if(Slave->maxCellVoltage - s->minCellVoltage > SLAVE_BALANCE_MIN_DELTA_U_MV) {
|
|
|
+ // blancing neccessay
|
|
|
+ for(cell_nr=0;cell_nr <MAX_SLAVE_CELLS;cell_nr++) {
|
|
|
+ if(Slave->CellConnectionState[cell_nr]==CELL_CONNECTED) {
|
|
|
+ // cell is connected
|
|
|
+ if(Slave->CellVoltage[cell_nr] - s->minCellVoltage > SLAVE_BALANCE_MIN_DELTA_U_MV) {
|
|
|
+ // cell has to be balanced
|
|
|
+ balance_cell(Slave,cell_nr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // no need to balance
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * set all balance registers to off
|
|
|
+ */
|
|
|
+uint32_t set_balancer_off(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t slave_nr=0;
|
|
|
+ BMS_CAN0_SLAVE_t* Slave;
|
|
|
+ for(slave_nr=0;slave_nr < CAN0_MAX_NR_OF_SLAVES -1 ;slave_nr++) {
|
|
|
+ Slave=&(s->Slave[slave_nr]);
|
|
|
+ Slave->Balance_Cell_0_7=0;
|
|
|
+ Slave->Balance_Cell_16_23=0;
|
|
|
+ Slave->Balance_Cell_8_15=0;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief set min and Max Cell Temperature
|
|
|
+ * */
|
|
|
+
|
|
|
+uint32_t set_block_min_max_temp(BMS_CAN0_SLAVE_t* Slave){
|
|
|
+ uint8_t i;
|
|
|
+ int8_t maxTemp=25;
|
|
|
+ int8_t minTemp=25;
|
|
|
+ for(i=0;i<MAX_SLAVE_CELLS;i++) {
|
|
|
+ if(Slave->TempSensConnectionState[i]==TEMP_SENSOR_CONNECTED){
|
|
|
+ if(Slave->CellTemp[i] > maxTemp) {
|
|
|
+ maxTemp=Slave->CellTemp[i];
|
|
|
+ }
|
|
|
+ if(Slave->CellTemp[i] < minTemp) {
|
|
|
+ minTemp=Slave->CellTemp[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Slave->maxCellTemp=maxTemp;
|
|
|
+ Slave->minCellTemp=minTemp;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief set min and Max Cell Temperature of System
|
|
|
+ * */
|
|
|
+
|
|
|
+uint32_t set_system_min_max_temp(MASTER_CAN0_STRUCT_t* s){
|
|
|
+ uint8_t i;
|
|
|
+ int8_t maxTemp=25;
|
|
|
+ int8_t minTemp=25;
|
|
|
+ BMS_CAN0_SLAVE_t* Slave;
|
|
|
+
|
|
|
+ for(i=0;i<CAN0_MAX_NR_OF_SLAVES;i++) {
|
|
|
+ if(s->Slave[i].SlaveConnectionState!=NOT_CONNECTED && s->Slave[i].SlaveType == SLAVE) {
|
|
|
+ Slave=&(s->Slave[i]);
|
|
|
+ if(Slave->maxCellTemp > maxTemp) {
|
|
|
+ maxTemp=Slave->maxCellTemp;
|
|
|
+ }
|
|
|
+ if(Slave->minCellTemp < minTemp) {
|
|
|
+ minTemp=Slave->minCellTemp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ s->maxCellTemp=maxTemp;
|
|
|
+ s->minCellTemp=minTemp;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t set_system_min_max_heatsink_temp(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t i;
|
|
|
+ int8_t maxTemp=25;
|
|
|
+ int8_t minTemp=25;
|
|
|
+ BMS_CAN0_SLAVE_t* Slave;
|
|
|
+
|
|
|
+ for(i=0;i<CAN0_MAX_NR_OF_SLAVES;i++) {
|
|
|
+ if(s->Slave[i].SlaveConnectionState!=NOT_CONNECTED &&s->Slave[i].SlaveType == SLAVE) {
|
|
|
+ Slave=&(s->Slave[i]);
|
|
|
+ if(Slave->HeatSinkTemp > maxTemp) {
|
|
|
+ maxTemp=Slave->HeatSinkTemp;
|
|
|
+ }
|
|
|
+ if(Slave->HeatSinkTemp < minTemp) {
|
|
|
+ minTemp=Slave->HeatSinkTemp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ s->maxHeatSinkTemp=maxTemp;
|
|
|
+ s->minHeatSinkTemp=minTemp;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief calculate Voltage of System by adding block voltages
|
|
|
+ */
|
|
|
+uint32_t calc_system_voltage(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint32_t systemVoltage=0;
|
|
|
+ uint8_t i=0;
|
|
|
+ for(i=0;i<CAN0_MAX_NR_OF_SLAVES;i++) {
|
|
|
+ if(s->Slave[i].SlaveConnectionState!=NOT_CONNECTED && s->Slave[i].SlaveType == SLAVE ) {
|
|
|
+ systemVoltage += s->Slave[i].BlockVoltage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return systemVoltage;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @bief calculate SOC by simple coulomb counting
|
|
|
+ */
|
|
|
+void calc_system_SoC(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ s->StateOfCharge = s->StateOfCharge - s->UI_Board.Ibatt*10 *200; // t=200ms ibatt in 10mA steps
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief convert inverter data into correct data types
|
|
|
+ */
|
|
|
+void refresh_inverter_tx_data(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ BMS_CAN1_INVERTER_TX* inv=&(s->inverter.txStruct);
|
|
|
+ inv->Values.batteryCapacity.value=36.0;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryCapacity.value));
|
|
|
+ inv->Values.batteryCurrent.value=( (float)(s->UI_Board.Ibatt) )/100 ;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryCurrent.value));
|
|
|
+ inv->Values.batterySOC.value=s->SoC_estimator.SoC_percentage_smooth;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batterySOC.value));
|
|
|
+ inv->Values.batterySOCtarget.value=(float)-1.0;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batterySOCtarget.value));
|
|
|
+ inv->Values.batterySOH.value=(float)1.0;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batterySOH.value));
|
|
|
+ inv->Values.batteryStatus.byte6=0;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryStatus.byte6));
|
|
|
+ inv->Values.batteryTemperature.value=(float)s->maxCellTemp;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryTemperature.value));
|
|
|
+ inv->Values.batteryVoltage.value=( (float)(s->systemVoltage)) /1000.0;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryVoltage.value));
|
|
|
+ inv->Values.maxBatteryChargeCurrent.value=s->SoC_estimator.MaxBatteryChargeCurrent;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.maxBatteryChargeCurrent.value));
|
|
|
+ inv->Values.maxBatteryChargeVoltage.value=s->startupConfig.maxBatteryVoltage;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.maxBatteryChargeVoltage.value));
|
|
|
+ inv->Values.maxBatteryDischargeCurrent.value=s->SoC_estimator.MaxBatteryDischargeCurrent;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.maxBatteryDischargeCurrent.value));
|
|
|
+ inv->Values.minBatteryDischargeVoltage.value=s->startupConfig.minBatteryVoltage;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.minBatteryDischargeVoltage.value));
|
|
|
+
|
|
|
+ if(s->relayState.HS_closed == TRUE && s->relayState.LS_closed == TRUE && s->relayState.PRECHARGE_closed == TRUE) {
|
|
|
+ inv->Values.batteryModeExtra.value = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ inv->Values.batteryModeExtra.value = 1;
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryModeExtra.value));
|
|
|
+ }
|
|
|
+ //inv->Values.batteryMode.value=(uint32_t)(s->RunMode);
|
|
|
+ //shuffle_lsb_msb_can1((uint8_t*)&(inv->Values.batteryMode.value));
|
|
|
+}
|
|
|
+
|
|
|
+void shuffle_lsb_msb_can1(uint8_t* value) {
|
|
|
+ uint8_t tempValue[4];
|
|
|
+ tempValue[3]=value[0];
|
|
|
+ tempValue[2]=value[1];
|
|
|
+ tempValue[1]=value[2];
|
|
|
+ tempValue[0]=value[3];
|
|
|
+
|
|
|
+ value[0]=tempValue[0];
|
|
|
+ value[1]=tempValue[1];
|
|
|
+ value[2]=tempValue[2];
|
|
|
+ value[3]=tempValue[3];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+uint8_t check_if_all_values_are_initialized(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t slaveNr;
|
|
|
+ uint8_t cellNr;
|
|
|
+ for(slaveNr=0;slaveNr<CAN0_MAX_NR_OF_SLAVES-1;slaveNr++){
|
|
|
+ if(s->Slave[slaveNr].SlaveConnectionState != NOT_CONNECTED) {
|
|
|
+ for(cellNr=0;cellNr<MAX_SLAVE_CELLS;cellNr++) {
|
|
|
+ if(s->Slave[slaveNr].CellConnectionState[cellNr]==CELL_CONNECTED) {
|
|
|
+ if(s->Slave[slaveNr].CellVoltage[cellNr] == 0xffff) {
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(s->Slave[slaveNr].TempSensConnectionState[cellNr] == TEMP_SENSOR_CONNECTED) {
|
|
|
+ if(s->Slave[slaveNr].CellTemp[cellNr] == -1) {
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief Handles communication with Slave-Boards over CAN0 interface
|
|
|
+ * @param s pointer to state variable
|
|
|
+ * @param time current timestamp in ms
|
|
|
+ *
|
|
|
+ * Master_CAN0_fsm is a finite state machine which handles the commuinication between Master and Slave boards.
|
|
|
+ * It requests Data from the Slaves by sending an Request telegram, and saves the received data in the
|
|
|
+ * provided MASTER_CAN0_STRUCT. It records CAN BUS Errors and hands these information to the Master_CAN0_ERROR_fsm.
|
|
|
+ */
|
|
|
+uint16_t Master_CAN0_fsm(MASTER_CAN0_STRUCT_t* s,uint32_t time) {
|
|
|
+ CAN_CONFIG *config;
|
|
|
+ int8_t can_state=CAN_OK;
|
|
|
+ uint8_t data_state=0;
|
|
|
+ uint8_t ui_state=FALSE;
|
|
|
+ switch(s->FsmState) {
|
|
|
+ case INIT:
|
|
|
+ s->slaveSelect=0;
|
|
|
+ s->cycleTimestamp=time;
|
|
|
+ s->FsmState=CHECK_IF_SLAVE_ACTIVE;
|
|
|
+ s->cycleCounter++;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case CHECK_IF_SLAVE_ACTIVE:
|
|
|
+ // transmitt data to Inverter
|
|
|
+ if(s->SoC_initialized==TRUE && s->allValuesInitialized ==TRUE) {
|
|
|
+ //Master_CAN1_Inverter_fsm( s,&(s->CAN1_fsmStruct)) ;
|
|
|
+ Master_CAN1_select_comm_mode ( s,&(s->CAN1_fsmStruct)) ;
|
|
|
+ }
|
|
|
+ // Update Master temp sensor
|
|
|
+ readMasterTempSensorFsm(s);
|
|
|
+ s->timestamp=Global_1msCounter;
|
|
|
+ if(s->Slave[s->slaveSelect].SlaveConnectionState == NOT_CONNECTED) {
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_SLAVE_TIMESLOT;
|
|
|
+ s->slaveSelect++; // ignore and go to next slave
|
|
|
+ if(s->slaveSelect>=CAN0_MAX_NR_OF_SLAVES) {
|
|
|
+ s->slaveSelect=0;
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ s->Slave[s->slaveSelect].SlaveTelegramsRecFlag=0; // no Telegrams recoredet yet
|
|
|
+ s->FsmState=SEND_REQUEST_TELEGRAM;
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SEND_REQUEST_TELEGRAM:
|
|
|
+ s->transmission_pending=TRUE;
|
|
|
+ CAN0_tx_send_request_telegram(&(s->Slave[s->slaveSelect]),s->Slave[s->slaveSelect].MasterAliveCnt);
|
|
|
+ s->Slave[s->slaveSelect].MasterAliveCnt++;
|
|
|
+ if(s->Slave[s->slaveSelect].MasterAliveCnt >=CAN0_NR_OF_TELEGRAMS) {
|
|
|
+ s->Slave[s->slaveSelect].MasterAliveCnt=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(s->Slave[s->slaveSelect].SlaveType==SLAVE) {
|
|
|
+ s->tempSlave=s->Slave[s->slaveSelect];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->tempSlave=s->Slave[s->slaveSelect];
|
|
|
+ s->temp_UI_Board=s->UI_Board;
|
|
|
+ }
|
|
|
+ s->FsmState=WAIT_FOR_SLAVE_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case WAIT_FOR_SLAVE_RESPONSE:
|
|
|
+ config= s->tempSlave.TxMailbox_ptr->p_CAN_Config;
|
|
|
+ //can_state=CAN_Check_error_Register(config);
|
|
|
+ if(s->tempSlave.SlaveType==SLAVE) {
|
|
|
+ if(s->tempSlave.SlaveConnectionState == CONNECTED) {
|
|
|
+ // real slave with real Sensors
|
|
|
+ CAN0_check_if_slave_rec(&(s->tempSlave));
|
|
|
+ }
|
|
|
+ else if(s->tempSlave.SlaveConnectionState == DEBUG_DATA_SIMULATED) {
|
|
|
+ // real slave with simulated Sensors
|
|
|
+ CAN0_DEBUG_data_check_if_slave_rec((&s->tempSlave),&(gDUMMY_data_struct.Slave[s->slaveSelect]) );
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // simulated Slave
|
|
|
+ CAN0_DEBUG_slave_check_if_slave_rec((&s->tempSlave),&(gDUMMY_data_struct.Slave[s->slaveSelect]) );
|
|
|
+ }
|
|
|
+
|
|
|
+ //all Telegrams received
|
|
|
+ if(s->tempSlave.SlaveTelegramsRecFlag==CAN0_ALL_TELEGRAMS_REC ) {
|
|
|
+ //s->slaveSelect++;
|
|
|
+ if(can_state == CAN_OK) {
|
|
|
+ // all telegrams received => reset error counter
|
|
|
+ if(s->slaveSelect == 3) {
|
|
|
+ s->FsmState= CHECK_CAN_DATA;
|
|
|
+ }
|
|
|
+ s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt=0;
|
|
|
+ s->FsmState= CHECK_CAN_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->FsmState =HANDLE_CAN_ERROR;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //timeout
|
|
|
+ else if(s->timestamp + CAN0_TIMEOUT_MS <=Global_1msCounter) {
|
|
|
+ s->FsmState =HANDLE_CAN_ERROR;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ // wait
|
|
|
+ else {
|
|
|
+ s->FsmState =WAIT_FOR_SLAVE_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+
|
|
|
+ if(s->tempSlave.SlaveConnectionState == CONNECTED) {
|
|
|
+ // real UI with real Sensors
|
|
|
+ ui_state=CAN0_check_if_UI_Board_rec( &(s->tempSlave) ,&(s->temp_UI_Board));
|
|
|
+ }
|
|
|
+ else if(s->tempSlave.SlaveConnectionState == DEBUG_DATA_SIMULATED) {
|
|
|
+ // real UI with simulated Sensors
|
|
|
+ ui_state=CAN0_DEBUG_data_check_if_UI_Board_rec(&(s->tempSlave),
|
|
|
+ &(gDUMMY_data_struct.Slave[s->slaveSelect]),
|
|
|
+ &(s->temp_UI_Board),
|
|
|
+ &(gDUMMY_data_struct.UI_Board));
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // simulated UI
|
|
|
+ ui_state=CAN0_DEBUG_slave_check_if_UI_Board_rec(&(s->tempSlave),
|
|
|
+ &(gDUMMY_data_struct.Slave[s->slaveSelect]),
|
|
|
+ &(s->temp_UI_Board),
|
|
|
+ &(gDUMMY_data_struct.UI_Board));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ui_state==TRUE) {
|
|
|
+ //s->slaveSelect++;
|
|
|
+ if(can_state==CAN_OK) {
|
|
|
+ s->FsmState= CHECK_CAN_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->FsmState =HANDLE_CAN_ERROR;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ //return TRUE;
|
|
|
+ }
|
|
|
+ //timeout
|
|
|
+ else if(s->timestamp + CAN0_TIMEOUT_MS <=time) {
|
|
|
+ s->FsmState =HANDLE_CAN_ERROR;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ // wait
|
|
|
+ else {
|
|
|
+ s->FsmState =WAIT_FOR_SLAVE_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case CHECK_CAN_DATA:
|
|
|
+ // check if rx data is correct
|
|
|
+
|
|
|
+ if(s->tempSlave.SlaveType ==SLAVE ) {
|
|
|
+
|
|
|
+ s->ErrorFlags=check_slave_data(s,&(s->Slave[s->slaveSelect]),
|
|
|
+ &(s->tempSlave),
|
|
|
+ BMS_SLAVE_MAX_TEMP,
|
|
|
+ BMS_ERROR_THRESHOLD_T_DISCHARGE_MAX,
|
|
|
+ BMS_SLAVE_MIN_TEMP,
|
|
|
+ BMS_ERROR_THRESHOLD_T_DISCHARGE_MIN,
|
|
|
+ BMS_SLAVE_MAX_CELL_VOLTAGE,
|
|
|
+ BMS_SLAVE_MIN_CELL_VOLTAGE);
|
|
|
+
|
|
|
+ if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_ALIVE_TIMEOUT) {
|
|
|
+ s->FsmState =SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_OVER_VOLTAGE) {
|
|
|
+ s->FsmState =SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_UNDER_VOLTAGE) {
|
|
|
+ s->FsmState =SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ if((s->ErrorFlags & BMS_SLAVE_DATA_ERROR_TEMP_LIMIT) && (s->allValuesInitialized == TRUE)) {
|
|
|
+ s->FsmState =SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_HEAT_SINK_LIMIT) {
|
|
|
+ s->FsmState =SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ if(s->ErrorFlags & BMS_SLAVE_DATA_ERROR_BOARD_ELECTRONIC) {
|
|
|
+ s->FsmState =SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // everything ok
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ data_state=check_UI_data(&(s->Slave[s->slaveSelect]),
|
|
|
+ &(s->tempSlave),
|
|
|
+ &(s->UI_Board),
|
|
|
+ &(s->temp_UI_Board));
|
|
|
+
|
|
|
+ if(s->allValuesInitialized==FALSE) {
|
|
|
+ s->allValuesInitialized=check_if_all_values_are_initialized(s);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // Check for "Soft" Errors and set Error states
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ s->transmission_pending=FALSE;
|
|
|
+
|
|
|
+ s->FsmState= DO_CALCULATIONS;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case DO_CALCULATIONS:
|
|
|
+ // calculate SoC SoH ?
|
|
|
+ if(s->Slave[s->slaveSelect].SlaveType==SLAVE) {
|
|
|
+ // calculate Slave Voltage
|
|
|
+ s->Slave[s->slaveSelect].BlockVoltage=calc_block_voltage(&(s->Slave[s->slaveSelect]));
|
|
|
+ // set min max Temp
|
|
|
+ set_block_min_max_temp(&(s->Slave[s->slaveSelect]));
|
|
|
+ // set min max Voltage
|
|
|
+ calc_min_max_voltage_slave(&(s->Slave[s->slaveSelect]));
|
|
|
+
|
|
|
+ s->slaveSelect++;
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_SLAVE_TIMESLOT;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // UI-Platine
|
|
|
+ // calc SoC
|
|
|
+ s->systemVoltage=calc_system_voltage(s);
|
|
|
+ calc_min_max_voltage_system(s);
|
|
|
+ set_system_min_max_temp(s);
|
|
|
+ // update UI FIFO
|
|
|
+ popUIFiFo(s);
|
|
|
+
|
|
|
+ BMS_Set_Error_Check_voltage_inconsitency(s);
|
|
|
+
|
|
|
+ // check if System voltage is too high
|
|
|
+ if(s->allValuesInitialized ==TRUE) {
|
|
|
+ // Check Umin 1 Umin2 Umin3
|
|
|
+ BMS_Set_Error_check_Voltage_level_min(s);
|
|
|
+ // Check Umax 1 and Umax 2
|
|
|
+ BMS_Set_Error_check_Voltage_level_max(s) ;
|
|
|
+ BMS_Set_Error_check_System_voltage_level_max(s);
|
|
|
+
|
|
|
+ // check if CHECK Imax
|
|
|
+ BMS_Set_Error_Check_derating(s);
|
|
|
+
|
|
|
+
|
|
|
+ // make balancing decisions
|
|
|
+ Master_Balancer_fsm(s);
|
|
|
+ }
|
|
|
+ // if in Winter MOde get SoC from FRAM
|
|
|
+// if(s->SoC_initialized==TRUE && s->allValuesInitialized ==FALSE && s->RunMode== RUN_MODE_WINTER) {
|
|
|
+// bms_SoC_init_estimator_FRAM(&(s->SoC_estimator), s->maxCellVoltage);
|
|
|
+// }
|
|
|
+ // calculate SoC of higest and Lowest Cell
|
|
|
+ if(s->SoC_initialized==FALSE && s->allValuesInitialized ==TRUE) {
|
|
|
+ set_system_min_max_heatsink_temp(s);
|
|
|
+ //set initial SoC
|
|
|
+ if(s->startupConfig.state.Bit.SOC_Initialized) {
|
|
|
+ // get initial SOC From FRAM
|
|
|
+ bms_SoC_init_estimator_FRAM(&(s->SoC_estimator), s->maxCellVoltage);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ bms_SoC_init_estimator(&(s->SoC_estimator), s->maxCellVoltage) ;
|
|
|
+ }
|
|
|
+ initSoCFifo(s);
|
|
|
+ s->SoC_initialized=TRUE;
|
|
|
+ s->FsmState=RUNNING_MODE_FSM;
|
|
|
+ }
|
|
|
+ else if(s->SoC_initialized==TRUE && s->allValuesInitialized ==TRUE) {
|
|
|
+ set_system_min_max_heatsink_temp(s);
|
|
|
+
|
|
|
+ // calc SoC
|
|
|
+
|
|
|
+ while(s->SoC_estimator.state != BMS_SOC_READY) {
|
|
|
+ bms_SoC_running_fsm( &(s->SoC_estimator),s->UI_Board.Ibatt*10 ,s->maxCellVoltage,s->minCellVoltage,s->minCellTemp,s->maxCellTemp);
|
|
|
+ }
|
|
|
+ s->SoC_estimator.state = BMS_SOC_IDLE ;
|
|
|
+
|
|
|
+ // if soc is at 100% set SoC initialized Flag
|
|
|
+ if(s->SoC_estimator.SoC_percentage_smooth > 0.9999) {
|
|
|
+ s->startupConfig.state.Bit.SOC_Initialized=1;
|
|
|
+ write_fram_set_startup_state(s);
|
|
|
+ }
|
|
|
+ // write SoC to FRAM
|
|
|
+ else if(s->startupConfig.state.Bit.SOC_Initialized==1) {
|
|
|
+ write_fram_set_SoC(s->SoC_estimator.SoC_percentage_smooth);
|
|
|
+ }
|
|
|
+ s->slaveSelect=0;
|
|
|
+
|
|
|
+ // transmitt data to Inverter
|
|
|
+ if(s->SoC_initialized==TRUE && s->allValuesInitialized ==TRUE) {
|
|
|
+ refresh_inverter_tx_data(s);
|
|
|
+ //Master_CAN1_Inverter_fsm( s,&(s->CAN1_fsmStruct)) ;
|
|
|
+ }
|
|
|
+ // set running Mode
|
|
|
+
|
|
|
+ // update inverter state
|
|
|
+ BMS_RCT_Inverter_fsm (s);
|
|
|
+ s->FsmState=RUNNING_MODE_FSM;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // nothing happend yet
|
|
|
+ s->FsmState=RUNNING_MODE_FSM;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case RUNNING_MODE_FSM:
|
|
|
+ // set running Mode
|
|
|
+ RunningModeFSM(s) ;
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case HANDLE_CAN_ERROR:
|
|
|
+ // delete interrupt flags
|
|
|
+ CAN0_clear_all_interrupt_flags();
|
|
|
+ // handle CAN Errors
|
|
|
+ config= s->tempSlave.TxMailbox_ptr->p_CAN_Config;
|
|
|
+ //can_state=CAN_Check_error_Register(config);
|
|
|
+ // all telegrams received, can telegrams disturbed
|
|
|
+ if(s->tempSlave.SlaveTelegramsRecFlag==CAN0_ALL_TELEGRAMS_REC ) {
|
|
|
+ // Telegram values could be disturbed => reject all received Data
|
|
|
+ // Don't use newly received data => ignore tempSlave
|
|
|
+ s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt++;
|
|
|
+ if(s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt >=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
+ s->FsmState=SHUT_DOWN_BMS;
|
|
|
+
|
|
|
+ //write Error
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_CAN_ERROR,0,BMS_ERROR_CLASS_2);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ s->FsmState=DO_CALCULATIONS;
|
|
|
+ return TRUE;
|
|
|
+
|
|
|
+ }
|
|
|
+ // telegrams missing, CAN Channel Ok
|
|
|
+ else{
|
|
|
+ if(can_state == CAN_OK) {
|
|
|
+ // CAN BUS OK, Slave is not reacting
|
|
|
+ // Don't use newly received data
|
|
|
+ s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt++;
|
|
|
+ if(s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt >=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
+ //write Error
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_CAN_ERROR,0,BMS_ERROR_CLASS_2);
|
|
|
+ s->FsmState=SHUT_DOWN_BMS;
|
|
|
+ s->reset_test_timestamp=Global_1msCounter;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ s->FsmState=DO_CALCULATIONS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // CAN BUS ERROR, telegrams not coming thought
|
|
|
+ // Don't use newly received data
|
|
|
+ s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt++;
|
|
|
+ if(s->Slave[s->slaveSelect].SlaveCanCommuniationError.FailedComCnt >=CAN0_MAX_NR_OF_FAILED_COM) {
|
|
|
+ //write Error
|
|
|
+ ErrorStackPushSlaveError(s,s->slaveSelect,BMS_ERROR_STACK_SLAVE_CAN_ERROR,0,BMS_ERROR_CLASS_2);
|
|
|
+ s->reset_test_timestamp=Global_1msCounter;
|
|
|
+ s->FsmState=SHUT_DOWN_BMS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ s->FsmState=DO_CALCULATIONS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case WAIT_FOR_NEXT_SLAVE_TIMESLOT:
|
|
|
+ // next timesolt ready
|
|
|
+
|
|
|
+ if(s->timestamp +CAN0_RASTER_MS <= time) {
|
|
|
+ s->FsmState= CHECK_IF_SLAVE_ACTIVE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ //wait
|
|
|
+ else{
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_SLAVE_TIMESLOT;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SEND_RECEIVE_INVERTER_DATA:
|
|
|
+ // send data to inverter and Wait for response
|
|
|
+ //CAN1_tx_Data_to_Inverter(time,&(s->inverter.txStruct));
|
|
|
+ //request DC INPUT VOLTAGE
|
|
|
+ //CAN1_request_float_value(Global_1msCounter,&(s->inverter.rxStruct.DCinputA_power),CAN1_RX_DC_INPUT_A_VOLTAGE);
|
|
|
+ s->startCan1Comm=TRUE;
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case WAIT_FOR_NEXT_COMMUNICATION_CYCLE:
|
|
|
+ if(s->cycleTimestamp +CAN0_COMM_CYCLE_MS <= time) {
|
|
|
+ s->FsmState= INIT;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->FsmState=WAIT_FOR_NEXT_COMMUNICATION_CYCLE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case SHUT_DOWN_BMS:
|
|
|
+ // Something went horribly wrong, turn off system
|
|
|
+ SwitchRelais( LS_RELAIS, 0);
|
|
|
+ SwitchRelais( PRE_CHARGE_RELAIS, 0);
|
|
|
+ SwitchRelais( HS_RELAIS, 0);
|
|
|
+ s->relayState.HS_closed=FALSE;
|
|
|
+ s->relayState.LS_closed=FALSE;
|
|
|
+ s->relayState.PRECHARGE_closed=FALSE;
|
|
|
+ CLEAR_OUTPIN(PIN_REGNR_LED4); // set LED4 to 0 to indicate that an Error has occured
|
|
|
+
|
|
|
+ //write_fram_word(BMS_STARTUP_ERROR_MODE_3,3,BMS_STARTUP_MODE_ADDR);
|
|
|
+ // go to Error Mode
|
|
|
+
|
|
|
+ // continue communication cycles
|
|
|
+ s->FsmState=DO_CALCULATIONS;
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return FALSE;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return FALSE;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+uint16_t init_master_operation_fsm(BMS_MASTER_OPERATION_t* opFsm) {
|
|
|
+ opFsm->FsmState=MASTER_OPERATION_INIT;
|
|
|
+ opFsm->timestamp=0;
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+uint16_t Master_CAN1_fsm_init(MASTER_CAN1_STRUCT_t* can1Fsm) {
|
|
|
+ can1Fsm->delay =3;
|
|
|
+ can1Fsm->fsmState=CAN1_INIT;
|
|
|
+ can1Fsm->txMsgNr=0;
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ *
|
|
|
+ */
|
|
|
+uint32_t Master_CAN1_select_comm_mode (MASTER_CAN0_STRUCT_t* s,MASTER_CAN1_INVERTER_STRUCT_t* fsmStruct) {
|
|
|
+ if(fsmStruct->slowRequestFsmRunning == TRUE) {
|
|
|
+ // slow requests are running => wait till requests are complete
|
|
|
+ Master_CAN1_Inverter_fsm(s,fsmStruct);
|
|
|
+ // set fast request running flag for faast request asap
|
|
|
+ fsmStruct->fastRequestFsmRunning=TRUE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(s->Slave[s->slaveSelect].SlaveType== UI || fsmStruct->fastRequestFsmRunning==TRUE) {
|
|
|
+ // commuincation with UI is about to begin or fast request flag is set
|
|
|
+ // this means 250ms tick is reached => initiate fast request if possible
|
|
|
+ Master_CAN1_Fast_request_fsm (s,fsmStruct);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // nothing special run normal fsm
|
|
|
+ Master_CAN1_Inverter_fsm(s,fsmStruct);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+}
|
|
|
+/*
|
|
|
+ * handles request of battery current and battery voltage from inverter
|
|
|
+ * which have to be sampled regulatly
|
|
|
+ */
|
|
|
+uint32_t Master_CAN1_Fast_request_fsm (MASTER_CAN0_STRUCT_t* s,MASTER_CAN1_INVERTER_STRUCT_t* fsmStruct) {
|
|
|
+ switch(fsmStruct->fastRxState) {
|
|
|
+ case CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT:
|
|
|
+ // request battery current from inverter
|
|
|
+ fsmStruct->nrOfRecCurrentSamples++;
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_BATTERY_CURRENT);
|
|
|
+ fsmStruct->fastRequestFsmRunning=TRUE;
|
|
|
+ fsmStruct->fastRxState=CAN1_FAST_RX_FSM_REQUEST_BATTERY_VOLTAGE;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case CAN1_FAST_RX_FSM_REQUEST_BATTERY_VOLTAGE:
|
|
|
+ // readout battery current and request battery voltage
|
|
|
+ if(CAN1_wait_for_response(&(s->inverter.rxStruct.batteryCurrent)) == TRUE ) {
|
|
|
+ // battery current received
|
|
|
+ pushInverterCurrentFIFO(s);
|
|
|
+ // compare UI Current and Inverter Current
|
|
|
+ if(fsmStruct->nrOfRecCurrentSamples > UI_CURRENT_FIFO_SIZE) {
|
|
|
+ // BMS_Set_Error_Check_current_consitency(s); //compare current of UI with Inverter
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // no response in 10ms => timeout
|
|
|
+ // go to normal communication fsm
|
|
|
+ fsmStruct->fastRxState = CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
+ fsmStruct->fastRequestFsmRunning=FALSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ // request battery voltage
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_BATTERY_VOLTAGE);
|
|
|
+ fsmStruct->fastRxState =CAN1_FAST_RX_FSM_REQUEST_COMPLETE;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case CAN1_FAST_RX_FSM_REQUEST_COMPLETE:
|
|
|
+ // readout voltage
|
|
|
+ if(CAN1_wait_for_response(&(s->inverter.rxStruct.batteryVoltage)) == TRUE ) {
|
|
|
+ // battery voltage received
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // no response in 10ms => timeout
|
|
|
+ // go to normal communication fsm
|
|
|
+ fsmStruct->fastRxState = CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
+ fsmStruct->fastRequestFsmRunning=FALSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ // fast requests done
|
|
|
+ fsmStruct->fastRxState = CAN1_FAST_RX_FSM_REQUEST_BATTERY_CURRENT;
|
|
|
+ fsmStruct->fastRequestFsmRunning=FALSE;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+uint32_t Master_CAN1_Inverter_fsm(MASTER_CAN0_STRUCT_t* s,MASTER_CAN1_INVERTER_STRUCT_t* fsmStruct) {
|
|
|
+ BMS_CAN1_INVERTER_TX* data = &(s->inverter.txStruct);
|
|
|
+ BMS_CAN1_INVERTER_CELL_DATA_t txPkg;
|
|
|
+ float* rx_ptr=&(s->inverter.rxStruct.expectedInputPower); // points to rx_address to sotre inverter value
|
|
|
+ uint8_t eightBitBuff[8]= {0,0,0,0,0,0,0,0} ;
|
|
|
+ switch(fsmStruct->fsmState) {
|
|
|
+ case CAN1_FSM_INIT:
|
|
|
+ // reset Message counters
|
|
|
+ fsmStruct->highPrioMsgNr=0;
|
|
|
+ fsmStruct->lowPrioMsgNr=0;
|
|
|
+ fsmStruct->requestTelegramNr=0;
|
|
|
+ fsmStruct->timeoutCyclesCnt=0;
|
|
|
+
|
|
|
+
|
|
|
+ // check if all Values are initialized
|
|
|
+ // if yes start transmitting
|
|
|
+ if(s->allValuesInitialized == TRUE) {
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ // not all values are initialized
|
|
|
+ //=> stay in INIT state
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case CAN1_FSM_SEND_HIGH_PRIO_DATA:
|
|
|
+ // rx process complete
|
|
|
+ fsmStruct->slowRequestFsmRunning=FALSE;
|
|
|
+
|
|
|
+ // transmit high priority data, which is
|
|
|
+ // maximum charge Current
|
|
|
+ // maximum Charge Voltage
|
|
|
+ // maximum Discharge Current
|
|
|
+ // minimum Discharge Voltage
|
|
|
+ // battery Current
|
|
|
+ // battery Voltage
|
|
|
+ // Battery SoC
|
|
|
+ // Battery Capacity
|
|
|
+ // Battery Temperature
|
|
|
+ // Battery SoH
|
|
|
+ // Battery Status
|
|
|
+ // Battery SoC Target
|
|
|
+
|
|
|
+ if(fsmStruct->highPrioMsgNr == 0) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.maxBatteryChargeCurrent),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 1) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.maxBatteryChargeVoltage),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 2) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.maxBatteryDischargeCurrent),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 3) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.minBatteryDischargeVoltage),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 4) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryCurrent),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 5) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryVoltage),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 7) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batterySOC),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 8) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryCapacity),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 9) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryTemperature),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 10) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batterySOH),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 11) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batterySOCtarget),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 12) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryMode),Global_1msCounter);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->highPrioMsgNr == 13) {
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryModeExtra),Global_1msCounter);
|
|
|
+ }
|
|
|
+
|
|
|
+ // increment high prio msg cnt
|
|
|
+ fsmStruct->highPrioMsgNr++ ;
|
|
|
+
|
|
|
+ // if all high prio msg are transmitted start transmitting low prio msg
|
|
|
+ if(fsmStruct->highPrioMsgNr > 13) {
|
|
|
+ fsmStruct->highPrioMsgNr=0;
|
|
|
+ // reset error cnt to start with error 1 again
|
|
|
+ fsmStruct->transmitErrorNrEs1=0;
|
|
|
+ fsmStruct->transmitErrorNrEs2=0;
|
|
|
+ fsmStruct->transmitErrorNrEs3=0;
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_ES_1;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // continue Transmitting high Prio Msg
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case CAN1_FSM_SEND_ES_1:
|
|
|
+ // send Error Calls 1 Errors
|
|
|
+
|
|
|
+ if( !ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1) && !ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2) && ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_3) ) {
|
|
|
+ // no Error has occured Transmitt everything ok
|
|
|
+ CLEAR_OUTPIN(PIN_REGNR_LED4);
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(data->Values.batteryStatus),Global_1msCounter);
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_LOW_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_3) && fsmStruct->transmitErrorNrEs3 < BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
+ // transmitt ES 3 Errors
|
|
|
+ // check if Error is active
|
|
|
+ SET_OUTPIN(PIN_REGNR_LED4);
|
|
|
+ while(s->ErrorBuffer.ES3_Error[fsmStruct->transmitErrorNrEs3].Master.active == 0){
|
|
|
+ // Error no ,longer Active go to next entry
|
|
|
+ fsmStruct->transmitErrorNrEs3++;
|
|
|
+ if(fsmStruct->transmitErrorNrEs3 >=BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ErrorStackGenerateStatusPkg(eightBitBuff,(uint8_t*)&(s->ErrorBuffer.ES3_Error[fsmStruct->transmitErrorNrEs3]));
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, eightBitBuff,Global_1msCounter);
|
|
|
+ fsmStruct->transmitErrorNrEs3++;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2) && fsmStruct->transmitErrorNrEs2 < BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
+ // transmitt ES 2 Errors
|
|
|
+ // check if Error is active
|
|
|
+ SET_OUTPIN(PIN_REGNR_LED4);
|
|
|
+ while(s->ErrorBuffer.ES2_Error[fsmStruct->transmitErrorNrEs2].Master.active == 0){
|
|
|
+ // Error no ,longer Active go to next entry
|
|
|
+ fsmStruct->transmitErrorNrEs2++;
|
|
|
+ if(fsmStruct->transmitErrorNrEs2 >=BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ErrorStackGenerateStatusPkg(eightBitBuff,(uint8_t*)&(s->ErrorBuffer.ES2_Error[fsmStruct->transmitErrorNrEs2]));
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, eightBitBuff,Global_1msCounter);
|
|
|
+ fsmStruct->transmitErrorNrEs2++;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1) && fsmStruct->transmitErrorNrEs1 < BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
+ // transmitt ES 1 Errors
|
|
|
+ // check if Error is active
|
|
|
+ SET_OUTPIN(PIN_REGNR_LED4);
|
|
|
+ while(s->ErrorBuffer.ES1_Error[fsmStruct->transmitErrorNrEs1].Master.active == 0){
|
|
|
+ // Error no ,longer Active go to next entry
|
|
|
+ fsmStruct->transmitErrorNrEs1++;
|
|
|
+ if(fsmStruct->transmitErrorNrEs1 >=BMS_ERROR_ERROR_STACK_SIZE) {
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ErrorStackGenerateStatusPkg(eightBitBuff,(uint8_t*)&(s->ErrorBuffer.ES1_Error[fsmStruct->transmitErrorNrEs1]));
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, eightBitBuff,Global_1msCounter);
|
|
|
+ fsmStruct->transmitErrorNrEs1++;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_LOW_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case CAN1_FSM_SEND_LOW_PRIO_DATA:
|
|
|
+ // send low Priority data which is
|
|
|
+ // Cell temperature and Cell Voltages
|
|
|
+ generateCellInfoPkg(s, fsmStruct->lowPrioMsgNr,&txPkg);
|
|
|
+ CAN_Write_dataset(&CAN_Tx0_RCT_INVERTER, (uint8_t*)&(txPkg),Global_1msCounter);
|
|
|
+ fsmStruct->lowPrioMsgNr++ ;
|
|
|
+ if(fsmStruct->lowPrioMsgNr >= (s->NrOfSlaves)*MAX_SLAVE_CELLS) {
|
|
|
+ // start with cell nr 0 again
|
|
|
+ fsmStruct->lowPrioMsgNr=0;
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_REQUEST_DATA;
|
|
|
+ fsmStruct->requestTelegramNr=0;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_LOW_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case CAN1_FSM_SEND_REQUEST_DATA:
|
|
|
+ // request data from RCT Inverter
|
|
|
+ // Set flag so request process cant be interrupted
|
|
|
+ fsmStruct->slowRequestFsmRunning=TRUE;
|
|
|
+ if(fsmStruct->requestTelegramNr==0) {
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_TOTAL_DC_PWR);
|
|
|
+ fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==1) {
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_DC_INPUT_A_POWER);
|
|
|
+ fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==2) {
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_DC_INPUT_B_POWER);
|
|
|
+ fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==3) {
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_DC_INPUT_A_POWER);
|
|
|
+ fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==4) {
|
|
|
+ CAN1_send_request_telegram(CAN1_RX_DC_INPUT_B_POWER);
|
|
|
+ fsmStruct->fsmState= CAN1_FSM_WAIT_FOR_RESPONSE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // you should not be here
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case CAN1_FSM_WAIT_FOR_RESPONSE:
|
|
|
+ // to be updateted for more values
|
|
|
+ if(fsmStruct->requestTelegramNr==0) {
|
|
|
+ rx_ptr=&(s->inverter.rxStruct.expectedInputPower);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==1) {
|
|
|
+ rx_ptr=&(s->inverter.rxStruct.DCinputA_power);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==2) {
|
|
|
+ rx_ptr=&(s->inverter.rxStruct.DCinputB_power);
|
|
|
+ }
|
|
|
+ else if(fsmStruct->requestTelegramNr==3) {
|
|
|
+ // not valid anymore
|
|
|
+ rx_ptr=&(s->inverter.rxStruct.DCinputA_power);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // not valid anymore
|
|
|
+ rx_ptr=&(s->inverter.rxStruct.DCinputB_power);
|
|
|
+ }
|
|
|
+ if(CAN1_wait_for_response(rx_ptr) == TRUE ) {
|
|
|
+ fsmStruct->requestTelegramNr++;
|
|
|
+ fsmStruct->receivedTelegrams++;
|
|
|
+ if(fsmStruct->requestTelegramNr >=CAN1_NR_OF_REQUEST_TELEGRAMS) {
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
+ fsmStruct->timeoutCyclesCnt=0;
|
|
|
+ s->inverterState.inverterCanOnline= TRUE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_REQUEST_DATA;
|
|
|
+ fsmStruct->timeoutCyclesCnt=0;
|
|
|
+ s->inverterState.inverterCanOnline= TRUE;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ fsmStruct->timeoutCyclesCnt++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(fsmStruct->timeoutCyclesCnt >5) {
|
|
|
+ // timeout
|
|
|
+ fsmStruct->receivedTelegrams=0;
|
|
|
+ //fsmStruct->timeoutCyclesCnt=0;
|
|
|
+
|
|
|
+ // set Error
|
|
|
+ BMS_Set_Error_CAN1_Timeout(s) ;
|
|
|
+ // set inverter fsm to can out
|
|
|
+ s->inverterState.inverterCanOnline= FALSE;
|
|
|
+ fsmStruct->fsmState=CAN1_FSM_SEND_HIGH_PRIO_DATA;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ // wait another 10ms for response
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ default :
|
|
|
+ // you should not be here
|
|
|
+ return FALSE;
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * @brief generate a pkg with value ID (32bit) Cell Index (8-bit) voltage (16 bit) temperature (8-bit)
|
|
|
+ */
|
|
|
+uint32_t generateCellInfoPkg(MASTER_CAN0_STRUCT_t* s, uint8_t cellIndex,BMS_CAN1_INVERTER_CELL_DATA_t* txPkg) {
|
|
|
+ // collect information
|
|
|
+ int8_t cellTemp;
|
|
|
+ uint16_t cellVoltage;
|
|
|
+ uint8_t SlaveNr;
|
|
|
+ uint8_t cellNr;
|
|
|
+ uint8_t payload[4];
|
|
|
+
|
|
|
+
|
|
|
+ uint32_t valueId=CAN1_TX_CELL_STATUS;
|
|
|
+
|
|
|
+ // shuffle Bits for correct Order
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&valueId);
|
|
|
+
|
|
|
+ SlaveNr = cellIndex/ (MAX_SLAVE_CELLS); // Slave Nr 0...14
|
|
|
+ cellNr = cellIndex - SlaveNr * MAX_SLAVE_CELLS ;// cellNr 0... 24
|
|
|
+
|
|
|
+ cellVoltage = s->Slave[SlaveNr].CellVoltage[cellNr];
|
|
|
+
|
|
|
+ if(s->Slave[SlaveNr].TempSensConnectionState[cellNr] == TEMP_SENSOR_CONNECTED) {
|
|
|
+ cellTemp = s->Slave[SlaveNr].CellTemp[cellNr] ;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // if temp sensor not connected transmit -51 deg
|
|
|
+ cellTemp = -51 ;
|
|
|
+ }
|
|
|
+ payload[0]=cellIndex;
|
|
|
+ payload[1]= cellVoltage >> 8;
|
|
|
+ payload[2]= cellVoltage & 0xFF;
|
|
|
+ payload[3]= (uint8_t)cellTemp;
|
|
|
+
|
|
|
+ shuffle_lsb_msb_can1((uint8_t*)&(payload[0]));
|
|
|
+
|
|
|
+ txPkg->valueId=valueId;
|
|
|
+ txPkg->payload= (payload[0] << 24) + (payload[1] << 16) + (payload[2] << 8) + payload[3] ;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+uint32_t Master_Balancer_fsm(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ switch(s->balancerState) {
|
|
|
+ case BMS_BALANCE_INIT:
|
|
|
+ if(s->allValuesInitialized== TRUE && (s->maxCellVoltage >=BMS_SLAVE_MAX_BALANCE_VOLTAGE)) {
|
|
|
+ // cellvoltage of system too high => no Balancing
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ else if(s->SoC_estimator.SoC_percentage_smooth <= BMS_SLAVE_MIN_BALANCE_SOC) {
|
|
|
+ // SoC under 80% do not balance
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ else if(s->maxHeatSinkTemp >= BMS_SLAVE_MAX_BALANCE_HEATSINK_TEMP) {
|
|
|
+ // Heatsink too hot => do not balance
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ else if(s->allValuesInitialized== TRUE && (s->UI_Board.Ibatt > BMS_SLAVE_BATTERY_CHARGE_THERESOLD) ) {
|
|
|
+ // battery discharging => no Balancing
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ else if(s->allValuesInitialized== TRUE && (s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD)) {
|
|
|
+ // ready for balancing
|
|
|
+ s->balancerState=BMS_BALANCE_GET_VOLTAGE;
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ // do nothing
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case BMS_BALANCE_GET_VOLTAGE:
|
|
|
+ // decide Which cells to balance
|
|
|
+ if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
+ set_balancer(s);
|
|
|
+ s->balancerState=BMS_BALANCE_BALANCE_CELLS;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ set_balancer_off(s) ;
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case BMS_BALANCE_BALANCE_CELLS:
|
|
|
+ // turn off balancing to obtain uneffected cell voltages
|
|
|
+ if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
+ set_balancer_off(s) ;
|
|
|
+ s->balancerState=BMS_BALANCE_COOL;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ set_balancer_off(s) ;
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case BMS_BALANCE_COOL:
|
|
|
+ // uneffected cell voltages are now available
|
|
|
+ if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
+ s->balancerState=BMS_BALANCE_INIT;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ set_balancer_off(s) ;
|
|
|
+ s->balancerState=BMS_BALANCE_OFF;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case BMS_BALANCE_OFF:
|
|
|
+ set_balancer_off(s) ;
|
|
|
+ if(s->UI_Board.Ibatt < BMS_SLAVE_BATTERY_CHARGE_THERESOLD) {
|
|
|
+ s->balancerState=BMS_BALANCE_INIT;
|
|
|
+ }
|
|
|
+ s->balancerState=BMS_BALANCE_INIT;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t get_nr_of_connected_slaves(MASTER_CAN0_STRUCT_t* s){
|
|
|
+ uint8_t i=0;
|
|
|
+ uint8_t nr=0;
|
|
|
+ for(i=0;i<CAN0_MAX_NR_OF_SLAVES-1;i++) {
|
|
|
+ if(s->Slave[i].SlaveConnectionState== CONNECTED) {
|
|
|
+ nr++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nr;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t RunningModeFSM(MASTER_CAN0_STRUCT_t* s){
|
|
|
+
|
|
|
+
|
|
|
+ // check for active Error States
|
|
|
+ if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_3) || s->RunMode.ErrorState3fsm != ES3_FSM_INIT) {
|
|
|
+ // handle Error Class 3
|
|
|
+ BMS_Master_ES3_fsm(s);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2) || s->RunMode.ErrorState2fsm != ES2_FSM_INIT) {
|
|
|
+ // handle Error Class 2
|
|
|
+ BMS_Master_ES2_fsm(s);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1) || s->RunMode.ErrorState1fsm != ES1_FSM_INIT) {
|
|
|
+ // handle Error Class 1
|
|
|
+ BMS_Master_ES1_fsm(s);
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ // normal operation
|
|
|
+ OPModeFSM( s );
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t OPModeFSM(MASTER_CAN0_STRUCT_t* s){
|
|
|
+ float SoC=s->SoC_estimator.SoC_percentage_smooth;
|
|
|
+ switch(s->RunMode.OperationMode) {
|
|
|
+ case OP_MODE_INIT:
|
|
|
+ // just jump to checkup startup conditions
|
|
|
+ s->RunMode.OperationMode=OP_MODE_CHECK_STARTUP_CONDITIONS;
|
|
|
+
|
|
|
+ CLEAR_OUTPIN(PIN_REGNR_LED4);
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case OP_MODE_CHECK_STARTUP_CONDITIONS:
|
|
|
+ if(s->allValuesInitialized==TRUE) {
|
|
|
+ // all values initalized and no error occured
|
|
|
+ // => Startup conditions apply => start startup
|
|
|
+ s->RunMode.OperationModeTimestamp=Global_1msCounter;
|
|
|
+ s->RunMode.OperationMode=OP_MODE_SET_PRECHARGE_RELAY;
|
|
|
+ s->RunMode.onCounter=0;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // wait till all values are initialiezd
|
|
|
+ s->RunMode.OperationMode=OP_MODE_CHECK_STARTUP_CONDITIONS;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case OP_MODE_SET_PRECHARGE_RELAY:
|
|
|
+ // set Low Side and Precharge Relais
|
|
|
+ s->relayState.LS_closed=TRUE;
|
|
|
+ s->relayState.PRECHARGE_closed=TRUE;
|
|
|
+ SwitchRelais( LS_RELAIS, 1);
|
|
|
+ SwitchRelais( PRE_CHARGE_RELAIS, 1);
|
|
|
+ if(s->RunMode.OperationModeTimestamp + 3000 <= Global_1msCounter) {
|
|
|
+ s->RunMode.OperationMode=OP_MODE_SET_MAIN_RELAY;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->RunMode.OperationMode= OP_MODE_SET_PRECHARGE_RELAY;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case OP_MODE_SET_MAIN_RELAY:
|
|
|
+ // Switch Highside relais
|
|
|
+ SwitchRelais( HS_RELAIS, 1);
|
|
|
+ s->relayState.HS_closed=TRUE;
|
|
|
+ s->RunMode.OperationMode=OP_MODE_NORMAL;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case OP_MODE_NORMAL:
|
|
|
+ // do nothing until SoC becomes low
|
|
|
+ s->RunMode.onCounter++;
|
|
|
+ if ( SoC < BMS_SOC_LOW_MARK) {
|
|
|
+ s->RunMode.OperationMode=OP_MODE_SOC_LOW;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->RunMode.OperationMode=OP_MODE_NORMAL;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case OP_MODE_SOC_LOW:
|
|
|
+ s->RunMode.onCounter++;
|
|
|
+ // do nothing until SoC becomes high again
|
|
|
+ if ( SoC > BMS_SOC_LOW_MARK) {
|
|
|
+ s->RunMode.OperationMode=OP_MODE_NORMAL;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ s->RunMode.OperationMode=OP_MODE_SOC_LOW;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ // something went horribly wrong
|
|
|
+ return FALSE;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+/*
|
|
|
+ * @brief Statemachine which handles Class 1 Errors
|
|
|
+ */
|
|
|
+
|
|
|
+uint32_t BMS_Master_ES1_fsm(MASTER_CAN0_STRUCT_t* s){
|
|
|
+ switch (s->RunMode.ErrorState1fsm) {
|
|
|
+ case ES1_FSM_INIT:
|
|
|
+ s->RunMode.ErrorState1fsm=ES1_FSM_CHECK_IF_ERROR_VALID;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES1_FSM_CHECK_IF_ERROR_VALID:
|
|
|
+ // Try to clear Error
|
|
|
+ BMS_Clear_Error_Buffer(s,BMS_ERROR_CLASS_1);
|
|
|
+
|
|
|
+
|
|
|
+ if(ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_1)) {
|
|
|
+ // Error still active
|
|
|
+ // Don't do anything
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // No Error
|
|
|
+ s->RunMode.ErrorState1fsm=ES1_FSM_ERROR_REVOKED;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES1_FSM_ERROR_REVOKED:
|
|
|
+ // No More Errors apply startup system again
|
|
|
+ s->RunMode.OperationMode=OP_MODE_INIT;
|
|
|
+ // set Fsm Back to INIT
|
|
|
+ s->RunMode.ErrorState1fsm=ES1_FSM_INIT;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t BMS_Master_ES2_fsm(MASTER_CAN0_STRUCT_t* s){
|
|
|
+ switch(s->RunMode.ErrorState2fsm) {
|
|
|
+ case ES2_FSM_INIT:
|
|
|
+ // save time for Timeout timer
|
|
|
+ s->RunMode.ErrorState2Timestamp=Global_1msCounter;
|
|
|
+ if(s->ErrorBuffer.ES2_New_Error==FALSE) {
|
|
|
+ // System had power cycle, Try to fix Error
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_CHECK_IF_ERROR_VALID;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // no restart => wait timeout then kill system
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_WAIT_FOR_SHUTDOWN;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES2_FSM_CHECK_IF_ERROR_VALID:
|
|
|
+ // Try to clear Error
|
|
|
+ BMS_Clear_Error_Buffer(s,BMS_ERROR_CLASS_2);
|
|
|
+ if(!ErrorStackCheckForActiveErrors(s,BMS_ERROR_CLASS_2)) {
|
|
|
+ // all Errors cleared
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_ERROR_REVOKED;
|
|
|
+ }
|
|
|
+ else if(s->RunMode.ErrorState2Timestamp + BMS_ERROR_FSM_ES2_TIMEOUT < Global_1msCounter ) {
|
|
|
+ // Timeout shutdown system
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_SYSTEM_SHUTDOWN;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // do nothing an wait for timeout or error to be cleared
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES2_FSM_WAIT_FOR_SHUTDOWN:
|
|
|
+ // wait timeout
|
|
|
+ if(s->RunMode.ErrorState2Timestamp + BMS_ERROR_FSM_ES2_TIMEOUT < Global_1msCounter ) {
|
|
|
+ // timeout
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_SYSTEM_SHUTDOWN;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // wait and do nothing
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES2_FSM_ERROR_REVOKED:
|
|
|
+ // no more Errors in the system => Start normal Operation again
|
|
|
+ s->RunMode.ErrorState2fsm=ES2_FSM_INIT;
|
|
|
+ s->RunMode.OperationMode=OP_MODE_INIT;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES2_FSM_SYSTEM_SHUTDOWN:
|
|
|
+ // Turn off power Supply
|
|
|
+ SwitchRelais( PWR_SUPPLY, 1);
|
|
|
+ // now System should be dead
|
|
|
+ while (1) {
|
|
|
+ // loop for ever
|
|
|
+ SwitchRelais( PWR_SUPPLY, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * @brief handle error class 3
|
|
|
+ * Todo System repair tool not implemented yet
|
|
|
+ */
|
|
|
+uint32_t BMS_Master_ES3_fsm(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ switch(s->RunMode.ErrorState3fsm){
|
|
|
+ case ES3_FSM_INIT:
|
|
|
+ // save timestamp for timeout
|
|
|
+ s->RunMode.ErrorState3Timestamp=Global_1msCounter;
|
|
|
+ s->RunMode.ErrorState3fsm=ES3_FSM_CONNECT_TO_SERVICE_TOOL;
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES3_FSM_CONNECT_TO_SERVICE_TOOL:
|
|
|
+ // not fully implemented yet
|
|
|
+ // just shut down system after timeout
|
|
|
+ if(s->RunMode.ErrorState3Timestamp + BMS_ERROR_FSM_ES3_TIMEOUT < Global_1msCounter ) {
|
|
|
+ // Shut down System
|
|
|
+ s->RunMode.ErrorState3fsm=ES3_FSM_SYSTEM_SHUTDOWN;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // wait
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case ES3_FSM_SYSTEM_SHUTDOWN:
|
|
|
+ // Turn off power Supply
|
|
|
+ SwitchRelais( PWR_SUPPLY, 1);
|
|
|
+ // now System should be dead
|
|
|
+ while (1) {
|
|
|
+ // loop for ever
|
|
|
+ SwitchRelais( PWR_SUPPLY, 1);
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * @brief check if connected Inverter has enough power to charge battery and recover from Wintermode
|
|
|
+ */
|
|
|
+
|
|
|
+int32_t checkIfInverterHasPower(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ if((s->inverter.rxStruct.expectedInputPower) > BMS_WINTER_MODE_RECOVER_PWR) {
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * @brief read Temp sensor on master Board
|
|
|
+ */
|
|
|
+uint32_t readMasterTempSensorFsm(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ switch(s->MasterTempSensState) {
|
|
|
+ case BMS_MASTER_TEMP_SENSOR_INITIATE_MEASUREMENT:
|
|
|
+ // trigger measurement
|
|
|
+ if(TempMess_triggerRead() == 0) {
|
|
|
+ // measurement triggered got to next state
|
|
|
+ s->MasterTempSensState=BMS_MASTER_TEMP_SENSOR_UPDATE_MEASUREMENT;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ case BMS_MASTER_TEMP_SENSOR_UPDATE_MEASUREMENT:
|
|
|
+ TempMess_update();
|
|
|
+ if(TempMess_poll_Value(&(s->masterTemp)) == 0) {
|
|
|
+ // readout succesfull go back to triggering measurement
|
|
|
+ s->MasterTempSensState=BMS_MASTER_TEMP_SENSOR_INITIATE_MEASUREMENT;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * @brief clear fifos
|
|
|
+ */
|
|
|
+
|
|
|
+uint32_t initUIFifo(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t i;
|
|
|
+ for(i=0;i<UI_VOLTAGE_FIFO_SIZE;i++) {
|
|
|
+ s->UI_Board.UbattFiFo[i]=0;
|
|
|
+ s->UI_Board.SystemVoltageFiFo[i]=0;
|
|
|
+ }
|
|
|
+ for(i=0;i<UI_CURRENT_FIFO_SIZE;i++) {
|
|
|
+ s->UI_Board.IbattFiFo[i]=0;
|
|
|
+ s->UI_Board.Ibatt_Inverter_FIFO[i]=0;
|
|
|
+ }
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t popUIFiFo(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t i;
|
|
|
+
|
|
|
+ for(i=UI_VOLTAGE_FIFO_SIZE-1;i>0;i--) {
|
|
|
+ s->UI_Board.UbattFiFo[i]=s->UI_Board.UbattFiFo[i-1];
|
|
|
+ }
|
|
|
+ s->UI_Board.UbattFiFo[0]=s->UI_Board.Ubatt;
|
|
|
+
|
|
|
+ for(i=UI_VOLTAGE_FIFO_SIZE-1;i>0;i--) {
|
|
|
+ s->UI_Board.SystemVoltageFiFo[i]=s->UI_Board.SystemVoltageFiFo[i-1];
|
|
|
+ }
|
|
|
+ s->UI_Board.SystemVoltageFiFo[0]=(s->systemVoltage);
|
|
|
+
|
|
|
+ for(i=UI_CURRENT_FIFO_SIZE-1;i>0;i--) {
|
|
|
+ s->UI_Board.IbattFiFo[i]=s->UI_Board.IbattFiFo[i-1];
|
|
|
+ }
|
|
|
+ s->UI_Board.IbattFiFo[0]=s->UI_Board.Ibatt;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t pushInverterCurrentFIFO(MASTER_CAN0_STRUCT_t* s) {
|
|
|
+ uint8_t i;
|
|
|
+ for(i=UI_CURRENT_FIFO_SIZE-1;i>0;i--) {
|
|
|
+ s->UI_Board.Ibatt_Inverter_FIFO[i]=s->UI_Board.Ibatt_Inverter_FIFO[i-1];
|
|
|
+ }
|
|
|
+ s->UI_Board.Ibatt_Inverter_FIFO[0]=s->inverter.rxStruct.batteryCurrent;
|
|
|
+
|
|
|
+// if(s->inverter.rxStruct.batteryCurrent > 100 || s->inverter.rxStruct.batteryCurrent < -100) {
|
|
|
+// // fake value
|
|
|
+// i++ ; // break here
|
|
|
+// }
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// *** End BMS_Master.c ******************************************************
|