單片機(jī)PID算法實(shí)現(xiàn)
// 比例項(xiàng)
pterm = pid->pgain * ferror;
if (pterm > 100 || pterm < -100)
{
pid->integral = 0.0;
}
else
{
// 積分項(xiàng)
pid->integral += pid->igain * ferror;
if (pid->integral > 100.0)
{
pid->integral = 100.0;
}
else if (pid->integral < 0.0)
pid->integral = 0.0;
}
// 微分項(xiàng)
dterm = ((float)(err - pid->last_error)) * pid->dgain;
result = pterm + pid->integral + dterm;
}
else
result = pid->integral; // 在死區(qū)范圍內(nèi),保持現(xiàn)有輸出
// 保存上次偏差
pid->last_error = err;
return (result);
}
//----------------------------------------------
參數(shù)說明:
p_gain = (float)(5.2);//比例系數(shù)
i_gain = (float)(0.77);//積分系數(shù)
d_gain = (float)(0.18);//微分系數(shù)
process_point為設(shè)定的穩(wěn)定值
display_value為系統(tǒng)輸出值
process_point為傳感器傳入當(dāng)前值
函數(shù)調(diào)用示例:
float display_value;
int count=0;
pid = &warm;
process_point = 30;
set_point = 40;
dead_band = 2;
integral_val =(float)(0.01);
scanf("%d",&process_point);
// 設(shè)定PV,SP值
pid_init(&warm, process_point, set_point);
// 初始化PID參數(shù)值
pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);
// 初始化PID輸出值
pid_setinteg(&warm,0.0);
//pid_setinteg(&warm,30.0);
pid_bumpless(&warm);
display_value = pid_calc(&warm);
printf("%f", display_value);
評論