正在做一项目,要用到编码器,编码器就是普通的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); } |
速度越快,导致出错的机率越大。
评分
查看全部评分
1. 如果是滑动传输——比如轮子类的,那么几乎无法精确定位——由于轮子存在滑动的可能。但慢速或一定的速度保证没有滑动发生是可以的。
2. 如果是固定传输——比如链条类的,可以较为精确的定位,通常需要有其他辅助定位装置定起点和中间修正。
3. 这种定位都属于相对的,因此都需要其他辅助手段。想想看是吗?
评分
查看全部评分