#include "velocity.h"
float Vel_ref = 0,Vel_fb = 0; //输入:参考值,反馈值
float Kp = 2,Ti = 1,Td = 0.5; //参数1:比例增益系数,积分时间系数,微分时间系数
float Ts = 0.002,OutMax = 10,OutMin = 0; //参数2:采样周期(时间),最大输出,最小输出
float a0 = 0,a1 = 0,a2 = 0; //变量1:a0,a1,a2
float Err = 0,Err_1 = 0,Err_2 = 0; //变量2:当前偏差e(k),前一步偏差e(k-1),前前一步偏差e(k-2)
float Vel_out = 0,Out_1 = 0; //输出:输出u(k),前一步输出u(k-1)
void pid_calc(void)
{
a0 = Kp + Ti * Ts + Td / Ts;
a1 = Kp + 2 * Td / Ts;
a2 = Td / Ts;
Err = Vel_ref - Vel_fb;
Vel_out = Out_1 + a0 * Err - a1 * Err_1 + a2 * Err_2;
if(Vel_out > OutMax)
Vel_out = OutMax;
if(Vel_out < OutMin)
Vel_out = OutMin;
Out_1 = Vel_out;
Err_2 = Err_1;
Err_1 = Err;
}
上图为PID增量算法程序,主芯片PSOC5
问题1:Ts表示的采样周期,是不是中断采样反馈值时的间隔时间,比如中断1秒采样一次,那Ts=1吗?
问题2:如果把上面代码放入for语句中循环,那Ts有是怎样的值? |