canAppl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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_1st.h"
  26. #include "FSM_2nd.h"
  27. #include "sys_ctrl.h"
  28. #include "string.h"
  29. //#include "hwsetup.h"
  30. #include "fp.def"
  31. /******************************
  32. *
  33. * Parameter
  34. *
  35. ******************************/
  36. MC_RunInfo_Struct_t MC_RunInfo;
  37. MC_ErrorCode_Struct_t MC_ErrorCode = CAN_MC_ALARMCODE_DEFAULT;
  38. MC_ErrorCode_CNT_RECORD_Struct_t MC_ErrorCntRecord = CAN_MC_ALARMCODE_CNT_RECORD_DEFAULT;
  39. MC_VerInfo_Struct_t MC_VerInfo;
  40. char Firmware_Special[32];
  41. MC_MacInfo_Struct_t MC_MacInfo;
  42. MC_RideLog_Struct_t MC_RideLog;
  43. BMS_RunInfo_Struct_t BMS_RunInfo;
  44. MC_ControlCode_Struct_t MC_ControlCode;
  45. MC_RunInfoToCDL_Struct_t MC_RunInfoToCDL;
  46. MC_RS_ASCII_Struct_t MC_RsASSCII;
  47. MC_UpperPCInfo_Struct_t MC_UpcInfo;
  48. BMS_VoltEstimat_Struct_t BMS_VoltEstimat;
  49. OBC_SetCustomPara_Struct_t OBC_SetCustomPara;
  50. OBC_ButtonStatus_Struct_t OBC_ButtonStatus = {0, 0, 0};
  51. ULONG ulOBC_ComTimeOutCount = 0;
  52. ULONG ulBMS_ComTimeOutCount = 0;
  53. UBYTE MC_MotorSPD_rpm_Percent = 0;
  54. UBYTE MC_WorkMode;
  55. UBYTE MC_BC_COM = 0;
  56. static LPF_OUT BMS_swCurIdcLpf;
  57. static BOOL blBMSCommFault = FALSE;
  58. void Can_voUpdateMC_UpcInfo(void)
  59. {
  60. MC_UpcInfo.stMotorInfo.uwPolePairs = cp_stMotorPara.swMotrPolePairs;
  61. MC_UpcInfo.stMotorInfo.uwRsmOhm = cp_stMotorPara.swRsOhm;
  62. MC_UpcInfo.stMotorInfo.uwLduH = cp_stMotorPara.swLdmH;
  63. MC_UpcInfo.stMotorInfo.uwLquH = cp_stMotorPara.swLqmH;
  64. MC_UpcInfo.stMotorInfo.uwFluxmWb = cp_stMotorPara.swFluxWb;
  65. MC_UpcInfo.stMotorInfo.uwIdMaxA = cp_stMotorPara.swIdMaxA;
  66. MC_UpcInfo.stMotorInfo.uwIdMinA = cp_stMotorPara.swIdMinA;
  67. MC_UpcInfo.stMotorInfo.uwRSpdRpm = cp_stMotorPara.swRSpeedRpm;
  68. MC_UpcInfo.stMotorInfo.uwRPwrWt = cp_stMotorPara.swRPwrWt;
  69. MC_UpcInfo.stMotorInfo.uwRCurA = cp_stMotorPara.swRIarmsA;
  70. MC_UpcInfo.stMotorInfo.uwRVolV = cp_stMotorPara.swRUdcV;
  71. MC_UpcInfo.stMotorInfo.uwJD = cp_stMotorPara.swJD;
  72. MC_UpcInfo.stMotorInfo.uwTorMaxNm = cp_stMotorPara.swTorMax;
  73. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = ass_stParaCong.uwWheelPerimeter;
  74. MC_UpcInfo.stBikeInfo.uwMechRationMotor = ass_stParaCong.uwMechRationMotor;
  75. MC_UpcInfo.stBikeInfo.uwAssistMaxSpdKmH = ass_stParaCong.uwAssistMaxSpdKmH;
  76. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = ass_stParaCong.uwThrottleMaxSpdKmH;
  77. MC_UpcInfo.stBikeInfo.uwNmFrontChainring = ass_stParaCong.uwNmFrontChainring;
  78. MC_UpcInfo.stBikeInfo.uwNmBackChainring = ass_stParaCong.uwNmBackChainring;
  79. MC_UpcInfo.stBikeInfo.uwAssistSelect1 = ass_stParaCong.uwAssistSelect1;
  80. MC_UpcInfo.stBikeInfo.uwAssistSelect2 = ass_stParaCong.uwAssistSelect2;
  81. MC_UpcInfo.stBikeInfo.uwLightVoltage = ass_stParaCong.uwLightVoltage;
  82. MC_UpcInfo.stBikeInfo.swWheelSizeAdjust = ass_stParaCong.swDeltPerimeter;
  83. MC_UpcInfo.stBikeInfo.uwStartMode = ass_stParaCong.uwStartMode;
  84. MC_UpcInfo.stBikeInfo.uwAutoPowerOffTime = ass_stParaCong.uwAutoPowerOffTime;
  85. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetOrigin = spi_stResolverOut.swSpiThetaOffsetOrignPu;
  86. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetNow = spi_stResolverOut.swSpiThetaOffsetPu;
  87. MC_UpcInfo.stMContorlInfo.uwIPeakMaxA = cp_stMotorPara.swIpeakMaxA;
  88. MC_UpcInfo.stMContorlInfo.uwAlamOCurA = cp_stControlPara.swAlmOverCurrentVal;
  89. MC_UpcInfo.stMContorlInfo.uwAlamOVolV = cp_stControlPara.swAlmOverVolVal3;
  90. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = cp_stControlPara.swAlmUnderVolVal2;
  91. MC_UpcInfo.stMContorlInfo.uwAlamOverSpdRpm = cp_stControlPara.swAlmOverSpdVal;
  92. MC_UpcInfo.stMContorlInfo.uwAlamOverHeatCe = cp_stControlPara.swAlmOverHeatCeVal;
  93. MC_UpcInfo.stMContorlInfo.uwAlamRecHeatCe = cp_stControlPara.swAlmRecOHeatVal;
  94. MC_UpcInfo.stMContorlInfo.uwPwrLimitStartCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
  95. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetOrigin = torsensor_stTorSensorCof.uwTorqueOffsetOrign;
  96. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow1 = torsensor_stTorSensorCof.uwTorqueOffsetNow1;
  97. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow2 = torsensor_stTorSensorCof.uwTorqueOffsetNow2;
  98. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow3 = torsensor_stTorSensorCof.uwTorqueOffsetNow3;
  99. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow4 = torsensor_stTorSensorCof.uwTorqueOffsetNow4;
  100. MC_UpcInfo.stSensorInfo.uwBikeTorMaxNm = torsensor_stTorSensorCof.uwMaxSensorTorquePu;
  101. MC_UpcInfo.stSensorInfo.uwBikeTor1StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep1RealNm;
  102. MC_UpcInfo.stSensorInfo.uwBikeTor1StepADC = torsensor_stTorSensorCof.uwBikeTorStep1ADC;
  103. MC_UpcInfo.stSensorInfo.uwBikeTor2StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep2RealNm;
  104. MC_UpcInfo.stSensorInfo.uwBikeTor2StepADC = torsensor_stTorSensorCof.uwBikeTorStep2ADC;
  105. MC_UpcInfo.stSensorInfo.uwBikeTor3StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep3RealNm;
  106. MC_UpcInfo.stSensorInfo.uwBikeTor3StepADC = torsensor_stTorSensorCof.uwBikeTorStep3ADC;
  107. MC_UpcInfo.stSensorInfo.uwBikeTor4StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep4RealNm;
  108. MC_UpcInfo.stSensorInfo.uwBikeTor4StepADC = torsensor_stTorSensorCof.uwBikeTorStep4ADC;
  109. MC_UpcInfo.stSensorInfo.uwCadSensorPulseNm = cadence_stFreGetCof.uwNumbersPulses;
  110. MC_UpcInfo.stSensorInfo.uwBikeSpdSensorPulseNm = bikespeed_stFreGetCof.uwNumbersPulses;
  111. MC_UpcInfo.stAssistInfo.swStartupGain = ass_stParaSet.uwStartupCoef;
  112. MC_UpcInfo.stAssistInfo.swStartcruiseGain = ass_stParaSet.uwStartupCruiseCoef;
  113. MC_UpcInfo.stAssistInfo.uwAssistStartNm = ass_stParaSet.uwAssistStartNm;
  114. MC_UpcInfo.stAssistInfo.uwAssistStopNm = ass_stParaSet.uwAssistStopNm;
  115. MC_UpcInfo.stAssistInfo.uwStartUpGainStep = ass_stParaSet.uwStartUpGainStep;
  116. MC_UpcInfo.stAssistInfo.uwStartUpCadNm = ass_stParaSet.uwStartUpCadNm;
  117. MC_UpcInfo.stAssistInfo.uwTorLPFCadNm = ass_stParaSet.uwTorLPFCadNm;
  118. MC_UpcInfo.stAssistInfo.uwSpeedAssistSpdRpm = ass_stParaSet.uwSpeedAssistSpdRpm;
  119. MC_UpcInfo.stAssistInfo.uwSpeedAssistIMaxA = ass_stParaSet.uwSpeedAssistIMaxA;
  120. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = ass_stParaSet.uwAssistLimitBikeSpdStart;
  121. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = ass_stParaSet.uwAssistLimitBikeSpdStop;
  122. MC_UpcInfo.stAssistInfo.uwCadenceAssistWeight = ass_stParaSet.uwCadenceWeight;
  123. MC_UpcInfo.stHistoryInfo.uwOpenTimes = cp_stHistoryPara.uwOpenTimes;
  124. MC_UpcInfo.stHistoryInfo.uwUsedTimeH = (UWORD)(cp_stHistoryPara.ulUsedTime >> 16);
  125. MC_UpcInfo.stHistoryInfo.uwUsedTimeL = (UWORD)(cp_stHistoryPara.ulUsedTime);
  126. MC_UpcInfo.stHistoryInfo.uwNTCTempMaxCe = cp_stHistoryPara.uwNTCTempMaxCe;
  127. MC_UpcInfo.stHistoryInfo.uwNTCTempMinCe = cp_stHistoryPara.uwNTCTempMinCe;
  128. ;
  129. MC_UpcInfo.stHistoryInfo.uwAlamHOcurTimes = cp_stHistoryPara.uwAlamHOcurTimes;
  130. MC_UpcInfo.stHistoryInfo.uwAlamSOcurTimes = cp_stHistoryPara.uwAlamSOcurTimes;
  131. MC_UpcInfo.stHistoryInfo.uwAlamOHeatTimes = cp_stHistoryPara.uwAlamOHeatTimes;
  132. MC_UpcInfo.stHistoryInfo.uwAlamRotorLockTimes = cp_stHistoryPara.uwAlamRotorLockTimes;
  133. MC_UpcInfo.stHistoryInfo.uwAlamPhsLossTimes = cp_stHistoryPara.uwAlamPhsLossTimes;
  134. MC_UpcInfo.stHistoryInfo.uwAlamOVolTimes = cp_stHistoryPara.uwAlamOVolTimes;
  135. MC_UpcInfo.stHistoryInfo.uwAlamUVolTimes = cp_stHistoryPara.uwAlamUVolTimes;
  136. MC_UpcInfo.stHistoryInfo.uwAlamComOTimeTimes = cp_stHistoryPara.uwAlamComOTimeTimes;
  137. MC_UpcInfo.stHistoryInfo.uwG1AvgPwrConsumption = cp_stHistoryPara.uwG1AvgPwrConsumption;
  138. MC_UpcInfo.stHistoryInfo.uwG2AvgPwrConsumption = cp_stHistoryPara.uwG2AvgPwrConsumption;
  139. MC_UpcInfo.stHistoryInfo.uwG3AvgPwrConsumption = cp_stHistoryPara.uwG3AvgPwrConsumption;
  140. MC_UpcInfo.stHistoryInfo.uwG4AvgPwrConsumption = cp_stHistoryPara.uwG4AvgPwrConsumption;
  141. MC_UpcInfo.stHistoryInfo.uwG5AvgPwrConsumption = cp_stHistoryPara.uwG5AvgPwrConsumption;
  142. MC_UpcInfo.stHistoryInfo.uwODOTripH = (UWORD)(cp_stHistoryPara.ulODOTrip >> 16);
  143. MC_UpcInfo.stHistoryInfo.uwODOTripL = (UWORD)cp_stHistoryPara.ulODOTrip;
  144. MC_UpcInfo.stHistoryInfo.uwODOTimeH = (UWORD)(cp_stHistoryPara.ulODOTime >> 16);
  145. MC_UpcInfo.stHistoryInfo.uwODOTimeL = (UWORD)cp_stHistoryPara.ulODOTime;
  146. MC_UpcInfo.stHistoryInfo.uwTripSumH = (UWORD)(cp_stHistoryPara.ulTripSum >> 16);
  147. MC_UpcInfo.stHistoryInfo.uwTripSumL = (UWORD)cp_stHistoryPara.ulTripSum;
  148. MC_UpcInfo.stHistoryInfo.uwTripSumTimeH = (UWORD)(cp_stHistoryPara.ulTripSumTime >> 16);
  149. MC_UpcInfo.stHistoryInfo.uwTripSumTimeL = (UWORD)cp_stHistoryPara.ulTripSumTime;
  150. MC_UpcInfo.stHistoryInfo.uwTorSensorAlamTimes = cp_stHistoryPara.uwTorSensorAlamTimes;
  151. MC_UpcInfo.stHistoryInfo.uwCadSensorAlamTimes = cp_stHistoryPara.uwCadSensorAlamTimes;
  152. MC_UpcInfo.stHistoryInfo.uwBikeSpdSensorAlamTimes = cp_stHistoryPara.uwBikeSpdSensorAlamTimes;
  153. MC_UpcInfo.stHistoryInfo.uwPosSensorAlamTimes = cp_stHistoryPara.uwPosSensorAlamTimes;
  154. MC_UpcInfo.stTestParaInfo.uwEEFirstDefaultSetFlg = cp_stFlg.ParaFirstSetFlg;
  155. MC_UpcInfo.stTestParaInfo.uwSPIOffsetFirstSetFlg = cp_stFlg.SpiOffsetFirstSetFlg;
  156. MC_UpcInfo.stTestParaInfo.RunModelSelect = cp_stFlg.RunModelSelect;
  157. MC_UpcInfo.stTestParaInfo.ThetaGetModelSelect = cp_stFlg.ThetaGetModelSelect;
  158. MC_UpcInfo.stTestParaInfo.CurrentSampleModelSelect = cp_stFlg.CurrentSampleModelSelect;
  159. MC_UpcInfo.stTestParaInfo.RotateDirectionSelect = cp_stFlg.RotateDirectionSelect;
  160. MC_UpcInfo.stTestParaInfo.uwInitPosCurAmp = cp_stControlPara.swAlignCurAp;
  161. MC_UpcInfo.stTestParaInfo.uwVFControlVolAmp = cp_stControlPara.swDragVolAp;
  162. MC_UpcInfo.stTestParaInfo.uwIFControlCurAmp = cp_stControlPara.swDragCurAp;
  163. MC_UpcInfo.stTestParaInfo.uwVFIFTargetFreHz = cp_stControlPara.swDragSpdHz;
  164. MC_UpcInfo.stTestParaInfo.uwSpeedLoopAccRate = 500;
  165. MC_UpcInfo.stTestParaInfo.uwSpeedLoopDecRate = 100;
  166. MC_UpcInfo.stTestParaInfo.uwSpeedLoopBandWidthHz = cp_stControlPara.swAsrPIBandwidth;
  167. MC_UpcInfo.stTestParaInfo.uwSpeedLoopCoefM = cp_stControlPara.swAsrPIM;
  168. MC_UpcInfo.stTestParaInfo.uwCuerrentLoopBandWidthHz = cp_stControlPara.swAcrPIBandwidth;
  169. MC_UpcInfo.stTestParaInfo.uwCurrentLoopCoefM = cp_stControlPara.swAcrRaCoef;
  170. MC_UpcInfo.stTestParaInfo.uwFluxObsBandWidthHz = cp_stControlPara.swObsFluxPICrossfreHz;
  171. MC_UpcInfo.stTestParaInfo.uwFluxObsCoefM = cp_stControlPara.swObsFluxPIDampratio;
  172. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLBandWidthHz = cp_stControlPara.swObsSpdPLLBandWidthHz;
  173. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLCoefM = cp_stControlPara.swObsSpdPLLM;
  174. MC_UpcInfo.stTestParaInfo.uwJm = cp_stMotorPara.swJD;
  175. MC_UpcInfo.stTestParaInfo.uwPWMMaxDuty = cp_stControlPara.swPWMMaxDuty;
  176. MC_UpcInfo.stTestParaInfo.uwPWM7to5Duty = cp_stControlPara.swPWM7to5Duty;
  177. MC_UpcInfo.stTestParaInfo.uwPwrLimit = cp_stControlPara.swPwrLimitValWt;
  178. MC_UpcInfo.stTestParaInfo.uwPwrLimitError = cp_stControlPara.swPwrLimitErrWt;
  179. MC_UpcInfo.stTestParaInfo.uwPwrLimitKp = cp_stControlPara.swPwrLimitKpPu;
  180. MC_UpcInfo.stTestParaInfo.uwPwrLimitKi = cp_stControlPara.swPwrLimitKiPu;
  181. OBC_SetCustomPara.uwWheelPerimeter = (UBYTE)ass_stParaCong.uwWheelPerimeter;
  182. OBC_SetCustomPara.StartUpMod = (UBYTE)ass_stParaCong.uwStartMode;
  183. OBC_SetCustomPara.BikeSpeedLimit = (UBYTE)ass_stParaCong.uwAssistMaxSpdKmH;
  184. OBC_SetCustomPara.DeltDiameter = (SBYTE)ass_stParaCong.swDeltPerimeter;
  185. OBC_SetCustomPara.AssistMod = (UBYTE)ass_stParaSet.uwAsssistSelectNum;
  186. OBC_SetCustomPara.AutoPowerOffTime = (UBYTE)ass_stParaCong.uwAutoPowerOffTime;
  187. }
  188. static UWORD SizeMCUP;
  189. void Can_voInitMC_Run(void)
  190. {
  191. UBYTE MCU_ID[12];
  192. ULONG MCU_ID_CRC32;
  193. flash_voRead();
  194. // Hardware version
  195. #if(MOTOR_ID_SEL == MOTOR_WELLING_CITY_36V)
  196. strncpy(MC_VerInfo.HW_Version, (char *)"CITY-36V", 8);
  197. #elif(MOTOR_ID_SEL == MOTOR_WELLING_CITY_48V)
  198. strncpy(MC_VerInfo.HW_Version, (char *)"CITY-48V", 8);
  199. #elif(MOTOR_ID_SEL == MOTOR_WELLING_MTB_36V)
  200. strncpy(MC_VerInfo.HW_Version, (char *)"MTB-36V ", 8);
  201. #elif(MOTOR_ID_SEL == MOTOR_WELLING_MTB_48V)
  202. strncpy(MC_VerInfo.HW_Version, (char *)"MTB-48V ", 8);
  203. #else
  204. strncpy(MC_VerInfo.HW_Version, (char *)"TEST ", 8);
  205. #endif
  206. strncpy(MC_VerInfo.HW_Version + 8, (char *)"GD ", 4);
  207. memcpy((uint8_t*)MCU_ID, (uint8_t*)(HW_ID_START_ADDRESS), 12);
  208. MCU_ID_CRC32 = CRC32_Calculate(MCU_ID,12);
  209. memcpy((uint8_t*)(MC_VerInfo.HW_Version + 12), (uint8_t*)(&MCU_ID_CRC32), 4);
  210. // Software version
  211. char chFwVersion[16]="V0r1r10_";
  212. strncat(chFwVersion,COMMIT_TIME,9);
  213. strncpy(MC_VerInfo.FW_Version, (char *)chFwVersion, 16);
  214. // Firmware Special Info
  215. char chFrimware[32]="TC00000-MC00000-V0r0. ";
  216. strncat(chFrimware,FINGER_PRINT,9);
  217. strncpy(Firmware_Special, (char *)chFrimware, 32);
  218. // Model name
  219. memcpy(MC_VerInfo.Mode, flash_stPara.ubMotorVersion, sizeof(flash_stPara.ubMotorVersion));
  220. // SN
  221. memcpy(MC_VerInfo.SN_Num, flash_stPara.ubSN, sizeof(flash_stPara.ubSN));
  222. // Product information
  223. // memcpy(MC_MacInfo.Manufacturer, flash_stPara.ubProdInfo.Manufacturer, sizeof(flash_stPara.ubProdInfo));
  224. memcpy(&MC_MacInfo, &flash_stPara.ubProdInfo, sizeof(flash_stPara.ubProdInfo));
  225. // User information
  226. memcpy(MC_RsASSCII.CustomASCII1, flash_stPara.ubRsASSCII.CustomASCII1, sizeof(MC_RsASSCII.CustomASCII1));
  227. memcpy(MC_RsASSCII.CustomASCII2, flash_stPara.ubRsASSCII.CustomASCII2, sizeof(MC_RsASSCII.CustomASCII2));
  228. memcpy(MC_RsASSCII.CustomASCII3, flash_stPara.ubRsASSCII.CustomASCII3, sizeof(MC_RsASSCII.CustomASCII3));
  229. BMS_VoltEstimat.uwInterResistpu = (UWORD)(((ULONG)1700 << 15) / cof_uwRbOm); // Q15 0.1mOhm BMS internal resistance 150mOhm + 20mOhm
  230. mth_voLPFilterCoef(1000000 / 100, EVENT_1MS_HZ, &BMS_swCurIdcLpf.uwKx); // 100Hz
  231. }
  232. void Can_voMC_Run_1ms(void) /* parasoft-suppress METRICS-28 "鏈」鐩湀澶嶆潅搴︽棤娉曟洿鏀癸紝鍚庣画閬垮厤" */
  233. {
  234. if (cp_stBikeRunInfoPara.BikeSpeedKmH > 30)
  235. {
  236. cp_stBikeRunInfoPara.ulRiTime++;
  237. cp_stHistoryPara.ulUsedTime++;
  238. }
  239. // Error Cnt record and Error Display Set
  240. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms++;
  241. if (MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms == ALAM_DETECT_CNT_IN_1ms && MC_ErrorCode.Code == 0)
  242. {
  243. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  244. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  245. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  246. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  247. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  248. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  249. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms = 0;
  250. }
  251. if (alm_blAlmOccrFlg == TRUE)
  252. {
  253. if (alm_blAlmSingleRecordDoneFlg == FALSE)
  254. {
  255. if (alm_unCode.bit.IPMFlt == 1)
  256. {
  257. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  258. cp_stHistoryPara.uwAlamHOcurTimes++;
  259. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  260. {
  261. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  262. }
  263. }
  264. if (alm_unCode.bit.OvrCur == 1)
  265. {
  266. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  267. cp_stHistoryPara.uwAlamSOcurTimes++;
  268. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  269. {
  270. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  271. }
  272. }
  273. if (alm_unCode.bit.OvrVlt == 1)
  274. {
  275. MC_ErrorCntRecord.Protect_OverVoltageCnt++;
  276. cp_stHistoryPara.uwAlamOVolTimes++;
  277. if (MC_ErrorCntRecord.Protect_OverVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  278. {
  279. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 1;
  280. }
  281. }
  282. if (alm_unCode.bit.UndrVlt == 1)
  283. {
  284. MC_ErrorCntRecord.Protect_UnderVoltageCnt++;
  285. cp_stHistoryPara.uwAlamUVolTimes++;
  286. if (MC_ErrorCntRecord.Protect_UnderVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  287. {
  288. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 1;
  289. }
  290. }
  291. if (alm_unCode.bit.RotorLock == 1)
  292. {
  293. MC_ErrorCntRecord.Protect_LockRotorCnt++;
  294. cp_stHistoryPara.uwAlamRotorLockTimes++;
  295. if (MC_ErrorCntRecord.Protect_LockRotorCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  296. {
  297. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 1;
  298. }
  299. }
  300. if (alm_unCode.bit.IPMOvrHeat == 1)
  301. {
  302. MC_ErrorCntRecord.Protect_OverTempCnt++;
  303. cp_stHistoryPara.uwAlamOHeatTimes++;
  304. if (MC_ErrorCntRecord.Protect_OverTempCnt == ALAM_DISPLAY_CNT_0LEVEL)
  305. {
  306. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 1;
  307. }
  308. }
  309. if (alm_unCode.bit.PhsLoss == 1)
  310. {
  311. MC_ErrorCntRecord.Fault_PhaseLineCnt++;
  312. cp_stHistoryPara.uwAlamPhsLossTimes++;
  313. if (MC_ErrorCntRecord.Fault_PhaseLineCnt == ALAM_DISPLAY_CNT_0LEVEL)
  314. {
  315. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 1;
  316. }
  317. }
  318. // if(alm_unCode.bit.CommOvrTm)
  319. // {
  320. // cp_stHistoryPara.uwAlamComOTimeTimes++;
  321. // }
  322. if (alm_unCode.bit.SpiThetaFlt == 1)
  323. {
  324. MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 1;
  325. }
  326. if (alm_unBikeCode.bit.BikeSpdSen == 1)
  327. {
  328. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 1;
  329. }
  330. if (alm_unBikeCode.bit.CadenceSen == 1)
  331. {
  332. cp_stHistoryPara.uwCadSensorAlamTimes++;
  333. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 1;
  334. }
  335. if (alm_unBikeCode.bit.PCBNTC == 1)
  336. {
  337. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 1;
  338. }
  339. if (alm_unBikeCode.bit.Throttle == 1)
  340. {
  341. MC_ErrorCode.ERROR_Bit.Fault_Throttle = 1;
  342. }
  343. if (alm_unBikeCode.bit.TorqSen == 1)
  344. {
  345. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 1;
  346. }
  347. alm_blAlmSingleRecordDoneFlg = TRUE;
  348. }
  349. }
  350. // Claear error Display
  351. if (MC_ErrorCode.ERROR_Bit.Protect_OverCurrent == 1)
  352. {
  353. if (alm_unCode.bit.IPMFlt != 1 && alm_unCode.bit.OvrCur != 1)
  354. {
  355. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  356. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 0;
  357. }
  358. }
  359. if (MC_ErrorCode.ERROR_Bit.Protect_OverVoltage == 1)
  360. {
  361. if (alm_unCode.bit.OvrVlt != 1)
  362. {
  363. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  364. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 0;
  365. }
  366. }
  367. if (MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage == 1)
  368. {
  369. if (alm_unCode.bit.UndrVlt != 1)
  370. {
  371. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  372. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 0;
  373. }
  374. }
  375. if (MC_ErrorCode.ERROR_Bit.Protect_LockRotor == 1)
  376. {
  377. if (alm_unCode.bit.RotorLock != 1)
  378. {
  379. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  380. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 0;
  381. }
  382. }
  383. if (MC_ErrorCode.ERROR_Bit.Protect_OverTemp == 1)
  384. {
  385. if (alm_unCode.bit.IPMOvrHeat != 1)
  386. {
  387. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  388. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 0;
  389. }
  390. }
  391. if (MC_ErrorCode.ERROR_Bit.Fault_PhaseLine == 1)
  392. {
  393. if (alm_unCode.bit.PhsLoss != 1)
  394. {
  395. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  396. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 0;
  397. }
  398. }
  399. if (MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor == 1)
  400. {
  401. if (alm_unCode.bit.SpiThetaFlt != 1)
  402. {
  403. MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 0;
  404. }
  405. }
  406. if (MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor == 1)
  407. {
  408. if (alm_unBikeCode.bit.BikeSpdSen != 1)
  409. {
  410. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 0;
  411. }
  412. }
  413. if (MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor == 1)
  414. {
  415. if (alm_unBikeCode.bit.CadenceSen != 1)
  416. {
  417. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 0;
  418. }
  419. }
  420. if (MC_ErrorCode.ERROR_Bit.Fault_PCBNTC == 1)
  421. {
  422. if (alm_unBikeCode.bit.PCBNTC != 1)
  423. {
  424. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 0;
  425. }
  426. }
  427. if (MC_ErrorCode.ERROR_Bit.Fault_Throttle == 1)
  428. {
  429. if (alm_unBikeCode.bit.Throttle != 1)
  430. {
  431. MC_ErrorCode.ERROR_Bit.Fault_Throttle = 0;
  432. }
  433. }
  434. if (MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor == 1)
  435. {
  436. if (alm_unBikeCode.bit.TorqSen != 1)
  437. {
  438. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 0;
  439. }
  440. }
  441. UWORD TempPower;
  442. if (FSM2nd_Run_state.state == Exit)
  443. {
  444. TempPower = 0;
  445. }
  446. else
  447. {
  448. TempPower = scm_stMotoPwrInLpf.slY.sw.hi;
  449. }
  450. BMS_VoltEstimat.swIdcPu = (SWORD)(((SLONG)TempPower << 13) / (SLONG)adc_stUpOut.uwVdcLpfPu); // Q15+Q13-Q14=Q14 Calculated dc current
  451. if (BMS_VoltEstimat.swIdcPu < 0)
  452. {
  453. BMS_VoltEstimat.swIdcPu = 0;
  454. }
  455. mth_voLPFilter(BMS_VoltEstimat.swIdcPu, &BMS_swCurIdcLpf);
  456. BMS_VoltEstimat.uwVdcCompPu = (UWORD)((ULONG)BMS_swCurIdcLpf.slY.sw.hi * BMS_VoltEstimat.uwInterResistpu >> 15); // Q14+Q15-Q15=Q14
  457. }
  458. void Can_voMC_Run_5ms(void)
  459. {
  460. cp_stBikeRunInfoPara.BikeSpeedKmH =
  461. (UWORD)((((UQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * FBASE * (ass_stParaCong.uwWheelPerimeter + ass_stParaCong.swDeltPerimeter) * 36 >> 20) *
  462. 1048 * 10) >> 20); // 1048 = Q20(1/1000) 0.1 km/h
  463. }
  464. void Can_voMC_Run_200ms(void)
  465. {
  466. UWORD TempPower;
  467. if((cp_ulSystickCnt - ulBMS_ComTimeOutCount) > 3000)
  468. {
  469. blBMSCommFault = TRUE; // communication abnormal
  470. }
  471. Can_GearSt_switch();
  472. SizeMCUP = sizeof(MC_UpcInfo.stAssistInfo);
  473. if (MC_BC_COM == 1)
  474. {
  475. MC_RunInfoToCDL.SysStatus = FSM1st_Sys_state.state;
  476. MC_RunInfoToCDL.SubStatus = FSM2nd_Run_state.state;
  477. MC_RunInfoToCDL.MotorStatus = curSpeed_state.state;
  478. MC_RunInfoToCDL.AssitStatus = Ass_FSM;
  479. MC_RunInfoToCDL.Id = scm_swIdRefPu;
  480. MC_RunInfoToCDL.Iq = scm_swIqRefPu;
  481. MC_RunInfoToCDL.Ud = scm_swUdRefPu;
  482. MC_RunInfoToCDL.Uq = scm_swUqRefPu;
  483. MC_RunInfoToCDL.InstantTorq = torsensor_stTorSensorOut.uwTorqueReg;
  484. MC_RunInfoToCDL.FilteTorq = torsensor_stTorSensorOut.uwTorqueLPFPu;
  485. MC_RunInfoToCDL.FreqTorq = ass_stTorqMafValue.slAverValue;
  486. MC_RunInfoToCDL.RS[0] = scm_swIdFdbLpfPu;
  487. MC_RunInfoToCDL.RS[1] = scm_swIqFdbLpfPu;
  488. MC_RunInfoToCDL.RS[2] = ass_stCalOut.swTorAss2CurrentTemp;
  489. SendData(ID_MC_TO_CDL, MODE_REPORT, 0xBA20, (UBYTE *)&MC_RunInfoToCDL.SysStatus);
  490. }
  491. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH; ///>杞﹂€?0.1km/h锛屽湴鍧€鍋忕Щ0
  492. MC_RunInfo.MotorSpeed = (SWORD)((SLONG)scm_uwSpdFbkLpfAbsPu * (SLONG)cof_uwVbRpm >> 15); ///>杈撳嚭杞€?1rpm锛屽湴鍧€鍋忕Щ2
  493. TempPower = scm_swMotorPwrInLpfWt;
  494. if (TempPower > 5000)
  495. {
  496. TempPower = 5000;
  497. }
  498. if (TempPower < 0)
  499. {
  500. TempPower = 0;
  501. }
  502. if (sysctrl_stPwmState.blPwmOnflg == FALSE)
  503. {
  504. TempPower = 0;
  505. }
  506. else
  507. {
  508. }
  509. do
  510. {
  511. UWORD PowerTemp1 = 0;
  512. static SLONG PowerFlt = 0;
  513. PowerTemp1 = ((TempPower / 10) * 5 ) >> 3;
  514. PowerTemp1 = (PowerTemp1 < 25) ? 0 : PowerTemp1;
  515. PowerFlt += ((PowerTemp1 << 10) - PowerFlt) >> 3;
  516. MC_RunInfo.Power = (PowerFlt < 0) ? 0 : (PowerFlt) >> 10;
  517. }while(0);
  518. //MC_RunInfo.Power = TempPower / 10; ///>鐢靛姛鐜?1W锛屽湴鍧€鍋忕Щ4
  519. //MC_RunInfo.Power = (scm_swIqFdbLpfPu * 250 * cof_uwIbAp / 1500) >> 14; //MaxIq = 15A,MaxPower=250W
  520. MC_RunInfo.BusVoltage = (UWORD)((((ULONG)adc_stUpOut.uwVdcLpfPu + (ULONG)BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14); ///>姣嶇嚎鐢靛帇 1mV锛屽湴鍧€鍋忕Щ6
  521. //MC_RunInfo.BusCurrent = (UWORD)((ULONG)BMS_VoltEstimat.swIdcPu * cof_uwIbAp * 100 >> 14); ///>姣嶇嚎鐢垫祦 1mA锛屽湴鍧€鍋忕Щ8
  522. MC_RunInfo.BusCurrent = (scm_swIqFdbLpfPu * cof_uwIbAp * 10) >> 14;
  523. MC_RunInfo.Cadence = (UBYTE)(((ULONG)cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * 60) >> 20); ///>韪忛 1rpm锛屽湴鍧€鍋忕Щ10
  524. MC_RunInfo.Torque = (UBYTE)(((ULONG)torsensor_stTorSensorOut.uwTorqueLPFPu * cof_uwTorqNm / 10) >> 14); ///>韪╄笍鍔涚煩 1Nm锛屽湴鍧€鍋忕Щ11
  525. MC_RunInfo.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir; ///>韪╄笍鏂瑰悜 0-姝?1-鍙?2-鍋滄锛屽湴鍧€鍋忕Щ12
  526. MC_RunInfo.GearSt = (UBYTE)MC_ControlCode.GearSt; ///>鍔╁姏妗d綅锛屽湴鍧€鍋忕Щ13
  527. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; ///>鐏紑鍏?0xF0-鍏筹紝0xF1-寮€锛屽湴鍧€鍋忕Щ14
  528. MC_RunInfo.SOC = (UBYTE)Can_SOC_Cal(); ///>鍓╀綑鐢甸噺 1%锛屽湴鍧€鍋忕Щ15
  529. Can_Trip_Cal();
  530. MC_RunInfo.ODO_Km = (UWORD)(MC_RideLog.ODO_Km / 10); ///>鎬婚噷绋?1km锛屽湴鍧€鍋忕Щ18
  531. if (cp_stBikeRunInfoPara.blGearStUpdate)
  532. {
  533. if((cp_stBikeRunInfoPara.uwBikeGear > 0) && (cp_stBikeRunInfoPara.uwBikeGear <= 5))
  534. {
  535. MC_RunInfo.PowerPerKm =
  536. (UBYTE)(*(&cp_stHistoryPara.uwG1AvgPwrConsumption + (cp_stBikeRunInfoPara.uwBikeGear - 1)) / 10); ///>楠炲啿娼庨崝鐔烩偓?0.01Ah/km 閿涘苯婀撮崸鈧崑蹇曅?0
  537. MC_RunInfo.RemainDistance = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
  538. }
  539. else
  540. {
  541. MC_RunInfo.PowerPerKm = 0;
  542. MC_RunInfo.RemainDistance = 0xffff; //invalid value
  543. }
  544. cp_stBikeRunInfoPara.BMSRestChargeLast = BMS_RunInfo.RC;
  545. cp_stBikeRunInfoPara.uwPowerPerKm = 0;
  546. cp_stBikeRunInfoPara.uwAvePowerPerKm = 0;
  547. cp_stBikeRunInfoPara.uwPowerPerKmSum = 0;
  548. memset(cp_stBikeRunInfoPara.uwPowerPerKmBuf, 0, sizeof(cp_stBikeRunInfoPara.uwPowerPerKmBuf));
  549. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  550. cp_stBikeRunInfoPara.uwCruisDis = 0;
  551. }
  552. MC_RunInfo.T_PCB = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40; ///>PCB娓╁害 +40鈩冿紝鍦板潃鍋忕Щ21
  553. MC_RunInfo.T_Coil = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40; ///>缁曠粍娓╁害 +40鈩冿紝鍦板潃鍋忕Щ22
  554. MC_RunInfo.T_MCU = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40; ///>MCU娓╁害 +40鈩冿紝鍦板潃鍋忕Щ23
  555. MC_RunInfo.Ride_Time = (UWORD)(cp_stBikeRunInfoPara.ulRiTime >> 10); ///>寮€鏈哄悗楠戣鏃堕棿 1s锛屽湴鍧€鍋忕Щ26
  556. if (MC_WorkMode == 1)
  557. {
  558. SendData(ID_MC_BC, MODE_REPORT, 0x1020, (UBYTE *)&MC_RunInfo.BikeSpeed);
  559. }
  560. }
  561. void Can_Trip_Cal(void)
  562. {
  563. if (bikespeed_stFreGetOut.blUpdateTripCntFlg == TRUE)
  564. {
  565. MC_RunInfo.Ride_Km++;
  566. cp_stHistoryPara.ulODOTrip++; // Unit: 0.1km
  567. cp_stHistoryPara.ulTripSum++;
  568. Can_RemainTrip_Cal();
  569. bikespeed_stFreGetOut.blUpdateTripCntFlg = FALSE;
  570. }
  571. MC_RideLog.ODO_Km = cp_stHistoryPara.ulODOTrip;
  572. MC_RideLog.TRIP_Km = cp_stHistoryPara.ulTripSum;
  573. MC_RideLog.ODO_Time = cp_stHistoryPara.ulODOTime + (cp_stBikeRunInfoPara.ulRiTime >> 10) / 60; // min
  574. MC_RideLog.TRIP_Time = cp_stHistoryPara.ulTripSumTime + (cp_stBikeRunInfoPara.ulRiTime >> 10) / 60; // min
  575. }
  576. void Can_RemainTrip_Cal(void)
  577. {
  578. if(blBMSCommFault == FALSE)
  579. {
  580. UWORD uwCruisCoef; //鎷ц墷鑹ц剾鑶告湪鑷夊獟Q12
  581. cp_stBikeRunInfoPara.uwCruisDis++; // 0.1 km
  582. if (cp_stBikeRunInfoPara.uwCruisDis >= 5) // 5 = 0.5km
  583. {
  584. cp_stBikeRunInfoPara.uwCruisDis = 0;
  585. cp_stBikeRunInfoPara.BMSRestChargeNow = BMS_RunInfo.RC;
  586. cp_stBikeRunInfoPara.uwPowerPerKm = (cp_stBikeRunInfoPara.BMSRestChargeLast - cp_stBikeRunInfoPara.BMSRestChargeNow)
  587. << 1; // BMS_RunInfo.RC (BMSRestChargeLast - BMSRestChargeNow) /0.5 km
  588. cp_stBikeRunInfoPara.BMSRestChargeLast = cp_stBikeRunInfoPara.BMSRestChargeNow;
  589. cp_stBikeRunInfoPara.uwPowerPerKmSum -= cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  590. cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT] = cp_stBikeRunInfoPara.uwPowerPerKm;
  591. cp_stBikeRunInfoPara.uwPowerPerKmSum += cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  592. if (cp_stBikeRunInfoPara.uwPowerPerKmBuf[31] == 0)
  593. {
  594. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum / (cp_stBikeRunInfoPara.uwAvePowerCNT + 1);
  595. }
  596. else
  597. {
  598. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum >> 5;
  599. }
  600. cp_stBikeRunInfoPara.uwAvePowerCNT++;
  601. if (cp_stBikeRunInfoPara.uwAvePowerCNT >= 32)
  602. {
  603. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  604. }
  605. if(cp_stBikeRunInfoPara.uwAvePowerPerKm > 0)
  606. {
  607. if (cp_stBikeRunInfoPara.uwBikeGear == 1)
  608. {
  609. uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG1AvgPwrConsumption);
  610. cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
  611. cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
  612. cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
  613. cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
  614. cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
  615. MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG1AvgPwrConsumption / 10);
  616. }
  617. else if (cp_stBikeRunInfoPara.uwBikeGear == 2)
  618. {
  619. uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG2AvgPwrConsumption);
  620. cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
  621. cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
  622. cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
  623. cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
  624. cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
  625. MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG2AvgPwrConsumption / 10);
  626. }
  627. else if (cp_stBikeRunInfoPara.uwBikeGear == 3)
  628. {
  629. uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG3AvgPwrConsumption);
  630. cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
  631. cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
  632. cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
  633. cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
  634. cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
  635. MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG3AvgPwrConsumption / 10);
  636. }
  637. else if (cp_stBikeRunInfoPara.uwBikeGear == 4)
  638. {
  639. uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG4AvgPwrConsumption);
  640. cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
  641. cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
  642. cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
  643. cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
  644. cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
  645. MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG4AvgPwrConsumption / 10);
  646. }
  647. else if (cp_stBikeRunInfoPara.uwBikeGear == 5)
  648. {
  649. uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG5AvgPwrConsumption);
  650. cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
  651. cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
  652. cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
  653. cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
  654. cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
  655. MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG5AvgPwrConsumption / 10);
  656. }
  657. else
  658. {
  659. uwCruisCoef = 4096;
  660. }
  661. MC_RunInfo.RemainDistance = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
  662. }
  663. }
  664. }
  665. }
  666. #if ((MOTOR_ID_SEL == MOTOR_WELLING_CITY_36V) || (MOTOR_ID_SEL == MOTOR_WELLING_MTB_36V))
  667. static SLONG slBMSMaxVol = 42000;
  668. static SLONG slBMSMinVol = 32000;
  669. #elif ((MOTOR_ID_SEL == MOTOR_WELLING_CITY_48V) || (MOTOR_ID_SEL == MOTOR_WELLING_MTB_48V))
  670. static SLONG slBMSMaxVol = 54600;
  671. static SLONG slBMSMinVol = 41600;
  672. #endif
  673. static SWORD SOC_Cnt, SOC_Value, SOC_MIN;
  674. static ULONG Voltage_Sum;
  675. static BOOL blSOCfirstSet = FALSE;
  676. UWORD Can_SOC_Cal(void)
  677. {
  678. UWORD templenght, VoltageAvg = 0;
  679. //寮€鏈?s绛夊緟鐢靛帇绋冲畾鍚庡垵濮嬪寲鐢甸噺
  680. if (cp_ulSystickCnt < 1000)
  681. {
  682. return (UWORD)0;
  683. }
  684. if(blBMSCommFault == FALSE)
  685. {
  686. SOC_Value = BMS_RunInfo.SOC;
  687. }
  688. else if (blSOCfirstSet == FALSE)
  689. {
  690. if(0 == ulBMS_ComTimeOutCount)
  691. {
  692. if (MC_RunInfo.BusVoltage >= slBMSMaxVol)
  693. {
  694. SOC_Value = 100;
  695. }
  696. else if (MC_RunInfo.BusVoltage <= slBMSMinVol)
  697. {
  698. SOC_Value = 0;
  699. }
  700. else
  701. {
  702. SOC_Value = (SWORD)(((SLONG)MC_RunInfo.BusVoltage - slBMSMinVol) * 100 / (slBMSMaxVol - slBMSMinVol));
  703. }
  704. SOC_MIN = SOC_Value;
  705. }
  706. else
  707. {
  708. SOC_MIN = BMS_RunInfo.SOC;
  709. }
  710. blSOCfirstSet = TRUE;
  711. }
  712. //1min鏇存柊涓€娆$數閲?
  713. else
  714. {
  715. templenght = 300; // 60s in 200ms time task
  716. SOC_Cnt++;
  717. Voltage_Sum += MC_RunInfo.BusVoltage;
  718. if (SOC_Cnt >= templenght) // 60s
  719. {
  720. VoltageAvg = (UWORD)(Voltage_Sum / templenght);
  721. if (VoltageAvg <= slBMSMinVol)
  722. {
  723. SOC_Value = 0;
  724. }
  725. else
  726. {
  727. SOC_Value = (SWORD)(((SLONG)VoltageAvg - (SLONG)slBMSMinVol) * 100 / ((SLONG)slBMSMaxVol - (SLONG)slBMSMinVol));
  728. }
  729. if (SOC_Value < SOC_MIN)
  730. {
  731. SOC_MIN = SOC_Value;
  732. }
  733. else
  734. {
  735. }
  736. SOC_Cnt = 0;
  737. Voltage_Sum = 0;
  738. }
  739. if (SOC_Value > SOC_MIN)
  740. {
  741. SOC_Value = SOC_MIN;
  742. }
  743. else if (SOC_Value < 0)
  744. {
  745. SOC_Value = 0;
  746. }
  747. else
  748. {
  749. // do nothing
  750. }
  751. }
  752. return (UWORD)SOC_Value;
  753. }
  754. void Can_GearSt_switch(void)
  755. {
  756. cp_stBikeRunInfoPara.uwBikeGearLast = cp_stBikeRunInfoPara.uwBikeGear;
  757. if (MC_WorkMode == 1) // 閰嶇疆妯″紡涓嶈嚜鍔ㄥ叧闂姪鍔?
  758. {
  759. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  760. }
  761. #ifdef RUN_ARCH_SIM
  762. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  763. #endif
  764. if ((cp_ulSystickCnt - ulOBC_ComTimeOutCount) < 3000) // 鎺у埗鍣ㄤ笌浠〃閫氫俊涓柇瓒呰繃3s锛屽叧闂姪鍔?
  765. {
  766. if (MC_ControlCode.GearSt <= 0x05)
  767. {
  768. cp_stBikeRunInfoPara.uwBikeGear = (UWORD)MC_ControlCode.GearSt;
  769. }
  770. else if (MC_ControlCode.GearSt == 0x33)
  771. {
  772. cp_stBikeRunInfoPara.uwBikeGear = 5;
  773. }
  774. else if (MC_ControlCode.GearSt == 0x22)
  775. {
  776. cp_stBikeRunInfoPara.uwBikeGear = 0x22;
  777. }
  778. else
  779. {
  780. // do nothing
  781. }
  782. }
  783. else
  784. {
  785. MC_ControlCode.GearSt = MC_GearSt_OFF;
  786. MC_RunInfo.GearSt = 0x00;
  787. cp_stBikeRunInfoPara.uwBikeGear = 0x00;
  788. }
  789. if (cp_stBikeRunInfoPara.uwBikeGear != cp_stBikeRunInfoPara.uwBikeGearLast)
  790. {
  791. cp_stBikeRunInfoPara.blGearStUpdate = TRUE;
  792. }
  793. else
  794. {
  795. cp_stBikeRunInfoPara.blGearStUpdate = FALSE;
  796. }
  797. }
  798. void Can_Light_switch(void)
  799. {
  800. /* light switch*/
  801. if (MC_ControlCode.LightSwitch == 0xF1)
  802. {
  803. cp_stBikeRunInfoPara.uwLightSwitch = 1;
  804. }
  805. else if (MC_ControlCode.LightSwitch == 0xF0)
  806. {
  807. cp_stBikeRunInfoPara.uwLightSwitch = 0;
  808. }
  809. else
  810. {
  811. // do nothing
  812. }
  813. }
  814. void Can_AssistCoef_Read(const UPC_CurveOrderInfo_Struct_t *order)
  815. {
  816. MC_UpcInfo.stAssistInfo.stCurveOrderInfo = *order;
  817. memcpy(&MC_UpcInfo.stAssistInfo.slTorquePolyA, &flash_stPara.slTorqAssGain[order->swTorqueCurveNum - 1], sizeof(POLY_COEF));
  818. memcpy(&MC_UpcInfo.stAssistInfo.slCadencePolyA, &flash_stPara.slCadAssGain[order->swCadenceCurveNum - 1], sizeof(POLY_COEF));
  819. }
  820. void Can_AssistCoef_Write(const UPC_CurveOrderInfo_Struct_t *order)
  821. {
  822. memcpy(&flash_stPara.slTorqAssGain[order->swTorqueCurveNum - 1], &MC_UpcInfo.stAssistInfo.slTorquePolyA, sizeof(POLY_COEF));
  823. memcpy(&flash_stPara.slCadAssGain[order->swCadenceCurveNum - 1], &MC_UpcInfo.stAssistInfo.slCadencePolyA, sizeof(POLY_COEF));
  824. }