BikeRatioCal.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * BikeRatioCal.c
  3. *
  4. * Created on: 2025年3月27日
  5. * Author: zhouxiong9
  6. */
  7. #include "BikeRatioCal.h"
  8. Bike_RatioCal_Struct_t Bike_RatioCalParam = {0,0,0,0,0};
  9. //参数初始化
  10. void BikeRatioCal_Init(UWORD Teeth_F, UWORD Teeth_B)
  11. {
  12. Bike_RatioCalParam.RatioDefault = (Teeth_F << 10) / Teeth_B;
  13. Bike_RatioCalParam.RatioResult = Bike_RatioCalParam.RatioDefault;
  14. }
  15. //传动比计算
  16. void BikeRatioCal_Process(UWORD MotorSpeed, UWORD Cadence, UWORD BikeSpeedRpm, UWORD PedalTorqueNm, UWORD Current, Bike_RatioCal_Struct_t* p_Bike_RatioCal)
  17. {
  18. ULONG Speed_F, Speed_B; //前后牙盘转速 rpm Q8
  19. UBYTE Coasted_Flag = 1; //滑行或静止标志,1-滑行或静止
  20. //有效判断
  21. if(BikeSpeedRpm > 20) //20rpm*60*2.19m/1000≈2.6km/h
  22. {
  23. if((PedalTorqueNm > 50) && (Cadence > 10)) //5Nm 10rpm
  24. Coasted_Flag = 0;
  25. else
  26. {
  27. if((MotorSpeed > 100) && (Current > 10)) //100rpm 0.1A
  28. {
  29. Coasted_Flag = 0;
  30. }
  31. }
  32. }
  33. //计算传动比
  34. if(Coasted_Flag == 0)
  35. {
  36. //转速计算
  37. Speed_F = ((((ULONG)MotorSpeed * 56) > ((ULONG)Cadence * 614)) ? ((ULONG)MotorSpeed * 56) : ((ULONG)Cadence * 614)) >> 8; //max(电机转速/4.55*256,踏频*2.4*256)
  38. Speed_B = BikeSpeedRpm;
  39. //计算传动比 Q10
  40. p_Bike_RatioCal->RatioPer = (Speed_B << 10) / Speed_F;
  41. //限制计算结果
  42. p_Bike_RatioCal->RatioPer = (p_Bike_RatioCal->RatioPer < 200) ? 200 : ((p_Bike_RatioCal->RatioPer > 5000) ? 5000 : p_Bike_RatioCal->RatioPer);
  43. //计算结果滤波
  44. p_Bike_RatioCal->RatioFltSum += ((p_Bike_RatioCal->RatioPer << 10) - p_Bike_RatioCal->RatioFltSum) >> 4;
  45. p_Bike_RatioCal->RatioFlt = p_Bike_RatioCal->RatioFltSum >> 10;
  46. //目标值计算
  47. p_Bike_RatioCal->RatioResult = p_Bike_RatioCal->RatioFlt;
  48. }
  49. else
  50. {
  51. p_Bike_RatioCal->RatioPer = p_Bike_RatioCal->RatioDefault;
  52. p_Bike_RatioCal->RatioResult = p_Bike_RatioCal->RatioDefault;
  53. p_Bike_RatioCal->RatioFlt = p_Bike_RatioCal->RatioDefault;
  54. p_Bike_RatioCal->RatioFltSum = 0;
  55. }
  56. }