2015-12-14 43 views
4

我希望能夠像枚舉數組列表一樣向枚舉變量狀態機添加個案。我通過創建一個數組列表並使用大小來告訴它有多少個案例,然後使用for循環執行它們。問題是我的方法調用完成之前會進入下一個案例。如何讓程序等待方法完成後再繼續,而不使用時間延遲,因爲時間可能因不同情況而異。這種情況和方法是在loop()方法的代碼底部。對狀態機使用數組列表

package com.qualcomm.ftcrobotcontroller.opmodes; 

import com.qualcomm.hardware.HiTechnicNxtCompassSensor; 
import com.qualcomm.robotcore.eventloop.opmode.OpMode; 
import com.qualcomm.robotcore.hardware.DcMotor; 
import com.qualcomm.robotcore.hardware.DcMotorController; 
import com.qualcomm.robotcore.util.ElapsedTime; 

import java.util.ArrayList; 

/** 
* Created by Robotics on 12/11/2015. 
*/ 
public class RobotAction extends OpMode { 

    //HiTechnicNxtCompassSensor compassSensor; 
    //double compassvalue = compassSensor.getDirection(); 
    double compassvalue; 
    double lastcompassvalue; 

    DcMotor rightdrive; 
    DcMotor leftdrive; 
    final double fullpower = 1; 

    double angle; 
    double distance; 

    final double wheelediameter = 4.0; 
    final double circumference = wheelediameter * Math.PI; 
    final double gearratio = 1; 
    final double countsperrotation = 1440; 


    double wheelrotations = distance/circumference; 
    double motorrotations = wheelrotations * gearratio; 
    double encodercounts = motorrotations * countsperrotation; 

    int currentpositionright; 

    int step; 

    ElapsedTime time; 

    String status; 
    String action; 

    public enum RelativePosition {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest} 
    RelativePosition relativeposition; 

    private void Action(double angle, int compassvalue, double encodercounts, RelativePosition relativeposition) { 

     action = "running"; 

     lastcompassvalue = compassvalue; 

     switch(relativeposition) { 


      case North: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (rightdrive.getCurrentPosition() < encodercounts) { 
        rightdrive.setTargetPosition((int) (encodercounts + 1)); 
        leftdrive.setTargetPosition((int) (encodercounts + 1)); 

        rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
        leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

        rightdrive.setDirection(DcMotor.Direction.REVERSE); 
        rightdrive.setPower(.5); 
        leftdrive.setPower(.5); 

       } 
       else { 
        rightdrive.setPower(0); 
        leftdrive.setPower(0); 
        rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
       } 
       break; 

      case NorthEast: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue > lastcompassvalue - (90-angle)) { 
        rightdrive.setDirection(DcMotor.Direction.FORWARD); 
        rightdrive.setPower(fullpower); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts + 1); 
         leftdrive.setTargetPosition((int) encodercounts + 1); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setDirection(DcMotor.Direction.REVERSE); 
         rightdrive.setPower(fullpower); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 

      case East: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue > lastcompassvalue - 90) { 
        rightdrive.setDirection(DcMotor.Direction.FORWARD); 
        rightdrive.setPower(fullpower); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts + 1); 
         leftdrive.setTargetPosition((int) encodercounts + 1); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setDirection(DcMotor.Direction.REVERSE); 
         rightdrive.setPower(fullpower); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 

