2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2014-12-29 16:06:11 -05:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
2013-12-15 18:30:16 -05:00
|
|
|
import java.net.URL;
|
2016-05-20 12:07:40 -04:00
|
|
|
import java.util.Arrays;
|
2013-12-15 18:30:16 -05:00
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.jar.Manifest;
|
2017-08-18 21:35:53 -07:00
|
|
|
//import org.opencv.core.Core;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
import edu.wpi.first.wpilibj.hal.FRCNetComm.tInstances;
|
|
|
|
|
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
|
|
|
|
|
import edu.wpi.first.wpilibj.hal.HAL;
|
2014-07-23 14:04:33 -04:00
|
|
|
import edu.wpi.first.wpilibj.internal.HardwareHLUsageReporting;
|
|
|
|
|
import edu.wpi.first.wpilibj.internal.HardwareTimer;
|
2017-07-08 10:50:56 -04:00
|
|
|
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
|
2013-12-15 18:30:16 -05:00
|
|
|
import edu.wpi.first.wpilibj.networktables.NetworkTable;
|
2016-10-20 23:54:04 -07:00
|
|
|
import edu.wpi.first.wpilibj.util.WPILibVersion;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Implement a Robot Program framework. The RobotBase class is intended to be subclassed by a user
|
|
|
|
|
* creating a robot program. Overridden autonomous() and operatorControl() methods are called at the
|
|
|
|
|
* appropriate time as the match proceeds. In the current implementation, the Autonomous code will
|
|
|
|
|
* run to completion before the OperatorControl code could start. In the future the Autonomous code
|
|
|
|
|
* might be spawned as a task, then killed at the end of the Autonomous period.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
|
|
|
|
public abstract class RobotBase {
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* The VxWorks priority that robot code should work at (so Java code should run at).
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public static final int ROBOT_TASK_PRIORITY = 101;
|
|
|
|
|
|
2016-12-23 11:48:56 -05:00
|
|
|
/**
|
|
|
|
|
* The ID of the main Java thread.
|
|
|
|
|
*/
|
|
|
|
|
// This is usually 1, but it is best to make sure
|
|
|
|
|
public static final long MAIN_THREAD_ID = Thread.currentThread().getId();
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
protected final DriverStation m_ds;
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* 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.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>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.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
protected RobotBase() {
|
2015-08-13 23:17:19 -07:00
|
|
|
NetworkTable.setNetworkIdentity("Robot");
|
2016-01-05 19:20:16 -08:00
|
|
|
NetworkTable.setPersistentFilename("/home/lvuser/networktables.ini");
|
2017-05-07 02:22:16 -04:00
|
|
|
NetworkTable.setServerMode(); // must be before b
|
2015-06-25 15:07:55 -04:00
|
|
|
m_ds = DriverStation.getInstance();
|
|
|
|
|
NetworkTable.getTable(""); // forces network tables to initialize
|
|
|
|
|
NetworkTable.getTable("LiveWindow").getSubTable("~STATUS~").putBoolean("LW Enabled", false);
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
LiveWindow.setEnabled(false);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free the resources for a RobotBase class.
|
|
|
|
|
*/
|
2016-05-20 12:07:40 -04:00
|
|
|
public void free() {
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2017-07-28 22:24:05 -07:00
|
|
|
* Get if the robot is a simulation.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return If the robot is running in simulation.
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isSimulation() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-28 22:24:05 -07:00
|
|
|
* Get if the robot is real.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return If the robot is running in the real world.
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isReal() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the Robot is currently disabled.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @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.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return True if the Robot is currently enabled by the field controls.
|
|
|
|
|
*/
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
|
return m_ds.isEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Determine if the robot is currently in Autonomous mode as determined by the field
|
|
|
|
|
* controls.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the robot is currently operating Autonomously.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public boolean isAutonomous() {
|
|
|
|
|
return m_ds.isAutonomous();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Determine if the robot is currently in Test mode as determined by the driver
|
|
|
|
|
* station.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the robot is currently operating in Test mode.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public boolean isTest() {
|
|
|
|
|
return m_ds.isTest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Determine if the robot is currently in Operator Control mode as determined by the field
|
|
|
|
|
* controls.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the robot is currently operating in Tele-Op mode.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public boolean isOperatorControl() {
|
|
|
|
|
return m_ds.isOperatorControl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates if new data is available from the driver station.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
|
|
|
|
* @return Has new data arrived over the network since the last time this function was called?
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public boolean isNewDataAvailable() {
|
|
|
|
|
return m_ds.isNewControlData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide an alternate "main loop" via startCompetition().
|
|
|
|
|
*/
|
|
|
|
|
public abstract void startCompetition();
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings("JavadocMethod")
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common initialization for all robot programs.
|
|
|
|
|
*/
|
|
|
|
|
public static void initializeHardwareConfiguration() {
|
2017-07-01 00:43:06 -07:00
|
|
|
if (!HAL.initialize(500, 0)) {
|
|
|
|
|
throw new IllegalStateException("Failed to initialize. Terminating");
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Set some implementations so that the static methods work properly
|
|
|
|
|
Timer.SetImplementation(new HardwareTimer());
|
|
|
|
|
HLUsageReporting.SetImplementation(new HardwareHLUsageReporting());
|
|
|
|
|
RobotState.SetImplementation(DriverStation.getInstance());
|
2016-11-11 22:32:13 -08:00
|
|
|
|
|
|
|
|
// Load opencv
|
2017-08-18 21:35:53 -07:00
|
|
|
/* TODO (after opencv is added again)
|
2016-11-11 22:32:13 -08:00
|
|
|
try {
|
|
|
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
|
|
} catch (UnsatisfiedLinkError ex) {
|
|
|
|
|
System.out.println("OpenCV Native Libraries could not be loaded.");
|
|
|
|
|
System.out.println("Please try redeploying, or reimage your roboRIO and try again.");
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
2017-08-18 21:35:53 -07:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starting point for the applications.
|
|
|
|
|
*/
|
2016-12-23 11:20:13 -08:00
|
|
|
@SuppressWarnings("PMD.UnusedFormalParameter")
|
2016-05-20 12:07:40 -04:00
|
|
|
public static void main(String... args) {
|
2015-06-25 15:07:55 -04:00
|
|
|
initializeHardwareConfiguration();
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
HAL.report(tResourceType.kResourceType_Language, tInstances.kLanguage_Java);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-11-07 12:01:44 -05:00
|
|
|
String robotName = "";
|
|
|
|
|
Enumeration<URL> resources = null;
|
|
|
|
|
try {
|
|
|
|
|
resources = RobotBase.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
|
2016-05-20 12:07:40 -04:00
|
|
|
} catch (IOException ex) {
|
|
|
|
|
ex.printStackTrace();
|
2015-11-07 12:01:44 -05:00
|
|
|
}
|
|
|
|
|
while (resources != null && resources.hasMoreElements()) {
|
|
|
|
|
try {
|
|
|
|
|
Manifest manifest = new Manifest(resources.nextElement().openStream());
|
|
|
|
|
robotName = manifest.getMainAttributes().getValue("Robot-Class");
|
2016-05-20 12:07:40 -04:00
|
|
|
} catch (IOException ex) {
|
|
|
|
|
ex.printStackTrace();
|
2015-11-07 12:01:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
System.out.println("********** Robot program starting **********");
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
RobotBase robot;
|
|
|
|
|
try {
|
|
|
|
|
robot = (RobotBase) Class.forName(robotName).newInstance();
|
2016-05-20 12:07:40 -04:00
|
|
|
} catch (Throwable throwable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
DriverStation.reportError("ERROR Unhandled exception instantiating robot " + robotName + " "
|
2016-05-20 12:07:40 -04:00
|
|
|
+ throwable.toString() + " at " + Arrays.toString(throwable.getStackTrace()), false);
|
2015-06-25 15:07:55 -04:00
|
|
|
System.err.println("WARNING: Robots don't quit!");
|
|
|
|
|
System.err.println("ERROR: Could not instantiate robot " + robotName + "!");
|
|
|
|
|
System.exit(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2016-05-20 12:07:40 -04:00
|
|
|
final File file = new File("/tmp/frc_versions/FRC_Lib_Version.ini");
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
if (file.exists()) {
|
2015-06-25 15:07:55 -04:00
|
|
|
file.delete();
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
try (FileOutputStream output = new FileOutputStream(file)) {
|
2016-12-02 20:36:22 -08:00
|
|
|
output.write("Java ".getBytes());
|
2016-10-20 23:54:04 -07:00
|
|
|
output.write(WPILibVersion.Version.getBytes());
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean errorOnExit = false;
|
|
|
|
|
try {
|
|
|
|
|
robot.startCompetition();
|
2016-05-20 12:07:40 -04:00
|
|
|
} catch (Throwable throwable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
DriverStation.reportError(
|
2016-05-20 12:07:40 -04:00
|
|
|
"ERROR Unhandled exception: " + throwable.toString() + " at "
|
|
|
|
|
+ Arrays.toString(throwable.getStackTrace()), false);
|
2015-06-25 15:07:55 -04:00
|
|
|
errorOnExit = true;
|
|
|
|
|
} finally {
|
|
|
|
|
// startCompetition never returns unless exception occurs....
|
|
|
|
|
System.err.println("WARNING: Robots don't quit!");
|
|
|
|
|
if (errorOnExit) {
|
|
|
|
|
System.err
|
2016-05-20 12:07:40 -04:00
|
|
|
.println("---> The startCompetition() method (or methods called by it) should have "
|
|
|
|
|
+ "handled the exception above.");
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
|
|
|
|
System.err.println("---> Unexpected return from startCompetition() method.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.exit(1);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|