canAppl.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. #include "canAppl.h"
  12. #include "CodePara.h"
  13. #include "FuncLayerAPI.h"
  14. #include "adc.h"
  15. #include "alarm.h"
  16. #include "usart.h"
  17. #include "can.h"
  18. #include "flash_master.h"
  19. #include "glbcof.h"
  20. #include "spdctrmode.h"
  21. #include "string.h"
  22. #include "syspar.h"
  23. #include "AssistCurve.h"
  24. #include "hwsetup.h"
  25. #include "spi_master.h"
  26. #include "fp.def"
  27. #include "CadAssist.h"
  28. #include "bikeinformation.h"
  29. #include "FSM_1st.h"
  30. #include "FSM_2nd.h"
  31. /******************************
  32. *
  33. * Parameter
  34. *
  35. ******************************/
  36. typedef _Bool BOOL;
  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. CONTORLFUN_STRUCT OBC_ControlFUN;
  51. MC_Error_Address_Struct ManageError_Address;
  52. OBC_SetCustomPara_Struct_t OBC_SetCustomPara;
  53. OBC_ButtonStatus_Struct_t OBC_ButtonStatus = {0,0,0,FALSE};
  54. ULONG ulOBC_ComTimeOutCount = 0;
  55. ULONG ulBMS_ComTimeOutCount = 0;
  56. uint8_t MC_MotorSPD_rpm_Percent = 0;
  57. uint8_t MC_WorkMode;
  58. uint8_t MC_BC_COM = 0;
  59. LPF_OUT BMS_swCurIdcLpf;
  60. BOOL blBMSCommFault = FALSE;
  61. static UWORD uwRemainDistanceCal;
  62. POWER_STRUCT Powercal;
  63. void Power_Cal(void);
  64. void Can_voUpdateMC_UpcInfo(void)
  65. {
  66. MC_UpcInfo.stMotorInfo.uwPolePairs = cp_stMotorPara.swMotrPolePairs;
  67. MC_UpcInfo.stMotorInfo.uwRsmOhm = cp_stMotorPara.swRsOhm;
  68. MC_UpcInfo.stMotorInfo.uwLduH = cp_stMotorPara.uwLdmH;
  69. MC_UpcInfo.stMotorInfo.uwLquH = cp_stMotorPara.uwLqmH;
  70. MC_UpcInfo.stMotorInfo.uwFluxmWb = cp_stMotorPara.swFluxWb;
  71. MC_UpcInfo.stMotorInfo.uwIdMaxA = cp_stMotorPara.swIdMaxA;
  72. MC_UpcInfo.stMotorInfo.uwIdMinA = cp_stMotorPara.swIdMinA;
  73. MC_UpcInfo.stMotorInfo.uwRSpdRpm = cp_stMotorPara.swRSpeedRpm;
  74. MC_UpcInfo.stMotorInfo.uwRPwrWt = cp_stMotorPara.swRPwrWt;
  75. MC_UpcInfo.stMotorInfo.uwRCurA = cp_stMotorPara.swRIarmsA;
  76. MC_UpcInfo.stMotorInfo.uwRVolV = cp_stMotorPara.swRUdcV;
  77. MC_UpcInfo.stMotorInfo.uwJD = cp_stMotorPara.swJD;
  78. MC_UpcInfo.stMotorInfo.uwTorMaxNm = cp_stMotorPara.swTorMax;
  79. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = ass_ParaCong.uwWheelPerimeter;
  80. MC_UpcInfo.stBikeInfo.uwMechRationMotor = ass_ParaCong.uwMechRationMotorEEPROM;//(UWORD)(((ULONG)ass_ParaCong.uwMechRationMotor*1000)/1024);
  81. MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = ass_ParaCong.uwThrottleMaxSpdKmH;
  82. MC_UpcInfo.stBikeInfo.uwCartSpdKmH = ass_ParaCong.uwCartSpdKmH;
  83. MC_UpcInfo.stBikeInfo.uwNmFrontChainring = ass_ParaCong.uwNmFrontChainring;
  84. MC_UpcInfo.stBikeInfo.uwNmBackChainring = ass_ParaCong.uwNmBackChainring;
  85. MC_UpcInfo.stBikeInfo.uwAssistSelect1 = ass_ParaCong.uwAssistSelect1;
  86. MC_UpcInfo.stBikeInfo.uwAssistSelect2 = ass_ParaCong.uwAssistSelect2;
  87. MC_UpcInfo.stBikeInfo.uwLightConfig = ass_ParaCong.uwLightConfig;
  88. MC_UpcInfo.stBikeInfo.swWheelSizeAdjust = ass_ParaCong.swDeltPerimeter;
  89. MC_UpcInfo.stBikeInfo.uwStartMode = ass_ParaCong.uwStartMode;
  90. MC_UpcInfo.stBikeInfo.uwAutoPowerOffTime = ass_ParaCong.uwAutoPowerOffTime;
  91. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetOrigin =0;// spi_stResolverOut.swSpiThetaOffsetOrignPu;
  92. MC_UpcInfo.stMContorlInfo.uwSPIPosOffsetNow = 0;//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.swAlmOverVolVal1;
  96. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = cp_stControlPara.swAlmUnderVolVal1;
  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.stMContorlInfo.uwAlamMotorOverHeatCe = cp_stControlPara.swAlmMotorOverHeatCeVal;
  102. MC_UpcInfo.stMContorlInfo.uwAlamMotorRecHeatCe = cp_stControlPara.swAlmMotorRecOHeatVal;
  103. MC_UpcInfo.stMContorlInfo.uwPwrLimitMotorStartCe = cp_stControlPara.swAlmPwrLimitMotorStartTempVal;
  104. MC_UpcInfo.stMContorlInfo.uwControlFunEN= cp_stControlPara.uwControlFunEN;
  105. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetOrigin = torsensor_stTorSensorCof.uwTorqueOffsetOrign;
  106. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow1 = torsensor_stTorSensorCof.uwTorqueOffsetNow1;
  107. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow2 = torsensor_stTorSensorCof.uwTorqueOffsetNow2;
  108. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow3 = torsensor_stTorSensorCof.uwTorqueOffsetNow3;
  109. MC_UpcInfo.stSensorInfo.uwTorSensorOffsetNow4 = torsensor_stTorSensorCof.uwTorqueOffsetNow4;
  110. MC_UpcInfo.stSensorInfo.uwBikeTorMaxNm = torsensor_stTorSensorCof.uwMaxSensorTorquePu;
  111. MC_UpcInfo.stSensorInfo.uwBikeTor1StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep1RealNm;
  112. MC_UpcInfo.stSensorInfo.uwBikeTor1StepADC = torsensor_stTorSensorCof.uwBikeTorStep1ADC;
  113. MC_UpcInfo.stSensorInfo.uwBikeTor2StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep2RealNm;
  114. MC_UpcInfo.stSensorInfo.uwBikeTor2StepADC = torsensor_stTorSensorCof.uwBikeTorStep2ADC;
  115. MC_UpcInfo.stSensorInfo.uwBikeTor3StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep3RealNm;
  116. MC_UpcInfo.stSensorInfo.uwBikeTor3StepADC = torsensor_stTorSensorCof.uwBikeTorStep3ADC;
  117. MC_UpcInfo.stSensorInfo.uwBikeTor4StepRealNm = torsensor_stTorSensorCof.uwBikeTorStep4RealNm;
  118. MC_UpcInfo.stSensorInfo.uwBikeTor4StepADC = torsensor_stTorSensorCof.uwBikeTorStep4ADC;
  119. MC_UpcInfo.stSensorInfo.uwTorque_SensorPulseNm = cadence_stFreGetCof.uwTorque_NumbersPulses;
  120. MC_UpcInfo.stSensorInfo.uwBikeSpdSensorPulseNm = bikespeed_stFreGetCof.uwNumbersPulses;
  121. MC_UpcInfo.stSensorInfo.uwCad_SensorPulseNm= cadence_stFreGetCof.uwCad_NumbersPulses;
  122. MC_UpcInfo.stAssistInfo.swStartupGain = ass_ParaSet.uwStartupCoef;
  123. MC_UpcInfo.stAssistInfo.swStartcruiseGain = ass_ParaSet.uwStartupCruiseCoef;
  124. MC_UpcInfo.stAssistInfo.uwAssistStartNm = ass_ParaSet.uwAssistStartNm;
  125. MC_UpcInfo.stAssistInfo.uwAssistStopNm = ass_ParaSet.uwAssistStopNm;
  126. MC_UpcInfo.stAssistInfo.uwStartUpGainStep = ass_ParaSet.uwStartUpGainStep;
  127. MC_UpcInfo.stAssistInfo.uwStartUpCadNm = ass_ParaSet.uwStartUpCadNm;
  128. MC_UpcInfo.stAssistInfo.uwTorLPFCadNm = ass_ParaSet.uwTorLPFCadNm;
  129. MC_UpcInfo.stAssistInfo.uwSpeedAssistSpdRpm = ass_ParaSet.uwSpeedAssistSpdRpm;
  130. MC_UpcInfo.stAssistInfo.uwSpeedAssistIMaxA = ass_ParaSet.uwSpeedAssistIMaxA;
  131. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = ass_ParaSet.uwAssistLimitBikeSpdStart;
  132. MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = ass_ParaSet.uwAssistLimitBikeSpdStop;
  133. MC_UpcInfo.stAssistInfo.uwCadenceAssistWeight = ass_ParaSet.uwCadenceWeight;
  134. MC_UpcInfo.stAssistInfo.uwCadAssPidKp = ass_stCadAssSpdCtl.uwPidKp;
  135. MC_UpcInfo.stAssistInfo.swCadAssPidLimMax = ass_stCadAssSpdCtl.swPidLimMax;
  136. MC_UpcInfo.stAssistInfo.swCadAssPidVolDec = ass_stCadAssSpdCtl.swPidVolDec;
  137. MC_UpcInfo.stAssistInfo.swCadAssTargetAssCurAcc = ass_stCadAssParaPro.swTargetAssCurAcc;
  138. MC_UpcInfo.stAssistInfo.uwMaxCadRpm = ass_stCadAssCoef.uwMaxCadRpm;
  139. MC_UpcInfo.stAssistInfo.reserve2 = ass_stReservePara.uwReserve2;
  140. MC_UpcInfo.stAssistInfo.reserve3 = ass_stReservePara.uwReserve3;
  141. MC_UpcInfo.stAssistInfo.reserve4 = ass_stReservePara.uwReserve4;
  142. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain1 = ass_stCalCoef.ucAssistRatioGain[0];
  143. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain2 = ass_stCalCoef.ucAssistRatioGain[1];
  144. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain3 = ass_stCalCoef.ucAssistRatioGain[2];
  145. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain4 = ass_stCalCoef.ucAssistRatioGain[3];
  146. MC_UpcInfo.stRideParaInfo.ucAssistRatioGain5 = ass_stCalCoef.ucAssistRatioGain[4];
  147. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain1 = ass_stCalCoef.ucAssistAccelerationGain[0];
  148. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain2 = ass_stCalCoef.ucAssistAccelerationGain[1];
  149. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain3 = ass_stCalCoef.ucAssistAccelerationGain[2];
  150. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain4 = ass_stCalCoef.ucAssistAccelerationGain[3];
  151. MC_UpcInfo.stRideParaInfo.ucAssistAccelerationGain5 = ass_stCalCoef.ucAssistAccelerationGain[4];
  152. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain1 = ass_stCalCoef.ucMaxCurrentGain[0];
  153. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain2 = ass_stCalCoef.ucMaxCurrentGain[1];
  154. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain3 = ass_stCalCoef.ucMaxCurrentGain[2];
  155. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain4 = ass_stCalCoef.ucMaxCurrentGain[3];
  156. MC_UpcInfo.stRideParaInfo.ucMaxCurrentGain5 = ass_stCalCoef.ucMaxCurrentGain[4];
  157. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain1 = ass_stCalCoef.ucMaxTorqueGain[0];
  158. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain2 = ass_stCalCoef.ucMaxTorqueGain[1];
  159. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain3 = ass_stCalCoef.ucMaxTorqueGain[2];
  160. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain4 = ass_stCalCoef.ucMaxTorqueGain[3];
  161. MC_UpcInfo.stRideParaInfo.ucMaxTorqueGain5 = ass_stCalCoef.ucMaxTorqueGain[4];
  162. MC_UpcInfo.stBikeInfo2.uwNoneOBCEnable = ass_ParaCong.uwNoneOBCEnable;
  163. MC_UpcInfo.stBikeInfo2.uwRearLightCycle = ass_ParaCong.uwRearLightCycle;
  164. MC_UpcInfo.stBikeInfo2.uwRearLightDuty = ass_ParaCong.uwRearLightDuty;
  165. MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit = ass_ParaCong.swDeltaBikeSpeedLimit;
  166. MC_UpcInfo.stHistoryInfo.uwOpenTimes = cp_stHistoryPara.uwOpenTimes;
  167. MC_UpcInfo.stHistoryInfo.uwUsedTimeH = (UWORD)(cp_stHistoryPara.ulUsedTime >> 16);
  168. MC_UpcInfo.stHistoryInfo.uwUsedTimeL = (UWORD)(cp_stHistoryPara.ulUsedTime);
  169. MC_UpcInfo.stHistoryInfo.swNTCTempMaxCe = cp_stHistoryPara.swNTCTempMaxCe;
  170. MC_UpcInfo.stHistoryInfo.swNTCTempMinCe = cp_stHistoryPara.swNTCTempMinCe;
  171. ;
  172. MC_UpcInfo.stHistoryInfo.uwAlamHOcurTimes = cp_stHistoryPara.uwAlamHOcurTimes;
  173. MC_UpcInfo.stHistoryInfo.uwAlamSOcurTimes = cp_stHistoryPara.uwAlamSOcurTimes;
  174. MC_UpcInfo.stHistoryInfo.uwAlamOHeatTimes = cp_stHistoryPara.uwAlamOHeatTimes;
  175. MC_UpcInfo.stHistoryInfo.uwAlamRotorLockTimes = cp_stHistoryPara.uwAlamRotorLockTimes;
  176. MC_UpcInfo.stHistoryInfo.uwAlamPhsLossTimes = cp_stHistoryPara.uwAlamPhsLossTimes;
  177. MC_UpcInfo.stHistoryInfo.uwAlamOVolTimes = cp_stHistoryPara.uwAlamOVolTimes;
  178. MC_UpcInfo.stHistoryInfo.uwAlamUVolTimes = cp_stHistoryPara.uwAlamUVolTimes;
  179. MC_UpcInfo.stHistoryInfo.uwAlamComOTimeTimes = cp_stHistoryPara.uwAlamComOTimeTimes;
  180. MC_UpcInfo.stHistoryInfo.uwG1AvgPwrConsumption = cp_stHistoryPara.uwG1AvgPwrConsumption;
  181. MC_UpcInfo.stHistoryInfo.uwG2AvgPwrConsumption = cp_stHistoryPara.uwG2AvgPwrConsumption;
  182. MC_UpcInfo.stHistoryInfo.uwG3AvgPwrConsumption = cp_stHistoryPara.uwG3AvgPwrConsumption;
  183. MC_UpcInfo.stHistoryInfo.uwG4AvgPwrConsumption = cp_stHistoryPara.uwG4AvgPwrConsumption;
  184. MC_UpcInfo.stHistoryInfo.uwG5AvgPwrConsumption = cp_stHistoryPara.uwG5AvgPwrConsumption;
  185. MC_UpcInfo.stHistoryInfo.uwODOTripH = (UWORD)(cp_stHistoryPara.ulODOTrip >> 16);
  186. MC_UpcInfo.stHistoryInfo.uwODOTripL = (UWORD)cp_stHistoryPara.ulODOTrip;
  187. MC_UpcInfo.stHistoryInfo.uwODOTimeH = (UWORD)(cp_stHistoryPara.ulODOTime >> 16);
  188. MC_UpcInfo.stHistoryInfo.uwODOTimeL = (UWORD)cp_stHistoryPara.ulODOTime;
  189. MC_UpcInfo.stHistoryInfo.uwTripSumH = (UWORD)(cp_stHistoryPara.ulTripSum >> 16);
  190. MC_UpcInfo.stHistoryInfo.uwTripSumL = (UWORD)cp_stHistoryPara.ulTripSum;
  191. MC_UpcInfo.stHistoryInfo.uwTripSumTimeH = (UWORD)(cp_stHistoryPara.ulTripSumTime >> 16);
  192. MC_UpcInfo.stHistoryInfo.uwTripSumTimeL = (UWORD)cp_stHistoryPara.ulTripSumTime;
  193. MC_UpcInfo.stHistoryInfo.uwTorSensorAlamTimes = cp_stHistoryPara.uwTorSensorAlamTimes;
  194. MC_UpcInfo.stHistoryInfo.uwCadSensorAlamTimes = cp_stHistoryPara.uwCadSensorAlamTimes;
  195. MC_UpcInfo.stHistoryInfo.uwBikeSpdSensorAlamTimes = cp_stHistoryPara.uwBikeSpdSensorAlamTimes;
  196. MC_UpcInfo.stHistoryInfo.uwPosSensorAlamTimes = cp_stHistoryPara.uwPosSensorAlamTimes;
  197. MC_UpcInfo.stHistoryInfo.uwRealODOTripH = (UWORD)(cp_stHistoryPara.ulRealODOTrip >> 16);
  198. MC_UpcInfo.stHistoryInfo.uwRealODOTripL = (UWORD)cp_stHistoryPara.ulRealODOTrip;
  199. MC_UpcInfo.stHistoryInfo.uwRealODOTimeH = (UWORD)(cp_stHistoryPara.ulRealODOTime >> 16);
  200. MC_UpcInfo.stHistoryInfo.uwRealODOTimeL = (UWORD)cp_stHistoryPara.ulRealODOTime;
  201. MC_UpcInfo.stTestParaInfo.uwEEFirstDefaultSetFlg = cp_stFlg.ParaFirstSetFlg;
  202. // MC_UpcInfo.stTestParaInfo.uwSPIOffsetFirstSetFlg = cp_stFlg.SpiOffsetFirstSetFlg;
  203. MC_UpcInfo.stTestParaInfo.RunModelSelect = cp_stFlg.RunModelSelect;
  204. MC_UpcInfo.stTestParaInfo.ThetaGetModelSelect = cp_stFlg.ThetaGetModelSelect;
  205. MC_UpcInfo.stTestParaInfo.CurrentSampleModelSelect = cp_stFlg.CurrentSampleModelSelect;
  206. MC_UpcInfo.stTestParaInfo.RotateDirectionSelect = cp_stFlg.RotateDirectionSelect;
  207. MC_UpcInfo.stTestParaInfo.uwInitPosCurAmp = cp_stControlPara.swAlignCurAp;
  208. MC_UpcInfo.stTestParaInfo.uwVFControlVolAmp = cp_stControlPara.swDragVolAp;
  209. MC_UpcInfo.stTestParaInfo.uwIFControlCurAmp = cp_stControlPara.swDragCurAp;
  210. MC_UpcInfo.stTestParaInfo.uwVFIFTargetFreHz = cp_stControlPara.swDragSpdHz;
  211. MC_UpcInfo.stTestParaInfo.uwSpeedLoopAccRate = 500;
  212. MC_UpcInfo.stTestParaInfo.uwSpeedLoopDecRate = 100;
  213. MC_UpcInfo.stTestParaInfo.uwSpeedLoopBandWidthHz = cp_stControlPara.swAsrPIBandwidth;
  214. MC_UpcInfo.stTestParaInfo.uwSpeedLoopCoefM = cp_stControlPara.swAsrPIM;
  215. MC_UpcInfo.stTestParaInfo.uwCuerrentLoopBandWidthHz = cp_stControlPara.swAcrPIBandwidth;
  216. MC_UpcInfo.stTestParaInfo.uwCurrentLoopCoefM = cp_stControlPara.swAcrRaCoef;
  217. MC_UpcInfo.stTestParaInfo.uwFluxObsBandWidthHz = cp_stControlPara.swObsFluxPICrossfreHz;
  218. MC_UpcInfo.stTestParaInfo.uwFluxObsCoefM = cp_stControlPara.swObsFluxPIDampratio;
  219. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLBandWidthHz = cp_stControlPara.swObsSpdPLLBandWidthHz;
  220. MC_UpcInfo.stTestParaInfo.uwThetaObsPLLCoefM = cp_stControlPara.swObsSpdPLLM;
  221. MC_UpcInfo.stTestParaInfo.uwJm = cp_stMotorPara.swJD ;
  222. MC_UpcInfo.stTestParaInfo.uwPWMMaxDuty = cp_stControlPara.swPWMMaxDuty;
  223. MC_UpcInfo.stTestParaInfo.uwPWM7to5Duty = cp_stControlPara.swPWM7to5Duty;
  224. MC_UpcInfo.stTestParaInfo.uwPwrLimit = cp_stControlPara.swPwrLimitValWt;
  225. MC_UpcInfo.stTestParaInfo.uwPwrLimitError = cp_stControlPara.swPwrLimitErrWt;
  226. MC_UpcInfo.stTestParaInfo.uwPwrLimitKp = cp_stControlPara.swPwrLimitKpPu;
  227. MC_UpcInfo.stTestParaInfo.uwPwrLimitKi = cp_stControlPara.swPwrLimitKiPu;
  228. OBC_SetCustomPara.uwWheelPerimeter = ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter;
  229. OBC_SetCustomPara.StartUpMod = 1;
  230. OBC_SetCustomPara.BikeSpeedLimit = ass_ParaSet.uwAssistLimitBikeSpdStart;
  231. OBC_SetCustomPara.DeltDiameter = ass_ParaCong.swDeltPerimeter;
  232. OBC_SetCustomPara.AssistMod = ass_ParaSet.uwAsssistSelectNum;
  233. OBC_SetCustomPara.AutoPowerOffTime = ass_ParaCong.uwAutoPowerOffTime;
  234. OBC_SetCustomPara.scDeltaBikeSpeedLimit = (SBYTE)ass_ParaCong.swDeltaBikeSpeedLimit;
  235. }
  236. UWORD SizeMCUP;
  237. void Can_voInitMC_Run(void)
  238. {
  239. // flash_voVerRead();
  240. // flash_voErrorRead();
  241. // MC版本信息初始化,Mode和SN从EEPROM读取
  242. strncpy(MC_VerInfo.HW_Version, (char *)"SC20100101V1 ", 16); //长度不超过16
  243. // Software version
  244. char chFwVersion[16]="V1r0r0_";
  245. strncat(chFwVersion,COMPLIE_TIME,9); // Commit time COMPLIE_TIME
  246. strncpy(MC_VerInfo.FW_Version, (char *)chFwVersion, 16);
  247. // Firmware Special Info
  248. char chFrimware[32]="NC2025X000-MS0000-V0r0. ";
  249. strncat(chFrimware,FINGER_PRINT,8); // Git Version
  250. if(cp_stFlg.RunModelSelect == CadAssist)
  251. {
  252. strncat(chFrimware,"C",1); // Cadance assist end with "C" Torque assist end with "T"
  253. }
  254. else if(cp_stFlg.RunModelSelect == TorqAssist)
  255. {
  256. strncat(chFrimware,"T",1); // Torque Assist end with "T", Cadence Assist end with "C"
  257. }
  258. else
  259. {
  260. /* 不做处理*/
  261. }
  262. strncpy(Firmware_Special, (char *)chFrimware, 32);
  263. //电机型号
  264. // memcpy(MC_VerInfo.Mode, Syspara2.flash_stPara.ubMotorVersion, sizeof(Syspara2.flash_stPara.ubMotorVersion));
  265. // memcpy(MC_VerInfo.SN_Num, Syspara2.flash_stPara.ubSN, sizeof(Syspara2.flash_stPara.ubSN));
  266. memcpy(MC_VerInfo.Mode, Productionpara.ubMotorVersion, sizeof(Productionpara.ubMotorVersion));
  267. memcpy(MC_VerInfo.SN_Num, Productionpara.ubSN, sizeof(Productionpara.ubSN));
  268. // MC生产信息
  269. memcpy(MC_MacInfo.Manufacturer, Productionpara.ubProdInfo.Manufacturer, sizeof(Productionpara.ubProdInfo));
  270. //自定义字符串
  271. memcpy(MC_RsASSCII.CustomASCII1, Syspara2.flash_stPara.ubRsASSCII.CustomASCII1, sizeof(MC_RsASSCII.CustomASCII1));
  272. memcpy(MC_RsASSCII.CustomASCII2, Syspara2.flash_stPara.ubRsASSCII.CustomASCII2, sizeof(MC_RsASSCII.CustomASCII2));
  273. memcpy(MC_RsASSCII.CustomASCII3, Syspara2.flash_stPara.ubRsASSCII.CustomASCII3, sizeof(MC_RsASSCII.CustomASCII3));
  274. //控制参数
  275. memcpy(&MC_UpcInfo.stTestParaInfo.uwTestParaSaveFlg, &Syspara2.flash_stPara.stTestParaInfo.uwTestParaSaveFlg, sizeof(MC_UpcInfo.stTestParaInfo));
  276. BMS_VoltEstimat.uwInterResistpu = ((ULONG)1700 << 15) / cof_uwRbOm; //Q15 0.1mOhm BMS internal resistance 150mOhm + 20mOhm
  277. mth_voLPFilterCoef(1000000 / 100, EVENT_1MS_HZ, &BMS_swCurIdcLpf.uwKx); //100Hz
  278. uwRemainDistanceCal = 0xffff; // init invalid value
  279. }
  280. void Can_voMC_Run_1ms(void)
  281. {
  282. static UWORD time_s=0;
  283. if (cp_stBikeRunInfoPara.BikeSpeedKmH > 30)
  284. {
  285. cp_stBikeRunInfoPara.ulRiTime++;
  286. }
  287. else
  288. {}
  289. if(++time_s>60000)
  290. {
  291. cp_stHistoryPara.ulUsedTime++;
  292. time_s=0;
  293. }
  294. // Error Cnt record and Error Display Set
  295. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms ++;
  296. UART_RxBuff_Struct_OBC.ul_UartComTimeOutCount++;
  297. UART_RxBuff_Struct_BMS.ul_UartComTimeOutCount++;
  298. if(MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms == ALAM_DETECT_CNT_IN_1ms && MC_ErrorCode.Code == 0)
  299. {
  300. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  301. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  302. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  303. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  304. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  305. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  306. MC_ErrorCntRecord.ulAlamCntTimeCnt_1ms = 0;
  307. }
  308. else
  309. {}
  310. //故障日志获取故障状态
  311. if (alm_blAlmOccrFlg == TRUE)
  312. {
  313. if (alm_blAlmSingleRecordDoneFlg == FALSE)//
  314. {
  315. if (alm_unCode.bit.IPMFlt == 1) //硬件过流
  316. {
  317. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  318. cp_stHistoryPara.uwAlamHOcurTimes++;
  319. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  320. {
  321. if(MC_ErrorCode.ERROR_Bit.Protect_OverCurrent==0)
  322. {
  323. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  324. /*error log updata*/
  325. ErrorLog_Updata();
  326. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  327. }
  328. }
  329. }
  330. if (alm_unCode.bit.OvrCur == 1) //软件过流
  331. {
  332. MC_ErrorCntRecord.Protect_OverCurrentCnt++;
  333. cp_stHistoryPara.uwAlamSOcurTimes++;
  334. if (MC_ErrorCntRecord.Protect_OverCurrentCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  335. {
  336. if(MC_ErrorCode.ERROR_Bit.Protect_OverCurrent==0)
  337. {
  338. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 1;
  339. /*error log updata*/
  340. ErrorLog_Updata();
  341. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  342. }
  343. }
  344. }
  345. if (alm_unCode.bit.OvrVlt == 1) //过压
  346. {
  347. MC_ErrorCntRecord.Protect_OverVoltageCnt++;
  348. cp_stHistoryPara.uwAlamOVolTimes++;
  349. if (MC_ErrorCntRecord.Protect_OverVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  350. {
  351. if(MC_ErrorCode.ERROR_Bit.Protect_OverVoltage==0)
  352. {
  353. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 1;
  354. /*error log updata*/
  355. ErrorLog_Updata();
  356. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  357. }
  358. }
  359. }
  360. if (alm_unCode.bit.UndrVlt == 1) //欠压
  361. {
  362. MC_ErrorCntRecord.Protect_UnderVoltageCnt++;
  363. cp_stHistoryPara.uwAlamUVolTimes++;
  364. if (MC_ErrorCntRecord.Protect_UnderVoltageCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  365. {
  366. if(MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage==0)
  367. {
  368. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 1;
  369. /*error log updata*/
  370. ErrorLog_Updata();
  371. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  372. }
  373. }
  374. }
  375. if (alm_unCode.bit.RotorLock == 1) //堵转
  376. {
  377. MC_ErrorCntRecord.Protect_LockRotorCnt++;
  378. cp_stHistoryPara.uwAlamRotorLockTimes++;
  379. if (MC_ErrorCntRecord.Protect_LockRotorCnt == ALAM_DISPLAY_CNT_0LEVEL) // ALAM_DISPLAY_CNT_2LEVEL
  380. {
  381. if(MC_ErrorCode.ERROR_Bit.Protect_LockRotor==0)
  382. {
  383. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 1;
  384. /*error log updata*/
  385. ErrorLog_Updata();
  386. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  387. }
  388. }
  389. }
  390. if (alm_unCode.bit.IPMOvrHeat == 1)//PCB过热
  391. {
  392. MC_ErrorCntRecord.Protect_OverTempCnt++;
  393. cp_stHistoryPara.uwAlamOHeatTimes++;
  394. if (MC_ErrorCntRecord.Protect_OverTempCnt == ALAM_DISPLAY_CNT_0LEVEL)
  395. {
  396. if(MC_ErrorCode.ERROR_Bit.Protect_OverTemp==0)
  397. {
  398. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 1;
  399. /*error log updata*/
  400. ErrorLog_Updata();
  401. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  402. }
  403. }
  404. }
  405. if (alm_unCode.bit.PhsLoss == 1) //缺相
  406. {
  407. MC_ErrorCntRecord.Fault_PhaseLineCnt++;
  408. cp_stHistoryPara.uwAlamPhsLossTimes++;
  409. if (MC_ErrorCntRecord.Fault_PhaseLineCnt == ALAM_DISPLAY_CNT_0LEVEL)
  410. {
  411. if(MC_ErrorCode.ERROR_Bit.Fault_PhaseLine==0)
  412. {
  413. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 1;
  414. /*error log updata*/
  415. ErrorLog_Updata();
  416. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  417. }
  418. }
  419. }
  420. // if(alm_unCode.bit.CommOvrTm)
  421. // {
  422. // cp_stHistoryPara.uwAlamComOTimeTimes++;
  423. // }
  424. // if (alm_unCode.bit.SpiThetaFlt == 1) //位置传感器故障
  425. // {
  426. // MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 1;
  427. // /*error log updata*/
  428. // ErrorLog_Updata();
  429. // que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  430. // }
  431. if (alm_unBikeCode.bit.BikeSpdSen == 1) //速度传感器故障
  432. {
  433. if(MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor==0)
  434. {
  435. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 1;
  436. cp_stHistoryPara.uwBikeSpdSensorAlamTimes++;
  437. /*error log updata*/
  438. ErrorLog_Updata();
  439. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  440. }
  441. }
  442. if (alm_unBikeCode.bit.CadenceSen == 1) //踏频传感器故障
  443. {
  444. cp_stHistoryPara.uwCadSensorAlamTimes++;
  445. if(MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor==0)
  446. {
  447. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 1;
  448. /*error log updata*/
  449. ErrorLog_Updata();
  450. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  451. }
  452. }
  453. if (alm_unBikeCode.bit.PCBNTC == 1) //PCB温度
  454. {
  455. if(MC_ErrorCode.ERROR_Bit.Fault_PCBNTC==0)
  456. {
  457. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 1;
  458. /*error log updata*/
  459. ErrorLog_Updata();
  460. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  461. }
  462. }
  463. // if (alm_unBikeCode.bit.Throttle == 1) //油门故障
  464. // {
  465. // MC_ErrorCode.ERROR_Bit.Fault_Throttle = 1;
  466. // /*error log updata*/
  467. // ErrorLog_Updata();
  468. // que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  469. // }
  470. if (alm_unBikeCode.bit.TorqSen == 1) //力矩传感器故障
  471. {
  472. if(MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor==0)
  473. {
  474. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 1;
  475. /*error log updata*/
  476. cp_stHistoryPara.uwTorSensorAlamTimes++;
  477. ErrorLog_Updata();
  478. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  479. }
  480. }
  481. // 电机霍尔故障
  482. if (alm_unCode.bit.HallLoss == 1)
  483. {
  484. MC_ErrorCntRecord.Fault_HallSensorCnt++;
  485. if (MC_ErrorCntRecord.Fault_HallSensorCnt >= ALAM_DISPLAY_CNT_0LEVEL)
  486. {
  487. MC_ErrorCntRecord.Fault_HallSensorCnt = ALAM_DISPLAY_CNT_0LEVEL+1;
  488. if(MC_ErrorCode.ERROR_Bit.Fault_HallSensor == 0)
  489. {
  490. cp_stHistoryPara.uwPosSensorAlamTimes++;
  491. MC_ErrorCode.ERROR_Bit.Fault_HallSensor = 1;
  492. /*error log updata*/
  493. ErrorLog_Updata();
  494. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  495. }
  496. }
  497. }
  498. //电机温度过热
  499. if (alm_unCode.bit.MotorOvrHeat == 1)
  500. {
  501. MC_ErrorCntRecord.Protect_MotorOverTempCnt++;
  502. if (MC_ErrorCntRecord.Protect_MotorOverTempCnt >= ALAM_DISPLAY_CNT_0LEVEL)
  503. {
  504. MC_ErrorCntRecord.Protect_MotorOverTempCnt = ALAM_DISPLAY_CNT_0LEVEL+1;
  505. if(MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp == 0)
  506. {
  507. cp_stHistoryPara.uwAlamOHeatTimes++;
  508. MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp = 1;
  509. /*error log updata*/
  510. ErrorLog_Updata();
  511. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  512. }
  513. }
  514. }
  515. alm_blAlmSingleRecordDoneFlg = TRUE;
  516. }
  517. }
  518. // Claear error Display
  519. if (MC_ErrorCode.ERROR_Bit.Protect_OverCurrent == 1)
  520. {
  521. if (alm_unCode.bit.IPMFlt != 1 && alm_unCode.bit.OvrCur != 1)
  522. {
  523. MC_ErrorCntRecord.Protect_OverCurrentCnt = 0;
  524. MC_ErrorCode.ERROR_Bit.Protect_OverCurrent = 0;
  525. }
  526. }
  527. if (MC_ErrorCode.ERROR_Bit.Protect_OverVoltage == 1)
  528. {
  529. if (alm_unCode.bit.OvrVlt != 1)
  530. {
  531. MC_ErrorCntRecord.Protect_OverVoltageCnt = 0;
  532. MC_ErrorCode.ERROR_Bit.Protect_OverVoltage = 0;
  533. }
  534. }
  535. if (MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage == 1)
  536. {
  537. if (alm_unCode.bit.UndrVlt != 1)
  538. {
  539. MC_ErrorCntRecord.Protect_UnderVoltageCnt = 0;
  540. MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage = 0;
  541. }
  542. }
  543. if (MC_ErrorCode.ERROR_Bit.Protect_LockRotor == 1)
  544. {
  545. if (alm_unCode.bit.RotorLock != 1)
  546. {
  547. MC_ErrorCntRecord.Protect_LockRotorCnt = 0;
  548. MC_ErrorCode.ERROR_Bit.Protect_LockRotor = 0;
  549. }
  550. }
  551. if (MC_ErrorCode.ERROR_Bit.Protect_OverTemp == 1)
  552. {
  553. if (alm_unCode.bit.IPMOvrHeat != 1)
  554. {
  555. MC_ErrorCntRecord.Protect_OverTempCnt = 0;
  556. MC_ErrorCode.ERROR_Bit.Protect_OverTemp = 0;
  557. }
  558. }
  559. if (MC_ErrorCode.ERROR_Bit.Fault_PhaseLine == 1)
  560. {
  561. if (alm_unCode.bit.PhsLoss != 1)
  562. {
  563. MC_ErrorCntRecord.Fault_PhaseLineCnt = 0;
  564. MC_ErrorCode.ERROR_Bit.Fault_PhaseLine = 0;
  565. }
  566. }
  567. // if (MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor == 1)
  568. // {
  569. // if (alm_unCode.bit.SpiThetaFlt != 1)
  570. // {
  571. // MC_ErrorCode.ERROR_Bit.Fault_SpiPosSensor = 0;
  572. // }
  573. // }
  574. if (MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor == 1)
  575. {
  576. if (alm_unBikeCode.bit.BikeSpdSen != 1)
  577. {
  578. MC_ErrorCode.ERROR_Bit.Fault_BikeSpdSensor = 0;
  579. }
  580. }
  581. if (MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor == 1)
  582. {
  583. if (alm_unBikeCode.bit.CadenceSen != 1)
  584. {
  585. MC_ErrorCode.ERROR_Bit.Fault_CadenceSensor = 0;
  586. }
  587. }
  588. if (MC_ErrorCode.ERROR_Bit.Fault_PCBNTC == 1)
  589. {
  590. if (alm_unBikeCode.bit.PCBNTC != 1)
  591. {
  592. MC_ErrorCode.ERROR_Bit.Fault_PCBNTC = 0;
  593. }
  594. }
  595. if (MC_ErrorCode.ERROR_Bit.Fault_Throttle == 1)
  596. {
  597. if (alm_unBikeCode.bit.Throttle != 1)
  598. {
  599. MC_ErrorCode.ERROR_Bit.Fault_Throttle = 0;
  600. }
  601. }
  602. if (MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor == 1)
  603. {
  604. if (alm_unBikeCode.bit.TorqSen != 1)
  605. {
  606. MC_ErrorCode.ERROR_Bit.Fault_TorqueSensor = 0;
  607. }
  608. }
  609. if (MC_ErrorCode.ERROR_Bit.Fault_HallSensor == 1)
  610. {
  611. if (alm_unCode.bit.HallLoss != 1)
  612. {
  613. MC_ErrorCntRecord.Fault_HallSensorCnt = 0;
  614. MC_ErrorCode.ERROR_Bit.Fault_HallSensor = 0;
  615. }
  616. }
  617. if (MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp == 1)
  618. {
  619. if (alm_unCode.bit.MotorOvrHeat != 1)
  620. {
  621. MC_ErrorCntRecord.Protect_MotorOverTempCnt = 0;
  622. MC_ErrorCode.ERROR_Bit.Protect_MotorOverTemp = 0;
  623. }
  624. }
  625. SWORD TempPower;
  626. if (FSM2nd_Run_state.state == Exit)
  627. {
  628. TempPower = 0;
  629. }
  630. else
  631. {
  632. TempPower = scm_stMotoPwrInLpf.slY.sw.hi;
  633. }
  634. BMS_VoltEstimat.swIdcPu = (SWORD)(((SLONG)TempPower << 13) / (SLONG)adc_stUpOut.uwVdcLpfPu); // Q15+Q13-Q14=Q14 Calculated dc current
  635. if (BMS_VoltEstimat.swIdcPu < 0)
  636. {
  637. BMS_VoltEstimat.swIdcPu = 0;
  638. }
  639. mth_voLPFilter(BMS_VoltEstimat.swIdcPu, &BMS_swCurIdcLpf);
  640. BMS_VoltEstimat.uwVdcCompPu = (ULONG)BMS_swCurIdcLpf.slY.sw.hi * BMS_VoltEstimat.uwInterResistpu >> 15; //Q14+Q15-Q15=Q14
  641. }
  642. void Can_voMC_Run_5ms(void)
  643. {
  644. cp_stBikeRunInfoPara.BikeSpeedKmH =
  645. (((SQWORD)bikespeed_stFreGetOut.uwLPFFrequencyPu * FBASE * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * 36 >> 20) * 1048 * 10) >>20; // 1048 = Q20(1/1000) 0.1 km/h
  646. MC_RunInfo.BusVoltage = ((SLONG)adc_stUpOut.uwVdcPu * cof_uwUbVt >> 14) * 100;
  647. MC_RunInfo.MotorSpeed = (SLONG)scm_uwSpdFbkLpfAbsPu * cof_uwVbRpm >> 15;
  648. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH;//cp_stBikeRunInfoPara.BikeSpeedKmH; // MC_RunInfo.MotorSpeed / 10 ;
  649. }
  650. void Can_voMC_Run_200ms(void)
  651. {
  652. // UWORD TempPower;
  653. // 电池通讯异常采用控制器计算电量
  654. if((cp_ulSystickCnt - ulBMS_ComTimeOutCount) > 3000)
  655. {
  656. blBMSCommFault = TRUE; // communication abnormal
  657. }
  658. else
  659. {
  660. blBMSCommFault = FALSE; // communication normal
  661. }
  662. Can_MaxMinTempHistory();
  663. Can_GearSt_switch();
  664. SizeMCUP = sizeof(MC_UpcInfo.stAssistInfo);
  665. if (MC_BC_COM == 1)
  666. {
  667. MC_RunInfoToCDL.SysStatus = FSM1st_Sys_state.state;
  668. MC_RunInfoToCDL.SubStatus = FSM2nd_Run_state.state;
  669. MC_RunInfoToCDL.MotorStatus = curSpeed_state.state;
  670. MC_RunInfoToCDL.AssitStatus = Ass_FSM;
  671. MC_RunInfoToCDL.Id = scm_swIdRefPu;
  672. MC_RunInfoToCDL.Iq = scm_swIqRefPu;
  673. MC_RunInfoToCDL.Ud = scm_swUdRefPu;
  674. MC_RunInfoToCDL.Uq = scm_swUqRefPu;
  675. MC_RunInfoToCDL.InstantTorq = torsensor_stTorSensorOut.uwTorqueReg;
  676. MC_RunInfoToCDL.FilteTorq = torsensor_stTorSensorOut.uwTorqueLPFPu;
  677. MC_RunInfoToCDL.FreqTorq = maf_torque.AverValue;
  678. MC_RunInfoToCDL.RS[0] = scm_swIdFdbLpfPu;
  679. MC_RunInfoToCDL.RS[1] = scm_swIqFdbLpfPu;
  680. MC_RunInfoToCDL.RS[2] = Assist_torqueper;
  681. MC_RunInfoToCDL.RS[3] = bikethrottle_stBikeThrottleOut.uwThrottlePercent;
  682. SendData(&UART_TxBuff_Struct_OBC, ID_MC_TO_CDL, MODE_REPORT, 0xBA20, (UBYTE *)&MC_RunInfoToCDL.SysStatus);
  683. }
  684. MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH; ///>车速 0.1km/h,地址偏移0
  685. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  686. {
  687. MC_RunInfo.MotorSpeed = (SLONG)abs(switchhall_stOut.swLowSpdLpfPu) * cof_uwVbRpm >> 15; ///>输出转速 1rpm,地址偏移2 if(scm_swMotorPwrInLpfWt > 5000)
  688. }
  689. else
  690. {
  691. MC_RunInfo.MotorSpeed = (SLONG)abs(scm_stSpdFbkLpf.slY.sw.hi)* cof_uwVbRpm >> 15;
  692. }
  693. if(scm_swMotorPwrInLpfWt > 5000)
  694. {
  695. scm_swMotorPwrInLpfWt = 5000;
  696. }
  697. if(scm_swMotorPwrInLpfWt < 0 )
  698. {
  699. scm_swMotorPwrInLpfWt = 0;
  700. }
  701. if(hw_blPWMOnFlg == FALSE)
  702. {
  703. scm_swMotorPwrInLpfWt = 0;
  704. }
  705. else
  706. {
  707. }
  708. Power_Cal();
  709. MC_RunInfo.Power= Powercal.PowerPoit;
  710. //MC_RunInfo.Power = scm_swMotorPwrInLpfWt / 10; ///>电功率 1W,地址偏移4
  711. MC_RunInfo.BusVoltage = ((ULONG)adc_stUpOut.uwVdcLpfPu * cof_uwUbVt * 100) >> 14; ///>母线电压 1mV,地址偏移6
  712. // MC_RunInfo.BusVoltage = ((ULONG)(adc_stUpOut.uwVdcLpfPu + BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14; ///>母线电压 1mV,地址偏移6
  713. // if (scm_stMotoPwrInLpf.slY.sw.hi < 10)
  714. // {
  715. // TempPower = 0;
  716. // }
  717. // else
  718. // {
  719. // TempPower = scm_stMotoPwrInLpf.slY.sw.hi;
  720. // }
  721. // MC_RunInfo.BusCurrent = (((ULONG)TempPower << 13) / adc_stUpOut.uwVdcLpfPu) * cof_uwIbAp * 100 >> 14; ///>母线电流 1mA,地址偏移8
  722. MC_RunInfo.BusCurrent = ((ULONG)adc_stUpOut.uwIbusAvgLpfPu) * cof_uwIbAp * 10 >> 14; ///14 >母线电流 1mA,地址偏移8
  723. MC_RunInfo.Cadence = (cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * 60) >> 20; ///>踏频 1rpm,地址偏移10
  724. MC_RunInfo.Torque = ((ULONG)torsensor_stTorSensorOut.uwTorqueLPFPu * cof_uwTorqNm / 10) >> 14; ///>踩踏力矩 1Nm,地址偏移11
  725. MC_RunInfo.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir; ///>踩踏方向 0-正,1-反,2-停止,地址偏移12
  726. MC_RunInfo.GearSt = MC_ControlCode.GearSt; ///>助力档位,地址偏移13
  727. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; ///>灯开关 0xF0-关,0xF1-开,地址偏移14
  728. MC_RunInfo.SOC = Can_SOC_Cal(); ///>剩余电量 1%,地址偏移15
  729. if(hw_blPWMOnFlg == FALSE)
  730. {
  731. MC_RunInfo.BusCurrent=0;
  732. }
  733. // MC_RunInfo.ODO_Km = MC_RideLog.ODO_Km / 10; ///>总里程 1km,地址偏移18
  734. Can_Trip_Cal();
  735. if (cp_stBikeRunInfoPara.blGearStUpdate)
  736. {
  737. if(cp_stBikeRunInfoPara.uwBikeGear > 0 &&(cp_stBikeRunInfoPara.uwBikeGear <=5))
  738. {
  739. MC_RunInfo.PowerPerKm =
  740. *(&cp_stHistoryPara.uwG1AvgPwrConsumption + (cp_stBikeRunInfoPara.uwBikeGear - 1)) / 10; //>平均功耗 0.01Ah/km ,地址偏移20
  741. uwRemainDistanceCal = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
  742. }
  743. else
  744. {
  745. MC_RunInfo.PowerPerKm = 0;
  746. uwRemainDistanceCal = 0xffff; //invalid value
  747. }
  748. cp_stBikeRunInfoPara.BMSRestChargeLast = BMS_RunInfo.RC;
  749. cp_stBikeRunInfoPara.uwPowerPerKm = 0;
  750. cp_stBikeRunInfoPara.uwAvePowerPerKm = 0;
  751. cp_stBikeRunInfoPara.uwPowerPerKmSum = 0;
  752. memset(cp_stBikeRunInfoPara.uwPowerPerKmBuf, 0, sizeof(cp_stBikeRunInfoPara.uwPowerPerKmBuf));
  753. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  754. cp_stBikeRunInfoPara.uwCruisDis = 0;
  755. }
  756. if(blBMSCommFault == TRUE)
  757. {
  758. MC_RunInfo.RemainDistance = 0xeeee; /* no bms info*/
  759. }
  760. else
  761. {
  762. MC_RunInfo.RemainDistance = uwRemainDistanceCal;
  763. }
  764. MC_RunInfo.T_PCB = adc_stUpOut.PCBTemp + 40; ///>PCB温度 +40℃,地址偏移21
  765. MC_RunInfo.T_Coil = adc_stUpOut.MotorTemp + 40; ///>绕组温度 +40℃,地址偏移22
  766. MC_RunInfo.T_MCU = adc_stUpOut.PCBTemp + 40; ///>MCU温度 +40℃,地址偏移23
  767. MC_RunInfo.Ride_Time = cp_stBikeRunInfoPara.ulRiTime >> 10; ///>开机后骑行时间 1s,地址偏移26
  768. MC_RunInfo.TorqueSensorData1=(UBYTE)torsensor_stTorSensorCof.uwTorqueOffsetNow1;
  769. MC_RunInfo.TorqueSensorData2=(UBYTE)torsensor_stTorSensorCof.uwTorqueOffsetNow2;
  770. MC_RunInfo.TorqueSensorData3=(UBYTE)torsensor_stTorSensorCof.uwTorqueOffsetNow3;
  771. static UBYTE test1 = 0;
  772. if (MC_WorkMode == 1)
  773. {
  774. #if(UART_ID==3)
  775. test1++;
  776. if(test1>=5)
  777. #else
  778. test1++;
  779. if(test1>=2)
  780. #endif
  781. {
  782. test1=0;
  783. SendData(&UART_TxBuff_Struct_OBC, ID_MC_BC, MODE_REPORT, 0x1020, (uint8_t *)&MC_RunInfo.BikeSpeed);
  784. }
  785. }
  786. }
  787. void Can_Trip_Cal(void)
  788. {
  789. if (bikespeed_stFreGetOut.blUpdateTripCntFlg == TRUE)
  790. {
  791. MC_RunInfo.Ride_Km++;
  792. cp_stHistoryPara.ulODOTrip++; // Unit: 0.1km
  793. cp_stHistoryPara.ulRealODOTrip++; // Unit: 0.1km
  794. cp_stHistoryPara.ulTripSum++;
  795. Can_RemainTrip_Cal();
  796. bikespeed_stFreGetOut.blUpdateTripCntFlg = FALSE;
  797. }
  798. MC_RideLog.ODO_Km = cp_stHistoryPara.ulODOTrip;
  799. MC_RideLog.TRIP_Km = cp_stHistoryPara.ulTripSum;
  800. MC_RideLog.ODO_Time = cp_stHistoryPara.ulODOTime + (cp_stBikeRunInfoPara.ulRiTime >> 10) / 60; // min
  801. MC_RideLog.TRIP_Time = cp_stHistoryPara.ulTripSumTime + (cp_stBikeRunInfoPara.ulRiTirpTime >> 10) / 60; // min
  802. }
  803. void Can_RemainTrip_Cal(void)
  804. {
  805. if(blBMSCommFault == FALSE)
  806. {
  807. UWORD uwCruisCoef; //功耗系数 Q12
  808. cp_stBikeRunInfoPara.uwCruisDis++; // 0.1 km
  809. if (cp_stBikeRunInfoPara.uwCruisDis >= 5) // 5 = 0.5km
  810. {
  811. cp_stBikeRunInfoPara.uwCruisDis = 0;
  812. cp_stBikeRunInfoPara.BMSRestChargeNow = BMS_RunInfo.RC;
  813. cp_stBikeRunInfoPara.uwPowerPerKm = (cp_stBikeRunInfoPara.BMSRestChargeLast - cp_stBikeRunInfoPara.BMSRestChargeNow)
  814. << 1; // BMS_RunInfo.RC (BMSRestChargeLast - BMSRestChargeNow) /0.5 km
  815. cp_stBikeRunInfoPara.BMSRestChargeLast = cp_stBikeRunInfoPara.BMSRestChargeNow;
  816. cp_stBikeRunInfoPara.uwPowerPerKmSum -= cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  817. cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT] = cp_stBikeRunInfoPara.uwPowerPerKm;
  818. cp_stBikeRunInfoPara.uwPowerPerKmSum += cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
  819. if (cp_stBikeRunInfoPara.uwPowerPerKmBuf[31] == 0)
  820. {
  821. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum / (cp_stBikeRunInfoPara.uwAvePowerCNT + 1);
  822. }
  823. else
  824. {
  825. cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum >> 5;
  826. }
  827. cp_stBikeRunInfoPara.uwAvePowerCNT++;
  828. if (cp_stBikeRunInfoPara.uwAvePowerCNT >= 32)
  829. {
  830. cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
  831. }
  832. if(cp_stBikeRunInfoPara.uwAvePowerPerKm > 0)
  833. {
  834. if (cp_stBikeRunInfoPara.uwBikeGear == 1)
  835. {
  836. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG1AvgPwrConsumption;
  837. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  838. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  839. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  840. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  841. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  842. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG1AvgPwrConsumption / 10;
  843. }
  844. else if (cp_stBikeRunInfoPara.uwBikeGear == 2)
  845. {
  846. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG2AvgPwrConsumption;
  847. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  848. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  849. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  850. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  851. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  852. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG2AvgPwrConsumption / 10;
  853. }
  854. else if (cp_stBikeRunInfoPara.uwBikeGear == 3)
  855. {
  856. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG3AvgPwrConsumption;
  857. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  858. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  859. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  860. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  861. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  862. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG3AvgPwrConsumption / 10;
  863. }
  864. else if (cp_stBikeRunInfoPara.uwBikeGear == 4)
  865. {
  866. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG4AvgPwrConsumption;
  867. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  868. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  869. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  870. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  871. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  872. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG4AvgPwrConsumption / 10;
  873. }
  874. else if (cp_stBikeRunInfoPara.uwBikeGear == 5)
  875. {
  876. uwCruisCoef = ((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG5AvgPwrConsumption;
  877. cp_stHistoryPara.uwG1AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12;
  878. cp_stHistoryPara.uwG2AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12;
  879. cp_stHistoryPara.uwG3AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12;
  880. cp_stHistoryPara.uwG4AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12;
  881. cp_stHistoryPara.uwG5AvgPwrConsumption = (ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12;
  882. MC_RunInfo.PowerPerKm = cp_stHistoryPara.uwG5AvgPwrConsumption / 10;
  883. }
  884. else
  885. {
  886. uwCruisCoef = 4096;
  887. }
  888. uwRemainDistanceCal = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
  889. }
  890. }
  891. }
  892. }
  893. #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
  894. SLONG slBMSMaxVol = 54000;
  895. SLONG slBMSMinVol = 40500;
  896. #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
  897. SLONG slBMSMaxVol = 41500;
  898. SLONG slBMSMinVol = 31000;
  899. #else
  900. SLONG slBMSMaxVol = 41500;
  901. SLONG slBMSMinVol = 31000;
  902. #endif
  903. SWORD SOC_Cnt, SOC_Value;
  904. SWORD SOC_MIN=50;
  905. SLONG Voltage_Sum;
  906. static BOOL blSOCfirstSet = FALSE;
  907. UWORD Can_SOC_Cal(void)
  908. {
  909. UWORD templenght, VoltageAvg = 0;
  910. // 开机1s等待电压稳定后初始化电量
  911. if (cp_ulSystickCnt < 500)
  912. {
  913. return (UWORD)0;
  914. }
  915. if(blBMSCommFault == FALSE)
  916. {
  917. SOC_Value = BMS_RunInfo.SOC;
  918. if((blSOCfirstSet == FALSE) && ( ulBMS_ComTimeOutCount !=0) )// 开机初始化一次电量
  919. {
  920. SOC_MIN = BMS_RunInfo.SOC;
  921. blSOCfirstSet = TRUE;
  922. }
  923. }
  924. else if(blSOCfirstSet == FALSE)// 开机初始化一次电量
  925. {
  926. if(0 == ulBMS_ComTimeOutCount)
  927. {
  928. if (MC_RunInfo.BusVoltage >= slBMSMaxVol)
  929. {
  930. SOC_Value = 100;
  931. }
  932. else if (MC_RunInfo.BusVoltage <= slBMSMinVol)
  933. {
  934. SOC_Value = 0;
  935. }
  936. else
  937. {
  938. SOC_Value = (SWORD)(((SLONG)MC_RunInfo.BusVoltage - slBMSMinVol) * 100 / (slBMSMaxVol - slBMSMinVol));
  939. }
  940. SOC_MIN = SOC_Value;
  941. }
  942. else
  943. {
  944. SOC_MIN = BMS_RunInfo.SOC;
  945. }
  946. blSOCfirstSet = TRUE;
  947. }
  948. // 1min更新一次电量
  949. else
  950. {
  951. templenght = 300; // 60s in 200ms time task
  952. SOC_Cnt++;
  953. Voltage_Sum += MC_RunInfo.BusVoltage;
  954. if (SOC_Cnt >= templenght) // 60s
  955. {
  956. VoltageAvg = (UWORD)(Voltage_Sum / templenght);
  957. if (VoltageAvg <= slBMSMinVol)
  958. {
  959. SOC_Value = 0;
  960. }
  961. else
  962. {
  963. SOC_Value = (SWORD)(((SLONG)VoltageAvg - (SLONG)slBMSMinVol) * 100 / ((SLONG)slBMSMaxVol - (SLONG)slBMSMinVol));
  964. }
  965. if (SOC_Value < SOC_MIN)
  966. {
  967. SOC_MIN = SOC_Value;
  968. }
  969. else
  970. {
  971. }
  972. SOC_Cnt = 0;
  973. Voltage_Sum = 0;
  974. }
  975. // 电量仅递减
  976. if (SOC_Value > SOC_MIN)
  977. {
  978. SOC_Value = SOC_MIN;
  979. }
  980. else if (SOC_Value < 0)
  981. {
  982. SOC_Value = 0;
  983. }
  984. else
  985. {
  986. // do noting
  987. }
  988. }
  989. return (UWORD)SOC_Value;
  990. }
  991. void Can_MaxMinTempHistory(void)
  992. {
  993. if (adc_stUpOut.PCBTemp > ((SWORD)cp_stHistoryPara.swNTCTempMaxCe))
  994. {
  995. cp_stHistoryPara.swNTCTempMaxCe = adc_stUpOut.PCBTemp;
  996. }
  997. else
  998. {}
  999. if (adc_stUpOut.PCBTemp < ((SWORD)cp_stHistoryPara.swNTCTempMinCe))
  1000. {
  1001. cp_stHistoryPara.swNTCTempMinCe = adc_stUpOut.PCBTemp;
  1002. }
  1003. else
  1004. {}
  1005. }
  1006. void Can_GearSt_switch(void)
  1007. {
  1008. cp_stBikeRunInfoPara.uwBikeGearLast = cp_stBikeRunInfoPara.uwBikeGear;
  1009. if (MC_WorkMode == 1) // 配置模式不自动关闭助力
  1010. {
  1011. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  1012. }
  1013. if ((cp_ulSystickCnt - ulOBC_ComTimeOutCount) < 3000) // 控制器与仪表通信中断超过3s,关闭助力
  1014. {
  1015. if (MC_ControlCode.GearSt <= 0x05)
  1016. {
  1017. cp_stBikeRunInfoPara.uwBikeGear = MC_ControlCode.GearSt;
  1018. }
  1019. else if (MC_ControlCode.GearSt == 0x33)
  1020. {
  1021. cp_stBikeRunInfoPara.uwBikeGear = 5;
  1022. }
  1023. else if(MC_ControlCode.GearSt == 0x22)
  1024. {
  1025. cp_stBikeRunInfoPara.uwBikeGear = 0x22;
  1026. }
  1027. else
  1028. {
  1029. cp_stBikeRunInfoPara.uwBikeGear = 0;
  1030. }
  1031. }
  1032. #if(BIKE_OXFORD_EN==0)
  1033. else
  1034. {
  1035. MC_ControlCode.GearSt = MC_GearSt_OFF;
  1036. MC_RunInfo.GearSt = 0x00;
  1037. cp_stBikeRunInfoPara.uwBikeGear = 0x00;
  1038. }
  1039. #endif
  1040. if (cp_stBikeRunInfoPara.uwBikeGear != cp_stBikeRunInfoPara.uwBikeGearLast)
  1041. {
  1042. cp_stBikeRunInfoPara.blGearStUpdate = TRUE;
  1043. }
  1044. else
  1045. {
  1046. cp_stBikeRunInfoPara.blGearStUpdate = FALSE;
  1047. }
  1048. }
  1049. void Can_Light_switch(void)
  1050. {
  1051. /* light switch*/
  1052. if (MC_ControlCode.LightSwitch == 0xF0)
  1053. {
  1054. cp_stBikeRunInfoPara.uwLightSwitch = 0;
  1055. }
  1056. else if (MC_ControlCode.LightSwitch == 0xF1)
  1057. {
  1058. cp_stBikeRunInfoPara.uwLightSwitch = 1;
  1059. }
  1060. }
  1061. void Can_AssistCoef_Read(UPC_CurveOrderInfo_Struct_t *order)
  1062. {
  1063. MC_UpcInfo.stAssistInfo.stCurveOrderInfo = *order;
  1064. memcpy(&MC_UpcInfo.stAssistInfo.slTorquePolyA, &Syspara2.flash_stPara.slTorqAssGain[order->uwTorqueCurveNum - 1], sizeof(POLY_COEF));
  1065. memcpy(&MC_UpcInfo.stAssistInfo.slCadencePolyA, &Syspara2.flash_stPara.slCadAssGain[order->uwCadenceCurveNum - 1], sizeof(POLY_COEF));
  1066. }
  1067. void Can_AssistCoef_Write(UPC_CurveOrderInfo_Struct_t *order)
  1068. {
  1069. memcpy(&Syspara2.flash_stPara.slTorqAssGain[order->uwTorqueCurveNum - 1], &MC_UpcInfo.stAssistInfo.slTorquePolyA, sizeof(POLY_COEF));
  1070. memcpy(&Syspara2.flash_stPara.slCadAssGain[order->uwCadenceCurveNum - 1], &MC_UpcInfo.stAssistInfo.slCadencePolyA, sizeof(POLY_COEF));
  1071. }
  1072. void ErrorLog_Updata(void)
  1073. {
  1074. que_stErrorLog.ErrorCode = MC_ErrorCode.Code;
  1075. que_stErrorLog.RunTime = cp_stHistoryPara.ulUsedTime;//MC_RunInfo.Ride_Time;
  1076. que_stErrorLog.RunInfo = MC_RunInfo;
  1077. //memcpy((uint8_t*)&que_stErrorLog.RunInfo, (uint8_t*)&MC_RunInfo.BikeSpeed, sizeof(MC_RunInfo_Struct_t));
  1078. que_stErrorLog.IqCurrentPu = scm_swIqFdbLpfPu;
  1079. que_stErrorLog.IqVoltagePu = scm_swUqRefPu;
  1080. que_stErrorLog.IdCurrentPu = scm_swIdFdbLpfPu;
  1081. que_stErrorLog.IdVoltagePu = scm_swUdRefPu;
  1082. }
  1083. void Power_Cal(void)
  1084. {
  1085. SWORD V_temp;
  1086. UWORD Poit_temp;
  1087. UWORD Power_temp;
  1088. SLONG Power_clac;
  1089. Poit_temp= (cp_stMotorPara.swRPwrWt)>>11;
  1090. Power_temp=cp_stMotorPara.swRPwrWt & 0x07ff;
  1091. if(cp_stControlPara.swPwrLimitValWt>700)
  1092. {
  1093. Powercal.swMaxpower=cp_stControlPara.swPwrLimitValWt-600;
  1094. }
  1095. V_temp=MC_RunInfo.BusVoltage/100;
  1096. Powercal.swPowerFdb=(SWORD) ((SLONG)(V_temp) * (SLONG)MC_RunInfo.BusCurrent/1000);
  1097. if(Poit_temp==0)
  1098. {
  1099. Powercal.PowerPoit=(SWORD)((SLONG)Powercal.swPowerFdb*250/((SLONG)Powercal.swMaxpower));
  1100. if(Powercal.PowerPoit > 250)
  1101. {
  1102. Powercal.PowerPoit = 250;
  1103. }
  1104. else if(Powercal.PowerPoit < 0 )
  1105. {
  1106. Powercal.PowerPoit = 0;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. Power_clac=(SLONG)Powercal.swPowerFdb*(Poit_temp+1) ;
  1112. Powercal.PowerPoit=(SWORD)((Power_clac/10) >>5);
  1113. }
  1114. if(hw_blPWMOnFlg == FALSE)
  1115. {
  1116. Powercal.PowerPoit = 0;
  1117. }
  1118. }