canAppl.c 51 KB

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