123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- #include "tasks.h"
- #include "iwdg.h"
- #include "gpio.h"
- #include "can_process.h"
- #include "uart_process.h"
- #include "eeprom_24c02.h"
- #include "eeprom_flash.h"
- #include "adc.h"
- #include "hall_sensor.h"
- #include "torque_sensor.h"
- #include "gas_sensor.h"
- #include "math_tools.h"
- #include "remain_distance.h"
- #include "protect_check.h"
- #include "fault_check.h"
- #include "key_driver.h"
- #include "encrypt.h"
- /************************全局变量************************/
- TrueOrFalse_Flag_Struct_t IsInitFinish_Flag = FALSE;
- /**************************局部函数定义*********************/
- /**************************全局函数定义*********************/
- //1ms任务处理函数
- void HAL_SYSTICK_Callback(void)
- {
- static uint16_t TimeCnt_10ms = 0;
- static uint16_t TimeCnt_100ms = 0;
- static uint16_t TimeCnt_1000ms = 0;
- static uint32_t FaultDelayTime = 0;
-
- if(IsInitFinish_Flag == FALSE)
- {
- return;
- }
-
- //踏频传感器采集及计算
- CadenceSensor_Process(&MC_CadenceResult, MC_ConfigParam.StopTime);
-
- //根据电机工作模式(MC_WorkMode)、力矩传感器和指拨AD值(ADC_SensorData)、控制档位(MC_ControlCode.GearSt)计算控制FOC输入值(MC_CalParam)
- if(((uint8_t)(MC_WorkMode ^ MC_WorkMode_Back) != (uint8_t)~0) ||
- ((uint8_t)(MC_ControlCode.GearSt ^ MC_ControlCode_Back.GearSt) != (uint8_t)~0) ||
- ((uint8_t)(MC_ControlCode.LightSwitch ^ MC_ControlCode_Back.LightSwitch) != (uint8_t)~0))
- {
- if((HAL_GetTick() - FaultDelayTime) > 200)
- {
- MC_ErrorCode.ERROR_Bit.Fault_MCU = 1;
- }
- }
- else
- {
- FaultDelayTime = HAL_GetTick();
- }
- MC_CalParam_Cal(MC_WorkMode, ADC_SensorData, MC_ControlCode.GearSt, IsBreakTrig_Flag, &MC_CalParam);
-
- //更新控制参数备份值
- Update_MC_CalParam_Back();
-
- //更新力矩传感器零点值
- TorqueOffSetData_Present_Update(&TorqueOffSetData.PresentData, ADC1_Result[ADC1_RANK_TORQUE_SENSOR], MC_TorqueCorrectParam.K, &MC_ErrorCode);
-
- //更新指拨零点值
- GasSensorOffSetData_Update(&GasSensor_OffSet, ADC1_Result[ADC1_RANK_GAS], &MC_ErrorCode);
-
- //10ms任务
- TimeCnt_10ms++;
- if(TimeCnt_10ms >= 10)
- {
- TimeCnt_10ms = 0;
- }
-
- //100ms任务
- TimeCnt_100ms++;
- if(TimeCnt_100ms >= 100)
- {
- TimeCnt_100ms = 0;
- //踏频计算滑动均值滤波
- MC_RunInfo.Cadence = MovingAverageFilter(MC_CadenceResult.Cadence_Data, MC_Cadence_Array, sizeof(MC_Cadence_Array) / 2);
- }
-
- //1000ms任务
- TimeCnt_1000ms++;
- if(TimeCnt_1000ms >= 1000)
- {
- TimeCnt_1000ms = 0;
-
- }
- }
- //CAN数据解析函数
- void CanRx_Process(void)
- {
- CAN_RxData_Process(&CAN_RxBuf_Struct_PBU, 500);
- CAN_RxData_Process_Temp(&CAN_RxBuf_Struct_BMS, 500);
- // CAN_RxData_Process(&CAN_RxBuf_Struct_BMS, 500);
- CAN_RxData_Process(&CAN_RxBuf_Struct_HMI, 500);
- CAN_RxData_Process(&CAN_RxBuf_Struct_CDL, 500);
- }
- //UART数据解析函数
- void UartRx_Process(void)
- {
- Uart_RxData_Process(&UART_RxBuff_Struct3, 500);
- }
- //MC运行信息更新
- void MC_RunInfo_Update(void)
- {
- static uint32_t PeriodTimeCnt = 0;
-
- if((HAL_GetTick() - PeriodTimeCnt) >= 200)
- {
- PeriodTimeCnt = HAL_GetTick();
-
- //计算电功率
- MC_RunInfo.Power = (uint16_t)((uint32_t)(MC_RunInfo.BusCurrent * MC_RunInfo.BusVoltage) / 1000000) / 2;
- MC_RunInfo.Power = (MC_RunInfo.Power < 20) ? 0 : MC_RunInfo.Power;
-
- //更新踏频方向
- MC_RunInfo.CadenceDir = MC_CadenceResult.Cadence_Dir;
-
- //计算力矩值
- MC_RunInfo.Torque = ADC_SensorData.TorqueSensor / 28;
-
- //当前助力档位
- MC_RunInfo.GearSt = MC_ControlCode.GearSt;
-
- //当前灯开关
- MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch;
-
- //剩余电量
- MC_RunInfo.SOC = (DeviceOnLine_Status.Status_Bit.BMS_OffLine == 1) ? Battery_SocCal(MC_RunInfo.BusVoltage, MC_RunInfo.BusCurrent)
- : BMS_RunInfo.SOC;
- //续航里程
- MC_RunInfo.RemainDistance = RemainDis.remainDistance;
-
- //总里程计算
- #define ODO_KM_SAVE_UNIT 1 //ODO里程存储最小单位
- static uint32_t WheelTurnCount = 0;
- if((MC_SpeedSensorData.WheelTurnCount - WheelTurnCount) >= (ODO_KM_SAVE_UNIT * 100000 / MC_ConfigParam.WheelSize))
- {
- MC_RunInfo.ODO_Km = MC_RunLog.ODO_Km + 1;
- WheelTurnCount = MC_SpeedSensorData.WheelTurnCount;
- }
-
- //存储骑行总里程
- if((MC_RunInfo.ODO_Km - MC_RunLog.ODO_Km) >= ODO_KM_SAVE_UNIT)
- {
- MC_RunLog.ODO_Km = MC_RunInfo.ODO_Km;
- SaveParamToEEprom_24C02(&I2C_Handle_EEPROM, EEPROM_24C02_ADDR_RUN_LOG, sizeof(MC_RunLog_Struct_t), (uint8_t*)&MC_RunLog.PowerOnCnt);
- }
-
- //平均功耗
- MC_RunInfo.PowerPerKm = RemainDis.Power_per_km_result / 10;
-
- }
- }
- //MC故障码发送
- void MC_SendErrorCode_Process(MC_ErrorCode_Struct_t ErrorCode)
- {
- static uint32_t PeriodTimeCnt = 0;
-
- if(ErrorCode.Code != 0x00000000)
- {
- if((HAL_GetTick() - PeriodTimeCnt) > 500)
- {
- PeriodTimeCnt = HAL_GetTick();
- SendData(ID_MC_BC, MODE_REPORT, 0x1104, (uint8_t*)&ErrorCode.Code);
- }
- }
- else
- {
- PeriodTimeCnt = HAL_GetTick();
- }
- }
- //MC主动发送运行信息
- void MC_SendRunInfo_Process(MC_WorkMode_Struct_t WorkMode)
- {
- static uint32_t PeriodTimeCnt = 0;
-
- if(WorkMode == MC_WorkMode_Config)
- {
- if((HAL_GetTick() - PeriodTimeCnt) >= 200)
- {
- SendData(ID_MC_BC, MODE_REPORT, 0x1020, (uint8_t*)&MC_RunInfo.BikeSpeed);
- PeriodTimeCnt = HAL_GetTick();
- }
- }
- else
- {
- PeriodTimeCnt = HAL_GetTick();
- }
- }
- //发给TE的传感器数据处理
- void MC_TE_SensorData_Process(uint16_t Speed, MC_TE_SensorData_Struct_t* p_MC_TE_SensorData)
- {
- static uint32_t PeriodTimeCnt = 0;
- static GPIO_PinState Cadence_Hall_1;
- static GPIO_PinState Cadence_Hall_2;
- static GPIO_PinState Motor_Hall_A;
- static GPIO_PinState Motor_Hall_B;
- static GPIO_PinState Break;
- static GPIO_PinState SpeedSensor;
- static TrueOrFalse_Flag_Struct_t IsFirstEnterFalg = TRUE;
- GPIO_PinState GPIO_PinState_Temp;
-
- //初始化变量
- if(IsFirstEnterFalg == TRUE)
- {
- Cadence_Hall_1 = HAL_GPIO_ReadPin(CADENCE_1_GPIO_Port, CADENCE_1_Pin);
- Cadence_Hall_2 = HAL_GPIO_ReadPin(CADENCE_2_GPIO_Port, CADENCE_2_Pin);
- Motor_Hall_A = HAL_GPIO_ReadPin(HALL_A_GPIO_Port, HALL_A_Pin);
- Motor_Hall_B = HAL_GPIO_ReadPin(HALL_B_GPIO_Port, HALL_B_Pin);
- Break = HAL_GPIO_ReadPin(BREAK_IN_GPIO_Port, BREAK_IN_Pin);
- SpeedSensor = HAL_GPIO_ReadPin(SPEED_SENSOR_GPIO_Port, SPEED_SENSOR_Pin);
- p_MC_TE_SensorData->CadenceHall_1_Cnt = 0;
- p_MC_TE_SensorData->CadenceHall_2_Cnt = 0;
- p_MC_TE_SensorData->MotorHall_A_Cnt = 0;
- p_MC_TE_SensorData->MotorHall_B_Cnt = 0;
- p_MC_TE_SensorData->BreakTrgiCnt = 0;
- p_MC_TE_SensorData->SpeedSensorTrigCnt = 0;
- IsFirstEnterFalg = FALSE;
- }
-
- //ADC数据更新采集
- p_MC_TE_SensorData->AD_BusCurrent = ADC1_Result_Filt[ADC1_RANK_CURRENT];
- p_MC_TE_SensorData->AD_RoilTemp = ADC1_Result_Filt[ADC1_RANK_NTC_ROIL];
- p_MC_TE_SensorData->AD_TE_Voltage = ADC1_Result_Filt[ADC1_RANK_3V3_TE];
- p_MC_TE_SensorData->AD_Torque = ADC1_Result_Filt[ADC1_RANK_TORQUE_SENSOR];
-
- //踏频霍尔1
- GPIO_PinState_Temp = HAL_GPIO_ReadPin(CADENCE_1_GPIO_Port, CADENCE_1_Pin);
- if(Cadence_Hall_1 != GPIO_PinState_Temp)
- {
- p_MC_TE_SensorData->CadenceHall_1_Cnt++;
- }
- Cadence_Hall_1 = GPIO_PinState_Temp;
- //踏频霍尔2
- GPIO_PinState_Temp = HAL_GPIO_ReadPin(CADENCE_2_GPIO_Port, CADENCE_2_Pin);
- if(Cadence_Hall_2 != GPIO_PinState_Temp)
- {
- p_MC_TE_SensorData->CadenceHall_2_Cnt++;
- }
- Cadence_Hall_2 = GPIO_PinState_Temp;
- //马达霍尔A
- GPIO_PinState_Temp = HAL_GPIO_ReadPin(HALL_A_GPIO_Port, HALL_A_Pin);
- if(Motor_Hall_A != GPIO_PinState_Temp)
- {
- p_MC_TE_SensorData->MotorHall_A_Cnt++;
- }
- Motor_Hall_A = GPIO_PinState_Temp;
- //马达霍尔B
- GPIO_PinState_Temp = HAL_GPIO_ReadPin(HALL_B_GPIO_Port, HALL_B_Pin);
- if(Motor_Hall_B != GPIO_PinState_Temp)
- {
- p_MC_TE_SensorData->MotorHall_B_Cnt++;
- }
- Motor_Hall_B = GPIO_PinState_Temp;
- //刹车
- GPIO_PinState_Temp = HAL_GPIO_ReadPin(BREAK_IN_GPIO_Port, BREAK_IN_Pin);
- if(Break != GPIO_PinState_Temp)
- {
- p_MC_TE_SensorData->BreakTrgiCnt++;
- }
- Break = GPIO_PinState_Temp;
- //速度传感器
- GPIO_PinState_Temp = HAL_GPIO_ReadPin(SPEED_SENSOR_GPIO_Port, SPEED_SENSOR_Pin);
- if(SpeedSensor != GPIO_PinState_Temp)
- {
- p_MC_TE_SensorData->SpeedSensorTrigCnt++;
- }
- SpeedSensor = GPIO_PinState_Temp;
- //电机转速值
- p_MC_TE_SensorData->SynC_Clock_Freq = 1000; //1000KHz
-
- //数据发送
- if((HAL_GetTick() - PeriodTimeCnt) >= 500)
- {
- SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x1014, (uint8_t*)&p_MC_TE_SensorData->AD_Torque);
- IsComOK_TE = FALSE;
- PeriodTimeCnt = HAL_GetTick();
- }
- }
- //根据踏频和母线电流计算限流系数
- uint8_t MC_CadenceLimit_Cal(uint8_t Cadence, uint16_t Current, uint8_t T_Roil)
- {
- static uint32_t PeriodTimeCnt = 0;
- static uint32_t IdcFiltSum = 0;
- static uint8_t IdcFiltCnt = 0; //滤波输入值计算
- static uint16_t IdcFilt = 0; //滤波结果
- static uint16_t Limit_Cnt = 0; //限流计时值
- static uint16_t OK_Cnt = 0; //限流恢复计时值
- static FlagStatus LimitFlag = RESET;
- static uint8_t Result = 100;
-
- if((HAL_GetTick() - PeriodTimeCnt) >= 100)
- {
- PeriodTimeCnt = HAL_GetTick();
-
- //母线电流滤波
- IdcFiltSum += Current;
- IdcFiltCnt++;
- if(IdcFiltCnt >= 8)
- {
- IdcFilt = IdcFiltSum >> 3;
- IdcFiltCnt = 0;
- IdcFiltSum = 0;
- }
-
- //限流保护计时
- if((Cadence < 70) && (IdcFilt > 6000))
- {
- Limit_Cnt++;
- }
- else
- {
- Limit_Cnt = 0;
- }
-
- //限流恢复计时
- if(((Cadence > 70) || (IdcFilt < 5000)) && (T_Roil < 150))
- {
- OK_Cnt++;
- }
- else
- {
- OK_Cnt = 0;
- }
-
- //限流判断
- if(Limit_Cnt > 300)
- {
- Limit_Cnt = 0;
- LimitFlag = SET;
- }
-
- //限流恢复判断
- if(OK_Cnt > 100)
- {
- OK_Cnt = 0;
- LimitFlag = RESET;
- }
-
- //限流系数计算
- if(LimitFlag == SET)
- {
- if(Cadence < 70)
- {
- Result = 30 + Cadence;
- Result = (Result > 100) ? 100 : Result;
- }
- else
- {
- Result = 100;
- }
- }
- else
- {
- Result = 100;
- }
- }
-
- return Result;
- }
- //接收到关机指令处理
- void PowerOff_Process(void)
- {
- MC_ControlCode.GearSt = MC_GearSt_OFF;
- Update_MC_ControlCode_Back();
- SendData(ID_MC_BC, MODE_REPORT, 0x1305, (uint8_t*)"READY");
- }
- //CAN设备通信状态检测处理
- void MC_CanRxCheck_Process(MC_WorkMode_Struct_t WorkMode, MC_GearSt_Struct_t* GearSt)
- {
- if(WorkMode == MC_WorkMode_Run)
- {
- //PBU通信状态检测
- if(IsComOK_PBU.IsOK_Flag == TRUE)
- {
- if((HAL_GetTick() - IsComOK_PBU.OK_TrigTime) > 1000)
- {
- IsComOK_PBU.IsOK_Flag = FALSE;
- *GearSt = MC_GearSt_OFF;
- Update_MC_ControlCode_Back();
- }
- }
- else
- {
- *GearSt = MC_GearSt_OFF;
- Update_MC_ControlCode_Back();
- }
- }
- }
- //运行总时间计算
- void MC_RunTime_Cal(uint32_t* p_Runtime)
- {
- static uint32_t PeriodTimeCnt = 0;
- static uint8_t Count = 0;
-
- if((HAL_GetTick()- PeriodTimeCnt) >= 60000)
- {
- PeriodTimeCnt = HAL_GetTick();
-
- (*p_Runtime)++;
- //存储运行总时间
- Count++;
- if(Count >= 5)
- {
- Count = 0;
- SaveParamToEEprom_24C02(&I2C_Handle_EEPROM, EEPROM_24C02_ADDR_RUN_LOG, sizeof(MC_RunLog_Struct_t), (uint8_t*)&MC_RunLog.PowerOnCnt);
- }
- }
- }
- //设备在线检测
- void MC_OnLineCheck(OnLine_Status_Struct_t* p_OnLineStatus, MC_ErrorCode_Struct_t* p_ErrorCode)
- {
- uint8_t SendTimeCnt;
- uint32_t PeriodTimeCnt;
-
- //发送指令查询PBU是否在线
- PeriodTimeCnt = HAL_GetTick();
- SendTimeCnt = 0;
- do
- {
- CAN_RxData_Process(&CAN_RxBuf_Struct_PBU, 500);
- if((HAL_GetTick() - PeriodTimeCnt) >= 50)
- {
- PeriodTimeCnt = HAL_GetTick();
- SendTimeCnt++;
-
- SendData(ID_MC_TO_PBU, MODE_READ, 0x5009, (uint8_t*)"HANDSHAKE");
- }
- //看门狗清零
- #if DEBUG
- HAL_IWDG_Refresh(&hiwdg);
- #endif
- }while((p_OnLineStatus->Status_Bit.PBU_OffLine == 1) && (SendTimeCnt <= 4));
- p_ErrorCode->ERROR_Bit.Fault_PBU_Check = p_OnLineStatus->Status_Bit.PBU_OffLine;
- //发送指令查询HMI是否在线
- if(PBU_ConfigParam.NoHMI_Flag == MC_SUPPORT_DISABLE)
- {
- PeriodTimeCnt = HAL_GetTick();
- SendTimeCnt = 0;
- do
- {
- CAN_RxData_Process(&CAN_RxBuf_Struct_HMI, 500);
- if((HAL_GetTick() - PeriodTimeCnt) >= 50)
- {
- PeriodTimeCnt = HAL_GetTick();
- SendTimeCnt++;
-
- SendData(ID_MC_TO_HMI, MODE_READ, 0x7009, (uint8_t*)"HANDSHAKE");
- }
- //看门狗清零
- #if DEBUG
- HAL_IWDG_Refresh(&hiwdg);
- #endif
- }while((p_OnLineStatus->Status_Bit.HMI_OffLine == 1) && (SendTimeCnt <= 4));
- }
- else
- {
- p_OnLineStatus->Status_Bit.HMI_OffLine = 0;
- }
- p_ErrorCode->ERROR_Bit.Fault_HMI_Check = p_OnLineStatus->Status_Bit.HMI_OffLine;
- //发送指令查询BMS是否在线
- PeriodTimeCnt = HAL_GetTick();
- SendTimeCnt = 0;
- do
- {
- CAN_RxData_Process_Temp(&CAN_RxBuf_Struct_BMS, 500);
- if((HAL_GetTick() - PeriodTimeCnt) >= 50)
- {
- PeriodTimeCnt = HAL_GetTick();
- SendTimeCnt++;
-
- SendData(ID_MC_TO_BMS, MODE_READ, 0x3009, (uint8_t*)"HANDSHAKE");
- }
- //看门狗清零
- #if DEBUG
- HAL_IWDG_Refresh(&hiwdg);
- #endif
- }while((p_OnLineStatus->Status_Bit.BMS_OffLine == 1) && (SendTimeCnt <= 4));
- p_ErrorCode->ERROR_Bit.Fault_BMS_Check = p_OnLineStatus->Status_Bit.BMS_OffLine;
- }
- //设备授权校验
- void MC_DeviceCheck(MC_ErrorCode_Struct_t* p_ErrorCode)
- {
- uint8_t T[16];
-
- CheckCodeCal(NULL, Secret_Key, T);
-
-
-
-
-
- }
- /**************************全局函数定义结束*****************/
|