From 2dba680fb1431fb96db44a64a06c9fb9c475deed Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Sun, 17 Dec 2017 03:18:25 -0500 Subject: [PATCH 01/16] Implemented state and stateMachine classes --- .../montclairrobotics/alloy/auto/State.java | 22 +++ .../alloy/auto/StateMachine.java | 128 +++++++++++++++++- .../montclairrobotics/alloy/vector/Polar.java | 2 +- 3 files changed, 144 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/montclairrobotics/alloy/auto/State.java b/src/main/java/org/montclairrobotics/alloy/auto/State.java index ebb5b45..4352f44 100644 --- a/src/main/java/org/montclairrobotics/alloy/auto/State.java +++ b/src/main/java/org/montclairrobotics/alloy/auto/State.java @@ -15,6 +15,8 @@ public abstract class State { Integer nextState = null; + String debug = "Running State: "; + String description = "None"; /** * The start method is the first thing called when the state is run @@ -37,10 +39,30 @@ public abstract class State { */ public abstract boolean isDone(); + /** + * In order to have a non linear state machine, the state machine must know what state + * to go to when it is done with the previous state. + * The state also sometimes needs to know the current state for example if it just wanted + * to increment the state by one. + * @param currentState the state the state machine is currently running + * @return the state the state machine should go to + */ public int getNextState(int currentState){ if(nextState != null){ return nextState; } return currentState + 1; } + + /** + * When a state machine is running, it will debug out information about + * the state it is running. It will debug the result of debugInfo + * + * @param currentState the current state so that it can be used in the debug + * @return debug information about the state + */ + public String debugInfo(int currentState){ + return debug + currentState + "\n Description" + description; + } + } diff --git a/src/main/java/org/montclairrobotics/alloy/auto/StateMachine.java b/src/main/java/org/montclairrobotics/alloy/auto/StateMachine.java index 6f9cd4d..7af9b1b 100644 --- a/src/main/java/org/montclairrobotics/alloy/auto/StateMachine.java +++ b/src/main/java/org/montclairrobotics/alloy/auto/StateMachine.java @@ -1,9 +1,12 @@ package org.montclairrobotics.alloy.auto; +import com.qualcomm.robotcore.util.ElapsedTime; import org.montclairrobotics.alloy.core.RobotCore; import org.montclairrobotics.alloy.ftc.FTCDebug; + import java.util.ArrayList; +import java.util.Arrays; /** * Created by MHS Robotics on 12/16/2017. @@ -15,15 +18,20 @@ * State machines can be used for controlling auto modes, * but can also be ran in teleop modes for pre coded instructions that make driving easier. * + * State machines are also states themselves so a state machine can run another state machine. + * This allows for the reuse of auto code * * @author Garrett Burroughs - * @since + * @since 0.1 + * @version 0.1 */ public class StateMachine extends State { + /** + * The states list keeps track of all of the states that will be run in the state machine + */ ArrayList states; - /** * Since state machines can run in a non linear fashion, the last state in the array * may not be the last state in the state machine, finalState keeps track of what signifies the end @@ -35,31 +43,137 @@ public class StateMachine extends State { */ Integer finalState; - public StateMachine(State ... states){ + /** + * The name of the state machine, this is purely for debugging purposes + */ + String name; + + /** + * This debug is used for outputting information about the state machine as it runs + */ + FTCDebug d; + + /** + * A boolean that keeps track of weather or not the current state has been run yet, + * this is used to determine it the start() method of the state should be run + */ + boolean stateStarted; + /** + * the time spent in the state that has most recently finished + */ + double timeInLastState; + + /** + * A timer object to keep track of the time in states. + */ + private ElapsedTime timer; + + /** + * Keeps track of weather or not the state machine has finished, + * True if it has + */ + boolean done = false; + + /** + * Keeps track of the current state + */ + int state; + + public StateMachine(String name, int finalState, State ... states){ + this.name = name; + this.states = new ArrayList<>(Arrays.asList(states)); + this.finalState = finalState; + d = new FTCDebug(RobotCore.getTelemetry()); + description = "A state machine"; } + public StateMachine(String name, String description, int finalState, State ... states){ + this(name, finalState, states); + super.description = description; + } + + + /** + * Read out that the state has started and reset the timer + */ @Override public void start() { - + d.log("Running", name); + timer.reset(); } + + /** + * The run method takes care of actually running the states + */ @Override public void run() { - + State currentState = states.get(state); + + //Check If the state has started, if it hasn't run the 'start()' method + if (!stateStarted) { + timer.reset(); + currentState.start(); + stateStarted = true; // State has been started + } + + currentState.run(); //run the state + + if(currentState.isDone()){ //check if the state has finished + timeInLastState = timer.nanoseconds(); // Update the last state time + currentState.stop(); // Run the stop() method on the state + if(currentState.getNextState(state) == finalState){ // Check if the state machine is done + done = true; // set the state machine as done + return; // return (exit) out of the run() method + } + if(currentState.getNextState(state) < states.size()) { // make sure there is a next state to go to + state = currentState.getNextState(state); // go to the next state + }else{ + d.log("ERROR", "STATE MACHINE OUT OF BOUNDS"); // Give the user an error if there is no next state to go to + done = true; // stop the state machine + return; // exit the run method to ensure nothing else runs + } + stateStarted = false; // The next state has not started yet. + } + d.msg(currentState.debugInfo(state)); // Debug info about the state } + + /** + * When the state machine is finished, read out it has finished + */ @Override public void stop() { - + d.log("Status", name + ", Finished "); } + /** + * determine if the state machine is done + * + * @return true if the state machine is done + */ @Override public boolean isDone() { - return false; + return done; } + /** + * Adds a state to the state machine. + * States can be added to a state machine but should not be added after the state machine + * + * @param state state that will be added to the state machine + */ public void addState(State state){ states.add(state); } + + /** + * Gets the time in the most recently completed state + * + * @return The time in last state + */ + public double getTimeInLastState(){ + return timeInLastState; + } } diff --git a/src/main/java/org/montclairrobotics/alloy/vector/Polar.java b/src/main/java/org/montclairrobotics/alloy/vector/Polar.java index 56f0a99..4d6989b 100644 --- a/src/main/java/org/montclairrobotics/alloy/vector/Polar.java +++ b/src/main/java/org/montclairrobotics/alloy/vector/Polar.java @@ -21,7 +21,7 @@ public class Polar implements Vector { public Polar(double magnitude, Angle angle) { this.magnitude = magnitude; - this.angle = angle; + this.angle = angle; } /** From 00ae340c3df9549d29921ad1b1ba0f4da929dadd Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Sun, 17 Dec 2017 03:36:46 -0500 Subject: [PATCH 02/16] Implemented alloy autonomous --- .../alloy/auto/AlloyAutonomous.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java b/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java index 89f62e4..cec0cab 100644 --- a/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java +++ b/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java @@ -1,6 +1,8 @@ package org.montclairrobotics.alloy.auto; import com.qualcomm.robotcore.eventloop.opmode.OpMode; +import com.qualcomm.robotcore.util.ElapsedTime; +import org.montclairrobotics.alloy.core.RobotCore; /** * Created by MHS Robotics on 12/5/2017. @@ -8,15 +10,61 @@ * @author Garrett Burroughs */ public abstract class AlloyAutonomous extends OpMode{ + /** + * Keeps track of when the auto mode is running (True after started and before finished) + */ + boolean running; + /** + * The actual auto mode that should be instantiated in setup + */ + public StateMachine auto; + /** + * A timer to keep track of time in the autoMode + */ + public ElapsedTime timer; + + + /** + * This is where the user should define all their code + * and where "Auto", should be instantiated + */ + public abstract void setup(); + + /** + * Runs when the play button is pressed + * Start will set up everything that the auto mode needs to run + */ + @Override + public void start(){ + auto.start(); // run the start method of the state machine + running = true; // the state machine has started running + timer.reset(); // reset the timer + } + + /** + * Init is called when the INIT button is pressed on the drivers station. + * The init method takes care of setting up global robot variables and running the user setup method + */ @Override public void init() { + new RobotCore(telemetry, hardwareMap, gamepad1, gamepad2); + setup(); } + /** + * the Loop method takes care of running the state machine + */ @Override public void loop() { - + if(running){ + auto.run(); // Run the state machine, which will in turn run the states + } + if(auto.isDone()){ // check if the state machine has finished (Last state achieved) + running = false; // stop the state machine + auto.stop(); // Finally stop the state machine + } } } From 4de9925032a69c2668dcc08bbe188a73a0766624 Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Sun, 17 Dec 2017 03:38:34 -0500 Subject: [PATCH 03/16] Added newState method in autostatmachine --- .../montclairrobotics/alloy/auto/AlloyAutonomous.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java b/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java index cec0cab..90c370d 100644 --- a/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java +++ b/src/main/java/org/montclairrobotics/alloy/auto/AlloyAutonomous.java @@ -67,4 +67,13 @@ public void loop() { auto.stop(); // Finally stop the state machine } } + + /** + * A wrapper around the add state method to allow the user to add states to the auto mode + * + * @param state the state to be added + */ + public void newState(State state){ + auto.addState(state); + } } From 6dd957ce8bfffc074bb95cdce6bb08d0a7aa3fb0 Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Fri, 26 Jan 2018 09:54:42 -0500 Subject: [PATCH 04/16] Update FTCDebug.java --- .../org/montclairrobotics/alloy/ftc/FTCDebug.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java b/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java index 47ed463..796993b 100644 --- a/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java +++ b/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java @@ -8,19 +8,19 @@ */ public class FTCDebug implements Debug { - Telemetry telemetry; + public static Telemetry telemetry; - public FTCDebug(Telemetry telemetry){ - this.telemetry = telemetry; + public FTCDebug(){ + this.telemetry = RobotCore.getTelemetry(); } @Override - public void log(String key, Object value) { - + public static void log(String key, Object value) { + telemetry.addData(key, object); } @Override - public void msg(Object value) { - + public static void msg(Object value) { + telemetry.addData("Debug", value); } } From 54874bb84e5c9b1f8819af42b91dce90bd5792e2 Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Fri, 26 Jan 2018 09:55:28 -0500 Subject: [PATCH 05/16] Created static init() method for Debug.java --- src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java b/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java index 796993b..cc03ed5 100644 --- a/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java +++ b/src/main/java/org/montclairrobotics/alloy/ftc/FTCDebug.java @@ -10,7 +10,7 @@ public class FTCDebug implements Debug { public static Telemetry telemetry; - public FTCDebug(){ + public static init(){ this.telemetry = RobotCore.getTelemetry(); } From 2a40fd5a0c5290cf44881939eaaccb104401564b Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Fri, 26 Jan 2018 09:56:47 -0500 Subject: [PATCH 06/16] Initialize FTCDebug --- src/main/java/org/montclairrobotics/alloy/core/Alloy.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/montclairrobotics/alloy/core/Alloy.java b/src/main/java/org/montclairrobotics/alloy/core/Alloy.java index 4c4a312..b90357d 100644 --- a/src/main/java/org/montclairrobotics/alloy/core/Alloy.java +++ b/src/main/java/org/montclairrobotics/alloy/core/Alloy.java @@ -36,6 +36,7 @@ public void init() { // Set Up the core robot components, This allows them to be accessed throughout the project new RobotCore(telemetry, hardwareMap, gamepad1, gamepad2); initialization(); + FTCDebug.init(); } @Override From ee4f1cd48fceda1fe4ecc17d945b1333344373bb Mon Sep 17 00:00:00 2001 From: Garrett Burroughs Date: Sat, 27 Jan 2018 00:04:56 -0500 Subject: [PATCH 07/16] Implemented base alloy autonomous --- .idea/misc.xml | 2 +- .../Exceptions/InvalidParameterException.java | 10 ++++++ .../alloy/auto/AlloyAutonomous.java | 21 ++++++++++- .../alloy/auto/SimpleAutonomous.java | 32 +++++++++++++++++ .../alloy/auto/States/Drive.java | 36 +++++++++++++++++++ .../alloy/auto/States/Turn.java | 36 +++++++++++++++++++ .../montclairrobotics/alloy/core/Alloy.java | 2 +- 7 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/montclairrobotics/alloy/Exceptions/InvalidParameterException.java create mode 100644 src/main/java/org/montclairrobotics/alloy/auto/SimpleAutonomous.java create mode 100644 src/main/java/org/montclairrobotics/alloy/auto/States/Drive.java create mode 100644 src/main/java/org/montclairrobotics/alloy/auto/States/Turn.java diff --git a/.idea/misc.xml b/.idea/misc.xml index cfcd7a9..5a8ced9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -2,7 +2,7 @@