canAppl.c 45 KB

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