alarm.c 56 KB

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