alarm.c 56 KB

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