PID控制算法的C语言实现一 PID算法原理

PID控制算法的C语言实现一 PID算法原理
最近两天在考虑一般控制算法的C语言实现问题,发现网络上尚没有一套完整的比较体系的讲解。于是总结了几天,整理一套思路分享给大家。
在工业应用中PID及其衍生算法是应用最广泛的算法之一,是当之无愧的万能算法,如果能够熟练掌握PID算法的设计与实现过程,对于一般的研发人员来讲,应该是足够应对一般研发问题了,而难能可贵的是,在我所接触的控制算法当中,PID控制算法又是最简单,最能体现反馈思想的控制算法,可谓经典中的经典。经典的未必是复杂的,经典的东西常常是简单的,而且是最简单的,想想牛顿的力学三大定律吧,想想爱因斯坦的质能方程吧,何等的简单!简单的不是原始的,简单的也不是落后的,简单到了美的程度。先看看PID算法的一般形式:
PID控制算法的C语言实现一 PID算法原理_第1张图片
PID的流程简单到了不能再简单的程度,通过误差信号控制被控量,而控制器本身就是比例、积分、微分三个环节的加和。这里我们规定(在t时刻):
1.输入量为rin(t);
2.输出量为rout(t);
3.偏差量为err(t)=rin(t)-rout(t);
pid的控制规律为
这里写图片描述
理解一下这个公式,主要从下面几个问题着手,为了便于理解,把控制环境具体一下:
1.规定这个流程是用来为直流电机调速的;
2.输入量rin(t)为电机转速预定值;
3.输出量rout(t)为电机转速实际值;
4.执行器为直流电机;
5.传感器为光电码盘,假设码盘为10线;
6.直流电机采用PWM调速 转速用单位 转/min 表示;
不难看出以下结论:
1.输入量rin(t)为电机转速预定值(转/min);
2. 输出量rout(t)为电机转速实际值(转/min);
3.偏差量为预定值和实际值之差(转/min);
那么以下几个问题需要弄清楚:
1.通过PID环节之后的U(t)是什么值呢?
2.控制执行器(直流电机)转动转速应该为电压值(也就是PWM占空比)。
3.那么U(t)与PWM之间存在怎样的联系呢?
http://blog.21ic.com/user1/3407/archives/2006/33541.html(见附录1)这篇文章上给出了一种方法,即,每个电压对应一个转速,电压和转速之间呈现线性关系。但是我考虑这种方法的前提是把/直流电机的特性理解为线性了,而实际情况下,直流电机的特性绝对不是线性的,或者说在局部上是趋于线性的,这就是为什么说PID调速有个范围的问题。具体看一下http://articles.e-works.net.cn/component/article90249.html(见附录2)这篇文章就可以了解了。所以在正式进行调速设计之前,需要现有开环系统,测试电机和转速之间的特性曲线(或者查阅电机的资料说明),然后再进行闭环参数整定。这篇先写到这,下一篇说明连续系统的离散化问题。并根据离散化后的特点讲述位置型PID和增量型PID的用法和C语言实现过程。

PID控制算法的C语言实现二 PID算法的离散化
上一节中,我论述了PID算法的基本形式,并对其控制过程的实现有了一个简要的说明,通过上一节的总结,基本已经可以明白PID控制的过程。这一节中先继续上一节内容补充说明一下。
1.说明一下反馈控制的原理,通过上一节的框图不难看出,PID控制其实是对偏差的控制过程;
2.如果偏差为0,则比例环节不起作用,只有存在偏差时,比例环节才起作用。
3.积分环节主要是用来消除静差,所谓静差,就是系统稳定后输出值和设定值之间的差值,积分环节实际上就是偏差累计的过程,把累计的误差加到原有系统上以抵消系统造成的静差。
4.而微分信号则反应了偏差信号的变化规律,或者说是变化趋势,根据偏差信号的变化趋势来进行超前调节,从而增加了系统的快速性。
好了,关于PID的基本说明就补充到这里,下面将对PID连续系统离散化,从而方便在处理器上实现。下面把连续状态的公式再贴一下:
这里写图片描述
假设采样间隔为T,则在第K T时刻:
偏差err(K)=rin(K)-rout(K);
积分环节用加和的形式表示,即err(K)+err(K+1)+……;
微分环节用斜率的形式表示,即[err(K)-err(K-1)]/T;
从而形成如下PID离散表示形式:
这里写图片描述
则u(K)可表示成为:
这里写图片描述
至于说Kp、Ki、Kd三个参数的具体表达式,我想可以轻松的推出了,这里节省时间,不再详细表示了。
其实到这里为止,PID的基本离散表示形式已经出来了。目前的这种表述形式属于位置型PID,另外一种表述方式为增量式PID,由U上述表达式可以轻易得到:
这里写图片描述
那么:
这里写图片描述
这就是离散化PID的增量式表示方式,由公式可以看出,增量式的表达结果和最近三次的偏差有关,这样就大大提高了系统的稳定性。需要注意的是最终的输出结果应该为
u(K)+增量调节值;
PID的离散化过程基本思路就是这样,下面是将离散化的公式转换成为C语言,从而实现微控制器的控制作用。

