基于Verilog的PID控制算法

HuanMie 发布于 2025-03-24 532 次阅读


算法简介:

        PID 算法即:比例 - 积分 - 微分算法,是自动控制领域广泛应用的经典控制算法,在物联网恒温水暖控制系统中发挥关键作用。它依据设定值与实际测量值的偏差进行控制,由比例(P)、积分(I)、微分(D)三个环节构成。

        采用实验法或理论计算法对 PID 参数进行整定,通过对系统进行阶跃响应测试,获取系统的临界增益和临界周期,然后根据经验公式计算出 PID 参数的初始值,再通过反复试验调整参数,使系统达到最佳控制效果。

        连续控制系统的理想PID控制规律为:

        式中,Kp——比例增益,Kp与比例度成倒数关系;

        Tt——积分时间常数;

        TD——微分时间常数;

        u(t)——PID控制器的输出信号;

        e(t)——给定值r(t)与测量值之差。

设计思路:

根据PID算法伪代码可将计算流程拆分成如下阶段:

1. 计算本次偏差

  • setpoint:期望的系统输出值。
  • measured_value:系统当前的实际输出值。
  • error0:当前时刻的偏差。

2. 积分项计算与限幅

  • integral:积分项
  • integral_limit:积分限幅,用于防止积分项无限制地增长,避免出现积分饱和现象。

3. 微分项计算

  • derivative:微分项。
  • T:采样时间间隔。

 4.PID 输出计算

  • out:PID 控制器的输出。
  • Kp:比例系数,用于放大当前偏差的影响。
  • Ki:积分系数,用于放大积分项的影响。
  • Kd:微分系数,用于放大微分项的影响。

loop:
error0 := setpoint − measured_value //本次偏差
integral := integral + error0 //积分
if(integral > integral_limit) integral = integral_limit //积分限幅
else if (integral < -integral_limit) integral = -integral_limit
derivative := (error0 − error1) / T //微分
out := Kp × error0 + Ki × integral + Kd × derivative
//
//记录偏差
error1 := error0
wait(T) //间隔T运行

PID算法 RTL设计

PID_Ctrl.v

//基于Verilog的PID控制器模块
//  位置式离散PID公式 
//  out=Kp*error(t)+Ki*Sum_error(t)+Kd*[error(t)-error(t-1)]
//  */
//  error1 := 0		//上次偏差
//  integral := 0	//积分和
//  integral_limit :=100 //积分限幅
//  out := 0		//输出
//  //setpoint 设定值为入口参数
//  //循环
//  //采样周期为T
//  //measured_value 采样得到值


module PID_Ctrl(
    input clk                   ,    // 50MHz主时钟
    input rst_n                 ,    // 异步复位
    input [15:0] temp_hex       ,    // 温度值
    input symbol                ,    // 温度符号
    input hex_in_valid          ,    // 输入数据有效
    input [15:0] ex_hex         ,    // 期望值
    output [15:0] pid_do        ,   // PID输出 
    output reg [15:0] half_float_pid_do, // 半精度浮点数输出
    output  pid_done                 // PID计算完成
    
);

assign rst = ~rst_n;
reg pid_half_float_done;
wire [15:0] fixed_to_float_out,ex_fix_to_float;
wire [15:0] str_to_float;


