PID控制算法的C語(yǔ)言實(shí)現(xiàn)完整版_第1頁(yè)
PID控制算法的C語(yǔ)言實(shí)現(xiàn)完整版_第2頁(yè)
PID控制算法的C語(yǔ)言實(shí)現(xiàn)完整版_第3頁(yè)
PID控制算法的C語(yǔ)言實(shí)現(xiàn)完整版_第4頁(yè)
PID控制算法的C語(yǔ)言實(shí)現(xiàn)完整版_第5頁(yè)
已閱讀5頁(yè),還剩96頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第頁(yè)P(yáng)ID控制算法的C語(yǔ)言實(shí)現(xiàn)一PID算法原理

最近兩天在考慮一般控制算法的C語(yǔ)言實(shí)現(xiàn)問(wèn)題,發(fā)現(xiàn)網(wǎng)絡(luò)上尚沒有一套完整的比較體系的講解。于是總結(jié)了幾天,整理一套思路分享給大家。

在工業(yè)應(yīng)用中PID及其衍生算法是應(yīng)用最廣泛的算法之一,是當(dāng)之無(wú)愧的萬(wàn)能算法,如果能夠熟練掌握PID算法的設(shè)計(jì)及實(shí)現(xiàn)過(guò)程,對(duì)于一般的研發(fā)人員來(lái)講,應(yīng)該是足夠應(yīng)對(duì)一般研發(fā)問(wèn)題了,而難能可貴的是,在我所接觸的控制算法當(dāng)中,PID控制算法又是最簡(jiǎn)單,最能體現(xiàn)反饋思想的控制算法,可謂經(jīng)典中的經(jīng)典。經(jīng)典的未必是復(fù)雜的,經(jīng)典的東西常常是簡(jiǎn)單的,而且是最簡(jiǎn)單的,想想牛頓的力學(xué)三大定律吧,想想愛因斯坦的質(zhì)能方程吧,何等的簡(jiǎn)單!簡(jiǎn)單的不是原始的,簡(jiǎn)單的也不是落后的,簡(jiǎn)單到了美的程度。先看看PID算法的一般形式:

PID的流程簡(jiǎn)單到了不能再簡(jiǎn)單的程度,通過(guò)誤差信號(hào)控制被控量,而控制器本身就是比例、積分、微分三個(gè)環(huán)節(jié)的加和。這里我們規(guī)定(在t時(shí)刻):

1.輸入量為rin(t);

2.輸出量為rout(t);

3.偏差量為err(t)=rin(t)-rout(t);

pid的控制規(guī)律為

理解一下這個(gè)公式,主要從下面幾個(gè)問(wèn)題著手,為了便于理解,把控制環(huán)境具體一下:

1.規(guī)定這個(gè)流程是用來(lái)為直流電機(jī)調(diào)速的;

2.輸入量rin(t)為電機(jī)轉(zhuǎn)速預(yù)定值;

3.輸出量rout(t)為電機(jī)轉(zhuǎn)速實(shí)際值;

4.執(zhí)行器為直流電機(jī);

5.傳感器為光電碼盤,假設(shè)碼盤為10線;

6.直流電機(jī)采用PWM調(diào)速轉(zhuǎn)速用單位轉(zhuǎn)/min表示;

不難看出以下結(jié)論:

1.輸入量rin(t)為電機(jī)轉(zhuǎn)速預(yù)定值(轉(zhuǎn)/min);

2.輸出量rout(t)為電機(jī)轉(zhuǎn)速實(shí)際值(轉(zhuǎn)/min);

3.偏差量為預(yù)定值和實(shí)際值之差(轉(zhuǎn)/min);

那么以下幾個(gè)問(wèn)題需要弄清楚:

1.通過(guò)PID環(huán)節(jié)之后的U(t)是什么值呢?

2.控制執(zhí)行器(直流電機(jī))轉(zhuǎn)動(dòng)轉(zhuǎn)速應(yīng)該為電壓值(也就是PWM占空比)。

