/** * @file Bikethrottle.c * @author Wang, Zhiyu(wangzy49@midea.com) * @brief throttle of ebike * @version 0.1 * @date 2021-09-29 * * @copyright Copyright (c) 2021 * */ /************************************************************************ Beginning of File, do not put anything above here except notes Compiler Directives: *************************************************************************/ #include "syspar.h" #include "typedefine.h" #include "mathtool.h" #include "hwsetup.h" #include "bikethrottle.h" /****************************** * * static Parameter * ******************************/ static BIKETHROTTLE_COF bikethrottle_stBikeThrottleCof = BIKETHROTTLE_COF_DEFAULT; static LPF_OUT scm_stBikeThrottleLpf; BIKETHROTTLE_OUT bikethrottle_stBikeThrottleOut = BIKETHROTTLE_OUT_DEFAULT; /****************************** * * extern Parameter * ******************************/ UWORD Bikethrottle_uwDMAReg = 0; /*************************************************************** Function: bikethrottle_voBikeThrottleCof; Description: bikethrottle coef cal Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikethrottle_voBikeThrottleCof(void) { ULONG ulLpfTm = 0; bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MAX_RANGE << 14) / (VBASE * 100); // Q15 bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu = ((ULONG)BIKETHROTTLE_VOLTAGE_MIN_RANGE << 14) / (VBASE * 100); // Q15 bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain = BIKETHROTTLE_VOLTAGE_SEN2MCUGAIN; bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq = BIKETHROTTLE_LPF_FRQ; bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq = BIKETHROTTLE_LPF_DISCRETEHZ; bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu = (((SQWORD)33 << 24) / 10) / (1 << ADC_RESOLUTION_BIT) / bikethrottle_stBikeThrottleCof.uwThrottleVolSen2McuGain * 100 * 10 / VBASE; // Real Voltage to Pu Q24 // 3.3/4096/harwaregain/VBase; bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu; /* Bike Throttle limit coef */ ulLpfTm = 1000000 / bikethrottle_stBikeThrottleCof.uwThrottleVolLPFFrq; mth_voLPFilterCoef(ulLpfTm, bikethrottle_stBikeThrottleCof.uwThrottleVolLPFDisFrq, &scm_stBikeThrottleLpf.uwKx); bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt = BIKETHROTTLE_ERROR_TIME / BIKETHROTTLE_ERROE_TIMEUNIT; bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt = BIKETHROTTLE_RECOVER_TIME / BIKETHROTTLE_ERROE_TIMEUNIT; } /*************************************************************** Function: bikethrottle_voBikeThrottleInit; Description: Bike throttle signal initialization Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikethrottle_voBikeThrottleInit(void) { bikethrottle_stBikeThrottleOut.uwThrottleVolReg = 0; bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0; bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0; bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE; bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0; bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0; } /*************************************************************** Function: bikethrottle_voBikeThrottleADC; Description: bikethrottle get Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikethrottle_voBikeThrottleADC(void) // need to match ADC_StartConversion(ADC1); { if (bikethrottle_stBikeThrottleOut.blThrottleErrorFlg == TRUE) { bikethrottle_stBikeThrottleOut.uwThrottleVolPu = 0; bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = 0; bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt++; if (bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt == bikethrottle_stBikeThrottleCof.uwThrottleRecoverCnt) { bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = FALSE; bikethrottle_stBikeThrottleOut.uwThrottleRecoverCnt = 0; } } else { bikethrottle_stBikeThrottleOut.uwThrottleVolReg = hw_uwADC0[6]; //hw_uwADC2[6]; bikethrottle_stBikeThrottleOut.uwThrottleVolPu = ((SQWORD)bikethrottle_stBikeThrottleOut.uwThrottleVolReg * bikethrottle_stBikeThrottleCof.uwThrottleVolReg2Pu) >> 10; // Q14 mth_voLPFilter(bikethrottle_stBikeThrottleOut.uwThrottleVolPu, &scm_stBikeThrottleLpf); bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = scm_stBikeThrottleLpf.slY.sw.hi; if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu < bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu) { bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu; bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++; if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt) { bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE; bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0; } } else if (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu > bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu) { bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu = bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu; bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt++; if (bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt == bikethrottle_stBikeThrottleCof.uwThrottleErrorCnt) { bikethrottle_stBikeThrottleOut.blThrottleErrorFlg = TRUE; bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0; } } else { bikethrottle_stBikeThrottleOut.uwThrottleErrorCnt = 0; bikethrottle_stBikeThrottleOut.uwThrottlePercent = (bikethrottle_stBikeThrottleOut.uwThrottleVolLPFPu - bikethrottle_stBikeThrottleCof.uwThrottleOffsetPu) * 1000 / (bikethrottle_stBikeThrottleCof.uwMaxThrottleVolOutputPu - bikethrottle_stBikeThrottleCof.uwMinThrottleVolOutputPu); } } } /************************************************************************* End of this File (EOF)! Do not put anything after this part! *************************************************************************/