diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index a26043f8bd..1f9866134b 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -22,6 +22,7 @@ import edu.wpi.first.hal.FRCNetComm.tInstances; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; import edu.wpi.first.networktables.NetworkTableEntry; +import edu.wpi.first.wpilibj.RobotBase; import edu.wpi.first.wpilibj.RobotState; import edu.wpi.first.wpilibj.Sendable; import edu.wpi.first.wpilibj.TimedRobot; @@ -253,6 +254,9 @@ public final class CommandScheduler implements Sendable, AutoCloseable { //Run the periodic method of all registered subsystems. for (Subsystem subsystem : m_subsystems.keySet()) { subsystem.periodic(); + if (RobotBase.isSimulation()) { + subsystem.simulationPeriodic(); + } m_watchdog.addEpoch(subsystem.getClass().getSimpleName() + ".periodic()"); } diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java index 7dc917b3a5..e693d87404 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 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. */ @@ -33,6 +33,14 @@ public interface Subsystem { default void periodic() { } + /** + * This method is called periodically by the {@link CommandScheduler}. Useful for updating + * subsystem-specific state that needs to be maintained for simulations, such as for updating + * {@link edu.wpi.first.wpilibj.simulation} classes and setting simulated sensor readings. + */ + default void simulationPeriodic() { + } + /** * Sets the default {@link Command} of the subsystem. The default command will be * automatically scheduled when no other commands are scheduled that require the subsystem. diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index ffd8599f74..91bf6e5275 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -7,6 +7,7 @@ #include "frc2/command/CommandScheduler.h" +#include #include #include #include @@ -192,6 +193,9 @@ void CommandScheduler::Run() { // Run the periodic method of all registered subsystems. for (auto&& subsystem : m_impl->subsystems) { subsystem.getFirst()->Periodic(); + if (frc::RobotBase::IsSimulation()) { + subsystem.getFirst()->SimulationPeriodic(); + } m_watchdog.AddEpoch("Subsystem Periodic()"); } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp index cccb55bc17..010fcb1c09 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 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. */ @@ -14,6 +14,8 @@ Subsystem::~Subsystem() { void Subsystem::Periodic() {} +void Subsystem::SimulationPeriodic() {} + Command* Subsystem::GetDefaultCommand() const { return CommandScheduler::GetInstance().GetDefaultCommand(this); } diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h b/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h index 687510d588..3f8a278aa7 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 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. */ @@ -48,6 +48,14 @@ class Subsystem { */ virtual void Periodic(); + /** + * This method is called periodically by the CommandScheduler. Useful for + * updating subsystem-specific state that needs to be maintained for + * simulations, such as for updating simulation classes and setting simulated + * sensor readings. + */ + virtual void SimulationPeriodic(); + /** * Sets the default Command of the subsystem. The default command will be * automatically scheduled when no other commands are scheduled that require