canAppl.c 44 KB

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