const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse0(
    .realstr_in({320'b0,"0.01"}),
    .float_out (str_to_float)
);
//积分限幅
wire [15:0] integral_limit_P;
const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse1(
    .realstr_in({320'b0,"100"}),
    .float_out (integral_limit_P )
);
wire [15:0] integral_limit_N;
const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse2(
    .realstr_in({320'b0,"-100"}),
    .float_out (integral_limit_N )
);
wire [15:0] Kp,Ki,Kd;
const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse3(
    .realstr_in({320'b0,"0.5"}),
    .float_out (Kp )
);
const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse4(
     .realstr_in({320'b0,"0.2"}),
    .float_out (Ki )
);
const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse5(
    .realstr_in({320'b0,"0.05"}),
    .float_out (Kd )
);

wire [15:0] float_zero;
const_realstr_to_float_tbuse #(
    .float_type     ("half_float")

) const_realstr_to_float_tbuse6(
    .realstr_in({320'b0,"0"}),
    .float_out (float_zero )
);
//输入格式位xxx.xx
fixed_to_float #(
    .int_width      (16     ),
    .decimal_width  (0      ),
    .float_type     ("half_float")
)fixed_to_float_0(
    .float_out(fixed_to_float_out       ),
    .valid_out(fixed_to_float_out_done  ),
    .fixed_in (temp_hex                  ),
    .valid_in (hex_in_valid             ),
    .clk      (clk                      ),
    .rst      (rst                      )
);
//浮点数乘法
wire [15:0] hex_to_float;
half_float_optimize_multiplier half_float_optimize_multiplier0(
    .clk            (clk                                    ),
    .rst            (rst                                    ),
    .float_ina      (str_to_float                           ),
    .float_inb      (fixed_to_float_out                     ),
    .valid_in       (fixed_to_float_out_done                ),
    .float_result   (hex_to_float                           ),
    .valid_out      (hex_to_float_done                      )
);

wire [15:0] pid_float_do_tmp;
wire [1:0] compare_result_zero;
//assign pid_float_do_tmp = (compare_result_zero == 2'b00) ? float_zero : half_float_pid_do;
assign pid_float_do_tmp = half_float_pid_do;

//浮点数乘法
wire [15:0] float_x100;
half_float_optimize_multiplier half_float_optimize_multiplierx100(
    .clk            (clk                                    ),
    .rst            (rst                                    ),
    .float_ina      (integral_limit_P                       ),
    .float_inb      (pid_float_do_tmp                      ),
    .valid_in       (pid_half_float_done                   ),
    .float_result   (float_x100                            ),
    .valid_out      (float_x100_done                      )
);

//浮点转定点
float_to_fixed #(
        .int_width(16),
        .decimal_width(0),
        .float_type("half_float")
        ) FLOAT_TO_FIXED
        (
        .fixed_out  (pid_do              ),
        .fixed_nan  (),
        .valid_out  (pid_done            ),
        .float_in   (float_x100          ),
        .valid_in   (float_x100_done     ),
        .clk        (clk                  ),
        .rst        (rst                  )
);

//浮点数转定点 ex_hex
fixed_to_float #(
    .int_width      (16     ),
    .decimal_width  (0      ),
    .float_type     ("half_float")
)fixed_to_float_1(
    .float_out(ex_fix_to_float       ),
    .valid_out(ex_fix_to_float_done  ),
    .fixed_in (ex_hex                ),
    .valid_in (hex_in_valid            ),
    .clk      (clk                      ),
    .rst      (rst                      )
);
wire [15:0] ex_hex_to_float;
//浮点数乘法
half_float_optimize_multiplier half_float_optimize_multiplier2(
    .clk            (clk                                    ),
    .rst            (rst                                    ),
    .float_ina      (str_to_float                           ),
    .float_inb      (ex_fix_to_float                        ),
    .valid_in       (ex_fix_to_float_done                   ),
    .float_result   (ex_hex_to_float                        ),
    .valid_out      (ex_hex_to_float_done                   )
);

//浮点数加法
reg [15:0] add_0_da,add_0_db;
wire[15:0] add_0_ds;
wire add_0_done;
reg add_0_start;
float_adder #(
    .float_type("half_float")
) float_adder0(
    .clk         (clk               ),
    .rst         (rst               ),
    .float_ina   (add_0_da          ),
    .float_inb   (add_0_db          ),
    .valid_in    (add_0_start       ),
    .float_result(add_0_ds          ),
    .valid_out   (add_0_done        )
);

//浮点数乘法
reg [15:0] mult1_da,mult1_db;
reg mult1_start;
wire[15:0] mult1_ds;
wire mult1_done;
half_float_optimize_multiplier half_float_optimize_multiplier1(
    .clk            (clk            ),
    .rst            (rst            ),
    .float_ina      (mult1_da       ),
    .float_inb      (mult1_db       ),
    .valid_in       (mult1_start    ),
    .float_result   (mult1_ds       ),
    .valid_out      (mult1_done     )

);

//浮点数减法
reg [15:0] add_sub0_da,add_sub0_db;
wire[15:0] add_sub0_ds;
wire add_sub0_done;
reg add_sub0_start;
float_subtracter #(
    .float_type("half_float")
) float_subtracter0(
    .clk         (clk            ),
    .rst         (rst            ),
    .float_ina   (add_sub0_da    ),
    .float_inb   (add_sub0_db    ),
    .valid_in    (add_sub0_start ),
    .float_result(add_sub0_ds    ),
    .valid_out   (add_sub0_done  )
);
//浮点数比大小
//2'b00:float_ina < float_inb
//2'b01:float_ina == float_inb
//2'b10:float_ina > float_inb
//2'b11:无法比较(两者至少包含一个NAN)
wire [1:0] compare_result_N,compare_result_P;
float_compare #(
    .exponent_width(5),
    .mantissa_width(10)
    ) FLOAT_CMP
    (
    .compare_result(compare_result_P),
    .float_ina(add_0_ds),
    .float_inb(integral_limit_P)
    );
wire [1:0] compare_result;
float_compare #(
    .exponent_width(5),
    .mantissa_width(10)
    ) FLOAT_CMP1
    (
    .compare_result(compare_result_N),
    .float_ina(add_0_ds),
    .float_inb(integral_limit_N)
    );

//浮点数比大小 判断浮点数是否大于0

float_compare #(
    .exponent_width(5),
    .mantissa_width(10)
    ) FLOAT_CMP2
    (
    .compare_result(compare_result_zero),
    .float_ina(half_float_pid_do),
    .float_inb(float_zero)
    );

//伪代码
//  loop: 
//      error0 := setpoint − measured_value	//本次偏差
//      integral := integral + error0	//积分
//      if(integral > integral_limit) integral = integral_limit	//积分限幅
//      else if (integral < -integral_limit) integral = -integral_limit
//      derivative := (error0 − error1) / T	//微分
//      out := Kp × error0 + Ki × integral + Kd × derivative
//  
//      //记录偏差
//      error1 := error0
//      wait(T)	//间隔T运行
//      goto loop
//integral = add_sub0_ds

reg [15:0] integral;
reg [15:0] derivative;
reg [15:0] error1,error0;
reg [15:0] multd1,multd2,multd3;
reg [15:0] add1;

reg [7:0]Count;//序列机基数
always@(posedge clk or negedge rst_n)
if(!rst_n)
	Count <= 1'd0;
else if(Count == 8'd101)
	Count <= 1'd0;
else 
	Count <= Count + 1'd1;

always @(posedge clk or negedge rst_n) begin
    if(!rst_n)begin
        error1 <= 16'd0;
        integral <= 16'd0;
        pid_half_float_done <= 1'b0;
  
        derivative <= 16'd0;
    end else begin
        case(Count)
        8'd1 : begin //error0 := setpoint − measured_value	//本次偏差
            pid_half_float_done = 1'b0;
            {add_sub0_da    ,   add_sub0_db} = {ex_hex_to_float,hex_to_float};
            {add_0_start,add_sub0_start,mult1_start} = 3'b010;

        end
        8'd12 : begin //integral := integral + error0	//积分
            error0 = add_sub0_ds;
            {add_0_da    ,   add_0_db} = {error0,integral};
            {add_0_start,add_sub0_start,mult1_start} = 3'b100;
        end
        8'd24 : begin //if(integral > integral_limit) integral = integral_limit	//积分限幅
            if(compare_result_P == 2'b10)begin
                integral <= integral_limit_P;
            end else begin
                
                if(compare_result_N == 2'b00)begin
                    integral <= integral_limit_N;
                end  else begin
                    integral <= add_0_ds;
                end
            end
            integral <= add_0_ds;
        end
        8'd36 : begin //derivative := (error0 − error1) / T	//微分
            {add_sub0_da    ,   add_sub0_db} = {error0,error1};
            {add_0_start,add_sub0_start,mult1_start} = 3'b010;
        end
        8'd48 : begin // Kp × error0 
            derivative = add_sub0_ds;
            {mult1_da    ,   mult1_db} = {Kp,error0};
            {add_0_start,add_sub0_start,mult1_start} = 3'b001;
        end
        8'd49 : begin
            multd1 = mult1_ds;
        end
        8'd50 : begin // Ki × integral
            {mult1_da    ,   mult1_db} = {Ki,integral};
            {add_0_start,add_sub0_start,mult1_start} = 3'b001;
        end
        8'd62 : begin
            multd2 = mult1_ds;
        end
        8'd63 : begin // Kd × derivative
            {mult1_da    ,   mult1_db} = {Kd,derivative};
            {add_0_start,add_sub0_start,mult1_start} = 3'b001;
        end
        8'd75 : begin
            multd3 = mult1_ds;
        end
        8'd76 : begin // Kp × error0 + Ki × integral
            {add_0_da    ,   add_0_db} = {multd1,multd2};
            {add_0_start,add_sub0_start,mult1_start} = 3'b100;
        end
        8'd88 : begin
            add1 = add_0_ds;
        end
        8'd89 : begin // Kp × error0 + Ki × integral + Kd × derivative
            {add_0_da    ,   add_0_db} = {add1,multd3};
            {add_0_start,add_sub0_start,mult1_start} = 3'b100;
        end
        8'd100 : begin
            error1 = error0;
            half_float_pid_do = add_0_ds;
            pid_half_float_done = 1'b1;
        end
        
    endcase
    end
end



endmodule

PID算法TB测试文件

float_PID_tb.v

`timescale 1ns/1ns

module float_PID_tb;

reg clk,rst;

parameter CYCLE = 10;


//产生时钟
initial begin
	clk = 0;
	forever begin
		#(CYCLE/2);
		clk = 1;
		#(CYCLE/2);
		clk = 0;
	end
end
wire [15:0] float_data_out,pid_do;
reg [15:0] ex_data;
reg hex_valid_in = 1'b1;
//例化float_Kalman
PID_Ctrl PID_Ctrl0(
    .clk                (clk            ),
    .rst_n              (rst            ),
    .temp_hex           (pid_do         ),
    .symbol             (1'b0           ),
    .hex_in_valid       (pid_done   ),
    .ex_hex             (ex_data      ),//期望值
    .half_float_pid_do  (float_data_out),
    .pid_do             (pid_do         ),
    .pid_done           (pid_done )
    
);
reg [8:0] cnt;
reg [2:0]two;
always @(posedge pid_done or negedge rst)begin
    if(!rst) begin
        cnt = 'd0;
        two = 'b0;
        ex_data = 'd0;
    end else begin
        if(pid_done)begin
            cnt = cnt + 1'b1;
        end else begin
            cnt = cnt;
        end
        if(cnt == 'd150) begin
            cnt = 'd0;
            two = two + 1'b1;
            
        end else begin
            if(two == 'd0) ex_data = 'd1500;
            if(two == 'd1) ex_data = 'd1000;
            if(two == 'd2) ex_data = 'd1800;
            if(two == 'd3) ex_data = 'd2000;
            if(two == 'd4) ex_data = 'd1500;
            if(two == 'd5) ex_data = 'd2000;
            if(two == 'd6) ex_data = 'd1800;
            if(two == 'd7)begin
                $display($time,"sim end!!!");
	            $finish;
            end      
        end
    end
end

initial begin
    rst = 0;

    #CYCLE
    rst = 1;


    //#(CYCLE*20000);
	

end

endmodule

仿真效果

此作者没有提供个人介绍
最后更新于 2025-03-24