3.那么U(t)及PWM之間存在怎樣的聯(lián)系呢?/user1/3407/archives/2006/33541.html(見附錄1)這篇文章上給出了一種方法,即,每個(gè)電壓對(duì)應(yīng)一個(gè)轉(zhuǎn)速,電壓和轉(zhuǎn)速之間呈現(xiàn)線性關(guān)系。但是我考慮這種方法的前提是把直流電機(jī)的特性理解為線性了,而實(shí)際情況下,直流電機(jī)的特性絕對(duì)不是線性的,或者說(shuō)在局部上是趨于線性的,這就是為什么說(shuō)PID調(diào)速有個(gè)范圍的問(wèn)題。具體看一下http://articles.e-works/component/article90249.htm(見附錄2)這篇文章就可以了解了。所以在正式進(jìn)行調(diào)速設(shè)計(jì)之前,需要現(xiàn)有開環(huán)系統(tǒng),測(cè)試電機(jī)和轉(zhuǎn)速之間的特性曲線(或者查閱電機(jī)的資料說(shuō)明),然后再進(jìn)行閉環(huán)參數(shù)整定。這篇先寫到這,下一篇說(shuō)明連續(xù)系統(tǒng)的離散化問(wèn)題。并根據(jù)離散化后的特點(diǎn)講述位置型PID和增量型PID的用法和C語(yǔ)言實(shí)現(xiàn)過(guò)程。PID控制算法的C語(yǔ)言實(shí)現(xiàn)二PID算法的離散化

上一節(jié)中,我論述了PID算法的基本形式,并對(duì)其控制過(guò)程的實(shí)現(xiàn)有了一個(gè)簡(jiǎn)要的說(shuō)明,通過(guò)上一節(jié)的總結(jié),基本已經(jīng)可以明白PID控制的過(guò)程。這一節(jié)中先繼續(xù)上一節(jié)內(nèi)容補(bǔ)充說(shuō)明一下。

1.說(shuō)明一下反饋控制的原理,通過(guò)上一節(jié)的框圖不難看出,PID控制其實(shí)是對(duì)偏差的控制過(guò)程;

2.如果偏差為0,則比例環(huán)節(jié)不起作用,只有存在偏差時(shí),比例環(huán)節(jié)才起作用。

3.積分環(huán)節(jié)主要是用來(lái)消除靜差,所謂靜差,就是系統(tǒng)穩(wěn)定后輸出值和設(shè)定值之間的差值,積分環(huán)節(jié)實(shí)際上就是偏差累計(jì)的過(guò)程,把累計(jì)的誤差加到原有系統(tǒng)上以抵消系統(tǒng)造成的靜差。

4.而微分信號(hào)則反應(yīng)了偏差信號(hào)的變化規(guī)律,或者說(shuō)是變化趨勢(shì),根據(jù)偏差信號(hào)的變化趨勢(shì)來(lái)進(jìn)行超前調(diào)節(jié),從而增加了系統(tǒng)的快速性。

好了,關(guān)于PID的基本說(shuō)明就補(bǔ)充到這里,下面將對(duì)PID連續(xù)系統(tǒng)離散化,從而方便在處理器上實(shí)現(xiàn)。下面把連續(xù)狀態(tài)的公式再貼一下:

假設(shè)采樣間隔為T,則在第KT時(shí)刻:偏差err(K)=rin(K)-rout(K);積分環(huán)節(jié)用加和的形式表示,即err(K)+err(K+1)+……;微分環(huán)節(jié)用斜率的形式表示,即[err(K)-err(K-1)]/T;從而形成如下PID離散表示形式:則u(K)可表示成為:至于說(shuō)Kp、Ki、Kd三個(gè)參數(shù)的具體表達(dá)式,我想可以輕松的推出了,這里節(jié)省時(shí)間,不再詳細(xì)表示了。其實(shí)到這里為止,PID的基本離散表示形式已經(jīng)出來(lái)了。目前的這種表述形式屬于位置型PID,另外一種表述方式為增量式PID,由U上述表達(dá)式可以輕易得到:那么:這就是離散化PID的增量式表示方式,由公式可以看出,增量式的表達(dá)結(jié)果和最近三次的偏差有關(guān),這樣就大大提高了系統(tǒng)的穩(wěn)定性。需要注意的是最終的輸出結(jié)果應(yīng)該為

