canAppl.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /**
  2. * @file canAppl.c
  3. * @author Zhang, Kai(zhangkai71@midea.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2022-03-02
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #include "canAppl.h"
  12. #include "CodePara.h"
  13. #include "FuncLayerAPI.h"
  14. #include "adc.h"
  15. #include "alarm.h"
  16. #include "can.h"
  17. #include "flash_master.h"
  18. #include "glbcof.h"
  19. #include "spdctrmode.h"
  20. #include "string.h"
  21. #include "syspar.h"
  22. #include "AssistCurve.h"
  23. #include "hwsetup.h"
  24. #include "spi_master.h"
  25. /******************************
  26. *
  27. * Parameter
  28. *
  29. ******************************/
  30. MC_RunInfo_Struct_t MC_RunInfo;
  31. MC_ErrorCode_Struct_t MC_ErrorCode = CAN_MC_ALARMCODE_DEFAULT;
  32. MC_ErrorCode_CNT_RECORD_Struct_t MC_ErrorCntRecord = CAN_MC_ALARMCODE_CNT_RECORD_DEFAULT;
  33. MC_VerInfo_Struct_t MC_VerInfo;
  34. MC_MacInfo_Struct_t MC_MacInfo;
  35. MC_RideLog_Struct_t MC_RideLog;
  36. BMS_RunInfo_Struct_t BMS_RunInfo;
  37. MC_ControlCode_Struct_t MC_ControlCode;
  38. MC_RunInfoToCDL_Struct_t MC_RunInfoToCDL;
  39. MC_RS_ASCII_Struct_t MC_RsASSCII;
  40. MC_UpperPCInfo_Struct_t MC_UpcInfo;
  41. BMS_VoltEstimat_Struct_t BMS_VoltEstimat;
  42. OBC_SetCustomPara_Struct_t OBC_SetCustomPara;
  43. OBC_ButtonStatus_Struct_t OBC_ButtonStatus = {0,0,0};
  44. ULONG ulOBC_ComTimeOutCount = 0;
  45. uint8_t MC_MotorSPD_rpm_Percent = 0;
  46. uint8_t MC_WorkMode;
  47. uint8_t MC_BC_COM = 1;
  48. LPF_OUT BMS_swCurIdcLpf;
  49. void Can_voUpdateMC_UpcInfo(void)
  50. {
  51. MC_UpcInfo.stMotorInfo.uwPolePairs = cp_stMotorPara.swMotrPolePairs;
  52. MC_UpcInfo.stMotorInfo.uwRsmOhm = cp_stMotorPara.swRsOhm;
  53. MC_UpcInfo.stMotorInfo.uwLduH = cp_stMotorPara.swLdmH;
  54. MC_UpcInfo.stMotorInfo.uwLquH = cp_stMotorPara.swLqmH;
  55. MC_UpcInfo.stMotorInfo.uwFluxmWb = cp_stMotorPara.swFluxWb;
  56. MC_UpcInfo.stMotorInfo.uwIdMaxA = cp_stMotorPara.swIdMaxA;
  57. MC_UpcInfo.stMotorInfo.uwIdMinA = cp_stMotorPara.swIdMinA;
  58. MC_UpcInfo.stMotorInfo.uwRSpdRpm = cp_stMotorPara.swRSpeedRpm;
  59. MC_UpcInfo.stMotorInfo.uwRPwrWt = cp_stMotorPara.swRPwrWt;
  60. MC_UpcInfo.stMotorInfo.uwRCurA = cp_stMotorPara.swRIarmsA;
  61. MC_UpcInfo.stMotorInfo.uwRVolV = cp_stMotorPara.swRUdcV;
  62. MC_UpcInfo.stMotorInfo.uwJD = cp_stMotorPara.swJD;
  63. MC_UpcInfo.stMotorInfo.uwTorMaxNm = cp_stMotorPara.swTorMax;
  64. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = ass_stParaCong.uwWheelPerimeter;
  65. MC_UpcInfo.stBikeInfo.uwMechRationMotor = ass_stParaCong.uwMechRationMotor;
  66. MC_UpcInfo.stBikeInfo.uwAssistMaxSpdKmH = ass_stParaCong.uwAssistMaxSpdKmH;
  67. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = ass_stParaCong.uwThrottleMaxSpdKmH;
  68. MC_UpcInfo.stBikeInfo.uwNmFrontChainring = ass_stParaCong.uwNmFrontChainring;
  69. MC_UpcInfo.stBikeInfo.uwNmBackChainring = ass_stParaCong.uwNmBackChainring;
  70. MC_UpcInfo.stBikeInfo.uwAssistSelect1 = ass_stParaCong.uwAssistSelect1;
  71. MC_UpcInfo.stBikeInfo.uwAssistSelect2 = ass_stParaCong.uwAssistSelect2;
  72. MC_UpcInfo.stBikeInfo.uwLightVoltage = ass_stParaCong.uwLightVoltage;
  73. MC_UpcInfo.stBikeInfo.swWheelSizeAdjust = ass_stParaCong.swDeltPerimeter;
  74. MC_UpcInfo.stBikeInfo.uwStartMode = ass_stParaCong.uwStartMode;
  75. MC_UpcInfo.stBikeInfo.uwAutoPowerOffTime = ass_stParaCong.uwAutoPowerOffTime;
  76. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetOrigin = spi_stResolverOut.swSpiThetaOffsetOrignPu;
  77. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetNow = spi_stResolverOut.swSpiThetaOffsetPu;
  78. MC_UpcInfo.stMContorlInfo.uwIPeakMaxA = cp_stMotorPara.swIpeakMaxA;
  79. MC_UpcInfo.stMContorlInfo.uwAlamOCurA = cp_stControlPara.swAlmOverCurrentVal;
  80. MC_UpcInfo.stMContorlInfo.uwAlamOVolV = cp_stControlPara.swAlmOverVolVal3;
  81. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = cp_stControlPara.swAlmUnderVolVal2;
  82. MC_UpcInfo.stMContorlInfo.uwAlamOverSpdRpm = cp_stControlPara.swAlmOverSpdVal;
  83. MC_UpcInfo.stMContorlInfo.uwAlamOverHeatCe = cp_stControlPara.swAlmOverHeatCeVal;
  84. MC_UpcInfo.stMContorlInfo.uwAlamRecHeatCe = cp_stControlPara.swAlmRecOHeatVal;
  85. MC_UpcInfo.stMContorlInfo.uwPwrLimitStartCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
  86. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetOrigin = torsensor_stTorSensorCof.uwTorqueOffsetOrign;
  87. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow1 = torsensor_stTorSensorCof.uwTorqueOffsetNow1;
  88. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow2 = torsensor_stTorSensorCof.uwTorqueOffsetNow2;
  89. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow3 = torsensor_stTorSensorCof.uwTorqueOffsetNow3;
  90. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow4 = torsensor_stTorSensorCof.uwTorqueOffsetNow4;
  91. MC_UpcInfo.stSensorInfo.uwBikeTorMaxNm = torsensor_stTorSensorCof.uwMaxSensorTorquePu;
  92. MC_UpcInfo.stSensorInfo.uwBikeTor1StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep1RealNm;
  93. MC_UpcInfo.stSensorInfo.uwBikeTor1StepADC = torsensor_stTorSensorCof.uwBikeTorStep1ADC;
  94. MC_UpcInfo.stSensorInfo.uwBikeTor2StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep2RealNm;
  95. MC_UpcInfo.stSensorInfo.uwBikeTor2StepADC = torsensor_stTorSensorCof.uwBikeTorStep2ADC;
  96. MC_UpcInfo.stSensorInfo.uwBikeTor3StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep3RealNm;
  97. MC_UpcInfo.stSensorInfo.uwBikeTor3StepADC = torsensor_stTorSensorCof.uwBikeTorStep3ADC;
  98. MC_UpcInfo.stSensorInfo.uwBikeTor4StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep4RealNm;
  99. MC_UpcInfo.stSensorInfo.uwBikeTor4StepADC = torsensor_stTorSensorCof.uwBikeTorStep4ADC;
  100. MC_UpcInfo.stSensorInfo.uwCadSensorPulseNm = cadence_stFreGetCof.uwNumbersPulses;
  101. MC_UpcInfo.stSensorInfo.uwBikeSpdSensorPulseNm = bikespeed_stFreGetCof.uwNumbersPulses;
  102. MC_UpcInfo.stAssistInfo.swStartupGain = ass_stParaSet.uwStartupCoef;
  103. MC_UpcInfo.stAssistInfo.swStartcruiseGain = ass_stParaSet.uwStartupCruiseCoef;
  104. MC_UpcInfo.stAssistInfo.uwAssistStartNm = ass_stParaSet.uwAssistStartNm;
  105. MC_UpcInfo.stAssistInfo.uwAssistStopNm = ass_stParaSet.uwAssistStopNm;
  106. MC_UpcInfo.stAssistInfo.uwStartUpGainStep = ass_stParaSet.uwStartUpGainStep;
  107. MC_UpcInfo.stAssistInfo.uwStartUpCadNm = ass_stParaSet.uwStartUpCadNm;
  108. MC_UpcInfo.stAssistInfo.uwTorLPFCadNm = ass_stParaSet.uwTorLPFCadNm;
  109. MC_UpcInfo.stAssistInfo.uwSpeedAssistSpdRpm = ass_stParaSet.uwSpeedAssistSpdRpm;
  110. MC_UpcInfo.stAssistInfo.uwSpeedAssistIMaxA = ass_stParaSet.uwSpeedAssistIMaxA;
  111. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = ass_stParaSet.uwAssistLimitBikeSpdStart;
  112. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = ass_stParaSet.uwAssistLimitBikeSpdStop;
  113. MC_UpcInfo.stAssistInfo.uwCadenceAssistWeight = ass_stParaSet.uwCadenceWeight;
  114. MC_UpcInfo.stHistoryInfo.uwOpenTimes = cp_stHistoryPara.uwOpenTimes;
  115. MC_UpcInfo.stHistoryInfo.uwUsedTimeH = (UWORD)(cp_stHistoryPara.ulUsedTime >> 16);
  116. MC_UpcInfo.stHistoryInfo.uwUsedTimeL = (UWORD)(cp_stHistoryPara.ulUsedTime);
  117. MC_UpcInfo.stHistoryInfo.uwNTCTempMaxCe = cp_stHistoryPara.uwNTCTempMaxCe;
  118. MC_UpcInfo.stHistoryInfo.uwNTCTempMinCe = cp_stHistoryPara.uwNTCTempMinCe;
  119. ;
  120. MC_UpcInfo.stHistoryInfo.uwAlamHOcurTimes = cp_stHistoryPara.uwAlamHOcurTimes;
  121. MC_UpcInfo.stHistoryInfo.uwAlamSOcurTimes = cp_stHistoryPara.uwAlamSOcurTimes;
  122. MC_UpcInfo.stHistoryInfo.uwAlamOHeatTimes = cp_stHistoryPara.uwAlamOHeatTimes;
  123. MC_UpcInfo.stHistoryInfo.uwAlamRotorLockTimes = cp_stHistoryPara.uwAlamRotorLockTimes;
  124. MC_UpcInfo.stHistoryInfo.uwAlamPhsLossTimes = cp_stHistoryPara.uwAlamPhsLossTimes;
  125. MC_UpcInfo.stHistoryInfo.uwAlamOVolTimes = cp_stHistoryPara.uwAlamOVolTimes;
  126. MC_UpcInfo.stHistoryInfo.uwAlamUVolTimes = cp_stHistoryPara.uwAlamUVolTimes;
  127. MC_UpcInfo.stHistoryInfo.uwAlamComOTimeTimes = cp_stHistoryPara.uwAlamComOTimeTimes;
  128. MC_UpcInfo.stHistoryInfo.uwG1AvgPwrConsumption = cp_stHistoryPara.uwG1AvgPwrConsumption;
  129. MC_UpcInfo.stHistoryInfo.uwG2AvgPwrConsumption = cp_stHistoryPara.uwG2AvgPwrConsumption;
  130. MC_UpcInfo.stHistoryInfo.uwG3AvgPwrConsumption = cp_stHistoryPara.uwG3AvgPwrConsumption;
  131. MC_UpcInfo.stHistoryInfo.uwG4AvgPwrConsumption = cp_stHistoryPara.uwG4AvgPwrConsumption;
  132. MC_UpcInfo.stHistoryInfo.uwG5AvgPwrConsumption = cp_stHistoryPara.uwG5AvgPwrConsumption;
  133. MC_UpcInfo.stHistoryInfo.uwODOTripH = (UWORD)(cp_stHistoryPara.ulODOTrip >> 16);
  134. MC_UpcInfo.stHistoryInfo.uwODOTripL = (UWORD)cp_stHistoryPara.ulODOTrip;
  135. MC_UpcInfo.stHistoryInfo.uwODOTimeH = (UWORD)(cp_stHistoryPara.ulODOTime >> 16);
  136. MC_UpcInfo.stHistoryInfo.uwODOTimeL = (UWORD)cp_stHistoryPara.ulODOTime;
  137. MC_UpcInfo.stHistoryInfo.uwTripSumH = (UWORD)(cp_stHistoryPara.ulTripSum >> 16);
  138. MC_UpcInfo.stHistoryInfo.uwTripSumL = (UWORD)cp_stHistoryPara.ulTripSum;
  139. MC_UpcInfo.stHistoryInfo.uwTripSumTimeH = (UWORD)(cp_stHistoryPara.ulTripSumTime >> 16);
  140. MC_UpcInfo.stHistoryInfo.uwTripSumTimeL = (UWORD)cp_stHistoryPara.ulTripSumTime;
  141. MC_UpcInfo.stHistoryInfo.uwTorSensorAlamTimes = cp_stHistoryPara.uwTorSensorAlamTimes;
  142. MC_UpcInfo.stHistoryInfo.uwCadSensorAlamTimes = cp_stHistoryPara.uwCadSensorAlamTimes;
  143. MC_UpcInfo.stHistoryInfo.uwBikeSpdSensorAlamTimes = cp_stHistoryPara.uwBikeSpdSensorAlamTimes;
  144. MC_UpcInfo.stHistoryInfo.uwPosSensorAlamTimes = cp_stHistoryPara.uwPosSensorAlamTimes;
  145. MC_UpcInfo.stTestParaInfo.uwEEFirstDefaultSetFlg = cp_stFlg.ParaFirstSetFlg;
  146. MC_UpcInfo.stTestParaInfo.uwSPIOffsetFirstSetFlg = cp_stFlg.SpiOffsetFirstSetFlg;
  147. MC_UpcInfo.stTestParaInfo.RunModelSelect = cp_stFlg.RunModelSelect;
  148. MC_UpcInfo.stTestParaInfo.ThetaGetModelSelect = cp_stFlg.ThetaGetModelSelect;
  149. MC_UpcInfo.stTestParaInfo.CurrentSampleModelSelect = cp_stFlg.CurrentSampleModelSelect;
  150. MC_UpcInfo.stTestParaInfo.RotateDirectionSelect = cp_stFlg.RotateDirectionSelect;
  151. MC_UpcInfo.stTestParaInfo.uwInitPosCurAmp = cp_stControlPara.swAlignCurAp;
  152. MC_UpcInfo.stTestParaInfo.uwVFControlVolAmp = cp_stControlPara.swDragVolAp;
  153. MC_UpcInfo.stTestParaInfo.uwIFControlCurAmp = cp_stControlPara.swDragCurAp;
  154. MC_UpcInfo.stTestParaInfo.uwVFIFTargetFreHz = cp_stControlPara.swDragSpdHz;
  155. MC_UpcInfo.stTestParaInfo.uwSpeedLoopAccRate = 500;
  156. MC_UpcInfo.stTestParaInfo.uwSpeedLoopDecRate = 100;
  157. MC_UpcInfo.stTestParaInfo.uwSpeedLoopBandWidthHz = cp_stControlPara.swAsrPIBandwidth;
  158. MC_UpcInfo.stTestParaInfo.uwSpeedLoopCoefM = cp_stControlPara.swAsrPIM;
  159. MC_UpcInfo.stTestParaInfo.uwCuerrentLoopBandWidthHz = cp_stControlPara.swAcrPIBandwidth;
  160. MC_UpcInfo.stTestParaInfo.uwCurrentLoopCoefM = cp_stControlPara.swAcrRaCoef;
  161. MC_UpcInfo.stTestParaInfo.uwFluxObsBandWidthHz = cp_stControlPara.swObsFluxPICrossfreHz;
  162. MC_UpcInfo.stTestParaInfo.uwFluxObsCoefM = cp_stControlPara.swObsFluxPIDampratio;
  163. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLBandWidthHz = cp_stControlPara.swObsSpdPLLBandWidthHz;
  164. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLCoefM = cp_stControlPara.swObsSpdPLLM;
  165. MC_UpcInfo.stTestParaInfo.uwJm = cp_stMotorPara.swJD ;
  166. MC_UpcInfo.stTestParaInfo.uwPWMMaxDuty = cp_stControlPara.swPWMMaxDuty;
  167. MC_UpcInfo.stTestParaInfo.uwPWM7to5Duty = cp_stControlPara.swPWM7to5Duty;
  168. MC_UpcInfo.stTestParaInfo.uwPwrLimit = cp_stControlPara.swPwrLimitValWt;
  169. MC_UpcInfo.stTestParaInfo.uwPwrLimitError = cp_stControlPara.swPwrLimitErrWt;
  170. MC_UpcInfo.stTestParaInfo.uwPwrLimitKp = cp_stControlPara.swPwrLimitKpPu;
  171. MC_UpcInfo.stTestParaInfo.uwPwrLimitKi = cp_stControlPara.swPwrLimitKiPu;
  172. OBC_SetCustomPara.uwWheelPerimeter = ass_stParaCong.uwWheelPerimeter;
  173. OBC_SetCustomPara.StartUpMod = ass_stParaCong.uwStartMode;
  174. OBC_SetCustomPara.BikeSpeedLimit = ass_stParaCong.uwAssistMaxSpdKmH;
  175. OBC_SetCustomPara.DeltDiameter = ass_stParaCong.swDeltPerimeter;
  176. OBC_SetCustomPara.AssistMod = ass_stParaSet.uwAsssistSelectNum;
  177. OBC_SetCustomPara.AutoPowerOffTime = ass_stParaCong.uwAutoPowerOffTime;
  178. }
  179. UWORD SizeMCUP;
  180. void Can_voInitMC_Run(void)
  181. {
  182. flash_voRead();
  183. // Hardware version
  184. strncpy(MC_VerInfo.HW_Version, (char *)"TTKZ018..", 9);
  185. // Software version
  186. strncpy(MC_VerInfo.FW_Version, (char *)"V0r1r0_20221121.", 16);
  187. // Model name
  188. memcpy(MC_VerInfo.Mode, flash_stPara.ubMotorVersion, sizeof(flash_stPara.ubMotorVersion));
  189. // SN
  190. memcpy(MC_VerInfo.SN_Num, flash_stPara.ubSN, sizeof(flash_stPara.ubSN));
  191. // Product information
  192. memcpy(MC_MacInfo.Manufacturer, flash_stPara.ubProdInfo.Manufacturer, sizeof(flash_stPara.ubProdInfo));
  193. // User information
  194. memcpy(MC_RsASSCII.CustomASCII1, flash_stPara.ubRsASSCII.CustomASCII1, sizeof(MC_RsASSCII.CustomASCII1));
  195. memcpy(MC_RsASSCII.CustomASCII2, flash_stPara.ubRsASSCII.CustomASCII2, sizeof(MC_RsASSCII.CustomASCII2));
  196. memcpy(MC_RsASSCII.CustomASCII3, flash_stPara.ubRsASSCII.CustomASCII3, sizeof(MC_RsASSCII.CustomASCII3));
  197. BMS_VoltEstimat.uwInterResistpu = ((ULONG)1700 << 15) / cof_uwRbOm; //Q15 0.1mOhm BMS internal resistance 150mOhm + 20mOhm
  198. mth_voLPFilterCoef(1000000 / 100, EVENT_1MS_HZ, &BMS_swCurIdcLpf.uwKx); //100Hz
  199. }
  200. void Can_voMC_Run_1ms(void)
  201. {
  202. if (cp_stBikeRunInfoPara.BikeSpeedKmH > 30)
  203. {
  204. cp_stBikeRunInfoPara.ulRiTime++;
  205. cp_stHistoryPara.ulUsedTime++;
  206. }
  207. else
  208. {}
  209. if (adc_stUpOut.PCBTemp > ((SWORD)cp_stHistoryPara.uwNTCTempMaxCe))
  210. {
  211. cp_stHistoryPara.uwNTCTempMaxCe = adc_stUpOut.PCBTemp;
  212. }
  213. else
  214. {}
  215. if (adc_stUpOut.PCBTemp < ((SWORD)cp_stHistoryPara.uwNTCTempMaxCe))
  216. {
  217. cp_stHistoryPara.uwNTCTempMaxCe = adc_stUpOut.PCBTemp;
  218. }
  219. else
  220. {}
  221. // Error Cnt record and Error Display Set
  222. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms ++;
  223. if(MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms == ALAM_DETECT_CNT_IN_1ms && MC_ErrorCode.Code == 0)
  224. {
  225. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  226. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  227. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  228. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  229. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  230. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  231. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms = 0;
  232. }
  233. else
  234. {}
  235. if (alm_blAlmOccrFlg == TRUE)
  236. {
  237. if (alm_blAlmSingleRecordDoneFlg == FALSE)
  238. {
  239. if (alm_unCode.bit.IPMFlt == 1)
  240. {
  241. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  242. cp_stHistoryPara.uwAlamHOcurTimes++;
  243. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL)//ALAM_DISPLAY_CNT_2LEVEL
  244. {
  245. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  246. }
  247. else
  248. {}
  249. }
  250. if (alm_unCode.bit.OvrCur == 1)
  251. {
  252. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  253. cp_stHistoryPara.uwAlamSOcurTimes++;
  254. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL)//ALAM_DISPLAY_CNT_2LEVEL
  255. {
  256. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  257. }
  258. else
  259. {}
  260. }
  261. if (alm_unCode.bit.OvrVlt == 1)
  262. {
  263. MC_ErrorCntRecord.Protect_OverVoltageCnt++;
  264. cp_stHistoryPara.uwAlamOVolTimes++;
  265. if (MC_ErrorCntRecord.Protect_OverVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL)//ALAM_DISPLAY_CNT_2LEVEL
  266. {
  267. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 1;
  268. }
  269. }
  270. if (alm_unCode.bit.UndrVlt == 1)
  271. {
  272. MC_ErrorCntRecord.Protect_UnderVoltageCnt++;
  273. cp_stHistoryPara.uwAlamUVolTimes++;
  274. if (MC_ErrorCntRecord.Protect_UnderVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL)//ALAM_DISPLAY_CNT_2LEVEL
  275. {
  276. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 1;
  277. }
  278. }
  279. if (alm_unCode.bit.RotorLock == 1)
  280. {
  281. MC_ErrorCntRecord.Protect_LockRotorCnt++;
  282. cp_stHistoryPara.uwAlamRotorLockTimes++;
  283. if (MC_ErrorCntRecord.Protect_LockRotorCnt ==ALAM_DISPLAY_CNT_0LEVEL)//ALAM_DISPLAY_CNT_2LEVEL
  284. {
  285. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 1;
  286. }
  287. }
  288. if (alm_unCode.bit.IPMOvrHeat == 1)
  289. {
  290. MC_ErrorCntRecord.Protect_OverTempCnt++;
  291. cp_stHistoryPara.uwAlamOHeatTimes++;
  292. if (MC_ErrorCntRecord.Protect_OverTempCnt == ALAM_DISPLAY_CNT_0LEVEL)
  293. {
  294. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 1;
  295. }
  296. }
  297. if (alm_unCode.bit.PhsLoss == 1)
  298. {
  299. MC_ErrorCntRecord.Fault_PhaseLineCnt++;
  300. cp_stHistoryPara.uwAlamPhsLossTimes++;
  301. if (MC_ErrorCntRecord.Fault_PhaseLineCnt == ALAM_DISPLAY_CNT_0LEVEL)
  302. {
  303. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 1;
  304. }
  305. }
  306. // if(alm_unCode.bit.CommOvrTm)
  307. // {
  308. // cp_stHistoryPara.uwAlamComOTimeTimes++;
  309. // }
  310. if (alm_unCode.bit.SpiThetaFlt == 1)
  311. {
  312. MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 1;
  313. }
  314. if (alm_unBikeCode.bit.BikeSpdSen == 1)
  315. {
  316. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 1;
  317. }
  318. if (alm_unBikeCode.bit.CadenceSen == 1)
  319. {
  320. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 1;
  321. }
  322. if (alm_unBikeCode.bit.PCBNTC == 1)
  323. {
  324. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 1;
  325. }
  326. if (alm_unBikeCode.bit.Throttle == 1)
  327. {
  328. MC_ErrorCode.ERROR_Bit.Fault_Throttle = 1;
  329. }
  330. if (alm_unBikeCode.bit.TorqSen == 1)
  331. {
  332. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 1;
  333. }
  334. alm_blAlmSingleRecordDoneFlg = TRUE;
  335. }
  336. }
  337. else
  338. {}
  339. // Claear error Display
  340. if (MC_ErrorCode.ERROR_Bit.Protect_OverCurrent == 1)
  341. {
  342. if (alm_unCode.bit.IPMFlt != 1 && alm_unCode.bit.OvrCur != 1)
  343. {
  344. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  345. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 0;
  346. }
  347. }
  348. else
  349. {}
  350. if (MC_ErrorCode.ERROR_Bit.Protect_OverVoltage == 1)
  351. {
  352. if (alm_unCode.bit.OvrVlt != 1)
  353. {
  354. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  355. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 0;
  356. }
  357. }
  358. else
  359. {}
  360. if (MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage == 1)
  361. {
  362. if (alm_unCode.bit.UndrVlt != 1)
  363. {
  364. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  365. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 0;
  366. }
  367. }
  368. else
  369. {}
  370. if (MC_ErrorCode.ERROR_Bit.Protect_LockRotor == 1)
  371. {
  372. if (alm_unCode.bit.RotorLock != 1)
  373. {
  374. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  375. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 0;
  376. }
  377. }
  378. else
  379. {}
  380. if (MC_ErrorCode.ERROR_Bit.Protect_OverTemp == 1)
  381. {
  382. if (alm_unCode.bit.IPMOvrHeat != 1)
  383. {
  384. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  385. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 0;
  386. }
  387. }
  388. else
  389. {}
  390. if (MC_ErrorCode.ERROR_Bit.Fault_PhaseLine == 1)
  391. {
  392. if (alm_unCode.bit.PhsLoss != 1)
  393. {
  394. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  395. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 0;
  396. }
  397. }
  398. else
  399. {}
  400. if(MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor == 1)
  401. {
  402. if (alm_unCode.bit.SpiThetaFlt != 1)
  403. {
  404. MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 0;
  405. }
  406. }
  407. if(MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor == 1)
  408. {
  409. if (alm_unBikeCode.bit.BikeSpdSen != 1)
  410. {
  411. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 0;
  412. }
  413. }
  414. if(MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor == 1)
  415. {
  416. if (alm_unBikeCode.bit.CadenceSen != 1)
  417. {
  418. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 0;
  419. }
  420. }
  421. if(MC_ErrorCode.ERROR_Bit.Fault_PCBNTC == 1)
  422. {
  423. if (alm_unBikeCode.bit.PCBNTC != 1)
  424. {
  425. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 0;
  426. }
  427. }
  428. if(MC_ErrorCode.ERROR_Bit.Fault_Throttle == 1)
  429. {
  430. if (alm_unBikeCode.bit.Throttle != 1)
  431. {
  432. MC_ErrorCode.ERROR_Bit.Fault_Throttle = 0;
  433. }
  434. }
  435. if(MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor == 1)
  436. {
  437. if (alm_unBikeCode.bit.TorqSen != 1)
  438. {
  439. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 0;
  440. }
  441. }
  442. BMS_VoltEstimat.swIdcPu = (((ULONG)scm_stMotoPwrInLpf.slY.sw.hi << 13) / adc_stUpOut.uwVdcLpfPu); //Q15+Q13-Q14=Q14 Calculated dc current
  443. if(BMS_VoltEstimat.swIdcPu < 0)
  444. {
  445. BMS_VoltEstimat.swIdcPu = 0;
  446. }
  447. mth_voLPFilter(BMS_VoltEstimat.swIdcPu, &BMS_swCurIdcLpf);
  448. BMS_VoltEstimat.uwVdcCompPu = (ULONG)BMS_swCurIdcLpf.slY.sw.hi * BMS_VoltEstimat.uwInterResistpu >> 15; //Q14+Q15-Q15=Q14
  449. }
  450. void Can_voMC_Run_5ms(void)
  451. {
  452. cp_stBikeRunInfoPara.BikeSpeedKmH =
  453. (((SQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * FBASE * (ass_stParaCong.uwWheelPerimeter + ass_stParaCong.swDeltPerimeter) * 36 >> 20) * 1048 * 10) >>
  454. 20; // 1048 = Q20(1/1000) 0.1 km/h
  455. // MC_RunInfo.BusVoltage = ((SLONG)adc_stUpOut.uwVdcPu * cof_uwUbVt >> 14) * 100;
  456. MC_RunInfo.BusVoltage = ((ULONG)(adc_stUpOut.uwVdcLpfPu + BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14;
  457. MC_RunInfo.MotorSpeed = (SLONG)scm_uwSpdFbkLpfAbsPu * cof_uwVbRpm >> 15;
  458. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH;//MC_RunInfo.MotorSpeed / 10;
  459. }
  460. void Can_voMC_Run_200ms(void)
  461. {
  462. UWORD TempPower;
  463. Can_GearSt_switch();
  464. SizeMCUP = sizeof(MC_UpcInfo.stAssistInfo);
  465. if (MC_BC_COM == 0)
  466. {
  467. MC_RunInfoToCDL.Torque = ((ULONG)ass_stTorqMafValue.swValue * TORQUEBASE / 10) >> 14; // 0.1 Nm
  468. MC_RunInfoToCDL.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir; // 踩踏方向 0-正,1-反,2-停止,
  469. MC_RunInfoToCDL.Cadence = (cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwVbRpm) >> 20; // rpm
  470. MC_RunInfoToCDL.GearSt = MC_ControlCode.GearSt;
  471. MC_RunInfoToCDL.T_PCB = adc_stUpOut.PCBTemp + 40;
  472. MC_RunInfoToCDL.T_Coil = adc_stUpOut.PCBTemp + 40;
  473. MC_RunInfoToCDL.BusVoltage = ((ULONG)adc_stUpOut.uwVdcLpfPu * cof_uwUbVt * 100) >> 14; // 1mV
  474. MC_RunInfoToCDL.BusCurrent = (((ULONG)scm_stMotoPwrInLpf.slY.sw.hi << 13) / adc_stUpOut.uwVdcLpfPu * cof_uwIbAp * 100) >> 14; // 1mA
  475. MC_RunInfoToCDL.MotorSpeed = (SLONG)scm_stSpdFbkLpf.slY.sw.hi * cof_uwVbRpm >> 15;
  476. // MC_RunInfoToCDL.BikeSpeed =
  477. // (((SQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * ass_stParaCong.uwWheelPerimeter * 3216 * 36 >> 15) * 1050) >>
  478. // 35; // 600 0.1cm 3216 = Q10(3.1415926) 105 = Q20(1/10000) 0.1 km/h;
  479. //
  480. MC_RunInfoToCDL.BikeSpeed =
  481. (((SQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * FBASE * ass_stParaCong.uwWheelPerimeter * 36 >> 20) * 1048 * 10) >> 20;
  482. MC_RunInfoToCDL.IqCurrent = ((SLONG)scm_swIqRefPu * cof_uwIbAp * 100) >> 14;
  483. MC_RunInfoToCDL.AlarmCode = MC_ErrorCode.Code && 0x00FF;
  484. SendData(ID_MC_TO_CDL, MODE_REPORT, 0xF112, (uint8_t *)&MC_RunInfoToCDL.Torque);
  485. }
  486. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH; ///>车速 0.1km/h,地址偏移0
  487. MC_RunInfo.MotorSpeed = (SLONG)scm_uwSpdFbkLpfAbsPu * cof_uwVbRpm >> 15; ///>输出转速 1rpm,地址偏移2
  488. if(scm_swMotorPwrInLpfWt > 5000)
  489. {
  490. scm_swMotorPwrInLpfWt = 5000;
  491. }
  492. if(scm_swMotorPwrInLpfWt < 0 )
  493. {
  494. scm_swMotorPwrInLpfWt = 0;
  495. }
  496. if(hw_blPWMOnFlg == FALSE)
  497. {
  498. scm_swMotorPwrInLpfWt = 0;
  499. }
  500. else
  501. {
  502. }
  503. MC_RunInfo.Power = scm_swMotorPwrInLpfWt / 10; ///>电功率 1W,地址偏移4
  504. MC_RunInfo.BusVoltage = ((ULONG)(adc_stUpOut.uwVdcLpfPu + BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14; ///>???? 1mV?????6
  505. if (scm_stMotoPwrInLpf.slY.sw.hi < 10)
  506. {
  507. TempPower = 0;
  508. }
  509. else
  510. {
  511. TempPower = scm_stMotoPwrInLpf.slY.sw.hi;
  512. }
  513. MC_RunInfo.BusCurrent = (((ULONG)TempPower << 13) / adc_stUpOut.uwVdcLpfPu) * cof_uwIbAp * 100 >> 14; ///>母线电流 1mA,地址偏移8
  514. MC_RunInfo.Cadence =(cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * 60) >> 20; ///>踏频 1rpm,地址偏移10
  515. MC_RunInfo.Torque = ((ULONG)torsensor_stTorSensorOut.uwTorqueLPFPu * cof_uwTorqNm / 10) >> 14; ///>踩踏力矩 1Nm,地址偏移11
  516. MC_RunInfo.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir; ///>踩踏方向 0-正,1-反,2-停止,地址偏移12
  517. MC_RunInfo.GearSt = MC_ControlCode.GearSt; ///>助力档位,地址偏移13
  518. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; ///>灯开关 0xF0-关,0xF1-开,地址偏移14
  519. Can_SOC_Cal(); ///>剩余电量 1%,地址偏移15
  520. MC_RunInfo.ODO_Km = MC_RideLog.ODO_Km / 10; ///>总里程 1km,地址偏移18
  521. Can_Trip_Cal();
  522. if (cp_stBikeRunInfoPara.blGearStUpdate)
  523. {
  524. MC_RunInfo.PowerPerKm =
  525. *(&cp_stHistoryPara.uwG1AvgPwrConsumption + (cp_stBikeRunInfoPara.uwBikeGear - 1)) / 10; ///>平均功耗 0.01Ah/km ,地址偏移20
  526. cp_stBikeRunInfoPara.BMSRestChargeLast = BMS_RunInfo.RC;
  527. cp_stBikeRunInfoPara.uwPowerPerKm = 0;
  528. cp_stBikeRunInfoPara.uwAvePowerPerKm = 0;
  529. cp_stBikeRunInfoPara.uwPowerPerKmSum = 0;
  530. memset(cp_stBikeRunInfoPara.uwPowerPerKmBuf, 0, sizeof(cp_stBikeRunInfoPara.uwPowerPerKmBuf));
  531. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  532. cp_stBikeRunInfoPara.uwCruisDis = 0;
  533. }
  534. MC_RunInfo.T_PCB = adc_stUpOut.PCBTemp + 40; ///>PCB温度 +40℃,地址偏移21
  535. MC_RunInfo.T_Coil = adc_stUpOut.PCBTemp + 40; ///>绕组温度 +40℃,地址偏移22
  536. MC_RunInfo.T_MCU = adc_stUpOut.PCBTemp + 40; ///>MCU温度 +40℃,地址偏移23
  537. MC_RunInfo.Ride_Time = cp_stBikeRunInfoPara.ulRiTime >> 10; ///>开机后骑行时间 1s,地址偏移26
  538. if (MC_WorkMode == 1)
  539. {
  540. SendData(ID_MC_BC, MODE_REPORT, 0x1020, (uint8_t *)&MC_RunInfo.BikeSpeed);
  541. }
  542. }
  543. void Can_Trip_Cal(void)
  544. {
  545. if (bikespeed_stFreGetOut.blUpdateTripCntFlg == TRUE)
  546. {
  547. MC_RunInfo.Ride_Km++;
  548. cp_stHistoryPara.ulODOTrip++; // Unit: 0.1km
  549. cp_stHistoryPara.ulTripSum++;
  550. Can_RemainTrip_Cal();
  551. bikespeed_stFreGetOut.blUpdateTripCntFlg = FALSE;
  552. }
  553. MC_RideLog.ODO_Km = cp_stHistoryPara.ulODOTrip;
  554. MC_RideLog.TRIP_Km = cp_stHistoryPara.ulTripSum;
  555. MC_RideLog.ODO_Time = cp_stHistoryPara.ulODOTime + (cp_stBikeRunInfoPara.ulRiTime >> 10) / 60; // min
  556. MC_RideLog.TRIP_Time = cp_stHistoryPara.ulTripSumTime + (cp_stBikeRunInfoPara.ulRiTime >> 10) / 60; // min
  557. }
  558. void Can_RemainTrip_Cal(void)
  559. {
  560. UWORD uwCruisCoef; //ϵQ12
  561. cp_stBikeRunInfoPara.uwCruisDis++; // 0.1 km
  562. if (cp_stBikeRunInfoPara.uwCruisDis >= 5) // 5 = 0.5km
  563. {
  564. cp_stBikeRunInfoPara.uwCruisDis = 0;
  565. cp_stBikeRunInfoPara.BMSRestChargeNow = BMS_RunInfo.RC;
  566. cp_stBikeRunInfoPara.uwPowerPerKm = (cp_stBikeRunInfoPara.BMSRestChargeLast - cp_stBikeRunInfoPara.BMSRestChargeNow)
  567. << 1; // BMS_RunInfo.RC (BMSRestChargeLast - BMSRestChargeNow) /0.5 km
  568. cp_stBikeRunInfoPara.BMSRestChargeLast = cp_stBikeRunInfoPara.BMSRestChargeNow;
  569. cp_stBikeRunInfoPara.uwPowerPerKmSum -= cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  570. cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT] = cp_stBikeRunInfoPara.uwPowerPerKm;
  571. cp_stBikeRunInfoPara.uwPowerPerKmSum += cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  572. if (cp_stBikeRunInfoPara.uwPowerPerKmBuf[31] == 0)
  573. {
  574. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum / (cp_stBikeRunInfoPara.uwAvePowerCNT + 1);
  575. }
  576. else
  577. {
  578. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum >> 5;
  579. }
  580. cp_stBikeRunInfoPara.uwAvePowerCNT++;
  581. if (cp_stBikeRunInfoPara.uwAvePowerCNT >= 32)
  582. {
  583. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  584. }
  585. if (cp_stBikeRunInfoPara.uwBikeGear == 1)
  586. {
  587. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG1AvgPwrConsumption;
  588. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  589. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  590. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  591. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  592. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  593. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG1AvgPwrConsumption / 10;
  594. }
  595. else if (cp_stBikeRunInfoPara.uwBikeGear == 2)
  596. {
  597. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG2AvgPwrConsumption;
  598. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  599. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  600. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  601. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  602. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  603. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG2AvgPwrConsumption / 10;
  604. }
  605. else if (cp_stBikeRunInfoPara.uwBikeGear == 3)
  606. {
  607. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG3AvgPwrConsumption;
  608. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  609. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  610. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  611. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  612. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  613. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG3AvgPwrConsumption / 10;
  614. }
  615. else if (cp_stBikeRunInfoPara.uwBikeGear == 4)
  616. {
  617. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG4AvgPwrConsumption;
  618. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  619. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  620. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  621. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  622. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  623. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG4AvgPwrConsumption / 10;
  624. }
  625. else if (cp_stBikeRunInfoPara.uwBikeGear == 5)
  626. {
  627. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG5AvgPwrConsumption;
  628. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  629. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  630. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  631. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  632. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  633. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG5AvgPwrConsumption / 10;
  634. }
  635. else
  636. {
  637. uwCruisCoef = 4096;
  638. }
  639. MC_RunInfo.RemainDistance = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm;
  640. }
  641. }
  642. SLONG slBMSMaxVol = 42000;
  643. SLONG slBMSMinVol = 32000;
  644. SWORD SOC_Cnt, SOC_Value, SOC_MIN;
  645. SLONG SOC_Sum;
  646. BOOL blSOCfirstSet = FALSE;
  647. void Can_SOC_Cal(void)
  648. {
  649. UWORD templenght;
  650. if (blSOCfirstSet == FALSE)
  651. {
  652. SOC_Value = ((SLONG)MC_RunInfo.BusVoltage - (SLONG)slBMSMinVol) * 100 / ((SLONG)slBMSMaxVol - (SLONG)slBMSMinVol);
  653. SOC_MIN = SOC_Value;
  654. if (SOC_MIN > 100)
  655. {
  656. SOC_MIN = 100;
  657. }
  658. blSOCfirstSet = TRUE;
  659. }
  660. else
  661. {
  662. templenght = 60000 / 200; // 60s in 200ms time task
  663. SOC_Cnt++;
  664. SOC_Sum += ((SLONG)MC_RunInfo.BusVoltage - (SLONG)slBMSMinVol) * 100 / ((SLONG)slBMSMaxVol - (SLONG)slBMSMinVol);
  665. if (SOC_Cnt >= templenght) // 60s
  666. {
  667. SOC_Value = SOC_Sum / templenght;
  668. if (SOC_Value < SOC_MIN)
  669. {
  670. SOC_MIN = SOC_Value;
  671. }
  672. else
  673. {}
  674. SOC_Cnt = 0;
  675. SOC_Sum = 0;
  676. }
  677. }
  678. if (SOC_Value > SOC_MIN)
  679. {
  680. SOC_Value = SOC_MIN;
  681. }
  682. else if (SOC_Value < 0)
  683. {
  684. SOC_Value = 0;
  685. }
  686. else
  687. {}
  688. MC_RunInfo.SOC = SOC_Value;
  689. }
  690. void Can_GearSt_switch(void)
  691. {
  692. cp_stBikeRunInfoPara.uwBikeGearLast = cp_stBikeRunInfoPara.uwBikeGear;
  693. if(MC_WorkMode == 1)//ģʽԶر
  694. {
  695. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  696. }
  697. if((cp_ulSystickCnt - ulOBC_ComTimeOutCount) < 3000) //DZͨжϳ3sر
  698. {
  699. if (MC_ControlCode.GearSt <= 0x05)
  700. {
  701. cp_stBikeRunInfoPara.uwBikeGear = MC_ControlCode.GearSt;
  702. }
  703. else if (MC_ControlCode.GearSt == 0x33)
  704. {
  705. cp_stBikeRunInfoPara.uwBikeGear = 5;
  706. }
  707. else if (MC_ControlCode.GearSt == 0x22)
  708. {
  709. cp_stBikeRunInfoPara.uwBikeGear = 0x22;
  710. }
  711. }
  712. else
  713. {
  714. MC_ControlCode.GearSt = MC_GearSt_OFF;
  715. MC_RunInfo.GearSt = 0x00;
  716. cp_stBikeRunInfoPara.uwBikeGear = 0x00;
  717. }
  718. if (cp_stBikeRunInfoPara.uwBikeGear != cp_stBikeRunInfoPara.uwBikeGearLast)
  719. {
  720. cp_stBikeRunInfoPara.blGearStUpdate = TRUE;
  721. }
  722. else
  723. {
  724. cp_stBikeRunInfoPara.blGearStUpdate = FALSE;
  725. }
  726. }
  727. void Can_Light_switch(void)
  728. {
  729. /* light switch*/
  730. if (MC_ControlCode.LightSwitch == 0xF1)
  731. {
  732. cp_stBikeRunInfoPara.uwLightSwitch = 1;
  733. }
  734. else if (MC_ControlCode.LightSwitch == 0xF0)
  735. {
  736. cp_stBikeRunInfoPara.uwLightSwitch = 0;
  737. }
  738. }
  739. void Can_AssistCoef_Read(UPC_CurveOrderInfo_Struct_t *order)
  740. {
  741. MC_UpcInfo.stAssistInfo.stCurveOrderInfo = *order;
  742. memcpy(&MC_UpcInfo.stAssistInfo.slTorquePolyA, &flash_stPara.slTorqAssGain[order->swTorqueCurveNum - 1], sizeof(POLY_COEF));
  743. memcpy(&MC_UpcInfo.stAssistInfo.slCadencePolyA, &flash_stPara.slCadAssGain[order->swCadenceCurveNum - 1], sizeof(POLY_COEF));
  744. }
  745. void Can_AssistCoef_Write(UPC_CurveOrderInfo_Struct_t *order)
  746. {
  747. memcpy(&flash_stPara.slTorqAssGain[order->swTorqueCurveNum - 1], &MC_UpcInfo.stAssistInfo.slTorquePolyA, sizeof(POLY_COEF));
  748. memcpy(&flash_stPara.slCadAssGain[order->swCadenceCurveNum - 1], &MC_UpcInfo.stAssistInfo.slCadencePolyA, sizeof(POLY_COEF));
  749. }