bikethrottle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "user.h"
  17. #include "typedefine.h"
  18. #include "mathtool.h"
  19. #include "bikethrottle.h"
  20. #include "api.h"
  21. #include "board_config.h"
  22. #include "bikespeed.h"
  23. #include "bikebrake.h"
  24. /******************************
  25. *
  26. * static Parameter
  27. *
  28. ******************************/
  29. static BIKETHROTTLE_COF bikethrottle_stBikeThrottleCof = BIKETHROTTLE_COF_DEFAULT;
  30. static LPF_OUT scm_stBikeThrottleLpf;
  31. BIKETHROTTLE_OUT bikethrottle_stBikeThrottleOut = BIKETHROTTLE_OUT_DEFAULT;
  32. BIKECRUISE_COF bikeCruise = BIKECRUISE_OUT_DEFAULT;
  33. /******************************
  34. *
  35. * extern Parameter
  36. *
  37. ******************************/
  38. /***************************************************************
  39. Function: bikethrottle_voBikeThrottleCof;
  40. Description: bikethrottle coef cal
  41. Call by: functions in main loop;
  42. Input Variables: N/A
  43. Output/Return Variables: N/A
  44. Subroutine Call: N/A;
  45. Reference: N/A
  46. ****************************************************************/
  47. void bikethrottle_voBikeThrottleCof(void)
  48. {
  49. ULONG ulLpfTm = 0;
  50. bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MAX_RANGE << 14) / (VBASE * 100); // Q15
  51. bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MIN_RANGE << 14) / (VBASE * 100); // Q15
  52. bikethrottle_stBikeThrottleCof.uwThrottleVoFaultlPu = ((ULONG)BIKETHROTTLE_VOLTAGE_FAULT_RANGE << 14) / (VBASE * 100);
  53. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain = BIKETHROTTLE_VOLTAGE_SEN2MCUGAIN;
  54. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq = BIKETHROTTLE_LPF_FRQ;
  55. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq = BIKETHROTTLE_LPF_DISCRETEHZ;
  56. bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu = (UWORD)((((UQWORD)33 << 24) / 10) / (1 << ADC_RESOLUTION_BIT) /
  57. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain * 100 * 10 /
  58. VBASE); // Real Voltage to Pu Q24
  59. // 3.3/4096/harwaregain/VBase;
  60. bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  61. /* Bike Throttle limit coef */
  62. ulLpfTm = 1000000 / bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq;
  63. mth_voLPFilterCoef(ulLpfTm, bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq, &scm_stBikeThrottleLpf.uwKx);
  64. bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt = BIKETHROTTLE_ERROR_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  65. bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt = BIKETHROTTLE_RECOVER_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  66. }
  67. /***************************************************************
  68. Function: bikethrottle_voBikeThrottleInit;
  69. Description: Bike throttle signal initialization
  70. Call by: functions in main loop;
  71. Input Variables: N/A
  72. Output/Return Variables: N/A
  73. Subroutine Call: N/A;
  74. Reference: N/A
  75. ****************************************************************/
  76. void bikethrottle_voBikeThrottleInit(void)
  77. {
  78. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = 0;
  79. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  80. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  81. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  82. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  83. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  84. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  85. }
  86. /***************************************************************
  87. Function: bikethrottle_voBikeThrottleADC;
  88. Description: bikethrottle get
  89. Call by: functions in main loop;
  90. Input Variables: N/A
  91. Output/Return Variables: N/A
  92. Subroutine Call: N/A;
  93. Reference: N/A
  94. ****************************************************************/
  95. void bikethrottle_voBikeThrottleADC(void) // need to match ADC_StartConversion(ADC1);
  96. {
  97. if(bikespeed_stSpdLimCoef.blThrottleExist)
  98. {
  99. if (bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE)
  100. {
  101. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  102. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  103. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  104. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt++;
  105. if (bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt == bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt)
  106. {
  107. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  108. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  109. }
  110. }
  111. else
  112. {
  113. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = iAdc_GetResultPointer(0)[HW_ADC_THRO_CH]; //hw_uwADC2[6];
  114. bikethrottle_stBikeThrottleOut.uwThrottleVolPu =
  115. (UWORD)(((ULONG)bikethrottle_stBikeThrottleOut.uwThrottleVolReg * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu) >> 10); // Q14
  116. //mth_voLPFilter((SWORD)bikethrottle_stBikeThrottleOut.uwThrottleVolPu, &scm_stBikeThrottleLpf);
  117. //bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = scm_stBikeThrottleLpf.slY.sw.hi;
  118. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleOut.uwThrottleVolPu;
  119. if (bikethrottle_stBikeThrottleOut.uwThrottleVolPu < bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu)
  120. {
  121. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  122. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  123. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  124. {
  125. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  126. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  127. }
  128. }
  129. else if (bikethrottle_stBikeThrottleOut.uwThrottleVolPu > bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu)
  130. {
  131. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu;
  132. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  133. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  134. {
  135. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  136. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  137. }
  138. }
  139. else
  140. {
  141. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  142. bikethrottle_stBikeThrottleOut.uwThrottlePercent =
  143. (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu - bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu) * 1000 /
  144. (bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu - bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu);
  145. }
  146. bikethrottle_voThrottlecruise();
  147. bikethrottle_voStartAllow();
  148. }
  149. }
  150. else
  151. {
  152. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  153. }
  154. }
  155. /***************************************************************
  156. Function: Throttlecruise;
  157. Description:
  158. Call by: functions in 100ms TimeTask;
  159. Input Variables: N/A
  160. Output/Return Variables: N/A
  161. Subroutine Call: N/A;
  162. Reference: N/A
  163. ****************************************************************/
  164. void bikethrottle_voThrottlecruise(void)
  165. {
  166. if((ass_stParaCong.uwControlFunEN & 0xFF) == 0xAA)
  167. {
  168. if((bikethrottle_stBikeThrottleOut.uwThrottleVolPu < (bikeCruise.ThrottlekeepVault+180)) && (bikethrottle_stBikeThrottleOut.uwThrottleVolPu > (bikeCruise.ThrottlekeepVault-180)))
  169. {
  170. if((BikeBrake_blGetstate() == FALSE)
  171. &&(alm_blAlmOccrFlg == FALSE)
  172. &&(cp_stBikeRunInfoPara.uwBikeGear > 0)
  173. &&(cp_stBikeRunInfoPara.uwBikeGear <= 5)
  174. &&(bikethrottle_stBikeThrottleOut.uwThrottlePercent > 200)
  175. &&(bikeCruise.CruiseMode == 0))
  176. {
  177. if((cp_ulSystickCnt - bikeCruise.KeepInTime) >= CRUISESETINGTIME) //8秒指拨不动进入巡航
  178. {
  179. bikeCruise.CruiseMode = 1;
  180. bikeCruise.CruisePercent = bikethrottle_stBikeThrottleOut.uwThrottlePercent;
  181. bikeCruise.ThrottleResetCruiseFlag = 1;
  182. }
  183. }
  184. if(bikethrottle_stBikeThrottleOut.uwThrottlePercent < 200)
  185. {
  186. bikeCruise.KeepInTime = cp_ulSystickCnt;
  187. }
  188. }
  189. else
  190. {
  191. bikeCruise.ThrottlekeepVault = bikethrottle_stBikeThrottleOut.uwThrottleVolPu;
  192. bikeCruise.KeepInTime = cp_ulSystickCnt;
  193. // bikeCruise.ThrottleResetCruiseFlag = 0;
  194. }
  195. //有故障退出,刹车退出,0档退出
  196. if((alm_blAlmOccrFlg == TRUE)
  197. ||(BikeBrake_blGetstate() == TRUE)
  198. ||(cp_stBikeRunInfoPara.uwBikeGear == 0)
  199. ||(bikeCruise.ThrottleResetCruiseFlag == 3))
  200. {
  201. bikeCruise.CruiseMode = 0;
  202. bikeCruise.KeepInTime = cp_ulSystickCnt;
  203. bikeCruise.ThrottleResetCruiseFlag = 0;
  204. }
  205. //指拨重新调速退出
  206. if(bikethrottle_stBikeThrottleOut.uwThrottlePercent > 350)
  207. {
  208. if(bikeCruise.ThrottleResetCruiseFlag == 2)
  209. {
  210. bikeCruise.ThrottleResetCruiseFlag = 3;
  211. }
  212. }
  213. else
  214. {
  215. if((bikeCruise.CruiseMode) && (bikeCruise.ThrottleResetCruiseFlag == 1) && (bikethrottle_stBikeThrottleOut.uwThrottlePercent < 200))
  216. {
  217. bikeCruise.ThrottleResetCruiseFlag = 2;
  218. }
  219. }
  220. if(bikeCruise.CruiseMode != 0)
  221. {
  222. bikethrottle_stBikeThrottleOut.uwThrottlePercent = bikeCruise.CruisePercent;
  223. }
  224. }
  225. }
  226. /***************************************************************
  227. Function: Throttlecruise;
  228. Description:
  229. Call by: functions in 100ms TimeTask;
  230. Input Variables: N/A
  231. Output/Return Variables: N/A
  232. Subroutine Call: N/A;
  233. Reference: N/A
  234. ****************************************************************/
  235. void bikethrottle_voStartAllow(void)
  236. {
  237. #if(THOTTLE_STARTRUNATSPEED == 0xAA)
  238. static UBYTE SetZeroRunMode = 0;
  239. if((bikespeed_stFreGetOut.uwLPFFrequencyPu < bikespeed_stFreGetCof.uwBikespeedKmh2Pu) && (bikethrottle_stBikeThrottleOut.uwThrottlePercent <= 200))
  240. {
  241. SetZeroRunMode = 0;
  242. }
  243. else if(cp_stBikeRunInfoPara.BikeSpeedKmH >= BIKETHROTTLE_STARTRUNSPEED)
  244. {
  245. SetZeroRunMode = 1;
  246. }
  247. if(SetZeroRunMode == 0)
  248. {
  249. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  250. }
  251. #endif
  252. }
  253. /*************************************************************************
  254. End of this File (EOF)!
  255. Do not put anything after this part!
  256. *************************************************************************/