alarm.c 57 KB

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