Skip to content

Instantly share code, notes, and snippets.

@unbun
Last active January 8, 2018 19:23
Show Gist options
  • Save unbun/8d8af6be25d85bff3558805f700b2b02 to your computer and use it in GitHub Desktop.
Save unbun/8d8af6be25d85bff3558805f700b2b02 to your computer and use it in GitHub Desktop.
A class used to store the different Auto Commands in an ArrayList so that we can initialize and access them w/o messing with stuff in Robot.java. There is also stuff in Robot.java that is used to access it
package org.usfirst.frc.team2791.robot.util;
import java.util.ArrayList;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* Allows user to list a group of Commands and then select from them based on a key </br>
* Also has team color choosing capabilities</br>
* It's basically just an ArrayList for Commands, and some methods to help access them
*/
public class CommandSelector {
private int key = 0;
private String teamColor = "RED";
private final ArrayList<Command> cList = new ArrayList<Command>();
private String name = "";
/**
* Allows user to list a group of Commands and then select from them based on a key </br>
* Also has team color choosing capabilities</br>
* It's basically just an ArrayList for Commands, and some methods to help access them
* @param name the name of what you are selecting (ex: 'Auto Mode')
*/
public CommandSelector(String name){
this.name = name;
}
/**
* @param command a command to add to the end of the list
*/
public void addCommand(Command command) {
cList.add(command);
}
/**
* @param command a command to add to the end of the list
* @param index the desired index that the command should be added to
* Adds a Command to the list at a certain point and shifts all commands after down by one
*/
public void addCommand(Command command, int index){
cList.add(index, command);
}
//***setters***
/**
* Increase the Selected Key by 1
* or reset it to 0 to stay in the ArrayList bounds
*/
public void incrementKey(){
key = ((key + 1) % cList.size());
}
/**
* Decrease the Selected Key by 1
* or reset it to max value to stay in the ArrayList bounds
*/
public void decrementKey(){
key = ((key - 1) + cList.size()) % cList.size();
}
/**
* @param key the desired key, which corresponds to a desired Command in the array list
*/
public void setKey(int key){
this.key = key;
}
/**
* Sets the team color to Red
*/
public void setColorToRed(){
this.teamColor = "RED";
}
/**
* Sets the team color to Blue
*/
public void setColorToBlue(){
this.teamColor = "BLUE";
}
/**
* Use the specific color setters [ setColorToRed() or setColorToBlue() ] if possible
* @param color the desired team color for the command to operate on, should be "RED" or "BLUE"
*/
public void setColor(String color){
this.teamColor = color;
}
//***getters***
public String getColor(){
return this.teamColor;
}
/**
* @return the key that corresponds to the position of the desired Command in the ArrayList
*/
public int getKey(){
return this.key;
}
public String getName(){
return this.name;
}
public Command getSelected() {
return cList.get(key);
}
/**
* @return the ArrayList full of all added Commands
*/
public ArrayList<Command> getCommandList(){
return this.cList;
}
/**
* Sends the Selected Command, Command Key, and Color to Smart Dashboard
*/
public void debug(){
String commandName = "";
try{
commandName = getSelected().getName();
}
catch(NullPointerException e){
commandName = "Selected Command Name Unavailable";
}
SmartDashboard.putString("Selected " + name + " Command", commandName);
SmartDashboard.putString("Selected Team Color", getColor());
SmartDashboard.putNumber("Selected " + name + " KEY", getKey());
}
}
public void roboInit(){
autoSelector = new CommandSelector("Auto Mode");
autoSelector.addCommand(new CenterGearAuton(autoSelector.getColor()));
autoSelector.addCommand(new BoilerGearAuton(autoSelector.getColor()));
autoSelector.addCommand(new LoadingStationGearAuton(autoSelector.getColor()));
autoSelector.addCommand(new HopperAuton(autoSelector.getColor()));
autoSelector.addCommand(new CenterGearAutonShooting(autoSelector.getColor()));
autoSelector.addCommand(new DriveStraightEncoderGyro(SmartDashboard.getNumber("TUNE PID Distance", 0.0), 0.7, 6));
autoSelector.addCommand(new StationaryGyroTurn(SmartDashboard.getNumber("TUNE PID Stat Angle", 0.0), 1));
}
...
public void disabledPeriodic() {
//allows us to reset the gyro and both encoders while disabled
if(OI.driver.getButtonSt()){
drivetrain.reset();
}
if(!(OI.operator.getButtonLB() && OI.operator.getButtonRB()))
lookForAction = true;
if(OI.operator.getButtonRB() && lookForAction){
autoSelector.incrementKey();
lookForAction = false;
}
if(OI.operator.getButtonLB() && lookForAction){
autoSelector.decrementKey();
lookForAction = false;
}
if(OI.operator.getButtonX())
autoSelector.setColorToBlue();
if(OI.operator.getButtonB())
autoSelector.setColorToRed();
}
...
public void autonomousInit() {
autonomousCommand = autoSelector.getSelected();
String teamColor = autoSelector.getColor();
if(autonomousCommand.getName().equals("CenterGear & Shoot")){
if(teamColor.equals("RED"))
visionTable.setVisionOffset(-60.0);
else
visionTable.setVisionOffset(60.0);
}
System.out.println("***Starting "+autonomousCommand.getName()+" AutoMode***");
if (autonomousCommand != null)
autonomousCommand.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment