|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
位置式 PID 控制算法 ) ~8 C9 O7 ]) Z! s( j* u) g
! v7 `7 E( n, x* w
在使用单片机作为控制 cpu 时,请稍作简化,具体的 PID 参数必须由具体对象通过实验确定。由于单片机的处理速度和 ram 资源的限制,一般不采用浮点数运算,而将所有参数全部用整数,运算到最后再除以一个 2 的 N 次方数据(相当于移位) ,作类似定点数运算,可大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的 “余数 ”,做好余数补偿。这个程序只是一般常用 pid 算法的基本架构,没有包含输入输出处理部分。: k5 ], U" }9 `5 Y/ p( O
4 k- M" M3 n* x#include <string.h>
/ K( i& j6 o, d" I. C1 T#include <stdio.h> 6 d" d) z7 K h8 b y: ~
& G: l" E2 r, T' m/ v
PID Function : E7 X! ~! {% { ]# O4 [7 b& {
The PID function is used in mainly
- |! j, I; M, s5 N' s. econtrol applications. PID Calc peRForms one iteration of the PID 5 `- i, h; x7 B p; l& i: _
algorithm.
3 H( @# s- q* z& K& p/ [While the PID function works, main is just a dummy program showing ~# o ~, y# }! h
a typical usage.
3 [7 @4 ], I& b+ {$ |# b# B# c$ R1 n) l9 |. C( m8 }
PID 功能
9 }, U+ _' w0 q/ G( f在 PID 功能主要用于控制应用。 PID 计算器执行一个 PID 的迭代算法。虽然 PID 功能的工程,% {( B4 h4 T: a: U1 R! ?
主要只是一个虚拟程序显示一个典型的使用。
* c0 d' D4 o+ E, z5 O9 Stypedef struct PID { / Y% ]) t0 }/ M% ?9 n& c
double SetPoint; // 设定目标 Desired Value I. }* s6 ]* W! ], o
double Proportion; // 比例常数 Proportional Const ' q" I( k, X3 F* g+ C
double Integral; // 积分常数 Integral Const . B- I {/ M$ A# I% U/ W
double Derivative; // 微分常数 Derivative Const
4 A# |" A" Z5 ]& e- R$ Ddouble LastError; // Error[-1] " _6 H) r! s) i) X# H* R
double PrevError; // Error[-2]
/ |* v9 i, \, Gdouble SumError; // Sums of Errors
% i: l# \- s' B3 ~. A/ i& r} PID; ; f% Y, O7 j" Q% G: ^, u9 [
double PIDCalc( PID *pp, double NextPoint )
5 r% H* Z3 `/ [) H! S7 \{ ) W( r u8 @7 s
double dError, Error; , q: z, y0 O/ c3 Z$ P
Error = pp->SetPoint - NextPoint; // 偏差
% U- m) {/ c0 F5 S- ]8 G
0 E; i5 C# f" O" m
1 Y8 s& ]- D# @" g/ @pp->SumError += Error; // 积分
u& V& B8 N8 n* b1 L# `2 _. GdError = pp->LastError - pp-> PrevError; // 当前微分! S4 d0 P7 Y' V, _
pp-> PrevError = pp->LastError;
$ X/ ?( o0 N$ h4 B+ m% spp->LastError = Error;
/ ?( O4 b% y0 u$ Mreturn (pp-> Proportion * Error // 比例项% E; R+ q$ b j" s) l4 E* d* K/ E
+ pp->Integral * pp->SumError // 积分项3 j# V& i3 Z( j5 W+ v
+ pp->Derivative * dError // 微分项
! M% }) T" `( z; T- U);
- A# P+ z8 j G0 h! {}
& u- X- r/ q9 p) v: k/*======================= 初始化的 PID 结构 Initialize PID
- R( J' ~, ?" H- D$ N* U6 ]Structure===========================*/
$ z4 t2 j8 ?2 }- Z# Xvoid PIDInit (PID *pp) ( K& Z2 ?8 z0 F& U
{ , |0 Q0 d* H' k6 p
memset ( pp,0,sizeof(PID));
+ C0 i4 L; Z5 n} ! h8 Z9 f% \/ G
/*======================= 主程序 Main * Q. c* b7 h5 g
Program=======================================*/
8 Z4 b0 [, D/ l$ ddouble sensor (void) // 虚拟传感器功能 Dummy Sensor Function{ return 100.0;}
, o2 M) I8 @: \. w: m2 jvoid actuator(double rDelta) // 虚拟驱动器功能 Dummy Actuator Function{} + F6 @+ s" i* ?
void main(void) 1 ~9 F" }1 w8 d/ g; ?
{
9 i/ v5 @8 Z2 ]2 E# Z; {PID sPID; // PID 控制结构 PID Control Structure
4 V( c& E3 F0 _; Edouble rOut; // PID 响应(输出) PID Response (Output) * w" |+ g% ^, |' z) i
double rIn; // PID 反馈(输入) PID Feedback (Input) 4 B F0 A- e) c5 y% i
PIDInit ( &sPID ); // 初始化结构 Initialize Structure
* P* s) n$ ^' JsPID.Proportion = 0.5; // 设置 PID 系数 Set PID Coefficients , I4 K6 A/ T( E H3 s/ `/ p5 r
sPID.Integral = 0.5;
; e7 \- z$ y/ o) ~# W" ? ^6 m! esPID.Derivative = 0.0;
* ]" t+ @: Z8 r X- EsPID.SetPoint = 100.0; // 设置 PID 设定 Set PID Setpoint & s* |" r9 C6 v9 O
for (;;) / o) ~2 |9 ^# A% W9 S+ I+ ]0 U
{ // 模拟最多的 PID 处理 Mock Up of PID Processing
( w) b' b' e0 DrIn = sensor (); // 读取输入 Read Input
3 W! a4 M& b! _' \6 a' M4 ^. [. T. w$ w
: O d# {! {$ Q8 X! lrOut = PIDCalc ( &sPID,rIn ); // 执行的 PID 迭代 Perform PID Interation
2 D1 o; a6 T0 q" |5 n% [ ^actuator ( rOut ); // 所需的更改的影响 Effect Needed Changes
4 _8 {4 h8 O) p: u3 ~# U' z# M0 n1 Z( p/ x! l5 ^, L1 A
: x7 L+ s& N' @( y
: J$ e/ w* \1 l9 i5 ?$ ?2 u# [9 O7 p* Q/ C* q9 P5 w
3 p( x: n' a) L. V9 T
/ q* V' `9 |. b! `$ y* d3 \; \
|
|