2222 reportWarning ,
2323)
2424from wpilib .shuffleboard import Shuffleboard
25+ import wpimath .units
2526
2627_kResourceType_SmartDashboard = tResourceType .kResourceType_SmartDashboard
2728_kSmartDashboard_LiveWindow = tInstances .kSmartDashboard_LiveWindow
@@ -35,9 +36,55 @@ class IterativeRobotMode(Enum):
3536 kTest = 4
3637
3738
39+ # todo should this be IterativeRobotPy or IterativeRobotBase (replacing) or IterativeRobotBasePy
3840class IterativeRobotPy (RobotBase ):
41+ """
42+ IterativeRobotPy implements a specific type of robot program framework,
43+ extending the RobotBase class. It provides similar functionality as
44+ IterativeRobotBase does in a python wrapper of C++, but in pure python.
3945
40- def __init__ (self , period : float ):
46+ The IterativeRobotPy class does not implement StartCompetition(), so it
47+ should not be used by teams directly.
48+
49+ This class provides the following functions which are called by the main
50+ loop, StartCompetition(), at the appropriate times:
51+
52+ robotInit() -- provide for initialization at robot power-on
53+
54+ DriverStationConnected() -- provide for initialization the first time the DS
55+ is connected
56+
57+ Init() functions -- each of the following functions is called once when the
58+ appropriate mode is entered:
59+
60+ - disabledInit() -- called each and every time disabled is entered from another mode
61+ - autonomousInit() -- called each and every time autonomous is entered from another mode
62+ - teleopInit() -- called each and every time teleop is entered from another mode
63+ - testInit() -- called each and every time test is entered from another mode
64+
65+ Periodic() functions -- each of these functions is called on an interval:
66+
67+ - robotPeriodic()
68+ - disabledPeriodic()
69+ - autonomousPeriodic()
70+ - teleopPeriodic()
71+ - testPeriodic()
72+
73+ Exit() functions -- each of the following functions is called once when the
74+ appropriate mode is exited:
75+
76+ - disabledExit() -- called each and every time disabled is exited
77+ - autonomousExit() -- called each and every time autonomous is exited
78+ - teleopExit() -- called each and every time teleop is exited
79+ - testExit() -- called each and every time test is exited
80+ """
81+
82+ def __init__ (self , period : wpimath .units .seconds ) -> None :
83+ """
84+ Constructor for IterativeRobotPy.
85+
86+ :param period: period of the main robot periodic loop in seconds.
87+ """
4188 super ().__init__ ()
4289 self ._periodS = period
4390 self .watchdog = Watchdog (self ._periodS , self .printLoopOverrunMessage )
@@ -56,73 +103,200 @@ def __init__(self, period: float):
56103 self ._testPeriodicHasRun : bool = False
57104
58105 def robotInit (self ) -> None :
106+ """
107+ Robot-wide initialization code should go here.
108+
109+ Users should override this method for default Robot-wide initialization
110+ which will be called when the robot is first powered on. It will be called
111+ exactly one time.
112+
113+ Note: This method is functionally identical to the class constructor so
114+ that should be used instead.
115+ """
59116 pass
60117
61118 def driverStationConnected (self ) -> None :
119+ """
120+ Code that needs to know the DS state should go here.
121+
122+ Users should override this method for initialization that needs to occur
123+ after the DS is connected, such as needing the alliance information.
124+ """
62125 pass
63126
64127 def simulationInit (self ) -> None :
128+ """
129+ Robot-wide simulation initialization code should go here.
130+
131+ Users should override this method for default Robot-wide simulation
132+ related initialization which will be called when the robot is first
133+ started. It will be called exactly one time after robotInit is called
134+ only when the robot is in simulation.
135+ """
65136 pass
66137
67138 def disabledInit (self ) -> None :
139+ """
140+ Initialization code for disabled mode should go here.
141+
142+ Users should override this method for initialization code which will be
143+ called each time
144+ the robot enters disabled mode.
145+ """
68146 pass
69147
70148 def autonomousInit (self ) -> None :
149+ """
150+ Initialization code for autonomous mode should go here.
151+
152+ Users should override this method for initialization code which will be
153+ called each time the robot enters autonomous mode.
154+ """
71155 pass
72156
73157 def teleopInit (self ) -> None :
158+ """
159+ Initialization code for teleop mode should go here.
160+
161+ Users should override this method for initialization code which will be
162+ called each time the robot enters teleop mode.
163+ """
74164 pass
75165
76166 def testInit (self ) -> None :
167+ """
168+ Initialization code for test mode should go here.
169+
170+ Users should override this method for initialization code which will be
171+ called each time the robot enters test mode.
172+ """
77173 pass
78174
79175 def robotPeriodic (self ) -> None :
176+ """
177+ Periodic code for all modes should go here.
178+
179+ This function is called each time a new packet is received from the driver
180+ station.
181+ """
80182 if not self ._robotPeriodicHasRun :
81- print (f"Default RobotPeriodic () method...Override me!" )
183+ print (f"Default robotPeriodic () method...Override me!" )
82184 self ._robotPeriodicHasRun = True
83185
186+ # todo why is this _simulationPeriodic in previous code? Can it be simulationPeriodic and still work?
84187 def simulationPeriodic (self ) -> None :
188+ """
189+ Periodic simulation code should go here.
190+
191+ This function is called in a simulated robot after user code executes.
192+ """
85193 if not self ._simulationPeriodicHasRun :
86194 print (f"Default simulationPeriodic() method...Override me!" )
87195 self ._simulationPeriodicHasRun = True
88196
89197 def disabledPeriodic (self ) -> None :
198+ """
199+ Periodic code for disabled mode should go here.
200+
201+ Users should override this method for code which will be called each time a
202+ new packet is received from the driver station and the robot is in disabled
203+ mode.
204+ """
90205 if not self ._disabledPeriodicHasRun :
91206 print (f"Default disabledPeriodic() method...Override me!" )
92207 self ._disabledPeriodicHasRun = True
93208
94209 def autonomousPeriodic (self ) -> None :
210+ """
211+ Periodic code for autonomous mode should go here.
212+
213+ Users should override this method for code which will be called each time a
214+ new packet is received from the driver station and the robot is in
215+ autonomous mode.
216+ """
95217 if not self ._autonomousPeriodicHasRun :
96218 print (f"Default autonomousPeriodic() method...Override me!" )
97219 self ._autonomousPeriodicHasRun = True
98220
99221 def teleopPeriodic (self ) -> None :
222+ """
223+ Periodic code for teleop mode should go here.
224+
225+ Users should override this method for code which will be called each time a
226+ new packet is received from the driver station and the robot is in teleop
227+ mode.
228+ """
100229 if not self ._teleopPeriodicHasRun :
101230 print (f"Default teleopPeriodic() method...Override me!" )
102231 self ._teleopPeriodicHasRun = True
103232
104233 def testPeriodic (self ) -> None :
234+ """
235+ Periodic code for test mode should go here.
236+
237+ Users should override this method for code which will be called each time a
238+ new packet is received from the driver station and the robot is in test
239+ mode.
240+ """
105241 if not self ._testPeriodicHasRun :
106242 print (f"Default testPeriodic() method...Override me!" )
107- self ._teleopPeriodicHasRun = True
243+ self ._testPeriodicHasRun = True
108244
109245 def disabledExit (self ) -> None :
246+ """
247+ Exit code for disabled mode should go here.
248+
249+ Users should override this method for code which will be called each time
250+ the robot exits disabled mode.
251+ """
110252 pass
111253
112254 def autonomousExit (self ) -> None :
255+ """
256+ Exit code for autonomous mode should go here.
257+
258+ Users should override this method for code which will be called each time
259+ the robot exits autonomous mode.
260+ """
113261 pass
114262
115263 def teleopExit (self ) -> None :
264+ """
265+ Exit code for teleop mode should go here.
266+
267+ Users should override this method for code which will be called each time
268+ the robot exits teleop mode.
269+ """
116270 pass
117271
118272 def testExit (self ) -> None :
273+ """
274+ Exit code for test mode should go here.
275+
276+ Users should override this method for code which will be called each time
277+ the robot exits test mode.
278+ """
119279 pass
120280
121- # todo @Deprecated(forRemoval=true, since="2025")
122281 def setNetworkTablesFlushEnabled (self , enabled : bool ) -> None :
282+ """
283+ Enables or disables flushing NetworkTables every loop iteration.
284+ By default, this is enabled.
285+
286+ :deprecated: Deprecated without replacement.
287+
288+ :param enabled: True to enable, false to disable.
289+ """
290+
123291 self ._ntFlushEnabled = enabled
124292
125293 def enableLiveWindowInTest (self , testLW : bool ) -> None :
294+ """
295+ Sets whether LiveWindow operation is enabled during test mode.
296+
297+ :param testLW: True to enable, false to disable. Defaults to false.
298+ @throws if called in test mode.
299+ """
126300 if self .isTestEnabled ():
127301 raise RuntimeError ("Can't configure test mode while in test mode!" )
128302 if not self ._reportedLw and testLW :
@@ -131,12 +305,21 @@ def enableLiveWindowInTest(self, testLW: bool) -> None:
131305 self ._lwEnabledInTest = testLW
132306
133307 def isLiveWindowEnabledInTest (self ) -> bool :
308+ """
309+ Whether LiveWindow operation is enabled during test mode.
310+ """
134311 return self ._lwEnabledInTest
135312
136- def getPeriod (self ) -> float :
313+ def getPeriod (self ) -> wpimath .units .seconds :
314+ """
315+ Gets time period between calls to Periodic() functions.
316+ """
137317 return self ._periodS
138318
139- def loopFunc (self ) -> None :
319+ def _loopFunc (self ) -> None :
320+ """
321+ Loop function.
322+ """
140323 DriverStation .refreshData ()
141324 self .watchdog .reset ()
142325
@@ -186,36 +369,36 @@ def loopFunc(self) -> None:
186369
187370 if self ._mode is IterativeRobotMode .kDisabled :
188371 self .disabledInit ()
189- self .watchdog .addEpoch ("DisabledInit ()" )
372+ self .watchdog .addEpoch ("disabledInit ()" )
190373 elif self ._mode is IterativeRobotMode .kAutonomous :
191374 self .autonomousInit ()
192- self .watchdog .addEpoch ("AutonomousInit ()" )
375+ self .watchdog .addEpoch ("autonomousInit ()" )
193376 elif self ._mode is IterativeRobotMode .kTeleop :
194377 self .teleopInit ()
195- self .watchdog .addEpoch ("TeleopInit ()" )
378+ self .watchdog .addEpoch ("teleopInit ()" )
196379 elif self ._mode is IterativeRobotMode .kTest :
197380 if self ._lwEnabledInTest :
198381 LiveWindow .setEnabled (True )
199382 Shuffleboard .enableActuatorWidgets ()
200383 self .testInit ()
201- self .watchdog .addEpoch ("TestInit ()" )
384+ self .watchdog .addEpoch ("testInit ()" )
202385 """
203386 match self._mode:
204387 case IterativeRobotMode.kDisabled:
205388 self.disabledInit()
206- self._watchdog.addEpoch("DisabledInit ()")
389+ self._watchdog.addEpoch("disabledInit ()")
207390 case IterativeRobotMode.kAutonomous:
208391 self.autonomousInit()
209- self._watchdog.addEpoch("AutonomousInit ()")
392+ self._watchdog.addEpoch("autonomousInit ()")
210393 case IterativeRobotMode.kTeleop:
211394 self.teleopInit()
212- self._watchdog.addEpoch("TeleopInit ()")
395+ self._watchdog.addEpoch("teleopInit ()")
213396 case IterativeRobotMode.kTest:
214397 if self._lwEnabledInTest:
215398 LiveWindow.setEnabled(True)
216399 Shuffleboard.enableActuatorWidgets()
217400 self.testInit()
218- self._watchdog.addEpoch("TestInit ()")
401+ self._watchdog.addEpoch("testInit ()")
219402 """
220403 self ._lastMode = self ._mode
221404
@@ -224,22 +407,22 @@ def loopFunc(self) -> None:
224407 case IterativeRobotMode .kDisabled :
225408 observeUserProgramDisabled ()
226409 self .disabledPeriodic ()
227- self .watchdog .addEpoch ("DisabledPeriodic ()" )
410+ self .watchdog .addEpoch ("disabledPeriodic ()" )
228411 case IterativeRobotMode .kAutonomous :
229412 observeUserProgramAutonomous ()
230413 self .autonomousPeriodic ()
231- self .watchdog .addEpoch ("AutonomousPeriodic ()" )
414+ self .watchdog .addEpoch ("autonomousPeriodic ()" )
232415 case IterativeRobotMode .kTeleop :
233416 observeUserProgramTeleop ()
234417 self .teleopPeriodic ()
235- self .watchdog .addEpoch ("TeleopPeriodic ()" )
418+ self .watchdog .addEpoch ("teleopPeriodic ()" )
236419 case IterativeRobotMode .kTest :
237420 observeUserProgramTest ()
238421 self .testPeriodic ()
239- self .watchdog .addEpoch ("TestPeriodic ()" )
422+ self .watchdog .addEpoch ("testPeriodic ()" )
240423
241424 self .robotPeriodic ()
242- self .watchdog .addEpoch ("RobotPeriodic ()" )
425+ self .watchdog .addEpoch ("robotPeriodic ()" )
243426
244427 SmartDashboard .updateValues ()
245428 self .watchdog .addEpoch ("SmartDashboard::UpdateValues()" )
@@ -270,4 +453,7 @@ def printLoopOverrunMessage(self) -> None:
270453 reportWarning (f"Loop time of { self .watchdog .getTimeout ()} s overrun" , False )
271454
272455 def printWatchdogEpochs (self ) -> None :
456+ """
457+ Prints list of epochs added so far and their times.
458+ """
273459 self .watchdog .printEpochs ()
0 commit comments