big refactor of command/commandbase/commandgroup. also add auton read from file.
authorLogan Howard <logan@oflogan.com>
Sat, 18 Apr 2015 04:40:25 +0000 (21:40 -0700)
committerLogan Howard <logan@oflogan.com>
Sat, 18 Apr 2015 04:40:25 +0000 (21:40 -0700)
21 files changed:
auton_times_and_speeds.conf [new file with mode: 0644]
src/org/usfirst/frc/team3501/robot/AutonData.java [new file with mode: 0644]
src/org/usfirst/frc/team3501/robot/Robot.java
src/org/usfirst/frc/team3501/robot/RobotMap.java
src/org/usfirst/frc/team3501/robot/autons/DriveOverStep.java
src/org/usfirst/frc/team3501/robot/autons/DrivePastStep.java
src/org/usfirst/frc/team3501/robot/autons/PickUpContainer.java
src/org/usfirst/frc/team3501/robot/commands/CloseClaw.java
src/org/usfirst/frc/team3501/robot/commands/Command.java [new file with mode: 0644]
src/org/usfirst/frc/team3501/robot/commands/CommandBase.java
src/org/usfirst/frc/team3501/robot/commands/CommandGroup.java [new file with mode: 0644]
src/org/usfirst/frc/team3501/robot/commands/DriveFor.java
src/org/usfirst/frc/team3501/robot/commands/DriveWithJoysticks.java
src/org/usfirst/frc/team3501/robot/commands/MoveArm.java
src/org/usfirst/frc/team3501/robot/commands/MoveArmFor.java
src/org/usfirst/frc/team3501/robot/commands/OpenClaw.java
src/org/usfirst/frc/team3501/robot/commands/TensionLeftWinch.java
src/org/usfirst/frc/team3501/robot/commands/TensionRightWinch.java
src/org/usfirst/frc/team3501/robot/commands/ToggleClaw.java
src/org/usfirst/frc/team3501/robot/commands/TurnOffCompressor.java
src/org/usfirst/frc/team3501/robot/commands/TurnOnCompressor.java

