canAppl.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /**
  2. * @file canAppl.c
  3. * @author Zhang, Kai(zhangkai71@midea.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2022-03-02
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #include "canAppl.h"
  12. #include "CodePara.h"
  13. #include "FuncLayerAPI.h"
  14. #include "adc.h"
  15. #include "alarm.h"
  16. #include "usart.h"
  17. #include "can.h"
  18. #include "flash_master.h"
  19. #include "glbcof.h"
  20. #include "spdctrmode.h"
  21. #include "string.h"
  22. #include "syspar.h"
  23. #include "AssistCurve.h"
  24. #include "hwsetup.h"
  25. #include "spi_master.h"
  26. #include "fp.def"
  27. #include "CadAssist.h"
  28. #include "bikeinformation.h"
  29. #include "FSM_1st.h"
  30. #include "FSM_2nd.h"
  31. /******************************
  32. *
  33. * Parameter
  34. *
  35. ******************************/
  36. typedef _Bool BOOL;
  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. CONTORLFUN_STRUCT OBC_ControlFUN;
  51. MC_Error_Address_Struct ManageError_Address;
  52. OBC_SetCustomPara_Struct_t OBC_SetCustomPara;
  53. OBC_ButtonStatus_Struct_t OBC_ButtonStatus = {0,0,0,FALSE};
  54. ULONG ulOBC_ComTimeOutCount = 0;
  55. ULONG ulBMS_ComTimeOutCount = 0;
  56. uint8_t MC_MotorSPD_rpm_Percent = 0;
  57. uint8_t MC_WorkMode;
  58. uint8_t MC_BC_COM = 0;
  59. LPF_OUT BMS_swCurIdcLpf;
  60. BOOL blBMSCommFault = FALSE;
  61. static UWORD uwRemainDistanceCal;
  62. POWER_STRUCT Powercal;
  63. void Power_Cal(void);
  64. void Can_voUpdateMC_UpcInfo(void)
  65. {
  66. MC_UpcInfo.stMotorInfo.uwPolePairs = cp_stMotorPara.swMotrPolePairs;
  67. MC_UpcInfo.stMotorInfo.uwRsmOhm = cp_stMotorPara.swRsOhm;
  68. MC_UpcInfo.stMotorInfo.uwLduH = cp_stMotorPara.uwLdmH;
  69. MC_UpcInfo.stMotorInfo.uwLquH = cp_stMotorPara.uwLqmH;
  70. MC_UpcInfo.stMotorInfo.uwFluxmWb = cp_stMotorPara.swFluxWb;
  71. MC_UpcInfo.stMotorInfo.uwIdMaxA = cp_stMotorPara.swIdMaxA;
  72. MC_UpcInfo.stMotorInfo.uwIdMinA = cp_stMotorPara.swIdMinA;
  73. MC_UpcInfo.stMotorInfo.uwRSpdRpm = cp_stMotorPara.swRSpeedRpm;
  74. MC_UpcInfo.stMotorInfo.uwRPwrWt = cp_stMotorPara.swRPwrWt;
  75. MC_UpcInfo.stMotorInfo.uwRCurA = cp_stMotorPara.swRIarmsA;
  76. MC_UpcInfo.stMotorInfo.uwRVolV = cp_stMotorPara.swRUdcV;
  77. MC_UpcInfo.stMotorInfo.uwJD = cp_stMotorPara.swJD;
  78. MC_UpcInfo.stMotorInfo.uwTorMaxNm = cp_stMotorPara.swTorMax;
  79. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = ass_ParaCong.uwWheelPerimeter;
  80. MC_UpcInfo.stBikeInfo.uwMechRationMotor = ass_ParaCong.uwMechRationMotorEEPROM;//(UWORD)(((ULONG)ass_ParaCong.uwMechRationMotor*1000)/1024);
  81. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = ass_ParaCong.uwThrottleMaxSpdKmH;
  82. MC_UpcInfo.stBikeInfo.uwCartSpdKmH = ass_ParaCong.uwCartSpdKmH;
  83. MC_UpcInfo.stBikeInfo.uwNmFrontChainring = ass_ParaCong.uwNmFrontChainring;
  84. MC_UpcInfo.stBikeInfo.uwNmBackChainring = ass_ParaCong.uwNmBackChainring;
  85. MC_UpcInfo.stBikeInfo.uwAssistSelect1 = ass_ParaCong.uwAssistSelect1;
  86. MC_UpcInfo.stBikeInfo.uwAssistSelect2 = ass_ParaCong.uwAssistSelect2;
  87. MC_UpcInfo.stBikeInfo.uwLightConfig = ass_ParaCong.uwLightConfig;
  88. MC_UpcInfo.stBikeInfo.swWheelSizeAdjust = ass_ParaCong.swDeltPerimeter;
  89. MC_UpcInfo.stBikeInfo.uwStartMode = ass_ParaCong.uwStartMode;
  90. MC_UpcInfo.stBikeInfo.uwAutoPowerOffTime = ass_ParaCong.uwAutoPowerOffTime;
  91. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetOrigin =0;// spi_stResolverOut.swSpiThetaOffsetOrignPu;
  92. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetNow = 0;//spi_stResolverOut.swSpiThetaOffsetPu;
  93. MC_UpcInfo.stMContorlInfo.uwIPeakMaxA = cp_stMotorPara.swIpeakMaxA;
  94. MC_UpcInfo.stMContorlInfo.uwAlamOCurA = cp_stControlPara.swAlmOverCurrentVal;
  95. MC_UpcInfo.stMContorlInfo.uwAlamOVolV = cp_stControlPara.swAlmOverVolVal1;
  96. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = cp_stControlPara.swAlmUnderVolVal1;
  97. MC_UpcInfo.stMContorlInfo.uwAlamOverSpdRpm = cp_stControlPara.swAlmOverSpdVal;
  98. MC_UpcInfo.stMContorlInfo.uwAlamOverHeatCe = cp_stControlPara.swAlmOverHeatCeVal;
  99. MC_UpcInfo.stMContorlInfo.uwAlamRecHeatCe = cp_stControlPara.swAlmRecOHeatVal;
  100. MC_UpcInfo.stMContorlInfo.uwPwrLimitStartCe = cp_stControlPara.swAlmPwrLimitStartTempVal;
  101. MC_UpcInfo.stMContorlInfo.uwAlamMotorOverHeatCe = cp_stControlPara.swAlmMotorOverHeatCeVal;
  102. MC_UpcInfo.stMContorlInfo.uwAlamMotorRecHeatCe = cp_stControlPara.swAlmMotorRecOHeatVal;
  103. MC_UpcInfo.stMContorlInfo.uwPwrLimitMotorStartCe = cp_stControlPara.swAlmPwrLimitMotorStartTempVal;
  104. MC_UpcInfo.stMContorlInfo.uwControlFunEN= cp_stControlPara.uwControlFunEN;
  105. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetOrigin = torsensor_stTorSensorCof.uwTorqueOffsetOrign;
  106. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow1 = torsensor_stTorSensorCof.uwTorqueOffsetNow1;
  107. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow2 = torsensor_stTorSensorCof.uwTorqueOffsetNow2;
  108. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow3 = torsensor_stTorSensorCof.uwTorqueOffsetNow3;
  109. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow4 = torsensor_stTorSensorCof.uwTorqueOffsetNow4;
  110. MC_UpcInfo.stSensorInfo.uwBikeTorMaxNm = torsensor_stTorSensorCof.uwMaxSensorTorquePu;
  111. MC_UpcInfo.stSensorInfo.uwBikeTor1StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep1RealNm;
  112. MC_UpcInfo.stSensorInfo.uwBikeTor1StepADC = torsensor_stTorSensorCof.uwBikeTorStep1ADC;
  113. MC_UpcInfo.stSensorInfo.uwBikeTor2StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep2RealNm;
  114. MC_UpcInfo.stSensorInfo.uwBikeTor2StepADC = torsensor_stTorSensorCof.uwBikeTorStep2ADC;
  115. MC_UpcInfo.stSensorInfo.uwBikeTor3StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep3RealNm;
  116. MC_UpcInfo.stSensorInfo.uwBikeTor3StepADC = torsensor_stTorSensorCof.uwBikeTorStep3ADC;
  117. MC_UpcInfo.stSensorInfo.uwBikeTor4StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep4RealNm;
  118. MC_UpcInfo.stSensorInfo.uwBikeTor4StepADC = torsensor_stTorSensorCof.uwBikeTorStep4ADC;
  119. MC_UpcInfo.stSensorInfo.uwTorque_SensorPulseNm = cadence_stFreGetCof.uwTorque_NumbersPulses;
  120. MC_UpcInfo.stSensorInfo.uwBikeSpdSensorPulseNm = bikespeed_stFreGetCof.uwNumbersPulses;
  121. MC_UpcInfo.stSensorInfo.uwCad_SensorPulseNm= cadence_stFreGetCof.uwCad_NumbersPulses;
  122. MC_UpcInfo.stAssistInfo.swStartupGain = ass_ParaSet.uwStartupCoef;
  123. MC_UpcInfo.stAssistInfo.swStartcruiseGain = ass_ParaSet.uwStartupCruiseCoef;
  124. MC_UpcInfo.stAssistInfo.uwAssistStartNm = ass_ParaSet.uwAssistStartNm;
  125. MC_UpcInfo.stAssistInfo.uwAssistStopNm = ass_ParaSet.uwAssistStopNm;
  126. MC_UpcInfo.stAssistInfo.uwStartUpGainStep = ass_ParaSet.uwStartUpGainStep;
  127. MC_UpcInfo.stAssistInfo.uwStartUpCadNm = ass_ParaSet.uwStartUpCadNm;
  128. MC_UpcInfo.stAssistInfo.uwTorLPFCadNm = ass_ParaSet.uwTorLPFCadNm;
  129. MC_UpcInfo.stAssistInfo.uwSpeedAssistSpdRpm = ass_ParaSet.uwSpeedAssistSpdRpm;
  130. MC_UpcInfo.stAssistInfo.uwSpeedAssistIMaxA = ass_ParaSet.uwSpeedAssistIMaxA;
  131. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = ass_ParaSet.uwAssistLimitBikeSpdStart;
  132. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = ass_ParaSet.uwAssistLimitBikeSpdStop;
  133. MC_UpcInfo.stAssistInfo.uwCadenceAssistWeight = ass_ParaSet.uwCadenceWeight;
  134. MC_UpcInfo.stAssistInfo.uwCadAssPidKp = ass_stCadAssSpdCtl.uwPidKp;
  135. MC_UpcInfo.stAssistInfo.swCadAssPidLimMax = ass_stCadAssSpdCtl.swPidLimMax;
  136. MC_UpcInfo.stAssistInfo.swCadAssPidVolDec = ass_stCadAssSpdCtl.swPidVolDec;
  137. MC_UpcInfo.stAssistInfo.swCadAssTargetAssCurAcc = ass_stCadAssParaPro.swTargetAssCurAcc;
  138. MC_UpcInfo.stAssistInfo.uwMaxCadRpm = ass_stCadAssCoef.uwMaxCadRpm;
  139. MC_UpcInfo.stAssistInfo.reserve2 = ass_stReservePara.uwReserve2;
  140. MC_UpcInfo.stAssistInfo.reserve3 = ass_stReservePara.uwReserve3;
  141. MC_UpcInfo.stAssistInfo.reserve4 = ass_stReservePara.uwReserve4;
  142. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain1 = ass_stCalCoef.ucAssistRatioGain[0];
  143. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain2 = ass_stCalCoef.ucAssistRatioGain[1];
  144. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain3 = ass_stCalCoef.ucAssistRatioGain[2];
  145. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain4 = ass_stCalCoef.ucAssistRatioGain[3];
  146. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain5 = ass_stCalCoef.ucAssistRatioGain[4];
  147. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain1 = ass_stCalCoef.ucAssistAccelerationGain[0];
  148. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain2 = ass_stCalCoef.ucAssistAccelerationGain[1];
  149. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain3 = ass_stCalCoef.ucAssistAccelerationGain[2];
  150. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain4 = ass_stCalCoef.ucAssistAccelerationGain[3];
  151. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain5 = ass_stCalCoef.ucAssistAccelerationGain[4];
  152. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain1 = ass_stCalCoef.ucMaxCurrentGain[0];
  153. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain2 = ass_stCalCoef.ucMaxCurrentGain[1];
  154. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain3 = ass_stCalCoef.ucMaxCurrentGain[2];
  155. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain4 = ass_stCalCoef.ucMaxCurrentGain[3];
  156. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain5 = ass_stCalCoef.ucMaxCurrentGain[4];
  157. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain1 = ass_stCalCoef.ucMaxTorqueGain[0];
  158. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain2 = ass_stCalCoef.ucMaxTorqueGain[1];
  159. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain3 = ass_stCalCoef.ucMaxTorqueGain[2];
  160. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain4 = ass_stCalCoef.ucMaxTorqueGain[3];
  161. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain5 = ass_stCalCoef.ucMaxTorqueGain[4];
  162. MC_UpcInfo.stBikeInfo2.uwNoneOBCEnable = ass_ParaCong.uwNoneOBCEnable;
  163. MC_UpcInfo.stBikeInfo2.uwRearLightCycle = ass_ParaCong.uwRearLightCycle;
  164. MC_UpcInfo.stBikeInfo2.uwRearLightDuty = ass_ParaCong.uwRearLightDuty;
  165. MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit = ass_ParaCong.swDeltaBikeSpeedLimit;
  166. MC_UpcInfo.stHistoryInfo.uwOpenTimes = cp_stHistoryPara.uwOpenTimes;
  167. MC_UpcInfo.stHistoryInfo.uwUsedTimeH = (UWORD)(cp_stHistoryPara.ulUsedTime >> 16);
  168. MC_UpcInfo.stHistoryInfo.uwUsedTimeL = (UWORD)(cp_stHistoryPara.ulUsedTime);
  169. MC_UpcInfo.stHistoryInfo.swNTCTempMaxCe = cp_stHistoryPara.swNTCTempMaxCe;
  170. MC_UpcInfo.stHistoryInfo.swNTCTempMinCe = cp_stHistoryPara.swNTCTempMinCe;
  171. ;
  172. MC_UpcInfo.stHistoryInfo.uwAlamHOcurTimes = cp_stHistoryPara.uwAlamHOcurTimes;
  173. MC_UpcInfo.stHistoryInfo.uwAlamSOcurTimes = cp_stHistoryPara.uwAlamSOcurTimes;
  174. MC_UpcInfo.stHistoryInfo.uwAlamOHeatTimes = cp_stHistoryPara.uwAlamOHeatTimes;
  175. MC_UpcInfo.stHistoryInfo.uwAlamRotorLockTimes = cp_stHistoryPara.uwAlamRotorLockTimes;
  176. MC_UpcInfo.stHistoryInfo.uwAlamPhsLossTimes = cp_stHistoryPara.uwAlamPhsLossTimes;
  177. MC_UpcInfo.stHistoryInfo.uwAlamOVolTimes = cp_stHistoryPara.uwAlamOVolTimes;
  178. MC_UpcInfo.stHistoryInfo.uwAlamUVolTimes = cp_stHistoryPara.uwAlamUVolTimes;
  179. MC_UpcInfo.stHistoryInfo.uwAlamComOTimeTimes = cp_stHistoryPara.uwAlamComOTimeTimes;
  180. MC_UpcInfo.stHistoryInfo.uwG1AvgPwrConsumption = cp_stHistoryPara.uwG1AvgPwrConsumption;
  181. MC_UpcInfo.stHistoryInfo.uwG2AvgPwrConsumption = cp_stHistoryPara.uwG2AvgPwrConsumption;
  182. MC_UpcInfo.stHistoryInfo.uwG3AvgPwrConsumption = cp_stHistoryPara.uwG3AvgPwrConsumption;
  183. MC_UpcInfo.stHistoryInfo.uwG4AvgPwrConsumption = cp_stHistoryPara.uwG4AvgPwrConsumption;
  184. MC_UpcInfo.stHistoryInfo.uwG5AvgPwrConsumption = cp_stHistoryPara.uwG5AvgPwrConsumption;
  185. MC_UpcInfo.stHistoryInfo.uwODOTripH = (UWORD)(cp_stHistoryPara.ulODOTrip >> 16);
  186. MC_UpcInfo.stHistoryInfo.uwODOTripL = (UWORD)cp_stHistoryPara.ulODOTrip;
  187. MC_UpcInfo.stHistoryInfo.uwODOTimeH = (UWORD)(cp_stHistoryPara.ulODOTime >> 16);
  188. MC_UpcInfo.stHistoryInfo.uwODOTimeL = (UWORD)cp_stHistoryPara.ulODOTime;
  189. MC_UpcInfo.stHistoryInfo.uwTripSumH = (UWORD)(cp_stHistoryPara.ulTripSum >> 16);
  190. MC_UpcInfo.stHistoryInfo.uwTripSumL = (UWORD)cp_stHistoryPara.ulTripSum;
  191. MC_UpcInfo.stHistoryInfo.uwTripSumTimeH = (UWORD)(cp_stHistoryPara.ulTripSumTime >> 16);
  192. MC_UpcInfo.stHistoryInfo.uwTripSumTimeL = (UWORD)cp_stHistoryPara.ulTripSumTime;
  193. MC_UpcInfo.stHistoryInfo.uwTorSensorAlamTimes = cp_stHistoryPara.uwTorSensorAlamTimes;
  194. MC_UpcInfo.stHistoryInfo.uwCadSensorAlamTimes = cp_stHistoryPara.uwCadSensorAlamTimes;
  195. MC_UpcInfo.stHistoryInfo.uwBikeSpdSensorAlamTimes = cp_stHistoryPara.uwBikeSpdSensorAlamTimes;
  196. MC_UpcInfo.stHistoryInfo.uwPosSensorAlamTimes = cp_stHistoryPara.uwPosSensorAlamTimes;
  197. MC_UpcInfo.stHistoryInfo.uwRealODOTripH = (UWORD)(cp_stHistoryPara.ulRealODOTrip >> 16);
  198. MC_UpcInfo.stHistoryInfo.uwRealODOTripL = (UWORD)cp_stHistoryPara.ulRealODOTrip;
  199. MC_UpcInfo.stHistoryInfo.uwRealODOTimeH = (UWORD)(cp_stHistoryPara.ulRealODOTime >> 16);
  200. MC_UpcInfo.stHistoryInfo.uwRealODOTimeL = (UWORD)cp_stHistoryPara.ulRealODOTime;
  201. MC_UpcInfo.stTestParaInfo.uwEEFirstDefaultSetFlg = cp_stFlg.ParaFirstSetFlg;
  202. // MC_UpcInfo.stTestParaInfo.uwSPIOffsetFirstSetFlg = cp_stFlg.SpiOffsetFirstSetFlg;
  203. MC_UpcInfo.stTestParaInfo.RunModelSelect = cp_stFlg.RunModelSelect;
  204. MC_UpcInfo.stTestParaInfo.ThetaGetModelSelect = cp_stFlg.ThetaGetModelSelect;
  205. MC_UpcInfo.stTestParaInfo.CurrentSampleModelSelect = cp_stFlg.CurrentSampleModelSelect;
  206. MC_UpcInfo.stTestParaInfo.RotateDirectionSelect = cp_stFlg.RotateDirectionSelect;
  207. MC_UpcInfo.stTestParaInfo.uwInitPosCurAmp = cp_stControlPara.swAlignCurAp;
  208. MC_UpcInfo.stTestParaInfo.uwVFControlVolAmp = cp_stControlPara.swDragVolAp;
  209. MC_UpcInfo.stTestParaInfo.uwIFControlCurAmp = cp_stControlPara.swDragCurAp;
  210. MC_UpcInfo.stTestParaInfo.uwVFIFTargetFreHz = cp_stControlPara.swDragSpdHz;
  211. MC_UpcInfo.stTestParaInfo.uwSpeedLoopAccRate = 500;
  212. MC_UpcInfo.stTestParaInfo.uwSpeedLoopDecRate = 100;
  213. MC_UpcInfo.stTestParaInfo.uwSpeedLoopBandWidthHz = cp_stControlPara.swAsrPIBandwidth;
  214. MC_UpcInfo.stTestParaInfo.uwSpeedLoopCoefM = cp_stControlPara.swAsrPIM;
  215. MC_UpcInfo.stTestParaInfo.uwCuerrentLoopBandWidthHz = cp_stControlPara.swAcrPIBandwidth;
  216. MC_UpcInfo.stTestParaInfo.uwCurrentLoopCoefM = cp_stControlPara.swAcrRaCoef;
  217. MC_UpcInfo.stTestParaInfo.uwFluxObsBandWidthHz = cp_stControlPara.swObsFluxPICrossfreHz;
  218. MC_UpcInfo.stTestParaInfo.uwFluxObsCoefM = cp_stControlPara.swObsFluxPIDampratio;
  219. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLBandWidthHz = cp_stControlPara.swObsSpdPLLBandWidthHz;
  220. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLCoefM = cp_stControlPara.swObsSpdPLLM;
  221. MC_UpcInfo.stTestParaInfo.uwJm = cp_stMotorPara.swJD ;
  222. MC_UpcInfo.stTestParaInfo.uwPWMMaxDuty = cp_stControlPara.swPWMMaxDuty;
  223. MC_UpcInfo.stTestParaInfo.uwPWM7to5Duty = cp_stControlPara.swPWM7to5Duty;
  224. MC_UpcInfo.stTestParaInfo.uwPwrLimit = cp_stControlPara.swPwrLimitValWt;
  225. MC_UpcInfo.stTestParaInfo.uwPwrLimitError = cp_stControlPara.swPwrLimitErrWt;
  226. MC_UpcInfo.stTestParaInfo.uwPwrLimitKp = cp_stControlPara.swPwrLimitKpPu;
  227. MC_UpcInfo.stTestParaInfo.uwPwrLimitKi = cp_stControlPara.swPwrLimitKiPu;
  228. OBC_SetCustomPara.uwWheelPerimeter = ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter;
  229. OBC_SetCustomPara.StartUpMod = 1;
  230. OBC_SetCustomPara.BikeSpeedLimit = ass_ParaSet.uwAssistLimitBikeSpdStart;
  231. OBC_SetCustomPara.DeltDiameter = ass_ParaCong.swDeltPerimeter;
  232. OBC_SetCustomPara.AssistMod = ass_ParaSet.uwAsssistSelectNum;
  233. OBC_SetCustomPara.AutoPowerOffTime = ass_ParaCong.uwAutoPowerOffTime;
  234. OBC_SetCustomPara.scDeltaBikeSpeedLimit = (SBYTE)ass_ParaCong.swDeltaBikeSpeedLimit;
  235. }
  236. UWORD SizeMCUP;
  237. void Can_voInitMC_Run(void)
  238. {
  239. // flash_voVerRead();
  240. // flash_voErrorRead();
  241. // MC版本信息初始化,Mode和SN从EEPROM读取
  242. strncpy(MC_VerInfo.HW_Version, (char *)"SC20100101V1 ", 16); //长度不超过16
  243. // Software version
  244. char chFwVersion[16]="V1r0r0_";
  245. strncat(chFwVersion,COMPLIE_TIME,9); // Commit time COMPLIE_TIME
  246. strncpy(MC_VerInfo.FW_Version, (char *)chFwVersion, 16);
  247. // Firmware Special Info
  248. char chFrimware[32]="NC2025X000-MS0000-V0r0. ";
  249. strncat(chFrimware,FINGER_PRINT,8); // Git Version
  250. if(cp_stFlg.RunModelSelect == CadAssist)
  251. {
  252. strncat(chFrimware,"C",1); // Cadance assist end with "C" Torque assist end with "T"
  253. }
  254. else if(cp_stFlg.RunModelSelect == TorqAssist)
  255. {
  256. strncat(chFrimware,"T",1); // Torque Assist end with "T", Cadence Assist end with "C"
  257. }
  258. else
  259. {
  260. /* 不做处理*/
  261. }
  262. strncpy(Firmware_Special, (char *)chFrimware, 32);
  263. //电机型号
  264. // memcpy(MC_VerInfo.Mode, Syspara2.flash_stPara.ubMotorVersion, sizeof(Syspara2.flash_stPara.ubMotorVersion));
  265. // memcpy(MC_VerInfo.SN_Num, Syspara2.flash_stPara.ubSN, sizeof(Syspara2.flash_stPara.ubSN));
  266. memcpy(MC_VerInfo.Mode, Productionpara.ubMotorVersion, sizeof(Productionpara.ubMotorVersion));
  267. memcpy(MC_VerInfo.SN_Num, Productionpara.ubSN, sizeof(Productionpara.ubSN));
  268. // MC生产信息
  269. memcpy(MC_MacInfo.Manufacturer, Productionpara.ubProdInfo.Manufacturer, sizeof(Productionpara.ubProdInfo));
  270. //自定义字符串
  271. memcpy(MC_RsASSCII.CustomASCII1, Syspara2.flash_stPara.ubRsASSCII.CustomASCII1, sizeof(MC_RsASSCII.CustomASCII1));
  272. memcpy(MC_RsASSCII.CustomASCII2, Syspara2.flash_stPara.ubRsASSCII.CustomASCII2, sizeof(MC_RsASSCII.CustomASCII2));
  273. memcpy(MC_RsASSCII.CustomASCII3, Syspara2.flash_stPara.ubRsASSCII.CustomASCII3, sizeof(MC_RsASSCII.CustomASCII3));
  274. //控制参数
  275. memcpy(&MC_UpcInfo.stTestParaInfo.uwTestParaSaveFlg, &Syspara2.flash_stPara.stTestParaInfo.uwTestParaSaveFlg, sizeof(MC_UpcInfo.stTestParaInfo));
  276. BMS_VoltEstimat.uwInterResistpu = ((ULONG)1700 << 15) / cof_uwRbOm; //Q15 0.1mOhm BMS internal resistance 150mOhm + 20mOhm
  277. mth_voLPFilterCoef(1000000 / 100, EVENT_1MS_HZ, &BMS_swCurIdcLpf.uwKx); //100Hz
  278. uwRemainDistanceCal = 0xffff; // init invalid value
  279. }
  280. void Can_voMC_Run_1ms(void)
  281. {
  282. static UWORD time_s=0;
  283. if (cp_stBikeRunInfoPara.BikeSpeedKmH > 30)
  284. {
  285. cp_stBikeRunInfoPara.ulRiTime++;
  286. }
  287. else
  288. {}
  289. if(++time_s>60000)
  290. {
  291. cp_stHistoryPara.ulUsedTime++;
  292. time_s=0;
  293. }
  294. // Error Cnt record and Error Display Set
  295. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms ++;
  296. UART_RxBuff_Struct_OBC.ul_UartComTimeOutCount++;
  297. UART_RxBuff_Struct_BMS.ul_UartComTimeOutCount++;
  298. if(MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms == ALAM_DETECT_CNT_IN_1ms && MC_ErrorCode.Code == 0)
  299. {
  300. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  301. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  302. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  303. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  304. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  305. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  306. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms = 0;
  307. }
  308. else
  309. {}
  310. //故障日志获取故障状态
  311. if ((alm_blAlmOccrFlg == TRUE) || (alm_blWarnOccrFlg == TRUE))
  312. {
  313. // if (alm_blAlmSingleRecordDoneFlg == FALSE)//
  314. {
  315. if ((alm_unCode.bit.IPMFlt == 1) && (MC_ErrorCode.ERROR_Bit.Protect_OverCurrent == 0)) //硬件过流
  316. {
  317. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  318. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  319. {
  320. if(MC_ErrorCode.ERROR_Bit.Protect_OverCurrent==0)
  321. {
  322. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  323. cp_stHistoryPara.uwAlamHOcurTimes++;
  324. /*error log updata*/
  325. ErrorLog_Updata();
  326. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  327. }
  328. }
  329. }
  330. if ((alm_unCode.bit.OvrCur == 1) && (MC_ErrorCode.ERROR_Bit.Protect_OverCurrent == 0))//软件过流
  331. {
  332. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  333. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  334. {
  335. if(MC_ErrorCode.ERROR_Bit.Protect_OverCurrent==0)
  336. {
  337. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  338. cp_stHistoryPara.uwAlamSOcurTimes++;
  339. /*error log updata*/
  340. ErrorLog_Updata();
  341. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  342. }
  343. }
  344. }
  345. if ((alm_unCode.bit.OvrVlt == 1) && (MC_ErrorCode.ERROR_Bit.Protect_OverVoltage == 0))//过压
  346. {
  347. MC_ErrorCntRecord.Protect_OverVoltageCnt++;
  348. if (MC_ErrorCntRecord.Protect_OverVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  349. {
  350. if(MC_ErrorCode.ERROR_Bit.Protect_OverVoltage==0)
  351. {
  352. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 1;
  353. cp_stHistoryPara.uwAlamOVolTimes++;
  354. /*error log updata*/
  355. ErrorLog_Updata();
  356. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  357. }
  358. }
  359. }
  360. if ((alm_unCode.bit.UndrVlt == 1) && (MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage == 0))//欠压
  361. {
  362. MC_ErrorCntRecord.Protect_UnderVoltageCnt++;
  363. if (MC_ErrorCntRecord.Protect_UnderVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  364. {
  365. if(MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage==0)
  366. {
  367. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 1;
  368. cp_stHistoryPara.uwAlamUVolTimes++;
  369. /*error log updata*/
  370. ErrorLog_Updata();
  371. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  372. }
  373. }
  374. }
  375. if ((alm_unCode.bit.RotorLock == 1) && (MC_ErrorCode.ERROR_Bit.Protect_LockRotor == 0))//堵转
  376. {
  377. MC_ErrorCntRecord.Protect_LockRotorCnt++;
  378. if (MC_ErrorCntRecord.Protect_LockRotorCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  379. {
  380. if(MC_ErrorCode.ERROR_Bit.Protect_LockRotor==0)
  381. {
  382. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 1;
  383. cp_stHistoryPara.uwAlamRotorLockTimes++;
  384. /*error log updata*/
  385. ErrorLog_Updata();
  386. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  387. }
  388. }
  389. }
  390. if ((alm_unCode.bit.IPMOvrHeat == 1) && (MC_ErrorCode.ERROR_Bit.Protect_OverTemp == 0))//PCB过热
  391. {
  392. MC_ErrorCntRecord.Protect_OverTempCnt++;
  393. if (MC_ErrorCntRecord.Protect_OverTempCnt == ALAM_DISPLAY_CNT_0LEVEL)
  394. {
  395. if(MC_ErrorCode.ERROR_Bit.Protect_OverTemp==0)
  396. {
  397. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 1;
  398. cp_stHistoryPara.uwAlamOHeatTimes++;
  399. /*error log updata*/
  400. ErrorLog_Updata();
  401. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  402. }
  403. }
  404. }
  405. if ((alm_unCode.bit.PhsLoss == 1) && (MC_ErrorCode.ERROR_Bit.Fault_PhaseLine == 0))//缺相
  406. {
  407. MC_ErrorCntRecord.Fault_PhaseLineCnt++;
  408. if (MC_ErrorCntRecord.Fault_PhaseLineCnt == ALAM_DISPLAY_CNT_0LEVEL)
  409. {
  410. if(MC_ErrorCode.ERROR_Bit.Fault_PhaseLine==0)
  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. }
  420. // if(alm_unCode.bit.CommOvrTm)
  421. // {
  422. // cp_stHistoryPara.uwAlamComOTimeTimes++;
  423. // }
  424. // if (alm_unCode.bit.SpiThetaFlt == 1) //位置传感器故障
  425. // {
  426. // MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 1;
  427. // /*error log updata*/
  428. // ErrorLog_Updata();
  429. // que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  430. // }
  431. if ((alm_unBikeCode.bit.BikeSpdSen == 1) && (MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor == 0))
  432. {
  433. MC_ErrorCntRecord.Fault_SpeedSensorCnt++;
  434. if (MC_ErrorCntRecord.Fault_SpeedSensorCnt == ALAM_DISPLAY_CNT_0LEVEL)
  435. {
  436. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 1;
  437. cp_stHistoryPara.uwBikeSpdSensorAlamTimes++;
  438. /*error log updata*/
  439. ErrorLog_Updata();
  440. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  441. }
  442. }
  443. if ((alm_unBikeCode.bit.CadenceSen == 1) && (MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor == 0))
  444. {
  445. MC_ErrorCntRecord.Fault_CadenceSensorCnt++;
  446. if (MC_ErrorCntRecord.Fault_CadenceSensorCnt == ALAM_DISPLAY_CNT_0LEVEL)
  447. {
  448. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 1;
  449. cp_stHistoryPara.uwCadSensorAlamTimes++;
  450. /*error log updata*/
  451. ErrorLog_Updata();
  452. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  453. }
  454. }
  455. if ((alm_unBikeCode.bit.PCBNTC == 1) && (MC_ErrorCode.ERROR_Bit.Fault_PCBNTC == 0))
  456. {
  457. MC_ErrorCntRecord.Fault_NtcSensorCnt++;
  458. if (MC_ErrorCntRecord.Fault_NtcSensorCnt == ALAM_DISPLAY_CNT_0LEVEL)
  459. {
  460. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 1;
  461. /*error log updata*/
  462. ErrorLog_Updata();
  463. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  464. }
  465. }
  466. // if (alm_unBikeCode.bit.Throttle == 1) //油门故障
  467. // {
  468. // MC_ErrorCode.ERROR_Bit.Fault_Throttle = 1;
  469. // /*error log updata*/
  470. // ErrorLog_Updata();
  471. // que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  472. // }
  473. if ((alm_unBikeCode.bit.TorqSen == 1) && (MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor == 0))
  474. {
  475. MC_ErrorCntRecord.Fault_TorqueSensorCnt++;
  476. if (MC_ErrorCntRecord.Fault_TorqueSensorCnt == ALAM_DISPLAY_CNT_0LEVEL)
  477. {
  478. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 1;
  479. cp_stHistoryPara.uwTorSensorAlamTimes++;
  480. /*error log updata*/
  481. ErrorLog_Updata();
  482. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  483. }
  484. }
  485. // 电机霍尔故障
  486. if (alm_unCode.bit.HallLoss == 1)
  487. {
  488. MC_ErrorCntRecord.Fault_HallSensorCnt++;
  489. if (MC_ErrorCntRecord.Fault_HallSensorCnt >= ALAM_DISPLAY_CNT_0LEVEL)
  490. {
  491. MC_ErrorCntRecord.Fault_HallSensorCnt = ALAM_DISPLAY_CNT_0LEVEL+1;
  492. if(MC_ErrorCode.ERROR_Bit.Fault_HallSensor == 0)
  493. {
  494. cp_stHistoryPara.uwPosSensorAlamTimes++;
  495. MC_ErrorCode.ERROR_Bit.Fault_HallSensor = 1;
  496. /*error log updata*/
  497. ErrorLog_Updata();
  498. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  499. }
  500. }
  501. }
  502. //电机温度过热
  503. if (alm_unCode.bit.MotorOvrHeat == 1)
  504. {
  505. MC_ErrorCntRecord.Protect_MotorOverTempCnt++;
  506. if (MC_ErrorCntRecord.Protect_MotorOverTempCnt >= ALAM_DISPLAY_CNT_0LEVEL)
  507. {
  508. MC_ErrorCntRecord.Protect_MotorOverTempCnt = ALAM_DISPLAY_CNT_0LEVEL+1;
  509. if(MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp == 0)
  510. {
  511. cp_stHistoryPara.uwAlamOHeatTimes++;
  512. MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp = 1;
  513. /*error log updata*/
  514. ErrorLog_Updata();
  515. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  516. }
  517. }
  518. }
  519. alm_blAlmSingleRecordDoneFlg = TRUE;
  520. }
  521. }
  522. // Claear error Display
  523. if (MC_ErrorCode.ERROR_Bit.Protect_OverCurrent == 1)
  524. {
  525. if (alm_unCode.bit.IPMFlt != 1 && alm_unCode.bit.OvrCur != 1)
  526. {
  527. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  528. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 0;
  529. }
  530. }
  531. if (MC_ErrorCode.ERROR_Bit.Protect_OverVoltage == 1)
  532. {
  533. if (alm_unCode.bit.OvrVlt != 1)
  534. {
  535. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  536. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 0;
  537. }
  538. }
  539. if (MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage == 1)
  540. {
  541. if (alm_unCode.bit.UndrVlt != 1)
  542. {
  543. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  544. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 0;
  545. }
  546. }
  547. if (MC_ErrorCode.ERROR_Bit.Protect_LockRotor == 1)
  548. {
  549. if (alm_unCode.bit.RotorLock != 1)
  550. {
  551. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  552. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 0;
  553. }
  554. }
  555. if (MC_ErrorCode.ERROR_Bit.Protect_OverTemp == 1)
  556. {
  557. if (alm_unCode.bit.IPMOvrHeat != 1)
  558. {
  559. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  560. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 0;
  561. }
  562. }
  563. if (MC_ErrorCode.ERROR_Bit.Fault_PhaseLine == 1)
  564. {
  565. if (alm_unCode.bit.PhsLoss != 1)
  566. {
  567. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  568. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 0;
  569. }
  570. }
  571. // if (MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor == 1)
  572. // {
  573. // if (alm_unCode.bit.SpiThetaFlt != 1)
  574. // {
  575. // MC_ErrorCntRecord.Fault_SpiPosSensorCnt = 0;
  576. // MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 0;
  577. // }
  578. // }
  579. if (MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor == 1)
  580. {
  581. if (alm_unBikeCode.bit.BikeSpdSen != 1)
  582. {
  583. MC_ErrorCntRecord.Fault_SpeedSensorCnt = 0;
  584. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 0;
  585. }
  586. }
  587. if (MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor == 1)
  588. {
  589. if (alm_unBikeCode.bit.CadenceSen != 1)
  590. {
  591. MC_ErrorCntRecord.Fault_CadenceSensorCnt = 0;
  592. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 0;
  593. }
  594. }
  595. if (MC_ErrorCode.ERROR_Bit.Fault_PCBNTC == 1)
  596. {
  597. if (alm_unBikeCode.bit.PCBNTC != 1)
  598. {
  599. MC_ErrorCntRecord.Fault_NtcSensorCnt = 0;
  600. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 0;
  601. }
  602. }
  603. if (MC_ErrorCode.ERROR_Bit.Fault_Throttle == 1)
  604. {
  605. if (alm_unBikeCode.bit.Throttle != 1)
  606. {
  607. MC_ErrorCntRecord.Fault_GasSensorCnt = 0;
  608. MC_ErrorCode.ERROR_Bit.Fault_Throttle = 0;
  609. }
  610. }
  611. if (MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor == 1)
  612. {
  613. if (alm_unBikeCode.bit.TorqSen != 1)
  614. {
  615. MC_ErrorCntRecord.Fault_TorqueSensorCnt = 0;
  616. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 0;
  617. }
  618. }
  619. if (MC_ErrorCode.ERROR_Bit.Fault_HallSensor == 1)
  620. {
  621. if (alm_unCode.bit.HallLoss != 1)
  622. {
  623. MC_ErrorCntRecord.Fault_HallSensorCnt = 0;
  624. MC_ErrorCode.ERROR_Bit.Fault_HallSensor = 0;
  625. }
  626. }
  627. if (MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp == 1)
  628. {
  629. if (alm_unCode.bit.MotorOvrHeat != 1)
  630. {
  631. MC_ErrorCntRecord.Protect_MotorOverTempCnt = 0;
  632. MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp = 0;
  633. }
  634. }
  635. SWORD TempPower;
  636. if (FSM2nd_Run_state.state == Exit)
  637. {
  638. TempPower = 0;
  639. }
  640. else
  641. {
  642. TempPower = scm_stMotoPwrInLpf.slY.sw.hi;
  643. }
  644. BMS_VoltEstimat.swIdcPu = (SWORD)(((SLONG)TempPower << 13) / (SLONG)adc_stUpOut.uwVdcLpfPu); // Q15+Q13-Q14=Q14 Calculated dc current
  645. if (BMS_VoltEstimat.swIdcPu < 0)
  646. {
  647. BMS_VoltEstimat.swIdcPu = 0;
  648. }
  649. mth_voLPFilter(BMS_VoltEstimat.swIdcPu, &BMS_swCurIdcLpf);
  650. BMS_VoltEstimat.uwVdcCompPu = (ULONG)BMS_swCurIdcLpf.slY.sw.hi * BMS_VoltEstimat.uwInterResistpu >> 15; //Q14+Q15-Q15=Q14
  651. }
  652. void Can_voMC_Run_5ms(void)
  653. {
  654. cp_stBikeRunInfoPara.BikeSpeedKmH =
  655. (((SQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * FBASE * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * 36 >> 20) * 1048 * 10) >>20; // 1048 = Q20(1/1000) 0.1 km/h
  656. MC_RunInfo.BusVoltage = ((SLONG)adc_stUpOut.uwVdcPu * cof_uwUbVt >> 14) * 100;
  657. MC_RunInfo.MotorSpeed = (SLONG)scm_uwSpdFbkLpfAbsPu * cof_uwVbRpm >> 15;
  658. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH;//cp_stBikeRunInfoPara.BikeSpeedKmH; // MC_RunInfo.MotorSpeed / 10 ;
  659. }
  660. void Can_voMC_Run_200ms(void)
  661. {
  662. // UWORD TempPower;
  663. // 电池通讯异常采用控制器计算电量
  664. if((cp_ulSystickCnt - ulBMS_ComTimeOutCount) > 3000)
  665. {
  666. blBMSCommFault = TRUE; // communication abnormal
  667. }
  668. else
  669. {
  670. blBMSCommFault = FALSE; // communication normal
  671. }
  672. Can_MaxMinTempHistory();
  673. Can_GearSt_switch();
  674. SizeMCUP = sizeof(MC_UpcInfo.stAssistInfo);
  675. if (MC_BC_COM == 1)
  676. {
  677. MC_RunInfoToCDL.SysStatus = FSM1st_Sys_state.state;
  678. MC_RunInfoToCDL.SubStatus = FSM2nd_Run_state.state;
  679. MC_RunInfoToCDL.MotorStatus = curSpeed_state.state;
  680. MC_RunInfoToCDL.AssitStatus = Ass_FSM;
  681. MC_RunInfoToCDL.Id = scm_swIdRefPu;
  682. MC_RunInfoToCDL.Iq = scm_swIqRefPu;
  683. MC_RunInfoToCDL.Ud = scm_swUdRefPu;
  684. MC_RunInfoToCDL.Uq = scm_swUqRefPu;
  685. MC_RunInfoToCDL.InstantTorq = torsensor_stTorSensorOut.uwTorqueReg;
  686. MC_RunInfoToCDL.FilteTorq = torsensor_stTorSensorOut.uwTorqueLPFPu;
  687. MC_RunInfoToCDL.FreqTorq = maf_torque.AverValue;
  688. MC_RunInfoToCDL.RS[0] = scm_swIdFdbLpfPu;
  689. MC_RunInfoToCDL.RS[1] = scm_swIqFdbLpfPu;
  690. MC_RunInfoToCDL.RS[2] = Assist_torqueper;
  691. MC_RunInfoToCDL.RS[3] = bikethrottle_stBikeThrottleOut.uwThrottlePercent;
  692. SendData(&UART_TxBuff_Struct_OBC, ID_MC_TO_CDL, MODE_REPORT, 0xBA20, (UBYTE *)&MC_RunInfoToCDL.SysStatus);
  693. }
  694. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH; ///>车速 0.1km/h,地址偏移0
  695. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  696. {
  697. MC_RunInfo.MotorSpeed = (SLONG)abs(switchhall_stOut.swLowSpdLpfPu) * cof_uwVbRpm >> 15; ///>输出转速 1rpm,地址偏移2 if(scm_swMotorPwrInLpfWt > 5000)
  698. }
  699. else
  700. {
  701. MC_RunInfo.MotorSpeed = (SLONG)abs(scm_stSpdFbkLpf.slY.sw.hi)* cof_uwVbRpm >> 15;
  702. }
  703. if(scm_swMotorPwrInLpfWt > 5000)
  704. {
  705. scm_swMotorPwrInLpfWt = 5000;
  706. }
  707. if(scm_swMotorPwrInLpfWt < 0 )
  708. {
  709. scm_swMotorPwrInLpfWt = 0;
  710. }
  711. if(hw_blPWMOnFlg == FALSE)
  712. {
  713. scm_swMotorPwrInLpfWt = 0;
  714. }
  715. else
  716. {
  717. }
  718. Power_Cal();
  719. MC_RunInfo.Power= Powercal.PowerPoit;
  720. //MC_RunInfo.Power = scm_swMotorPwrInLpfWt / 10; ///>电功率 1W,地址偏移4
  721. MC_RunInfo.BusVoltage = ((ULONG)adc_stUpOut.uwVdcLpfPu * cof_uwUbVt * 100) >> 14; ///>母线电压 1mV,地址偏移6
  722. // MC_RunInfo.BusVoltage = ((ULONG)(adc_stUpOut.uwVdcLpfPu + BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14; ///>母线电压 1mV,地址偏移6
  723. // if (scm_stMotoPwrInLpf.slY.sw.hi < 10)
  724. // {
  725. // TempPower = 0;
  726. // }
  727. // else
  728. // {
  729. // TempPower = scm_stMotoPwrInLpf.slY.sw.hi;
  730. // }
  731. // MC_RunInfo.BusCurrent = (((ULONG)TempPower << 13) / adc_stUpOut.uwVdcLpfPu) * cof_uwIbAp * 100 >> 14; ///>母线电流 1mA,地址偏移8
  732. MC_RunInfo.BusCurrent = ((ULONG)adc_stUpOut.uwIbusAvgLpfPu) * cof_uwIbAp * 10 >> 14; ///14 >母线电流 1mA,地址偏移8
  733. MC_RunInfo.Cadence = (cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * 60) >> 20; ///>踏频 1rpm,地址偏移10
  734. MC_RunInfo.Torque = ((ULONG)torsensor_stTorSensorOut.uwTorqueLPFPu * cof_uwTorqNm / 10) >> 14; ///>踩踏力矩 1Nm,地址偏移11
  735. MC_RunInfo.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir; ///>踩踏方向 0-正,1-反,2-停止,地址偏移12
  736. MC_RunInfo.GearSt = MC_ControlCode.GearSt; ///>助力档位,地址偏移13
  737. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; ///>灯开关 0xF0-关,0xF1-开,地址偏移14
  738. MC_RunInfo.SOC = Can_SOC_Cal(); ///>剩余电量 1%,地址偏移15
  739. if(hw_blPWMOnFlg == FALSE)
  740. {
  741. MC_RunInfo.BusCurrent=0;
  742. }
  743. // MC_RunInfo.ODO_Km = MC_RideLog.ODO_Km / 10; ///>总里程 1km,地址偏移18
  744. Can_Trip_Cal();
  745. if (cp_stBikeRunInfoPara.blGearStUpdate)
  746. {
  747. if(cp_stBikeRunInfoPara.uwBikeGear > 0 &&(cp_stBikeRunInfoPara.uwBikeGear <=5))
  748. {
  749. MC_RunInfo.PowerPerKm =
  750. *(&cp_stHistoryPara.uwG1AvgPwrConsumption + (cp_stBikeRunInfoPara.uwBikeGear - 1)) / 10; //>平均功耗 0.01Ah/km ,地址偏移20
  751. uwRemainDistanceCal = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
  752. }
  753. else
  754. {
  755. MC_RunInfo.PowerPerKm = 0;
  756. uwRemainDistanceCal = 0xffff; //invalid value
  757. }
  758. cp_stBikeRunInfoPara.BMSRestChargeLast = BMS_RunInfo.RC;
  759. cp_stBikeRunInfoPara.uwPowerPerKm = 0;
  760. cp_stBikeRunInfoPara.uwAvePowerPerKm = 0;
  761. cp_stBikeRunInfoPara.uwPowerPerKmSum = 0;
  762. memset(cp_stBikeRunInfoPara.uwPowerPerKmBuf, 0, sizeof(cp_stBikeRunInfoPara.uwPowerPerKmBuf));
  763. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  764. cp_stBikeRunInfoPara.uwCruisDis = 0;
  765. }
  766. if(blBMSCommFault == TRUE)
  767. {
  768. MC_RunInfo.RemainDistance = 0xeeee; /* no bms info*/
  769. }
  770. else
  771. {
  772. MC_RunInfo.RemainDistance = uwRemainDistanceCal;
  773. }
  774. MC_RunInfo.T_PCB = adc_stUpOut.PCBTemp + 40; ///>PCB温度 +40℃,地址偏移21
  775. MC_RunInfo.T_Coil = adc_stUpOut.MotorTemp + 40; ///>绕组温度 +40℃,地址偏移22
  776. MC_RunInfo.T_MCU = adc_stUpOut.PCBTemp + 40; ///>MCU温度 +40℃,地址偏移23
  777. MC_RunInfo.Ride_Time = cp_stBikeRunInfoPara.ulRiTime >> 10; ///>开机后骑行时间 1s,地址偏移26
  778. MC_RunInfo.TorqueSensorData1=(UBYTE)torsensor_stTorSensorCof.uwTorqueOffsetNow1;
  779. MC_RunInfo.TorqueSensorData2=(UBYTE)torsensor_stTorSensorCof.uwTorqueOffsetNow2;
  780. MC_RunInfo.TorqueSensorData3=(UBYTE)torsensor_stTorSensorCof.uwTorqueOffsetNow3;
  781. static UBYTE test1 = 0;
  782. if (MC_WorkMode == 1)
  783. {
  784. #if(UART_ID==3)
  785. test1++;
  786. if(test1>=5)
  787. #else
  788. test1++;
  789. if(test1>=2)
  790. #endif
  791. {
  792. test1=0;
  793. SendData(&UART_TxBuff_Struct_OBC, ID_MC_BC, MODE_REPORT, 0x1020, (uint8_t *)&MC_RunInfo.BikeSpeed);
  794. }
  795. }
  796. }
  797. void Can_Trip_Cal(void)
  798. {
  799. if (bikespeed_stFreGetOut.blUpdateTripCntFlg == TRUE)
  800. {
  801. MC_RunInfo.Ride_Km++;
  802. cp_stHistoryPara.ulODOTrip++; // Unit: 0.1km
  803. cp_stHistoryPara.ulRealODOTrip++; // Unit: 0.1km
  804. cp_stHistoryPara.ulTripSum++;
  805. Can_RemainTrip_Cal();
  806. bikespeed_stFreGetOut.blUpdateTripCntFlg = FALSE;
  807. }
  808. MC_RideLog.ODO_Km = cp_stHistoryPara.ulODOTrip;
  809. MC_RideLog.TRIP_Km = cp_stHistoryPara.ulTripSum;
  810. MC_RideLog.ODO_Time = cp_stHistoryPara.ulODOTime + (cp_stBikeRunInfoPara.ulRiTime >> 10) / 60; // min
  811. MC_RideLog.TRIP_Time = cp_stHistoryPara.ulTripSumTime + (cp_stBikeRunInfoPara.ulRiTirpTime >> 10) / 60; // min
  812. }
  813. void Can_RemainTrip_Cal(void)
  814. {
  815. if(blBMSCommFault == FALSE)
  816. {
  817. UWORD uwCruisCoef; //功耗系数 Q12
  818. cp_stBikeRunInfoPara.uwCruisDis++; // 0.1 km
  819. if (cp_stBikeRunInfoPara.uwCruisDis >= 5) // 5 = 0.5km
  820. {
  821. cp_stBikeRunInfoPara.uwCruisDis = 0;
  822. cp_stBikeRunInfoPara.BMSRestChargeNow = BMS_RunInfo.RC;
  823. cp_stBikeRunInfoPara.uwPowerPerKm = (cp_stBikeRunInfoPara.BMSRestChargeLast - cp_stBikeRunInfoPara.BMSRestChargeNow)
  824. << 1; // BMS_RunInfo.RC (BMSRestChargeLast - BMSRestChargeNow) /0.5 km
  825. cp_stBikeRunInfoPara.BMSRestChargeLast = cp_stBikeRunInfoPara.BMSRestChargeNow;
  826. cp_stBikeRunInfoPara.uwPowerPerKmSum -= cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  827. cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT] = cp_stBikeRunInfoPara.uwPowerPerKm;
  828. cp_stBikeRunInfoPara.uwPowerPerKmSum += cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  829. if (cp_stBikeRunInfoPara.uwPowerPerKmBuf[31] == 0)
  830. {
  831. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum / (cp_stBikeRunInfoPara.uwAvePowerCNT + 1);
  832. }
  833. else
  834. {
  835. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum >> 5;
  836. }
  837. cp_stBikeRunInfoPara.uwAvePowerCNT++;
  838. if (cp_stBikeRunInfoPara.uwAvePowerCNT >= 32)
  839. {
  840. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  841. }
  842. if(cp_stBikeRunInfoPara.uwAvePowerPerKm > 0)
  843. {
  844. if (cp_stBikeRunInfoPara.uwBikeGear == 1)
  845. {
  846. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG1AvgPwrConsumption;
  847. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  848. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  849. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  850. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  851. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  852. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG1AvgPwrConsumption / 10;
  853. }
  854. else if (cp_stBikeRunInfoPara.uwBikeGear == 2)
  855. {
  856. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG2AvgPwrConsumption;
  857. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  858. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  859. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  860. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  861. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  862. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG2AvgPwrConsumption / 10;
  863. }
  864. else if (cp_stBikeRunInfoPara.uwBikeGear == 3)
  865. {
  866. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG3AvgPwrConsumption;
  867. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  868. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  869. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  870. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  871. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  872. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG3AvgPwrConsumption / 10;
  873. }
  874. else if (cp_stBikeRunInfoPara.uwBikeGear == 4)
  875. {
  876. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG4AvgPwrConsumption;
  877. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  878. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  879. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  880. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  881. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  882. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG4AvgPwrConsumption / 10;
  883. }
  884. else if (cp_stBikeRunInfoPara.uwBikeGear == 5)
  885. {
  886. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG5AvgPwrConsumption;
  887. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  888. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  889. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  890. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  891. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  892. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG5AvgPwrConsumption / 10;
  893. }
  894. else
  895. {
  896. uwCruisCoef = 4096;
  897. }
  898. uwRemainDistanceCal = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
  899. }
  900. }
  901. }
  902. }
  903. #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
  904. SLONG slBMSMaxVol = 54000;
  905. SLONG slBMSMinVol = 40500;
  906. #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
  907. SLONG slBMSMaxVol = 41500;
  908. SLONG slBMSMinVol = 31000;
  909. #else
  910. SLONG slBMSMaxVol = 41500;
  911. SLONG slBMSMinVol = 31000;
  912. #endif
  913. SWORD SOC_Cnt, SOC_Value;
  914. SWORD SOC_MIN=50;
  915. SLONG Voltage_Sum;
  916. static BOOL blSOCfirstSet = FALSE;
  917. UWORD Can_SOC_Cal(void)
  918. {
  919. UWORD templenght, VoltageAvg = 0;
  920. // 开机1s等待电压稳定后初始化电量
  921. if (cp_ulSystickCnt < 500)
  922. {
  923. return (UWORD)0;
  924. }
  925. if(blBMSCommFault == FALSE)
  926. {
  927. SOC_Value = BMS_RunInfo.SOC;
  928. if((blSOCfirstSet == FALSE) && ( ulBMS_ComTimeOutCount !=0) )// 开机初始化一次电量
  929. {
  930. SOC_MIN = BMS_RunInfo.SOC;
  931. blSOCfirstSet = TRUE;
  932. }
  933. }
  934. else if(blSOCfirstSet == FALSE)// 开机初始化一次电量
  935. {
  936. if(0 == ulBMS_ComTimeOutCount)
  937. {
  938. if (MC_RunInfo.BusVoltage >= slBMSMaxVol)
  939. {
  940. SOC_Value = 100;
  941. }
  942. else if (MC_RunInfo.BusVoltage <= slBMSMinVol)
  943. {
  944. SOC_Value = 0;
  945. }
  946. else
  947. {
  948. SOC_Value = (SWORD)(((SLONG)MC_RunInfo.BusVoltage - slBMSMinVol) * 100 / (slBMSMaxVol - slBMSMinVol));
  949. }
  950. SOC_MIN = SOC_Value;
  951. }
  952. else
  953. {
  954. SOC_MIN = BMS_RunInfo.SOC;
  955. }
  956. blSOCfirstSet = TRUE;
  957. }
  958. // 1min更新一次电量
  959. else
  960. {
  961. templenght = 300; // 60s in 200ms time task
  962. SOC_Cnt++;
  963. Voltage_Sum += MC_RunInfo.BusVoltage;
  964. if (SOC_Cnt >= templenght) // 60s
  965. {
  966. VoltageAvg = (UWORD)(Voltage_Sum / templenght);
  967. if (VoltageAvg <= slBMSMinVol)
  968. {
  969. SOC_Value = 0;
  970. }
  971. else
  972. {
  973. SOC_Value = (SWORD)(((SLONG)VoltageAvg - (SLONG)slBMSMinVol) * 100 / ((SLONG)slBMSMaxVol - (SLONG)slBMSMinVol));
  974. }
  975. if (SOC_Value < SOC_MIN)
  976. {
  977. SOC_MIN = SOC_Value;
  978. }
  979. else
  980. {
  981. }
  982. SOC_Cnt = 0;
  983. Voltage_Sum = 0;
  984. }
  985. // 电量仅递减
  986. if (SOC_Value > SOC_MIN)
  987. {
  988. SOC_Value = SOC_MIN;
  989. }
  990. else if (SOC_Value < 0)
  991. {
  992. SOC_Value = 0;
  993. }
  994. else
  995. {
  996. // do noting
  997. }
  998. }
  999. return (UWORD)SOC_Value;
  1000. }
  1001. void Can_MaxMinTempHistory(void)
  1002. {
  1003. if (adc_stUpOut.PCBTemp > ((SWORD)cp_stHistoryPara.swNTCTempMaxCe))
  1004. {
  1005. cp_stHistoryPara.swNTCTempMaxCe = adc_stUpOut.PCBTemp;
  1006. }
  1007. else
  1008. {}
  1009. if (adc_stUpOut.PCBTemp < ((SWORD)cp_stHistoryPara.swNTCTempMinCe))
  1010. {
  1011. cp_stHistoryPara.swNTCTempMinCe = adc_stUpOut.PCBTemp;
  1012. }
  1013. else
  1014. {}
  1015. }
  1016. void Can_GearSt_switch(void)
  1017. {
  1018. cp_stBikeRunInfoPara.uwBikeGearLast = cp_stBikeRunInfoPara.uwBikeGear;
  1019. if (MC_WorkMode == 1) // 配置模式不自动关闭助力
  1020. {
  1021. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  1022. }
  1023. if ((cp_ulSystickCnt - ulOBC_ComTimeOutCount) < 3000) // 控制器与仪表通信中断超过3s,关闭助力
  1024. {
  1025. if (MC_ControlCode.GearSt <= 0x05)
  1026. {
  1027. cp_stBikeRunInfoPara.uwBikeGear = MC_ControlCode.GearSt;
  1028. }
  1029. else if (MC_ControlCode.GearSt == 0x33)
  1030. {
  1031. cp_stBikeRunInfoPara.uwBikeGear = 5;
  1032. }
  1033. else if(MC_ControlCode.GearSt == 0x22)
  1034. {
  1035. cp_stBikeRunInfoPara.uwBikeGear = 0x22;
  1036. }
  1037. else
  1038. {
  1039. cp_stBikeRunInfoPara.uwBikeGear = 0;
  1040. }
  1041. }
  1042. #if(BIKE_OXFORD_EN==0)
  1043. else
  1044. {
  1045. MC_ControlCode.GearSt = MC_GearSt_OFF;
  1046. MC_RunInfo.GearSt = 0x00;
  1047. cp_stBikeRunInfoPara.uwBikeGear = 0x00;
  1048. }
  1049. #endif
  1050. if (cp_stBikeRunInfoPara.uwBikeGear != cp_stBikeRunInfoPara.uwBikeGearLast)
  1051. {
  1052. cp_stBikeRunInfoPara.blGearStUpdate = TRUE;
  1053. }
  1054. else
  1055. {
  1056. cp_stBikeRunInfoPara.blGearStUpdate = FALSE;
  1057. }
  1058. }
  1059. void Can_Light_switch(void)
  1060. {
  1061. /* light switch*/
  1062. if (MC_ControlCode.LightSwitch == 0xF0)
  1063. {
  1064. cp_stBikeRunInfoPara.uwLightSwitch = 0;
  1065. }
  1066. else if (MC_ControlCode.LightSwitch == 0xF1)
  1067. {
  1068. cp_stBikeRunInfoPara.uwLightSwitch = 1;
  1069. }
  1070. }
  1071. void Can_AssistCoef_Read(UPC_CurveOrderInfo_Struct_t *order)
  1072. {
  1073. MC_UpcInfo.stAssistInfo.stCurveOrderInfo = *order;
  1074. memcpy(&MC_UpcInfo.stAssistInfo.slTorquePolyA, &Syspara2.flash_stPara.slTorqAssGain[order->uwTorqueCurveNum - 1], sizeof(POLY_COEF));
  1075. memcpy(&MC_UpcInfo.stAssistInfo.slCadencePolyA, &Syspara2.flash_stPara.slCadAssGain[order->uwCadenceCurveNum - 1], sizeof(POLY_COEF));
  1076. }
  1077. void Can_AssistCoef_Write(UPC_CurveOrderInfo_Struct_t *order)
  1078. {
  1079. memcpy(&Syspara2.flash_stPara.slTorqAssGain[order->uwTorqueCurveNum - 1], &MC_UpcInfo.stAssistInfo.slTorquePolyA, sizeof(POLY_COEF));
  1080. memcpy(&Syspara2.flash_stPara.slCadAssGain[order->uwCadenceCurveNum - 1], &MC_UpcInfo.stAssistInfo.slCadencePolyA, sizeof(POLY_COEF));
  1081. }
  1082. void ErrorLog_Updata(void)
  1083. {
  1084. que_stErrorLog.ErrorCode = MC_ErrorCode.Code;
  1085. que_stErrorLog.RunTime = cp_stHistoryPara.ulUsedTime;//MC_RunInfo.Ride_Time;
  1086. que_stErrorLog.RunInfo = MC_RunInfo;
  1087. //memcpy((uint8_t*)&que_stErrorLog.RunInfo, (uint8_t*)&MC_RunInfo.BikeSpeed, sizeof(MC_RunInfo_Struct_t));
  1088. que_stErrorLog.IqCurrentPu = scm_swIqFdbLpfPu;
  1089. que_stErrorLog.IqVoltagePu = scm_swUqRefPu;
  1090. que_stErrorLog.IdCurrentPu = scm_swIdFdbLpfPu;
  1091. que_stErrorLog.IdVoltagePu = scm_swUdRefPu;
  1092. }
  1093. void Power_Cal(void)
  1094. {
  1095. SWORD V_temp;
  1096. UWORD Poit_temp;
  1097. UWORD Power_temp;
  1098. SLONG Power_clac;
  1099. Poit_temp= (cp_stMotorPara.swRPwrWt)>>11;
  1100. Power_temp=cp_stMotorPara.swRPwrWt & 0x07ff;
  1101. if(cp_stControlPara.swPwrLimitValWt>700)
  1102. {
  1103. Powercal.swMaxpower=cp_stControlPara.swPwrLimitValWt-600;
  1104. }
  1105. V_temp=MC_RunInfo.BusVoltage/100;
  1106. Powercal.swPowerFdb=(SWORD) ((SLONG)(V_temp) * (SLONG)MC_RunInfo.BusCurrent/1000);
  1107. if(Poit_temp==0)
  1108. {
  1109. Powercal.PowerPoit=(SWORD)((SLONG)Powercal.swPowerFdb*250/((SLONG)Powercal.swMaxpower));
  1110. if(Powercal.PowerPoit > 250)
  1111. {
  1112. Powercal.PowerPoit = 250;
  1113. }
  1114. else if(Powercal.PowerPoit < 0 )
  1115. {
  1116. Powercal.PowerPoit = 0;
  1117. }
  1118. }
  1119. else
  1120. {
  1121. Power_clac=(SLONG)Powercal.swPowerFdb*(Poit_temp+1) ;
  1122. Powercal.PowerPoit=(SWORD)((Power_clac/10) >>5);
  1123. }
  1124. if(hw_blPWMOnFlg == FALSE)
  1125. {
  1126. Powercal.PowerPoit = 0;
  1127. }
  1128. }