Adds match specific calls to Java and C++ (#720)

Uses caching, matching the joystick calls.
This commit is contained in:
Thad House
2017-11-09 19:59:29 -08:00
committed by Peter Johnson
parent 2225c4fee2
commit 7bbd13d914
9 changed files with 310 additions and 51 deletions

View File

@@ -12,6 +12,7 @@ import java.nio.ByteBuffer;
import edu.wpi.first.wpilibj.hal.AllianceStationID;
import edu.wpi.first.wpilibj.hal.ControlWord;
import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.hal.MatchInfoData;
import edu.wpi.first.wpilibj.hal.PowerJNI;
/**
@@ -54,6 +55,10 @@ public class DriverStation implements RobotState.Interface {
Red, Blue, Invalid
}
public enum MatchType {
None, Practice, Qualification, Elimination
}
private static final double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
private double m_nextMessageTime = 0.0;
@@ -76,11 +81,13 @@ public class DriverStation implements RobotState.Interface {
private HALJoystickAxes[] m_joystickAxes = new HALJoystickAxes[kJoystickPorts];
private HALJoystickPOVs[] m_joystickPOVs = new HALJoystickPOVs[kJoystickPorts];
private HALJoystickButtons[] m_joystickButtons = new HALJoystickButtons[kJoystickPorts];
private MatchInfoData m_matchInfo = new MatchInfoData();
// Joystick Cached Data
private HALJoystickAxes[] m_joystickAxesCache = new HALJoystickAxes[kJoystickPorts];
private HALJoystickPOVs[] m_joystickPOVsCache = new HALJoystickPOVs[kJoystickPorts];
private HALJoystickButtons[] m_joystickButtonsCache = new HALJoystickButtons[kJoystickPorts];
private MatchInfoData m_matchInfoCache = new MatchInfoData();
// Joystick button rising/falling edge flags
HALJoystickButtons[] m_joystickButtonsPressed = new HALJoystickButtons[kJoystickPorts];
@@ -93,7 +100,7 @@ public class DriverStation implements RobotState.Interface {
private Thread m_thread;
private volatile boolean m_threadKeepAlive = true;
private final Object m_joystickMutex;
private final Object m_cacheDataMutex;
// Robot state status variables
private boolean m_userInDisabled = false;
@@ -122,7 +129,7 @@ public class DriverStation implements RobotState.Interface {
* variable.
*/
private DriverStation() {
m_joystickMutex = new Object();
m_cacheDataMutex = new Object();
for (int i = 0; i < kJoystickPorts; i++) {
m_joystickButtons[i] = new HALJoystickButtons();
m_joystickAxes[i] = new HALJoystickAxes(HAL.kMaxJoystickAxes);
@@ -244,7 +251,7 @@ public class DriverStation implements RobotState.Interface {
}
boolean error = false;
boolean retVal = false;
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
@@ -328,7 +335,7 @@ public class DriverStation implements RobotState.Interface {
boolean error = false;
double retVal = 0.0;
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
if (axis >= m_joystickAxes[stick].m_count) {
// set error
error = true;
@@ -358,7 +365,7 @@ public class DriverStation implements RobotState.Interface {
}
boolean error = false;
int retVal = -1;
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
if (pov >= m_joystickPOVs[stick].m_count) {
error = true;
retVal = -1;
@@ -383,7 +390,7 @@ public class DriverStation implements RobotState.Interface {
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
return m_joystickButtons[stick].m_buttons;
}
}
@@ -398,7 +405,7 @@ public class DriverStation implements RobotState.Interface {
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-5");
}
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
return m_joystickAxes[stick].m_count;
}
}
@@ -413,7 +420,7 @@ public class DriverStation implements RobotState.Interface {
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-5");
}
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
return m_joystickPOVs[stick].m_count;
}
}
@@ -428,7 +435,7 @@ public class DriverStation implements RobotState.Interface {
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-5");
}
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
return m_joystickButtons[stick].m_count;
}
}
@@ -445,7 +452,7 @@ public class DriverStation implements RobotState.Interface {
}
boolean error = false;
boolean retVal = false;
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
// TODO: Remove this when calling for descriptor on empty stick no longer
// crashes
if (1 > m_joystickButtons[stick].m_count && 1 > m_joystickAxes[stick].m_count) {
@@ -474,7 +481,7 @@ public class DriverStation implements RobotState.Interface {
}
boolean error = false;
int retVal = -1;
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
// TODO: Remove this when calling for descriptor on empty stick no longer
// crashes
if (1 > m_joystickButtons[stick].m_count && 1 > m_joystickAxes[stick].m_count) {
@@ -503,7 +510,7 @@ public class DriverStation implements RobotState.Interface {
}
boolean error = false;
String retVal = "";
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
// TODO: Remove this when calling for descriptor on empty stick no longer
// crashes
if (1 > m_joystickButtons[stick].m_count && 1 > m_joystickAxes[stick].m_count) {
@@ -533,7 +540,7 @@ public class DriverStation implements RobotState.Interface {
}
int retVal = -1;
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
retVal = HAL.getJoystickAxisType((byte) stick, (byte) axis);
}
return retVal;
@@ -649,6 +656,72 @@ public class DriverStation implements RobotState.Interface {
return HAL.getBrownedOut();
}
/**
* Get the game specific message.
*
* @return the game specific message
*/
public String getGameSpecificMessage() {
synchronized (m_cacheDataMutex) {
return m_matchInfo.gameSpecificMessage;
}
}
/**
* Get the event name.
*
* @return the event name
*/
public String getEventName() {
synchronized (m_cacheDataMutex) {
return m_matchInfo.eventName;
}
}
/**
* Get the match type.
*
* @return the match type
*/
public MatchType getMatchType() {
int matchType;
synchronized (m_cacheDataMutex) {
matchType = m_matchInfo.matchType;
}
switch (matchType) {
case 1:
return MatchType.Practice;
case 2:
return MatchType.Qualification;
case 3:
return MatchType.Elimination;
default:
return MatchType.None;
}
}
/**
* Get the match number.
*
* @return the match number
*/
public int getMatchNumber() {
synchronized (m_cacheDataMutex) {
return m_matchInfo.matchNumber;
}
}
/**
* Get the replay number.
*
* @return the replay number
*/
public int getReplayNumber() {
synchronized (m_cacheDataMutex) {
return m_matchInfo.replayNumber;
}
}
/**
* Get the current alliance from the FMS.
*
@@ -802,11 +875,14 @@ public class DriverStation implements RobotState.Interface {
m_joystickButtonsCache[stick].m_buttons = HAL.getJoystickButtons(stick, m_buttonCountBuffer);
m_joystickButtonsCache[stick].m_count = m_buttonCountBuffer.get(0);
}
HAL.getMatchInfo(m_matchInfoCache);
// Force a control word update, to make sure the data is the newest.
updateControlWord(true);
// lock joystick mutex to swap cache data
synchronized (m_joystickMutex) {
synchronized (m_cacheDataMutex) {
for (int i = 0; i < kJoystickPorts; i++) {
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
m_joystickButtonsPressed[i].m_buttons |=
@@ -829,6 +905,10 @@ public class DriverStation implements RobotState.Interface {
HALJoystickPOVs[] currentPOVs = m_joystickPOVs;
m_joystickPOVs = m_joystickPOVsCache;
m_joystickPOVsCache = currentPOVs;
MatchInfoData currentInfo = m_matchInfo;
m_matchInfo = m_matchInfoCache;
m_matchInfoCache = currentInfo;
}
}

View File

@@ -118,6 +118,8 @@ public class HAL extends JNIWrapper {
public static native boolean getBrownedOut();
public static native int getMatchInfo(MatchInfoData info);
public static native int setErrorData(String error);
public static native int sendError(boolean isError, int errorCode, boolean isLVCode,

View File

@@ -0,0 +1,56 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. 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. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.hal;
/**
* Structure for holding the match info data request.
*/
public class MatchInfoData {
/**
* Stores the event name.
*/
@SuppressWarnings("MemberName")
public String eventName;
/**
* Stores the game specific message.
*/
@SuppressWarnings("MemberName")
public String gameSpecificMessage;
/**
* Stores the match number.
*/
@SuppressWarnings("MemberName")
public int matchNumber;
/**
* Stores the replay number.
*/
@SuppressWarnings("MemberName")
public int replayNumber;
/**
* Stores the match type.
*/
@SuppressWarnings("MemberName")
public int matchType;
/**
* Called from JNI to set the structure data.
*/
@SuppressWarnings("JavadocMethod")
public void setData(String eventName, String gameSpecificMessage,
int matchNumber, int replayNumber, int matchType) {
this.eventName = eventName;
this.gameSpecificMessage = gameSpecificMessage;
this.matchNumber = matchNumber;
this.replayNumber = replayNumber;
this.matchType = matchType;
}
}