bikethrottle.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "bikespeed.h"
  22. /******************************
  23. *
  24. * static Parameter
  25. *
  26. ******************************/
  27. static BIKETHROTTLE_COF bikethrottle_stBikeThrottleCof = BIKETHROTTLE_COF_DEFAULT;
  28. static LPF_OUT scm_stBikeThrottleLpf;
  29. BIKETHROTTLE_OUT bikethrottle_stBikeThrottleOut = BIKETHROTTLE_OUT_DEFAULT;
  30. /******************************
  31. *
  32. * extern Parameter
  33. *
  34. ******************************/
  35. /***************************************************************
  36. Function: bikethrottle_voBikeThrottleCof;
  37. Description: bikethrottle coef cal
  38. Call by: functions in main loop;
  39. Input Variables: N/A
  40. Output/Return Variables: N/A
  41. Subroutine Call: N/A;
  42. Reference: N/A
  43. ****************************************************************/
  44. void bikethrottle_voBikeThrottleCof(void)
  45. {
  46. ULONG ulLpfTm = 0;
  47. bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MAX_RANGE << 14) / (VBASE * 100); // Q15
  48. bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MIN_RANGE << 14) / (VBASE * 100); // Q15
  49. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain = BIKETHROTTLE_VOLTAGE_SEN2MCUGAIN;
  50. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq = BIKETHROTTLE_LPF_FRQ;
  51. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq = BIKETHROTTLE_LPF_DISCRETEHZ;
  52. bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu = (UWORD)((((UQWORD)33 << 24) / 10) / (1 << ADC_RESOLUTION_BIT) /
  53. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain * 100 * 10 /
  54. VBASE); // Real Voltage to Pu Q24
  55. // 3.3/4096/harwaregain/VBase;
  56. bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  57. /* Bike Throttle limit coef */
  58. ulLpfTm = 1000000 / bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq;
  59. mth_voLPFilterCoef(ulLpfTm, bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq, &scm_stBikeThrottleLpf.uwKx);
  60. bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt = BIKETHROTTLE_ERROR_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  61. bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt = BIKETHROTTLE_RECOVER_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  62. }
  63. /***************************************************************
  64. Function: bikethrottle_voBikeThrottleInit;
  65. Description: Bike throttle signal initialization
  66. Call by: functions in main loop;
  67. Input Variables: N/A
  68. Output/Return Variables: N/A
  69. Subroutine Call: N/A;
  70. Reference: N/A
  71. ****************************************************************/
  72. void bikethrottle_voBikeThrottleInit(void)
  73. {
  74. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = 0;
  75. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  76. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  77. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  78. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  79. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  80. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  81. }
  82. /***************************************************************
  83. Function: bikethrottle_voBikeThrottleADC;
  84. Description: bikethrottle get
  85. Call by: functions in main loop;
  86. Input Variables: N/A
  87. Output/Return Variables: N/A
  88. Subroutine Call: N/A;
  89. Reference: N/A
  90. ****************************************************************/
  91. void bikethrottle_voBikeThrottleADC(void) // need to match ADC_StartConversion(ADC1);
  92. {
  93. if(bikespeed_stSpdLimCoef.blThrottleExist)
  94. {
  95. if (bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE)
  96. {
  97. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  98. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  99. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  100. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt++;
  101. if (bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt == bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt)
  102. {
  103. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  104. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  105. }
  106. }
  107. else
  108. {
  109. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = iAdc_GetResultPointer(0)[HW_ADC_THRO_CH]; //hw_uwADC2[6];
  110. bikethrottle_stBikeThrottleOut.uwThrottleVolPu =
  111. (UWORD)(((ULONG)bikethrottle_stBikeThrottleOut.uwThrottleVolReg * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu) >> 10); // Q14
  112. //mth_voLPFilter((SWORD)bikethrottle_stBikeThrottleOut.uwThrottleVolPu, &scm_stBikeThrottleLpf);
  113. //bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = scm_stBikeThrottleLpf.slY.sw.hi;
  114. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleOut.uwThrottleVolPu;
  115. if (bikethrottle_stBikeThrottleOut.uwThrottleVolPu < bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu)
  116. {
  117. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  118. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  119. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  120. {
  121. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  122. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  123. }
  124. }
  125. else if (bikethrottle_stBikeThrottleOut.uwThrottleVolPu > bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu)
  126. {
  127. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu;
  128. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  129. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  130. {
  131. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  132. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  133. }
  134. }
  135. else
  136. {
  137. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  138. bikethrottle_stBikeThrottleOut.uwThrottlePercent =
  139. (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu - bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu) * 1000 /
  140. (bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu - bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu);
  141. }
  142. }
  143. }
  144. else
  145. {
  146. bikethrottle_stBikeThrottleOut.uwThrottlePercent = 0;
  147. }
  148. }
  149. /*************************************************************************
  150. End of this File (EOF)!
  151. Do not put anything after this part!
  152. *************************************************************************/