2017-01-22 130 views
-2

我正在嘗試開發一種自動駕駛汽車。我有一個傳感器來計算我們汽車車輪的圈數。當圈數達到一個特定的輸入數字時,它應該改變狀態,但if語句似乎沒有工作。不是比較這兩個數字,而是輸入else語句,直到輸入vueltas全部爲「1」。如果我更改了代碼,並寫入if(vueltas < 15),它就可以工作。但我需要的數字是可以改變的。這是代碼,我希望程序保持相同的狀態,直到這個輸入數字的圈數爲止。我已經證明輸入的數字是正確的,並且是15。elsif(obst ='1')是在汽車檢測到障礙物的情況下,但這與這個問題無關。VHDL - if <語句不起作用

注:vueltas在西班牙

  library IEEE; 
    use IEEE.STD_LOGIC_1164.ALL; 
    use IEEE.NUMERIC_STD.ALL; 
    --use IEEE.STD_LOGIC_ARITH.ALL; 
    use IEEE.STD_LOGIC_UNSIGNED.ALL; 

    entity Circuito is Port (
     clk : in STD_LOGIC; 
     ir1 : in STD_LOGIC; 
     ir2 : in STD_LOGIC; 
     moverCoche : in STD_LOGIC; 
     angulo : in STD_LOGIC_VECTOR(4 downto 0); 
     vMaxCurva : in STD_LOGIC_VECTOR(4 downto 0); 
     posInicial : in STD_LOGIC_VECTOR(9 downto 0); 
     vueltasCurva : in STD_LOGIC_VECTOR(9 downto 0); 
     vueltasRecta : in STD_LOGIC_VECTOR(9 downto 0); 
     obst : in STD_LOGIC; 
     servoOut : out STD_LOGIC; 
     motorOut : out STD_LOGIC; 
     vueltasLed : out STD_LOGIC_VECTOR(9 downto 0); 
     vueltasDentroDeCircuito : out STD_LOGIC_VECTOR(11 downto 0); 
     revolucionesPorSeg : out STD_LOGIC_VECTOR(11 downto 0)); 
    end Circuito; 

    architecture Behavioral of Circuito is 

     component motor_pwm_clk32kHz is Port (
      clk  : in STD_LOGIC; 
      entrada : in STD_LOGIC_VECTOR(4 downto 0); 
      salida : out STD_LOGIC); 
     end component; 

     component servo_pwm_clk32kHz is Port (
      clk : in STD_LOGIC; 
      pos : in STD_LOGIC_VECTOR(4 downto 0); 
      servo : out STD_LOGIC); 
     end component; 

     component Contador_Vueltas is Port ( 
      out1 : in STD_LOGIC; --Negro: 1 Blanco: 0 
      out2 : in STD_LOGIC; --Negro: 1 Blanco: 0 
      vueltas : out STD_LOGIC_VECTOR (9 downto 0); 
      rst : in STD_LOGIC; 
      clk : in STD_LOGIC); 
     end component; 

     component revoluciones is Port (
      clk : in STD_LOGIC; 
      vueltasDentroDeCircuito : in STD_LOGIC_VECTOR(11 downto 0); 
      revoluciones : out STD_LOGIC_VECTOR(11 downto 0)); 
     end component; 

     signal posServo, posMotor: STD_LOGIC_VECTOR(4 downto 0); 

     signal vueltas : STD_LOGIC_VECTOR(9 downto 0); 
     signal primeraVuelta : STD_LOGIC := '1'; 
     signal sigReiniciarVueltas : STD_LOGIC; 
     signal sigVueltasDentroDeCircuito : STD_LOGIC_VECTOR(11 downto 0); 
     signal sigVueltasInicioEstado : STD_LOGIC_VECTOR(11 downto 0); 
     --signal sigVueltasRecta : unsigned := to_integer(unsigned(vueltasRecta)); 
     --constant sigVueltasRecta : STD_LOGIC_VECTOR(9 downto 0) := "0000011110"; 
     --constant sigVueltasCurva : STD_LOGIC_VECTOR(9 downto 0) := "0000011110"; 
     signal flag : STD_LOGIC := '0'; 

     signal Qt: STD_LOGIC_VECTOR(3 downto 0); 
     SUBTYPE STATE_TYPE IS STD_LOGIC_VECTOR(3 downto 0); 
     SIGNAL STATE: STATE_TYPE; 
     CONSTANT s0 : STATE_TYPE := "0000"; 
     CONSTANT s1 : STATE_TYPE := "0001"; 
     CONSTANT s2 : STATE_TYPE := "0010"; 
     CONSTANT s3 : STATE_TYPE := "0011"; 
     CONSTANT s4 : STATE_TYPE := "0100"; 
     CONSTANT s5 : STATE_TYPE := "0101"; 
     CONSTANT s6 : STATE_TYPE := "0110"; 
     CONSTANT s7 : STATE_TYPE := "0111"; 
     CONSTANT s8 : STATE_TYPE := "1000"; 

    begin 

     UUT_Motor: motor_pwm_clk32kHz Port Map (
      clk => clk, 
      entrada => posMotor, 
      salida => motorOut); 

     UUT_Servo: servo_pwm_clk32kHz Port Map (
      clk => clk, 
      pos => posServo, 
      servo => servoOut); 

     UUT_ContadorVueltas: Contador_Vueltas Port Map (
      clk => clk, 
      rst => sigReiniciarVueltas, 
      vueltas => vueltas, 
      out1 => ir1, 
      out2 => ir2); 

     UUT_Revoluciones: revoluciones Port Map(
      clk => clk, 
      vueltasDentroDeCircuito => sigVueltasDentroDeCircuito, 
      revoluciones => revolucionesPorSeg 
     ); 

     process(clk, moverCoche) 
     begin 
      if (moverCoche = '0') then 
       Qt <= s0; 
       sigReiniciarVueltas <= '1'; 
       sigVueltasDentroDeCircuito <= (others => '0'); 
       posServo <= "10000"; 
       posMotor <= "10000"; 
      elsif (clk'event and clk = '1') then 
       case Qt is 
       --Quieto 
       when s0 => 
        posServo <= "10000"; 
        posMotor <= "10000"; 
        sigReiniciarVueltas <= '0'; 
        Qt <= s1; 
       --Recta1 
       when s1 => 
        sigReiniciarVueltas <= '0'; 
        posServo <= "10000"; 
        posMotor <= vMaxCurva; --Min: 10011 
        sigVueltasDentroDeCircuito <= ("00" & vueltas); 
        if (unsigned(vueltas) >= unsigned(vueltasRecta)) then 
         Qt <= s2; 
         sigReiniciarVueltas <= '1'; 
        elsif (obst = '1') then 
         Qt <= s8; 
        else 
    --     sigVueltasRecta <= vueltasRecta;   
         Qt <= s1;     
        end if; 
       -- Curva1 
       when s2 => 
        sigReiniciarVueltas <= '0'; 
        posServo <= angulo; 
        posMotor <= vMaxCurva; 
        sigVueltasDentroDeCircuito <= posInicial + ("00" & vueltas); 
        if (unsigned(vueltas) >= unsigned(vueltasCurva)) then 
         sigReiniciarVueltas <= '1'; 
         Qt <= s3; 
        elsif (obst = '1') then 
         Qt <= s8; 
        else 
         Qt <= s2; 
        end if; 
       --Recta2 
       when s3 => 
        sigReiniciarVueltas <= '0'; 
        posServo <= "10000"; 
        posMotor <= vMaxCurva; --Min: 10011 
        sigVueltasDentroDeCircuito <= posInicial + vueltasCurva + ("00" & vueltas); 
        if (unsigned(vueltas) >= unsigned(vueltasRecta)) then 
         sigReiniciarVueltas <= '1'; 
         Qt <= s4; 
        elsif (obst = '1') then 
         Qt <= s8; 
        else 
         Qt <= s3; 
        end if; 
       --Curva2 
       when s4 => 
        sigReiniciarVueltas <= '0'; 
        posServo <= angulo; 
        posMotor <= vMaxCurva; 
        sigVueltasDentroDeCircuito <= posInicial + vueltasCurva + vueltasRecta + ("00" & vueltas); 
        if (unsigned(vueltas) >= unsigned(vueltasCurva)) then 
         sigVueltasDentroDeCircuito <= (others => '0'); 
         sigReiniciarVueltas <= '1'; 
         Qt <= s4; 
        elsif (obst = '1') then 
         Qt <= s8; 
        else 
         Qt <= s1; 
        end if; 

       --Mantener Quieto 
       when s5 => 
        posMotor <= "10000"; 
        Qt <= s5; 

       when others => 
        if(obst = '1') then 
         posMotor <= "00000"; 
         --sigReiniciarVueltas <= '0'; 
         Qt <= s8; 
        else 
         Qt <= s1; 
        end if; 
       end case; 
       vueltasDentroDeCircuito <= sigVueltasDentroDeCircuito; 
       vueltasLed <= vueltasRecta; 
      end if; 
     end process; 
    end Behavioral; 
+2

請參閱[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。你的問題不允許你的讀者重現你的問題。 – user1155120

回答

0

=圈,雖然有可能在std_logic_vector執行算術比較(在這種情況下,<),它可能不是最好的做法,因爲它是未知是否基礎值是有符號或無符號的。如果您需要進行任何算術或比較,請使用numeric_std包中的unsignedsigned類型。

發現一個很好的討論here

+0

它仍然不起作用。它仍然是這樣。我已經更改了代碼,是否還有其他更改可以執行? – ainhoarru

+0

@ainhoarru這不是事實。雖然你通常不能用'std_logic_vector'進行算術運算,你可以用'''和類似的運算符來比較'std_logic_vector'。如果載體的長度不同,你應該注意 - 你會得到奇怪的結果。 –

+1

@MatthewTaylor我編輯了我的答案以反映您的評論。雖然你說對SLV進行算術比較是正確的,但我不會認爲這是一個很好的設計練習 – gsm