alarm.c 59 KB

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