bikethrottle.c 6.8 KB

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