canAppl.c 53 KB

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