u(K)+增量調(diào)節(jié)值;PID的離散化過(guò)程基本思路就是這樣,下面是將離散化的公式轉(zhuǎn)換成為C語(yǔ)言,從而實(shí)現(xiàn)微控制器的控制作用。PID控制算法的C語(yǔ)言實(shí)現(xiàn)三位置型PID的C語(yǔ)言實(shí)現(xiàn)

上一節(jié)中已經(jīng)抽象出了位置性PID和增量型PID的數(shù)學(xué)表達(dá)式,這一節(jié),重點(diǎn)講解C語(yǔ)言代碼的實(shí)現(xiàn)過(guò)程,算法的C語(yǔ)言實(shí)現(xiàn)過(guò)程具有一般性,通過(guò)PID算法的C語(yǔ)言實(shí)現(xiàn),可以以此類推,設(shè)計(jì)其它算法的C語(yǔ)言實(shí)現(xiàn)。

第一步:定義PID變量結(jié)構(gòu)體,代碼如下:struct_pid{

floatSetSpeed;

//定義設(shè)定值

floatActualSpeed;

//定義實(shí)際值

floaterr;

//定義偏差值

floaterr_last;

//定義上一個(gè)偏差值

floatKp,Ki,Kd;

//定義比例、積分、微分系數(shù)

floatvoltage;

//定義電壓值(控制執(zhí)行器的變量)

floatintegral;

//定義積分值

}pid;控制算法中所需要用到的參數(shù)在一個(gè)結(jié)構(gòu)體中統(tǒng)一定義,方便后面的使用。

第二部:初始化變量,代碼如下:voidPID_init(){

printf("PID_initbegin\n");

pid.SetSpeed=0.0;

pid.ActualSpeed=0.0;

pid.err=0.0;

pid.err_last=0.0;

pid.voltage=0.0;

egral=0.0;

pid.Kp=0.2;

pid.Ki=0.015;

pid.Kd=0.2;

printf("PID_initend\n");

}統(tǒng)一初始化變量,尤其是Kp,Ki,Kd三個(gè)參數(shù),調(diào)試過(guò)程當(dāng)中,對(duì)于要求的控制效果,可以通過(guò)調(diào)節(jié)這三個(gè)量直接進(jìn)行調(diào)節(jié)。第三步:編寫控制算法,代碼如下:floatPID_realize(floatspeed){

pid.SetSpeed=speed;

pid.err=pid.SetSpeed-pid.ActualSpeed;

egral+=pid.err;

pid.voltage=pid.Kp*pid.err+pid.Ki*egral+pid.Kd*(pid.err-pid.err_last);

pid.err_last=pid.err;

pid.ActualSpeed=pid.voltage*1.0;

returnpid.ActualSpeed;

}注意:這里用了最基本的算法實(shí)現(xiàn)形式,沒有考慮死區(qū)問(wèn)題,沒有設(shè)定上下限,只是對(duì)公式的一種直接的實(shí)現(xiàn),后面的介紹當(dāng)中還會(huì)逐漸的對(duì)此改進(jìn)。

到此為止,PID的基本實(shí)現(xiàn)部分就初步完成了。下面是測(cè)試代碼:intmain(){

printf("Systembegin\n");

PID_init();

intcount=0;

while(count<1000)

{

floatspeed=PID_realize(200.0);

printf("%f\n",speed);

count++;

}

return0;

}下面是經(jīng)過(guò)1000次的調(diào)節(jié)后輸出的1000個(gè)數(shù)據(jù)(具體的參數(shù)整定過(guò)程就不說(shuō)明了,網(wǎng)上這種說(shuō)明非常多):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.820192

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.172019

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.201949

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.972019

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.999473PID控制算法的C語(yǔ)言實(shí)現(xiàn)四增量型PID的C語(yǔ)言實(shí)現(xiàn)

