正在做一项目,要用到编码器,编码器就是普通的1000线增量式编码器,用于步进电机的定位反馈,这两天实际测试,发现在高速情况(大于900rpm)下用
STM32读到的编码器位置会超前理想位置(
单片机实际发送的脉冲数转换成编码器的线数),转动过程中超前的位置会一直增大,一直不知道什么原因。编码器部分程序参考STM32官方例程,在低速情况下运行多长时间都不会出现实际位置超前理想位置的情况,求大神帮助,谢谢!
编码器初始化函数:
/**
* Increment Encoder interface init;
* PB6 --
tiM4_CH1, signal A;
* PB7 -- TIM4_CH2, signal B;
* Encoder output -- OD/OC driver;
*/
void Encoder_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* firstly init port ------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PB6 Pin: IPU enable ----------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PB7 Pin: IPU enable ----------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable TIM4 Update IRQ -----------------------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable Timer4 clock --------------------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM4);
/* Use internal clock for Timer4 ----------------------------------------*/
TIM_InternalClockConfig(TIM4);
/* TIM4_CLK = (APB1_CLK / prescale)*2 =256khz --------------------------*/
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* ENCODER_PPR: pulse number per revolution ------------------------------*/
/* One period = 1 revolution, since count on both edge of A&B, ------------*/
/* So the total pulse number in One period = 4*ENCODER_PPR ----------------*/
TIM_TimeBaseStructure.TIM_Period = (4 * CO_OD_ROM.encoder_ppr)-1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
/* T1 count, IC1 Falling, IC2 Falling -----------------------------------*/
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Falling,TIM_ICPolarity_Falling);
TIM_Cmd(TIM4, ENABLE);
/* The counter can be read out -----------------------------------------*/
//TIM_GetCounter
TIM4->CNT = 0;
/*The save_position has been restored before ------------------------*/
//CO_OD_RAM.motor_position = CO_OD_ROM.save_position;
close_srd.encoder_ovf_cnt = 0;
close_srd.real_lines = 0;
close_srd.close_state = EQU;
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
}
步进电机控制的PWM中断处理函数
void Motor_Timer_Interrupt(void)
{
static int i = 0;
volatile signed long abs_real_lines;
volatile signed short step_diff;
TIM3->ARR = srd.step_delay;
TIM3->CCR1 = (srd.step_delay >>1);
TIM3->CNT = 0;
GET_REAL_LINES();
abs_real_lines = abs(close_srd.real_lines); //实际线数绝对值
step_diff = close_srd.target_lines - abs_real_lines;
debug_buf[i++] = step_diff; //
调试发现低速时理想与实际值偏差step_diff始终保持在一个较小值,当高速时step_diff为负数,并且随转动时间加长而增大
if(i>=DEBUG_SIZE) i = 0;
//加减速处理过程(修改srd.step_delay),srd.step_count为发出的脉冲数,加减速过程会自加1
//脉冲数到编码器线数的转换
PULSE2LINES(srd.step_count,close_srd.target_lines);
}
一周热门 更多>