BikeRatioCal.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = 1024;
  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 = 0xFF; //滑行或静止标志,1-滑行,2-静止
  20. //有效判断
  21. if(BikeSpeedRpm > 80) //80rpm*60*2.19m/1000≈10.5km/h
  22. {
  23. if((PedalTorqueNm > 100) && (Cadence > 10)) //10Nm 10rpm
  24. Coasted_Flag = 0;
  25. else
  26. {
  27. if((MotorSpeed > 100) && (Current > 10)) //100rpm 0.1A
  28. Coasted_Flag = 0;
  29. else
  30. Coasted_Flag = 1;
  31. }
  32. }
  33. else
  34. Coasted_Flag = 2;
  35. //计算传动比
  36. if(Coasted_Flag == 2) //静止更新为默认值
  37. {
  38. p_Bike_RatioCal->RatioResult = p_Bike_RatioCal->RatioDefault;
  39. p_Bike_RatioCal->RatioFltSum = 0;
  40. }
  41. else
  42. {
  43. if(Coasted_Flag == 0) //骑行或转把时计算
  44. {
  45. //转速计算
  46. Speed_F = ((((ULONG)MotorSpeed * 56) > ((ULONG)Cadence * 614)) ? ((ULONG)MotorSpeed * 56) : ((ULONG)Cadence * 614)) >> 8; //max(电机转速/4.55*256,踏频*2.4*256)
  47. Speed_B = BikeSpeedRpm;
  48. //计算传动比 Q10
  49. p_Bike_RatioCal->RatioPer = (Speed_B << 10) / Speed_F;
  50. //限制计算结果
  51. p_Bike_RatioCal->RatioPer = (p_Bike_RatioCal->RatioPer < 400) ? 400 : ((p_Bike_RatioCal->RatioPer > 2048) ? 2048 : p_Bike_RatioCal->RatioPer);
  52. }
  53. //计算结果滤波
  54. p_Bike_RatioCal->RatioFltSum += ((p_Bike_RatioCal->RatioPer << 10) - p_Bike_RatioCal->RatioFltSum) >> 4;
  55. p_Bike_RatioCal->RatioFlt = p_Bike_RatioCal->RatioFltSum >> 10;
  56. //目标值
  57. if((p_Bike_RatioCal->RatioResult + 5) > p_Bike_RatioCal->RatioFlt)
  58. p_Bike_RatioCal->RatioResult -= 5;
  59. else if((p_Bike_RatioCal->RatioResult + 10) < p_Bike_RatioCal->RatioFlt)
  60. p_Bike_RatioCal->RatioResult += 20;
  61. }
  62. }