canAppl.c 44 KB

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