canAppl.c 45 KB

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