zoukankan      html  css  js  c++  java
  • PID控制算法的C语言实现四 增量型PID的C语言实现

     

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

    struct _pid{
        float SetSpeed;            //定义设定值
        float ActualSpeed;        //定义实际值
        float err;                //定义偏差值
        float err_next;            //定义上一个偏差值
        float err_last;            //定义最上前的偏差值
        float Kp,Ki,Kd;            //定义比例、积分、微分系数
    }pid;

    void PID_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;
    }

    float PID_realize(float speed){
        pid.SetSpeed=speed;
        pid.err=pid.SetSpeed-pid.ActualSpeed;
        float incrementSpeed=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;
        return pid.ActualSpeed;
    }

    int main(){
        PID_init();
        int count=0;
        while(count<1000)
        {
            float speed=PID_realize(200.0);
            printf("%f ",speed);
            count++;
        }
        return 0;
    }



    运行后的1000个数据为:

    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.200348
    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.942017
    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.972015
    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.992004
    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.999512

     

  • 相关阅读:
    结对项目——自动生成小学四则运算题目
    个人项目作业
    自我介绍+软工5问
    团队作业3--需求改进&系统设计
    团队作业2-需求规格说明书
    团队项目-第一周
    结对项目:四则运算表达式生成程序
    个人项目作业--WC的实现
    自我介绍+软工五问
    团队作业3-需求改进&系统设计
  • 原文地址:https://www.cnblogs.com/ITGUANCHAZHE/p/14319752.html
Copyright © 2011-2022 走看看