1、是这样的,我用两个STM32单片机,1号单片机STM32单片机发生频率F,2号单片机测量这个频率,通过2号单片机设定目标频率,通过PID算法控制,控制DAC输出电压,1号单片机通过ADC测量这个电压值,发生800-1500HZ频率的,就是不知道PID算法执行OUT参数怎么设置?2、我在网上找到一个别人程序中 if(pp->SumError>900) pp->SumError=900;
else if(pp->SumError<-900) pp->SumError=-900;这几句什么用处,看到不少很明白,为什么要加这几句话,我很好奇,为什么他的算法没有积分时间,微分时间?感觉这个程序经典的。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;
}
}
} |