mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpilib] DriverStation: Change alliance station to use optional (#5229)
Many teams have issues trying to read the DS too early. By switching to an optional, we cause teams to check 2 things. Either 1) they explicitly check, and their code is correct, or 2) they just read .value() and their code reboots in a loop. However, because the DS will eventually connect, this 2nd case is ok, and should theoretically be undetectable on the field.
This commit is contained in:
@@ -7,6 +7,7 @@ package edu.wpi.first.wpilibj.examples.digitalcommunication;
|
||||
import edu.wpi.first.wpilibj.DigitalOutput;
|
||||
import edu.wpi.first.wpilibj.DriverStation;
|
||||
import edu.wpi.first.wpilibj.TimedRobot;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is a sample program demonstrating how to communicate to a light controller from the robot
|
||||
@@ -26,8 +27,14 @@ public class Robot extends TimedRobot {
|
||||
|
||||
@Override
|
||||
public void robotPeriodic() {
|
||||
boolean setAlliance = false;
|
||||
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
|
||||
if (alliance.isPresent()) {
|
||||
setAlliance = alliance.get() == DriverStation.Alliance.Red;
|
||||
}
|
||||
|
||||
// pull alliance port high if on red alliance, pull low if on blue alliance
|
||||
m_allianceOutput.set(DriverStation.getAlliance() == DriverStation.Alliance.Red);
|
||||
m_allianceOutput.set(setAlliance);
|
||||
|
||||
// pull enabled port high if enabled, low if disabled
|
||||
m_enabledOutput.set(DriverStation.isEnabled());
|
||||
|
||||
@@ -8,6 +8,7 @@ import edu.wpi.first.wpilibj.DriverStation;
|
||||
import edu.wpi.first.wpilibj.I2C;
|
||||
import edu.wpi.first.wpilibj.I2C.Port;
|
||||
import edu.wpi.first.wpilibj.TimedRobot;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is a sample program demonstrating how to communicate to a light controller from the robot
|
||||
@@ -48,8 +49,14 @@ public class Robot extends TimedRobot {
|
||||
// alliance, enabled in teleop mode, with 43 seconds left in the match.
|
||||
StringBuilder stateMessage = new StringBuilder(6);
|
||||
|
||||
String allianceString = "U";
|
||||
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
|
||||
if (alliance.isPresent()) {
|
||||
allianceString = alliance.get() == DriverStation.Alliance.Red ? "R" : "B";
|
||||
}
|
||||
|
||||
stateMessage
|
||||
.append(DriverStation.getAlliance() == DriverStation.Alliance.Red ? "R" : "B")
|
||||
.append(allianceString)
|
||||
.append(DriverStation.isEnabled() ? "E" : "D")
|
||||
.append(DriverStation.isAutonomous() ? "A" : "T")
|
||||
.append(String.format("%03d", (int) DriverStation.getMatchTime()));
|
||||
|
||||
Reference in New Issue
Block a user