1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-05-17 04:26:00 +02:00
cdf2018-principal/fpga/Principal.vhd

341 lines
13 KiB
VHDL
Raw Normal View History

2018-02-07 17:57:01 +01:00
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
2018-02-07 17:57:01 +01:00
entity Principal is
2018-02-27 19:33:58 +01:00
Generic(
2018-05-11 15:58:18 +02:00
fFpga : INTEGER := 50_000_000
2018-02-27 19:33:58 +01:00
);
Port (
CLK : in std_logic;
BTN: in std_logic;
2018-05-11 15:58:18 +02:00
SDA: inout std_logic;
SCL: inout std_logic;
2018-02-27 19:33:58 +01:00
LEFTCHA: in std_logic;
LEFTCHB: in std_logic;
RIGHTCHA: in std_logic;
RIGHTCHB: in std_logic;
FRONTTRIGGER: out std_logic;
BACKTRIGGER: out std_logic;
2018-05-10 10:09:56 +02:00
FRONTLECHO: in std_logic;
BACKLECHO: in std_logic;
FRONTRECHO: in std_logic;
BACKRECHO: in std_logic;
2018-05-09 01:00:40 +02:00
ENAREF: out std_logic;
2018-05-01 08:45:02 +02:00
ENA: out std_logic;
2018-05-11 15:58:18 +02:00
IN1: out std_logic;
2018-05-01 08:45:02 +02:00
IN2: out std_logic;
2018-05-09 01:00:40 +02:00
ENBREF: out std_logic;
2018-05-01 08:45:02 +02:00
ENB: out std_logic;
2018-05-11 15:58:18 +02:00
IN3: out std_logic;
2018-05-01 08:45:02 +02:00
IN4: out std_logic
2018-02-27 19:33:58 +01:00
);
2018-02-07 17:57:01 +01:00
end Principal;
architecture Behavioral of Principal is
-- General
signal reset : std_logic := '0';
-- Encoder
signal left : integer;
signal right : integer;
2018-02-27 10:41:33 +01:00
component hedm is
Port (
clk : in STD_LOGIC;
chA : in STD_LOGIC;
chB : in STD_LOGIC;
reset : in STD_LOGIC;
zero : in STD_LOGIC;
counts : out integer
);
end component;
2018-02-28 12:06:11 +01:00
-- Distance sensors
2018-05-10 10:09:56 +02:00
signal frontMin : integer := 0;
signal backMin : integer := 0;
signal frontL : integer := 0;
signal frontLRaw : integer := 0;
signal frontLFinished : std_logic;
signal backL : integer := 0;
signal backLRaw : integer := 0;
signal backLFinished : std_logic;
signal frontR : integer := 0;
signal frontRRaw : integer := 0;
signal frontRFinished : std_logic;
signal backR : integer := 0;
signal backRRaw : integer := 0;
signal backRFinished : std_logic;
2018-02-27 19:33:58 +01:00
component hcsr04 IS
generic(
fFpga : INTEGER := fFpga
);
port(
clk : IN STD_LOGIC;
reset : IN STD_LOGIC;
echo : IN STD_LOGIC;
distance : OUT INTEGER;
trigger : OUT STD_LOGIC;
start : IN STD_LOGIC;
finished : OUT STD_LOGIC
);
end component;
2018-02-28 12:06:11 +01:00
component fir is
Port (
clock : in STD_LOGIC;
reset : in STD_LOGIC;
signalIn : in INTEGER;
signalOut : out INTEGER;
start : in STD_LOGIC;
done : out STD_LOGIC
);
end component;
2018-05-02 05:50:33 +02:00
-- PWM clock
signal pwmClk : std_logic := '0';
signal pwmCounter : integer := 0;
2018-05-06 12:50:03 +02:00
constant PWM_DIVIDER : integer := 4096;
2018-05-02 05:50:33 +02:00
2018-05-01 08:45:02 +02:00
-- Motor controller
signal enAd : std_logic_vector(7 downto 0);
signal enBd : std_logic_vector(7 downto 0);
2018-05-11 15:58:18 +02:00
signal ind : std_logic_vector(7 downto 0);
2018-05-01 08:45:02 +02:00
component PWM is
port (
clk : in std_logic;
data : in std_logic_vector (7 downto 0);
pulse : out std_logic
);
end component;
-- CF
2018-05-11 15:58:18 +02:00
component I2CSLAVE
generic(
DEVICE: std_logic_vector(7 downto 0) := x"42"
);
port(
MCLK : in std_logic;
nRST : in std_logic;
SDA_IN : in std_logic;
SCL_IN : in std_logic;
SDA_OUT : out std_logic;
SCL_OUT : out std_logic;
ADDRESS : out std_logic_vector(7 downto 0);
DATA_OUT : out std_logic_vector(7 downto 0);
DATA_IN : in std_logic_vector(7 downto 0);
WR : out std_logic;
RD : out std_logic
);
end component;
2018-05-11 15:58:18 +02:00
signal sdaIn : std_logic;
signal sclIn : std_logic;
signal sdaOut : std_logic;
signal sclOut : std_logic;
signal address : std_logic_vector(7 downto 0);
signal dataOut : std_logic_vector(7 downto 0);
signal dataIn : std_logic_vector(7 downto 0);
signal wr : std_logic;
signal rd : std_logic;
signal rdP : std_logic;
2018-05-11 15:58:18 +02:00
signal leftB : std_logic_vector(15 downto 0);
signal rightB : std_logic_vector(15 downto 0);
signal frontLRawB : std_logic_vector(15 downto 0);
signal frontRRawB : std_logic_vector(15 downto 0);
signal backLRawB : std_logic_vector(15 downto 0);
signal backRRawB : std_logic_vector(15 downto 0);
signal frontLB : std_logic_vector(15 downto 0);
signal frontRB : std_logic_vector(15 downto 0);
signal backLB : std_logic_vector(15 downto 0);
signal backRB : std_logic_vector(15 downto 0);
2018-02-07 17:57:01 +01:00
begin
reset <= BTN;
2018-05-02 05:50:33 +02:00
pwmClkGenerator: process (clk) begin
if rising_edge(clk) then
if (pwmCounter >= PWM_DIVIDER) then
pwmClk <= not pwmClk;
pwmCounter <= 0;
else
pwmCounter <= pwmCounter + 1;
end if;
end if;
end process;
2018-02-27 10:41:33 +01:00
leftCoder: hedm port map (
2018-02-27 19:33:58 +01:00
clk => CLK,
chA => LEFTCHA,
chB => LEFTCHB,
reset => reset,
2018-05-11 15:58:18 +02:00
zero => '0',
2018-02-27 19:33:58 +01:00
counts => left
);
2018-02-27 10:41:33 +01:00
rightCoder: hedm port map (
2018-02-27 19:33:58 +01:00
clk => CLK,
chA => RIGHTCHA,
chB => RIGHTCHB,
reset => reset,
2018-05-11 15:58:18 +02:00
zero => '0',
2018-02-27 19:33:58 +01:00
counts => right
);
2018-02-28 12:06:11 +01:00
2018-05-10 10:09:56 +02:00
frontLCapt: hcsr04 port map (
2018-05-11 15:58:18 +02:00
clk => CLK,
reset => reset,
echo => FRONTLECHO,
distance => frontLRaw,
trigger => FRONTTRIGGER,
start => '1',
finished => frontLFinished
);
frontLFilter : FIR port map (
clock => CLK,
reset => reset,
signalIn => frontLRaw,
signalOut => frontL,
start => frontLFinished
-- done => done
);
frontRCapt: hcsr04 port map (
clk => CLK,
reset => reset,
echo => FRONTRECHO,
distance => frontRRaw,
-- trigger => FRONTTRIGGER,
start => '1',
finished => frontRFinished
);
frontRFilter : FIR port map (
clock => CLK,
reset => reset,
signalIn => frontRRaw,
signalOut => frontR,
start => frontRFinished
-- done => done
);
backLCapt: hcsr04 port map (
2018-02-27 19:33:58 +01:00
clk => CLK,
reset => reset,
2018-05-11 15:58:18 +02:00
echo => BACKLECHO,
distance => backLRaw,
trigger => BACKTRIGGER,
2018-02-28 12:06:11 +01:00
start => '1',
2018-05-11 15:58:18 +02:00
finished => backLFinished
2018-02-28 12:06:11 +01:00
);
2018-05-11 15:58:18 +02:00
backLFilter : FIR port map (
2018-02-28 12:06:11 +01:00
clock => CLK,
reset => reset,
2018-05-11 15:58:18 +02:00
signalIn => backLRaw,
signalOut => backL,
start => backLFinished
2018-02-28 12:06:11 +01:00
-- done => done
2018-02-27 19:33:58 +01:00
);
2018-05-11 15:58:18 +02:00
backRCapt: hcsr04 port map (
2018-05-10 10:09:56 +02:00
clk => CLK,
reset => reset,
2018-05-11 15:58:18 +02:00
echo => BACKRECHO,
distance => backRRaw,
-- trigger => BACKTRIGGER,
2018-05-10 10:09:56 +02:00
start => '1',
2018-05-11 15:58:18 +02:00
finished => backRFinished
2018-05-10 10:09:56 +02:00
);
2018-05-11 15:58:18 +02:00
backRFilter : FIR port map (
2018-05-10 10:09:56 +02:00
clock => CLK,
reset => reset,
2018-05-11 15:58:18 +02:00
signalIn => backRRaw,
signalOut => backR,
start => backRFinished
2018-05-10 10:09:56 +02:00
-- done => done
);
2018-05-01 08:45:02 +02:00
enAp : PWM port map (
2018-05-02 05:50:33 +02:00
clk => pwmClk,
2018-05-01 08:45:02 +02:00
data => enAd,
pulse => ENA
);
2018-05-09 01:00:40 +02:00
ENAREF <= '1';
2018-05-01 08:45:02 +02:00
2018-05-11 15:58:18 +02:00
IN1 <= ind(0);
IN2 <= ind(1);
2018-05-01 08:45:02 +02:00
enBp : PWM port map (
2018-05-02 05:50:33 +02:00
clk => pwmClk,
2018-05-01 08:45:02 +02:00
data => enBd,
pulse => ENB
);
2018-05-09 01:00:40 +02:00
ENBREF <= '1';
2018-02-27 10:41:33 +01:00
2018-05-11 15:58:18 +02:00
IN3 <= ind(2);
IN4 <= ind(3);
FA : i2cslave port map (
MCLK => clk,
nRST => not reset,
SDA_IN => sdaIn,
SCL_IN => sclIn,
SDA_OUT => sdaOut,
SCL_OUT => sclOut,
ADDRESS => address,
DATA_OUT => dataOut,
DATA_IN => dataIn,
WR => wr,
RD => rd
2018-05-01 08:45:02 +02:00
);
2018-02-28 12:06:11 +01:00
2018-05-11 15:58:18 +02:00
SCL <= 'Z' when sclOut = '1' else '0';
sclIn <= to_UX01(SCL);
SDA <= 'Z' when sdaOut = '1' else '0';
sdaIn <= to_UX01(SDA);
2018-02-28 12:06:11 +01:00
2018-05-11 15:58:18 +02:00
leftB <= std_logic_vector(to_signed(left, 16));
rightB <= std_logic_vector(to_signed(right, 16));
frontLRawB <= std_logic_vector(to_unsigned(frontLRaw, 16));
frontRRawB <= std_logic_vector(to_unsigned(frontRRaw, 16));
backLRawB <= std_logic_vector(to_unsigned(backLRaw, 16));
backRRawB <= std_logic_vector(to_unsigned(backRRaw, 16));
frontLB <= std_logic_vector(to_unsigned(frontL, 16));
frontRB <= std_logic_vector(to_unsigned(frontR, 16));
backLB <= std_logic_vector(to_unsigned(backL, 16));
backRB <= std_logic_vector(to_unsigned(backR, 16));
dataIn <= x"50" when address = x"00" else
leftB(15 downto 8) when address = x"10" else
leftB(7 downto 0) when address = x"11" else
rightB(15 downto 8) when address = x"12" else
rightB(7 downto 0) when address = x"13" else
frontLRawB(15 downto 8) when address = x"20" else
frontLRawB(7 downto 0) when address = x"21" else
frontRRawB(15 downto 8) when address = x"22" else
frontRRawB(7 downto 0) when address = x"23" else
backLRawB(15 downto 8) when address = x"24" else
backLRawB(7 downto 0) when address = x"25" else
backRRawB(15 downto 8) when address = x"26" else
backRRawB(7 downto 0) when address = x"27" else
frontLB(15 downto 8) when address = x"30" else
frontLB(7 downto 0) when address = x"31" else
frontRB(15 downto 8) when address = x"32" else
frontRB(7 downto 0) when address = x"33" else
backLB(15 downto 8) when address = x"34" else
backLB(7 downto 0) when address = x"35" else
backRB(15 downto 8) when address = x"36" else
backRB(7 downto 0) when address = x"37" else
ind when address = x"60" else
enAd when address = x"61" else
enBd when address = x"62" else
(others => '0');
ind <= dataOut when (address = x"60" and wr = '1') else ind;
enAd <= dataOut when (address = x"61" and wr = '1') else enAd;
enBd <= dataOut when (address = x"62" and wr = '1') else enBd;
2018-05-10 10:09:56 +02:00
2018-02-07 17:57:01 +01:00
end Behavioral;