上一節(jié)中介紹了最簡(jiǎn)單的位置型PID的實(shí)現(xiàn)手段,這一節(jié)主要講解增量式PID的實(shí)現(xiàn)方法,位置型和增量型PID的數(shù)學(xué)公式請(qǐng)參見我的系列文《PID控制算法的C語(yǔ)言實(shí)現(xiàn)二》中的講解。實(shí)現(xiàn)過(guò)程仍然是分為定義變量、初始化變量、實(shí)現(xiàn)控制算法函數(shù)、算法測(cè)試四個(gè)部分,詳細(xì)分類請(qǐng)參加《PID控制算法的C語(yǔ)言實(shí)現(xiàn)三》中的講解,這里直接給出代碼了。

#include<stdio.h>

#include<stdlib.h>

struct_pid{

floatSetSpeed;

//定義設(shè)定值

floatActualSpeed;

//定義實(shí)際值

floaterr;

//定義偏差值

floaterr_next;

//定義上一個(gè)偏差值

floaterr_last;

//定義最上前的偏差值

floatKp,Ki,Kd;

//定義比例、積分、微分系數(shù)

}pid;

voidPID_init(){

pid.SetSpeed=0.0;

pid.ActualSpeed=0.0;

pid.err=0.0;

pid.err_last=0.0;

pid.err_next=0.0;

pid.Kp=0.2;

pid.Ki=0.015;

pid.Kd=0.2;

}

floatPID_realize(floatspeed){

pid.SetSpeed=speed;

pid.err=pid.SetSpeed-pid.ActualSpeed;

floatincrementSpeed=pid.Kp*(pid.err-pid.err_next)+pid.Ki*pid.err+pid.Kd*(pid.err-2*pid.err_next+pid.err_last);

pid.ActualSpeed+=incrementSpeed;

pid.err_last=pid.err_next;

pid.err_next=pid.err;

returnpid.ActualSpeed;

}

intmain(){

PID_init();

intcount=0;

while(count<1000)

{

floatspeed=PID_realize(200.0);

printf("%f\n",speed);

count++;

}

return0;

}

運(yùn)行后的1000個(gè)數(shù)據(jù)為:83.000000

11.555000

59.559677

28.175406

52.907425

38.944149

51.891701

46.141655

53.339050

51.510002

55.908447

55.944637

58.970676

59.882942

62.224998

63.537247

65.527702

67.011047

68.810638

70.355309

72.042023

73.595642

75.207603

76.745430

78.301514

79.812126

81.321915

82.800293

84.268898

85.713097

87.143440

88.552994

89.946945

91.322067

92.680977

94.022224

95.347176

96.655235

97.947174

99.222801

100.482597

101.726562

102.955040

104.168114

105.366058

106.549004

107.717178

108.870743

110.009888

111.134796

112.245636

113.342598

114.425842

115.495552

116.551880

117.595009

118.625099

119.642311

120.646812

121.638756

122.618294

123.585594

124.540794

125.484062

126.415535

127.335365

128.243698

129.140671

130.026443

130.901138

131.764893

132.617859

133.460159

134.291931

135.113297

135.924408

136.725372

137.516327

138.297394

139.068695

139.830353

140.582489

141.325226

142.058685

142.782974

143.498199

144.204498

144.901962

145.590714

146.270844

146.942474

147.605713

148.260651

148.907410

149.546082

150.176773

150.799576

151.414597

152.021927

152.621674

153.213913

153.798752

154.376282

154.946594

155.509781

156.065918

156.615112

157.157440

157.692993

158.221848

158.744095

159.259811

159.769073

160.271973

160.768585

161.258987

161.743271

162.221481

162.693726

163.160065

163.620575

164.075333

164.524399

164.967865

165.405777

165.838226

166.265259

166.686951

167.103378

167.514587

167.920670

168.321671

168.717667

169.108704

169.494858

169.876175

170.252731

170.624588

170.991791

171.354401

171.712479

172.066086

172.415268

