RoboRio Robot Configuration Picker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Robot extends SampleRobot | |
| { | |
| // specify all possible robot configs, with the default (first one) being the competition config | |
| // so it can default to that if we don't load the config.txt file properly | |
| public static RobotConfig config = RobotConfigPicker.get(new RobotConfig[] { new RRRConfigCompetition(), new RRRConfigPractice()}); | |
| public static boolean isCompBot() | |
| { | |
| return config instanceof RRRConfigCompetition; | |
| } | |
| ... | |
| rest of code | |
| ... | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.wfrobotics.reuse.config; | |
| import java.io.BufferedReader; | |
| import java.io.FileReader; | |
| import org.wfrobotics.robot.config.robotConfigs.RobotConfig; | |
| import edu.wpi.first.wpilibj.DriverStation; | |
| public class RobotConfigPicker | |
| { | |
| /** | |
| * Read the contents of the text file on the Rio at ~/config.txt to match to a config class's name | |
| * and set the activeConfig which can be grabbed via {@link #get()}. Will print an error to the | |
| * driver station and select the first config in the array if it can't match a name. | |
| * @param configs Array of {@link RobotConfig} configs | |
| */ | |
| public static RobotConfig get(RobotConfig[] configs) | |
| { | |
| return get(configs, "~/config.txt"); | |
| } | |
| /** | |
| * Read the contents of the text file on the Rio at {@code file} to match to a config class's name | |
| * and set the activeConfig which can be grabbed via {@link #get()}. Will print an error to the | |
| * driver station and select the first config in the array if it can't match a name. | |
| * @param configs Array of {@link RobotConfigBase} configs | |
| * @param file custom file path to look for the config name | |
| */ | |
| public static RobotConfig get(RobotConfig[] configs, String file) | |
| { | |
| String configFilePath = getFile(file); | |
| String configName = readFile(configFilePath); | |
| for (RobotConfig robotConfig : configs) | |
| { | |
| // try to match the file's config name to any of the configs passed in to this class | |
| if(robotConfig.getClass().getSimpleName().toLowerCase().equals(configName.toLowerCase())) | |
| { | |
| // found, so return the config | |
| DriverStation.reportWarning(" Using config: " + robotConfig.getClass().getSimpleName(), false); | |
| return robotConfig; | |
| } | |
| } | |
| // couldn't find a config, so we revert to the first one passed in | |
| // but throw an error so the Driver station is aware | |
| DriverStation.reportError(" Failed to find config file, using config instead: " + configs[0].getClass().getSimpleName(), false); | |
| return configs[0]; | |
| } | |
| private static String readFile(String filename) | |
| { | |
| String configName = ""; | |
| try | |
| { | |
| BufferedReader reader = new BufferedReader(new FileReader(filename)); | |
| String line; | |
| if ((line = reader.readLine()) != null) | |
| { | |
| configName = line; | |
| } | |
| reader.close(); | |
| return configName; | |
| } | |
| catch (Exception e) | |
| { | |
| System.err.format("Exception occurred trying to read '%s'", filename); | |
| return configName; | |
| } | |
| } | |
| private static String getFile(String filePath) { | |
| filePath = filePath.replaceFirst("^~", System.getProperty("user.home")); | |
| return filePath; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment