mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpilib] Split DriverStation into smaller classes (#8628)
This commit is contained in:
@@ -5,7 +5,9 @@
|
||||
package org.wpilib.snippets.digitalcommunication;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.Alliance;
|
||||
import org.wpilib.driverstation.MatchState;
|
||||
import org.wpilib.driverstation.RobotState;
|
||||
import org.wpilib.framework.TimedRobot;
|
||||
import org.wpilib.hardware.discrete.DigitalOutput;
|
||||
|
||||
@@ -28,22 +30,22 @@ public class Robot extends TimedRobot {
|
||||
@Override
|
||||
public void robotPeriodic() {
|
||||
boolean setAlliance = false;
|
||||
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
|
||||
Optional<Alliance> alliance = MatchState.getAlliance();
|
||||
if (alliance.isPresent()) {
|
||||
setAlliance = alliance.get() == DriverStation.Alliance.RED;
|
||||
setAlliance = alliance.get() == Alliance.RED;
|
||||
}
|
||||
|
||||
// pull alliance port high if on red alliance, pull low if on blue alliance
|
||||
m_allianceOutput.set(setAlliance);
|
||||
|
||||
// pull enabled port high if enabled, low if disabled
|
||||
m_enabledOutput.set(DriverStation.isEnabled());
|
||||
m_enabledOutput.set(RobotState.isEnabled());
|
||||
|
||||
// pull auto port high if in autonomous, low if in teleop (or disabled)
|
||||
m_autonomousOutput.set(DriverStation.isAutonomous());
|
||||
m_autonomousOutput.set(RobotState.isAutonomous());
|
||||
|
||||
// pull alert port high if match time remaining is between 30 and 25 seconds
|
||||
var matchTime = DriverStation.getMatchTime();
|
||||
var matchTime = MatchState.getMatchTime();
|
||||
m_alertOutput.set(matchTime <= 30 && matchTime >= 25);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
package org.wpilib.snippets.i2ccommunication;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.Alliance;
|
||||
import org.wpilib.driverstation.MatchState;
|
||||
import org.wpilib.driverstation.RobotState;
|
||||
import org.wpilib.framework.TimedRobot;
|
||||
import org.wpilib.hardware.bus.I2C;
|
||||
import org.wpilib.hardware.bus.I2C.Port;
|
||||
@@ -51,16 +53,16 @@ public class Robot extends TimedRobot {
|
||||
StringBuilder stateMessage = new StringBuilder(6);
|
||||
|
||||
String allianceString = "U";
|
||||
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
|
||||
Optional<Alliance> alliance = MatchState.getAlliance();
|
||||
if (alliance.isPresent()) {
|
||||
allianceString = alliance.get() == DriverStation.Alliance.RED ? "R" : "B";
|
||||
allianceString = alliance.get() == Alliance.RED ? "R" : "B";
|
||||
}
|
||||
|
||||
stateMessage
|
||||
.append(allianceString)
|
||||
.append(DriverStation.isEnabled() ? "E" : "D")
|
||||
.append(DriverStation.isAutonomous() ? "A" : "T")
|
||||
.append(String.format("%03d", (int) DriverStation.getMatchTime()));
|
||||
.append(RobotState.isEnabled() ? "E" : "D")
|
||||
.append(RobotState.isAutonomous() ? "A" : "T")
|
||||
.append(String.format("%03d", (int) MatchState.getMatchTime()));
|
||||
|
||||
writeString(stateMessage.toString());
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
package org.wpilib.templates.educational;
|
||||
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.RobotState;
|
||||
import org.wpilib.driverstation.internal.DriverStationBackend;
|
||||
import org.wpilib.framework.RobotBase;
|
||||
import org.wpilib.hardware.hal.ControlWord;
|
||||
import org.wpilib.hardware.hal.RobotMode;
|
||||
@@ -36,23 +37,23 @@ public class EducationalRobot extends RobotBase {
|
||||
@Override
|
||||
public void startCompetition() {
|
||||
// Create an opmode per robot mode
|
||||
DriverStation.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
DriverStation.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
DriverStation.addOpMode(RobotMode.TEST, "Test");
|
||||
DriverStation.publishOpModes();
|
||||
RobotState.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
RobotState.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
RobotState.addOpMode(RobotMode.TEST, "Test");
|
||||
RobotState.publishOpModes();
|
||||
|
||||
final ControlWord word = new ControlWord();
|
||||
DriverStationModeThread modeThread = new DriverStationModeThread(word);
|
||||
|
||||
int event = WPIUtilJNI.makeEvent(false, false);
|
||||
|
||||
DriverStation.provideRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.provideRefreshedDataEventHandle(event);
|
||||
|
||||
// Tell the DS that the robot is ready to be enabled
|
||||
DriverStation.observeUserProgramStarting();
|
||||
DriverStationBackend.observeUserProgramStarting();
|
||||
|
||||
while (!Thread.currentThread().isInterrupted() && !m_exit) {
|
||||
DriverStation.refreshControlWordFromCache(word);
|
||||
DriverStationBackend.refreshControlWordFromCache(word);
|
||||
modeThread.inControl(word);
|
||||
if (isDisabled()) {
|
||||
disabled();
|
||||
@@ -93,7 +94,7 @@ public class EducationalRobot extends RobotBase {
|
||||
}
|
||||
}
|
||||
|
||||
DriverStation.removeRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.removeRefreshedDataEventHandle(event);
|
||||
modeThread.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
package org.wpilib.templates.robotbaseskeleton;
|
||||
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.RobotState;
|
||||
import org.wpilib.driverstation.internal.DriverStationBackend;
|
||||
import org.wpilib.framework.RobotBase;
|
||||
import org.wpilib.hardware.hal.ControlWord;
|
||||
import org.wpilib.hardware.hal.RobotMode;
|
||||
@@ -31,23 +32,23 @@ public class Robot extends RobotBase {
|
||||
@Override
|
||||
public void startCompetition() {
|
||||
// Create an opmode per robot mode
|
||||
DriverStation.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
DriverStation.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
DriverStation.addOpMode(RobotMode.TEST, "Test");
|
||||
DriverStation.publishOpModes();
|
||||
RobotState.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
RobotState.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
RobotState.addOpMode(RobotMode.TEST, "Test");
|
||||
RobotState.publishOpModes();
|
||||
|
||||
final ControlWord word = new ControlWord();
|
||||
DriverStationModeThread modeThread = new DriverStationModeThread(word);
|
||||
|
||||
int event = WPIUtilJNI.makeEvent(false, false);
|
||||
|
||||
DriverStation.provideRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.provideRefreshedDataEventHandle(event);
|
||||
|
||||
// Tell the DS that the robot is ready to be enabled
|
||||
DriverStation.observeUserProgramStarting();
|
||||
DriverStationBackend.observeUserProgramStarting();
|
||||
|
||||
while (!Thread.currentThread().isInterrupted() && !m_exit) {
|
||||
DriverStation.refreshControlWordFromCache(word);
|
||||
DriverStationBackend.refreshControlWordFromCache(word);
|
||||
modeThread.inControl(word);
|
||||
if (isDisabled()) {
|
||||
disabled();
|
||||
@@ -88,7 +89,7 @@ public class Robot extends RobotBase {
|
||||
}
|
||||
}
|
||||
|
||||
DriverStation.removeRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.removeRefreshedDataEventHandle(event);
|
||||
modeThread.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
package org.wpilib.templates.romieducational;
|
||||
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.RobotState;
|
||||
import org.wpilib.driverstation.internal.DriverStationBackend;
|
||||
import org.wpilib.framework.RobotBase;
|
||||
import org.wpilib.hardware.hal.ControlWord;
|
||||
import org.wpilib.hardware.hal.RobotMode;
|
||||
@@ -36,23 +37,23 @@ public class EducationalRobot extends RobotBase {
|
||||
@Override
|
||||
public void startCompetition() {
|
||||
// Create an opmode per robot mode
|
||||
DriverStation.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
DriverStation.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
DriverStation.addOpMode(RobotMode.TEST, "Test");
|
||||
DriverStation.publishOpModes();
|
||||
RobotState.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
RobotState.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
RobotState.addOpMode(RobotMode.TEST, "Test");
|
||||
RobotState.publishOpModes();
|
||||
|
||||
final ControlWord word = new ControlWord();
|
||||
DriverStationModeThread modeThread = new DriverStationModeThread(word);
|
||||
|
||||
int event = WPIUtilJNI.makeEvent(false, false);
|
||||
|
||||
DriverStation.provideRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.provideRefreshedDataEventHandle(event);
|
||||
|
||||
// Tell the DS that the robot is ready to be enabled
|
||||
DriverStation.observeUserProgramStarting();
|
||||
DriverStationBackend.observeUserProgramStarting();
|
||||
|
||||
while (!Thread.currentThread().isInterrupted() && !m_exit) {
|
||||
DriverStation.refreshControlWordFromCache(word);
|
||||
DriverStationBackend.refreshControlWordFromCache(word);
|
||||
modeThread.inControl(word);
|
||||
if (isDisabled()) {
|
||||
disabled();
|
||||
@@ -93,7 +94,7 @@ public class EducationalRobot extends RobotBase {
|
||||
}
|
||||
}
|
||||
|
||||
DriverStation.removeRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.removeRefreshedDataEventHandle(event);
|
||||
modeThread.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
package org.wpilib.templates.xrpeducational;
|
||||
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.RobotState;
|
||||
import org.wpilib.driverstation.internal.DriverStationBackend;
|
||||
import org.wpilib.framework.RobotBase;
|
||||
import org.wpilib.hardware.hal.ControlWord;
|
||||
import org.wpilib.hardware.hal.RobotMode;
|
||||
@@ -36,23 +37,23 @@ public class EducationalRobot extends RobotBase {
|
||||
@Override
|
||||
public void startCompetition() {
|
||||
// Create an opmode per robot mode
|
||||
DriverStation.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
DriverStation.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
DriverStation.addOpMode(RobotMode.TEST, "Test");
|
||||
DriverStation.publishOpModes();
|
||||
RobotState.addOpMode(RobotMode.AUTONOMOUS, "Auto");
|
||||
RobotState.addOpMode(RobotMode.TELEOPERATED, "Teleop");
|
||||
RobotState.addOpMode(RobotMode.TEST, "Test");
|
||||
RobotState.publishOpModes();
|
||||
|
||||
final ControlWord word = new ControlWord();
|
||||
DriverStationModeThread modeThread = new DriverStationModeThread(word);
|
||||
|
||||
int event = WPIUtilJNI.makeEvent(false, false);
|
||||
|
||||
DriverStation.provideRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.provideRefreshedDataEventHandle(event);
|
||||
|
||||
// Tell the DS that the robot is ready to be enabled
|
||||
DriverStation.observeUserProgramStarting();
|
||||
DriverStationBackend.observeUserProgramStarting();
|
||||
|
||||
while (!Thread.currentThread().isInterrupted() && !m_exit) {
|
||||
DriverStation.refreshControlWordFromCache(word);
|
||||
DriverStationBackend.refreshControlWordFromCache(word);
|
||||
modeThread.inControl(word);
|
||||
if (isDisabled()) {
|
||||
disabled();
|
||||
@@ -93,7 +94,7 @@ public class EducationalRobot extends RobotBase {
|
||||
}
|
||||
}
|
||||
|
||||
DriverStation.removeRefreshedDataEventHandle(event);
|
||||
DriverStationBackend.removeRefreshedDataEventHandle(event);
|
||||
modeThread.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.junit.jupiter.api.parallel.ResourceLock;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.wpilib.driverstation.DriverStation;
|
||||
import org.wpilib.driverstation.MatchState;
|
||||
import org.wpilib.hardware.hal.AllianceStationID;
|
||||
import org.wpilib.hardware.hal.HAL;
|
||||
import org.wpilib.hardware.hal.RobotMode;
|
||||
@@ -126,7 +126,7 @@ class I2CCommunicationTest {
|
||||
SimHooks.stepTiming(0.02);
|
||||
|
||||
String str = assertTimeoutPreemptively(Duration.ofMillis(20L), () -> m_future.get());
|
||||
String expected = String.format("%03d", (int) DriverStation.getMatchTime());
|
||||
String expected = String.format("%03d", (int) MatchState.getMatchTime());
|
||||
|
||||
assertEquals(expected, str.substring(3));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user