bikethrottle.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @file Bikethrottle.c
  3. * @author Wang, Zhiyu(wangzy49@midea.com)
  4. * @brief throttle of ebike
  5. * @version 0.1
  6. * @date 2021-09-29
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. /************************************************************************
  12. Beginning of File, do not put anything above here except notes
  13. Compiler Directives:
  14. *************************************************************************/
  15. #include "syspar.h"
  16. #include "typedefine.h"
  17. #include "mathtool.h"
  18. #include "bikethrottle.h"
  19. //#include "api.h"
  20. #include "board_config.h"
  21. #include "hwsetup.h"
  22. #include "CodePara.h"
  23. #include "power.h"
  24. #include "bikespeed.h"
  25. #include "alarm.h"
  26. #include "FSM_1st.h"
  27. #include "Cadence.h"
  28. #include "bikebrake.h"
  29. #include "UserGpio_Config.h"
  30. /******************************
  31. *
  32. * static Parameter
  33. *
  34. ******************************/
  35. static BIKETHROTTLE_COF bikethrottle_stBikeThrottleCof = BIKETHROTTLE_COF_DEFAULT;
  36. static LPF_OUT scm_stBikeThrottleLpf;
  37. BIKETHROTTLE_OUT bikethrottle_stBikeThrottleOut = BIKETHROTTLE_OUT_DEFAULT;
  38. BIKECRUISE_COF bikeCruise=BIKECRUISE_OUT_DEFAULT;
  39. /******************************
  40. *
  41. * extern Parameter
  42. *
  43. ******************************/
  44. UWORD Bikethrottle_uwDMAReg = 0;
  45. //UBYTE SetZeroRunMode=0;
  46. /***************************************************************
  47. Function: bikethrottle_voBikeThrottleCof;
  48. Description: bikethrottle coef cal
  49. Call by: functions in main loop;
  50. Input Variables: N/A
  51. Output/Return Variables: N/A
  52. Subroutine Call: N/A;
  53. Reference: N/A
  54. ****************************************************************/
  55. void bikethrottle_voBikeThrottleCof(void)
  56. {
  57. ULONG ulLpfTm = 0;
  58. bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MAX_RANGE << 14) / (VBASE * 100); // Q15
  59. bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MIN_RANGE << 14) / (VBASE * 100); // Q15
  60. bikethrottle_stBikeThrottleCof.uwThrottleVoFaultlPu = ((ULONG)BIKETHROTTLE_VOLTAGE_FAULT_RANGE << 14) / (VBASE * 100);
  61. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain = BIKETHROTTLE_VOLTAGE_SEN2MCUGAIN;
  62. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq = BIKETHROTTLE_LPF_FRQ;
  63. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq = BIKETHROTTLE_LPF_DISCRETEHZ;
  64. bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu = (((SQWORD)33 << 24) / 10) / (1 << ADC_RESOLUTION_BIT) /
  65. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain * 100 * 10 /
  66. VBASE; // Real Voltage to Pu Q24
  67. // 3.3/4096/harwaregain/VBase;
  68. bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  69. // bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu = iAdc_GetResultPointer(0)[HW_ADC_THRO_CH] * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu >> 10;
  70. /* Bike Throttle limit coef */
  71. ulLpfTm = 1000000 / bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq;
  72. mth_voLPFilterCoef(ulLpfTm, bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq, &scm_stBikeThrottleLpf.uwKx);
  73. bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt = BIKETHROTTLE_ERROR_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  74. bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt = BIKETHROTTLE_RECOVER_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  75. }
  76. /***************************************************************
  77. Function: bikethrottle_voBikeThrottleInit;
  78. Description: Bike throttle signal initialization
  79. Call by: functions in main loop;
  80. Input Variables: N/A
  81. Output/Return Variables: N/A
  82. Subroutine Call: N/A;
  83. Reference: N/A
  84. ****************************************************************/
  85. void bikethrottle_voBikeThrottleInit(void)
  86. {
  87. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = 0;
  88. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  89. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  90. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  91. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  92. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  93. }
  94. /***************************************************************
  95. Function: bikethrottle_voBikeThrottleADC;
  96. Description: bikethrottle get
  97. Call by: functions in main loop;
  98. Input Variables: N/A
  99. Output/Return Variables: N/A
  100. Subroutine Call: N/A;
  101. Reference: N/A
  102. ****************************************************************/
  103. void bikethrottle_voBikeThrottleADC(void) // need to match ADC_StartConversion(ADC1);
  104. {
  105. if (bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE)
  106. {
  107. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  108. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  109. if(bikethrottle_stBikeThrottleOut.uwThrottleActPer<250)
  110. {
  111. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt++;
  112. // if (bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt == bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt)
  113. {
  114. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  115. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  122. }
  123. if(cp_stBikeRunInfoPara.uwBikeGear!=bikethrottle_stBikeThrottleOut.uwThrottleGear)
  124. {
  125. if((cp_stBikeRunInfoPara.uwBikeGear==1) &&(bikethrottle_stBikeThrottleOut.uwThrottleGear==0))
  126. {
  127. if(bikethrottle_stBikeThrottleOut.uwThrottleVolPu>bikethrottle_stBikeThrottleCof.uwThrottleVoFaultlPu)//25% if(bikethrottle_stBikeThrottleOut.uwThrottleActPer>250)
  128. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  129. }
  130. bikethrottle_stBikeThrottleOut.uwThrottleGear=cp_stBikeRunInfoPara.uwBikeGear;
  131. }
  132. else if(power_stPowStateOut.blPowerStartupFlg == FALSE)
  133. {
  134. if(bikethrottle_stBikeThrottleOut.uwThrottleActPer>250)
  135. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  136. }
  137. {
  138. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = THROTTLEVOLREG();// DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_3);//
  139. bikethrottle_stBikeThrottleOut.uwThrottleVolPu =
  140. ((SQWORD)bikethrottle_stBikeThrottleOut.uwThrottleVolReg * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu) >> 10; // Q14
  141. mth_voLPFilter(bikethrottle_stBikeThrottleOut.uwThrottleVolPu, &scm_stBikeThrottleLpf);
  142. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = scm_stBikeThrottleLpf.slY.sw.hi;
  143. //故障判断
  144. if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu < bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu)
  145. {
  146. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  147. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  148. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  149. {
  150. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  151. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  152. }
  153. }
  154. // else if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu > bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu)
  155. // {
  156. // bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu;
  157. // bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  158. // if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  159. // {
  160. // bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  161. // bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  162. // }
  163. // }
  164. else //无故障
  165. {
  166. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  167. bikethrottle_stBikeThrottleOut.uwThrottlePercent =
  168. (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu - bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu) * 1000 /
  169. (bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu - bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu);
  170. bikethrottle_stBikeThrottleOut.uwThrottleActPer=bikethrottle_stBikeThrottleOut.uwThrottlePercent;
  171. }
  172. // bikethrottle_stBikeThrottleOut.uwThrottlePercent= 1000;// bikethrottle_stBikeThrottleOut.uwThrottleVolReg;
  173. #if(THOTTLECRUISE_EN!=0)
  174. if((cp_stControlPara.uwControlFunEN &0xff)==0xaa)
  175. {
  176. if(bikeCruise.CruiseMode!=0)
  177. {
  178. bikethrottle_stBikeThrottleOut.uwThrottlePercent= bikeCruise.CruisePercent;
  179. }
  180. }
  181. #endif
  182. #if(EMCDEAL_EN==0)
  183. if(bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE)
  184. {
  185. bikethrottle_stBikeThrottleOut.uwThrottlePercent=0;
  186. }
  187. #endif
  188. //#if(STARTRUNATSPEED!=0)
  189. // if((bikespeed_stFreGetOut.uwFrequencyPu<RESETTRUNSPEED) &&( bikethrottle_stBikeThrottleOut.uwThrottlePercent<250))
  190. // {
  191. // SetZeroRunMode=0;
  192. // }
  193. // else if(cp_stBikeRunInfoPara.BikeSpeedKmH>=STARTRUNSPEED)
  194. // {
  195. // SetZeroRunMode=1;
  196. // }
  197. // if(SetZeroRunMode==0)
  198. // {
  199. // bikethrottle_stBikeThrottleOut.uwThrottlePercent=0;
  200. // }
  201. //
  202. //#endif
  203. }
  204. }
  205. void Throttlecruise(void)
  206. {
  207. if( ( bikethrottle_stBikeThrottleOut.uwThrottleVolPu < (bikeCruise.ThrottlekeepVault+180) )
  208. && ( bikethrottle_stBikeThrottleOut.uwThrottleVolPu > (bikeCruise.ThrottlekeepVault-180) ))
  209. {
  210. if( (BikeBrake_blGetstate() == FALSE)
  211. &&(alm_blAlmOccrFlg==FALSE)
  212. &&(cp_stBikeRunInfoPara.uwBikeGear>0)
  213. &&(cp_stBikeRunInfoPara.uwBikeGear<=5)
  214. &&(bikethrottle_stBikeThrottleOut.uwThrottlePercent>250)
  215. &&(switch_flg.SysRun_Flag ==TRUE)
  216. &&(bikeCruise.CruiseMode==0))
  217. {
  218. if(bikeCruise.KeepInCnt<32767)
  219. bikeCruise.KeepInCnt++;
  220. if(bikeCruise.KeepInCnt >= CRUISESETINGTIME ) //8秒指拨不动进入巡航
  221. {
  222. bikeCruise.CruiseMode=1;
  223. bikeCruise.CruisePercent=bikethrottle_stBikeThrottleOut.uwThrottlePercent;
  224. bikeCruise.ThrottleResetCruiseFlag=1;
  225. }
  226. }
  227. if(bikethrottle_stBikeThrottleOut.uwThrottlePercent<250)
  228. bikeCruise.KeepInCnt=0;
  229. }
  230. else
  231. {
  232. bikeCruise.ThrottlekeepVault= bikethrottle_stBikeThrottleOut.uwThrottleVolPu;
  233. bikeCruise.KeepInCnt=0;
  234. // bikeCruise.ThrottleResetCruiseFlag=0;
  235. }
  236. //有故障退出,刹车退出,0档退出,有踏频退出,有力矩退出
  237. if((cadence_stFreGetOut.uwForwardCnt > (ass_stCadAssCoef.uwCadencePulses >> 3))
  238. ||(alm_blAlmOccrFlg==TRUE)
  239. ||(BikeBrake_blGetstate() == TRUE)
  240. ||(cp_stBikeRunInfoPara.uwBikeGear==0)
  241. ||(bikeCruise.ThrottleResetCruiseFlag ==3))
  242. {
  243. bikeCruise.CruiseMode=0;
  244. bikeCruise.KeepInCnt=0;
  245. bikeCruise.ThrottleResetCruiseFlag =0;
  246. }
  247. //指拨重新调速退出
  248. if(bikethrottle_stBikeThrottleOut.uwThrottleVolPu>bikethrottle_stBikeThrottleCof.uwThrottleVoFaultlPu)
  249. {
  250. if( bikeCruise.ThrottleResetCruiseFlag==2 )
  251. bikeCruise.ThrottleResetCruiseFlag=3;
  252. }
  253. else
  254. {
  255. if((bikeCruise.CruiseMode) &&(bikeCruise.ThrottleResetCruiseFlag==1))
  256. {
  257. bikeCruise.ThrottleResetCruiseFlag=2;
  258. }
  259. }
  260. }
  261. /*************************************************************************
  262. End of this File (EOF)!
  263. Do not put anything after this part!
  264. *************************************************************************/