diff --git a/auton_times_and_speeds.conf b/auton_times_and_speeds.conf
new file mode 100644 (file)
index 0000000..f429f3f
--- /dev/null
@@ -0,0 +1,4 @@
+# format is: `speed time name`. speed can be negative for reverse motion.
+0.7 1.2 drive_over_step
+0.5 1.5 drive_past_step
+0.5 1.4 pickup_container
diff --git a/src/org/usfirst/frc/team3501/robot/AutonData.java b/src/org/usfirst/frc/team3501/robot/AutonData.java
new file mode 100644 (file)
index 0000000..a643b06
--- /dev/null
@@ -0,0 +1,98 @@
+package org.usfirst.frc.team3501.robot;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashMap;
+
+public class AutonData {
+
+    HashMap<String, Double> speeds;
+    HashMap<String, Double> times;
+
+    public AutonData() {
+        speeds = new HashMap<String, Double>();
+        times  = new HashMap<String, Double>();
+
+        populate();
+    }
+
+    public double getSpeed(String key) {
+        Double ret = speeds.get(key);
+
+        return (ret != null) ? ret : 0;
+    }
+
+    public double getTime(String key) {
+        Double ret = times.get(key);
+
+        return (ret != null) ? ret : 0;
+    }
+
+    public void update() {
+        speeds.clear();
+        times.clear();
+
+        populate();
+    }
+
+    private void populate() {
+        String file;
+
+        try {
+            file = readConfigFile();
+        } catch (IOException e) {
+            e.printStackTrace();
+            populateDefaults();
+            return;
+        }
+
+        try {
+            Arrays.stream(file.split("\n"))
+            .map(line -> line.split(" "))
+            .forEach((action) -> {
+                double speed = Double.parseDouble(action[0]);
+                double time  = Double.parseDouble(action[1]);
+                String name  = action[2];
+
+                speeds.put(name, speed);
+                times.put(name, time);
+            });
+        } catch (Exception e) {
+            e.printStackTrace();
+            populateDefaults();
+        }
+    }
+
+    private void populateDefaults() {
+        speeds.clear();
+        times.clear();
+
+        speeds.put("drive_over_step",  0.7);
+        speeds.put("drive_past_step",  0.5);
+        speeds.put("pickup_container", 0.5);
+
+        times.put("drive_over_step",  1.2);
+        times.put("drive_past_step",  1.5);
+        times.put("pickup_container", 1.4);
+    }
+
+    private String readConfigFile() throws IOException {
+        BufferedReader in = new BufferedReader(new FileReader(
+                "auton_times_and_speeds.conf"));
+
+        StringBuilder sb = new StringBuilder();
+
+        in.readLine(); // get rid of first line
+
+        String curLine;
+        while ((curLine = in.readLine()) != null)
+            sb.append(curLine + "\n");
+        String finalString = sb.toString();
+
+        in.close();
+
+        return finalString;
+    }
+}
index b99f059d9aaa77085ca6381d27fd609061d0d9fb..532d25415fb7b47fc56b7fd66d968eba63b58cb2 100644 (file)
@@ -22,8 +22,9 @@ public class Robot extends IterativeRobot {
 
        public static OI oi;
 
-       private SendableChooser autoChooser;
+       public static AutonData autonData;
 
+       private SendableChooser autonChooser;
     private Command autonomousCommand;
 
     public void robotInit() {
@@ -35,6 +36,8 @@ public class Robot extends IterativeRobot {
 
                pneumatics = new Pneumatics();
 
+               autonData = new AutonData();
+
                chooseAuto();
     }
 
@@ -45,7 +48,9 @@ public class Robot extends IterativeRobot {
     public void autonomousInit() {
         schedule(new TurnOnCompressor());
 
-        autonomousCommand = (Command) autoChooser.getSelected();
+        autonData.update();
+
+        autonomousCommand = (Command) autonChooser.getSelected();
         autonomousCommand.start();
     }
 
@@ -68,13 +73,13 @@ public class Robot extends IterativeRobot {
     }
 
     private void chooseAuto() {
-        autoChooser = new SendableChooser();
+        autonChooser = new SendableChooser();
 
-        autoChooser.addDefault("Pick up container", new ContainerOverStep());
-        autoChooser.addObject("Drive over step",    new DriveOverStep());
-        autoChooser.addObject("Drive past step",    new DrivePastStep());
+        autonChooser.addDefault("Pick up container", new ContainerOverStep());
+        autonChooser.addObject("Drive over step",    new DriveOverStep());
+        autonChooser.addObject("Drive past step",    new DrivePastStep());
 
-        SmartDashboard.putData("Auto Mode", autoChooser);
+        SmartDashboard.putData("Auto Mode", autonChooser);
     }
 
     private void schedule(Command c) {
index 2f2da695e89ff53a3bf1e9b99f15ec86c6461e6b..37dad27752feac1f4b7ba04e5bc6528a7aa95d5f 100644 (file)
@@ -25,9 +25,4 @@ public class RobotMap {
     public static final int CLAW_FORWARD_CHANNEL = 0, CLAW_REVERSE_CHANNEL = 1;
 
     public static final Value OPEN = Value.kForward, CLOSED = Value.kReverse;
-
-    // auton
-    public static final double OVER_STEP_TIME = 1.2, OVER_STEP_SPEED = 0.7,
-                               PAST_STEP_TIME = 1.5, PAST_STEP_SPEED = 0.5,
-                               PICKUP_TIME    = 1.4, PICKUP_SPEED    = 0.5;
 }
index f70b3d280d81aa5acc0b74e853b9ab58540b3118..224bc9ca5eac3c0a2a644cfbf4e0915c34c5dce6 100644 (file)
@@ -1,9 +1,8 @@
 package org.usfirst.frc.team3501.robot.autons;
 
-import org.usfirst.frc.team3501.robot.RobotMap;
-import org.usfirst.frc.team3501.robot.commands.CommandBase;
+import org.usfirst.frc.team3501.robot.commands.Command;
 
-public class DriveOverStep extends CommandBase {
+public class DriveOverStep extends Command {
 
     private double speed;
 
@@ -11,8 +10,8 @@ public class DriveOverStep extends CommandBase {
         super("DriveOverStep");
         requires(drivetrain);
 
-        setTimeout(RobotMap.OVER_STEP_TIME);
-        speed = RobotMap.OVER_STEP_SPEED;
+        setTimeout(autonData.getTime("drive_over_step"));
+        speed = autonData.getSpeed("drive_over_step");
     }
 
     // TODO: this is an ugly "solution"
index d3105f849a0eab994acf68d5682116a8baac07b1..f45975064b74726adaf3c195bd902648e1c54b0d 100644 (file)
@@ -1,9 +1,8 @@
 package org.usfirst.frc.team3501.robot.autons;
 
-import org.usfirst.frc.team3501.robot.RobotMap;
-import org.usfirst.frc.team3501.robot.commands.CommandBase;
+import org.usfirst.frc.team3501.robot.commands.Command;
 
-public class DrivePastStep extends CommandBase {
+public class DrivePastStep extends Command {
 
     private double speed;
 
@@ -11,8 +10,8 @@ public class DrivePastStep extends CommandBase {
         super("DrivePastStep");
         requires(drivetrain);
 
-        setTimeout(RobotMap.PAST_STEP_TIME);
-        this.speed = RobotMap.PAST_STEP_SPEED;
+        setTimeout(autonData.getTime("drive_past_step"));
+        speed = autonData.getSpeed("drive_past_step");
     }
 
     protected void execute() {
index b930f124f29cd954783657d395022fc7a2ee5674..52fb2b4479dfb464c9155de948b3e4525058b3aa 100644 (file)
@@ -1,11 +1,8 @@
 package org.usfirst.frc.team3501.robot.autons;
 
 import org.usfirst.frc.team3501.robot.Robot;
-import org.usfirst.frc.team3501.robot.RobotMap;
 import org.usfirst.frc.team3501.robot.commands.*;
 
-import edu.wpi.first.wpilibj.command.CommandGroup;
-
 public class PickUpContainer extends CommandGroup {
 
     public PickUpContainer() {
@@ -19,6 +16,8 @@ public class PickUpContainer extends CommandGroup {
 
     private void queueCommands() {
         addSequential(new CloseClaw());
-        addSequential(new MoveArmFor(RobotMap.PICKUP_TIME, RobotMap.PICKUP_SPEED));
+        addSequential(new MoveArmFor(
+                Robot.autonData.getTime("pickup_container"),
+                Robot.autonData.getSpeed("pickup_container")));
     }
 }
index 3cf7125f64ea1367229731fbef7ec522dd2d7317..d4b1199d17223f258b96f8249246927b6b35d29c 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class CloseClaw extends CommandBase {
+public class CloseClaw extends Command {
 
     public CloseClaw() {
         super("CloseClaw");
diff --git a/src/org/usfirst/frc/team3501/robot/commands/Command.java b/src/org/usfirst/frc/team3501/robot/commands/Command.java
new file mode 100644 (file)
index 0000000..42c58d6
--- /dev/null
@@ -0,0 +1,24 @@
+package org.usfirst.frc.team3501.robot.commands;
+
+import edu.wpi.first.wpilibj.command.Scheduler;
+
+public abstract class Command
+    extends edu.wpi.first.wpilibj.command.Command
+    implements CommandBase {
+
+    public Command(String commandName) {
+        super(commandName);
+    }
+
+    protected void schedule(Command c) {
+        Scheduler.getInstance().add(c);
+    }
+
+    protected void initialize() {}
+
+    protected void execute() {}
+
+    protected void end() {}
+
+    protected void interrupted() {}
+}
index 50467045ab345c2baa5821ef041c20e67100e259..3a5044e0976819d30718f309d2c66933642dd481 100644 (file)
@@ -1,43 +1,19 @@
 package org.usfirst.frc.team3501.robot.commands;
 
+import org.usfirst.frc.team3501.robot.AutonData;
 import org.usfirst.frc.team3501.robot.OI;
 import org.usfirst.frc.team3501.robot.Robot;
 import org.usfirst.frc.team3501.robot.subsystems.*;
 
-import edu.wpi.first.wpilibj.command.Command;
-import edu.wpi.first.wpilibj.command.Scheduler;
+public interface CommandBase {
 
-public abstract class CommandBase extends Command {
+    final static OI oi = Robot.oi;
 
-    protected static OI oi;
+    final static AutonData autonData = Robot.autonData;
 
-    protected static Drivetrain drivetrain;
-    protected static Arm arm;
-    protected static Claw claw;
+    final static Drivetrain drivetrain = Robot.drivetrain;
+    final static               Arm arm = Robot.arm;
+    final static             Claw claw = Robot.claw;
 
-    protected static Pneumatics pneumatics;
-
-    public CommandBase(String commandName) {
-        super(commandName);
-
-        oi = Robot.oi;
-
-        drivetrain = Robot.drivetrain;
-        arm        = Robot.arm;
-        claw       = Robot.claw;
-
-        pneumatics = Robot.pneumatics;
-    }
-
-    protected void schedule(Command c) {
-        Scheduler.getInstance().add(c);
-    }
-
-    protected void initialize() {}
-
-    protected void execute() {}
-
-    protected void end() {}
-
-    protected void interrupted() {}
+    final static Pneumatics pneumatics = Robot.pneumatics;
 }
diff --git a/src/org/usfirst/frc/team3501/robot/commands/CommandGroup.java b/src/org/usfirst/frc/team3501/robot/commands/CommandGroup.java
new file mode 100644 (file)
index 0000000..fcb7d26
--- /dev/null
@@ -0,0 +1,10 @@
+package org.usfirst.frc.team3501.robot.commands;
+
+public class CommandGroup
+    extends edu.wpi.first.wpilibj.command.CommandGroup
+    implements CommandBase {
+
+    public CommandGroup(String commandGroupName) {
+        super(commandGroupName);
+    }
+}
index c1ca5076102cb20da7aa7bdcdce6acd6a8351a49..295834b5dbddc14fa93d558e5a11d7a3eb7b38d1 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class DriveFor extends CommandBase {
+public class DriveFor extends Command {
 
     private double speed;
 
index 338e01ebf233ac2c91d429891173483d60612859..cf5446ad825a7fa3307e632d7243ec45759fcc7d 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class DriveWithJoysticks extends CommandBase {
+public class DriveWithJoysticks extends Command {
 
     public DriveWithJoysticks() {
         super("DriveWithJoysticks");
index 2354b3dd73517920d819a89ada92c9da2a1dc8c5..e2097717654b03189f8759cbea23f5edc06faffc 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class MoveArm extends CommandBase {
+public class MoveArm extends Command {
 
     public MoveArm() {
         super("MoveArm");
index 7f475f18425edfa9605e1be9a8c2e5293213e50b..b9f3bc73ed62baeb20dbb443ae251922c3ee397c 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class MoveArmFor extends CommandBase {
+public class MoveArmFor extends Command {
 
     private double speed;
 
index a3aa3a28ee72565a8f5ecdc83c66a6d20dd8a653..6adb5a7a149f80947a0c26b07e3a4257800a8773 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class OpenClaw extends CommandBase {
+public class OpenClaw extends Command {
 
     public OpenClaw() {
         super("OpenClaw");
index 4d9712ca6817d2e4680f6fe65ae89224ef8e5203..b915896c790952e28973360f7d743721c76ff17d 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class TensionLeftWinch extends CommandBase {
+public class TensionLeftWinch extends Command {
 
     private double speed;
 
index 1bf68cf822de7bdbdd0c5e19d4f2286f6f1a45e1..30dfb10fa9a306a2be6eaa1033d37335fceb5265 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class TensionRightWinch extends CommandBase {
+public class TensionRightWinch extends Command {
 
     private double speed;
 
index f1afa162ffd2f419bad31171e46a1187dfb94d5d..b7e7b5288740dc1c52360955cacd4273821bfe7b 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class ToggleClaw extends CommandBase {
+public class ToggleClaw extends Command {
 
     public ToggleClaw() {
         super("ToggleClaw");
index dbb78c9670d62a432087229317c8c80b8252f898..85838f9db3e85406838da88150656af67608a501 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class TurnOffCompressor extends CommandBase {
+public class TurnOffCompressor extends Command {
 
     public TurnOffCompressor() {
         super("TurnOffCompressor");
index 74d0fead7b8c5c43acb665b0ab178384adbb975e..49853f2ff14e2725689b4a5c14732cd7bf4fd87e 100644 (file)
@@ -1,6 +1,6 @@
 package org.usfirst.frc.team3501.robot.commands;
 
-public class TurnOnCompressor extends CommandBase {
+public class TurnOnCompressor extends Command {
 
     public TurnOnCompressor() {
         super("TurnOnCompressor");