canAppl.c 43 KB

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