PID控制算法的C语言实现三 位置型PID的C语言实现
上一节中已经抽象出了位置性PID和增量型PID的数学表达式,这一节,重点讲解C语言代码的实现过程,算法的C语言实现过程具有一般性,通过PID算法的C语言实现,可以以此类推,设计其它算法的C语言实现。
第一步:定义PID变量结构体,代码如下:
struct _pid{
float SetSpeed; //定义设定值
float ActualSpeed; //定义实际值
float err; //定义偏差值
float err_last; //定义上一个偏差值
float Kp,Ki,Kd; //定义比例、积分、微分系数
float voltage; //定义电压值(控制执行器的变量)
float integral; //定义积分值
}pid;
控制算法中所需要用到的参数在一个结构体中统一定义,方便后面的使用。
第二部:初始化变量,代码如下:
void PID_init(){
printf(“PID_init begin \n”);
pid.SetSpeed=0.0;
pid.ActualSpeed=0.0;
pid.err=0.0;
pid.err_last=0.0;
pid.voltage=0.0;
pid.integral=0.0;
pid.Kp=0.2;
pid.Ki=0.015;
pid.Kd=0.2;
printf(“PID_init end \n”);
}
统一初始化变量,尤其是Kp,Ki,Kd三个参数,调试过程当中,对于要求的控制效果,可以通过调节这三个量直接进行调节。
第三步:编写控制算法,代码如下:
float PID_realize(float speed){
pid.SetSpeed=speed;
pid.err=pid.SetSpeed-pid.ActualSpeed;
pid.integral+=pid.err;
pid.voltage=pid.Kp*pid.err+pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);
pid.err_last=pid.err;
pid.ActualSpeed=pid.voltage*1.0;
return pid.ActualSpeed;
}
注意:这里用了最基本的算法实现形式,没有考虑死区问题,没有设定上下限,只是对公式的一种直接的实现,后面的介绍当中还会逐渐的对此改进。
到此为止,PID的基本实现部分就初步完成了。下面是测试代码:
int main(){
printf(“System begin \n”);
PID_init();
int count=0;
while(count<1000)
{
float speed=PID_realize(200.0);
printf(“%f\n”,speed);
count++;
}
return 0;
}
下面是经过1000次的调节后输出的1000个数据(具体的参数整定过程就不说明了,网上这种说明非常多):

