我想使用P控制器(稍后使用PID)控制电机。这是我用于 P 控制器的 VHDL 代码:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
USE ieee.numeric_std.ALL;
USE ieee.STD_LOGIC_ARITH.ALL;
USE ieee.std_logic_signed.ALL;
entity P is
Port ( e : in STD_LOGIC_VECTOR (8 downto 0);
PWM : out STD_LOGIC_VECTOR (8 downto 0));
end P;
architecture Behavioral of P is
signal eInt : integer := 0;
signal PWMInt : integer := 0;
--min/max
signal borne : integer := 255;
--Gain
signal Ku : integer := 1;
--saturation
component saturation is
Port ( entier : in integer;
borne : in integer;
sotie : out STD_LOGIC_VECTOR (8 downto 0));
end component;
begin
eInt <= conv_integer(e);
PWMInt <= Ku*eInt;
sat : saturation port map(PWMInt, borne, PWM);
end Behavioral;
其中饱和将PWM的数量限制为"borne"(因此PWM在二进制中最多为255),e是误差(命令 - 测量)。
该块的输出是直接连接到电机的PWM(这是在另一个文件中完成的)。开环工作得很好,闭环很糟糕。我强烈怀疑我处理整数的方式有问题,因为测试平台工作正常,但物理实现根本不正确。
只要您使用所有这些算术库,就几乎不可能知道发生了什么。
最佳方法:摆脱std_logic_signed
、std_logic_arith
,并使端口signed
或unsigned
numeric_std。 conv_integer
被numeric_std库中的to_integer
所取代。
如果无法更改端口,请在整个内部使用(例如)signed
,并且仅在输入/输出端口上从/到std_logic_vector
转换。
我想你是在模拟这个循环吗?