      case SouthEast: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue > lastcompassvalue - (90+angle)) { 
        rightdrive.setDirection(DcMotor.Direction.FORWARD); 
        rightdrive.setPower(fullpower); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts); 
         leftdrive.setTargetPosition((int) encodercounts); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setDirection(DcMotor.Direction.REVERSE); 
         rightdrive.setPower(fullpower); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 

      case South: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue > lastcompassvalue - 180) { 
        rightdrive.setDirection(DcMotor.Direction.FORWARD); 
        rightdrive.setPower(fullpower); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts + 1); 
         leftdrive.setTargetPosition((int) encodercounts + 1); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setDirection(DcMotor.Direction.REVERSE); 
         rightdrive.setPower(fullpower); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 

      case SouthWest: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue < lastcompassvalue + (90+angle)) { 
        rightdrive.setPower(fullpower); 
        leftdrive.setDirection(DcMotor.Direction.REVERSE); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts + 1); 
         leftdrive.setTargetPosition((int) encodercounts + 1); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setPower(fullpower); 
         leftdrive.setDirection(DcMotor.Direction.FORWARD); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 

      case West: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue < lastcompassvalue + 90) { 
        rightdrive.setPower(fullpower); 
        leftdrive.setDirection(DcMotor.Direction.REVERSE); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts + 1); 
         leftdrive.setTargetPosition((int) encodercounts + 1); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setPower(fullpower); 
         leftdrive.setDirection(DcMotor.Direction.FORWARD); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 

      case NorthWest: 
       currentpositionright = rightdrive.getCurrentPosition(); 

       if (compassvalue < lastcompassvalue + (90-angle)) { 
        rightdrive.setPower(fullpower); 
        leftdrive.setDirection(DcMotor.Direction.REVERSE); 
        leftdrive.setPower(fullpower); 
       } 
       else { 
        if(rightdrive.getCurrentPosition() < encodercounts) { 
         rightdrive.setTargetPosition((int) encodercounts + 1); 
         leftdrive.setTargetPosition((int) encodercounts + 1); 

         rightdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION); 

         rightdrive.setPower(fullpower); 
         leftdrive.setDirection(DcMotor.Direction.FORWARD); 
         leftdrive.setPower(fullpower); 
        } 
        else { 
         rightdrive.setPower(0); 
         leftdrive.setPower(0); 
         rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
         leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
        } 
       } 

       break; 
     } 
     action = "done"; 
    } 

    private final ArrayList<Integer> Step = new ArrayList<>(); 

    @Override 
    public void init() { 

     rightdrive = hardwareMap.dcMotor.get("right_drive"); 
     leftdrive = hardwareMap.dcMotor.get("left_drive"); 

     rightdrive.setDirection(DcMotor.Direction.REVERSE); 

     leftdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 
     rightdrive.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS); 


     step = 1; 
     Step.add(step); 
     //compassSensor = (HiTechnicNxtCompassSensor) hardwareMap.compassSensor.get("compass"); 
     compassvalue = 0; 

     relativeposition = RelativePosition.North; 

     distance = 12; 

     angle = 60; 



    } 

    public void start() { 
     status = "Running"; 

     time = new ElapsedTime(); 
     time.reset(); 

    } 

    @Override 
    public void loop() { 

     telemetry.addData("Status", status); 
     double currenttime = time.time(); 

     int size = Step.size(); 
     for (int i=0; i<size; i++) { 
      int element = Step.get(i); 

      switch (element) { 
       case 1: 

        Action(angle, (int) compassvalue,encodercounts,relativeposition); 

        step++; 
        Step.add(step); 

        break; 
       case 2: 

        rightdrive.setPower(0); 
        leftdrive.setPower(0); 

        status = "Done"; 

      } 
     } 


    } 
} 
+1

儘量只保留相關的代碼;那裏有很多可以取出的代碼。使讀起來更容易! – RafaelC

+0

我的歉意我試圖解決這個問題,我自己嘗試了幾個不同的事情。我可能在調試時留下了一些內容。 –

回答

1

,我想到的第一件事情,一個天真的解決方案是使用一個共同的全球布爾作爲一個標誌來檢查方法仍在運行。當然,這是基於我的假設,一次只有一個方法可以等待,因爲根據你的問題,你不想運行另一種方法,直到第一個方法完成執行。

編輯:

設置一個布爾值,真當程序運行時,開始你的方法執行之前。

然後每次方法開始執行時,將其設置爲false,然後在從方法返回之前將其設置爲true。在發出方法調用的循環中,只需在調用另一個方法之前檢查該變量即可。

+0

所以基本上有一個布爾值在函數的開頭設置爲true,然後在最後設置爲false?我對Java編程相當陌生,這是我第一個廣泛的程序,這只是整件事情的一部分。 –

+0

@ 5108AmishElectrians編輯進行更詳細的解釋。希望有所幫助。 –

+0

今天我會嘗試一下,並報告它是如何工作的。 –