[wpilib] Split DriverStation into smaller classes (#8628)

This commit is contained in:
Thad House
2026-04-18 19:56:45 -07:00
committed by GitHub
parent 58ad633ae2
commit ab2aef2c29
108 changed files with 4406 additions and 3211 deletions

View File

@@ -23,7 +23,8 @@ import java.util.WeakHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.wpilib.command2.Command.InterruptionBehavior;
import org.wpilib.driverstation.DriverStation;
import org.wpilib.driverstation.DriverStationErrors;
import org.wpilib.driverstation.RobotState;
import org.wpilib.event.EventLoop;
import org.wpilib.framework.RobotBase;
import org.wpilib.framework.TimedRobot;
@@ -175,7 +176,7 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
*/
private void schedule(Command command) {
if (command == null) {
DriverStation.reportWarning("Tried to schedule a null command", true);
DriverStationErrors.reportWarning("Tried to schedule a null command", true);
return;
}
if (m_inRunLoop) {
@@ -189,7 +190,7 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
// run when disabled, or the command is already scheduled.
if (m_disabled
|| isScheduled(command)
|| DriverStation.isDisabled() && !command.runsWhenDisabled()) {
|| RobotState.isDisabled() && !command.runsWhenDisabled()) {
return;
}
@@ -269,7 +270,7 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
m_watchdog.addEpoch("buttons.run()");
m_inRunLoop = true;
boolean isDisabled = DriverStation.isDisabled();
boolean isDisabled = RobotState.isDisabled();
// Run scheduled commands, remove finished commands.
for (Iterator<Command> iterator = m_scheduledCommands.iterator(); iterator.hasNext(); ) {
Command command = iterator.next();
@@ -337,11 +338,12 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
public void registerSubsystem(Subsystem... subsystems) {
for (Subsystem subsystem : subsystems) {
if (subsystem == null) {
DriverStation.reportWarning("Tried to register a null subsystem", true);
DriverStationErrors.reportWarning("Tried to register a null subsystem", true);
continue;
}
if (m_subsystems.containsKey(subsystem)) {
DriverStation.reportWarning("Tried to register an already-registered subsystem", true);
DriverStationErrors.reportWarning(
"Tried to register an already-registered subsystem", true);
continue;
}
m_subsystems.put(subsystem, null);
@@ -379,11 +381,12 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
*/
public void setDefaultCommand(Subsystem subsystem, Command defaultCommand) {
if (subsystem == null) {
DriverStation.reportWarning("Tried to set a default command for a null subsystem", true);
DriverStationErrors.reportWarning(
"Tried to set a default command for a null subsystem", true);
return;
}
if (defaultCommand == null) {
DriverStation.reportWarning("Tried to set a null default command", true);
DriverStationErrors.reportWarning("Tried to set a null default command", true);
return;
}
@@ -394,7 +397,7 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
}
if (defaultCommand.getInterruptionBehavior() == InterruptionBehavior.kCancelIncoming) {
DriverStation.reportWarning(
DriverStationErrors.reportWarning(
"Registering a non-interruptible default command!\n"
+ "This will likely prevent any other commands from requiring this subsystem.",
true);
@@ -413,7 +416,8 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
*/
public void removeDefaultCommand(Subsystem subsystem) {
if (subsystem == null) {
DriverStation.reportWarning("Tried to remove a default command for a null subsystem", true);
DriverStationErrors.reportWarning(
"Tried to remove a default command for a null subsystem", true);
return;
}
@@ -458,7 +462,7 @@ public final class CommandScheduler implements Sendable, AutoCloseable {
*/
private void cancel(Command command, Optional<Command> interruptor) {
if (command == null) {
DriverStation.reportWarning("Tried to cancel a null command", true);
DriverStationErrors.reportWarning("Tried to cancel a null command", true);
return;
}
if (m_endingCommands.contains(command)) {

View File

@@ -39,7 +39,7 @@ public class WaitUntilCommand extends Command {
* wait.
*
* @param time the match time after which to end, in seconds
* @see org.wpilib.driverstation.DriverStation#getMatchTime()
* @see org.wpilib.driverstation.MatchState#getMatchTime()
*/
public WaitUntilCommand(double time) {
this(() -> Timer.getMatchTime() < time);

View File

@@ -7,8 +7,8 @@ package org.wpilib.command2.button;
import java.util.HashMap;
import java.util.Map;
import org.wpilib.command2.CommandScheduler;
import org.wpilib.driverstation.DriverStation.POVDirection;
import org.wpilib.driverstation.GenericHID;
import org.wpilib.driverstation.POVDirection;
import org.wpilib.event.EventLoop;
import org.wpilib.math.util.Pair;

View File

@@ -6,8 +6,8 @@ package org.wpilib.command2.button;
import static org.wpilib.util.ErrorMessages.requireNonNullParam;
import org.wpilib.driverstation.DriverStation.POVDirection;
import org.wpilib.driverstation.GenericHID;
import org.wpilib.driverstation.POVDirection;
/**
* A {@link Trigger} that gets its state from a POV on a {@link GenericHID}.

View File

@@ -4,7 +4,7 @@
package org.wpilib.command2.button;
import org.wpilib.driverstation.DriverStation;
import org.wpilib.driverstation.RobotState;
/**
* A class containing static {@link Trigger} factories for running callbacks when the robot mode
@@ -20,7 +20,7 @@ public final class RobotModeTriggers {
* @return A trigger that is true when the robot is enabled in autonomous mode.
*/
public static Trigger autonomous() {
return new Trigger(DriverStation::isAutonomousEnabled);
return new Trigger(RobotState::isAutonomousEnabled);
}
/**
@@ -29,7 +29,7 @@ public final class RobotModeTriggers {
* @return A trigger that is true when the robot is enabled in teleop mode.
*/
public static Trigger teleop() {
return new Trigger(DriverStation::isTeleopEnabled);
return new Trigger(RobotState::isTeleopEnabled);
}
/**
@@ -38,7 +38,7 @@ public final class RobotModeTriggers {
* @return A trigger that is true when the robot is disabled.
*/
public static Trigger disabled() {
return new Trigger(DriverStation::isDisabled);
return new Trigger(RobotState::isDisabled);
}
/**
@@ -47,6 +47,6 @@ public final class RobotModeTriggers {
* @return A trigger that is true when the robot is enabled in test mode.
*/
public static Trigger test() {
return new Trigger(DriverStation::isTestEnabled);
return new Trigger(RobotState::isTestEnabled);
}
}

View File

@@ -12,7 +12,7 @@
#include "wpi/commands2/CommandPtr.hpp"
#include "wpi/commands2/Subsystem.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/RobotState.hpp"
#include "wpi/framework/RobotBase.hpp"
#include "wpi/framework/TimedRobot.hpp"
#include "wpi/hal/UsageReporting.hpp"
@@ -99,7 +99,7 @@ void CommandScheduler::Schedule(Command* command) {
RequireUngrouped(command);
if (m_impl->disabled || m_impl->scheduledCommands.contains(command) ||
(wpi::DriverStation::IsDisabled() && !command->RunsWhenDisabled())) {
(wpi::RobotState::IsDisabled() && !command->RunsWhenDisabled())) {
return;
}
@@ -181,7 +181,7 @@ void CommandScheduler::Run() {
loopCache->Poll();
m_watchdog.AddEpoch("buttons.Run()");
bool isDisabled = wpi::DriverStation::IsDisabled();
bool isDisabled = wpi::RobotState::IsDisabled();
// create a new set to avoid iterator invalidation.
for (Command* command : wpi::util::SmallSet(m_impl->scheduledCommands)) {
if (!IsScheduled(command)) {

View File

@@ -16,51 +16,51 @@ Trigger CommandGenericHID::Button(int button, wpi::EventLoop* loop) const {
return Trigger(loop, [this, button] { return m_hid.GetRawButton(button); });
}
Trigger CommandGenericHID::POV(wpi::DriverStation::POVDirection angle,
Trigger CommandGenericHID::POV(wpi::POVDirection angle,
wpi::EventLoop* loop) const {
return POV(0, angle, loop);
}
Trigger CommandGenericHID::POV(int pov, wpi::DriverStation::POVDirection angle,
Trigger CommandGenericHID::POV(int pov, wpi::POVDirection angle,
wpi::EventLoop* loop) const {
return Trigger(loop,
[this, pov, angle] { return m_hid.GetPOV(pov) == angle; });
}
Trigger CommandGenericHID::POVUp(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::UP, loop);
return POV(wpi::POVDirection::UP, loop);
}
Trigger CommandGenericHID::POVUpRight(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::UP_RIGHT, loop);
return POV(wpi::POVDirection::UP_RIGHT, loop);
}
Trigger CommandGenericHID::POVRight(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::RIGHT, loop);
return POV(wpi::POVDirection::RIGHT, loop);
}
Trigger CommandGenericHID::POVDownRight(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::DOWN_RIGHT, loop);
return POV(wpi::POVDirection::DOWN_RIGHT, loop);
}
Trigger CommandGenericHID::POVDown(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::DOWN, loop);
return POV(wpi::POVDirection::DOWN, loop);
}
Trigger CommandGenericHID::POVDownLeft(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::DOWN_LEFT, loop);
return POV(wpi::POVDirection::DOWN_LEFT, loop);
}
Trigger CommandGenericHID::POVLeft(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::LEFT, loop);
return POV(wpi::POVDirection::LEFT, loop);
}
Trigger CommandGenericHID::POVUpLeft(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::UP_LEFT, loop);
return POV(wpi::POVDirection::UP_LEFT, loop);
}
Trigger CommandGenericHID::POVCenter(wpi::EventLoop* loop) const {
return POV(wpi::DriverStation::POVDirection::CENTER, loop);
return POV(wpi::POVDirection::CENTER, loop);
}
Trigger CommandGenericHID::AxisLessThan(int axis, double threshold,

View File

@@ -4,22 +4,22 @@
#include "wpi/commands2/button/RobotModeTriggers.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/RobotState.hpp"
using namespace wpi::cmd;
Trigger RobotModeTriggers::Autonomous() {
return Trigger{&wpi::DriverStation::IsAutonomousEnabled};
return Trigger{&wpi::RobotState::IsAutonomousEnabled};
}
Trigger RobotModeTriggers::Teleop() {
return Trigger{&wpi::DriverStation::IsTeleopEnabled};
return Trigger{&wpi::RobotState::IsTeleopEnabled};
}
Trigger RobotModeTriggers::Disabled() {
return Trigger{&wpi::DriverStation::IsDisabled};
return Trigger{&wpi::RobotState::IsDisabled};
}
Trigger RobotModeTriggers::Test() {
return Trigger{&wpi::DriverStation::IsTestEnabled};
return Trigger{&wpi::RobotState::IsTestEnabled};
}

View File

@@ -6,8 +6,9 @@
#include "wpi/commands2/CommandScheduler.hpp"
#include "wpi/commands2/button/Trigger.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/POVDirection.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
namespace wpi::cmd {
@@ -56,7 +57,7 @@ class CommandGenericHID {
* @param angle POV angle.
* @return a Trigger instance based around this angle of a POV on the HID.
*/
Trigger POV(wpi::DriverStation::POVDirection angle,
Trigger POV(wpi::POVDirection angle,
wpi::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
@@ -70,7 +71,7 @@ class CommandGenericHID {
* @param angle POV angle.
* @return a Trigger instance based around this angle of a POV on the HID.
*/
Trigger POV(int pov, wpi::DriverStation::POVDirection angle,
Trigger POV(int pov, wpi::POVDirection angle,
wpi::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;

View File

@@ -5,8 +5,8 @@
#pragma once
#include "wpi/commands2/button/Trigger.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/POVDirection.hpp"
namespace wpi::cmd {
/**
@@ -26,7 +26,7 @@ class POVButton : public Trigger {
* @param angle The angle of the POV corresponding to a button press.
* @param povNumber The number of the POV on the joystick.
*/
POVButton(wpi::GenericHID* joystick, wpi::DriverStation::POVDirection angle,
POVButton(wpi::GenericHID* joystick, wpi::POVDirection angle,
int povNumber = 0)
: Trigger([joystick, angle, povNumber] {
return joystick->GetPOV(povNumber) == angle;

View File

@@ -9,7 +9,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Union
import hal
from typing_extensions import Self
from wpilib import (
DriverStation,
RobotState,
EventLoop,
RobotBase,
TimedRobot,
@@ -192,7 +192,7 @@ class CommandScheduler(Sendable):
if self.isScheduled(command):
return
if DriverStation.isDisabled() and not command.runsWhenDisabled():
if RobotState.isDisabled() and not command.runsWhenDisabled():
return
requirements = command.getRequirements()
@@ -250,7 +250,7 @@ class CommandScheduler(Sendable):
self._watchdog.addEpoch("buttons.run()")
self._inRunLoop = True
isDisabled = DriverStation.isDisabled()
isDisabled = RobotState.isDisabled()
# Run scheduled commands, remove finished commands.
for command in self._scheduledCommands.copy():

View File

@@ -10,7 +10,7 @@ import static org.mockito.Mockito.when;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.wpilib.command2.Command.InterruptionBehavior;
import org.wpilib.driverstation.DriverStation;
import org.wpilib.driverstation.RobotState;
import org.wpilib.simulation.DriverStationSim;
/** Basic setup for all {@link Command tests}. */
@@ -33,7 +33,7 @@ public class CommandTestBase {
DriverStationSim.setEnabled(enabled);
DriverStationSim.notifyNewData();
while (DriverStation.isEnabled() != enabled) {
while (RobotState.isEnabled() != enabled) {
try {
Thread.sleep(1);
} catch (InterruptedException exception) {

View File

@@ -10,8 +10,8 @@
#include "wpi/commands2/CommandScheduler.hpp"
#include "wpi/commands2/RunCommand.hpp"
#include "wpi/commands2/WaitUntilCommand.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/Joystick.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/simulation/JoystickSim.hpp"
using namespace wpi::cmd;
@@ -19,7 +19,7 @@ class POVButtonTest : public CommandTestBase {};
TEST_F(POVButtonTest, SetPOV) {
wpi::sim::JoystickSim joysim(1);
joysim.SetPOV(wpi::DriverStation::POVDirection::UP);
joysim.SetPOV(wpi::POVDirection::UP);
joysim.NotifyNewData();
auto& scheduler = CommandScheduler::GetInstance();
@@ -28,11 +28,11 @@ TEST_F(POVButtonTest, SetPOV) {
WaitUntilCommand command([&finished] { return finished; });
wpi::Joystick joy(1);
POVButton(&joy, wpi::DriverStation::POVDirection::RIGHT).OnTrue(&command);
POVButton(&joy, wpi::POVDirection::RIGHT).OnTrue(&command);
scheduler.Run();
EXPECT_FALSE(scheduler.IsScheduled(&command));
joysim.SetPOV(wpi::DriverStation::POVDirection::RIGHT);
joysim.SetPOV(wpi::POVDirection::RIGHT);
joysim.NotifyNewData();
scheduler.Run();

View File

@@ -6,7 +6,7 @@
#include "../CommandTestBase.hpp"
#include "wpi/commands2/button/Trigger.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/simulation/DriverStationSim.hpp"