alarm.c 60 KB

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