4

I wanted to be able to add cases to a enum variable state machine like an array list. I did so by creating an array list and using the size to tell it how many cases there are and execute them accordingly with a for loop. The problem is it will progress into the next case before my method call is complete. How can i make the program wait for the method to complete before continuing without using a time delay, as the time may vary for different situations. The case and method are towards the bottom of the code in the loop() method.

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
    Try to keep only the relevant code; There is lots of code there that can be taken out. Makes it easier to read! – rafaelc Dec 14 '15 at 04:17
  • My apologies I was trying a couple different things trying to solve this issue on my own. I may have left some things in there as I have been debugging. – 5108 AmishElectricians Dec 14 '15 at 04:48

1 Answers1

1

The first thing that I think of, a naive solution is to use a common global boolean as a flag to check if the method is still running. Of course this is based on my assumption that at a time, you'll have only one method that you will be waiting on, since according to your question, you don't want to run another method until the first one is done with its execution.

Edit:

Set a boolean to true when the program runs, before starting your method executions.

Then everytime a method starts executing, set it to false, and then before returning from your method, set it back to true. In your loop that issues method calls, just check that variable before calling another method.

Abhishek Patel
  • 774
  • 5
  • 19