canAppl.c 52 KB

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