alarm.c 61 KB

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