canAppl.c 43 KB

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