bikethrottle.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include "api.h"
  21. #include "api_rt.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. UWORD Bikethrottle_uwDMAReg = 0;
  36. /***************************************************************
  37. Function: bikethrottle_voBikeThrottleCof;
  38. Description: bikethrottle coef cal
  39. Call by: functions in main loop;
  40. Input Variables: N/A
  41. Output/Return Variables: N/A
  42. Subroutine Call: N/A;
  43. Reference: N/A
  44. ****************************************************************/
  45. void bikethrottle_voBikeThrottleCof(void)
  46. {
  47. ULONG ulLpfTm = 0;
  48. bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MAX_RANGE << 14) / (VBASE * 100); // Q15
  49. bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MIN_RANGE << 14) / (VBASE * 100); // Q15
  50. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain = BIKETHROTTLE_VOLTAGE_SEN2MCUGAIN;
  51. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq = BIKETHROTTLE_LPF_FRQ;
  52. bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq = BIKETHROTTLE_LPF_DISCRETEHZ;
  53. bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu = (UWORD)((((UQWORD)33 << 24) / 10) / (1 << ADC_RESOLUTION_BIT) /
  54. bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain * 100 * 10 /
  55. VBASE); // Real Voltage to Pu Q24
  56. // 3.3/4096/harwaregain/VBase;
  57. bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  58. /* Bike Throttle limit coef */
  59. ulLpfTm = 1000000 / bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq;
  60. mth_voLPFilterCoef(ulLpfTm, bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq, &scm_stBikeThrottleLpf.uwKx);
  61. bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt = BIKETHROTTLE_ERROR_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  62. bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt = BIKETHROTTLE_RECOVER_TIME / BIKETHROTTLE_ERROE_TIMEUNIT;
  63. }
  64. /***************************************************************
  65. Function: bikethrottle_voBikeThrottleInit;
  66. Description: Bike throttle signal initialization
  67. Call by: functions in main loop;
  68. Input Variables: N/A
  69. Output/Return Variables: N/A
  70. Subroutine Call: N/A;
  71. Reference: N/A
  72. ****************************************************************/
  73. void bikethrottle_voBikeThrottleInit(void)
  74. {
  75. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = 0;
  76. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  77. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  78. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  79. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  80. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  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 (bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE)
  94. {
  95. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  96. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  97. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt++;
  98. if (bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt == bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt)
  99. {
  100. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  101. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  102. }
  103. }
  104. else
  105. {
  106. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = iAdc_GetResultPointer(0)[HW_ADC_THRO_CH]; //hw_uwADC2[6];
  107. bikethrottle_stBikeThrottleOut.uwThrottleVolPu =
  108. (UWORD)(((ULONG)bikethrottle_stBikeThrottleOut.uwThrottleVolReg * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu) >> 10); // Q14
  109. mth_voLPFilter((SWORD)bikethrottle_stBikeThrottleOut.uwThrottleVolPu, &scm_stBikeThrottleLpf);
  110. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = scm_stBikeThrottleLpf.slY.sw.hi;
  111. if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu < bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu)
  112. {
  113. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  114. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  115. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  116. {
  117. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  118. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  119. }
  120. }
  121. else if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu > bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu)
  122. {
  123. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu;
  124. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  125. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  126. {
  127. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  128. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  129. }
  130. }
  131. else
  132. {
  133. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  134. bikethrottle_stBikeThrottleOut.uwThrottlePercent =
  135. (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu - bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu) * 1000 /
  136. (bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu - bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu);
  137. }
  138. }
  139. }
  140. /*************************************************************************
  141. End of this File (EOF)!
  142. Do not put anything after this part!
  143. *************************************************************************/