172.760086

173.100601

173.436844

173.768890

174.096786

174.420578

174.740326

175.056076

175.367889

175.675797

175.979858

176.280121

176.576630

176.869431

177.158569

177.444092

177.726044

178.004471

178.279419

178.550934

178.819046

179.083817

179.345276

179.603470

179.858429

180.110214

180.358841

180.604370

180.846817

181.086243

181.322662

181.556137

181.786682

182.014359

182.239182

182.461197

182.680435

182.896942

183.110733

183.321854

183.530334

183.736206

183.939514

184.140274

184.338531

184.534302

184.727631

184.918533

185.107056

185.293228

185.477066

185.658615

185.837891

186.014923

186.189743

186.362381

186.532852

186.701202

186.867432

187.031601

187.193710

187.353790

187.511871

187.667984

187.822128

187.974365

188.124680

188.273132

188.419724

188.564484

188.707428

188.848587

188.987976

189.125626

189.261566

189.395798

189.528351

189.659256

189.788513

189.916168

190.042221

190.166702

190.289627

190.411011

190.530884

190.649246

190.766144

190.881561

190.995544

191.108109

191.219254

191.329025

191.437408

191.544449

191.650146

191.754517

191.857590

191.959366

192.059875

192.159134

192.257141

192.353928

192.449509

192.543884

192.637085

192.729126

192.820007

192.909760

192.998383

193.085907

193.172333

193.257675

193.341965

193.425186

193.507385

193.588531

193.668686

193.747818

193.825974

193.903152

193.979370

194.054626

194.128952

194.202332

194.274811

194.346375

194.417053

194.486832

194.555756

194.623810

194.691010

194.757370

194.822906

194.887619

194.951523

195.014633

195.076950

195.138489

195.199265

195.259277

195.318542

195.377060

195.434845

195.491913

195.548264

195.603912

195.658859

195.713135

195.766724

195.819641

195.871902

195.923508

195.974472

196.024796

196.074493

196.123566

196.172028

196.219879

196.267136

196.313797

196.359879

196.405380

196.450317

196.494690

196.538513

196.581787

196.624512

196.666702

196.708374

196.749512

196.790146

196.830261

196.869888

196.909012

196.947647

196.985809

197.023483

197.060699

197.097443

197.133728

197.169556

197.204941

197.239883

197.274384

197.308456

197.342102

197.375320

197.408127

197.440521

197.472519

197.504105

197.535309

197.566116

197.596542

197.626587

197.656250

197.685547

197.714478

197.743042

197.771255

197.799118

197.826630

197.853806

197.880630

197.907120

197.933289

197.959122

197.984634

198.009827

198.034698

198.059265

198.083527

198.107483

198.131134

198.154495

198.177567

198.201948

198.222839

198.245056

198.266998

198.288666

198.310059

198.331177

198.352036

198.372635

198.392975

198.413071

198.432907

198.452499

198.471848

198.490952

198.509811

198.528442

198.546829

198.565002

198.582932

198.600647

198.618134

198.635406

198.652466

198.669312

198.685944

198.702377

198.718597

198.734619

198.750443

198.766068

198.781494

198.796722

198.811768

198.826614

198.841278

198.855759

198.870056

198.884186

198.898132

198.911911

198.925507

198.938934

198.952194

198.965286

198.978226

198.990997

199.003616

199.016068

199.028366

199.040512

199.052505

199.064346

199.076050

199.087601

199.099014

199.110275

199.121399

199.132385

199.143234

199.153946

199.164520

199.174973

199.185287

199.195465

199.205521

199.215454

199.225266

199.234955

199.244522

199.253967

199.263290

199.272507

199.281601

199.290588

199.299454

199.308212

199.316864

199.325409

199.333847

199.342178

199.350403

199.358521

199.366547

199.374466

199.382294

199.390015

199.397644

199.405167

199.412613

199.419952

199.427200

199.434357

199.441422

199.448410

199.455307

199.462112

199.468842

199.475479

199.482040

199.488510

199.494904

199.501221

