alarm.c 56 KB

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