From b5fb7386071df4bef8eac1c067851e4b5f17240d Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Tue, 21 May 2024 12:45:36 -0700 Subject: [PATCH] [wpilib] Make robot base class functions protected Users should be inheriting from these classes instead of directly instantiating them. --- wpilibc/src/main/native/cpp/TimedRobot.cpp | 34 ++++++++--------- .../src/main/native/cpp/TimesliceRobot.cpp | 10 ++--- .../src/main/native/include/frc/TimedRobot.h | 25 ++++++------ .../main/native/include/frc/TimesliceRobot.h | 25 ++++++------ .../edu/wpi/first/wpilibj/TimedRobot.java | 38 +++++++++---------- .../edu/wpi/first/wpilibj/TimesliceRobot.java | 2 +- 6 files changed, 68 insertions(+), 66 deletions(-) diff --git a/wpilibc/src/main/native/cpp/TimedRobot.cpp b/wpilibc/src/main/native/cpp/TimedRobot.cpp index 538d1b2cf47..f98dab4552d 100644 --- a/wpilibc/src/main/native/cpp/TimedRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimedRobot.cpp @@ -17,6 +17,15 @@ using namespace frc; +TimedRobot::~TimedRobot() { + int32_t status = 0; + + HAL_StopNotifier(m_notifier, &status); + FRC_ReportError(status, "StopNotifier"); + + HAL_CleanNotifier(m_notifier, &status); +} + void TimedRobot::StartCompetition() { RobotInit(); @@ -76,6 +85,14 @@ void TimedRobot::EndCompetition() { HAL_StopNotifier(m_notifier, &status); } +void TimedRobot::AddPeriodic(std::function callback, + units::second_t period, units::second_t offset) { + m_callbacks.emplace( + callback, m_startTime, + std::chrono::microseconds{static_cast(period.value() * 1e6)}, + std::chrono::microseconds{static_cast(offset.value() * 1e6)}); +} + TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { m_startTime = std::chrono::microseconds{RobotController::GetFPGATime()}; AddPeriodic([=, this] { LoopFunc(); }, period); @@ -88,20 +105,3 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { HAL_Report(HALUsageReporting::kResourceType_Framework, HALUsageReporting::kFramework_Timed); } - -TimedRobot::~TimedRobot() { - int32_t status = 0; - - HAL_StopNotifier(m_notifier, &status); - FRC_ReportError(status, "StopNotifier"); - - HAL_CleanNotifier(m_notifier, &status); -} - -void TimedRobot::AddPeriodic(std::function callback, - units::second_t period, units::second_t offset) { - m_callbacks.emplace( - callback, m_startTime, - std::chrono::microseconds{static_cast(period.value() * 1e6)}, - std::chrono::microseconds{static_cast(offset.value() * 1e6)}); -} diff --git a/wpilibc/src/main/native/cpp/TimesliceRobot.cpp b/wpilibc/src/main/native/cpp/TimesliceRobot.cpp index b817aa97f2e..07a1b6bced2 100644 --- a/wpilibc/src/main/native/cpp/TimesliceRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimesliceRobot.cpp @@ -8,11 +8,6 @@ using namespace frc; -TimesliceRobot::TimesliceRobot(units::second_t robotPeriodicAllocation, - units::second_t controllerPeriod) - : m_nextOffset{robotPeriodicAllocation}, - m_controllerPeriod{controllerPeriod} {} - void TimesliceRobot::Schedule(std::function func, units::second_t allocation) { if (m_nextOffset + allocation > m_controllerPeriod) { @@ -25,3 +20,8 @@ void TimesliceRobot::Schedule(std::function func, AddPeriodic(func, m_controllerPeriod, m_nextOffset); m_nextOffset += allocation; } + +TimesliceRobot::TimesliceRobot(units::second_t robotPeriodicAllocation, + units::second_t controllerPeriod) + : m_nextOffset{robotPeriodicAllocation}, + m_controllerPeriod{controllerPeriod} {} diff --git a/wpilibc/src/main/native/include/frc/TimedRobot.h b/wpilibc/src/main/native/include/frc/TimedRobot.h index 647285474ad..60a9b1cb98f 100644 --- a/wpilibc/src/main/native/include/frc/TimedRobot.h +++ b/wpilibc/src/main/native/include/frc/TimedRobot.h @@ -33,6 +33,11 @@ class TimedRobot : public IterativeRobotBase { /// Default loop period. static constexpr auto kDefaultPeriod = 20_ms; + ~TimedRobot() override; + + TimedRobot(TimedRobot&&) = default; + TimedRobot& operator=(TimedRobot&&) = default; + /** * Provide an alternate "main loop" via StartCompetition(). */ @@ -43,18 +48,6 @@ class TimedRobot : public IterativeRobotBase { */ void EndCompetition() override; - /** - * Constructor for TimedRobot. - * - * @param period Period. - */ - explicit TimedRobot(units::second_t period = kDefaultPeriod); - - ~TimedRobot() override; - - TimedRobot(TimedRobot&&) = default; - TimedRobot& operator=(TimedRobot&&) = default; - /** * Add a callback to run at a specific period with a starting time offset. * @@ -70,6 +63,14 @@ class TimedRobot : public IterativeRobotBase { void AddPeriodic(std::function callback, units::second_t period, units::second_t offset = 0_s); + protected: + /** + * Constructor for TimedRobot. + * + * @param period Period. + */ + explicit TimedRobot(units::second_t period = kDefaultPeriod); + private: class Callback { public: diff --git a/wpilibc/src/main/native/include/frc/TimesliceRobot.h b/wpilibc/src/main/native/include/frc/TimesliceRobot.h index bc045d58b60..3701604e608 100644 --- a/wpilibc/src/main/native/include/frc/TimesliceRobot.h +++ b/wpilibc/src/main/native/include/frc/TimesliceRobot.h @@ -84,18 +84,6 @@ namespace frc { */ class TimesliceRobot : public TimedRobot { public: - /** - * Constructor for TimesliceRobot. - * - * @param robotPeriodicAllocation The allocation to give the TimesliceRobot - * periodic functions. - * @param controllerPeriod The controller period. The sum of all scheduler - * allocations should be less than or equal to this - * value. - */ - explicit TimesliceRobot(units::second_t robotPeriodicAllocation, - units::second_t controllerPeriod); - /** * Schedule a periodic function with the constructor's controller period and * the given allocation. The function's runtime allocation will be placed @@ -112,6 +100,19 @@ class TimesliceRobot : public TimedRobot { */ void Schedule(std::function func, units::second_t allocation); + protected: + /** + * Constructor for TimesliceRobot. + * + * @param robotPeriodicAllocation The allocation to give the TimesliceRobot + * periodic functions. + * @param controllerPeriod The controller period. The sum of all scheduler + * allocations should be less than or equal to this + * value. + */ + explicit TimesliceRobot(units::second_t robotPeriodicAllocation, + units::second_t controllerPeriod); + private: units::second_t m_nextOffset; units::second_t m_controllerPeriod; 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 2251d712d9f..b6cc9826d16 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java @@ -77,25 +77,6 @@ public int compareTo(Callback rhs) { private final PriorityQueue m_callbacks = new PriorityQueue<>(); - /** Constructor for TimedRobot. */ - protected TimedRobot() { - this(kDefaultPeriod); - } - - /** - * Constructor for TimedRobot. - * - * @param period Period in seconds. - */ - protected TimedRobot(double period) { - super(period); - m_startTimeUs = RobotController.getFPGATime(); - addPeriodic(this::loopFunc, period); - NotifierJNI.setNotifierName(m_notifier, "TimedRobot"); - - HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed); - } - @Override public void close() { NotifierJNI.stopNotifier(m_notifier); @@ -160,6 +141,25 @@ public void endCompetition() { NotifierJNI.stopNotifier(m_notifier); } + /** Constructor for TimedRobot. */ + protected TimedRobot() { + this(kDefaultPeriod); + } + + /** + * Constructor for TimedRobot. + * + * @param period Period in seconds. + */ + protected TimedRobot(double period) { + super(period); + m_startTimeUs = RobotController.getFPGATime(); + addPeriodic(this::loopFunc, period); + NotifierJNI.setNotifierName(m_notifier, "TimedRobot"); + + HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed); + } + /** * Add a callback to run at a specific period. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimesliceRobot.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimesliceRobot.java index 5d10bb57eb3..99de18ee3e4 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimesliceRobot.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimesliceRobot.java @@ -79,7 +79,7 @@ public class TimesliceRobot extends TimedRobot { * @param controllerPeriod The controller period in seconds. The sum of all scheduler allocations * should be less than or equal to this value. */ - public TimesliceRobot(double robotPeriodicAllocation, double controllerPeriod) { + protected TimesliceRobot(double robotPeriodicAllocation, double controllerPeriod) { m_nextOffset = robotPeriodicAllocation; m_controllerPeriod = controllerPeriod; }