199.507462

199.513611

199.519699

199.525696

199.531631

199.537476

199.543259

199.548965

199.554611

199.560181

199.565674

199.571106

199.576462

199.581757

199.586990

199.592148

199.597244

199.602280

199.607254

199.612167

199.617020

199.621811

199.626541

199.631210

199.635818

199.640366

199.644867

199.649307

199.653687

199.658020

199.662292

199.666519

199.670685

199.674805

199.678864

199.682877

199.686844

199.690750

199.694626

199.698441

199.702209

199.705933

199.709610

199.713242

199.716827

199.720367

199.723862

199.727310

199.730713

199.734085

199.737411

199.740692

199.743942

199.747147

199.750305

199.753433

199.756516

199.759567

199.762573

199.765549

199.768478

199.771378

199.774231

199.777054

199.779846

199.782593

199.785309

199.787994

199.790649

199.793259

199.795853

199.798401

199.800919

199.803406

199.805862

199.808289

199.810684

199.813049

199.815384

199.817688

199.819962

199.822220

199.824432

199.826630

199.828796

199.830933

199.833054

199.835144

199.837204

199.839233

199.841248

199.843231

199.845200

199.847137

199.849045

199.850937

199.852798

199.854645

199.856461

199.858261

199.860031

199.861786

199.863510

199.865219

199.866898

199.868561

199.870209

199.871826

199.873428

199.875015

199.876572

199.878113

199.879639

199.881149

199.882629

199.884094

199.885544

199.886978

199.888397

199.889786

199.891174

199.892532

199.893875

199.895203

199.896515

199.897812

199.899094

199.900360

199.901611

199.902847

199.904068

199.905273

199.906464

199.907639

199.908798

199.909943

199.911072

199.912186

199.913284

199.914368

199.915436

199.916489

199.917526

199.918564

199.919571

199.920578

199.921570

199.922546

199.923523

199.924469

199.925415

199.926346

199.927261

199.928177

199.929077

199.929962

199.930832

199.931702

199.932556

199.933395

199.934235

199.935059

199.935867

199.936676

199.937469

199.938248

199.939026

199.939789

199.940536

199.941284

199.942019

199.942749

199.943466

199.944168

199.944870

199.945557

199.946243

199.946915

199.947586

199.948242

199.948883

199.949524

199.950150

199.950775

199.951385

199.951996

199.952591

199.953186

199.953766

199.954346

199.954910

199.955475

199.956024

199.956573

199.957108

199.957642

199.958176

199.958694

199.959213

199.959717

199.960220

199.960724

199.961212

199.961700

199.962173

199.962646

199.963120

199.963577

199.964035

199.964478

199.964920

199.965363

199.965790

199.966217

199.966644

199.967056

199.967468

199.967880

199.968277

199.968674

199.969070

199.969452

199.969833

199.970215

199.970581

199.970947

199.971313

199.971664

199.972019

199.972366

199.972717

199.973053

199.973389

199.973724

199.974045

199.974380

199.974701

199.975021

199.975327

199.975632

199.975937

199.976242

199.976532

199.976822

199.977112

199.977402

199.977676

199.977966

199.978241

199.978516

199.978790

199.979050

199.979309

199.979568

199.979828

199.980072

199.980331

199.980576

199.980820

199.981064

199.981293

199.981537

199.981766

199.981995

199.982224

199.982437

199.982666

199.982880

199.983093

199.983307

199.983521

199.983719

199.983932

199.984131

199.984329

199.984528

199.984726

199.984909

199.985107

199.985291

199.985474

199.985657

199.985840

199.986023

199.986191

199.986374

199.986542

199.986710

199.986877

199.987045

199.987213

199.987366

199.987534

199.987686

199.987839

199.987991

199.988144

199.988297

199.988449

199.988586

199.988739

199.988876

199.989014

199.989151

199.989288

199.989426

199.989563

199.989685

199.989822

199.989944

199.990067

199.990189

199.990311

199.990433

199.990555

199.990677

199.990799

199.990906

199.991028

199.991135

