mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Applies Google Styleguide to Java parts of the library (#23)
This was partially applied to simulation but simulation is a bit of a mess and has a lot of duplicated code.
This commit is contained in:
committed by
Peter Johnson
parent
64ab6e51fe
commit
a834fff7b2
@@ -10,29 +10,28 @@ package edu.wpi.first.wpilibj;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary;
|
||||
import edu.wpi.first.wpilibj.communication.HALControlWord;
|
||||
import edu.wpi.first.wpilibj.communication.HALAllianceStationID;
|
||||
import edu.wpi.first.wpilibj.communication.HALControlWord;
|
||||
import edu.wpi.first.wpilibj.hal.HALUtil;
|
||||
import edu.wpi.first.wpilibj.hal.PowerJNI;
|
||||
|
||||
/**
|
||||
* Provide access to the network communication data to / from the Driver
|
||||
* Station.
|
||||
* Provide access to the network communication data to / from the Driver Station.
|
||||
*/
|
||||
public class DriverStation implements RobotState.Interface {
|
||||
|
||||
/**
|
||||
* Number of Joystick Ports
|
||||
* Number of Joystick Ports.
|
||||
*/
|
||||
public static final int kJoystickPorts = 6;
|
||||
|
||||
private class HALJoystickButtons {
|
||||
public int buttons;
|
||||
public byte count;
|
||||
public int m_buttons;
|
||||
public byte m_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* The robot alliance that the robot is a part of
|
||||
* The robot alliance that the robot is a part of.
|
||||
*/
|
||||
public enum Alliance {
|
||||
Red, Blue, Invalid
|
||||
@@ -69,7 +68,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
|
||||
private Thread m_thread;
|
||||
private final Object m_dataSem;
|
||||
private volatile boolean m_thread_keepalive = true;
|
||||
private volatile boolean m_threadKeepAlive = true;
|
||||
private boolean m_userInDisabled = false;
|
||||
private boolean m_userInAutonomous = false;
|
||||
private boolean m_userInTeleop = false;
|
||||
@@ -90,8 +89,8 @@ public class DriverStation implements RobotState.Interface {
|
||||
/**
|
||||
* DriverStation constructor.
|
||||
*
|
||||
* The single DriverStation instance is created statically with the instance
|
||||
* static member variable.
|
||||
* <p>The single DriverStation instance is created statically with the instance static member
|
||||
* variable.
|
||||
*/
|
||||
protected DriverStation() {
|
||||
m_dataSem = new Object();
|
||||
@@ -110,18 +109,18 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Kill the thread
|
||||
* Kill the thread.
|
||||
*/
|
||||
public void release() {
|
||||
m_thread_keepalive = false;
|
||||
m_threadKeepAlive = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the service routine for the DS polling thread.
|
||||
* Provides the service routine for the DS polling m_thread.
|
||||
*/
|
||||
private void task() {
|
||||
int safetyCounter = 0;
|
||||
while (m_thread_keepalive) {
|
||||
while (m_threadKeepAlive) {
|
||||
HALUtil.takeMultiWait(m_packetDataAvailableSem, m_packetDataAvailableMutex);
|
||||
synchronized (this) {
|
||||
getData();
|
||||
@@ -156,8 +155,8 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for new data or for timeout, which ever comes first. If timeout is 0,
|
||||
* wait for new data only.
|
||||
* Wait for new data or for timeout, which ever comes first. If timeout is 0, wait for new data
|
||||
* only.
|
||||
*
|
||||
* @param timeout The maximum time in milliseconds to wait.
|
||||
*/
|
||||
@@ -166,14 +165,14 @@ public class DriverStation implements RobotState.Interface {
|
||||
try {
|
||||
m_dataSem.wait(timeout);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy data from the DS task for the user. If no new data exists, it will
|
||||
* just be returned, otherwise the data will be copied from the DS polling
|
||||
* loop.
|
||||
* Copy data from the DS task for the user. If no new data exists, it will just be returned,
|
||||
* otherwise the data will be copied from the DS polling loop.
|
||||
*/
|
||||
protected synchronized void getData() {
|
||||
|
||||
@@ -182,9 +181,9 @@ public class DriverStation implements RobotState.Interface {
|
||||
m_joystickAxes[stick] = FRCNetworkCommunicationsLibrary.HALGetJoystickAxes(stick);
|
||||
m_joystickPOVs[stick] = FRCNetworkCommunicationsLibrary.HALGetJoystickPOVs(stick);
|
||||
ByteBuffer countBuffer = ByteBuffer.allocateDirect(1);
|
||||
m_joystickButtons[stick].buttons =
|
||||
FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte) stick, countBuffer);
|
||||
m_joystickButtons[stick].count = countBuffer.get();
|
||||
m_joystickButtons[stick].m_buttons =
|
||||
FRCNetworkCommunicationsLibrary.HALGetJoystickButtons(stick, countBuffer);
|
||||
m_joystickButtons[stick].m_count = countBuffer.get();
|
||||
}
|
||||
|
||||
m_newControlData = true;
|
||||
@@ -200,8 +199,8 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports errors related to unplugged joysticks Throttles the errors so that
|
||||
* they don't overwhelm the DS
|
||||
* Reports errors related to unplugged joysticks Throttles the errors so that they don't overwhelm
|
||||
* the DS.
|
||||
*/
|
||||
private void reportJoystickUnpluggedError(String message) {
|
||||
double currentTime = Timer.getFPGATimestamp();
|
||||
@@ -212,8 +211,8 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports errors related to unplugged joysticks Throttles the errors so that
|
||||
* they don't overwhelm the DS
|
||||
* Reports errors related to unplugged joysticks Throttles the errors so that they don't overwhelm
|
||||
* the DS.
|
||||
*/
|
||||
private void reportJoystickUnpluggedWarning(String message) {
|
||||
double currentTime = Timer.getFPGATimestamp();
|
||||
@@ -224,11 +223,11 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the axis on a joystick. This depends on the mapping of the
|
||||
* joystick connected to the specified port.
|
||||
* Get the value of the axis on a joystick. This depends on the mapping of the joystick connected
|
||||
* to the specified port.
|
||||
*
|
||||
* @param stick The joystick to read.
|
||||
* @param axis The analog axis value to read from the joystick.
|
||||
* @param axis The analog axis value to read from the joystick.
|
||||
* @return The value of the axis on the joystick.
|
||||
*/
|
||||
public synchronized double getStickAxis(int stick, int axis) {
|
||||
@@ -256,7 +255,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of axes on a given joystick port
|
||||
* Returns the number of axes on a given joystick port.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return The number of axes on the indicated joystick
|
||||
@@ -294,7 +293,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of POVs on a given joystick port
|
||||
* Returns the number of POVs on a given joystick port.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return The number of POVs on the indicated joystick
|
||||
@@ -319,13 +318,13 @@ public class DriverStation implements RobotState.Interface {
|
||||
throw new RuntimeException("Joystick index is out of range, should be 0-3");
|
||||
}
|
||||
|
||||
return m_joystickButtons[stick].buttons;
|
||||
return m_joystickButtons[stick].m_buttons;
|
||||
}
|
||||
|
||||
/**
|
||||
* The state of one joystick button. Button indexes begin at 1.
|
||||
*
|
||||
* @param stick The joystick to read.
|
||||
* @param stick The joystick to read.
|
||||
* @param button The button index, beginning at 1.
|
||||
* @return The state of the joystick button.
|
||||
*/
|
||||
@@ -335,7 +334,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
|
||||
if (button > m_joystickButtons[stick].count) {
|
||||
if (button > m_joystickButtons[stick].m_count) {
|
||||
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
|
||||
+ " not available, check if controller is plugged in");
|
||||
return false;
|
||||
@@ -344,11 +343,11 @@ public class DriverStation implements RobotState.Interface {
|
||||
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java");
|
||||
return false;
|
||||
}
|
||||
return ((0x1 << (button - 1)) & m_joystickButtons[stick].buttons) != 0;
|
||||
return ((0x1 << (button - 1)) & m_joystickButtons[stick].m_buttons) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of buttons on a joystick
|
||||
* Gets the number of buttons on a joystick.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return The number of buttons on the indicated joystick
|
||||
@@ -360,11 +359,11 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
|
||||
return m_joystickButtons[stick].count;
|
||||
return m_joystickButtons[stick].m_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of isXbox on a joystick
|
||||
* Gets the value of isXbox on a joystick.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return A boolean that returns the value of isXbox
|
||||
@@ -376,7 +375,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
// TODO: Remove this when calling for descriptor on empty stick no longer
|
||||
// crashes
|
||||
if (1 > m_joystickButtons[stick].count && 1 > m_joystickAxes[stick].length) {
|
||||
if (1 > m_joystickButtons[stick].m_count && 1 > m_joystickAxes[stick].length) {
|
||||
reportJoystickUnpluggedWarning("Joystick on port " + stick
|
||||
+ " not available, check if controller is plugged in");
|
||||
return false;
|
||||
@@ -389,7 +388,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of type on a joystick
|
||||
* Gets the value of type on a joystick.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return The value of type
|
||||
@@ -401,7 +400,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
// TODO: Remove this when calling for descriptor on empty stick no longer
|
||||
// crashes
|
||||
if (1 > m_joystickButtons[stick].count && 1 > m_joystickAxes[stick].length) {
|
||||
if (1 > m_joystickButtons[stick].m_count && 1 > m_joystickAxes[stick].length) {
|
||||
reportJoystickUnpluggedWarning("Joystick on port " + stick
|
||||
+ " not available, check if controller is plugged in");
|
||||
return -1;
|
||||
@@ -410,7 +409,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the joystick at a port
|
||||
* Gets the name of the joystick at a port.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return The value of name
|
||||
@@ -422,7 +421,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
// TODO: Remove this when calling for descriptor on empty stick no longer
|
||||
// crashes
|
||||
if (1 > m_joystickButtons[stick].count && 1 > m_joystickAxes[stick].length) {
|
||||
if (1 > m_joystickButtons[stick].m_count && 1 > m_joystickAxes[stick].length) {
|
||||
reportJoystickUnpluggedWarning("Joystick on port " + stick
|
||||
+ " not available, check if controller is plugged in");
|
||||
return "";
|
||||
@@ -431,8 +430,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be
|
||||
* enabled.
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be enabled.
|
||||
*
|
||||
* @return True if the robot is enabled, false otherwise.
|
||||
*/
|
||||
@@ -442,8 +440,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be
|
||||
* disabled.
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be disabled.
|
||||
*
|
||||
* @return True if the robot should be disabled, false otherwise.
|
||||
*/
|
||||
@@ -452,8 +449,8 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be
|
||||
* running in autonomous mode.
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in
|
||||
* autonomous mode.
|
||||
*
|
||||
* @return True if autonomous mode should be enabled, false otherwise.
|
||||
*/
|
||||
@@ -463,9 +460,9 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be
|
||||
* running in test mode.
|
||||
*$
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in test
|
||||
* mode.
|
||||
*
|
||||
* @return True if test mode should be enabled, false otherwise.
|
||||
*/
|
||||
public boolean isTest() {
|
||||
@@ -474,20 +471,18 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be
|
||||
* running in operator-controlled mode.
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in
|
||||
* operator-controlled mode.
|
||||
*
|
||||
* @return True if operator-controlled mode should be enabled, false
|
||||
* otherwise.
|
||||
* @return True if operator-controlled mode should be enabled, false otherwise.
|
||||
*/
|
||||
public boolean isOperatorControl() {
|
||||
return !(isAutonomous() || isTest());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the FPGA outputs are enabled. The outputs
|
||||
* may be disabled if the robot is disabled or e-stopped, the watdhog has
|
||||
* expired, or if the roboRIO browns out.
|
||||
* Gets a value indicating whether the FPGA outputs are enabled. The outputs may be disabled if
|
||||
* the robot is disabled or e-stopped, the watdhog has expired, or if the roboRIO browns out.
|
||||
*
|
||||
* @return True if the FPGA outputs are enabled.
|
||||
*/
|
||||
@@ -497,7 +492,7 @@ public class DriverStation implements RobotState.Interface {
|
||||
|
||||
/**
|
||||
* Check if the system is browned out.
|
||||
*$
|
||||
*
|
||||
* @return True if the system is browned out
|
||||
*/
|
||||
public boolean isBrownedOut() {
|
||||
@@ -505,9 +500,9 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a new control packet from the driver station arrived since the last
|
||||
* time this function was called?
|
||||
*$
|
||||
* Has a new control packet from the driver station arrived since the last time this function was
|
||||
* called?
|
||||
*
|
||||
* @return True if the control data has been updated since the last call.
|
||||
*/
|
||||
public synchronized boolean isNewControlData() {
|
||||
@@ -517,8 +512,8 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current alliance from the FMS
|
||||
*$
|
||||
* Get the current alliance from the FMS.
|
||||
*
|
||||
* @return the current alliance
|
||||
*/
|
||||
public Alliance getAlliance() {
|
||||
@@ -574,11 +569,10 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the driver station attached to a Field Management System? Note: This
|
||||
* does not work with the Blue DS.
|
||||
*$
|
||||
* @return True if the robot is competing on a field being controlled by a
|
||||
* Field Management System
|
||||
* Is the driver station attached to a Field Management System? Note: This does not work with the
|
||||
* Blue DS.
|
||||
*
|
||||
* @return True if the robot is competing on a field being controlled by a Field Management System
|
||||
*/
|
||||
public boolean isFMSAttached() {
|
||||
HALControlWord controlWord = FRCNetworkCommunicationsLibrary.HALGetControlWord();
|
||||
@@ -591,14 +585,12 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the approximate match time The FMS does not send an official match
|
||||
* time to the robots, but does send an approximate match time. The value will
|
||||
* count down the time remaining in the current period (auto or teleop).
|
||||
* Warning: This is not an official time (so it cannot be used to dispute ref
|
||||
* calls or guarantee that a function will trigger before the match ends) The
|
||||
* Practice Match function of the DS approximates the behaviour seen on the
|
||||
* field.
|
||||
*$
|
||||
* Return the approximate match time The FMS does not send an official match time to the robots,
|
||||
* but does send an approximate match time. The value will count down the time remaining in the
|
||||
* current period (auto or teleop). Warning: This is not an official time (so it cannot be used to
|
||||
* dispute ref calls or guarantee that a function will trigger before the match ends) The
|
||||
* Practice Match function of the DS approximates the behaviour seen on the field.
|
||||
*
|
||||
* @return Time remaining in current match period (auto or teleop) in seconds
|
||||
*/
|
||||
public double getMatchTime() {
|
||||
@@ -606,9 +598,9 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Report error to Driver Station. Also prints error to System.err Optionally
|
||||
* appends Stack trace to error message
|
||||
*$
|
||||
* Report error to Driver Station. Also prints error to System.err Optionally appends Stack trace
|
||||
* to error message.
|
||||
*
|
||||
* @param printTrace If true, append stack trace to error string
|
||||
*/
|
||||
public static void reportError(String error, boolean printTrace) {
|
||||
@@ -616,25 +608,26 @@ public class DriverStation implements RobotState.Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Report warning to Driver Station. Also prints error to System.err Optionally
|
||||
* appends Stack trace to warning message
|
||||
*$
|
||||
* Report warning to Driver Station. Also prints error to System.err Optionally appends Stack
|
||||
* trace to warning message.
|
||||
*
|
||||
* @param printTrace If true, append stack trace to warning string
|
||||
*/
|
||||
public static void reportWarning(String error, boolean printTrace) {
|
||||
reportErrorImpl(false, 1, error, printTrace);
|
||||
}
|
||||
|
||||
private static void reportErrorImpl(boolean is_error, int code, String error, boolean printTrace) {
|
||||
private static void reportErrorImpl(boolean isError, int code, String error, boolean
|
||||
printTrace) {
|
||||
StackTraceElement[] traces = Thread.currentThread().getStackTrace();
|
||||
String locString;
|
||||
if (traces.length > 3)
|
||||
if (traces.length > 3) {
|
||||
locString = traces[3].toString();
|
||||
else
|
||||
} else {
|
||||
locString = new String();
|
||||
}
|
||||
boolean haveLoc = false;
|
||||
String traceString = new String();
|
||||
traceString = " at ";
|
||||
String traceString = " at ";
|
||||
for (int i = 3; i < traces.length; i++) {
|
||||
String loc = traces[i].toString();
|
||||
traceString += loc + "\n";
|
||||
@@ -644,48 +637,50 @@ public class DriverStation implements RobotState.Interface {
|
||||
haveLoc = true;
|
||||
}
|
||||
}
|
||||
FRCNetworkCommunicationsLibrary.HALSendError(is_error, code, false, error, locString, printTrace ? traceString : "", true);
|
||||
FRCNetworkCommunicationsLibrary.HALSendError(isError, code, false, error, locString,
|
||||
printTrace ? traceString : "", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only to be used to tell the Driver Station what code you claim to be
|
||||
* executing for diagnostic purposes only
|
||||
*$
|
||||
* @param entering If true, starting disabled code; if false, leaving disabled
|
||||
* code
|
||||
* Only to be used to tell the Driver Station what code you claim to be executing for diagnostic
|
||||
* purposes only.
|
||||
*
|
||||
* @param entering If true, starting disabled code; if false, leaving disabled code
|
||||
*/
|
||||
@SuppressWarnings("MethodName")
|
||||
public void InDisabled(boolean entering) {
|
||||
m_userInDisabled = entering;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only to be used to tell the Driver Station what code you claim to be
|
||||
* executing for diagnostic purposes only
|
||||
*$
|
||||
* @param entering If true, starting autonomous code; if false, leaving
|
||||
* autonomous code
|
||||
* Only to be used to tell the Driver Station what code you claim to be executing for diagnostic
|
||||
* purposes only.
|
||||
*
|
||||
* @param entering If true, starting autonomous code; if false, leaving autonomous code
|
||||
*/
|
||||
@SuppressWarnings("MethodName")
|
||||
public void InAutonomous(boolean entering) {
|
||||
m_userInAutonomous = entering;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only to be used to tell the Driver Station what code you claim to be
|
||||
* executing for diagnostic purposes only
|
||||
*$
|
||||
* @param entering If true, starting teleop code; if false, leaving teleop
|
||||
* code
|
||||
* Only to be used to tell the Driver Station what code you claim to be executing for diagnostic
|
||||
* purposes only.
|
||||
*
|
||||
* @param entering If true, starting teleop code; if false, leaving teleop code
|
||||
*/
|
||||
@SuppressWarnings("MethodName")
|
||||
public void InOperatorControl(boolean entering) {
|
||||
m_userInTeleop = entering;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only to be used to tell the Driver Station what code you claim to be
|
||||
* executing for diagnostic purposes only
|
||||
*$
|
||||
* Only to be used to tell the Driver Station what code you claim to be executing for diagnostic
|
||||
* purposes only.
|
||||
*
|
||||
* @param entering If true, starting test code; if false, leaving test code
|
||||
*/
|
||||
@SuppressWarnings("MethodName")
|
||||
public void InTest(boolean entering) {
|
||||
m_userInTest = entering;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user