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 "bikethrottle.h"
  19. #include "api.h"
  20. #include "board_config.h"
  21. /******************************
  22. *
  23. * static Parameter
  24. *
  25. ******************************/
  26. static BIKETHROTTLE_COF bikethrottle_stBikeThrottleCof = BIKETHROTTLE_COF_DEFAULT;
  27. static LPF_OUT scm_stBikeThrottleLpf;
  28. BIKETHROTTLE_OUT bikethrottle_stBikeThrottleOut = BIKETHROTTLE_OUT_DEFAULT;
  29. /******************************
  30. *
  31. * extern Parameter
  32. *
  33. ******************************/
  34. UWORD Bikethrottle_uwDMAReg = 0;
  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.blThrottleErrorFlg = FALSE;
  78. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  79. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  80. }
  81. /***************************************************************
  82. Function: bikethrottle_voBikeThrottleADC;
  83. Description: bikethrottle get
  84. Call by: functions in main loop;
  85. Input Variables: N/A
  86. Output/Return Variables: N/A
  87. Subroutine Call: N/A;
  88. Reference: N/A
  89. ****************************************************************/
  90. void bikethrottle_voBikeThrottleADC(void) // need to match ADC_StartConversion(ADC1);
  91. {
  92. if (bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE)
  93. {
  94. bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0;
  95. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0;
  96. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt++;
  97. if (bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt == bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt)
  98. {
  99. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE;
  100. bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0;
  101. }
  102. }
  103. else
  104. {
  105. bikethrottle_stBikeThrottleOut.uwThrottleVolReg = iAdc_GetResultPointer(0)[HW_ADC_THRO_CH]; //hw_uwADC2[6];
  106. bikethrottle_stBikeThrottleOut.uwThrottleVolPu =
  107. (UWORD)(((ULONG)bikethrottle_stBikeThrottleOut.uwThrottleVolReg * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu) >> 10); // Q14
  108. mth_voLPFilter((SWORD)bikethrottle_stBikeThrottleOut.uwThrottleVolPu, &scm_stBikeThrottleLpf);
  109. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = scm_stBikeThrottleLpf.slY.sw.hi;
  110. if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu < bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu)
  111. {
  112. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu;
  113. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  114. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  115. {
  116. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  117. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  118. }
  119. }
  120. else if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu > bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu)
  121. {
  122. bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu;
  123. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++;
  124. if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt)
  125. {
  126. bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE;
  127. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  128. }
  129. }
  130. else
  131. {
  132. bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0;
  133. bikethrottle_stBikeThrottleOut.uwThrottlePercent =
  134. (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu - bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu) * 1000 /
  135. (bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu - bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu);
  136. }
  137. }
  138. }
  139. /*************************************************************************
  140. End of this File (EOF)!
  141. Do not put anything after this part!
  142. *************************************************************************/