83.000001
11.555000
59.559675
28.175408
52.907421
38.944152
51.891699
46.141651
53.339054
51.509998
55.908450
55.944631
58.970680
59.882936
62.225001
63.537254
65.527707
67.011058
68.810646
70.355318
72.042040
73.595658
75.207620
76.745444
78.301526
79.812136
81.321929
82.800304
84.268909
85.713108
87.143455
88.553005
89.946960
91.322078
92.680996
94.022234
95.347186
96.655242
97.947180
99.222808
100.482601
101.726572
102.955049
104.168125
105.366066
106.549019
107.717187
108.870756
110.009898
111.134811
112.245652
113.342615
114.425860
115.495564
116.551897
117.595029
118.625116
119.642331
120.646826
121.638767
122.618307
123.585603
124.540813
125.484079
126.415549
127.335383
128.243715
129.140691
130.026459
130.901149
131.764909
132.617870
133.460162
134.291942
135.113308
135.924419
136.725382
137.516332
138.297401
139.068697
139.830352
140.582499
141.325237
142.058701
142.782985
143.498218
144.204509
144.901969
145.590726
146.270843
146.942486
147.605718
148.260674
148.907425
149.546109
150.176794
150.799612
151.414626
152.021959
152.621696
153.213951
153.798781
154.376315
154.946626
155.509812
156.065958
156.615146
157.157471
157.693012
158.221871
158.744097
159.259826
159.769078
160.271991
160.768588
161.258996
161.743264
162.221494
162.693737
163.160075
163.620593
164.075347
164.524422
164.967877
165.405795
165.838235
166.265257
166.686967
167.103377
167.514610
167.920681
168.321682
168.717670
169.108719
169.494862
169.876198
170.252740
170.624605
170.991799
171.354406
171.712487
172.066080
172.415265
172.760077
173.100594
173.436838
173.768895
174.096796
174.420594
174.740352
175.056096
175.367915
175.675818
175.979886
176.280136
176.576656
176.869444
177.158600
177.444121
177.726087
178.004510
178.279458
178.550967
178.819094
179.083860
179.345315
179.603504
179.858466
180.110241
180.358866
180.604388
180.846849
181.086262
181.322699
181.556172
181.786733
182.014396
182.239222
182.461226
182.680475
182.896971
183.110768
183.321881
183.530369
183.736239
183.939545
184.140301
184.338555
184.534321
184.727651
184.918558
185.107080
185.293243
185.477080
185.658625
185.837886
186.014930
186.189745
186.362382
186.532859
186.701207
186.867437
187.031605
187.193713
187.353802
187.511884
187.667997
187.822151
187.974384
188.124700
188.273148
188.419728
188.564488
188.707429
188.848592
188.987995
189.125644
189.261576
189.395801
189.528364
189.659258
189.788528
189.916170
190.042233
190.166702
190.289633
190.411007
190.530867
190.649236
190.766119
190.881544
190.995531
191.108087
191.219243
191.329005
191.437382
191.544428
191.650111
191.754504
191.857565
191.959350
192.059857
192.159119
192.257135
192.353919
192.449511
192.543890
192.637105
192.729137
192.820032
192.909776
192.998410
193.085920
193.172360
193.257700
193.341993
193.425214
193.507408
193.588568
193.668715
193.747847
193.826004
193.903175
193.979391
194.054643
194.128963
194.202349
194.274828
194.346393
194.417073
194.486854
194.555777
194.623820
194.691027
194.757390
194.822919
194.887626
194.951536
195.014633
195.076965
195.138496
195.199273
195.259270
195.318547
195.377060
195.434856
195.491918
195.548283
195.603919
195.658886
195.713145
195.766734
195.819654
195.871912
195.923517
195.974472
196.024791
196.074478
196.123558
196.172016
196.219859
196.267115
196.313778
196.359851
196.405363
196.450296
196.494672
196.538492
196.581753
196.624494
196.666678
196.708363
196.749493
196.790138
196.830267
196.869889
196.909019
196.947656
196.985803
197.023493
197.060701
197.097449
197.133733
197.169558
197.204940
197.239872
197.274378
197.308436
197.342089
197.375309
197.408125
197.440523
197.472520
197.504114
197.535309
197.566127
197.596546
197.626594
197.656258
197.685546
197.714486
197.743047
197.771265
197.799113
197.826629
197.853799
197.880631
197.907131
197.933284
197.959122
197.984629
198.009823
198.034705
198.059275
198.083520
198.107481
198.131129
198.154493
198.177566
198.200349
198.222843
198.245062
198.267001
198.288662
198.310059
198.331178
198.352049
198.372645
198.392982
198.413066
198.432911
198.452499
198.471846
198.490953
198.509819
198.528439
198.546842
198.565003
198.582945
198.600648
198.618147
198.635415
198.652474
198.669313
198.685955
198.702378
198.718611
198.734625
198.750448
198.766067
198.781497
198.796736
198.811776
198.826628
198.841303
198.855788
198.870087
198.884218
198.898162
198.911943
198.925538
198.938970
198.952229
198.965320
198.978257
198.991033
199.003643
199.016092
199.028390
199.040542
199.052536
199.064373
199.076067
199.087617
199.099019
199.110280
199.121407
199.132381
199.143240
199.153940
199.164511
199.174957
199.185270
199.195457
199.205514
199.215440
199.225262
199.234930
199.244503
199.253928
199.263275
199.272468
199.281571
199.290541
199.299421
199.308165
199.316815
199.325345
199.333789
199.342115
199.350336
199.358462
199.366479
199.374396
199.382228
199.389943
199.397586
199.405110
199.412555
199.419891
199.427152
199.434307
199.441389
199.448363
199.455264
199.462073
199.468802
199.475442
199.481995
199.488475
199.494857
199.501183
199.507404
199.513578
199.519639
199.525656
199.531579
199.537437
199.543230
199.548936
199.554583
199.560149
199.565647
199.571073
199.576436
199.581730
199.586961
199.592118
199.597220
199.602260
199.607218
199.612132
199.616974
199.621764
199.626486
199.631156
199.635757
199.640316
199.644808
199.649249
199.653636
199.657959
199.662246
199.666457
199.670635
199.674752
199.678815
199.682833
199.686798
199.690715
199.694583
199.698409
199.702177
199.705905
199.709582
199.713209
199.716788
199.720339
199.723826
199.727276
199.730690
199.734054
199.737378
199.740657
199.743901
199.747111
199.750260
199.753393
199.756474
199.759526
199.762524
199.765490
199.768422
199.771314
199.774169
199.776992
199.779775
199.782527
199.785247
199.787938
199.790590
199.793204
199.795787
199.798338
199.800860
199.803343
199.805802
199.808225
199.810624
199.812986
199.815326
199.817642
199.819915
199.822175
199.824388
199.826587
199.828755
199.830902
199.833006
199.835097
199.837155
199.839194
199.841210
199.843191
199.845168
199.847096
199.849024
199.850905
199.852784
199.854621
199.856449
199.858238
199.860016
199.861757
199.863486
199.865199
199.866879
199.868549
199.870186
199.871813
199.873419
199.874997
199.876563
199.878109
199.879620
199.881136
199.882613
199.884088
199.885527
199.886971
199.888371
199.889783
199.891142
199.892518
199.893845
199.895180
199.896485
199.897783
199.899057
199.900322
199.901562
199.902797
199.904010
199.905222
199.906392
199.907576
199.908720
199.909875
199.910985
199.912108
199.913193
199.914287
199.915352
199.916423
199.917459
199.918505
199.919527
199.920526
199.921513
199.922496
199.923452
199.924415
199.925348
199.926275
199.927198
199.928108
199.929019
199.929903
199.930788
199.931653
199.932509
199.933353
199.934187
199.935002
199.935816
199.936617
199.937420
199.938195
199.938971
199.939733
199.940477
199.941228
199.941961
199.942685
199.943392
199.944111
199.944804
199.945491
199.946181
199.946854
199.947518
199.948165
199.948824
199.949456
199.950083
199.950714
199.951326
199.951930
199.952532
199.953125
199.953714
199.954290
199.954863
199.955424
199.955979
199.956538
199.957073
199.957623
199.958146
199.958671
199.959189
199.959693
199.960203
199.960689
199.961191
199.961665
199.962156
199.962619
199.963098
199.963543
199.964014
199.964448
199.964907
199.965330
199.965772
199.966201
199.966625
199.967046
199.967458
199.967868
199.968263
199.968664
199.969047
199.969437
199.969817
199.970193
199.970565
199.970943
199.971297
199.971668
199.972011
199.972363
199.972712
199.973047
199.973388
199.973726
199.974049
199.974379
199.974699
199.975014
199.975326
199.975645
199.975939
199.976249
199.976546
199.976832
199.977125
199.977414
199.977688
199.977969
199.978247
199.978525
199.978782
199.979061
199.979312
199.979576
199.979825
199.980077
199.980335
199.980569
199.980812
199.981053
199.981300
199.981522
199.981755
199.981984
199.982213
199.982427
199.982648
199.982860
199.983080
199.983298
199.983501
199.983704
199.983914
199.984114
199.984309
199.984500
199.984698
199.984887
199.985079
199.985262
199.985442
199.985623
199.985803
199.985984
199.986170
199.986327
199.986508
199.986668
199.986846
199.987006
199.987169
199.987321
199.987481
199.987633
199.987800
199.987948
199.988094
199.988237
199.988386
199.988526
199.988675
199.988815
199.988965
199.989090
199.989231
199.989359
199.989491
199.989629
199.989757
199.989889
199.990012
199.990133
199.990253
199.990373
199.990493
199.990614
199.990734
199.990854
199.990960
199.991072
199.991180
199.991289
199.991398
199.991507
199.991616
199.991718
199.991837
199.991922
199.992025
199.992123
199.992214
199.992314
199.992412
199.992503
199.992604
199.992701
199.992792
199.992878
199.992967
199.993047
199.993136
199.993216
199.993305
199.993385
199.993474
199.993554
199.993637
199.993726
199.993806
199.993881
199.993952
199.994024
199.994101
199.994170
199.994241
199.994313
199.994391
199.994459
199.994531
199.994602
199.994680
199.994748
199.994805
199.994868
199.994928
199.994989
199.995049
199.995109
199.995175
199.995226
199.995295
199.995346
199.995416
199.995466
199.995536
199.995593
199.995653
199.995713
199.995759
199.995811
199.995859
199.995902
199.995960
199.995999
199.996051
199.996100
199.996148
199.996191
199.996249
199.996288
199.996340
199.996389
199.996438
199.996480
199.996538
199.996578
199.996629
199.996678
199.996712
199.996746
199.996787
199.996824
199.996855
199.996896
199.996927
199.996967
199.997005
199.997036
199.997076
199.997113
199.997145
199.997185
199.997216
199.997256
199.997294
199.997325
199.997365
199.997403
199.997434
199.997474
199.997512
199.997543
199.997583
199.997614
199.997640
199.997669
199.997689
199.997711
199.997740
199.997760
199.997789
199.997809
199.997838
199.997858
199.997880
199.997909
199.997929
199.997958
199.997978
199.998007
199.998027
199.998049
199.998078
199.998098
199.998127
199.998147
199.998170
199.998199
199.998218
199.998247
199.998267
199.998296
199.998316
199.998339
199.998368
199.998387
199.998416
199.998436
199.998459
199.998488
199.998508
199.998537
199.998556
199.998585
199.998590
199.998605
199.998616
199.998634
199.998642
199.998654
199.998665
199.998676
199.998694
199.998702
199.998714
199.998725
199.998743
199.998745
199.998766
199.998774
199.998785
199.998803
199.998805
199.998826
199.998834
199.998845
199.998863
199.998871
199.998883
199.998894
199.998905
199.998923
199.998931
199.998943
199.998954
199.998972
199.998974
199.998995
199.999003
199.999014
199.999032
199.999034
199.999055
199.999063
199.999074
199.999092
199.999094
199.999115
199.999123
199.999135
199.999152
199.999161
199.999172
199.999183
199.999201
199.999203
199.999224
199.999232
199.999243
199.999261
199.999263
199.999284
199.999292
199.999304
199.999321
199.999323
199.999344
199.999352
199.999364
199.999381
199.999390
199.999401
199.999412
199.999430
199.999432
199.999453
199.999461
199.999473

PID控制算法的C语言实现四 增量型PID的C语言实现
上一节中介绍了最简单的位置型PID的实现手段,这一节主要讲解增量式PID的实现方法,位置型和增量型PID的数学公式请参见我的系列文《PID控制算法的C语言实现二》中的讲解。实现过程仍然是分为定义变量、初始化变量、实现控制算法函数、算法测试四个部分,详细分类请参加《PID控制算法的C语言实现三》中的讲解,这里直接给出代码了。

include

include

你可能感兴趣的:(四轴飞行器飞控)