diff --git a/tasks/java-evaluation/Elevator.java b/tasks/java-evaluation/Elevator.java new file mode 100644 index 0000000..843dfb0 --- /dev/null +++ b/tasks/java-evaluation/Elevator.java @@ -0,0 +1,22 @@ +public class Elevator { + private int floor; + + public Elevator(int start) { + System.out.println("Elevator instantiated at floor " + start); + floor = start; + } + + public void moveUp() { + floor++; + System.out.println("Moving up... now at floor " + floor); + } + + public void moveDown() { + floor--; + System.out.println("Moving down... now at floor " + floor); + } + + public int getCurrentFloor() { + return floor; + } +} \ No newline at end of file diff --git a/tasks/java-evaluation/ElevatorController.java b/tasks/java-evaluation/ElevatorController.java new file mode 100644 index 0000000..2b43a20 --- /dev/null +++ b/tasks/java-evaluation/ElevatorController.java @@ -0,0 +1,26 @@ +public class ElevatorController { + private int minFloor; + private int maxFloor; + private Elevator elevator; + + public ElevatorController(Elevator e, int min, int max) { + elevator = e; + minFloor = min; + maxFloor = max; + } + + public void goToFloor(int floor) { + if (floor < minFloor || floor > maxFloor) { + System.out.println("Floor " + floor + " is not a valid floor"); + } else { + while (elevator.getCurrentFloor() != floor) { + if (elevator.getCurrentFloor() < floor) { + elevator.moveUp(); + } else { + elevator.moveDown(); + } + } + System.out.println("Arrived at floor " + floor); + } + } +} \ No newline at end of file diff --git a/tasks/java-evaluation/Main.java b/tasks/java-evaluation/Main.java new file mode 100644 index 0000000..913c4f5 --- /dev/null +++ b/tasks/java-evaluation/Main.java @@ -0,0 +1,12 @@ +public class Main { + public static void main(String[] args) { + Elevator elevator = new Elevator(1); + ElevatorController controller = new ElevatorController(elevator, 1, 5); + controller.goToFloor(3); + controller.goToFloor(2); + controller.goToFloor(0); + controller.goToFloor(6); + controller.goToFloor(5); + controller.goToFloor(1); + } +} \ No newline at end of file diff --git a/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java b/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java index 1500ca0..27d6304 100644 --- a/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java +++ b/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java @@ -22,6 +22,7 @@ public RobotContainer() { private void configureBindings() { // TODO: Bind indexForSeconds(1.5) to joystick button 1 + joystick.button(1).onTrue(indexer.indexForSeconds(1.5)); } public Command getAutonomousCommand() { diff --git a/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java b/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java index e89f0e5..9a5155f 100644 --- a/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java +++ b/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java @@ -5,21 +5,25 @@ package frc.robot.subsystems; import edu.wpi.first.wpilibj2.command.SubsystemBase; +import edu.wpi.first.wpilibj2.command.*; public class Indexer extends SubsystemBase { // TODO: Add indexing state variable here - + private boolean indexing; /** Creates a new Indexer subsystem. */ - public Indexer() {} - + public Indexer() { + indexing = false; + } /** Starts the indexing process. */ public void startIndexing() { // TODO: Set indexing state to true + indexing = true; } /** Stops the indexing process. */ public void stopIndexing() { // TODO: Set indexing state to false + indexing = false; } /** @@ -29,10 +33,14 @@ public void stopIndexing() { */ public boolean isIndexing() { // TODO: Return indexing state - return false; + return indexing; } // TODO: Implement indexForSeconds() command factory + public Command indexForSeconds(double seconds) { + // return Commands.runOnce(() -> startIndexing()).withTimeout(seconds).runOnce(() -> stopIndexing()); + return Commands.runOnce(() -> startIndexing()).andThen(Commands.waitSeconds(seconds)).andThen(()-> stopIndexing()); + } @Override public void periodic() {