Skip to content

Commit

Permalink
[wpilib] Make robot base class functions protected
Browse files Browse the repository at this point in the history
Users should be inheriting from these classes instead of directly
instantiating them.
  • Loading branch information
calcmogul committed Sep 13, 2024
1 parent 97c6c86 commit be7b72a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 64 deletions.
30 changes: 15 additions & 15 deletions wpilibc/src/main/native/cpp/TimedRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

using namespace frc;

TimedRobot::~TimedRobot() {
int32_t status = 0;

HAL_StopNotifier(m_notifier, &status);
FRC_ReportError(status, "StopNotifier");
}

void TimedRobot::StartCompetition() {
RobotInit();

Expand Down Expand Up @@ -76,6 +83,14 @@ void TimedRobot::EndCompetition() {
HAL_StopNotifier(m_notifier, &status);
}

void TimedRobot::AddPeriodic(std::function<void()> callback,
units::second_t period, units::second_t offset) {
m_callbacks.emplace(
callback, m_startTime,
std::chrono::microseconds{static_cast<int64_t>(period.value() * 1e6)},
std::chrono::microseconds{static_cast<int64_t>(offset.value() * 1e6)});
}

TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
m_startTime = std::chrono::microseconds{RobotController::GetFPGATime()};
AddPeriodic([=, this] { LoopFunc(); }, period);
Expand All @@ -88,18 +103,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");
}

void TimedRobot::AddPeriodic(std::function<void()> callback,
units::second_t period, units::second_t offset) {
m_callbacks.emplace(
callback, m_startTime,
std::chrono::microseconds{static_cast<int64_t>(period.value() * 1e6)},
std::chrono::microseconds{static_cast<int64_t>(offset.value() * 1e6)});
}
10 changes: 5 additions & 5 deletions wpilibc/src/main/native/cpp/TimesliceRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> func,
units::second_t allocation) {
if (m_nextOffset + allocation > m_controllerPeriod) {
Expand All @@ -25,3 +20,8 @@ void TimesliceRobot::Schedule(std::function<void()> 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} {}
25 changes: 13 additions & 12 deletions wpilibc/src/main/native/include/frc/TimedRobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand All @@ -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.
*
Expand All @@ -71,6 +64,14 @@ class TimedRobot : public IterativeRobotBase {
void AddPeriodic(std::function<void()> 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:
Expand Down
25 changes: 13 additions & 12 deletions wpilibc/src/main/native/include/frc/TimesliceRobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -112,6 +100,19 @@ class TimesliceRobot : public TimedRobot {
*/
void Schedule(std::function<void()> 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;
Expand Down
38 changes: 19 additions & 19 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,6 @@ public int compareTo(Callback rhs) {

private final PriorityQueue<Callback> 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);
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit be7b72a

Please sign in to comment.