绿色设置的为目标值100,红色为编码器实际数据 。
最后也是两者合在了一起,PID调试成功。
源码直接分享,用的是CCStheia,KEIL打不开。大家可以看一下源码的思路,PID部分几乎不用改
链接:https://pan.baidu.com/s/1-fSQ79qrB1yw8h8KZa1mjg?pwd=mess
提取码:mess
--来自百度网盘超级会员V3的分享
下面是MOTOR.C,PID和电机驱动都在里面
#include "ti_msp_dl_config.h"
#include "oled.h"
#include "stdio.h"#include "motor.h"extern int gEncoderVal;
int read_pwm=0;void Motor_SetPwm(float left) //取值范围为3200~0 3200占空比为100%,0占空比0%
{DL_TimerG_setCaptureCompareValue(PWM_Motor_INST, left, DL_TIMER_CC_0_INDEX); //通道0的}int Position_PID (int Encoder,int Target)
{ float Position_KP=26,Position_KI=0.05,Position_KD=0;static float Bias,Pwm,Integral_bias,Last_Bias;Bias=Target-Encoder; //计算偏差Integral_bias+=Bias; //求出偏差的积分Pwm=Position_KP*Bias+Position_KI*Integral_bias+Position_KD*(Bias-Last_Bias); //位置式PID控制器Last_Bias=Bias; //保存上一次偏差 return Pwm; //增量输出}int Jueduizhi(int a)
{if(a>=0){a=a ; }if(a<0){a=(-a) ;}return a;
}void Motor_Go()
{Motor_SetPwm( read_pwm );//左电机DL_GPIO_setPins(LEFT_PORT_PORT , LEFT_PORT_LEFT1_PIN ); DL_GPIO_clearPins(LEFT_PORT_PORT , LEFT_PORT_LEFT2_PIN );}void Motor_Back()
{ Motor_SetPwm( read_pwm );//左电机DL_GPIO_setPins(LEFT_PORT_PORT , LEFT_PORT_LEFT2_PIN ); DL_GPIO_clearPins(LEFT_PORT_PORT , LEFT_PORT_LEFT1_PIN );}void kongzhi()
{read_pwm= Position_PID (gEncoderVal,100);if( read_pwm>=0){if(read_pwm>3200){read_pwm=3200;}Motor_Go() ;}else {read_pwm=-read_pwm;if(read_pwm>3200){read_pwm=3200;}Motor_Back() ;}}