canAppl.c 55 KB

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