一个设定频率,一个测量频率,通过PID调节控制DAC寄存器的值,这个DAC12位的,执行机构通过这个电压改变频率,这个PID算法的各个参数怎么搞出来?
//extern int U_Set;
//extern u16 CCR1_Val;
/*====================================================================================================
PID Function
The PID (比例、积分、微分) function is used in mainly
control applications. PIDCalc performs one iteration of the PID
algorithm.
While the PID function works, main is just a dummy program showing
a typical usage.
=====================================================================================================*/
typedef struct PID {
float SetPoint; // 设定目标Desired value
float Proportion; // 比例常数Proportional Const
float Integral; // 积分常数Integral Const
float Derivative; // 微分常数Derivative Const
float LastError; // Error[-1]
float PrevError; // Error[-2]
float SumError; // Sums of Errors
} PID;
/*====================================================================================================/
PID计算部分
=====================================================================================================*/
PID vPID={0,0.1,0.001,0.001,0,0,0};
float PIDCalc( PID *pp, float NextPoint )
{
float dError,
Error;
Error = pp->SetPoint - NextPoint; // 偏差
pp->SumError += Error; // 积分
dError = pp->LastError - pp->PrevError; // 当前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
if(pp->SumError>900) pp->SumError=900;
else if(pp->SumError<-900) pp->SumError=-900;
return (pp->Proportion * Error // 比例项
+ pp->Integral * pp->SumError // 积分项
+ pp->Derivative * dError // 微分项
);
}
float Vol(int vSet,int v3){
float vOut; // PID Response (Output)
float vIn; // PID Feedback (Input)
vPID.SetPoint = vSet; // Set PID Setpoint
vIn = (float)v3; // Read Input
vOut = PIDCalc ( &vPID,vIn ); // Perform PID Interation
return vOut;
}
void mian()
{
while(1)
{
if(Flag_3S==1)
{
if(target-TIM_ExtCntFreq>=6||target-TIM_ExtCntFreq<=-6)
{
Pv=Vol(target,TIM_ExtCntFreq);// PID算法
dac_value=dac_value+Pv/2.04;//DAC 寄存器调节
DAC_SetChannel1Data(DAC_Align_12b_R,dac_value);
}
led3=~led3;
Flag_3S=0;
}
}
}
|