Skip to content

Commit 5cb8329

Browse files
committed
initial commit
0 parents  commit 5cb8329

32 files changed

+1872
-0
lines changed

control/run.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
cd $(dirname $0)
4+
5+
mkdir -p wrk
6+
7+
ghdl_args="--std=08 --workdir=wrk"
8+
9+
ghdl -a $ghdl_args ./src/*
10+
ghdl -a $ghdl_args ./test/*
11+
12+
ghdl --elab-run $ghdl_args tb --wave=wave.ghw
13+

control/src/actuator.vhd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
library ieee;
2+
context ieee.ieee_std_context;
3+
4+
entity actuator is
5+
port (
6+
CLK: in std_logic;
7+
RST: in std_logic;
8+
EN: in std_logic;
9+
I: in std_logic_vector(7 downto 0);
10+
O: out std_logic_vector(7 downto 0)
11+
);
12+
end entity;
13+
14+
architecture arch of actuator is
15+
16+
begin
17+
18+
O <= I;
19+
20+
end arch;

control/src/hold.vhd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
library ieee;
2+
context ieee.ieee_std_context;
3+
4+
entity hold is
5+
port (
6+
CLK: in std_logic;
7+
RST: in std_logic;
8+
EN: in std_logic;
9+
I: in std_logic_vector(7 downto 0);
10+
O: out std_logic_vector(7 downto 0)
11+
);
12+
end entity;
13+
14+
architecture arch of hold is
15+
16+
begin
17+
18+
O <= I;
19+
20+
end arch;

control/src/pid.vhd

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
library ieee;
2+
context ieee.ieee_std_context;
3+
4+
entity pid is
5+
port ( CLK, RST, EN: in std_logic;
6+
R,I: in std_logic_vector(7 downto 0);
7+
O: out std_logic_vector(7 downto 0)
8+
);
9+
end entity;
10+
11+
architecture arch of pid is
12+
13+
type t_q is array (natural range 2 downto 0) of signed(7 downto 0);
14+
type t_e is array (natural range 2 downto 0) of signed(7 downto 0);
15+
type t_eq is array (natural range 2 downto 0) of signed(15 downto 0);
16+
type t_u is array (natural range 1 downto 0) of signed(18 downto 0);
17+
18+
constant q: t_q:=("00000100","00000010","00000010");
19+
signal e: t_e;
20+
signal eq: t_eq;
21+
signal u: t_u;
22+
23+
signal s0: signed(16 downto 0);
24+
signal s1: signed(17 downto 0);
25+
signal uraw: signed(19 downto 0);
26+
27+
begin
28+
29+
e(0) <= signed(R)-signed(I);
30+
31+
process(CLK) begin if rising_edge(CLK) then
32+
if rst then
33+
u(1) <= (others=>'0');
34+
e(2) <= (others=>'0');
35+
e(1) <= (others=>'0');
36+
elsif EN then
37+
u(1) <= u(0);
38+
e(2 downto 1) <= e(1 downto 0);
39+
end if;
40+
end if; end process;
41+
42+
ms: for i in 0 to 2 generate
43+
eq(i) <= e(i) * q(i);
44+
end generate;
45+
46+
s0 <= resize(eq(0),s0'length) + eq(1);
47+
s1 <= resize(s0,s1'length) + eq(2);
48+
49+
uraw <= resize(s1,uraw'length) + u(1);
50+
51+
u(0) <= uraw(18 downto 0) when (uraw(19) xnor uraw(18)) else
52+
(18 => '0', others=>'1') when (not uraw(19)) else
53+
(18 => '1', 0 => '1', others=>'0');
54+
55+
O <= std_logic_vector(u(0)(18 downto 11));
56+
57+
end arch;

control/src/plant.vhd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library ieee;
2+
context ieee.ieee_std_context;
3+
4+
entity plant is
5+
port ( CLK, RST, EN: in std_logic;
6+
I: in std_logic_vector(7 downto 0);
7+
O: out std_logic_vector(7 downto 0)
8+
);
9+
end entity;
10+
11+
architecture arch of plant is
12+
signal s: std_logic_vector(7 downto 0);
13+
begin
14+
15+
process(CLK) begin if rising_edge(CLK) then
16+
if RST then
17+
s <= (others=>'0');
18+
elsif EN then
19+
s <= std_logic_vector(signed(s)+1) when signed(I)>0 else std_logic_vector(signed(s)-1) when signed(I)<0 else s;
20+
end if;
21+
end if; end process;
22+
23+
O <= s;
24+
25+
end arch;

control/src/sys.vhd

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
library ieee;
2+
context ieee.ieee_std_context;
3+
4+
entity sys is
5+
port ( CLK, RST: in std_logic;
6+
R: in std_logic_vector(7 downto 0)
7+
);
8+
end entity;
9+
10+
---
11+
12+
library ieee;
13+
context ieee.ieee_std_context;
14+
15+
architecture arch of sys is
16+
signal i, u, o, p: std_logic_vector(7 downto 0);
17+
signal cnt_c, cnt_p: unsigned(7 downto 0);
18+
signal en_c, en_o, en_p, en_i: std_logic;
19+
begin
20+
21+
m_c: entity work.pid
22+
port map ( CLK, RST, en_c, R, i, u );
23+
24+
m_o: entity work.actuator
25+
port map ( CLK, RST, en_o, u, o );
26+
27+
m_p: entity work.plant
28+
port map ( CLK, RST, en_p, o, p );
29+
30+
m_i: entity work.hold
31+
port map ( CLK, RST, en_i, p, i );
32+
33+
p_clks: process(CLK) begin if rising_edge(CLK) then
34+
if RST then
35+
cnt_c <= (others=>'0');
36+
cnt_p <= (others=>'0');
37+
else
38+
cnt_c <= (others=>'0') when cnt_c?=9 else cnt_c+1;
39+
cnt_p <= (others=>'0') when cnt_c?=90 else cnt_p+1;
40+
end if;
41+
end if; end process;
42+
43+
en_c <= cnt_c?=1;
44+
en_o <= '1';
45+
en_p <= cnt_p?=1;
46+
en_i <= '1';
47+
48+
end arch;

control/test/tb.vhd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--http://ctms.engin.umich.edu/CTMS/index.php?example=MotorSpeed&section=ControlDigital
2+
--https://dsp.stackexchange.com/questions/14965/how-to-implement-a-filter-in-matlab
3+
--http://stackoverflow.com/questions/22236977/latex-math-in-github-wikis
4+
--
5+
6+
entity tb is
7+
end entity;
8+
9+
---
10+
11+
library ieee;
12+
context ieee.ieee_std_context;
13+
14+
architecture arch of tb is
15+
constant t: time:= 10 ns;
16+
signal clk, rst, done: std_logic:='0';
17+
signal r: std_logic_vector(7 downto 0):=(others=>'1');
18+
begin
19+
20+
p_clk: process begin
21+
clk <= '1'; wait for t/2;
22+
clk <= '0'; wait for t/2;
23+
if done then wait; end if;
24+
end process;
25+
26+
p_rst: process begin
27+
rst <= '1'; wait for 10*t;
28+
rst <= '0'; wait for 5 ms;
29+
if done then wait; end if;
30+
end process;
31+
32+
p_main: process begin
33+
r <= std_logic_vector(to_signed(0,8)); wait for 1 us;
34+
r <= std_logic_vector(to_signed(63,8)); wait for 1 ms;
35+
r <= std_logic_vector(to_signed(127,8)); wait for 1 ms;
36+
37+
assert false report "end of test" severity note;
38+
done <= '1';
39+
-- Wait forever; this will finish the simulation.
40+
wait;
41+
end process;
42+
43+
uut: entity work.sys port map ( clk, rst, r );
44+
45+
end arch;

doc/2020_03/img/control.pdf

16.3 KB
Binary file not shown.

doc/2020_03/img/cosim.pdf

74.3 KB
Binary file not shown.

doc/2020_03/img/cosim_zoom.pdf

74.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)