zoukankan      html  css  js  c++  java
  • 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 ");
        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 ");
    }

    统一初始化变量,尤其是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 ");
        PID_init();
        int count=0;
        while(count<1000)
        {
            float speed=PID_realize(200.0);
            printf("%f ",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

  • 相关阅读:
    postgresql postgres.Jsonb
    gorm jsonb
    json
    postgresql重置序列和自增主键
    go build x509: certificate has expired or is not yet valid
    权限修饰符
    交换两个变量的值
    Java编译报错:编码GBK的不可映射字符
    原码反码补码的理解
    Scanner类中hasNext()方法的解析
  • 原文地址:https://www.cnblogs.com/ITGUANCHAZHE/p/14319753.html
Copyright © 2011-2022 走看看