1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <units/time.h>
#include "frc/RobotBase.h"
#include "frc/Watchdog.h"
namespace frc {
/**
* IterativeRobotBase implements a specific type of robot program framework,
* extending the RobotBase class.
*
* The IterativeRobotBase class does not implement StartCompetition(), so it
* should not be used by teams directly.
*
* This class provides the following functions which are called by the main
* loop, StartCompetition(), at the appropriate times:
*
* RobotInit() -- provide for initialization at robot power-on
*
* DriverStationConnected() -- provide for initialization the first time the DS
* is connected
*
* Init() functions -- each of the following functions is called once when the
* appropriate mode is entered:
*
* \li DisabledInit() -- called each and every time disabled is entered from
* another mode
* \li AutonomousInit() -- called each and every time autonomous is entered from
* another mode
* \li TeleopInit() -- called each and every time teleop is entered from another
* mode
* \li TestInit() -- called each and every time test is entered from another
* mode
*
* Periodic() functions -- each of these functions is called on an interval:
*
* \li RobotPeriodic()
* \li DisabledPeriodic()
* \li AutonomousPeriodic()
* \li TeleopPeriodic()
* \li TestPeriodic()
*
* Exit() functions -- each of the following functions is called once when the
* appropriate mode is exited:
*
* \li DisabledExit() -- called each and every time disabled is exited
* \li AutonomousExit() -- called each and every time autonomous is exited
* \li TeleopExit() -- called each and every time teleop is exited
* \li TestExit() -- called each and every time test is exited
*/
class IterativeRobotBase : public RobotBase {
public:
/**
* Robot-wide initialization code should go here.
*
* Users should override this method for default Robot-wide initialization
* which will be called when the robot is first powered on. It will be called
* exactly one time.
*
* Note: This method is functionally identical to the class constructor so
* that should be used instead.
*/
virtual void RobotInit();
/**
* Code that needs to know the DS state should go here.
*
* Users should override this method for initialization that needs to occur
* after the DS is connected, such as needing the alliance information.
*/
virtual void DriverStationConnected();
/**
* Robot-wide simulation initialization code should go here.
*
* Users should override this method for default Robot-wide simulation
* related initialization which will be called when the robot is first
* started. It will be called exactly one time after RobotInit is called
* only when the robot is in simulation.
*/
virtual void SimulationInit();
/**
* Initialization code for disabled mode should go here.
*
* Users should override this method for initialization code which will be
* called each time
* the robot enters disabled mode.
*/
virtual void DisabledInit();
/**
* Initialization code for autonomous mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters autonomous mode.
*/
virtual void AutonomousInit();
/**
* Initialization code for teleop mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters teleop mode.
*/
virtual void TeleopInit();
/**
* Initialization code for test mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters test mode.
*/
virtual void TestInit();
/**
* Periodic code for all modes should go here.
*
* This function is called each time a new packet is received from the driver
* station.
*/
virtual void RobotPeriodic();
/**
* Periodic simulation code should go here.
*
* This function is called in a simulated robot after user code executes.
*/
virtual void SimulationPeriodic();
/**
* Periodic code for disabled mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in disabled
* mode.
*/
virtual void DisabledPeriodic();
/**
* Periodic code for autonomous mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in
* autonomous mode.
*/
virtual void AutonomousPeriodic();
/**
* Periodic code for teleop mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in teleop
* mode.
*/
virtual void TeleopPeriodic();
/**
* Periodic code for test mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in test
* mode.
*/
virtual void TestPeriodic();
/**
* Exit code for disabled mode should go here.
*
* Users should override this method for code which will be called each time
* the robot exits disabled mode.
*/
virtual void DisabledExit();
/**
* Exit code for autonomous mode should go here.
*
* Users should override this method for code which will be called each time
* the robot exits autonomous mode.
*/
virtual void AutonomousExit();
/**
* Exit code for teleop mode should go here.
*
* Users should override this method for code which will be called each time
* the robot exits teleop mode.
*/
virtual void TeleopExit();
/**
* Exit code for test mode should go here.
*
* Users should override this method for code which will be called each time
* the robot exits test mode.
*/
virtual void TestExit();
/**
* Enables or disables flushing NetworkTables every loop iteration.
* By default, this is enabled.
*
* @param enabled True to enable, false to disable
* @deprecated Deprecated without replacement.
*/
[[deprecated("Deprecated without replacement.")]]
void SetNetworkTablesFlushEnabled(bool enabled);
/**
* Sets whether LiveWindow operation is enabled during test mode.
*
* @param testLW True to enable, false to disable. Defaults to false.
* @throws if called in test mode.
*/
void EnableLiveWindowInTest(bool testLW);
/**
* Whether LiveWindow operation is enabled during test mode.
*/
bool IsLiveWindowEnabledInTest();
/**
* Gets time period between calls to Periodic() functions.
*/
units::second_t GetPeriod() const;
/**
* Prints list of epochs added so far and their times.
*/
void PrintWatchdogEpochs();
/**
* Constructor for IterativeRobotBase.
*
* @param period Period.
*/
explicit IterativeRobotBase(units::second_t period);
~IterativeRobotBase() override = default;
protected:
IterativeRobotBase(IterativeRobotBase&&) = default;
IterativeRobotBase& operator=(IterativeRobotBase&&) = default;
/**
* Loop function.
*/
void LoopFunc();
private:
enum class Mode { kNone, kDisabled, kAutonomous, kTeleop, kTest };
Mode m_lastMode = Mode::kNone;
units::second_t m_period;
Watchdog m_watchdog;
bool m_ntFlushEnabled = true;
bool m_lwEnabledInTest = false;
bool m_calledDsConnected = false;
void PrintLoopOverrunMessage();
};
} // namespace frc