Skip to content

Commit 53a57a5

Browse files
committed
[wpilib] Make robot base class functions protected
Users should be inheriting from these classes instead of directly instantiating them.
1 parent a66fa33 commit 53a57a5

File tree

6 files changed

+67
-65
lines changed

6 files changed

+67
-65
lines changed

wpilibc/src/main/native/cpp/TimedRobot.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
using namespace frc;
1919

20+
TimedRobot::~TimedRobot() {
21+
if (m_notifier != HAL_kInvalidHandle) {
22+
int32_t status = 0;
23+
HAL_StopNotifier(m_notifier, &status);
24+
FRC_ReportError(status, "StopNotifier");
25+
}
26+
}
27+
2028
void TimedRobot::StartCompetition() {
2129
RobotInit();
2230

@@ -76,6 +84,14 @@ void TimedRobot::EndCompetition() {
7684
HAL_StopNotifier(m_notifier, &status);
7785
}
7886

87+
void TimedRobot::AddPeriodic(std::function<void()> callback,
88+
units::second_t period, units::second_t offset) {
89+
m_callbacks.emplace(
90+
callback, m_startTime,
91+
std::chrono::microseconds{static_cast<int64_t>(period.value() * 1e6)},
92+
std::chrono::microseconds{static_cast<int64_t>(offset.value() * 1e6)});
93+
}
94+
7995
TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
8096
m_startTime = std::chrono::microseconds{RobotController::GetFPGATime()};
8197
AddPeriodic([=, this] { LoopFunc(); }, period);
@@ -88,19 +104,3 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
88104
HAL_Report(HALUsageReporting::kResourceType_Framework,
89105
HALUsageReporting::kFramework_Timed);
90106
}
91-
92-
TimedRobot::~TimedRobot() {
93-
if (m_notifier != HAL_kInvalidHandle) {
94-
int32_t status = 0;
95-
HAL_StopNotifier(m_notifier, &status);
96-
FRC_ReportError(status, "StopNotifier");
97-
}
98-
}
99-
100-
void TimedRobot::AddPeriodic(std::function<void()> callback,
101-
units::second_t period, units::second_t offset) {
102-
m_callbacks.emplace(
103-
callback, m_startTime,
104-
std::chrono::microseconds{static_cast<int64_t>(period.value() * 1e6)},
105-
std::chrono::microseconds{static_cast<int64_t>(offset.value() * 1e6)});
106-
}

wpilibc/src/main/native/cpp/TimesliceRobot.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88

99
using namespace frc;
1010

