alarm.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: alarm.c
  4. Partner Filename: alarm.h
  5. Description: System fault detection and diagnosis
  6. Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
  7. CPU TYPE : GD32F3x0
  8. *************************************************************************
  9. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. ************************************************************************/
  15. /************************************************************************
  16. Beginning of File, do not put anything above here except notes
  17. Compiler Directives:
  18. *************************************************************************/
  19. #ifndef _ALARM_C_
  20. #define _ALARM_C_
  21. #endif
  22. /************************************************************************
  23. Included File:
  24. *************************************************************************/
  25. #include "syspar.h"
  26. #include "user.h"
  27. #include "FSM_1st.h"
  28. #include "FSM_2nd.h"
  29. #include "spdctrFSM.h"
  30. #include "power.h"
  31. #include "emcdeal.h"
  32. #include "canAppl.h"
  33. #include "hwsetup.h"
  34. #include "UserGpio_Config.h"
  35. /************************************************************************
  36. Private Variables:
  37. ************************************************************************/
  38. UWORD alm_pvt_uwResPwrWt = 0;
  39. _Bool alm_pvt_blIPMOTReCheckFlg = FALSE;
  40. _Bool alm_pvt_blMotorOTReCheckFlg = FALSE;
  41. SWORD alm_pvt_swSpdRefAbsPu = 0;
  42. SWORD alm_pvt_swSpdFbAbsPu = 0;
  43. SLONG alm_pvt_slSpdFbLpfAbsPu = 0;
  44. SWORD alm_pvt_swSpdFbLpfAbsPu = 0;
  45. SWORD alm_pvt_swIqRefAbsPu = 0;
  46. SLONG alm_pvt_slIqRefLpfAbsPu = 0;
  47. SWORD alm_pvt_swIqRefLpfAbsPu = 0;
  48. _Bool alm_pvt_blTbcFirstFlg = FALSE;
  49. _Bool alm_pvt_blTbsFirstFlg = FALSE;
  50. SWORD alm_pvt_swRtLockPwrRatio = 0;
  51. static ULONG alm_pvt_ulWarn2ErrorCount = 0;
  52. _Bool alm_pvt_blCadFltPowerUpFlg = FALSE;
  53. IABCSTRUCT alm_I_Filter;
  54. /************************************************************************
  55. Constant Table:
  56. *************************************************************************/
  57. /************************************************************************
  58. Exported Functions:
  59. *************************************************************************/
  60. /***************************************************************
  61. Function:
  62. Description:
  63. Call by:
  64. Input Variables:
  65. Output/Return Variables:
  66. Subroutine Call:
  67. Reference:
  68. ****************************************************************/
  69. void alm_voInit(void)
  70. {
  71. /* Alarm code and flag init */
  72. alm_unCode.all = 0;
  73. alm_unBikeCode.all = 0;
  74. alm_blAlmOccrFlg = FALSE;
  75. alm_blWarnOccrFlg = FALSE;
  76. /* Alarm action init */
  77. alm_unAction.all = 0;
  78. /* Alarm handle fsm init */
  79. alm_enFSMStatus = Alm_Stop;
  80. alm_enBikeFSMStatus = Alm_Stop;
  81. /* Clear stop count */
  82. alm_stStopCt.ulThrPhsShrtFrc = 0;
  83. alm_stStopCt.ulPWMOff = 0;
  84. alm_stStopCt.ulThrPhsShrt = 0;
  85. alm_stStopCt.uwRotorStopCnt = 0;
  86. alm_stStopCt.ulPWMOffShrtSw = 0;
  87. alm_stStopCt.ulShrtPWMOffSw = 0;
  88. alm_stStopCt.ulBikePWMOff = 0;
  89. /* Alarm recover count init */
  90. alm_stRecCt.ulGlbl = 0;
  91. alm_stRecCt.ulOvrVlt = 0;
  92. alm_stRecCt.ulOvrVlt1 = 0;
  93. alm_stRecCt.ulUndrVlt = 0;
  94. alm_stRecCt.ulUndrVlt1 = 0;
  95. alm_stRecCt.ulIPMOvrHeat = 0;
  96. alm_stRecCt.ulIPMOvrHeat1 = 0;
  97. alm_stRecCt.ulIPMOC = 0;
  98. alm_stRecCt.ulBikeGlbl = 0;
  99. alm_stRecCt.ulBikeSpdFlt = 0;
  100. alm_stRecCt.ulCadenceFlt = 0;
  101. alm_stRecCt.ulTorqFlt = 0;
  102. alm_stRecCt.ulThrottleFlt = 0;
  103. alm_stRecCt.ulPCBNTCFlt = 0;
  104. alm_stRecCt.ulMotorNTCFlt = 0;
  105. /* Alarm detect count init */
  106. alm_stDecCt.ulIPMOvrHeat = 0;
  107. alm_stDecCt.ulIPMOvrHeat1 = 0;
  108. alm_stDecCt.ulOvrCur = 0;
  109. alm_stDecCt.ulOvrSpd = 0;
  110. alm_stDecCt.ulOvrVltLvl1 = 0;
  111. alm_stDecCt.ulPhsALoss = 0;
  112. alm_stDecCt.ulPhsBLoss = 0;
  113. alm_stDecCt.ulPhsCLoss = 0;
  114. alm_stDecCt.slRotorLock = 0;
  115. alm_stDecCt.ulUndrVltLvl1 = 0;
  116. alm_stDecCt.uwBikeSpdFlt = 0;
  117. alm_stDecCt.uwCadenceFlt = 0;
  118. alm_stDecCt.uwTorqFlt = 0;
  119. alm_stDecCt.uwThrottleFlt = 0;
  120. alm_stDecCt.uwPCBNTCFlt = 0;
  121. alm_stDecCt.uwMotorNTCFlt = 0;
  122. /* Private variables init */
  123. alm_pvt_blIPMOTReCheckFlg = FALSE;
  124. alm_pvt_blMotorOTReCheckFlg = FALSE;
  125. alm_I_Filter.ulIa_Filter=0;
  126. alm_I_Filter.ulIb_Filter=0;
  127. alm_I_Filter.ulIc_Filter=0;
  128. }
  129. /***************************************************************
  130. Function:
  131. Description:
  132. Call by:
  133. Input Variables:
  134. Output/Return Variables:
  135. Subroutine Call:
  136. Reference:
  137. ****************************************************************/
  138. void alm_voCoef(void)
  139. {
  140. alm_stDetectTbcCoefIn.uwIbAp = IBASE;
  141. alm_stDetectTbcCoefIn.uwUbVt = VBASE;
  142. alm_stDetectTbcCoefIn.uwFTbcHz = FTBC_HZ;
  143. alm_stDetectTbcCoefIn.uwPairs = cp_stMotorPara.swMotrPolePairs;
  144. alm_stDetectTbcCoefIn.uwMtRsOm = cp_stMotorPara.swRsOhm;
  145. alm_stDetectTbcCoefIn.uwFbHz = FBASE;
  146. alm_stDetectTbcCoefIn.uwOvrCurIa = cp_stControlPara.swAlmOverCurrentVal;
  147. alm_stDetectTbcCoefIn.uwOvrCurTu = cp_stControlPara.swAlmOverCurrentTM;
  148. alm_stDetectTbcCoefIn.uwAdcDetHigVt = cp_stControlPara.swAlmAdcDetectHighVal;
  149. alm_stDetectTbcCoefIn.uwAdcDetLowVt = cp_stControlPara.swAlmAdcDetectLowVal;
  150. alm_stDetectTbcCoefIn.uwPhsLossVt = cp_stControlPara.swAlmPhsLossVal;
  151. alm_stDetectTbcCoefIn.uwPhsLossTs = cp_stControlPara.swAlmPhsLossTM;
  152. alm_stDetectTbcCoefIn.uwRotorLockSpdK = cp_stControlPara.swAlmRotorLockK;
  153. alm_stDetectTbcCoefIn.uwRotorLockTs = cp_stControlPara.swAlmRotorLockTM;
  154. alm_stDetectTbcCoefIn.swIqRefLpfAbsAp = cp_stControlPara.swAlmRotorLockCurVal;
  155. alm_stDetectTbcCoefIn.swSpdFbLpfAbsRpm = cp_stControlPara.swAlmRotorLockSpdVal;
  156. alm_voDetecTBCCoef(&alm_stDetectTbcCoefIn, &alm_stDetectTbcCoef);
  157. alm_stDetectTbsCoefIn.uwPairs = cp_stMotorPara.swMotrPolePairs;
  158. alm_stDetectTbsCoefIn.uwFbHz = FBASE;
  159. alm_stDetectTbsCoefIn.uwFTbsHz = FTBS_HZ;
  160. alm_stDetectTbsCoefIn.uwUbVt = VBASE;
  161. alm_stDetectTbsCoefIn.uwOvrVlt1Vt = cp_stControlPara.swAlmOverVolVal1;
  162. alm_stDetectTbsCoefIn.uwOvrVlt1Tm = cp_stControlPara.swAlmOverVolTM1;
  163. alm_stDetectTbsCoefIn.uwUndrVlt1Vt = cp_stControlPara.swAlmUnderVolVal1;
  164. alm_stDetectTbsCoefIn.uwUndrVlt1Tm = cp_stControlPara.swAlmUnderVolTM1;
  165. alm_stDetectTbsCoefIn.uwOvrSpdRpm = cp_stControlPara.swAlmOverSpdVal;
  166. alm_stDetectTbsCoefIn.uwOvrSpdTm = cp_stControlPara.swAlmOverSpdTM;
  167. alm_stDetectTbsCoefIn.uwIpmOvrHeatRecCe = cp_stControlPara.swAlmRecOHeatVal;
  168. alm_stDetectTbsCoefIn.uwIpmOvrHeatTs = cp_stControlPara.swAlmOverHeatTM;
  169. alm_stDetectTbsCoefIn.uwIpmOvrHeatCe = cp_stControlPara.swAlmOverHeatCeVal;
  170. alm_stDetectTbsCoefIn.uwMotorOvrHeatRecCe = cp_stControlPara.swAlmMotorRecOHeatVal;
  171. alm_stDetectTbsCoefIn.uwMotorOvrHeatTs = ALM_MOTOR_OVR_TM;
  172. alm_stDetectTbsCoefIn.uwMotorOvrHeatCe = cp_stControlPara.swAlmMotorOverHeatCeVal;
  173. alm_voDetecTBSCoef(&alm_stDetectTbsCoefIn, &alm_stDetectTbsCoef);
  174. alm_stStopTbcCoefIn.uwIbAp = IBASE;
  175. alm_stStopTbcCoefIn.uwUbVt = VBASE;
  176. alm_stStopTbcCoefIn.uwFTbcHz = FTBC_HZ;
  177. alm_stStopTbcCoefIn.uwThrPhsShrtFrcTs = cp_stControlPara.swAlmThrPhsShrtFrcTM;
  178. alm_stStopTbcCoefIn.uwIPMOverCurStopTs = cp_stControlPara.swAlmIPMOverCurStopTM;
  179. alm_stStopTbcCoefIn.uwPwmoffShrt1SwTs = cp_stControlPara.swAlmPWMOffShrtsw1TM;
  180. alm_stStopTbcCoefIn.uwShrtPwmoffSwTs = cp_stControlPara.swAlmShrtPWMOffswTM;
  181. alm_stStopTbcCoefIn.uwPwmOffTs = cp_stControlPara.swAlmPWMOffTM;
  182. alm_stStopTbcCoefIn.uwThrPhsShrtTs = cp_stControlPara.swAlmThrPhsShrtNormTM;
  183. alm_stStopTbcCoefIn.uwOvrVlt1Vt = cp_stControlPara.swAlmOverVolVal1;
  184. alm_voStopTBCCoef(&alm_stStopTbcCoefIn, &alm_stStopTbcCoef);
  185. alm_stResetCoefIn.uwUbVt = VBASE;
  186. alm_stResetCoefIn.uwFTbcHz = FTBC_HZ;
  187. alm_stResetCoefIn.uwRecAllTs = cp_stControlPara.swAlmRecAllTM;
  188. alm_stResetCoefIn.uwIpmOcRecTs = cp_stControlPara.swAlmRecOCTM;
  189. alm_stResetCoefIn.uwOvrVltRecVt = cp_stControlPara.swAlmRecOVVal;
  190. alm_stResetCoefIn.uwOvrVltRecTs = cp_stControlPara.swAlmRecOVTM;
  191. alm_stResetCoefIn.uwUndrVltRecVt = cp_stControlPara.swAlmRecUVVal;
  192. alm_stResetCoefIn.uwUndrVltRecTs = cp_stControlPara.swAlmRecUVTM;
  193. alm_stResetCoefIn.uwIpmOvrHeatRecCe = cp_stControlPara.swAlmRecOHeatVal;
  194. alm_stResetCoefIn.uwIpmOvrHeatRecTs = cp_stControlPara.swAlmRecOHeatTM;
  195. alm_stResetCoefIn.uwIpmOvrHeatRec1Ts = cp_stControlPara.swAlmRecOHeatTM1;
  196. alm_stResetCoefIn.uwMotorOvrHeatRecCe = cp_stControlPara.swAlmMotorRecOHeatVal;
  197. alm_stResetCoefIn.uwMotorOvrHeatRecTs = cp_stControlPara.swAlmRecOHeatTM;
  198. alm_stResetCoefIn.uwMotorOvrHeatRec1Ts = cp_stControlPara.swAlmRecOHeatTM1;
  199. alm_voResetCoef(&alm_stResetCoefIn, &alm_stResetCoef);
  200. alm_stDetect200MSCoefIn.swMotorSpdMinRpm = ALM_MOTORSPD_MIN_RPM;
  201. alm_stDetect200MSCoefIn.uwTorqMinNm = ALM_TROQ_MIN_Nm;
  202. alm_stDetect200MSCoefIn.uwBikeSpdFltTs = ALM_BIKESPD_FLT_TS;
  203. alm_stDetect200MSCoefIn.uwCadenceFltTs = ALM_CADENCE_FLT_TS;
  204. alm_stDetect200MSCoefIn.uwFbHz = FBASE;
  205. alm_stDetect200MSCoefIn.uwFT200MSHz = 5 ;
  206. alm_stDetect200MSCoefIn.uwMotorNTCFltTs = ALM_MOTORNTC_FLT_TS;
  207. alm_stDetect200MSCoefIn.uwPairs = cp_stMotorPara.swMotrPolePairs;
  208. alm_stDetect200MSCoefIn.uwPCBNTCFltTs = ALM_PCBNTC_FLT_TS;
  209. alm_stDetect200MSCoefIn.uwThrottleFltTs = ALM_THROTTLE_FLT_TS;
  210. alm_stDetect200MSCoefIn.uwTorqFltTs = ALM_TORQ_FLT_TS;
  211. alm_stDetect200MSCoefIn.uwTorqMaxVol = ALM_TORQ_MAX_VOL;
  212. alm_stDetect200MSCoefIn.uwTorqMinVol = ALM_TORQ_MIN_VOL;
  213. alm_stDetect200MSCoefIn.uwThrottleMaxVol = ALM_THROTTLE_MAX_VOL;
  214. alm_stDetect200MSCoefIn.uwThrottleMinVol =ALM_THROTTLE_MIN_VOL;
  215. alm_stDetect200MSCoefIn.uwNTCMaxVol = ALM_NTC_MAX_VOL;
  216. alm_stDetect200MSCoefIn.uwNTCMinVol = ALM_NTC_MIN_VOL;
  217. alm_voDetec200MSCoef(&alm_stDetect200MSCoefIn, &alm_stDetect200MSCoef);
  218. alm_stReset1MSCoefIn.uwBikeGlblTm = ALM_BIKE_REC_ALL_TM;
  219. alm_stReset1MSCoefIn.uwBikeSpdFltTm = ALM_BIKESPD_REC_TM;
  220. alm_stReset1MSCoefIn.uwCadenceFltTm = ALM_CADENCE_REC_TM;
  221. alm_stReset1MSCoefIn.uwFT1MSHz = 1000;
  222. alm_stReset1MSCoefIn.uwMotorNTCFltTm = ALM_MOTORNTC_REC_TM;
  223. alm_stReset1MSCoefIn.uwPCBNTCFltTm = ALM_PCBNTC_REC_TM;
  224. alm_stReset1MSCoefIn.uwThrottleFltTm = ALM_THROTTLE_REC_TM;
  225. alm_stReset1MSCoefIn.uwTorqFltTm = ALM_TORQ_REC_TM;
  226. alm_voReset1MSCoef(&alm_stReset1MSCoefIn, &alm_stReset1MSCoef);
  227. }
  228. /***************************************************************
  229. Function:
  230. Description:
  231. Call by:
  232. Input Variables:
  233. Output/Return Variables:
  234. Subroutine Call:
  235. Reference:
  236. ****************************************************************/
  237. void alm_voDetecTBCCoef(ALM_DETECTBC_COFIN *in, ALM_DETECTBC_COF *out)
  238. {
  239. UWORD uwRbOm;
  240. if (in->uwIbAp < 1)
  241. {
  242. in->uwIbAp = 1;
  243. }
  244. if (in->uwFbHz < 1)
  245. {
  246. in->uwFbHz = 1;
  247. }
  248. uwRbOm = (ULONG)in->uwUbVt * 100000 / in->uwIbAp; /* unit: 0.01Ohm, Resistance base */
  249. out->uwRsPu = ((ULONG)in->uwMtRsOm << 15) / uwRbOm; /* Q15, Phase resistance */
  250. out->uwPbWt = (ULONG)in->uwUbVt * in->uwIbAp * 3 / 100 >> 1; /* unit: 0.1w, Power base */
  251. out->ulOvrCurValPu = ((ULONG)in->uwOvrCurIa << 14) / in->uwIbAp; // CUR_AP2PU(x) (((ULONG)(x)<<14)/IBASE) Q14
  252. out->ulOvrCurValCt = ((ULONG)in->uwOvrCurTu * in->uwFTbcHz / 1000000) >> 1; // TBC_US2CT(x) ((ULONG)(x)*FTBC_HZ/1000000)
  253. out->slAdcDetHigValPu = ((SLONG)in->uwAdcDetHigVt * 4096 / 330); //_IQ12(A) (SLONG)(A * 4096L)
  254. out->slAdcDetLowValPu = ((SLONG)in->uwAdcDetLowVt * 4096 / 330);
  255. out->ulPhsLossValPu = ((ULONG)in->uwPhsLossVt << 14) / in->uwIbAp;
  256. out->ulPhsLossValCt = ((ULONG)in->uwPhsLossTs * in->uwFTbcHz) >> 1; // TBC_S2CT(x) ((ULONG)(x)*FTBC_HZ)
  257. out->slRotorLockSpdK = ((SLONG)in->uwRotorLockSpdK * 1024 / 100); //_IQ10(0.5) (SLONG)(A * 1024L)
  258. out->slRotorLockValCt = ((ULONG)in->uwRotorLockTs * in->uwFTbcHz) >> 1;
  259. out->slIqRefLpfAbsValPu = ((SLONG)in->swIqRefLpfAbsAp << 14) / in->uwIbAp; // CUR_AP2PU(x) (((ULONG)(x)<<14)/IBASE)
  260. out->slSpdFbLpfAbsValPu = ((SLONG)in->swSpdFbLpfAbsRpm << 15) / 60 * in->uwPairs / in->uwFbHz;
  261. }
  262. /***************************************************************
  263. Function:
  264. Description:
  265. Call by:
  266. Input Variables:
  267. Output/Return Variables:
  268. Subroutine Call:
  269. Reference:
  270. ****************************************************************/
  271. void alm_voDetecTBC(ALM_IN *in, ALM_DETECTBC_COF *coef)
  272. {
  273. SWORD swTmp1, swTmp2;
  274. /* MicroFault Handle */
  275. // if(clasB_unFaultCode.all != 0)
  276. // {
  277. // alm_unAction.bit.PWMOff = TRUE;
  278. // //alm_unCode.bit.MCUErr = TRUE;
  279. // }
  280. /*=======================================================================
  281. IPM fault
  282. =======================================================================*/
  283. if(alm_unCode.bit.IPMFlt==TRUE)// if (MCU_BRKIN_FLG) //
  284. {
  285. alm_unAction.bit.ThrPhsShrtFrc = TRUE;
  286. // alm_unCode.bit.IPMFlt = TRUE;
  287. }
  288. if (alm_pvt_blTbcFirstFlg == TRUE)
  289. {
  290. /*=======================================================================
  291. Software Over current
  292. =======================================================================*/
  293. #if 0//(EMCDEAL_EN!=0)
  294. if(EcmDeal.EmcModeFlag==TRUE)
  295. {
  296. alm_stDecCt.ulOvrCur=0;
  297. }
  298. else
  299. #endif
  300. // {
  301. // if (in->blADCInitOvrFlg)
  302. // {
  303. // if (curSpeed_state.state != Stop)
  304. // {
  305. // if (in->uwIpeakPu > coef->ulOvrCurValPu) //
  306. // {
  307. // alm_stDecCt.ulOvrCur++;
  308. // if (alm_stDecCt.ulOvrCur >= coef->ulOvrCurValCt) // 500us
  309. // {
  310. // alm_stDecCt.ulOvrCur = coef->ulOvrCurValCt;
  311. // alm_unAction.bit.PWMOff = TRUE;
  312. // alm_unCode.bit.OvrCur = TRUE;
  313. // }
  314. // }
  315. // else
  316. // {
  317. // alm_stDecCt.ulOvrCur = 0;
  318. // }
  319. // }
  320. // else
  321. // {
  322. // alm_stDecCt.ulOvrCur = 0;
  323. // }
  324. // }
  325. // }
  326. /*=======================================================================
  327. ADC Self Detecting Fault
  328. =======================================================================*/
  329. // if (in->blADCInitOvrFlg)
  330. // {
  331. // if((in->uwIdcOffset >= coef->slAdcDetHigValPu)||(in->uwIdcOffset <= coef->slAdcDetLowValPu))
  332. // {
  333. // alm_unAction.bit.ThrPhsShrt = TRUE;
  334. // alm_unCode.bit.ADCOffsetFlt = TRUE;
  335. // }
  336. // }
  337. /*=======================================================================
  338. Over load
  339. =======================================================================*/
  340. /*=======================================================================
  341. Phase loss
  342. =======================================================================*/
  343. if (curSpeed_state.state != Stop)
  344. {
  345. alm_I_Filter.ulIa_Filter=(in->uwIaAbsPu +alm_I_Filter.ulIa_Filter*7)>>3;
  346. alm_I_Filter.ulIb_Filter=(in->uwIbAbsPu +alm_I_Filter.ulIb_Filter*7)>>3;
  347. alm_I_Filter.ulIc_Filter=(in->uwIcAbsPu +alm_I_Filter.ulIc_Filter*7)>>3;
  348. // if (scm_swIqRefPu > 150 || scm_swIqRefPu < -150)
  349. // {
  350. if((cp_stBikeRunInfoPara.BikeSpeedKmH>70) &&(MC_RunInfo.BusCurrent>5000))//70-7km/h
  351. {
  352. if (alm_I_Filter.ulIa_Filter < coef->ulPhsLossValPu) // 1A
  353. {
  354. alm_stDecCt.ulPhsALoss++;
  355. }
  356. else
  357. {
  358. alm_stDecCt.ulPhsALoss = 0;
  359. }
  360. if (alm_I_Filter.ulIb_Filter < coef->ulPhsLossValPu)
  361. {
  362. alm_stDecCt.ulPhsBLoss++;
  363. }
  364. else
  365. {
  366. alm_stDecCt.ulPhsBLoss = 0;
  367. }
  368. if (alm_I_Filter.ulIc_Filter < coef->ulPhsLossValPu)
  369. {
  370. alm_stDecCt.ulPhsCLoss++;
  371. }
  372. else
  373. {
  374. alm_stDecCt.ulPhsCLoss = 0;
  375. }
  376. }
  377. else
  378. {
  379. alm_stDecCt.ulPhsALoss = 0;
  380. alm_stDecCt.ulPhsBLoss = 0;
  381. alm_stDecCt.ulPhsCLoss = 0;
  382. }
  383. // }
  384. if (alm_stDecCt.ulPhsALoss >= coef->ulPhsLossValCt || alm_stDecCt.ulPhsBLoss >= coef->ulPhsLossValCt ||
  385. alm_stDecCt.ulPhsCLoss >= coef->ulPhsLossValCt) // 5s
  386. {
  387. alm_stDecCt.ulPhsALoss = coef->ulPhsLossValCt;
  388. alm_stDecCt.ulPhsBLoss = coef->ulPhsLossValCt;
  389. alm_stDecCt.ulPhsCLoss = coef->ulPhsLossValCt;
  390. // alm_unAction.bit.PWMOff = TRUE;
  391. // alm_unAction.bit.ThrPhsShrt = TRUE;
  392. alm_unCode.bit.PhsLoss = TRUE;
  393. }
  394. }
  395. else
  396. {
  397. alm_stDecCt.ulPhsALoss = 0;
  398. alm_stDecCt.ulPhsBLoss = 0;
  399. alm_stDecCt.ulPhsCLoss = 0;
  400. }
  401. /*=======================================================================
  402. Set AlmTbcDetectFlg
  403. =======================================================================*/
  404. alm_pvt_blTbcFirstFlg = FALSE;
  405. }
  406. else
  407. {
  408. /*=======================================================================
  409. Rotor lock for position sensor: switchhall
  410. =======================================================================*/
  411. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  412. {
  413. if (curSpeed_state.state == ClzLoop)
  414. {
  415. if(abs(in->swIqRefPu) > coef->slIqRefLpfAbsValPu && in->uwSpdFbkLpfAbsPu < coef->slSpdFbLpfAbsValPu)
  416. {
  417. alm_stDecCt.slRotorLock++;
  418. if (alm_stDecCt.slRotorLock >= coef->slRotorLockValCt)
  419. {
  420. alm_stDecCt.slRotorLock = coef->slRotorLockValCt;
  421. // alm_unAction.bit.PWMOff = TRUE;
  422. alm_unCode.bit.RotorLock = TRUE;
  423. }
  424. }
  425. else
  426. {
  427. alm_stDecCt.slRotorLock = 0;
  428. }
  429. }
  430. }
  431. // else if(cp_stFlg.ThetaGetModelSelect == ANG_OBSERVER)
  432. // {
  433. // /*=======================================================================
  434. // Rotor lock for sensorless
  435. // =======================================================================*/
  436. // alm_pvt_swSpdRefAbsPu = in->uwSpdRefAbsPu; // Q15
  437. //
  438. // alm_pvt_swIqRefAbsPu = abs(in->swIqRefPu);
  439. // alm_pvt_slIqRefLpfAbsPu = (SLONG)0x0010 * (alm_pvt_swIqRefAbsPu - alm_pvt_swIqRefLpfAbsPu) + alm_pvt_slIqRefLpfAbsPu; // Q30
  440. // alm_pvt_swIqRefLpfAbsPu = alm_pvt_slIqRefLpfAbsPu >> 15;
  441. //
  442. // alm_pvt_swSpdFbAbsPu = in->uwSpdFbkLpfAbsPu; // Q15
  443. // alm_pvt_slSpdFbLpfAbsPu = (SLONG)0x0010 * (alm_pvt_swSpdFbAbsPu - alm_pvt_swSpdFbLpfAbsPu) + alm_pvt_slSpdFbLpfAbsPu; // Q30
  444. // alm_pvt_swSpdFbLpfAbsPu = alm_pvt_slSpdFbLpfAbsPu >> 15; // Q15
  445. //
  446. // if (((curSpeed_state.state == ClzLoop) || (curSpeed_state.state == StartUp)) && (in->uwSpdRefAbsPu > 0))
  447. // {
  448. // swTmp1 = (in->swIalhpaPu * in->swIalhpaPu + in->swIbetaPu * in->swIbetaPu) >> 14; // Q14=Q14+Q14-Q14
  449. // swTmp2 = swTmp1 * coef->uwRsPu >> 14; // Q15=Q14+Q15-Q14
  450. // alm_pvt_uwResPwrWt = swTmp2 * coef->uwPbWt >> 15; // unit: 0.1w
  451. // if ((in->swMotorPwrInWt > 0) && (alm_pvt_swIqRefLpfAbsPu > coef->slIqRefLpfAbsValPu) &&
  452. // (alm_pvt_swSpdFbLpfAbsPu < coef->slSpdFbLpfAbsValPu))
  453. // {
  454. // alm_pvt_swRtLockPwrRatio = (SWORD)((SLONG)alm_pvt_uwResPwrWt * 100 / in->swMotorPwrInWt);
  455. // if (alm_pvt_uwResPwrWt > (coef->slRotorLockSpdK * in->swMotorPwrInWt >> 10)) // k = 0.5
  456. // {
  457. // alm_stDecCt.slRotorLock++;
  458. // if (alm_stDecCt.slRotorLock >= coef->slRotorLockValCt) // 4s
  459. // {
  460. // alm_stDecCt.slRotorLock = coef->slRotorLockValCt;
  461. // // alm_unAction.bit.ThrPhsShrt = TRUE;
  462. // alm_unAction.bit.PWMOff = TRUE;
  463. // alm_unCode.bit.RotorLock = TRUE;
  464. // }
  465. // }
  466. // else
  467. // {
  468. // alm_stDecCt.slRotorLock--;
  469. // if (alm_stDecCt.slRotorLock < 0)
  470. // {
  471. // alm_stDecCt.slRotorLock = 0;
  472. // }
  473. // }
  474. // }
  475. // else
  476. // {
  477. // alm_stDecCt.slRotorLock = 0;
  478. // }
  479. // }
  480. // else
  481. // {
  482. // alm_stDecCt.slRotorLock = 0;
  483. // }
  484. //
  485. // }
  486. /*=======================================================================
  487. Set AlmTbcDetectFlg
  488. =======================================================================*/
  489. alm_pvt_blTbcFirstFlg = TRUE;
  490. }
  491. /*========================== Alarm flag set ===========================*/
  492. //if((alm_unCode.bit.OvrCur == 1) || (alm_unCode.bit.IPMFlt == 1) || (alm_unCode.bit.MCUErr == 1))
  493. if((alm_unCode.bit.OvrCur == 1) || (alm_unCode.bit.IPMFlt == 1))
  494. {
  495. alm_blAlmOccrFlg = TRUE;
  496. }
  497. else if ((alm_unCode.all != 0) || (alm_unBikeCode.all != 0))
  498. {
  499. alm_blWarnOccrFlg = TRUE;
  500. if(alm_pvt_ulWarn2ErrorCount > 24000)
  501. {
  502. alm_blAlmOccrFlg = TRUE;
  503. }
  504. else
  505. {
  506. alm_pvt_ulWarn2ErrorCount++;
  507. }
  508. }
  509. }
  510. /***************************************************************
  511. Function:
  512. Description:
  513. Call by:
  514. Input Variables:
  515. Output/Return Variables:
  516. Subroutine Call:
  517. Reference:
  518. ****************************************************************/
  519. void alm_voDetecTBSCoef(ALM_DETECTBS_COFIN *in, ALM_DETECTBS_COF *out)
  520. {
  521. if (in->uwFbHz < 1)
  522. {
  523. in->uwFbHz = 1;
  524. }
  525. if (in->uwUbVt < 1)
  526. {
  527. in->uwUbVt = 1;
  528. }
  529. out->ulOvrVltLvl1ValPu = ((ULONG)in->uwOvrVlt1Vt << 14) / in->uwUbVt;
  530. out->ulOvrVltLvl1ValCt = ((ULONG)in->uwOvrVlt1Tm * in->uwFTbsHz / 1000) >> 1;// TBS_S2CT(x) ((ULONG)(x)*FTBS_HZ)
  531. out->ulUndrVltLvl1ValPu = ((ULONG)in->uwUndrVlt1Vt << 14) / in->uwUbVt;
  532. out->ulUndrVltLvl1ValCt = ((ULONG)in->uwUndrVlt1Tm * in->uwFTbsHz / 1000) >> 1; // TBS_MS2CT(x) ((ULONG)(x)*FTBS_HZ/1000)
  533. out->slOvrSpdValPu = ((SLONG)in->uwOvrSpdRpm << 15) / 60 * in->uwPairs /
  534. in->uwFbHz; // SPD_RPM2PU(x) (((SLONG)(x)<<15)/60*MOTOR_PAIRS/FBASE) /* rpm to Pu(Q15) */
  535. out->ulOvrSpdValCt = ((ULONG)in->uwOvrSpdTm * in->uwFTbsHz / 1000) >> 1;
  536. out->uwIPMOvrHeatRecValCe = in->uwIpmOvrHeatRecCe;
  537. out->ulIPMOvrHeatValCt = ((ULONG)in->uwIpmOvrHeatTs * in->uwFTbsHz) >> 1;
  538. out->uwIPMOvrHeatValCe = in->uwIpmOvrHeatCe;
  539. out->uwMotorOvrHeatRecValCe = in->uwMotorOvrHeatRecCe;
  540. out->ulMotorOvrHeatValCt = ((ULONG)in->uwMotorOvrHeatTs * in->uwFTbsHz) >> 1;
  541. out->uwMotorOvrHeatValCe = in->uwMotorOvrHeatCe;
  542. }
  543. /***************************************************************
  544. Function:
  545. Description:
  546. Call by:
  547. Input Variables:
  548. Output/Return Variables:
  549. Subroutine Call:
  550. Reference:
  551. ****************************************************************/
  552. void alm_voDetecTBS(ALM_IN *in, ALM_DETECTBS_COF *coef)
  553. {
  554. if (alm_pvt_blTbsFirstFlg == FALSE)
  555. {
  556. /*=======================================================================
  557. Over voltage
  558. =======================================================================*/
  559. if (in->uwVdcPu > coef->ulOvrVltLvl1ValPu) // 45V
  560. {
  561. alm_stDecCt.ulOvrVltLvl1++;
  562. if (alm_stDecCt.ulOvrVltLvl1 >= coef->ulOvrVltLvl1ValCt) // 1ms
  563. {
  564. alm_stDecCt.ulOvrVltLvl1 = coef->ulOvrVltLvl1ValCt;
  565. // alm_unAction.bit.ThrPhsShrt = TRUE;
  566. alm_unCode.bit.OvrVlt = TRUE;
  567. // alm_unAction.bit.PWMOff = TRUE;
  568. }
  569. }
  570. else
  571. {
  572. alm_stDecCt.ulOvrVltLvl1 = 0;
  573. }
  574. /*=======================================================================
  575. Under voltage
  576. =======================================================================*/
  577. if (in->uwVdcCompPu < coef->ulUndrVltLvl1ValPu) // 160v
  578. {
  579. alm_stDecCt.ulUndrVltLvl1++;
  580. if (alm_stDecCt.ulUndrVltLvl1 >= coef->ulUndrVltLvl1ValCt) // 1000ms
  581. {
  582. alm_stDecCt.ulUndrVltLvl1 = coef->ulUndrVltLvl1ValCt;
  583. // alm_unAction.bit.PWMOff = TRUE;
  584. alm_unCode.bit.UndrVlt = TRUE;
  585. // alm_unAction.bit.PWMOff = TRUE;
  586. }
  587. }
  588. else
  589. {
  590. alm_stDecCt.ulUndrVltLvl1 = 0;
  591. }
  592. alm_pvt_blTbsFirstFlg = TRUE;
  593. }
  594. else
  595. {
  596. /*=======================================================================
  597. Over speed
  598. =======================================================================*/
  599. if (in->uwSpdFbkLpfAbsPu > coef->slOvrSpdValPu) // 6000rpm
  600. {
  601. alm_stDecCt.ulOvrSpd++;
  602. if (alm_stDecCt.ulOvrSpd >= coef->ulOvrSpdValCt) // 100ms
  603. {
  604. alm_stDecCt.ulOvrSpd = coef->ulOvrSpdValCt;
  605. // alm_unAction.bit.ThrPhsShrt = TRUE;
  606. alm_unCode.bit.OvrSpd = TRUE;
  607. }
  608. }
  609. else
  610. {
  611. alm_stDecCt.ulOvrSpd = 0;
  612. }
  613. /*=======================================================================
  614. over heat
  615. =======================================================================*/
  616. if (alm_pvt_blIPMOTReCheckFlg == TRUE)
  617. {
  618. if (in->swIPMTempCe > coef->uwIPMOvrHeatRecValCe) // 70
  619. {
  620. alm_stDecCt.ulIPMOvrHeat1++;
  621. if (alm_stDecCt.ulIPMOvrHeat1 >= coef->ulIPMOvrHeatValCt) // 2s
  622. {
  623. alm_stDecCt.ulIPMOvrHeat1 = coef->ulIPMOvrHeatValCt;
  624. // alm_unAction.bit.ThrPhsShrt = TRUE;
  625. // alm_unAction.bit.PWMOff = TRUE;
  626. alm_unCode.bit.IPMOvrHeat = TRUE;
  627. }
  628. }
  629. else
  630. {
  631. alm_stDecCt.ulIPMOvrHeat1 = 0;
  632. }
  633. }
  634. else
  635. {
  636. if (in->swIPMTempCe > coef->uwIPMOvrHeatValCe) // 85
  637. {
  638. alm_stDecCt.ulIPMOvrHeat++;
  639. if (alm_stDecCt.ulIPMOvrHeat >= coef->ulIPMOvrHeatValCt) // 2s
  640. {
  641. alm_stDecCt.ulIPMOvrHeat = coef->ulIPMOvrHeatValCt;
  642. // alm_unAction.bit.ThrPhsShrt = TRUE;
  643. // alm_unAction.bit.PWMOff = TRUE;
  644. alm_unCode.bit.IPMOvrHeat = TRUE;
  645. alm_pvt_blIPMOTReCheckFlg = TRUE;
  646. }
  647. }
  648. else
  649. {
  650. alm_stDecCt.ulIPMOvrHeat = 0;
  651. }
  652. }
  653. /*=======================================================================
  654. Motor over heat
  655. =======================================================================*/
  656. if (alm_pvt_blMotorOTReCheckFlg == TRUE)
  657. {
  658. if (in->uwMotorTempCe > coef->uwMotorOvrHeatRecValCe) // 70
  659. {
  660. alm_stDecCt.ulMotorOvrHeat1++;
  661. if (alm_stDecCt.ulMotorOvrHeat1 >= coef->ulMotorOvrHeatValCt) // 2s
  662. {
  663. alm_stDecCt.ulMotorOvrHeat1 = coef->ulMotorOvrHeatValCt;
  664. // alm_unAction.bit.ThrPhsShrt = TRUE;
  665. // alm_unAction.bit.PWMOff = TRUE;
  666. alm_unCode.bit.MotorOvrHeat = TRUE;
  667. }
  668. }
  669. else
  670. {
  671. alm_stDecCt.ulMotorOvrHeat1 = 0;
  672. }
  673. }
  674. else
  675. {
  676. if (in->uwMotorTempCe > coef->uwMotorOvrHeatValCe) // 85
  677. {
  678. alm_stDecCt.ulMotorOvrHeat++;
  679. if (alm_stDecCt.ulMotorOvrHeat >= coef->ulMotorOvrHeatValCt) // 2s
  680. {
  681. alm_stDecCt.ulMotorOvrHeat = coef->ulMotorOvrHeatValCt;
  682. // alm_unAction.bit.ThrPhsShrt = TRUE;
  683. // alm_unAction.bit.PWMOff = TRUE;
  684. alm_unCode.bit.MotorOvrHeat = TRUE;
  685. alm_pvt_blMotorOTReCheckFlg = TRUE;
  686. }
  687. }
  688. else
  689. {
  690. alm_stDecCt.ulMotorOvrHeat = 0;
  691. }
  692. }
  693. /*=======================================================================
  694. Hall loss // MPS position loss
  695. =======================================================================*/
  696. if(cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL)
  697. {
  698. if(((in->uwSectorNum == 0)||(in->uwSectorNum == 7)))
  699. {
  700. alm_stDecCt.ulHallLoss ++;
  701. }
  702. else
  703. {
  704. alm_stDecCt.ulHallLoss = 0;
  705. }
  706. }
  707. else
  708. {
  709. alm_stDecCt.ulHallLoss = 0;
  710. }
  711. #if 0//(EMCDEAL_EN != 0)
  712. if(EcmDeal.EmcModeFlag==TRUE)
  713. {
  714. alm_stDecCt.ulHallLoss=0;
  715. }
  716. else
  717. #endif
  718. {
  719. #if(EMCDEAL_EN!=0)
  720. if(alm_stDecCt.ulHallLoss >= 2)
  721. #else
  722. if(alm_stDecCt.ulHallLoss >= 50) //250ms
  723. #endif
  724. {
  725. alm_stDecCt.ulHallLoss = 0;
  726. // alm_unAction.bit.PWMOff = TRUE;
  727. alm_unCode.bit.HallLoss = TRUE;
  728. }
  729. }
  730. // cp_stHistoryPara.uwPosSensorAlamTimes++;
  731. /*=======================================================================
  732. Communication over time
  733. =======================================================================*/
  734. // if (uart_bCommOvrTmFlg)
  735. // {
  736. // alm_unAction.bit.PWMOff = TRUE;
  737. // // alm_unAction.bit.ThrPhsShrt = TRUE;
  738. // alm_unCode.bit.CommOvrTm = TRUE;
  739. // }
  740. alm_pvt_blTbsFirstFlg = FALSE;
  741. }
  742. }
  743. /***************************************************************
  744. Function:
  745. Description:
  746. Call by:
  747. Input Variables:
  748. Output/Return Variables:
  749. Subroutine Call:
  750. Reference:
  751. ****************************************************************/
  752. void alm_voHandleTBC(ALM_IN *in)
  753. {
  754. /* Alarm occur */
  755. if (alm_blAlmOccrFlg)
  756. {
  757. /* mircfault */
  758. // if(clasB_unFaultCode.all)
  759. // {
  760. // if(scm_stSpdFbkLpf.slY.sw.hi < SPD_RPM2PU(10))//puֵ Լ10rpm
  761. // {
  762. // NVIC_SystemReset();
  763. // }
  764. // }
  765. /* Alarm handle FSM */
  766. switch (alm_enFSMStatus)
  767. {
  768. case Alm_Stop:
  769. alm_voStopTBC(in, &alm_stStopTbcCoef); /* Stop in TBC */
  770. if (cmfsm_stFlg.blMotorStopFlg)
  771. {
  772. sysfsm_stFlg.blFSMRstOvrFlg = FALSE; /* Enable control mode FSM reset */
  773. sysfsm_stFlg.blCtrlMdVarClcOvrFlg = FALSE; /* Enable control mode variable clear */
  774. alm_enFSMStatus = Alm_VarClc;
  775. }
  776. break;
  777. case Alm_VarClc:
  778. if (sysfsm_stFlg.blFSMRstOvrFlg && sysfsm_stFlg.blCtrlMdVarClcOvrFlg)
  779. {
  780. alm_enFSMStatus = Alm_Reset;
  781. }
  782. break;
  783. case Alm_Reset:
  784. alm_voReset(in, &alm_stResetCoef);
  785. break;
  786. default:
  787. break;
  788. }
  789. }
  790. }
  791. /***************************************************************
  792. Function:
  793. Description:
  794. Call by:
  795. Input Variables:
  796. Output/Return Variables:
  797. Subroutine Call:
  798. Reference:
  799. ****************************************************************/
  800. void alm_voHandleTBS(ALM_IN *in)
  801. {
  802. /* Alarm occur */
  803. if (alm_blAlmOccrFlg)
  804. {
  805. /* Alarm handle FSM */
  806. switch (alm_enFSMStatus)
  807. {
  808. case Alm_Stop:
  809. // alm_voStopTBS(); /* Stop in TBS */
  810. break;
  811. case Alm_VarClc:
  812. break;
  813. case Alm_Reset:
  814. // alm_voReset(in,&alm_stResetCoef);
  815. break;
  816. default:
  817. break;
  818. }
  819. }
  820. }
  821. /***************************************************************
  822. Function:
  823. Description:
  824. Call by:
  825. Input Variables:
  826. Output/Return Variables:
  827. Subroutine Call:
  828. Reference:
  829. ****************************************************************/
  830. void alm_voStopTBCCoef(ALM_STOPTBC_COFIN *in, ALM_STOPTBC_COF *out)
  831. {
  832. if (in->uwIbAp < 1)
  833. {
  834. in->uwIbAp = 1;
  835. }
  836. if (in->uwUbVt < 1)
  837. {
  838. in->uwUbVt = 1;
  839. }
  840. out->ulThrPhsShrtFrcValCt = ((ULONG)in->uwThrPhsShrtFrcTs * in->uwFTbcHz) / 1000; // TBC_S2CT(x) ((ULONG)(x)*FTBC_HZ)
  841. out->ulStopCurValCt = ((ULONG)in->uwIPMOverCurStopTs * in->uwFTbcHz) / 1000;
  842. out->ulPWMOffShrt1SwValCt = ((ULONG)in->uwPwmoffShrt1SwTs * in->uwFTbcHz) / 1000;
  843. out->ulShrtPWMOffSwValCt = ((ULONG)in->uwShrtPwmoffSwTs * in->uwFTbcHz) / 1000;
  844. out->ulPWMOffValCt = ((ULONG)in->uwPwmOffTs * in->uwFTbcHz) / 1000;
  845. out->ulThrPhsShrtNormValCt = ((ULONG)in->uwThrPhsShrtTs * in->uwFTbcHz) / 1000;
  846. out->ulOvrVltLvl1ValPu = (((ULONG)in->uwOvrVlt1Vt << 14) / in->uwUbVt) / 1000;
  847. }
  848. /***************************************************************
  849. Function:
  850. Description:
  851. Call by:
  852. Input Variables:
  853. Output/Return Variables:
  854. Subroutine Call:
  855. Reference:
  856. ****************************************************************/
  857. void alm_voStopTBC(ALM_IN *in, ALM_STOPTBC_COF *coef)
  858. {
  859. if (alm_unAction.bit.ThrPhsShrtFrc)
  860. {
  861. if (MCU_BRKIN_FLG)
  862. {
  863. hw_voPWMOff(); /* PWM off */
  864. MCU_BRKIN_FLG_CLR; /* Clear TIME0 break flag */
  865. MCU_POEN_FLG_EN; /* Enable TIME0 Channel outputs */
  866. alm_stStopCt.ulThrPhsShrtFrc++;
  867. if (alm_stStopCt.ulThrPhsShrtFrc > coef->ulThrPhsShrtFrcValCt) // 200ms
  868. {
  869. alm_stStopCt.uwRotorStopCnt++;
  870. if (alm_stStopCt.uwRotorStopCnt >= coef->ulStopCurValCt) // 100ms
  871. {
  872. hw_voPWMOff(); /* PWM off */
  873. cmfsm_stFlg.blMotorStopFlg = TRUE;
  874. alm_stStopCt.uwRotorStopCnt = coef->ulStopCurValCt;
  875. }
  876. }
  877. alm_stStopCt.ulShrtPWMOffSw = 0;
  878. }
  879. else
  880. {}
  881. }
  882. else if (alm_unAction.bit.PWMOff)
  883. {
  884. alm_stStopCt.ulPWMOffShrtSw++;
  885. alm_stStopCt.ulPWMOff++;
  886. if (alm_stStopCt.ulPWMOffShrtSw < coef->ulPWMOffShrt1SwValCt) // 100ms
  887. {
  888. hw_voPWMOff();
  889. alm_stStopCt.uwRotorStopCnt = 0;
  890. }
  891. // else if(alm_stStopCt.ulPWMOffShrtSw < coef->ulPWMOffShrt2SwValCt)
  892. // {
  893. // hw_voThrPhsShrt();
  894. // }
  895. else
  896. {
  897. alm_stStopCt.ulPWMOffShrtSw = 0;
  898. }
  899. if (alm_stStopCt.ulPWMOff > coef->ulPWMOffValCt) // 200ms
  900. {
  901. cmfsm_stFlg.blMotorStopFlg = TRUE;
  902. alm_stStopCt.ulPWMOff = 0;
  903. }
  904. alm_stStopCt.ulShrtPWMOffSw = 0;
  905. }
  906. else // if (alm_unAction.bit.ThrPhsShrt)
  907. {
  908. alm_stStopCt.ulShrtPWMOffSw++;
  909. alm_stStopCt.ulThrPhsShrt++;
  910. /* if (alm_stStopCt.ulShrtPWMOffSw < coef->ulThrPhsShrtNormValCt) // 100ms
  911. {
  912. hw_voThrPhsShrt(); // Three phase short
  913. }
  914. else*/ if (alm_stStopCt.ulShrtPWMOffSw < (coef->ulThrPhsShrtNormValCt + coef->ulShrtPWMOffSwValCt)) // 100ms + 100ms
  915. {
  916. hw_voPWMOff();
  917. if (in->uwVdcPu > coef->ulOvrVltLvl1ValPu) // over vol level1 43V
  918. {
  919. alm_stStopCt.ulShrtPWMOffSw = 0;
  920. }
  921. alm_stStopCt.uwRotorStopCnt = 0;
  922. }
  923. else
  924. {
  925. alm_stStopCt.ulShrtPWMOffSw = 0;
  926. }
  927. if (alm_stStopCt.ulThrPhsShrt > coef->ulThrPhsShrtFrcValCt) // 200ms
  928. {
  929. hw_voPWMOff(); /* PWM off */
  930. cmfsm_stFlg.blMotorStopFlg = TRUE;
  931. alm_stStopCt.ulThrPhsShrt = 0;
  932. }
  933. alm_stStopCt.ulPWMOffShrtSw = 0;
  934. }
  935. }
  936. /***************************************************************
  937. Function:
  938. Description:
  939. Call by:
  940. Input Variables:
  941. Output/Return Variables:
  942. Subroutine Call:
  943. Reference:
  944. ****************************************************************/
  945. void alm_voStopTBS(void)
  946. {
  947. // if (alm_unAction.bit.SlowDwn)
  948. // {
  949. // acm_voAlmCtrMdTbs();
  950. // }
  951. }
  952. /***************************************************************
  953. Function:
  954. Description:
  955. Call by:
  956. Input Variables:
  957. Output/Return Variables:
  958. Subroutine Call:
  959. Reference:
  960. ****************************************************************/
  961. void alm_voResetCoef(ALM_RESET_COFIN *in, ALM_RESET_COF *out)
  962. {
  963. if (in->uwUbVt < 1)
  964. {
  965. in->uwUbVt = 1;
  966. }
  967. out->ulRecAllValCt = ((ULONG)in->uwRecAllTs * in->uwFTbcHz) / 1000; // TBC_S2CT(x) ((ULONG)(x)*FTBC_HZ)
  968. out->ulIPMOcRecValCt = ((ULONG)in->uwIpmOcRecTs * in->uwFTbcHz) / 1000;
  969. out->ulOvrVltRecValPu = ((ULONG)in->uwOvrVltRecVt << 14) / in->uwUbVt;
  970. out->ulOvrVltRecValCt = ((ULONG)in->uwOvrVltRecTs * in->uwFTbcHz) / 1000;
  971. out->ulUndrVltRecValPu = ((ULONG)in->uwUndrVltRecVt << 14) / in->uwUbVt;
  972. out->ulUndrVltRecValCt = ((ULONG)in->uwUndrVltRecTs * in->uwFTbcHz) / 1000;
  973. out->uwIPMOvrHeatRecValCe = in->uwIpmOvrHeatRecCe;
  974. out->ulIPMOvrHeatRecValCt = ((ULONG)in->uwIpmOvrHeatRecTs * in->uwFTbcHz);
  975. out->ulIPMOvrHeatRec1ValCt = ((ULONG)in->uwIpmOvrHeatRec1Ts * in->uwFTbcHz);
  976. out->uwMotorOvrHeatRecValCe = in->uwMotorOvrHeatRecCe;
  977. out->ulMotorOvrHeatRecValCt = ((ULONG)in->uwMotorOvrHeatRecTs * in->uwFTbcHz);
  978. out->ulMotorOvrHeatRec1ValCt = ((ULONG)in->uwMotorOvrHeatRec1Ts * in->uwFTbcHz);
  979. }
  980. /***************************************************************
  981. Function:
  982. Description:
  983. Call by:
  984. Input Variables:
  985. Output/Return Variables:
  986. Subroutine Call:
  987. Reference:
  988. ****************************************************************/
  989. void alm_voReset(ALM_IN *in, ALM_RESET_COF *coef)
  990. {
  991. /*=======================================================================
  992. Recover condition
  993. =======================================================================*/
  994. /* Recover time of global */
  995. if (alm_stRecCt.ulGlbl < coef->ulRecAllValCt) // 200ms
  996. {
  997. alm_stRecCt.ulGlbl++;
  998. }
  999. /* Recover time of IPM OC */
  1000. if (alm_stRecCt.ulIPMOC < coef->ulIPMOcRecValCt) // 100ms
  1001. {
  1002. alm_stRecCt.ulIPMOC++;
  1003. }
  1004. /* Recover time of over voltage */
  1005. if (in->uwVdcPu < coef->ulOvrVltRecValPu) // 450v
  1006. {
  1007. if (alm_stRecCt.ulOvrVlt < coef->ulOvrVltRecValCt) // 100ms
  1008. {
  1009. alm_stRecCt.ulOvrVlt++;
  1010. }
  1011. }
  1012. /* Recover time of under voltage */
  1013. if (in->uwVdcPu > coef->ulUndrVltRecValPu) // 30v
  1014. {
  1015. if (alm_stRecCt.ulUndrVlt < coef->ulUndrVltRecValCt) // 100ms
  1016. {
  1017. alm_stRecCt.ulUndrVlt++;
  1018. }
  1019. }
  1020. /* Recover time of IPM over heat */
  1021. if (in->swIPMTempCe < coef->uwIPMOvrHeatRecValCe) // 75
  1022. {
  1023. if (alm_stRecCt.ulIPMOvrHeat < coef->ulIPMOvrHeatRecValCt) // 60s
  1024. {
  1025. alm_stRecCt.ulIPMOvrHeat++;
  1026. }
  1027. alm_stRecCt.ulIPMOvrHeat1 = 0;
  1028. }
  1029. else
  1030. {
  1031. if (alm_stRecCt.ulIPMOvrHeat1 < coef->ulIPMOvrHeatRec1ValCt) // 120s
  1032. {
  1033. alm_stRecCt.ulIPMOvrHeat1++;
  1034. }
  1035. alm_stRecCt.ulIPMOvrHeat = 0;
  1036. }
  1037. /*=======================================================================
  1038. Alarm code clear
  1039. =======================================================================*/
  1040. if (alm_stRecCt.ulGlbl >= coef->ulRecAllValCt) // 200ms
  1041. {
  1042. /* IPM fault */
  1043. if ((alm_unCode.bit.IPMFlt) && (alm_stRecCt.ulIPMOC >= coef->ulIPMOcRecValCt))
  1044. {
  1045. MCU_BRKIN_FLG_CLR; /* Clear TIME0 break flag */
  1046. MCU_POEN_FLG_EN; /* Enable TIME0 Channel outputs */
  1047. // DL_GPIO_clearPins(GPIOA, DL_GPIO_PIN_31);
  1048. alm_unCode.bit.IPMFlt = FALSE;
  1049. // IO_FTESTLED_OFF();// DL_TimerG_setCaptureCompareValue(PWM_F_INST, 0, GPIO_PWM_F_C1_IDX); //指示 test
  1050. }
  1051. /* Over current */
  1052. if (alm_unCode.bit.OvrCur)
  1053. {
  1054. alm_stDecCt.ulOvrCur = 0;
  1055. alm_unCode.bit.OvrCur = FALSE;
  1056. }
  1057. /* Over voltage */
  1058. if (alm_unCode.bit.OvrVlt)
  1059. {
  1060. if (alm_stRecCt.ulOvrVlt >= coef->ulOvrVltRecValCt) // 4s 20s
  1061. {
  1062. alm_stDecCt.ulOvrVltLvl1 = 0;
  1063. alm_unCode.bit.OvrVlt = FALSE;
  1064. }
  1065. }
  1066. /* Under voltage */
  1067. if (alm_unCode.bit.UndrVlt)
  1068. {
  1069. if (alm_stRecCt.ulUndrVlt >= coef->ulUndrVltRecValCt) // 4s 20s
  1070. {
  1071. alm_stDecCt.ulUndrVltLvl1 = 0;
  1072. alm_unCode.bit.UndrVlt = FALSE;
  1073. }
  1074. }
  1075. /* IPM over heat */
  1076. if (alm_unCode.bit.IPMOvrHeat)
  1077. {
  1078. if (alm_stRecCt.ulIPMOvrHeat >= coef->ulIPMOvrHeatRecValCt)
  1079. {
  1080. alm_stDecCt.ulIPMOvrHeat = 0;
  1081. alm_stDecCt.ulIPMOvrHeat1 = 0;
  1082. alm_unCode.bit.IPMOvrHeat = FALSE;
  1083. alm_pvt_blIPMOTReCheckFlg = FALSE;
  1084. }
  1085. else if (alm_stRecCt.ulIPMOvrHeat1 >= coef->ulIPMOvrHeatRec1ValCt)
  1086. {
  1087. alm_stDecCt.ulIPMOvrHeat = 0;
  1088. alm_stDecCt.ulIPMOvrHeat1 = 0;
  1089. alm_unCode.bit.IPMOvrHeat = FALSE;
  1090. alm_pvt_blIPMOTReCheckFlg = TRUE;
  1091. }
  1092. }
  1093. /* Motor over heat */
  1094. if (alm_unCode.bit.MotorOvrHeat)
  1095. {
  1096. if (alm_stRecCt.ulMotorOvrHeat >= coef->ulMotorOvrHeatRecValCt)
  1097. {
  1098. alm_stDecCt.ulMotorOvrHeat = 0;
  1099. alm_stDecCt.ulMotorOvrHeat1 = 0;
  1100. alm_unCode.bit.MotorOvrHeat = FALSE;
  1101. alm_pvt_blMotorOTReCheckFlg = FALSE;
  1102. }
  1103. else if (alm_stRecCt.ulMotorOvrHeat1 >= coef->ulMotorOvrHeatRec1ValCt)
  1104. {
  1105. alm_stDecCt.ulMotorOvrHeat = 0;
  1106. alm_stDecCt.ulMotorOvrHeat1 = 0;
  1107. alm_unCode.bit.MotorOvrHeat = FALSE;
  1108. alm_pvt_blMotorOTReCheckFlg = TRUE;
  1109. }
  1110. }
  1111. /* Hall loss */
  1112. if (alm_unCode.bit.HallLoss)
  1113. {
  1114. if(((in->uwSectorNum != 0)&&(in->uwSectorNum != 7))||(cp_stFlg.ThetaGetModelSelect != ANG_SWITCHHALL))
  1115. {
  1116. alm_stDecCt.ulHallLoss = 0;
  1117. alm_unCode.bit.HallLoss = FALSE;
  1118. }
  1119. }
  1120. /* Phase loss */
  1121. if ((alm_unCode.bit.PhsLoss) && (signal_state.Assist == 0 && signal_state.Sensor == 0))
  1122. {
  1123. alm_stDecCt.ulPhsALoss = 0;
  1124. alm_stDecCt.ulPhsBLoss = 0;
  1125. alm_stDecCt.ulPhsCLoss = 0;
  1126. alm_unCode.bit.PhsLoss = FALSE;
  1127. }
  1128. /* Rotor lock */
  1129. if ((alm_unCode.bit.RotorLock) && (signal_state.Assist == 0 && signal_state.Sensor == 0))
  1130. {
  1131. alm_stDecCt.slRotorLock = 0;
  1132. alm_unCode.bit.RotorLock = FALSE;
  1133. }
  1134. /* Over Speed */
  1135. if(alm_unCode.bit.OvrSpd)
  1136. {
  1137. alm_stDecCt.ulOvrSpd = 0;
  1138. alm_unCode.bit.OvrSpd = FALSE;
  1139. }
  1140. /* Communication over time */
  1141. if (alm_unCode.bit.CommOvrTm)
  1142. {
  1143. alm_unCode.bit.CommOvrTm = FALSE;
  1144. }
  1145. if (alm_unCode.bit.ADCOffsetFlt)
  1146. {
  1147. /* ADC init */
  1148. adc_stDownOut.ulIdcRegSum = 0;
  1149. adc_stDownOut.ulUaRegSum = 0;
  1150. adc_stDownOut.ulUbRegSum = 0;
  1151. adc_stDownOut.ulUcRegSum = 0;
  1152. adc_stDownOut.uwADCCalibCt = 0;
  1153. adc_stDownOut.blADCCalibFlg = FALSE;
  1154. adc_stUpOut.uwADCCalibCt = 0;
  1155. adc_stUpOut.blADCCalibFlg = FALSE;
  1156. sysfsm_stFlg.blADCInitOvrFlg = FALSE;
  1157. alm_unCode.bit.ADCOffsetFlt = FALSE;
  1158. }
  1159. }
  1160. /*=======================================================================
  1161. Alarm flag clear
  1162. =======================================================================*/
  1163. if (!alm_unCode.all)
  1164. {
  1165. /* Clear alarm action */
  1166. alm_unAction.all = 0;
  1167. /* Clear stop count */
  1168. alm_stStopCt.ulThrPhsShrtFrc = 0;
  1169. alm_stStopCt.ulPWMOff = 0;
  1170. alm_stStopCt.ulThrPhsShrt = 0;
  1171. alm_stStopCt.uwRotorStopCnt = 0;
  1172. alm_stStopCt.ulPWMOffShrtSw = 0;
  1173. alm_stStopCt.ulShrtPWMOffSw = 0;
  1174. /* Clear recover count */
  1175. alm_stRecCt.ulGlbl = 0;
  1176. alm_stRecCt.ulOvrVlt = 0;
  1177. alm_stRecCt.ulOvrVlt1 = 0;
  1178. alm_stRecCt.ulUndrVlt = 0;
  1179. alm_stRecCt.ulUndrVlt1 = 0;
  1180. alm_stRecCt.ulIPMOvrHeat = 0;
  1181. alm_stRecCt.ulIPMOvrHeat1 = 0;
  1182. alm_stRecCt.ulIPMOC = 0;
  1183. if(alm_unBikeCode.all == 0)
  1184. {
  1185. /* Clear alarm flag */
  1186. alm_blAlmOccrFlg = FALSE;
  1187. alm_blWarnOccrFlg = FALSE;
  1188. alm_pvt_ulWarn2ErrorCount = 0;
  1189. alm_blAlmSingleRecordDoneFlg = FALSE;
  1190. }
  1191. }
  1192. }
  1193. /***************************************************************
  1194. Function:
  1195. Description:
  1196. Call by:
  1197. Input Variables:
  1198. Output/Return Variables:
  1199. Subroutine Call:
  1200. Reference:
  1201. ****************************************************************/
  1202. void alm_voHandleRst(void)
  1203. {
  1204. alm_enFSMStatus = Alm_Stop;
  1205. }
  1206. /***************************************************************
  1207. Function:
  1208. Description:
  1209. Call by:
  1210. Input Variables:
  1211. Output/Return Variables:
  1212. Subroutine Call:
  1213. Reference:
  1214. ****************************************************************/
  1215. void alm_voDetec200MSCoef(ALM_DETEC200MS_COFIN *in, ALM_DETEC200MS_COF *out)
  1216. {
  1217. if (in->uwFbHz < 1)
  1218. {
  1219. in->uwFbHz = 1;
  1220. }
  1221. out->swMotorSpdMinPu = (SWORD)(((SLONG)in->swMotorSpdMinRpm << 15) / (SWORD)60 * (SWORD)in->uwPairs / (SWORD)in->uwFbHz);
  1222. out->uwTorqMinPu = (UWORD)(((ULONG)in->uwTorqMinNm << 14) / TORQUEBASE);
  1223. out->uwBikeSpdFltCt = in->uwBikeSpdFltTs * in->uwFT200MSHz;
  1224. out->uwCadenceFltCt = in->uwCadenceFltTs * in->uwFT200MSHz;
  1225. out->uwTorqFltCt = in->uwTorqFltTs * in->uwFT200MSHz;
  1226. out->uwThrottleFltCt = in->uwThrottleFltTs * in->uwFT200MSHz;
  1227. out->uwPCBNTCFltCt = in->uwPCBNTCFltTs * in->uwFT200MSHz;
  1228. out->uwMotorNTCFltCt = in->uwMotorNTCFltTs * in->uwFT200MSHz;
  1229. out->uwTorqMaxReg = (UWORD)((ULONG)in->uwTorqMaxVol * 4096 / 33);
  1230. out->uwTorqMinReg = (UWORD)((ULONG)in->uwTorqMinVol * 4096 / 33);
  1231. out->uwThrottleMaxReg = (UWORD)((ULONG)in->uwThrottleMaxVol * 4096 / 33);
  1232. out->uwThrottleMinReg = (UWORD)((ULONG)in->uwThrottleMinVol * 4096 / 33);
  1233. out->uwNTCMaxReg = (UWORD)((ULONG)in->uwNTCMaxVol * 4096 / 33);
  1234. out->uwNTCMinReg = (UWORD)((ULONG)in->uwNTCMinVol * 4096 / 33);
  1235. }
  1236. /***************************************************************
  1237. Function:
  1238. Description:
  1239. Call by:
  1240. Input Variables:
  1241. Output/Return Variables:
  1242. Subroutine Call:
  1243. Reference:
  1244. ****************************************************************/
  1245. void alm_voDetec200MS(const ALM_BIKE_IN *in, const ALM_DETEC200MS_COF *coef)
  1246. {
  1247. /* Bike speed sensor fault */
  1248. if(in->uwCadenceFreqPu > 150 || in->swMotorSpdAbsPu > coef->swMotorSpdMinPu) // 0.2Hz 150
  1249. {
  1250. if(in->uwBikeSpdPu < 75) // 0.1hz 75
  1251. {
  1252. alm_stDecCt.uwBikeSpdFlt ++;
  1253. if(alm_stDecCt.uwBikeSpdFlt >= coef->uwBikeSpdFltCt)
  1254. {
  1255. alm_stDecCt.uwBikeSpdFlt = coef->uwBikeSpdFltCt;
  1256. alm_unBikeCode.bit.BikeSpdSen = 1;
  1257. // alm_unAction.bit.PWMOff = 1;
  1258. }
  1259. }
  1260. else if(in->blBikeSpdOvrFlg)
  1261. {
  1262. alm_unBikeCode.bit.BikeSpdSen = 1;
  1263. // alm_unAction.bit.PWMOff = 1;
  1264. }
  1265. else
  1266. {
  1267. alm_stDecCt.uwBikeSpdFlt = 0;
  1268. }
  1269. }
  1270. else
  1271. {
  1272. alm_stDecCt.uwBikeSpdFlt = 0;
  1273. }
  1274. if(power_stPowStateOut.blPowerStartupFlg == TRUE)
  1275. {
  1276. if(cp_stFlg.RunModelSelect == CadAssist) /* 纯踏频助力判断逻辑 */
  1277. {
  1278. /* Bike cadence sensor fault */
  1279. if(in->blCadenceFreqOvrFlg)
  1280. {
  1281. alm_unBikeCode.bit.CadenceSen = 1;
  1282. // alm_unAction.bit.PWMOff = 1;
  1283. }
  1284. }
  1285. else if((cp_stFlg.RunModelSelect == TorqAssist) &&(power_stPowStateOut.blPowerStartupFlg == TRUE))/* 力矩中轴助力判断逻辑 */
  1286. {
  1287. /* Bike cadence sensor fault */
  1288. if(in->uwBikeSpdPu > 475 && in->uwTroqPu > coef->uwTorqMinPu) //475-5km/h
  1289. {
  1290. if(in->uwCadenceFreqPu == 0)
  1291. {
  1292. alm_stDecCt.uwCadenceFlt ++;
  1293. if(alm_stDecCt.uwCadenceFlt >= coef->uwCadenceFltCt)
  1294. {
  1295. alm_stDecCt.uwCadenceFlt = coef->uwCadenceFltCt;
  1296. alm_unBikeCode.bit.CadenceSen = 1;
  1297. // alm_unAction.bit.PWMOff = 1;
  1298. }
  1299. }
  1300. else if(in->blCadenceFreqOvrFlg)
  1301. {
  1302. alm_unBikeCode.bit.CadenceSen = 1;
  1303. // alm_unAction.bit.PWMOff = 1;
  1304. }
  1305. else
  1306. {
  1307. alm_stDecCt.uwCadenceFlt = 0;
  1308. }
  1309. }
  1310. else
  1311. {
  1312. alm_stDecCt.uwCadenceFlt = 0;
  1313. }
  1314. /* Bike torque sensor fault */
  1315. if((in->uwTroqReg < coef->uwTorqMinReg) || (in->uwTroqReg >= coef->uwTorqMaxReg)) //Fault: U_Torq < 0.1V or >=3V
  1316. {
  1317. alm_stDecCt.uwTorqFlt ++;
  1318. if(alm_stDecCt.uwTorqFlt >= coef->uwTorqFltCt)
  1319. {
  1320. alm_stDecCt.uwTorqFlt = coef->uwTorqFltCt;
  1321. alm_unBikeCode.bit.TorqSen = 1;
  1322. // alm_unAction.bit.PWMOff = 1;
  1323. }
  1324. }
  1325. else
  1326. {
  1327. alm_stDecCt.uwTorqFlt = 0;
  1328. }
  1329. }
  1330. }
  1331. /* Bike throttle fault */
  1332. #if 0
  1333. if(in->blThrottleExistFlg)
  1334. {
  1335. if((in->uwThrottleReg < coef->uwThrottleMinReg) || (in->uwThrottleReg >= coef->uwThrottleMaxReg)) // Fault: U_Throttle < 0.1V or >=3V
  1336. {
  1337. alm_stDecCt.uwThrottleFlt ++;
  1338. if(alm_stDecCt.uwThrottleFlt >= coef->uwThrottleFltCt)
  1339. {
  1340. alm_stDecCt.uwThrottleFlt = coef->uwThrottleFltCt;
  1341. alm_unBikeCode.bit.Throttle = 1;
  1342. // alm_unAction.bit.PWMOff = 1;
  1343. }
  1344. }
  1345. else
  1346. {
  1347. alm_stDecCt.uwThrottleFlt = 0;
  1348. }
  1349. }
  1350. #endif
  1351. /* PCB NTC fault */
  1352. // if((in->uwPCBNTCReg < coef->uwNTCMinReg) || (in->uwPCBNTCReg > coef->uwNTCMaxReg)) // Fault: NTC>100k or <0.032k
  1353. // {
  1354. // alm_stDecCt.uwPCBNTCFlt ++;
  1355. // if(alm_stDecCt.uwPCBNTCFlt >= coef->uwPCBNTCFltCt)
  1356. // {
  1357. // alm_stDecCt.uwPCBNTCFlt = coef->uwPCBNTCFltCt;
  1358. // alm_unBikeCode.bit.PCBNTC = 1;
  1359. // alm_unAction.bit.PWMOff = 1;
  1360. // }
  1361. // }
  1362. // else
  1363. // {
  1364. // alm_stDecCt.uwPCBNTCFlt = 0;
  1365. // }
  1366. /* Motor NTC fault */
  1367. if(in->blMotorNTCExistFlg)
  1368. {
  1369. if((in->uwMotorNTCReg < coef->uwNTCMinReg) || (in->uwMotorNTCReg > coef->uwNTCMaxReg)) // Fault: NTC>100k or <0.032k
  1370. {
  1371. alm_stDecCt.uwMotorNTCFlt ++;
  1372. if(alm_stDecCt.uwMotorNTCFlt >= coef->uwMotorNTCFltCt)
  1373. {
  1374. alm_stDecCt.uwMotorNTCFlt = coef->uwMotorNTCFltCt;
  1375. alm_unBikeCode.bit.MotorNTC = 1;
  1376. // alm_unAction.bit.PWMOff = 1;
  1377. }
  1378. }
  1379. else
  1380. {
  1381. alm_stDecCt.uwMotorNTCFlt = 0;
  1382. }
  1383. }
  1384. }
  1385. /***************************************************************
  1386. Function:
  1387. Description:
  1388. Call by:
  1389. Input Variables:
  1390. Output/Return Variables:
  1391. Subroutine Call:
  1392. Reference:
  1393. ****************************************************************/
  1394. void alm_voHandle1MS(const ALM_BIKE_IN *in)
  1395. {
  1396. if(alm_unBikeCode.all != 0)
  1397. {
  1398. /* Alarm handle FSM */
  1399. switch (alm_enBikeFSMStatus)
  1400. {
  1401. case Alm_Stop:
  1402. if (alm_unAction.bit.PWMOff != 0)
  1403. {
  1404. alm_stStopCt.ulBikePWMOff++;
  1405. hw_voPWMOff();
  1406. // sysctrl_voPwmOff();
  1407. if (alm_stStopCt.ulBikePWMOff > 200) // 200ms, SysFault_hook, Event 1ms
  1408. {
  1409. cmfsm_stFlg.blMotorStopFlg = TRUE;
  1410. alm_stStopCt.ulBikePWMOff = 0;
  1411. }
  1412. }
  1413. if (cmfsm_stFlg.blMotorStopFlg)
  1414. {
  1415. sysfsm_stFlg.blFSMRstOvrFlg = FALSE; // Enable control mode FSM reset
  1416. sysfsm_stFlg.blCtrlMdVarClcOvrFlg = FALSE; // Enable control mode variable clear
  1417. alm_enBikeFSMStatus = Alm_VarClc;
  1418. }
  1419. break;
  1420. case Alm_VarClc:
  1421. if (sysfsm_stFlg.blFSMRstOvrFlg && sysfsm_stFlg.blCtrlMdVarClcOvrFlg)
  1422. {
  1423. alm_enBikeFSMStatus = Alm_Reset;
  1424. }
  1425. break;
  1426. case Alm_Reset:
  1427. alm_voReset1MS(in, &alm_stReset1MSCoef, &alm_stDetect200MSCoef);
  1428. break;
  1429. default:
  1430. break;
  1431. }
  1432. }
  1433. }
  1434. /***************************************************************
  1435. Function:
  1436. Description:
  1437. Call by:
  1438. Input Variables:
  1439. Output/Return Variables:
  1440. Subroutine Call:
  1441. Reference:
  1442. ****************************************************************/
  1443. void alm_voReset1MSCoef(const ALM_RESET1MS_COFIN *in, ALM_RESET1MS_COF *out)
  1444. {
  1445. out->ulRecAllValCt = (ULONG)in->uwBikeGlblTm * in->uwFT1MSHz / 1000;
  1446. out->ulRecBikeSpdCt = (ULONG)in->uwBikeSpdFltTm * in->uwFT1MSHz / 1000;
  1447. out->ulRecCadenceCt = (ULONG)in->uwCadenceFltTm * in->uwFT1MSHz / 1000;
  1448. out->ulRecTorqCt = (ULONG)in->uwTorqFltTm * in->uwFT1MSHz / 1000;
  1449. out->ulRecThrottleCt = (ULONG)in->uwThrottleFltTm * in->uwFT1MSHz / 1000;
  1450. out->ulRecPCBNTCCt = (ULONG)in->uwPCBNTCFltTm * in->uwFT1MSHz / 1000;
  1451. out->ulRecMotorNTCCt = (ULONG)in->uwMotorNTCFltTm * in->uwFT1MSHz / 1000;
  1452. }
  1453. /***************************************************************
  1454. Function:
  1455. Description:
  1456. Call by:
  1457. Input Variables:
  1458. Output/Return Variables:
  1459. Subroutine Call:
  1460. Reference:
  1461. ****************************************************************/
  1462. void alm_voReset1MS(const ALM_BIKE_IN *in, const ALM_RESET1MS_COF *coef, const ALM_DETEC200MS_COF *detctcoef) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
  1463. {
  1464. /*=======================================================================
  1465. Recover condition
  1466. =======================================================================*/
  1467. /* Recover time of global */
  1468. if (alm_stRecCt.ulBikeGlbl < coef->ulRecAllValCt) // 1s
  1469. {
  1470. alm_stRecCt.ulBikeGlbl++;
  1471. }
  1472. /* Recover time of bike speed sensor fault */
  1473. if((!in->blBikeSpdOvrFlg) && (in->uwBikeSpdPu > 0))
  1474. {
  1475. if(alm_stRecCt.ulBikeSpdFlt < coef->ulRecBikeSpdCt)
  1476. {
  1477. alm_stRecCt.ulBikeSpdFlt++;
  1478. }
  1479. }
  1480. /* Recover time of bike candence sensor fault */
  1481. if((!in->blCadenceFreqOvrFlg) && (in->uwCadenceFreqPu > 0))
  1482. {
  1483. if (alm_stRecCt.ulCadenceFlt < coef->ulRecCadenceCt)
  1484. {
  1485. alm_stRecCt.ulCadenceFlt++;
  1486. }
  1487. }
  1488. /* Recover time of bike torque sensor fault */
  1489. if((in->uwTroqReg > detctcoef->uwTorqMinReg) && (in->uwTroqReg < detctcoef->uwTorqMaxReg))
  1490. {
  1491. if (alm_stRecCt.ulTorqFlt < coef->ulRecTorqCt)
  1492. {
  1493. alm_stRecCt.ulTorqFlt++;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. alm_stRecCt.ulTorqFlt = 0;
  1499. }
  1500. /* Recover time of bike throttle fault */
  1501. if((in->uwThrottleReg > detctcoef->uwThrottleMinReg) && (in->uwThrottleReg < detctcoef->uwThrottleMaxReg))
  1502. {
  1503. if (alm_stRecCt.ulThrottleFlt < coef->ulRecThrottleCt)
  1504. {
  1505. alm_stRecCt.ulThrottleFlt++;
  1506. }
  1507. }
  1508. else
  1509. {
  1510. alm_stRecCt.ulThrottleFlt = 0;
  1511. }
  1512. /* Recover time of PCB NTC fault */
  1513. if((in->uwPCBNTCReg > detctcoef->uwNTCMinReg) && (in->uwThrottleReg < detctcoef->uwNTCMaxReg))
  1514. {
  1515. if (alm_stRecCt.ulPCBNTCFlt < coef->ulRecPCBNTCCt)
  1516. {
  1517. alm_stRecCt.ulPCBNTCFlt++;
  1518. }
  1519. }
  1520. else
  1521. {
  1522. alm_stRecCt.ulPCBNTCFlt = 0;
  1523. }
  1524. /* Recover time of motor NTC fault */
  1525. if((in->uwPCBNTCReg > detctcoef->uwNTCMinReg) && (in->uwThrottleReg < detctcoef->uwNTCMaxReg))
  1526. {
  1527. if (alm_stRecCt.ulMotorNTCFlt < coef->ulRecMotorNTCCt)
  1528. {
  1529. alm_stRecCt.ulMotorNTCFlt++;
  1530. }
  1531. }
  1532. else
  1533. {
  1534. alm_stRecCt.ulMotorNTCFlt = 0;
  1535. }
  1536. /*=======================================================================
  1537. Alarm code clear
  1538. =======================================================================*/
  1539. if (alm_stRecCt.ulBikeGlbl >= coef->ulRecAllValCt)
  1540. {
  1541. /* Bike speed sensor fault */
  1542. if((alm_unBikeCode.bit.BikeSpdSen != 0) && (alm_stRecCt.ulBikeSpdFlt >= coef->ulRecBikeSpdCt))
  1543. {
  1544. alm_stDecCt.uwBikeSpdFlt = 0;
  1545. alm_unBikeCode.bit.BikeSpdSen = 0;
  1546. }
  1547. /* Bike cadence sensor fault */
  1548. if((alm_unBikeCode.bit.CadenceSen != 0) && (alm_stRecCt.ulCadenceFlt >= coef->ulRecCadenceCt))
  1549. {
  1550. alm_stDecCt.uwCadenceFlt = 0;
  1551. alm_unBikeCode.bit.CadenceSen = 0;
  1552. }
  1553. /* Bike torque sensor fault */
  1554. if(((alm_unBikeCode.bit.TorqSen != 0) && (alm_stRecCt.ulTorqFlt >= coef->ulRecTorqCt))
  1555. ||(cp_stFlg.RunModelSelect == ClZLOOP))
  1556. {
  1557. alm_stDecCt.uwTorqFlt = 0;
  1558. alm_unBikeCode.bit.TorqSen = 0;
  1559. }
  1560. /* Bike throttle fault */
  1561. if((alm_unBikeCode.bit.Throttle != 0) && (alm_stRecCt.ulThrottleFlt >= coef->ulRecThrottleCt))
  1562. {
  1563. alm_stDecCt.uwThrottleFlt = 0;
  1564. alm_unBikeCode.bit.Throttle = 0;
  1565. }
  1566. /* PCB NTC fault */
  1567. if((alm_unBikeCode.bit.PCBNTC != 0) && (alm_stRecCt.ulPCBNTCFlt >= coef->ulRecPCBNTCCt))
  1568. {
  1569. alm_stDecCt.uwPCBNTCFlt = 0;
  1570. alm_unBikeCode.bit.PCBNTC = 0;
  1571. }
  1572. /* Motor NTC fault */
  1573. if((alm_unBikeCode.bit.MotorNTC != 0) && (alm_stRecCt.ulMotorNTCFlt >= coef->ulRecMotorNTCCt))
  1574. {
  1575. alm_stDecCt.uwMotorNTCFlt = 0;
  1576. alm_unBikeCode.bit.MotorNTC = 0;
  1577. }
  1578. }
  1579. /*=======================================================================
  1580. Alarm flag clear
  1581. =======================================================================*/
  1582. if(alm_unBikeCode.all == 0)
  1583. {
  1584. /* Clear stop count */
  1585. alm_stStopCt.ulBikePWMOff = 0;
  1586. /* Clear recover count */
  1587. alm_stRecCt.ulBikeGlbl = 0;
  1588. alm_stRecCt.ulBikeSpdFlt = 0;
  1589. alm_stRecCt.ulCadenceFlt = 0;
  1590. alm_stRecCt.ulTorqFlt = 0;
  1591. alm_stRecCt.ulThrottleFlt = 0;
  1592. alm_stRecCt.ulPCBNTCFlt = 0;
  1593. alm_stRecCt.ulMotorNTCFlt = 0;
  1594. }
  1595. }
  1596. /*************************************************************************
  1597. Local Functions (N/A)
  1598. *************************************************************************/
  1599. /************************************************************************
  1600. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  1601. All rights reserved.
  1602. *************************************************************************/
  1603. #ifdef _ALARM_C_
  1604. #undef _ALARM_C_
  1605. #endif
  1606. /*************************************************************************
  1607. End of this File (EOF):
  1608. !!!!!!Do not put anything after this part!!!!!!!!!!!
  1609. *************************************************************************/