diff --git a/wpilibc/src/main/native/cpp/TimedRobot.cpp b/wpilibc/src/main/native/cpp/TimedRobot.cpp index 7ff22a9f347..60ddeb22bda 100644 --- a/wpilibc/src/main/native/cpp/TimedRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimedRobot.cpp @@ -17,6 +17,14 @@ using namespace frc; +TimedRobot::~TimedRobot() { + if (m_notifier != HAL_kInvalidHandle) { + int32_t status = 0; + HAL_StopNotifier(m_notifier, &status); + FRC_ReportError(status, "StopNotifier"); + } +} + void TimedRobot::StartCompetition() { RobotInit(); @@ -76,6 +84,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,19 +104,3 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { HAL_Report(HALUsageReporting::kResourceType_Framework, HALUsageReporting::kFramework_Timed); } - -TimedRobot::~TimedRobot() { - if (m_notifier != HAL_kInvalidHandle) { - int32_t status = 0; - HAL_StopNotifier(m_notifier, &status); - FRC_ReportError(status, "StopNotifier"); - } -} - -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 68d5ecf14c5..f2009e7bed2 100644 --- a/wpilibc/src/main/native/include/frc/TimedRobot.h +++ b/wpilibc/src/main/native/include/frc/TimedRobot.h @@ -34,6 +34,11 @@ class TimedRobot : public IterativeRobotBase { /// Default loop period. static constexpr auto kDefaultPeriod = 20_ms; + TimedRobot(TimedRobot&&) = default; + TimedRobot& operator=(TimedRobot&&) = default; + + ~TimedRobot() override; + /** * Provide an alternate "main loop" via StartCompetition(). */ @@ -44,18 +49,6 @@ class TimedRobot : public IterativeRobotBase { */ void EndCompetition() override; - /** - * Constructor for TimedRobot. - * - * @param period Period. - */ - explicit TimedRobot(units::second_t period = kDefaultPeriod); - - TimedRobot(TimedRobot&&) = default; - TimedRobot& operator=(TimedRobot&&) = default; - - ~TimedRobot() override; - /** * Add a callback to run at a specific period with a starting time offset. * @@ -71,6 +64,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 7b59f528a6b..52aa5cd5b24 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java @@ -76,25 +76,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); @@ -159,6 +140,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; }