incrementalpid_t *pidStructure;
typedef struct{
float Target; //设定目标值
float Measured; //测量值
float err; //本次偏差值
float err_last; //上一次偏差
float err_beforeLast; //上上次偏差
float Kp, Ki, Kd; //Kp, Ki, Kd控制系数
float pwm; //pwm输出
float zancun;
}incrementalpid_t;
int Position_PID(positionpid_t *pid_t, int target, int measured)
{
pid_t->Target = (float)target;
pid_t->Measured = (float)measured;
pid_t->err = pid_t->Target - pid_t->Measured; //误差(目标值-测量值)
pid_t->integral_err += pid_t->err; //误差积分
pid_t->pwm = pid_t->Kp*pid_t->err //增量pid
+ pid_t->Ki*pid_t->integral_err
+ pid_t->Kd*(pid_t->err - pid_t->err_last);
pid_t->err_last = pid_t->err;
if(pid_t->pwm < -PositionPID_Amplitude) pid_t->pwm = -PositionPID_Amplitude;
if(pid_t->pwm > PositionPID_Amplitude) pid_t->pwm = PositionPID_Amplitude;
return (int)pid_t->pwm;
}
新人一枚,想问一下如果想用TIM_SetComparex()这个函数去接收pid_t->pwm的话,要怎么去写,一开始是
TIM_SetCompare1(TIM1,pidStructure->pwm)这样直接去写的,但是没有成效,所以不知道是哪里有问题(想法很简单,就把pid这个输出量传给TIM_SetComparex去控制pwm的输出)是这个想法一开始就不成立吗,恳求大佬指点指点(也省略了很多代码没贴出,主要是吧pid_t->pwm作为pwm的操控量)。
|