Advertisement
Guest User

Untitled

a guest
May 17th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity LIFT is
  6.     port(
  7.         iOUT        : in  std_logic_vector(3 downto 0) := (others => '0'); --outside input
  8.         iIN         : in  std_logic_vector(3 downto 0) := (others => '0'); --inside elevator car input
  9.         DIR         : in  std_logic;    --FPGA button to switch for outside input (if you either want to go up/down)
  10.         -- 0 down, 1 up
  11.  
  12.         floor       : out std_logic_vector(6 downto 0) := "1111110";
  13.         an          : out std_logic_vector(3 downto 0) := "1110"; --output lcd 
  14.         DOOR        : out std_logic                    := '1'; --output door
  15.         WEIGHT      : in  std_logic;
  16.         DOOR_SENSOR : in  std_logic                    := '0';
  17.         CLK         : in  std_logic);
  18. end LIFT;
  19.  
  20. architecture behavioral of LIFT is
  21.  
  22.     type lift_states is (initial, waiting, check, openDoor, closeDoor, move); --states that the elevator has
  23.    
  24.     signal curr_state : lift_states;    -- := initial;
  25.     signal next_state : lift_states;
  26.  
  27.     signal INTERIOR : std_logic_vector (13 downto 0) := (others => '0'); -- interior commands
  28.     signal UP       : std_logic_vector (13 downto 0) := (others => '0'); -- exterior commands for going up
  29.     signal DOWN     : std_logic_vector (13 downto 0) := (others => '0'); -- exterior commands for going down
  30.    
  31.     constant empty    : std_logic_vector (13 downto 0) := (others => '0'); -- empty vector used for comparison 
  32.     constant iempty : std_logic_vector (3 downto 0) := (others => '0'); -- empty vector used for input comparison
  33.  
  34.     signal press : natural;             --It's 1 for ext. commands and 2 for interior commands and 0 for no commands
  35.  
  36.     signal position  : integer   := 0;  --lift's floor
  37.     signal direction : std_logic := '1'; --direction, up = 1 / down = 0;         
  38.  
  39.     signal GOu, GOd : std_logic;        -- checks if there are commands lower/higher relative to position    
  40.  
  41.     signal i, k : natural;              -- used to determine commands relative to position
  42.  
  43.     signal counter : natural;
  44.  
  45.     signal freq : std_logic := '0';
  46.  
  47. begin
  48.  
  49.     proccy : process(CLK)
  50.     begin
  51.         if (rising_edge(CLK)) then
  52.             curr_state <= next_state;
  53.         end if;
  54.     end process proccy;
  55.  
  56.     commands : process(freq)
  57.     begin
  58.         if (rising_edge(freq)) then
  59.  
  60.             if (iIN /= iempty) then
  61.                 press <= 2;
  62.             end if;
  63.  
  64.             if (iOUT /= iempty) then
  65.                 press <= 1;
  66.             end if;
  67.  
  68.             if (press = 1) then
  69.                 if (DIR = '1') then
  70.                     UP(to_integer(unsigned(iOUT))) <= '1';
  71.                     press                          <= 0;
  72.                 end if;
  73.  
  74.                 if (DIR = '0') then
  75.                     DOWN(to_integer(unsigned(iOUT))) <= '1';
  76.                     press                            <= 0;
  77.                 end if;
  78.             elsif (press = 2) then
  79.                 INTERIOR(to_integer(unsigned(iIN))) <= '1';
  80.                 press                               <= 0;
  81.             end if;
  82.  
  83.             if (position /= to_integer(unsigned(iOUT)) and position /= to_integer(unsigned(iIN))) then
  84.                 UP(position)       <= '0';
  85.                 DOWN(position)     <= '0';
  86.                 INTERIOR(position) <= '0';
  87.             end if;
  88.         end if;
  89.     end process commands;
  90.  
  91.     states_lift : process(DOOR_SENSOR, DOWN, GOd, GOu, INTERIOR, UP, WEIGHT, counter, curr_state, direction, i, next_state, position)
  92.     begin
  93.         case curr_state is
  94.  
  95.             when initial => position <= 0; --initialize everything
  96.                 -- +door sensor, weight sensor;
  97.  
  98.                 next_state <= closeDoor;
  99.             --waiting for stuff
  100.  
  101.             when waiting => if (UP /= empty or DOWN /= empty or INTERIOR /= empty) then
  102.                     next_state <= check;
  103.                 end if;
  104.  
  105.             --rough w8ing process
  106.  
  107.             when check => GOu <= '0';
  108.                 GOd <= '0';
  109.                 if (WEIGHT /= '1') then
  110.                     if (UP /= empty) then
  111.                         next_state <= move;
  112.                     end if;
  113.  
  114.                     if (DOWN /= empty) then
  115.                         next_state <= move;
  116.                     end if;
  117.  
  118.                     if (INTERIOR /= empty) then
  119.                         next_state <= move;
  120.                     end if;
  121.                 end if;
  122.                 if (UP(position) = '1' or DOWN(position) = '1' or INTERIOR(position) = '1') then
  123.                     next_state <= openDoor;
  124.                 end if;
  125.  
  126.             --the checking state
  127.  
  128.             when move => if (direction = '1') then
  129.                     if (i < 14 and GOu /= '1' and GOd /= '1') then
  130.                         if (i > position) then
  131.                             if (UP(i) = '1' or INTERIOR(i) = '1') then
  132.                                 GOu <= '1';
  133.                             else
  134.                                 i <= i + 1;
  135.                             end if;
  136.                         else
  137.                             if (UP(i) = '1' or DOWN(i) = '1' or INTERIOR(i) = '1') then
  138.                                 GOd <= '1';
  139.                             else
  140.                                 i <= i + 1;
  141.                             end if;
  142.                         end if;
  143.                     else
  144.                         if (GOu = '1') then
  145.  
  146.                             if (UP(position) = '1' or INTERIOR(position) = '1') then
  147.                                 next_state <= OpenDoor;
  148.                             else
  149.                                 if (next_state /= OpenDoor) then
  150.                                     position <= position + 1;
  151.                                 end if;
  152.                             end if;
  153.  
  154.                         else
  155.                             if (GOd = '1') then
  156.                                 if (UP(position) = '1' or INTERIOR(position) = '1' or DOWN(position) = '1') then
  157.                                     next_state <= OpenDoor;
  158.                                 else
  159.                                     if (next_state /= OpenDoor) then
  160.                                         position <= position - 1;
  161.                                     end if;
  162.                                 end if;
  163.                             else
  164.                                 direction <= '0';
  165.                                 i         <= 13;
  166.                             end if;
  167.                         end if;
  168.  
  169.                     end if;
  170.  
  171.                 else
  172.  
  173.                     if (i > 0 and GOd /= '1' and GOu /= '1') then
  174.                         if (i < position) then
  175.                             if (DOWN(i) = '1' or INTERIOR(i) = '1') then
  176.                                 GOd <= '1';
  177.                             else
  178.                                 i <= i - 1;
  179.                             end if;
  180.                         else
  181.                             if (DOWN(i) = '1' or INTERIOR(i) = '1') then
  182.                                 GOu <= '1';
  183.                             else
  184.                                 i <= i - 1;
  185.                             end if;
  186.                         end if;
  187.  
  188.                     else
  189.  
  190.                         if (GOd = '1') then
  191.  
  192.                             if (DOWN(position) = '1' or INTERIOR(position) = '1') then
  193.                                 next_state <= OpenDoor;
  194.                             else
  195.                                 if (next_state /= OpenDoor) then
  196.                                     position <= position - 1;
  197.                                 end if;
  198.                             end if;
  199.  
  200.                         else
  201.                             if (GOu = '1') then
  202.                                 if (DOWN(position) = '1' or INTERIOR(position) = '1') then
  203.                                     next_state <= OpenDoor;
  204.                                 else
  205.                                     if (next_state /= OpenDoor) then
  206.                                         position <= position + 1;
  207.                                     end if;
  208.                                 end if;
  209.                             else
  210.                                 direction <= '1';
  211.                             end if;
  212.                         end if;
  213.  
  214.                     end if;
  215.                 end if;
  216.  
  217.             when openDoor =>            --opens door, checks sensor for weight, waits a period of time
  218.  
  219.                 DOOR <= '1';
  220.                 if (counter = 3) then
  221.                     next_state <= closeDoor;
  222.                 else
  223.                     counter <= counter + 1;
  224.                 end if;
  225.  
  226.             when closeDoor =>           --closes door
  227.  
  228.                 counter <= 0;
  229.                 if (door_sensor = '0') then
  230.                     DOOR       <= '0';
  231.                     next_state <= waiting;
  232.                 else
  233.                     next_state <= openDoor;
  234.                 end if;
  235.  
  236.         end case;
  237.  
  238.     end process states_lift;
  239.  
  240.     display : process(position)
  241.     begin
  242.         case position is
  243.             when 1      => floor <= "1111110";
  244.             when 2      => floor <= "0110000";
  245.             when 3      => floor <= "1101101";
  246.             when 4      => floor <= "1111001";
  247.             when 5      => floor <= "0110011";
  248.             when 6      => floor <= "1011011";
  249.             when 7      => floor <= "1011111";
  250.             when 8      => floor <= "1110000";
  251.             when 9      => floor <= "1111111";
  252.             when 10     => floor <= "1111011";
  253.             when 11     => floor <= "1110111"; --a
  254.             when 12     => floor <= "0011111"; --b
  255.             when 13     => floor <= "1001110"; --c
  256.             when others => floor <= "1111111";
  257.         end case;
  258.     end process display;
  259.  
  260.     frequency : process(freq, k)
  261.     begin
  262.         if (k = 200) then
  263.             k    <= 0;
  264.             freq <= not freq;
  265.         else
  266.             k <= k + 1;
  267.         end if;
  268.     end process frequency;
  269.  
  270. end architecture behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement