12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /*
- * 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 = 1024;
- 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 = 0xFF; //滑行或静止标志,1-滑行,2-静止
- //有效判断
- if(BikeSpeedRpm > 80) //80rpm*60*2.19m/1000≈10.5km/h
- {
- if((PedalTorqueNm > 100) && (Cadence > 10)) //10Nm 10rpm
- Coasted_Flag = 0;
- else
- {
- if((MotorSpeed > 100) && (Current > 10)) //100rpm 0.1A
- Coasted_Flag = 0;
- else
- Coasted_Flag = 1;
- }
- }
- else
- Coasted_Flag = 2;
- //计算传动比
- if(Coasted_Flag == 2) //静止更新为默认值
- {
- p_Bike_RatioCal->RatioResult = p_Bike_RatioCal->RatioDefault;
- p_Bike_RatioCal->RatioFltSum = 0;
- }
- else
- {
- 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 < 400) ? 400 : ((p_Bike_RatioCal->RatioPer > 2048) ? 2048 : 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;
- //目标值
- if((p_Bike_RatioCal->RatioResult + 5) > p_Bike_RatioCal->RatioFlt)
- p_Bike_RatioCal->RatioResult -= 5;
- else if((p_Bike_RatioCal->RatioResult + 10) < p_Bike_RatioCal->RatioFlt)
- p_Bike_RatioCal->RatioResult += 20;
- }
- }
|