{"id":452,"date":"2025-03-24T00:04:09","date_gmt":"2025-03-23T16:04:09","guid":{"rendered":"http:\/\/blog.huanmie.fun\/?p=452"},"modified":"2025-03-24T00:09:11","modified_gmt":"2025-03-23T16:09:11","slug":"%e5%9f%ba%e4%ba%8everilog%e7%9a%84pid%e6%8e%a7%e5%88%b6%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.huanmie.fun\/index.php\/2025\/03\/24\/%e5%9f%ba%e4%ba%8everilog%e7%9a%84pid%e6%8e%a7%e5%88%b6%e7%ae%97%e6%b3%95\/","title":{"rendered":"\u57fa\u4e8eVerilog\u7684PID\u63a7\u5236\u7b97\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7b97\u6cd5\u7b80\u4ecb\uff1a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PID \u7b97\u6cd5\u5373\uff1a\u6bd4\u4f8b - \u79ef\u5206 - \u5fae\u5206\u7b97\u6cd5\uff0c\u662f\u81ea\u52a8\u63a7\u5236\u9886\u57df\u5e7f\u6cdb\u5e94\u7528\u7684\u7ecf\u5178\u63a7\u5236\u7b97\u6cd5\uff0c\u5728\u7269\u8054\u7f51\u6052\u6e29\u6c34\u6696\u63a7\u5236\u7cfb\u7edf\u4e2d\u53d1\u6325\u5173\u952e\u4f5c\u7528\u3002\u5b83\u4f9d\u636e\u8bbe\u5b9a\u503c\u4e0e\u5b9e\u9645\u6d4b\u91cf\u503c\u7684\u504f\u5dee\u8fdb\u884c\u63a7\u5236\uff0c\u7531\u6bd4\u4f8b\uff08P\uff09\u3001\u79ef\u5206\uff08I\uff09\u3001\u5fae\u5206\uff08D\uff09\u4e09\u4e2a\u73af\u8282\u6784\u6210\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u91c7\u7528\u5b9e\u9a8c\u6cd5\u6216\u7406\u8bba\u8ba1\u7b97\u6cd5\u5bf9 PID \u53c2\u6570\u8fdb\u884c\u6574\u5b9a\uff0c\u901a\u8fc7\u5bf9\u7cfb\u7edf\u8fdb\u884c\u9636\u8dc3\u54cd\u5e94\u6d4b\u8bd5\uff0c\u83b7\u53d6\u7cfb\u7edf\u7684\u4e34\u754c\u589e\u76ca\u548c\u4e34\u754c\u5468\u671f\uff0c\u7136\u540e\u6839\u636e\u7ecf\u9a8c\u516c\u5f0f\u8ba1\u7b97\u51fa PID \u53c2\u6570\u7684\u521d\u59cb\u503c\uff0c\u518d\u901a\u8fc7\u53cd\u590d\u8bd5\u9a8c\u8c03\u6574\u53c2\u6570\uff0c\u4f7f\u7cfb\u7edf\u8fbe\u5230\u6700\u4f73\u63a7\u5236\u6548\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fde\u7eed\u63a7\u5236\u7cfb\u7edf\u7684\u7406\u60f3PID\u63a7\u5236\u89c4\u5f8b\u4e3a\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"399\" height=\"45\" src=\"http:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745830-image.png\" alt=\"\" class=\"wp-image-477\" srcset=\"https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745830-image.png 399w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745830-image-300x34.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u5f0f\u4e2d\uff0cKp\u2014\u2014\u6bd4\u4f8b\u589e\u76ca\uff0cKp\u4e0e\u6bd4\u4f8b\u5ea6\u6210\u5012\u6570\u5173\u7cfb\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tt\u2014\u2014\u79ef\u5206\u65f6\u95f4\u5e38\u6570\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TD\u2014\u2014\u5fae\u5206\u65f6\u95f4\u5e38\u6570\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u\uff08t\uff09\u2014\u2014PID\u63a7\u5236\u5668\u7684\u8f93\u51fa\u4fe1\u53f7\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e\uff08t\uff09\u2014\u2014\u7ed9\u5b9a\u503cr\uff08t\uff09\u4e0e\u6d4b\u91cf\u503c\u4e4b\u5dee\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bbe\u8ba1\u601d\u8def\uff1a<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6839\u636ePID\u7b97\u6cd5\u4f2a\u4ee3\u7801\u53ef\u5c06\u8ba1\u7b97\u6d41\u7a0b\u62c6\u5206\u6210\u5982\u4e0b\u9636\u6bb5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. \u8ba1\u7b97\u672c\u6b21\u504f\u5dee<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>setpoint<\/code>\uff1a\u671f\u671b\u7684\u7cfb\u7edf\u8f93\u51fa\u503c\u3002<\/li>\n\n\n\n<li><code>measured_value<\/code>\uff1a\u7cfb\u7edf\u5f53\u524d\u7684\u5b9e\u9645\u8f93\u51fa\u503c\u3002<\/li>\n\n\n\n<li><code>error0<\/code>\uff1a\u5f53\u524d\u65f6\u523b\u7684\u504f\u5dee\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">2. \u79ef\u5206\u9879\u8ba1\u7b97\u4e0e\u9650\u5e45<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>integral<\/code>\uff1a\u79ef\u5206\u9879<\/li>\n\n\n\n<li><code>integral_limit<\/code>\uff1a\u79ef\u5206\u9650\u5e45\uff0c\u7528\u4e8e\u9632\u6b62\u79ef\u5206\u9879\u65e0\u9650\u5236\u5730\u589e\u957f\uff0c\u907f\u514d\u51fa\u73b0\u79ef\u5206\u9971\u548c\u73b0\u8c61\u3002<br><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3. \u5fae\u5206\u9879\u8ba1\u7b97<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>derivative<\/code>\uff1a\u5fae\u5206\u9879\u3002<\/li>\n\n\n\n<li><code>T<\/code>\uff1a\u91c7\u6837\u65f6\u95f4\u95f4\u9694\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;4.PID \u8f93\u51fa\u8ba1\u7b97<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>out<\/code>\uff1aPID \u63a7\u5236\u5668\u7684\u8f93\u51fa\u3002<\/li>\n\n\n\n<li><code>Kp<\/code>\uff1a\u6bd4\u4f8b\u7cfb\u6570\uff0c\u7528\u4e8e\u653e\u5927\u5f53\u524d\u504f\u5dee\u7684\u5f71\u54cd\u3002<\/li>\n\n\n\n<li><code>Ki<\/code>\uff1a\u79ef\u5206\u7cfb\u6570\uff0c\u7528\u4e8e\u653e\u5927\u79ef\u5206\u9879\u7684\u5f71\u54cd\u3002<\/li>\n\n\n\n<li><code>Kd<\/code>\uff1a\u5fae\u5206\u7cfb\u6570\uff0c\u7528\u4e8e\u653e\u5927\u5fae\u5206\u9879\u7684\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<p class=\"wp-block-paragraph\">loop:<br>error0 := setpoint \u2212 measured_value \/\/\u672c\u6b21\u504f\u5dee<br>integral := integral + error0 \/\/\u79ef\u5206<br>if(integral &gt; integral_limit) integral = integral_limit \/\/\u79ef\u5206\u9650\u5e45<br>else if (integral &lt; -integral_limit) integral = -integral_limit<br>derivative := (error0 \u2212 error1) \/ T \/\/\u5fae\u5206<br>out := Kp \u00d7 error0 + Ki \u00d7 integral + Kd \u00d7 derivative<br>\/\/<br>\/\/\u8bb0\u5f55\u504f\u5dee<br>error1 := error0<br>wait(T) \/\/\u95f4\u9694T\u8fd0\u884c<\/p>\n<\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">PID\u7b97\u6cd5 RTL\u8bbe\u8ba1<\/h2>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>PID_Ctrl.v<\/summary>\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u57fa\u4e8eVerilog\u7684PID\u63a7\u5236\u5668\u6a21\u5757\n\/\/  \u4f4d\u7f6e\u5f0f\u79bb\u6563PID\u516c\u5f0f \n\/\/  out=Kp*error(t)+Ki*Sum_error(t)+Kd*&#91;error(t)-error(t-1)]\n\/\/  *\/\n\/\/  error1 := 0\t\t\/\/\u4e0a\u6b21\u504f\u5dee\n\/\/  integral := 0\t\/\/\u79ef\u5206\u548c\n\/\/  integral_limit :=100 \/\/\u79ef\u5206\u9650\u5e45\n\/\/  out := 0\t\t\/\/\u8f93\u51fa\n\/\/  \/\/setpoint \u8bbe\u5b9a\u503c\u4e3a\u5165\u53e3\u53c2\u6570\n\/\/  \/\/\u5faa\u73af\n\/\/  \/\/\u91c7\u6837\u5468\u671f\u4e3aT\n\/\/  \/\/measured_value \u91c7\u6837\u5f97\u5230\u503c\n\n\nmodule PID_Ctrl(\n    input clk                   ,    \/\/ 50MHz\u4e3b\u65f6\u949f\n    input rst_n                 ,    \/\/ \u5f02\u6b65\u590d\u4f4d\n    input &#91;15:0] temp_hex       ,    \/\/ \u6e29\u5ea6\u503c\n    input symbol                ,    \/\/ \u6e29\u5ea6\u7b26\u53f7\n    input hex_in_valid          ,    \/\/ \u8f93\u5165\u6570\u636e\u6709\u6548\n    input &#91;15:0] ex_hex         ,    \/\/ \u671f\u671b\u503c\n    output &#91;15:0] pid_do        ,   \/\/ PID\u8f93\u51fa \n    output reg &#91;15:0] half_float_pid_do, \/\/ \u534a\u7cbe\u5ea6\u6d6e\u70b9\u6570\u8f93\u51fa\n    output  pid_done                 \/\/ PID\u8ba1\u7b97\u5b8c\u6210\n    \n);\n\nassign rst = ~rst_n;\nreg pid_half_float_done;\nwire &#91;15:0] fixed_to_float_out,ex_fix_to_float;\nwire &#91;15:0] str_to_float;\n\n\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse0(\n    .realstr_in({320'b0,\"0.01\"}),\n    .float_out (str_to_float)\n);\n\/\/\u79ef\u5206\u9650\u5e45\nwire &#91;15:0] integral_limit_P;\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse1(\n    .realstr_in({320'b0,\"100\"}),\n    .float_out (integral_limit_P )\n);\nwire &#91;15:0] integral_limit_N;\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse2(\n    .realstr_in({320'b0,\"-100\"}),\n    .float_out (integral_limit_N )\n);\nwire &#91;15:0] Kp,Ki,Kd;\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse3(\n    .realstr_in({320'b0,\"0.5\"}),\n    .float_out (Kp )\n);\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse4(\n     .realstr_in({320'b0,\"0.2\"}),\n    .float_out (Ki )\n);\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse5(\n    .realstr_in({320'b0,\"0.05\"}),\n    .float_out (Kd )\n);\n\nwire &#91;15:0] float_zero;\nconst_realstr_to_float_tbuse #(\n    .float_type     (\"half_float\")\n\n) const_realstr_to_float_tbuse6(\n    .realstr_in({320'b0,\"0\"}),\n    .float_out (float_zero )\n);\n\/\/\u8f93\u5165\u683c\u5f0f\u4f4dxxx.xx\nfixed_to_float #(\n    .int_width      (16     ),\n    .decimal_width  (0      ),\n    .float_type     (\"half_float\")\n)fixed_to_float_0(\n    .float_out(fixed_to_float_out       ),\n    .valid_out(fixed_to_float_out_done  ),\n    .fixed_in (temp_hex                  ),\n    .valid_in (hex_in_valid             ),\n    .clk      (clk                      ),\n    .rst      (rst                      )\n);\n\/\/\u6d6e\u70b9\u6570\u4e58\u6cd5\nwire &#91;15:0] hex_to_float;\nhalf_float_optimize_multiplier half_float_optimize_multiplier0(\n    .clk            (clk                                    ),\n    .rst            (rst                                    ),\n    .float_ina      (str_to_float                           ),\n    .float_inb      (fixed_to_float_out                     ),\n    .valid_in       (fixed_to_float_out_done                ),\n    .float_result   (hex_to_float                           ),\n    .valid_out      (hex_to_float_done                      )\n);\n\nwire &#91;15:0] pid_float_do_tmp;\nwire &#91;1:0] compare_result_zero;\n\/\/assign pid_float_do_tmp = (compare_result_zero == 2'b00) ? float_zero : half_float_pid_do;\nassign pid_float_do_tmp = half_float_pid_do;\n\n\/\/\u6d6e\u70b9\u6570\u4e58\u6cd5\nwire &#91;15:0] float_x100;\nhalf_float_optimize_multiplier half_float_optimize_multiplierx100(\n    .clk            (clk                                    ),\n    .rst            (rst                                    ),\n    .float_ina      (integral_limit_P                       ),\n    .float_inb      (pid_float_do_tmp                      ),\n    .valid_in       (pid_half_float_done                   ),\n    .float_result   (float_x100                            ),\n    .valid_out      (float_x100_done                      )\n);\n\n\/\/\u6d6e\u70b9\u8f6c\u5b9a\u70b9\nfloat_to_fixed #(\n        .int_width(16),\n        .decimal_width(0),\n        .float_type(\"half_float\")\n        ) FLOAT_TO_FIXED\n        (\n        .fixed_out  (pid_do              ),\n        .fixed_nan  (),\n        .valid_out  (pid_done            ),\n        .float_in   (float_x100          ),\n        .valid_in   (float_x100_done     ),\n        .clk        (clk                  ),\n        .rst        (rst                  )\n);\n\n\/\/\u6d6e\u70b9\u6570\u8f6c\u5b9a\u70b9 ex_hex\nfixed_to_float #(\n    .int_width      (16     ),\n    .decimal_width  (0      ),\n    .float_type     (\"half_float\")\n)fixed_to_float_1(\n    .float_out(ex_fix_to_float       ),\n    .valid_out(ex_fix_to_float_done  ),\n    .fixed_in (ex_hex                ),\n    .valid_in (hex_in_valid            ),\n    .clk      (clk                      ),\n    .rst      (rst                      )\n);\nwire &#91;15:0] ex_hex_to_float;\n\/\/\u6d6e\u70b9\u6570\u4e58\u6cd5\nhalf_float_optimize_multiplier half_float_optimize_multiplier2(\n    .clk            (clk                                    ),\n    .rst            (rst                                    ),\n    .float_ina      (str_to_float                           ),\n    .float_inb      (ex_fix_to_float                        ),\n    .valid_in       (ex_fix_to_float_done                   ),\n    .float_result   (ex_hex_to_float                        ),\n    .valid_out      (ex_hex_to_float_done                   )\n);\n\n\/\/\u6d6e\u70b9\u6570\u52a0\u6cd5\nreg &#91;15:0] add_0_da,add_0_db;\nwire&#91;15:0] add_0_ds;\nwire add_0_done;\nreg add_0_start;\nfloat_adder #(\n    .float_type(\"half_float\")\n) float_adder0(\n    .clk         (clk               ),\n    .rst         (rst               ),\n    .float_ina   (add_0_da          ),\n    .float_inb   (add_0_db          ),\n    .valid_in    (add_0_start       ),\n    .float_result(add_0_ds          ),\n    .valid_out   (add_0_done        )\n);\n\n\/\/\u6d6e\u70b9\u6570\u4e58\u6cd5\nreg &#91;15:0] mult1_da,mult1_db;\nreg mult1_start;\nwire&#91;15:0] mult1_ds;\nwire mult1_done;\nhalf_float_optimize_multiplier half_float_optimize_multiplier1(\n    .clk            (clk            ),\n    .rst            (rst            ),\n    .float_ina      (mult1_da       ),\n    .float_inb      (mult1_db       ),\n    .valid_in       (mult1_start    ),\n    .float_result   (mult1_ds       ),\n    .valid_out      (mult1_done     )\n\n);\n\n\/\/\u6d6e\u70b9\u6570\u51cf\u6cd5\nreg &#91;15:0] add_sub0_da,add_sub0_db;\nwire&#91;15:0] add_sub0_ds;\nwire add_sub0_done;\nreg add_sub0_start;\nfloat_subtracter #(\n    .float_type(\"half_float\")\n) float_subtracter0(\n    .clk         (clk            ),\n    .rst         (rst            ),\n    .float_ina   (add_sub0_da    ),\n    .float_inb   (add_sub0_db    ),\n    .valid_in    (add_sub0_start ),\n    .float_result(add_sub0_ds    ),\n    .valid_out   (add_sub0_done  )\n);\n\/\/\u6d6e\u70b9\u6570\u6bd4\u5927\u5c0f\n\/\/2'b00:float_ina &lt; float_inb\n\/\/2'b01:float_ina == float_inb\n\/\/2'b10:float_ina > float_inb\n\/\/2'b11:\u65e0\u6cd5\u6bd4\u8f83\uff08\u4e24\u8005\u81f3\u5c11\u5305\u542b\u4e00\u4e2aNAN\uff09\nwire &#91;1:0] compare_result_N,compare_result_P;\nfloat_compare #(\n    .exponent_width(5),\n    .mantissa_width(10)\n    ) FLOAT_CMP\n    (\n    .compare_result(compare_result_P),\n    .float_ina(add_0_ds),\n    .float_inb(integral_limit_P)\n    );\nwire &#91;1:0] compare_result;\nfloat_compare #(\n    .exponent_width(5),\n    .mantissa_width(10)\n    ) FLOAT_CMP1\n    (\n    .compare_result(compare_result_N),\n    .float_ina(add_0_ds),\n    .float_inb(integral_limit_N)\n    );\n\n\/\/\u6d6e\u70b9\u6570\u6bd4\u5927\u5c0f \u5224\u65ad\u6d6e\u70b9\u6570\u662f\u5426\u5927\u4e8e0\n\nfloat_compare #(\n    .exponent_width(5),\n    .mantissa_width(10)\n    ) FLOAT_CMP2\n    (\n    .compare_result(compare_result_zero),\n    .float_ina(half_float_pid_do),\n    .float_inb(float_zero)\n    );\n\n\/\/\u4f2a\u4ee3\u7801\n\/\/  loop: \n\/\/      error0 := setpoint \u2212 measured_value\t\/\/\u672c\u6b21\u504f\u5dee\n\/\/      integral := integral + error0\t\/\/\u79ef\u5206\n\/\/      if(integral > integral_limit) integral = integral_limit\t\/\/\u79ef\u5206\u9650\u5e45\n\/\/      else if (integral &lt; -integral_limit) integral = -integral_limit\n\/\/      derivative := (error0 \u2212 error1) \/ T\t\/\/\u5fae\u5206\n\/\/      out := Kp \u00d7 error0 + Ki \u00d7 integral + Kd \u00d7 derivative\n\/\/  \n\/\/      \/\/\u8bb0\u5f55\u504f\u5dee\n\/\/      error1 := error0\n\/\/      wait(T)\t\/\/\u95f4\u9694T\u8fd0\u884c\n\/\/      goto loop\n\/\/integral = add_sub0_ds\n\nreg &#91;15:0] integral;\nreg &#91;15:0] derivative;\nreg &#91;15:0] error1,error0;\nreg &#91;15:0] multd1,multd2,multd3;\nreg &#91;15:0] add1;\n\nreg &#91;7:0]Count;\/\/\u5e8f\u5217\u673a\u57fa\u6570\nalways@(posedge clk or negedge rst_n)\nif(!rst_n)\n\tCount &lt;= 1'd0;\nelse if(Count == 8'd101)\n\tCount &lt;= 1'd0;\nelse \n\tCount &lt;= Count + 1'd1;\n\nalways @(posedge clk or negedge rst_n) begin\n    if(!rst_n)begin\n        error1 &lt;= 16'd0;\n        integral &lt;= 16'd0;\n        pid_half_float_done &lt;= 1'b0;\n  \n        derivative &lt;= 16'd0;\n    end else begin\n        case(Count)\n        8'd1 : begin \/\/error0 := setpoint \u2212 measured_value\t\/\/\u672c\u6b21\u504f\u5dee\n            pid_half_float_done = 1'b0;\n            {add_sub0_da    ,   add_sub0_db} = {ex_hex_to_float,hex_to_float};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b010;\n\n        end\n        8'd12 : begin \/\/integral := integral + error0\t\/\/\u79ef\u5206\n            error0 = add_sub0_ds;\n            {add_0_da    ,   add_0_db} = {error0,integral};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b100;\n        end\n        8'd24 : begin \/\/if(integral > integral_limit) integral = integral_limit\t\/\/\u79ef\u5206\u9650\u5e45\n            if(compare_result_P == 2'b10)begin\n                integral &lt;= integral_limit_P;\n            end else begin\n                \n                if(compare_result_N == 2'b00)begin\n                    integral &lt;= integral_limit_N;\n                end  else begin\n                    integral &lt;= add_0_ds;\n                end\n            end\n            integral &lt;= add_0_ds;\n        end\n        8'd36 : begin \/\/derivative := (error0 \u2212 error1) \/ T\t\/\/\u5fae\u5206\n            {add_sub0_da    ,   add_sub0_db} = {error0,error1};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b010;\n        end\n        8'd48 : begin \/\/ Kp \u00d7 error0 \n            derivative = add_sub0_ds;\n            {mult1_da    ,   mult1_db} = {Kp,error0};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b001;\n        end\n        8'd49 : begin\n            multd1 = mult1_ds;\n        end\n        8'd50 : begin \/\/ Ki \u00d7 integral\n            {mult1_da    ,   mult1_db} = {Ki,integral};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b001;\n        end\n        8'd62 : begin\n            multd2 = mult1_ds;\n        end\n        8'd63 : begin \/\/ Kd \u00d7 derivative\n            {mult1_da    ,   mult1_db} = {Kd,derivative};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b001;\n        end\n        8'd75 : begin\n            multd3 = mult1_ds;\n        end\n        8'd76 : begin \/\/ Kp \u00d7 error0 + Ki \u00d7 integral\n            {add_0_da    ,   add_0_db} = {multd1,multd2};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b100;\n        end\n        8'd88 : begin\n            add1 = add_0_ds;\n        end\n        8'd89 : begin \/\/ Kp \u00d7 error0 + Ki \u00d7 integral + Kd \u00d7 derivative\n            {add_0_da    ,   add_0_db} = {add1,multd3};\n            {add_0_start,add_sub0_start,mult1_start} = 3'b100;\n        end\n        8'd100 : begin\n            error1 = error0;\n            half_float_pid_do = add_0_ds;\n            pid_half_float_done = 1'b1;\n        end\n        \n    endcase\n    end\nend\n\n\n\nendmodule<\/code><\/pre>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\">PID\u7b97\u6cd5TB\u6d4b\u8bd5\u6587\u4ef6<\/h2>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>float_PID_tb.v<\/summary>\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>`timescale 1ns\/1ns\n\nmodule float_PID_tb;\n\nreg clk,rst;\n\nparameter CYCLE = 10;\n\n\n\/\/\u4ea7\u751f\u65f6\u949f\ninitial begin\n\tclk = 0;\n\tforever begin\n\t\t#(CYCLE\/2);\n\t\tclk = 1;\n\t\t#(CYCLE\/2);\n\t\tclk = 0;\n\tend\nend\nwire &#91;15:0] float_data_out,pid_do;\nreg &#91;15:0] ex_data;\nreg hex_valid_in = 1'b1;\n\/\/\u4f8b\u5316float_Kalman\nPID_Ctrl PID_Ctrl0(\n    .clk                (clk            ),\n    .rst_n              (rst            ),\n    .temp_hex           (pid_do         ),\n    .symbol             (1'b0           ),\n    .hex_in_valid       (pid_done   ),\n    .ex_hex             (ex_data      ),\/\/\u671f\u671b\u503c\n    .half_float_pid_do  (float_data_out),\n    .pid_do             (pid_do         ),\n    .pid_done           (pid_done )\n    \n);\nreg &#91;8:0] cnt;\nreg &#91;2:0]two;\nalways @(posedge pid_done or negedge rst)begin\n    if(!rst) begin\n        cnt = 'd0;\n        two = 'b0;\n        ex_data = 'd0;\n    end else begin\n        if(pid_done)begin\n            cnt = cnt + 1'b1;\n        end else begin\n            cnt = cnt;\n        end\n        if(cnt == 'd150) begin\n            cnt = 'd0;\n            two = two + 1'b1;\n            \n        end else begin\n            if(two == 'd0) ex_data = 'd1500;\n            if(two == 'd1) ex_data = 'd1000;\n            if(two == 'd2) ex_data = 'd1800;\n            if(two == 'd3) ex_data = 'd2000;\n            if(two == 'd4) ex_data = 'd1500;\n            if(two == 'd5) ex_data = 'd2000;\n            if(two == 'd6) ex_data = 'd1800;\n            if(two == 'd7)begin\n                $display($time,\"sim end!!!\");\n\t            $finish;\n            end      \n        end\n    end\nend\n\ninitial begin\n    rst = 0;\n\n    #CYCLE\n    rst = 1;\n\n\n    \/\/#(CYCLE*20000);\n\t\n\nend\n\nendmodule\n<\/code><\/pre>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\">\u4eff\u771f\u6548\u679c<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"542\" src=\"http:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745666-32e13c3b46b95455d50decdc529ea1e-1024x542.jpg\" alt=\"\" class=\"wp-image-471\" srcset=\"https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745666-32e13c3b46b95455d50decdc529ea1e-1024x542.jpg 1024w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745666-32e13c3b46b95455d50decdc529ea1e-300x159.jpg 300w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745666-32e13c3b46b95455d50decdc529ea1e-768x406.jpg 768w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745666-32e13c3b46b95455d50decdc529ea1e.jpg 1376w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"552\" src=\"http:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745741-d4d98e3b2f8b68d75e9487abf677c83-1024x552.jpg\" alt=\"\" class=\"wp-image-472\" srcset=\"https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745741-d4d98e3b2f8b68d75e9487abf677c83-1024x552.jpg 1024w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745741-d4d98e3b2f8b68d75e9487abf677c83-300x162.jpg 300w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745741-d4d98e3b2f8b68d75e9487abf677c83-768x414.jpg 768w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745741-d4d98e3b2f8b68d75e9487abf677c83.jpg 1336w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"620\" src=\"http:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745757-8520a62ee188be16dc97503b9f23692-1024x620.jpg\" alt=\"\" class=\"wp-image-473\" srcset=\"https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745757-8520a62ee188be16dc97503b9f23692-1024x620.jpg 1024w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745757-8520a62ee188be16dc97503b9f23692-300x182.jpg 300w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745757-8520a62ee188be16dc97503b9f23692-768x465.jpg 768w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745757-8520a62ee188be16dc97503b9f23692-1536x930.jpg 1536w, https:\/\/blog.huanmie.fun\/wp-content\/uploads\/2025\/03\/1742745757-8520a62ee188be16dc97503b9f23692.jpg 1638w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7b97\u6cd5\u7b80\u4ecb\uff1a &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PID \u7b97\u6cd5\u5373\uff1a\u6bd4\u4f8b  &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-452","post","type-post","status-publish","format-standard","hentry","category-fpga"],"_links":{"self":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":20,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"predecessor-version":[{"id":478,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/posts\/452\/revisions\/478"}],"wp:attachment":[{"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.huanmie.fun\/index.php\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}