canAppl.c 39 KB

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