完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近在调试编码器,用的貌似是官方给出的例程,但是在做pid的时候遇到困难,不知道该如何在此基础上实现pid,有大神指教吗?
#define ENCODER_tiMER TIM3 // Encoder unit connected to TIM3 #define ENCODER_TIM_PERIOD (u16)32 //line of encoder #define COUNTER_RESET (u16)0 #define ICx_FILTER (u8) 6 // 6<-> 670nsec static volatile u16 hEncoder_Timer_Overflow; static u8 Duty_TIM3 = 0; extern u16 hEncoder_Timer_Overflag; u16 rotor_speed=0; u8 Period_TIM3 = 0,CollecFlag_TIM3; void ENC_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; NVIC_InitTypeDef NVIC_InitStructure; //TIM3 clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //priority of TIM3 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /** *Òý½Å¸´Óà µ½µ×ÊǸ´ÓÃģʽ **/ GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3); /* Timer configuration in Encoder mode */ TIM_DeInit(ENCODER_TIMER); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0X00; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_TIM_PERIOD)-1; //ÉèÖÃPWMÖÜÆÚ TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure); //±àÂëÆ÷½Ó¿Úģʽ³õʼ»¯ ´Ëº¯Êý˵Ã÷Ö»Óж¨Ê±Æ÷µÄchanel1 and chanel2 ¾ßÓбàÂëÆ÷¹¦ÄÜ£¬ÆäËûͨµÀΪ¼¯³É±àÂëÆ÷¹¦ÄÜ TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); // TIM_ICInitStructure.TIM_Channel=TIM_Channel_2; // TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;//Â˲¨Ê±¼äÈçºÎ¼ÆËãµÃÀ´£¿£¿ TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure); // TIM_SelectInputTrigger(ENCODER_TIMER, TIM_TS_TI2FP2); //Ñ¡ÔñʱÖÓ´¥·¢Ô´ // TIM_SelectSlaveMode(ENCODER_TIMER, TIM_SlaveMode_Reset);//´ÓģʽѡÔñ // TIM_SelectMasterSlaveMode(ENCODER_TIMER, TIM_MasterSlaveMode_Enable); //Ö÷´ÓģʽѡÔñ // Clear all pending interrupts TIM_ClearFlag(ENCODER_TIMER,TIM_IT_CC1|TIM_FLAG_Update); TIM_ITConfig(ENCODER_TIMER,TIM_IT_CC1|TIM_IT_Update, ENABLE); //Reset counter ENCODER_TIMER->CNT = COUNTER_RESET; TIM_Cmd(ENCODER_TIMER, ENABLE); } s16 ENC_Get_Rotorspeed(void) { static u16 lastCount = 0; u16 curCount = ENCODER_TIMER->CNT;// rotor_speed=(128*hEncoder_Timer_Overflag+curCount-lastCount)/64; printf("hEncoder_Timer_Overflag=%drn",hEncoder_Timer_Overflag); lastCount = curCount; return (u16) rotor_speed; } void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //Òç³öÖÐ¶Ï { TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //Çå³ýÖжϱê־λ hEncoder_Timer_Overflag++; } else if(TIM_GetITStatus(TIM3,TIM_IT_CC1)==SET) { Duty_TIM3=TIM_GetCapture1(TIM3); if(TIM_GetCapture2(TIM3)>64) Period_TIM3=TIM_GetCapture2(TIM3); CollecFlag_TIM3=0; } } 采用的是中断溢出的方式进行速度计算的,关于中断服务函数是参考资料写的,但是实现PID好像还有不少的问题、阿莫西论坛 哪位能帮我把阿莫西论坛的源码下载下来。小弟谢谢了先。源码可以发生到俅俅:605172128 |
|
相关推荐
1个回答
|
|
楼主 做出来了吗 求指教
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2183 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
2011 浏览 3 评论
4612 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2154 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2687 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 09:06 , Processed in 0.481319 second(s), Total 43, Slave 37 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号