赞
踩
本次选用的编码器电机为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文件
- #include "encoder.h"
-
- void Encoder_TIM2_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStruct; //配置IO口
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; //定时器初始化
- TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStruct.TIM_Period = 65535;
- TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
-
- //定时器编码器模式初始化
- TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
-
- TIM_ICInitTypeDef TIM_ICInitStruct; //输入捕获单元配置
- TIM_ICStructInit(&TIM_ICInitStruct);
- TIM_ICInitStruct.TIM_ICFilter = 10;
- TIM_ICInit(TIM2, &TIM_ICInitStruct);
-
- TIM_ClearFlag(TIM2, TIM_FLAG_Update);
-
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
-
- TIM_SetCounter(TIM2, 0);
-
- TIM_Cmd(TIM2, ENABLE);
- }
-
- void Encoder_TIM4_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
- TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStruct.TIM_Period = 65535;
- TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);
-
- TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
-
-
- TIM_ICInitTypeDef TIM_ICInitStruct;
- TIM_ICStructInit(&TIM_ICInitStruct);
- TIM_ICInitStruct.TIM_ICFilter = 10;
- TIM_ICInit(TIM4, &TIM_ICInitStruct);
-
- TIM_ClearFlag(TIM4, TIM_FLAG_Update);
-
- TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
-
- TIM_SetCounter(TIM4, 0);
-
- TIM_Cmd(TIM4, ENABLE);
- }
-
- int Read_Spead(int TIMx) //读取编码器输出脉冲数
- {
- int value_1;
- switch(TIMx)
- {
- case 2:value_1 = (short)TIM_GetCounter(TIM2);TIM_SetCounter(TIM2, 0);break;
- case 4:value_1 = (short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4, 0);break;
- default:value_1 = 0;
- }
- return value_1;
- }
-
- void TIM2_IRQHander(void)
- {
- if(TIM_GetITStatus(TIM2, TIM_IT_Update) == 1)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- }
- }
-
- void TIM4_IRQHander(void)
- {
- if(TIM_GetITStatus(TIM4, TIM_IT_Update) == 1)
- {
- TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
- }
- }
我使用的是简单的delay一下来采集定时器捕获的编码器脉冲数,不过我建议使用定时器中断来处理编码器采集。此函数只采集右轮脉冲进行计算
主函数循环体内函数:
- while(1)
- {
- delay_s(1);
- {
- uint16_t right = Read_Spead(2);//采集右轮脉冲数
- displacement = 0.225 * (right / 1560);//计算位移
- speed = displacement;//由于我设置的为延时一秒就不用除时间
- OLED_Float(0, 0, speed, 4);//通过OLED显示速度
- set_PWM(999);//设置电机PWM
- TIM_SetCounter(TIM2, 0);//下一次一秒计数前再一次将计数清零
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。