/* * BikeRatioCal.c * * Created on: 2025年3月27日 * Author: zhouxiong9 */ #include "BikeRatioCal.h" Bike_RatioCal_Struct_t Bike_RatioCalParam = {0,0,0,0,0}; //参数初始化 void BikeRatioCal_Init(UWORD Teeth_F, UWORD Teeth_B) { Bike_RatioCalParam.RatioDefault = (Teeth_F << 10) / Teeth_B; Bike_RatioCalParam.RatioResult = Bike_RatioCalParam.RatioDefault; } //传动比计算 void BikeRatioCal_Process(UWORD MotorSpeed, UWORD Cadence, UWORD BikeSpeedRpm, UWORD PedalTorqueNm, UWORD Current, Bike_RatioCal_Struct_t* p_Bike_RatioCal) { ULONG Speed_F, Speed_B; //前后牙盘转速 rpm Q8 UBYTE Coasted_Flag = 1; //滑行或静止标志,1-滑行或静止 //有效判断 if(BikeSpeedRpm > 20) //20rpm*60*2.19m/1000≈2.6km/h { if((PedalTorqueNm > 50) && (Cadence > 10)) //5Nm 10rpm Coasted_Flag = 0; else { if((MotorSpeed > 100) && (Current > 10)) //100rpm 0.1A { Coasted_Flag = 0; } } } //计算传动比 if(Coasted_Flag == 0) { //转速计算 Speed_F = ((((ULONG)MotorSpeed * 56) > ((ULONG)Cadence * 614)) ? ((ULONG)MotorSpeed * 56) : ((ULONG)Cadence * 614)) >> 8; //max(电机转速/4.55*256,踏频*2.4*256) Speed_B = BikeSpeedRpm; //计算传动比 Q10 p_Bike_RatioCal->RatioPer = (Speed_B << 10) / Speed_F; //限制计算结果 p_Bike_RatioCal->RatioPer = (p_Bike_RatioCal->RatioPer < 200) ? 200 : ((p_Bike_RatioCal->RatioPer > 5000) ? 5000 : p_Bike_RatioCal->RatioPer); //计算结果滤波 p_Bike_RatioCal->RatioFltSum += ((p_Bike_RatioCal->RatioPer << 10) - p_Bike_RatioCal->RatioFltSum) >> 4; p_Bike_RatioCal->RatioFlt = p_Bike_RatioCal->RatioFltSum >> 10; //目标值计算 p_Bike_RatioCal->RatioResult = p_Bike_RatioCal->RatioFlt; } else { p_Bike_RatioCal->RatioPer = p_Bike_RatioCal->RatioDefault; p_Bike_RatioCal->RatioResult = p_Bike_RatioCal->RatioDefault; p_Bike_RatioCal->RatioFlt = p_Bike_RatioCal->RatioDefault; p_Bike_RatioCal->RatioFltSum = 0; } }