123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- /*
- * @Author : ChenJie
- * @Date : 2021-12-15 10:40:06
- * @Version : V3.0
- * @LastEditors : ChenJie
- * @LastEditTime : 2022-01-11 18:14:41
- * @Description : AppFunc
- * @FilePath : \VehicleControl\VehicleControl\src\System\Vehicle\AppFunc.c
- */
- #include "string.h"
- #include "stdlib.h"
- #include "HardwareLib.h"
- #include "CanVar.h"
- #include "math.h"
- #include "AppFunc.h"
- #include "Std_Types.h"
- #define Lock 0
- #define Unlock 1
- #define LockCtrl() PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, Lock)
- #define UnlockCtrl() PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, Unlock)
- void LockAndUnlockCtrl()
- {
- static uint16 LockDelay = 0;
- static uint16 UnlockDelay = 0;
- static uint8 CtrlFlg = 0;
- static uint16 BleConCounter = 0;
- static uint16 battSwpeModeCounter = 0;
- if (ebcd_flg_ebcManCtrlEn) //手动控制使能
- {
- if (ebcd_flg_ebcManCtrlMode==1)//循环控制模式
- {
- if(ebcd_st_lockSensor==1)//处于锁紧状态,传感器判定一个
- {
- LockDelay = LockDelay + 10;
- if(LockDelay>LockDelayTime)//锁紧状态超时就解锁
- {
- ManuControlTimes++;
- LockDelay = 0;
- CtrlFlg = Unlock;
- }
- }
- else if(ebcd_st_unlockSensor==1)//处于解锁状态,传感器判定一个
- {
- UnlockDelay = UnlockDelay + 10;
- if(UnlockDelay>UnlockDelayTime)
- {
- UnlockDelay = 0;
- CtrlFlg = Lock;
- }
- }
- }
- else if(ebcd_flg_ebcManCtrlMode==2)//控制锁紧
- {
- CtrlFlg = Lock;
- }
- else if(ebcd_flg_ebcManCtrlMode==3)//控制解锁
- {
- CtrlFlg = Unlock;
- }
- }
- else //正常工作模式,暂时为空
- {
- switch(ebcd_st_ebcWork)
- {
- case 0://行车状态,自动进入,默认状态
- {
- PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,0);//行车状态换电指示灯关闭
- if(bled_flg_handShake==1)
- {
- ebcd_st_ebcWork = 1;
- bled_flg_handShake = 0;
- break;
- }
- break;
- }
- case 1://通讯状态,握手才能进入
- {
- BleConCounter = BleConCounter + 10;
- if((BleConCounter%1000)<500)//换电指示灯闪烁
- {
- PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,1);
- }
- else
- {
- PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,0);
- }
- if(BleConCounter>=60*1000||bled_flg_backToDrv == 1)
- {
- ebcd_st_ebcWork = 0;//切换到行车状态
- BleConCounter = 0;
- bled_flg_backToDrv = 0;
- break;
- }
- else if(BattCD_stWakeupU.B.bLogic==0&&(bled_flg_toSwapMod==1 || BattCD_stWakeupU.B.bWkpSig1==1))
- {
- ebcd_st_ebcWork = 2;//切换到换电状态
- BleConCounter = 0;
- bled_flg_toSwapMod = 0;
- break;
- }
- if(bled_st_dataTrans==1)
- {
- BleConCounter = 0;
- bled_st_dataTrans = 0;
- }
- break;
- }
- case 2://换电状态
- {
- PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,1);//换电状态换电指示灯常亮
- battSwpeModeCounter = battSwpeModeCounter + 10;
- if(bled_flg_swapBattDone==1)
- {
- bled_flg_swapBattDone = 0;
- ebcd_st_ebcWork = 1; //切换到通讯状态
- break;
- }
- else if(battSwpeModeCounter>=60*1000)
- {
- battSwpeModeCounter = 0;//切换到行车状态
- ebcd_st_ebcWork = 0;
- break;
- }
- if(bled_st_dataTrans==1)
- {
- battSwpeModeCounter = 0;
- bled_st_dataTrans = 0;
- }
- break;
- }
- }
- }
- //锁紧解锁执行
- if(CtrlFlg)
- {
- UnlockCtrl();
- }
- else
- {
- LockCtrl();
- }
- }
- /**
- * @brief : 互锁检测函数
- * @param {*}
- * @return {*}
- */
- void GetHVLockState(void)
- {
- // PWM输出,互锁1检测,频率100HZ,占空比30%
- uint16 VehCo_fTestUI = 1000;
- uint16 VehCo_rTestUW = 3000;
- uint32 PwmFreAcq = 0;
- uint16 PwmDutyAcq = 0;
- PPWMDrv_Interface(_PPWM_INDEX_HVLOCK2, VehCo_fTestUI, VehCo_rTestUW);
- PulseAcqDrv_GetChanFreq(_PULSEACQ_INDEX_HVLOCK2, &PwmFreAcq, &PwmDutyAcq);
- if (abs(PwmFreAcq - VehCo_fTestUI) < 100 && abs(PwmDutyAcq - VehCo_rTestUW) < 500)
- {
- m_flg_HVlock1 = 1;
- }
- else
- {
- m_flg_HVlock1 = 0;
- }
- //互锁2检测,配置高有效,悬空为0则未接入,高电平为1则接入,
- // DINDrv_SetChanThres(_DIN_INDEX_PLUGHVLOCK, 0, 4095 * 3);
- m_flg_HVlock2 = DINDrv_GetChanState(_DIN_INDEX_PLUGHVLOCK);
- }
- /**
- * @brief : 数字量传感器信号检测函数
- * @param {*}
- * @return {*}
- */
- void GetDIOState(void)
- {
- uint8 temp[4];
- //松开传感器检测,配置低有效,底层悬空为1,触发为0,应用层输出悬空为0,触发为1
- memset(temp, 0x00, 4);
- ebcd_st_unlockSensor = 0;
- DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR1, 1, 4095U);
- DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR2, 1, 4095U);
- DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR3, 1, 4095U);
- DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR4, 1, 4095U);
- temp[0] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR1);
- temp[1] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR2);
- temp[2] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR3);
- temp[3] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR4);
- ebcd_st_unlockSensor = (getbit(temp[3], 0) << 3) | (getbit(temp[2], 0) << 2) | (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
- //夹紧传感器检测,配置低有效,底层悬空为1,触发为0,应用层输出悬空为0,触发为1
- memset(temp, 0x00, 4);
- ebcd_st_lockSensor = 0;
- DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR1, 1, 4095U);
- DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR2, 1, 4095U);
- DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR3, 1, 4095U);
- DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR4, 1, 4095U);
- temp[0] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR1);
- temp[1] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR2);
- temp[2] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR3);
- temp[3] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR4);
- ebcd_st_lockSensor = (getbit(temp[3], 0) << 3) | (getbit(temp[2], 0) << 2) | (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
- //落座传感器检测,高有效,悬空为0则未接入,高电平为1则接入
- memset(temp, 0x00, 4);
- temp[0] = !DINDrv_GetChanState(_DIN_INDEX_READYSENSOR1);
- temp[1] = !DINDrv_GetChanState(_DIN_INDEX_READYSENSOR2);
- ebcd_st_pedstSensor = (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
- //根据上述量得到,运动状态值
- if (ebcd_st_lockSensor == 0x0F)
- {
- ebcd_st_lockSucJug = 1;
- }
- else if (ebcd_st_lockSensor == 0x00)
- {
- ebcd_st_lockSucJug = 0;
- }
- else
- {
- ebcd_st_lockSucJug = 2;
- }
- if (ebcd_st_unlockSensor == 0x0F)
- {
- ebcd_st_unlockSucJug = 1;
- }
- else if (ebcd_st_unlockSensor == 0x00)
- {
- ebcd_st_unlockSucJug = 2;
- }
- else
- {
- ebcd_st_unlockSucJug = 0;
- }
- if (ebcd_st_pedstSucJug == 0x03)
- {
- ebcd_st_pedstSucJug = 1;
- }
- else if (ebcd_st_pedstSucJug == 0x00)
- {
- ebcd_st_pedstSucJug = 0;
- }
- else
- {
- ebcd_st_pedstSucJug = 2;
- }
- }
- /**
- * @brief : 获取模拟量输入值,并进行转换
- * @param {*}
- * @return {*}
- */
- void GetAIOValue(void)
- {
- uint16 AirPressureTemp_Vol = 0;
- uint16 PluginTemp1_Vol = 0;
- uint32 PluginTemp1_R = 0;
- uint16 PluginTemp2_Vol = 0;
- uint32 PluginTemp2_R = 0;
- uint16 PluginTemp3_Vol = 0;
- uint32 PluginTemp3_R = 0;
- uint16 PluginTemp4_Vol = 0;
- uint32 PluginTemp4_R = 0;
- AirPressureTemp_Vol = ATDDrv_GetChanResult(_ATD_INDEX_AIRPRESSURE);
- AirPressureTemp_Vol = (uint16)(AirPressureTemp_Vol * 1000 / 4095.0);
- Test_LockPressure = AirPressureTemp_Vol;//锁紧力测试变量
- /*气压数据转换*/
- AirPressureTemp_Vol = MAX(AirPressureTemp_Vol, 500);
- AirPressureTemp_Vol = MIN(AirPressureTemp_Vol, 4500);
- ebcd_P_airSensor = (uint8)((AirPressureTemp_Vol - 500) / 40);
- /*温度采集获取*/
- PluginTemp1_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP1);
- PluginTemp2_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP2);
- PluginTemp3_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP3);
- PluginTemp4_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP4);
- PluginTemp1_R = (uint32)((PluginTemp1_Vol / (5.0 * 4095 - PluginTemp1_Vol)) * 1000.0);
- PluginTemp2_R = (uint32)((PluginTemp2_Vol / (5.0 * 4095 - PluginTemp2_Vol)) * 1000.0);
- PluginTemp3_R = (uint32)((PluginTemp3_Vol / (5.0 * 4095 - PluginTemp3_Vol)) * 1000.0);
- PluginTemp4_R = (uint32)((PluginTemp4_Vol / (5.0 * 4095 - PluginTemp4_Vol)) * 1000.0);
- ebcd_T_plugin[0] = (uint8)Look1_u32u8(PluginTemp1_R, R_table, T_table, 240);
- ebcd_T_plugin[1] = (uint8)Look1_u32u8(PluginTemp2_R, R_table, T_table, 240);
- ebcd_T_plugin[2] = (uint8)Look1_u32u8(PluginTemp3_R, R_table, T_table, 240);
- ebcd_T_plugin[3] = (uint8)Look1_u32u8(PluginTemp4_R, R_table, T_table, 240);
- }
- /**
- * @brief : lookUp Table Fun
- * @param {uint32} u0 x
- * @param {uint32} bp0 x_table
- * @param {uint16} table y_table
- * @param {uint16} maxIndex
- * @return {*}
- */
- uint16 Look1_u32u8(uint32 u0, uint32 *bp0, uint8 *table, uint16 MaxLen)
- {
- uint32 bpIdx = 0;
- uint32 iLeft = 0;
- uint32 iRght = 0;
- uint16 y = 0;
- uint32 yL_0d0 = 0;
- uint32 yR_0d0 = 0;
- uint32 maxIndex = MaxLen - 1;
- if (u0 <= bp0[0U])
- {
- iLeft = 0U;
- iRght = 0U;
- }
- else if (u0 < bp0[maxIndex])
- {
- //对折法寻找u0的位置
- bpIdx = maxIndex >> 1U;
- iLeft = 0U;
- iRght = maxIndex;
- while ((iRght - iLeft) > 1)
- {
- if (u0 < bp0[bpIdx])
- {
- iRght = bpIdx;
- }
- else
- {
- iLeft = bpIdx;
- }
- bpIdx = (iRght + iLeft) >> 1U;
- }
- }
- else
- {
- iLeft = maxIndex;
- iRght = maxIndex;
- }
- //找到位置以后计算插值
- if (iLeft != iRght)
- {
- //线性插值
- yR_0d0 = table[iLeft + 1U];
- yL_0d0 = table[iLeft];
- if (yR_0d0 >= yL_0d0)
- {
- y = (uint16)(((uint32)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
- }
- else
- {
- y = (uint16)(yL_0d0 - ((uint32)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]));
- }
- }
- else
- {
- y = (uint16)table[iLeft];
- }
- return y;
- }
|