11-
TimesliceRobot::TimesliceRobot(units::second_t robotPeriodicAllocation,
12-
units::second_t controllerPeriod)
13-
: m_nextOffset{robotPeriodicAllocation},
14-
m_controllerPeriod{controllerPeriod} {}
15-
1611
void TimesliceRobot::Schedule(std::function<void()> func,
1712
units::second_t allocation) {
1813
if (m_nextOffset + allocation > m_controllerPeriod) {
@@ -25,3 +20,8 @@ void TimesliceRobot::Schedule(std::function<void()> func,
2520
AddPeriodic(func, m_controllerPeriod, m_nextOffset);
2621
m_nextOffset += allocation;
2722
}
23+
24+
TimesliceRobot::TimesliceRobot(units::second_t robotPeriodicAllocation,
25+
units::second_t controllerPeriod)
26+
: m_nextOffset{robotPeriodicAllocation},
27+
m_controllerPeriod{controllerPeriod} {}

wpilibc/src/main/native/include/frc/TimedRobot.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class TimedRobot : public IterativeRobotBase {
3434
/// Default loop period.
3535
static constexpr auto kDefaultPeriod = 20_ms;
3636

37+
TimedRobot(TimedRobot&&) = default;
38+
TimedRobot& operator=(TimedRobot&&) = default;
39+
40+
~TimedRobot() override;
41+
3742
/**
3843
* Provide an alternate "main loop" via StartCompetition().
3944
*/
@@ -44,18 +49,6 @@ class TimedRobot : public IterativeRobotBase {
4449
*/
4550
void EndCompetition() override;
4651

47-
/**
48-
* Constructor for TimedRobot.
49-
*
50-
* @param period Period.
51-
*/
52-
explicit TimedRobot(units::second_t period = kDefaultPeriod);
53-
54-
TimedRobot(TimedRobot&&) = default;
55-
TimedRobot& operator=(TimedRobot&&) = default;
56-
57-
~TimedRobot() override;
58-
5952
/**
6053
* Add a callback to run at a specific period with a starting time offset.
6154
*
@@ -71,6 +64,14 @@ class TimedRobot : public IterativeRobotBase {
7164
void AddPeriodic(std::function<void()> callback, units::second_t period,
7265
units::second_t offset = 0_s);
7366

67+
protected:
68+
/**
69+
* Constructor for TimedRobot.
70+
*
71+
* @param period Period.
72+
*/
73+
explicit TimedRobot(units::second_t period = kDefaultPeriod);
74+
7475
private:
7576
class Callback {
7677
public:

wpilibc/src/main/native/include/frc/TimesliceRobot.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ namespace frc {
8484
*/
8585
class TimesliceRobot : public TimedRobot {
8686
public:
87-
/**
88-
* Constructor for TimesliceRobot.
89-
*
90-
* @param robotPeriodicAllocation The allocation to give the TimesliceRobot
91-
* periodic functions.
92-
* @param controllerPeriod The controller period. The sum of all scheduler
93-
* allocations should be less than or equal to this
94-
* value.
95-
*/
96-
explicit TimesliceRobot(units::second_t robotPeriodicAllocation,
97-
units::second_t controllerPeriod);
98-
9987
/**
10088
* Schedule a periodic function with the constructor's controller period and
10189
* the given allocation. The function's runtime allocation will be placed
@@ -112,6 +100,19 @@ class TimesliceRobot : public TimedRobot {
112100
*/
113101
void Schedule(std::function<void()> func, units::second_t allocation);
114102

103+
protected:
104+
/**
105+
* Constructor for TimesliceRobot.
106+
*
107+
* @param robotPeriodicAllocation The allocation to give the TimesliceRobot
108+
* periodic functions.
109+
* @param controllerPeriod The controller period. The sum of all scheduler
110+
* allocations should be less than or equal to this
111+
* value.
112+
*/
113+
explicit TimesliceRobot(units::second_t robotPeriodicAllocation,
114+
units::second_t controllerPeriod);
115+
115116
private:
116117
units::second_t m_nextOffset;
117118
units::second_t m_controllerPeriod;

wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,6 @@ public int compareTo(Callback rhs) {
7676

7777
private final PriorityQueue<Callback> m_callbacks = new PriorityQueue<>();
7878

79-
/** Constructor for TimedRobot. */
80-
protected TimedRobot() {
81-
this(kDefaultPeriod);
82-
}
83-
84-
/**
85-
* Constructor for TimedRobot.
86-
*
87-
* @param period Period in seconds.
88-
*/
89-
protected TimedRobot(double period) {
90-
super(period);
91-
m_startTimeUs = RobotController.getFPGATime();
92-
addPeriodic(this::loopFunc, period);
93-
NotifierJNI.setNotifierName(m_notifier, "TimedRobot");
94-
95-
HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed);
96-
}
97-
9879
@Override
9980
public void close() {
10081
NotifierJNI.stopNotifier(m_notifier);
@@ -159,6 +140,25 @@ public void endCompetition() {
159140
NotifierJNI.stopNotifier(m_notifier);
160141
}
161142

143+
/** Constructor for TimedRobot. */
144+
protected TimedRobot() {
145+
this(kDefaultPeriod);
146+
}
147+
148+
/**
149+
* Constructor for TimedRobot.
150+
*
151+
* @param period Period in seconds.
152+
*/
153+
protected TimedRobot(double period) {
154+
super(period);
155+
m_startTimeUs = RobotController.getFPGATime();
156+
addPeriodic(this::loopFunc, period);
157+
NotifierJNI.setNotifierName(m_notifier, "TimedRobot");
158+
159+
HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed);
160+
}
161+
162162
/**
163163
* Add a callback to run at a specific period.
164164
*

wpilibj/src/main/java/edu/wpi/first/wpilibj/TimesliceRobot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class TimesliceRobot extends TimedRobot {
7979
* @param controllerPeriod The controller period in seconds. The sum of all scheduler allocations
8080
* should be less than or equal to this value.
8181
*/
82-
public TimesliceRobot(double robotPeriodicAllocation, double controllerPeriod) {
82+
protected TimesliceRobot(double robotPeriodicAllocation, double controllerPeriod) {
8383
m_nextOffset = robotPeriodicAllocation;
8484
m_controllerPeriod = controllerPeriod;
8585
}

0 commit comments

Comments
 (0)