From 7905259e217f0d0a42a545ed9e4e1fe6fee23579 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 31 Jul 2014 09:45:33 -0400 Subject: [PATCH] Refactors the RobotBase Setup into a method. This allows the robot base setup to be used for the robot base as well as the setup for the test system. Change-Id: I2f8e37d42c84001f4b4eff2afd7c3e1d73785d7c --- .../java/edu/wpi/first/wpilibj/RobotBase.java | 293 +++++++++--------- .../first/wpilibj/test/AbstractComsSetup.java | 21 +- 2 files changed, 152 insertions(+), 162 deletions(-) diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/RobotBase.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/RobotBase.java index ebac725ee4..2fb0fd9212 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/RobotBase.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/RobotBase.java @@ -1,25 +1,21 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */ +/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ +/* the project. */ /*----------------------------------------------------------------------------*/ package edu.wpi.first.wpilibj; import java.io.IOException; import java.net.URL; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.IntBuffer; import java.util.Enumeration; import java.util.jar.Manifest; +import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary; import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tInstances; import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType; -import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary; import edu.wpi.first.wpilibj.communication.UsageReporting; -import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.internal.HardwareHLUsageReporting; import edu.wpi.first.wpilibj.internal.HardwareTimer; import edu.wpi.first.wpilibj.networktables.NetworkTable; @@ -33,144 +29,149 @@ import edu.wpi.first.wpilibj.networktables.NetworkTable; * might be spawned as a task, then killed at the end of the Autonomous period. */ public abstract class RobotBase { - /** - * The VxWorks priority that robot code should work at (so Java code should run at) - */ - public static final int ROBOT_TASK_PRIORITY = 101; + /** + * The VxWorks priority that robot code should work at (so Java code should run at) + */ + public static final int ROBOT_TASK_PRIORITY = 101; - /** - * Boolean System property. If true (default), send System.err messages to the driver station. - */ - public final static String ERRORS_TO_DRIVERSTATION_PROP = "first.driverstation.senderrors"; + /** + * Boolean System property. If true (default), send System.err messages to the driver station. + */ + public final static String ERRORS_TO_DRIVERSTATION_PROP = "first.driverstation.senderrors"; - protected final DriverStation m_ds; + protected final DriverStation m_ds; - /** - * Constructor for a generic robot program. - * User code should be placed in the constructor that runs before the Autonomous or Operator - * Control period starts. The constructor will run to completion before Autonomous is entered. - * - * This must be used to ensure that the communications code starts. In the future it would be - * nice to put this code into it's own task that loads on boot so ensure that it runs. - */ - protected RobotBase() { - // TODO: StartCAPI(); - // TODO: See if the next line is necessary - // Resource.RestartProgram(); + /** + * Constructor for a generic robot program. + * User code should be placed in the constructor that runs before the Autonomous or Operator + * Control period starts. The constructor will run to completion before Autonomous is entered. + * + * This must be used to ensure that the communications code starts. In the future it would be + * nice to put this code into it's own task that loads on boot so ensure that it runs. + */ + protected RobotBase() { + // TODO: StartCAPI(); + // TODO: See if the next line is necessary + // Resource.RestartProgram(); -// if (getBooleanProperty(ERRORS_TO_DRIVERSTATION_PROP, true)) { -// Utility.sendErrorStreamToDriverStation(true); -// } - NetworkTable.setServerMode();//must be before b - m_ds = DriverStation.getInstance(); - NetworkTable.getTable(""); // forces network tables to initialize - NetworkTable.getTable("LiveWindow").getSubTable("~STATUS~").putBoolean("LW Enabled", false); - } +// if (getBooleanProperty(ERRORS_TO_DRIVERSTATION_PROP, true)) { +// Utility.sendErrorStreamToDriverStation(true); +// } + NetworkTable.setServerMode();//must be before b + m_ds = DriverStation.getInstance(); + NetworkTable.getTable(""); // forces network tables to initialize + NetworkTable.getTable("LiveWindow").getSubTable("~STATUS~").putBoolean("LW Enabled", false); + } - /** - * Free the resources for a RobotBase class. - */ - public void free() { - } + /** + * Free the resources for a RobotBase class. + */ + public void free() { + } - /** - * @return If the robot is running in simulation. - */ - public static boolean isSimulation() { - return false; - } + /** + * @return If the robot is running in simulation. + */ + public static boolean isSimulation() { + return false; + } - /** - * @return If the robot is running in the real world. - */ - public static boolean isReal() { - return true; - } + /** + * @return If the robot is running in the real world. + */ + public static boolean isReal() { + return true; + } - /** - * Determine if the Robot is currently disabled. - * @return True if the Robot is currently disabled by the field controls. - */ - public boolean isDisabled() { - return m_ds.isDisabled(); - } + /** + * Determine if the Robot is currently disabled. + * @return True if the Robot is currently disabled by the field controls. + */ + public boolean isDisabled() { + return m_ds.isDisabled(); + } - /** - * Determine if the Robot is currently enabled. - * @return True if the Robot is currently enabled by the field controls. - */ - public boolean isEnabled() { - return m_ds.isEnabled(); - } + /** + * Determine if the Robot is currently enabled. + * @return True if the Robot is currently enabled by the field controls. + */ + public boolean isEnabled() { + return m_ds.isEnabled(); + } - /** - * Determine if the robot is currently in Autonomous mode. - * @return True if the robot is currently operating Autonomously as determined by the field controls. - */ - public boolean isAutonomous() { - return m_ds.isAutonomous(); - } + /** + * Determine if the robot is currently in Autonomous mode. + * @return True if the robot is currently operating Autonomously as determined by the field controls. + */ + public boolean isAutonomous() { + return m_ds.isAutonomous(); + } - /** - * Determine if the robot is currently in Test mode - * @return True if the robot is currently operating in Test mode as determined by the driver station. - */ - public boolean isTest() { - return m_ds.isTest(); - } + /** + * Determine if the robot is currently in Test mode + * @return True if the robot is currently operating in Test mode as determined by the driver station. + */ + public boolean isTest() { + return m_ds.isTest(); + } - /** - * Determine if the robot is currently in Operator Control mode. - * @return True if the robot is currently operating in Tele-Op mode as determined by the field controls. - */ - public boolean isOperatorControl() { - return m_ds.isOperatorControl(); - } + /** + * Determine if the robot is currently in Operator Control mode. + * @return True if the robot is currently operating in Tele-Op mode as determined by the field controls. + */ + public boolean isOperatorControl() { + return m_ds.isOperatorControl(); + } - /** - * Indicates if new data is available from the driver station. - * @return Has new data arrived over the network since the last time this function was called? - */ - public boolean isNewDataAvailable() { - return m_ds.isNewControlData(); - } + /** + * Indicates if new data is available from the driver station. + * @return Has new data arrived over the network since the last time this function was called? + */ + public boolean isNewDataAvailable() { + return m_ds.isNewControlData(); + } - /** - * Provide an alternate "main loop" via startCompetition(). - */ - public abstract void startCompetition(); + /** + * Provide an alternate "main loop" via startCompetition(). + */ + public abstract void startCompetition(); - public static boolean getBooleanProperty(String name, boolean defaultValue) { - String propVal = System.getProperty(name); - if (propVal == null) { - return defaultValue; - } - if (propVal.equalsIgnoreCase("false")) { - return false; - } else if (propVal.equalsIgnoreCase("true")) { - return true; - } else { - throw new IllegalStateException(propVal); - } - } + public static boolean getBooleanProperty(String name, boolean defaultValue) { + String propVal = System.getProperty(name); + if (propVal == null) { + return defaultValue; + } + if (propVal.equalsIgnoreCase("false")) { + return false; + } else if (propVal.equalsIgnoreCase("true")) { + return true; + } else { + throw new IllegalStateException(propVal); + } + } - /** - * Starting point for the applications. Starts the OtaServer and then runs - * the robot. - * @throws javax.microedition.midlet.MIDletStateChangeException - */ - public static void main(String args[]) { - boolean errorOnExit = false; + /** + * Common initialization for all robot programs. + */ + public static void initializeHardwareConfiguration(){ + FRCNetworkCommunicationsLibrary.FRCNetworkCommunicationReserve(); + FRCNetworkCommunicationsLibrary.FRCNetworkCommunicationObserveUserProgramStarting(); - FRCNetworkCommunicationsLibrary.FRCNetworkCommunicationReserve(); - FRCNetworkCommunicationsLibrary.FRCNetworkCommunicationObserveUserProgramStarting(); + // Set some implementations so that the static methods work properly + Timer.SetImplementation(new HardwareTimer()); + HLUsageReporting.SetImplementation(new HardwareHLUsageReporting()); + RobotState.SetImplementation(DriverStation.getInstance()); + } - // Set some implementations so that the static methods work properly - Timer.SetImplementation(new HardwareTimer()); - HLUsageReporting.SetImplementation(new HardwareHLUsageReporting()); - RobotState.SetImplementation(DriverStation.getInstance()); + /** + * Starting point for the applications. + */ + public static void main(String args[]) { + boolean errorOnExit = false; - UsageReporting.report(tResourceType.kResourceType_Language, tInstances.kLanguage_Java); + initializeHardwareConfiguration(); + + UsageReporting.report(tResourceType.kResourceType_Language, tInstances.kLanguage_Java); String robotName = ""; Enumeration resources = null; @@ -178,13 +179,13 @@ public abstract class RobotBase { resources = RobotBase.class.getClassLoader().getResources("META-INF/MANIFEST.MF"); } catch (IOException e) {e.printStackTrace();} while (resources != null && resources.hasMoreElements()) { - try { - Manifest manifest = new Manifest(resources.nextElement().openStream()); - robotName = manifest.getMainAttributes().getValue("Robot-Class"); - } catch (IOException e) {e.printStackTrace();} + try { + Manifest manifest = new Manifest(resources.nextElement().openStream()); + robotName = manifest.getMainAttributes().getValue("Robot-Class"); + } catch (IOException e) {e.printStackTrace();} } - RobotBase robot; + RobotBase robot; try { robot = (RobotBase) Class.forName(robotName).newInstance(); } catch (InstantiationException|IllegalAccessException|ClassNotFoundException e) { @@ -193,19 +194,19 @@ public abstract class RobotBase { return; } - try { - robot.startCompetition(); - } catch (Throwable t) { - t.printStackTrace(); - errorOnExit = true; - } finally { - // startCompetition never returns unless exception occurs.... - System.err.println("WARNING: Robots don't quit!"); - if (errorOnExit) { - System.err.println("---> The startCompetition() method (or methods called by it) should have handled the exception above."); - } else { - System.err.println("---> Unexpected return from startCompetition() method."); - } - } - } + try { + robot.startCompetition(); + } catch (Throwable t) { + t.printStackTrace(); + errorOnExit = true; + } finally { + // startCompetition never returns unless exception occurs.... + System.err.println("WARNING: Robots don't quit!"); + if (errorOnExit) { + System.err.println("---> The startCompetition() method (or methods called by it) should have handled the exception above."); + } else { + System.err.println("---> Unexpected return from startCompetition() method."); + } + } + } } diff --git a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/AbstractComsSetup.java b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/AbstractComsSetup.java index c569a2c713..3a9e45cc74 100644 --- a/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/AbstractComsSetup.java +++ b/wpilibj/wpilibJavaIntegrationTests/src/main/java/edu/wpi/first/wpilibj/test/AbstractComsSetup.java @@ -16,12 +16,8 @@ import org.junit.runner.Description; import org.junit.runners.model.MultipleFailureException; import edu.wpi.first.wpilibj.DriverStation; -import edu.wpi.first.wpilibj.HLUsageReporting; -import edu.wpi.first.wpilibj.RobotState; +import edu.wpi.first.wpilibj.RobotBase; import edu.wpi.first.wpilibj.Timer; -import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary; -import edu.wpi.first.wpilibj.internal.HardwareHLUsageReporting; -import edu.wpi.first.wpilibj.internal.HardwareTimer; import edu.wpi.first.wpilibj.livewindow.LiveWindow; /** @@ -47,15 +43,8 @@ public abstract class AbstractComsSetup { static{ if (!initialized) { // Set some implementations so that the static methods work properly - Timer.SetImplementation(new HardwareTimer()); - HLUsageReporting.SetImplementation(new HardwareHLUsageReporting()); - RobotState.SetImplementation(DriverStation.getInstance()); - + RobotBase.initializeHardwareConfiguration(); - // Start up the network communications - FRCNetworkCommunicationsLibrary.FRCNetworkCommunicationReserve(); - FRCNetworkCommunicationsLibrary - .FRCNetworkCommunicationObserveUserProgramStarting(); LiveWindow.setEnabled(false); TestBench.out().println("Started coms"); @@ -71,14 +60,14 @@ public abstract class AbstractComsSetup { TestBench.out().print("\rWaiting for enable: " + i++); } TestBench.out().println(); - + // Ready to go! initialized = true; TestBench.out().println("Running!"); } } - - + + protected abstract Logger getClassLogger(); /** This causes a stack trace to be printed as the test is running as well as at the end */