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 Jul 17, 2024
1 parent 57fa388 commit 11bc03e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
18 changes: 9 additions & 9 deletions wpilibc/src/main/native/cpp/TimedRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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();

Expand Down Expand Up @@ -83,15 +92,6 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
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<void()> callback,
units::second_t period, units::second_t offset) {
m_callbacks.emplace(callback, m_startTime, period, offset);
Expand Down
11 changes: 6 additions & 5 deletions wpilibc/src/main/native/include/frc/TimedRobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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().
*/
Expand All @@ -42,18 +47,14 @@ class TimedRobot : public IterativeRobotBase {
*/
void EndCompetition() override;

protected:
/**
* 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.
*
Expand Down
2 changes: 1 addition & 1 deletion wpilibc/src/main/native/include/frc/TimesliceRobot.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace frc {
* boot the roboRIO into safe mode and delete the robot program to recover.
*/
class TimesliceRobot : public TimedRobot {
public:
protected:
/**
* Constructor for TimesliceRobot.
*
Expand Down
5 changes: 5 additions & 0 deletions wpilibc/src/test/native/cpp/TimedRobotTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class MockRobot : public TimedRobot {

MockRobot() : TimedRobot{kPeriod} { m_robotInitCount++; }

void AddPeriodic(std::function<void()> callback, units::second_t period,
units::second_t offset = 0_s) {
TimedRobot::AddPeriodic(std::move(callback), period, offset);
}

void SimulationInit() override { m_simulationInitCount++; }

void DisabledInit() override { m_disabledInitCount++; }
Expand Down
4 changes: 4 additions & 0 deletions wpilibc/src/test/native/cpp/TimesliceRobotTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class MockRobot : public TimesliceRobot {

MockRobot() : TimesliceRobot{2_ms, 5_ms} {}

void Schedule(std::function<void()> func, units::second_t allocation) {
TimesliceRobot::Schedule(std::move(func), allocation);
}

void RobotPeriodic() override { m_robotPeriodicCount++; }
};
} // namespace
Expand Down
42 changes: 21 additions & 21 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,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_startTime = Timer.getFPGATimestamp();
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 @@ -152,6 +133,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_startTime = Timer.getFPGATimestamp();
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 All @@ -161,7 +161,7 @@ public void endCompetition() {
* @param callback The callback to run.
* @param periodSeconds The period at which to run the callback in seconds.
*/
public final void addPeriodic(Runnable callback, double periodSeconds) {
protected final void addPeriodic(Runnable callback, double periodSeconds) {
m_callbacks.add(new Callback(callback, m_startTime, periodSeconds, 0.0));
}

Expand All @@ -176,7 +176,7 @@ public final void addPeriodic(Runnable callback, double periodSeconds) {
* @param offsetSeconds The offset from the common starting time in seconds. This is useful for
* scheduling a callback in a different timeslot relative to TimedRobot.
*/
public final void addPeriodic(Runnable callback, double periodSeconds, double offsetSeconds) {
protected final void addPeriodic(Runnable callback, double periodSeconds, double offsetSeconds) {
m_callbacks.add(new Callback(callback, m_startTime, periodSeconds, offsetSeconds));
}

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 All @@ -95,7 +95,7 @@ public TimesliceRobot(double robotPeriodicAllocation, double controllerPeriod) {
* @param func Function to schedule.
* @param allocation The function's runtime allocation in seconds out of the controller period.
*/
public void schedule(Runnable func, double allocation) {
protected void schedule(Runnable func, double allocation) {
if (m_nextOffset + allocation > m_controllerPeriod) {
throw new IllegalStateException(
"Function scheduled at offset "
Expand Down

0 comments on commit 11bc03e

Please sign in to comment.