[wpilib] Add methods to check game and enabled state together (#2661)

This avoids users having to call both IsOperatorControl() and IsEnabled() to figure out if their robot is
enabled and in the teleop state. The expression above involves calling two methods that each have their
own lock.

These new methods should only involve locking one mutex, since only one call is made to HAL_GetControlWord().
This commit is contained in:
Prateek Machiraju
2020-08-29 16:32:19 -04:00
committed by GitHub
parent 5d1220e629
commit 526f26685d
8 changed files with 106 additions and 6 deletions

View File

@@ -641,6 +641,19 @@ public class DriverStation {
}
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* autonomous mode and enabled.
*
* @return True if autonomous should be set and the robot should be enabled.
*/
public boolean isAutonomousEnabled() {
synchronized (m_controlWordMutex) {
updateControlWord(false);
return m_controlWordCache.getAutonomous() && m_controlWordCache.getEnabled();
}
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* operator-controlled mode.
@@ -651,6 +664,20 @@ public class DriverStation {
return !(isAutonomous() || isTest());
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* operator-controller mode and enabled.
*
* @return True if operator-controlled mode should be set and the robot should be enabled.
*/
public boolean isOperatorControlEnabled() {
synchronized (m_controlWordMutex) {
updateControlWord(false);
return !m_controlWordCache.getAutonomous() && !m_controlWordCache.getTest()
&& m_controlWordCache.getEnabled();
}
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in test
* mode.

View File

@@ -211,6 +211,16 @@ public abstract class RobotBase implements AutoCloseable {
return m_ds.isAutonomous();
}
/**
* Determine if the robot is current in Autonomous mode and enabled as determined by
* the field controls.
*
* @return True if the robot is currently operating autonomously while enabled.
*/
public boolean isAutonomousEnabled() {
return m_ds.isAutonomousEnabled();
}
/**
* Determine if the robot is currently in Test mode as determined by the driver
* station.
@@ -231,6 +241,16 @@ public abstract class RobotBase implements AutoCloseable {
return m_ds.isOperatorControl();
}
/**
* Determine if the robot is current in Operator Control mode and enabled as determined by
* the field controls.
*
* @return True if the robot is currently operating in Tele-Op mode while enabled.
*/
public boolean isOperatorControlEnabled() {
return m_ds.isOperatorControlEnabled();
}
/**
* Indicates if new data is available from the driver station.
*