当前位置:   article > 正文

stm32霍尔编码器电机测速原理_霍尔编码器测速原理

霍尔编码器测速原理

        本次选用的编码器电机为13线的霍尔编码器电机,电机减速比为30:1,转动一圈输出13*30=390个脉冲。轮胎直径为75mm,轮胎周长为pi*d=3*75=225mm.定时器采用四倍频计数,则一圈输出390*4=1560个脉冲。具体编码器知识这里就不多说了。

         根据测速原理:假设编码器输出的脉冲数为N,而电机转动一圈输出1569个脉冲,转动一圈轮子将前进225mm。那输出脉冲数为N时前进的距离就应该为225*(N/1560)mm,再除以时间及可得速度。

下面为具体代码:

encoder.c文件

  1. #include "encoder.h"
  2. void Encoder_TIM2_Init(void)
  3. {
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开时钟
  5. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  6. GPIO_InitTypeDef GPIO_InitStruct; //配置IO口
  7. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  8. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  9. GPIO_Init(GPIOA, &GPIO_InitStruct);
  10. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; //定时器初始化
  11. TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  12. TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  13. TIM_TimeBaseInitStruct.TIM_Period = 65535;
  14. TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
  15. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
  16. //定时器编码器模式初始化
  17. TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  18. TIM_ICInitTypeDef TIM_ICInitStruct; //输入捕获单元配置
  19. TIM_ICStructInit(&TIM_ICInitStruct);
  20. TIM_ICInitStruct.TIM_ICFilter = 10;
  21. TIM_ICInit(TIM2, &TIM_ICInitStruct);
  22. TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  23. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  24. TIM_SetCounter(TIM2, 0);
  25. TIM_Cmd(TIM2, ENABLE);
  26. }
  27. void Encoder_TIM4_Init(void)
  28. {
  29. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  30. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  31. GPIO_InitTypeDef GPIO_InitStruct;
  32. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  33. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  34. GPIO_Init(GPIOB, &GPIO_InitStruct);
  35. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  36. TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  37. TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  38. TIM_TimeBaseInitStruct.TIM_Period = 65535;
  39. TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
  40. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);
  41. TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  42. TIM_ICInitTypeDef TIM_ICInitStruct;
  43. TIM_ICStructInit(&TIM_ICInitStruct);
  44. TIM_ICInitStruct.TIM_ICFilter = 10;
  45. TIM_ICInit(TIM4, &TIM_ICInitStruct);
  46. TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  47. TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
  48. TIM_SetCounter(TIM4, 0);
  49. TIM_Cmd(TIM4, ENABLE);
  50. }
  51. int Read_Spead(int TIMx) //读取编码器输出脉冲数
  52. {
  53. int value_1;
  54. switch(TIMx)
  55. {
  56. case 2:value_1 = (short)TIM_GetCounter(TIM2);TIM_SetCounter(TIM2, 0);break;
  57. case 4:value_1 = (short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4, 0);break;
  58. default:value_1 = 0;
  59. }
  60. return value_1;
  61. }
  62. void TIM2_IRQHander(void)
  63. {
  64. if(TIM_GetITStatus(TIM2, TIM_IT_Update) == 1)
  65. {
  66. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  67. }
  68. }
  69. void TIM4_IRQHander(void)
  70. {
  71. if(TIM_GetITStatus(TIM4, TIM_IT_Update) == 1)
  72. {
  73. TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
  74. }
  75. }

        我使用的是简单的delay一下来采集定时器捕获的编码器脉冲数,不过我建议使用定时器中断来处理编码器采集。此函数只采集右轮脉冲进行计算

主函数循环体内函数:

  1. while(1)
  2. {
  3. delay_s(1);
  4. {
  5. uint16_t right = Read_Spead(2);//采集右轮脉冲数
  6. displacement = 0.225 * (right / 1560);//计算位移
  7. speed = displacement;//由于我设置的为延时一秒就不用除时间
  8. OLED_Float(0, 0, speed, 4);//通过OLED显示速度
  9. set_PWM(999);//设置电机PWM
  10. TIM_SetCounter(TIM2, 0);//下一次一秒计数前再一次将计数清零
  11. }
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/656490
推荐阅读
相关标签
  

闽ICP备14008679号