canAppl.c 43 KB

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