diff --git a/wpilibc/src/main/native/cpp/IterativeRobot.cpp b/wpilibc/src/main/native/cpp/IterativeRobot.cpp index c8664a54fc..a81601fecd 100644 --- a/wpilibc/src/main/native/cpp/IterativeRobot.cpp +++ b/wpilibc/src/main/native/cpp/IterativeRobot.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-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. */ @@ -24,6 +24,10 @@ IterativeRobot::IterativeRobot() : IterativeRobotBase(kPacketPeriod) { void IterativeRobot::StartCompetition() { RobotInit(); + if (IsSimulation()) { + SimulationInit(); + } + // Tell the DS that the robot is ready to be enabled HAL_ObserveUserProgramStarting(); diff --git a/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp b/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp index 2997234f11..c51bb2cec2 100644 --- a/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp +++ b/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-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. */ @@ -34,6 +34,10 @@ void IterativeRobotBase::RobotInit() { wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; } +void IterativeRobotBase::SimulationInit() { + wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; +} + void IterativeRobotBase::DisabledInit() { wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; } @@ -58,6 +62,14 @@ void IterativeRobotBase::RobotPeriodic() { } } +void IterativeRobotBase::SimulationPeriodic() { + static bool firstRun = true; + if (firstRun) { + wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; + firstRun = false; + } +} + void IterativeRobotBase::DisabledPeriodic() { static bool firstRun = true; if (firstRun) { @@ -161,6 +173,12 @@ void IterativeRobotBase::LoopFunc() { m_watchdog.AddEpoch("LiveWindow::UpdateValues()"); Shuffleboard::Update(); m_watchdog.AddEpoch("Shuffleboard::Update()"); + + if (IsSimulation()) { + SimulationPeriodic(); + m_watchdog.AddEpoch("SimulationPeriodic()"); + } + m_watchdog.Disable(); // Warn on loop time overruns diff --git a/wpilibc/src/main/native/cpp/TimedRobot.cpp b/wpilibc/src/main/native/cpp/TimedRobot.cpp index a9358dde31..7daa833b7d 100644 --- a/wpilibc/src/main/native/cpp/TimedRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimedRobot.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-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. */ @@ -24,6 +24,10 @@ using namespace frc; void TimedRobot::StartCompetition() { RobotInit(); + if (IsSimulation()) { + SimulationInit(); + } + // Tell the DS that the robot is ready to be enabled HAL_ObserveUserProgramStarting(); diff --git a/wpilibc/src/main/native/include/frc/IterativeRobotBase.h b/wpilibc/src/main/native/include/frc/IterativeRobotBase.h index ff159c49df..24933b6190 100644 --- a/wpilibc/src/main/native/include/frc/IterativeRobotBase.h +++ b/wpilibc/src/main/native/include/frc/IterativeRobotBase.h @@ -61,6 +61,16 @@ class IterativeRobotBase : public RobotBase { */ virtual void RobotInit(); + /** + * Robot-wide simulation initialization code should go here. + * + * Users should override this method for default Robot-wide simulation + * related initialization which will be called when the robot is first + * started. It will be called exactly one time after RobotInit is called + * only when the robot is in simulation. + */ + virtual void SimulationInit(); + /** * Initialization code for disabled mode should go here. * @@ -102,6 +112,13 @@ class IterativeRobotBase : public RobotBase { */ virtual void RobotPeriodic(); + /** + * Periodic simulation code should go here. + * + * This function is called in a simulated robot after user code executes. + */ + virtual void SimulationPeriodic(); + /** * Periodic code for disabled mode should go here. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobot.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobot.java index f1baefb25a..66079a8b0e 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobot.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobot.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-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. */ @@ -43,6 +43,10 @@ public class IterativeRobot extends IterativeRobotBase { public void startCompetition() { robotInit(); + if (isSimulation()) { + simulationInit(); + } + // Tell the DS that the robot is ready to be enabled HAL.observeUserProgramStarting(); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java index b9a7a6a7ad..974c529534 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-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. */ @@ -89,6 +89,18 @@ public abstract class IterativeRobotBase extends RobotBase { System.out.println("Default robotInit() method... Override me!"); } + /** + * Robot-wide simulation initialization code should go here. + * + *
Users should override this method for default Robot-wide simulation + * related initialization which will be called when the robot is first + * started. It will be called exactly one time after RobotInit is called + * only when the robot is in simulation. + */ + public void simulationInit() { + System.out.println("Default simulationInit() method... Override me!"); + } + /** * Initialization code for disabled mode should go here. * @@ -144,6 +156,20 @@ public abstract class IterativeRobotBase extends RobotBase { } } + private boolean m_spFirstRun = true; + + /** + * Periodic simulation code should go here. + * + *
This function is called in a simulated robot after user code executes. + */ + public void simulationPeriodic() { + if (m_spFirstRun) { + System.out.println("Default simulationPeriodic() method... Override me!"); + m_spFirstRun = false; + } + } + private boolean m_dpFirstRun = true; /** @@ -193,6 +219,7 @@ public abstract class IterativeRobotBase extends RobotBase { } } + @SuppressWarnings("PMD.CyclomaticComplexity") protected void loopFunc() { m_watchdog.reset(); @@ -264,6 +291,12 @@ public abstract class IterativeRobotBase extends RobotBase { m_watchdog.addEpoch("LiveWindow.updateValues()"); Shuffleboard.update(); m_watchdog.addEpoch("Shuffleboard.update()"); + + if (isSimulation()) { + simulationPeriodic(); + m_watchdog.addEpoch("simulationPeriodic()"); + } + m_watchdog.disable(); // Warn on loop time overruns diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java index b6b6b25d41..63952896cf 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-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. */ @@ -63,6 +63,10 @@ public class TimedRobot extends IterativeRobotBase { public void startCompetition() { robotInit(); + if (isSimulation()) { + simulationInit(); + } + // Tell the DS that the robot is ready to be enabled HAL.observeUserProgramStarting();