199.991257

199.991364

199.991470

199.991577

199.991684

199.991791

199.991898

199.992019

199.992096

199.992203

199.992294

199.992401

199.992493

199.992584

199.992676

199.992767

199.992859

199.992950

199.993042

199.993134

199.993225

199.993301

199.993393

199.993469

199.993561

199.993637

199.993713

199.993790

199.993866

199.993942

199.994019

199.994095

199.994171

199.994247

199.994324

199.994400

199.994476

199.994537

199.994614

199.994675

199.994751

199.994812

199.994873

199.994934

199.994995

199.995056

199.995117

199.995178

199.995239

199.995300

199.995361

199.995422

199.995483

199.995544

199.995605

199.995667

199.995712

199.995773

199.995819

199.995880

199.995926

199.995987

199.996033

199.996094

199.996140

199.996185

199.996231

199.996277

199.996323

199.996368

199.996414

199.996460

199.996506

199.996552

199.996597

199.996643

199.996689

199.996735

199.996780

199.996826

199.996872

199.996902

199.996948

199.996979

199.997025

199.997055

199.997101

199.997131

199.997177

199.997208

199.997253

199.997284

199.997314

199.997345

199.997375

199.997406

199.997437

199.997467

199.997498

199.997528

199.997559

199.997589

199.997620

199.997650

199.997681

199.997711

199.997742

199.997772

199.997803

199.997833

199.997864

199.997894

199.997925

199.997955

199.997986

199.998016

199.998047

199.998077

199.998093

199.998123

199.998138

199.998169

199.998184

199.998215

199.998230

199.998260

199.998276

199.998306

199.998322

199.998352

199.998367

199.998398

199.998413

199.998444

199.998459

199.998489

199.998505

199.998520

199.998535

199.998550

199.998566

199.998581

199.998596

199.998611

199.998627

199.998642

199.998657

199.998672

199.998688

199.998703

199.998718

199.998734

199.998749

199.998764

199.998779

199.998795

199.998810

199.998825

199.998840

199.998856

199.998871

199.998886

199.998901

199.998917

199.998932

199.998947

199.998962

199.998978

199.998993

199.999008

199.999023

199.999039

199.999054

199.999069

199.999084

199.999100

199.999115

199.999130

199.999146

199.999161

199.999176

199.999191

199.999207

199.999222

199.999237

199.999252

199.999268

199.999283

199.999298

199.999298

199.999313

199.999313

199.999329

199.999329

199.999344

199.999344

199.999359

199.999359

199.999374

199.999374

199.999390

199.999390

199.999405

199.999405

199.999420

199.999420

199.999435

199.999435

199.999451

199.999451

199.999466

199.999466

199.999481

199.999481

199.999496

199.999496

199.999512

199.999512PID控制算法的C語(yǔ)言實(shí)現(xiàn)五積分分離的PID控制算法C語(yǔ)言實(shí)現(xiàn)

通過(guò)三、四兩篇文章,基本上已經(jīng)弄清楚了PID控制算法的最常規(guī)的表達(dá)方法。在普通PID控制中,引入積分環(huán)節(jié)的目的,主要是為了消除靜差,提高控制精度。但是在啟動(dòng)、結(jié)束或大幅度增減設(shè)定時(shí),短時(shí)間內(nèi)系統(tǒng)輸出有很大的偏差,會(huì)造成PID運(yùn)算的積分積累,導(dǎo)致控制量超過(guò)執(zhí)行機(jī)構(gòu)可能允許的最大動(dòng)作范圍對(duì)應(yīng)極限控制量,從而引起較大的超調(diào),甚至是震蕩,這是絕對(duì)不允許的。

為了克服這一問(wèn)題,引入了積分分離的概念,其基本思路是當(dāng)被控量及設(shè)定值偏差較大時(shí),取消積分作用;當(dāng)被控量接近給定值時(shí),引入積分控制,以消除靜差,提高精度。其具體實(shí)現(xiàn)代碼如下:

pid.Kp=0.2;

pid.Ki=0.04;

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論