speed_sensor.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "speed_sensor.h"
  2. #include "math_tools.h"
  3. //局部变量定义
  4. //全局变量定义
  5. MC_SpeedSensorData_Struct_t MC_SpeedSensorData = {0, TRUE, FALSE, 0, 0xFFFFFFFF, 0};
  6. uint16_t MC_Speed_Array[10] = {0};
  7. /**************************局部函数定义*************************/
  8. /**************************全局函数定义*************************/
  9. //速度传感器IO初始化
  10. void SpeedSensor_GPIO_Init(void)
  11. {
  12. GPIO_InitTypeDef GPIO_InitStruct;
  13. __HAL_RCC_GPIOB_CLK_ENABLE();
  14. GPIO_InitStruct.Pin = SPEED_SENSOR_Pin;
  15. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  16. GPIO_InitStruct.Pull = GPIO_PULLUP;
  17. HAL_GPIO_Init(SPEED_SENSOR_GPIO_Port, &GPIO_InitStruct);
  18. HAL_NVIC_SetPriority(SPEED_SENSOR_EXTI_IRQn, 2, 0);
  19. HAL_NVIC_EnableIRQ(SPEED_SENSOR_EXTI_IRQn);
  20. }
  21. //车速计算和停止判断处理
  22. void SpeedSensor_Process(MC_SpeedSensorData_Struct_t* p_MC_SpeedSensorData, uint16_t* AvgResult, uint8_t WheelSize, uint8_t Poles)
  23. {
  24. //车速计算
  25. static uint32_t PeriodTimeCnt = 0;
  26. static uint16_t FiltTemp[10];
  27. if((HAL_GetTick() - PeriodTimeCnt) >= 50)
  28. {
  29. //停止判断
  30. static uint32_t StopDelayTimeCnt = 0;
  31. if(p_MC_SpeedSensorData->IsTrigFlag == TRUE)
  32. {
  33. StopDelayTimeCnt = HAL_GetTick();
  34. p_MC_SpeedSensorData->IsTrigFlag = FALSE;
  35. }
  36. if((HAL_GetTick() - StopDelayTimeCnt) > (4000 / Poles)) // 超时信号触发标志未更新,认为停车
  37. {
  38. p_MC_SpeedSensorData->IsStopFlag = TRUE;
  39. }
  40. //车速计算,计算周期50ms
  41. if(p_MC_SpeedSensorData->IsStopFlag == FALSE)
  42. {
  43. if(p_MC_SpeedSensorData->DiffTime_ms == 0)
  44. {
  45. p_MC_SpeedSensorData->DiffTime_ms = 0xFFFFFFFF;
  46. }
  47. p_MC_SpeedSensorData->Speed_Data = (uint32_t)WheelSize * 360 / Poles / p_MC_SpeedSensorData->DiffTime_ms;//单位0.1km/h
  48. //滑动均值滤波
  49. *AvgResult = MovingAverageFilter(p_MC_SpeedSensorData->Speed_Data, FiltTemp, sizeof(FiltTemp) >> 1);
  50. //在确定停止前进行衰减
  51. if((HAL_GetTick() - p_MC_SpeedSensorData->TrigSysTime) > (p_MC_SpeedSensorData->DiffTime_ms * 2))
  52. {
  53. p_MC_SpeedSensorData->DiffTime_ms = (p_MC_SpeedSensorData->DiffTime_ms * 1038) >> 10;
  54. }
  55. }
  56. else
  57. {
  58. p_MC_SpeedSensorData->Speed_Data = 0;
  59. *AvgResult = 0;
  60. p_MC_SpeedSensorData->DiffTime_ms = 0xFFFFFFFF;
  61. return;
  62. }
  63. PeriodTimeCnt = HAL_GetTick();
  64. }
  65. }
  66. void SpeedCal_ByCadence(uint16_t CadenceData, uint8_t T_Front, uint8_t T_Tail, uint16_t WheelSize, uint16_t* BikeSpeed)
  67. {
  68. static uint32_t PeriodTimeCnt = 0;
  69. static uint8_t FltCount = 0;
  70. static uint32_t FltSum = 0;
  71. if((HAL_GetTick() - PeriodTimeCnt) >= 50)
  72. {
  73. PeriodTimeCnt = HAL_GetTick();
  74. FltSum += ((CadenceData * T_Front / T_Tail * WheelSize * 144) / 10000);//车速 = 踏频 * 2.4 * 前飞齿数 / 后飞齿数 * 60 / 1000 km/h,结果单位0.1km/h
  75. FltCount++;
  76. if(FltCount >= 8)
  77. {
  78. FltCount = 0;
  79. *BikeSpeed = FltSum >> 3;
  80. FltSum = 0;
  81. }
  82. }
  83. }