Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library ieee;
- use ieee.std_logic_1164.all;
- use ieee.numeric_std.all;
- entity LIFT is
- port(
- iOUT : in std_logic_vector(3 downto 0) := (others => '0'); --outside input
- iIN : in std_logic_vector(3 downto 0) := (others => '0'); --inside elevator car input
- DIR : in std_logic; --FPGA button to switch for outside input (if you either want to go up/down)
- -- 0 down, 1 up
- floor : out std_logic_vector(6 downto 0) := "1111110";
- an : out std_logic_vector(3 downto 0) := "1110"; --output lcd
- DOOR : out std_logic := '1'; --output door
- WEIGHT : in std_logic;
- DOOR_SENSOR : in std_logic := '0';
- CLK : in std_logic);
- end LIFT;
- architecture behavioral of LIFT is
- type lift_states is (initial, waiting, check, openDoor, closeDoor, move); --states that the elevator has
- signal curr_state : lift_states; -- := initial;
- signal next_state : lift_states;
- signal INTERIOR : std_logic_vector (13 downto 0) := (others => '0'); -- interior commands
- signal UP : std_logic_vector (13 downto 0) := (others => '0'); -- exterior commands for going up
- signal DOWN : std_logic_vector (13 downto 0) := (others => '0'); -- exterior commands for going down
- constant empty : std_logic_vector (13 downto 0) := (others => '0'); -- empty vector used for comparison
- constant iempty : std_logic_vector (3 downto 0) := (others => '0'); -- empty vector used for input comparison
- signal press : natural; --It's 1 for ext. commands and 2 for interior commands and 0 for no commands
- signal position : integer := 0; --lift's floor
- signal direction : std_logic := '1'; --direction, up = 1 / down = 0;
- signal GOu, GOd : std_logic; -- checks if there are commands lower/higher relative to position
- signal i, k : natural; -- used to determine commands relative to position
- signal counter : natural;
- signal freq : std_logic := '0';
- begin
- proccy : process(CLK)
- begin
- if (rising_edge(CLK)) then
- curr_state <= next_state;
- end if;
- end process proccy;
- commands : process(freq)
- begin
- if (rising_edge(freq)) then
- if (iIN /= iempty) then
- press <= 2;
- end if;
- if (iOUT /= iempty) then
- press <= 1;
- end if;
- if (press = 1) then
- if (DIR = '1') then
- UP(to_integer(unsigned(iOUT))) <= '1';
- press <= 0;
- end if;
- if (DIR = '0') then
- DOWN(to_integer(unsigned(iOUT))) <= '1';
- press <= 0;
- end if;
- elsif (press = 2) then
- INTERIOR(to_integer(unsigned(iIN))) <= '1';
- press <= 0;
- end if;
- if (position /= to_integer(unsigned(iOUT)) and position /= to_integer(unsigned(iIN))) then
- UP(position) <= '0';
- DOWN(position) <= '0';
- INTERIOR(position) <= '0';
- end if;
- end if;
- end process commands;
- states_lift : process(DOOR_SENSOR, DOWN, GOd, GOu, INTERIOR, UP, WEIGHT, counter, curr_state, direction, i, next_state, position)
- begin
- case curr_state is
- when initial => position <= 0; --initialize everything
- -- +door sensor, weight sensor;
- next_state <= closeDoor;
- --waiting for stuff
- when waiting => if (UP /= empty or DOWN /= empty or INTERIOR /= empty) then
- next_state <= check;
- end if;
- --rough w8ing process
- when check => GOu <= '0';
- GOd <= '0';
- if (WEIGHT /= '1') then
- if (UP /= empty) then
- next_state <= move;
- end if;
- if (DOWN /= empty) then
- next_state <= move;
- end if;
- if (INTERIOR /= empty) then
- next_state <= move;
- end if;
- end if;
- if (UP(position) = '1' or DOWN(position) = '1' or INTERIOR(position) = '1') then
- next_state <= openDoor;
- end if;
- --the checking state
- when move => if (direction = '1') then
- if (i < 14 and GOu /= '1' and GOd /= '1') then
- if (i > position) then
- if (UP(i) = '1' or INTERIOR(i) = '1') then
- GOu <= '1';
- else
- i <= i + 1;
- end if;
- else
- if (UP(i) = '1' or DOWN(i) = '1' or INTERIOR(i) = '1') then
- GOd <= '1';
- else
- i <= i + 1;
- end if;
- end if;
- else
- if (GOu = '1') then
- if (UP(position) = '1' or INTERIOR(position) = '1') then
- next_state <= OpenDoor;
- else
- if (next_state /= OpenDoor) then
- position <= position + 1;
- end if;
- end if;
- else
- if (GOd = '1') then
- if (UP(position) = '1' or INTERIOR(position) = '1' or DOWN(position) = '1') then
- next_state <= OpenDoor;
- else
- if (next_state /= OpenDoor) then
- position <= position - 1;
- end if;
- end if;
- else
- direction <= '0';
- i <= 13;
- end if;
- end if;
- end if;
- else
- if (i > 0 and GOd /= '1' and GOu /= '1') then
- if (i < position) then
- if (DOWN(i) = '1' or INTERIOR(i) = '1') then
- GOd <= '1';
- else
- i <= i - 1;
- end if;
- else
- if (DOWN(i) = '1' or INTERIOR(i) = '1') then
- GOu <= '1';
- else
- i <= i - 1;
- end if;
- end if;
- else
- if (GOd = '1') then
- if (DOWN(position) = '1' or INTERIOR(position) = '1') then
- next_state <= OpenDoor;
- else
- if (next_state /= OpenDoor) then
- position <= position - 1;
- end if;
- end if;
- else
- if (GOu = '1') then
- if (DOWN(position) = '1' or INTERIOR(position) = '1') then
- next_state <= OpenDoor;
- else
- if (next_state /= OpenDoor) then
- position <= position + 1;
- end if;
- end if;
- else
- direction <= '1';
- end if;
- end if;
- end if;
- end if;
- when openDoor => --opens door, checks sensor for weight, waits a period of time
- DOOR <= '1';
- if (counter = 3) then
- next_state <= closeDoor;
- else
- counter <= counter + 1;
- end if;
- when closeDoor => --closes door
- counter <= 0;
- if (door_sensor = '0') then
- DOOR <= '0';
- next_state <= waiting;
- else
- next_state <= openDoor;
- end if;
- end case;
- end process states_lift;
- display : process(position)
- begin
- case position is
- when 1 => floor <= "1111110";
- when 2 => floor <= "0110000";
- when 3 => floor <= "1101101";
- when 4 => floor <= "1111001";
- when 5 => floor <= "0110011";
- when 6 => floor <= "1011011";
- when 7 => floor <= "1011111";
- when 8 => floor <= "1110000";
- when 9 => floor <= "1111111";
- when 10 => floor <= "1111011";
- when 11 => floor <= "1110111"; --a
- when 12 => floor <= "0011111"; --b
- when 13 => floor <= "1001110"; --c
- when others => floor <= "1111111";
- end case;
- end process display;
- frequency : process(freq, k)
- begin
- if (k = 200) then
- k <= 0;
- freq <= not freq;
- else
- k <= k + 1;
- end if;
- end process frequency;
- end architecture behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement