#include #include #include #include "stm32f10x_rtc.h" #include "CDZ_State.h" #include "adc.h" #include "KC.h" #include "can.h" #include "CDZ_DATA.h" #include "Err_code.h" #include "Err_Info_Process.h" #include "SecTimer.h" #include "kC_Features.h" CDZ_Sys CDZ_SYS; Dcu_State DCU_SendState; extern CanRxMsg BMSCAN1Msg; struct New_CDZ CDZ_New={0}; void Charging_message_sending(void); bool StopChargeProcessing(void); static State handleIdleEvent(Event event); static State handleInitEvent(Event event); static State handleHandEvent(Event event); static State handleReadyEvent(Event event); static State handleStartEvent(Event event); static State handleRunningEvent(Event event); static State handleStopEvent(Event event); static State handleAC_ChargeEvent(Event event); static void handleIdleState(void); static void handleInitState(void); static void handleHandState(void); static void handleReadyState(void); static void handleStartState(void); static void handleRunningState(void); static void handleStoppedState(void); static void handleAC_ChargeState(void); static Event getIdleEvent(void); static Event getInitEvent(void); static Event getHandEvent(void); static Event getReadyEvent(void); static Event getStartEvent(void); static Event getRunningEvent(void); static Event getStopEvent(void); static Event getACChagreEvent(void); State CDZ_STATE_T1(Event event); State CDZ_STATE_T2(Event event); State CDZ_STATE_T3(Event event); State CDZ_STATE_T4(Event event); State CDZ_STATE_T5(Event event); State CDZ_STATE_T6(Event event); State CDZ_STATE_T7(Event event); State CDZ_STATE_T8(Event event); State CDZ_STATE_T9(Event event); State CDZ_STATE_T10(Event event); State CDZ_STATE_T11(Event event); State CDZ_STATE_T12(Event event); //异常进入结束阶段 State CDZ_STATE_T13(Event event); State CDZ_STATE_T14(Event event); State CDZ_STATE_T15(Event event); State CDZ_STATE_T16(Event event); // 状态表 3 2 状态转换 TransitionFunction stateTable[STATE_NUM][EVENT_NUM] = { {handleIdleEvent, CDZ_STATE_T1,CDZ_STATE_T15,NULL,NULL,NULL}, {handleInitEvent, CDZ_STATE_T2,CDZ_STATE_T13,NULL,NULL,NULL}, {handleHandEvent, CDZ_STATE_T3,CDZ_STATE_T13,CDZ_STATE_T14,NULL,NULL}, {handleReadyEvent, CDZ_STATE_T4,CDZ_STATE_T9,CDZ_STATE_T13,CDZ_STATE_T14,NULL}, {handleStartEvent, CDZ_STATE_T5,CDZ_STATE_T10,CDZ_STATE_T11,CDZ_STATE_T14,CDZ_STATE_T13}, {handleRunningEvent, CDZ_STATE_T6,CDZ_STATE_T8,CDZ_STATE_T12,NULL,NULL}, {handleStopEvent, CDZ_STATE_T7,NULL,NULL,NULL,NULL}, {handleAC_ChargeEvent, CDZ_STATE_T16,NULL,NULL,NULL,NULL}, }; // 事件处理函数表 状态操作 ActionFunction actionTable[STATE_NUM] = { handleIdleState, handleInitState, handleHandState, handleReadyState, handleStartState, handleRunningState, handleStoppedState, handleAC_ChargeState, }; //事件转换 EventFun EventTable[STATE_NUM] = { getIdleEvent, getInitEvent, getHandEvent, getReadyEvent, getStartEvent, getRunningEvent, getStopEvent, getACChagreEvent, }; //三个表是要一一对应的 Event getNextEvent(void) { return EVENT_0; } struct CDZ_Sys SysInfo = { .This_State = STATE_IDLE, };//充电系统信息 void AppCtrOnoffHandle(void) { DCDC_emergency_stop(); actionTable[SysInfo.This_State](); // 处理事件 Event event = EventTable[SysInfo.This_State](); // 获取下一个事件 // 根据状态表进行状态转换 TransitionFunction transition = stateTable[SysInfo.This_State][event]; //获取指针,如果发生某个时间将 if (transition != NULL) { SysInfo.This_State = transition(event);//转换时候的事件处理 } } /*事件状态处理函数表*/ // 处理IDLE状态下的操作 void handleIdleState(void) { // IDLE状态下的操作 // ... CDZ_Idle("IDLE状态\r\n"); } void handleInitState(void) { // Init状态下的操作 // ... CDZ_Init("Init状态\r\n"); } void handleHandState(void) { // hand状态下的操作 // ... CDZ_HAND("Hand状态\r\n"); if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_HANDING_CHM)!= 0) { CDZ_ChargeInfo.BmsRcvCode = 0; CDZ_SYS.GB_TYPE = GB2015; bms_SendBHM(GetBHM_Vol());//发送握手报文 printf("D_CDZ_HANDING_CHM\r\n"); } else if(TickOut(&CDZ_SYS.CHM_timer_count,3000)) { printf("NO Handshake Signal\n\r"); TickOut(&CDZ_SYS.CHM_timer_count, 0); } } #define STATE_0 0 #define STATE_1 1 #define STATE_2 2 typedef void (*Frame_Send)(uint16_t step); void Frame_Send_1(Frame_Send SendData,uint8_t PGN) { if(TickOut(&CDZ_New.timer[CC_CHARGE_READY_timer], 100)==true) { SendData(1); BMSCAN1Msg.Data[0]=0; BMSCAN1Msg.ExtId =0; TickOut(&CDZ_New.timer[CC_CHARGE_READY_timer], 0); } if((BMSCAN1Msg.Data[0]==0x11)&&(BMSCAN1Msg.Data[6]==PGN) &&(BMSCAN1Msg.ExtId & 0xFFFF0000) == CAN_CM_CTRL_ID ) { BMSCAN1Msg.Data[0]=0; BMSCAN1Msg.ExtId =0; CDZ_SYS.identifystep=STATE_1; } } void Frame_Send_2(Frame_Send SendData) { SendData(2); CDZ_SYS.identifystep=STATE_2; TickOut(&CDZ_New.timer[CC_CHARGE_READY_timer1], 0); } void Frame_Send_3(uint8_t PGN) { if((BMSCAN1Msg.Data[0]==0x13)&&(BMSCAN1Msg.Data[6]==PGN) &&(BMSCAN1Msg.ExtId & 0xFFFF0000) == CAN_CM_CTRL_ID ) { BMSCAN1Msg.Data[0]=0; BMSCAN1Msg.ExtId =0; CDZ_SYS.identifystep=STATE_0; CDZ_ChargeInfo.BmsRcvCode = 0; } if(TickOut(&CDZ_New.timer[CC_CHARGE_READY_timer1], 50)==true) { CDZ_SYS.identifystep=STATE_0; CDZ_ChargeInfo.BmsRcvCode = 0; } } void handleReadyState(void) { // READY状态下的操作 // ... CDZ_Ready("READY状态\r\n"); switch(CDZ_SYS.identifystep) { case STATE_0:Frame_Send_1(bms_SendBRM,0x02);break; case STATE_1:Frame_Send_2(bms_SendBRM);break; case STATE_2:Frame_Send_3(0x02);break; } } void handleStartState(void) { // START状态下的操作 // ... CDZ_STARTD("STARTD状态\r\n"); uint8_t bmsBROstate = ({ uint8_t BRO_record = 0; if(CDZ_ChargeInfo.REV_CML_FLAG == true\ && Charging_Pile_Param() == true\ && VCU_Charge_Condition() == Yes\ && BMS_Charge_Condition() == true\ && Send_BROAA_Condition() == Yes\ ) { BRO_record = 0xAA; } else { BRO_record = 0x0; } BRO_record; }); if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_CONFIG_CML)!= 0 && CDZ_ChargeInfo.ready != 0XAA) { bms_SendBRO(bmsBROstate); if(bmsBROstate == 0x00) { /*****未准备好的处理*******/ CLOSE_DC_RELAY(); } else if(READ_DC_YX2() == false) { /**********闭合DC继电器*********/ OPEN_DC_RELAY(); } //CDZ_ChargeInfo.BmsRcvCode &= ~D_CDZ_CONFIG_CML; CDZ_ChargeInfo.REV_CML_FLAG = true; } else if(CDZ_ChargeInfo.REV_CML_FLAG == false) { switch(CDZ_SYS.identifystep) { case STATE_0:Frame_Send_1(bms_SendBCP,0x06);break; case STATE_1:Frame_Send_2(bms_SendBCP);break; case STATE_2:Frame_Send_3(0x06);break; } } if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_CONFIG_CTS)!= 0) { CDZ_ChargeInfo.BmsRcvCode &= ~D_CDZ_CONFIG_CTS; } else if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_CONFIG_CRO)!= 0) { printf("CDZ_HANDING_CRM AA\n\r"); CDZ_ChargeInfo.BmsRcvCode &= ~D_CDZ_CONFIG_CRO; } } // 处理RUNNING状态下的操作 void handleRunningState(void) { // RUNNING状态下的操作 // ... CDZ_Running("RUNNING状态\r\n"); Charging_message_sending(); } // 处理STOPPED状态下的操作 void handleStoppedState(void) { // STOPPED状态下的操作 // ... CDZ_Stopped("STOPPED状态\r\n"); CDZ_SYS.GB_TYPE = 0; if(TickOut(&CDZ_New.timer[CHARGEING_timer], 50)==true) { bms_SendBST(0); TickOut(&CDZ_New.timer[CHARGEING_timer], 0); } if(TickOut(&CDZ_New.timer[end_timer], 250)==true) { bms_SendBSD(0); TickOut(&CDZ_New.timer[end_timer], 0); } } //交流充电函数 void handleAC_ChargeState(void) { } // 获取事件操作 Event getIdleEvent(void) { work_mode This_WorkState = Get_Sampl_Data.WorkMode(); // printf("e=%d\r\n",This_WorkState); if(This_WorkState== DC_Charging) { if(BMSDefaultParameters.parenr->Get_Car_Working_Mode(BMSDefaultParameters.parenr) == DC_charge_mode) { printf("进入直流充电流程\r\n"); return EVENT_1; } } else if(This_WorkState == AC_Charging) { memset((char *)&SecTimer,0,sizeof(SecTimer)); printf("进入交流充电流程\r\n"); return EVENT_2; } return EVENT_0; } Event getInitEvent(void) { if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_HANDING_CHM)!= 0 || (CDZ_ChargeInfo.BmsRcvCode & D_CDZ_HANDING_CRM)!= 0) { ErrClearSequence();//清除全部故障码 memset((char *)&CDZ_ChargeInfo,0,sizeof(CDZ_ChargeInfo)); memset((char *)&SecTimer,0,sizeof(SecTimer)); return EVENT_1; } else if(Get_Sampl_Data.WorkMode() != DC_Charging) { return EVENT_2; } return EVENT_0; } Event getHandEvent(void) { if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_HANDING_CRM)!= 0 && CDZ_SYS.GB_TYPE == GB2015) { printf("BMS_PROTOCAL_GB2015\n\r"); return EVENT_1; } else if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_HANDING_CRM)!= 0) { CDZ_SYS.GB_TYPE = GB2011; printf("BMS_PROTOCAL_GB2011\n\r"); return EVENT_1; } else if(Get_Sampl_Data.WorkMode() != DC_Charging) { return EVENT_2; } else if(SecStar(&SecTimer[Hand_SecTimerNUM],10))//10S退出握手阶段 { SecStop(&SecTimer[Hand_SecTimerNUM]); return EVENT_3; } return EVENT_0; } Event getReadyEvent(void) { if(CDZ_ChargeInfo.identify ==0xAA) //辨识到 { printf("REV D_CDZ_HANDING_CRM AA\n\r"); return EVENT_1; } else if(REV_Pile_TimeOut()==false) { printf("Ready-State Received The Charging Pile Abort Message\n\r"); return EVENT_2; } else if(Get_Sampl_Data.WorkMode() != DC_Charging) { return EVENT_3; } return EVENT_0; } Event getStartEvent(void) { if(CDZ_ChargeInfo.ready == 0XAA) { printf("CDZ__CRO AA to Charging\n\r"); return EVENT_1; } if(REV_Pile_TimeOut()==false) { printf("Start-State Received The Charging Pile Abort Message\n\r"); return EVENT_3; } else if(Err_Read(40) == true\ || Err_Read(6) == true\ || Err_Read(28) == true\ || Err_Read(33) == true\ || Err_Read(9) == true\ || Err_Read(10) == true\ || Err_Read(38) == true\ ) { //printf("Falut Code!!!!\n\r"); return EVENT_3; } else if(Get_Sampl_Data.WorkMode() != DC_Charging) { return EVENT_5; } // else if(SecStar(&SecTimer[Start2_SecTimerNUM],5) == true && VCU_Charge_Condition() == false) // { // SecStop(&SecTimer[Start2_SecTimerNUM]); // printf("NO HV!!!\n\r"); // return EVENT_3; // } else if(SecStar(&SecTimer[Start1_SecTimerNUM],60))//60S退出配置阶段 { SecStop(&SecTimer[Start1_SecTimerNUM]); return EVENT_4; } return EVENT_0; } Event getRunningEvent(void) { if(REV_Pile_TimeOut()==false) { printf("Running-State Received The Charging Pile Abort Message\n\r"); return EVENT_3; } else if(StopChargeProcessing() == false) { return EVENT_1; } // else if(Get_Sampl_Data.WorkMode() != DC_Charging) // { // return EVENT_3; // } return EVENT_0; } Event getStopEvent(void) { if(SecStar(&SecTimer[Stop_SecTimerNUM],10))//10S退出结束流程 { SecStop(&SecTimer[Stop_SecTimerNUM]); return EVENT_1; } return EVENT_0; } Event getACChagreEvent(void) { uint8_t OncConSt_Status = BMSDefaultParameters.parenr->Get_Car_OncConSt_Status(BMSDefaultParameters.parenr); // printf("CarWork %d\r\n",CarWork); if(OncConSt_Status == No_connect && SecStar(&SecTimer[ACCharg_SecTimerNUM],2) == true) { SecStop(&SecTimer[ACCharg_SecTimerNUM]); return EVENT_1; } else if(OncConSt_Status == Half_connect)//OBC半连接松开会出现无连接,当半连接时清除计数值 { SecStop(&SecTimer[ACCharg_SecTimerNUM]); } return EVENT_0; } /*T 表 Start */ State CDZ_STATE_T1(Event event) { printf("T1\r\n"); /******初始化默认********/ CLOSE_DC_RELAY(); CLOSE_AC_RELAY(); CLOSE_CC2_CC_RELAY(); OPEN_CC1_CP_RELAY(); /***********************/ return STATE_INIT; } State CDZ_STATE_T2(Event event) { printf("T2\r\n"); OPEN_CC1_CP_RELAY();//切换到CC1,让充电桩识别插枪 return STATE_HAND; } State CDZ_STATE_T3(Event event) { printf("T3\r\n"); return STATE_READY; } State CDZ_STATE_T4(Event event) { CDZ_ChargeInfo.REV_CML_FLAG = false;//进入配置阶段把标志位清除 printf("T4\r\n"); return STATE_START; } State CDZ_STATE_T5(Event event) { printf("T5\r\n"); return STATE_RUNNING; } State CDZ_STATE_T6(Event event) { printf("T6\r\n"); return STATE_STOPPED; } State CDZ_STATE_T7(Event event) { printf("T7\r\n"); CLOSE_DC_RELAY(); OPEN_AC_RELAY(); CLOSE_CC1_CP_RELAY(); return STATE_IDLE; } State CDZ_STATE_T8(Event event) { printf("T8\r\n"); return STATE_HAND; } State CDZ_STATE_T9(Event event) { printf("T9\r\n"); return STATE_HAND; } State CDZ_STATE_T10(Event event) { printf("T10\r\n"); return STATE_HAND; } State CDZ_STATE_T11(Event event) { printf("T11\r\n"); return STATE_STOPPED; } State CDZ_STATE_T12(Event event) //异常进入结束阶段 { printf("T12\r\n"); return STATE_STOPPED; } State CDZ_STATE_T13(Event event) //未插直流转接头 { printf("T13\r\n"); CLOSE_DC_RELAY(); return STATE_IDLE; } State CDZ_STATE_T14(Event event) //充电阶段超时 { printf("T14\r\n"); CLOSE_DC_RELAY(); return STATE_INIT; } State CDZ_STATE_T15(Event event) { printf("T15\r\n"); //进入交流充电流程 OPEN_AC_RELAY(); OPEN_CC2_CC_RELAY(); CLOSE_CC1_CP_RELAY(); OPEN_GUN_TEMP_RELAY(); return STATE_AC_Charge; } State CDZ_STATE_T16(Event event) { printf("T16\r\n"); CLOSE_GUN_TEMP_RELAY(); CLOSE_CC2_CC_RELAY(); OPEN_CC1_CP_RELAY(); extern enum GUNState This_GUNState; extern work_mode This_State_work; extern work_mode last_Stat_work; extern enum GUNState SUMStateLast; This_State_work = Normal_operation; last_Stat_work= Normal_operation; This_GUNState = NO_GUN; return STATE_IDLE; } /*T 表 END */ /* EVENT 0 事件跳转 */ // 处理空闲事件 State handleIdleEvent(Event event) { // 处理开始事件的逻辑 // ... CDZ_Idle("空闲状态\r\n"); return STATE_IDLE; // 返回下一个状态 } // 处理初始化事件 State handleInitEvent(Event event) { // 处理开始事件的逻辑 // ... CDZ_Init("处理初始化事件\r\n"); return STATE_INIT; // 返回下一个状态 } // 处理握手事件 State handleHandEvent(Event event) { // 处理开始事件的逻辑 // ... CDZ_HAND("处理握手事件\r\n"); return STATE_HAND; // 返回下一个状态 } // 处理准备事件 State handleReadyEvent(Event event) { // 处理开始事件的逻辑 // ... return STATE_READY; // 返回下一个状态 } // 处理开始事件 State handleStartEvent(Event event) { // 处理开始事件的逻辑 // ... return STATE_START; // 返回下一个状态 } // 处理进行中事件 State handleRunningEvent(Event event) { // 处理开始事件的逻辑 // ... return STATE_RUNNING; // 返回下一个状态 } // 处理停止事件 State handleStopEvent(Event event) { // 处理停止事件的逻辑 // ... return STATE_STOPPED; // 返回下一个状态 } State handleAC_ChargeEvent(Event event) { // 交流充电事项 return STATE_AC_Charge; // 返回下一个状态 } /************以下为基础构建*******/ //急停 bool DCDC_emergency_stop(void) { return true; } bool StopChargeProcessing(void) { if(BMS_Charge_Condition()==false) { printf("BMS The Charging Phase Stops Abnormally\n\r"); return false; } else if(DCU_Time_Out_Handl()==false) { return false; } else if(DCU_Fault_SelfTest()==false) { return false; } else if((CDZ_ChargeInfo.BmsRcvCode & D_CDZ_CHARGE_CCS) != 0 && CDZ_Charging_Condition() == false)//CCS 不允许充电 { printf("CCS No Charging Allowedn\r"); return false; } return true; } void Charging_message_sending(void) { switch(CDZ_SYS.identifystep) { case STATE_0:Frame_Send_1(bms_SendBCS,0x11);break; case STATE_1:Frame_Send_2(bms_SendBCS);break; case STATE_2:Frame_Send_3(0x011);break; } if(TickOut(&CDZ_New.timer[CHARGEING_timer], 50)==true) { bms_SendBCL(); TickOut(&CDZ_New.timer[CHARGEING_timer], 0); } if(TickOut(&CDZ_New.timer[CHARGEING_timer2], 250)==true) { bms_SendBSM(1); TickOut(&CDZ_New.timer[CHARGEING_timer2], 0); } }