Skip to content

Commit 2f0c2e1

Browse files
committed
Fix up watchdog interface.
Signed-off-by: Mike Stitt <[email protected]>
1 parent 9429b8c commit 2f0c2e1

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

subprojects/robotpy-wpilib/wpilib/iterativerobotpy.py

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IterativeRobotPy(RobotBase):
2222
def __init__(self, period: float):
2323
super().__init__()
2424
self._periodS = period
25-
self._watchdog = Watchdog(self._periodS, self.printLoopOverrunMessage)
25+
self.watchdog = Watchdog(self._periodS, self.printLoopOverrunMessage)
2626
self._networkTableInstanceDefault = NetworkTableInstance.getDefault()
2727
self._mode: IterativeRobotMode = IterativeRobotMode.kNone
2828
self._lastMode: IterativeRobotMode = IterativeRobotMode.kNone
@@ -37,9 +37,6 @@ def __init__(self, period: float):
3737
self._teleopPeriodicHasRun: bool = False
3838
self._testPeriodicHasRun: bool = False
3939

40-
def printLoopOverrunMessage(self):
41-
pass
42-
4340
def robotInit(self):
4441
pass
4542

@@ -123,7 +120,7 @@ def getPeriod(self) -> float:
123120

124121
def loopFunc(self):
125122
DriverStation.refreshData()
126-
self._watchdog.reset()
123+
self.watchdog.reset()
127124

128125
isEnabled, isAutonomous, isTest = self.getControlState()
129126
if not isEnabled:
@@ -141,6 +138,20 @@ def loopFunc(self):
141138

142139
# If self._mode changed, call self._mode exit and entry functions
143140
if self._lastMode is not self._mode:
141+
142+
if self._lastMode is IterativeRobotMode.kDisabled:
143+
self.disabledExit()
144+
elif self._lastMode is IterativeRobotMode.kAutonomous:
145+
self.autonomousExit()
146+
elif self._lastMode is IterativeRobotMode.kTeleop:
147+
self.teleopExit()
148+
elif self._lastMode is IterativeRobotMode.kTest:
149+
if self._lwEnabledInTest:
150+
LiveWindow.setEnabled(False)
151+
Shuffleboard.disableActuatorWidgets()
152+
self.testExit()
153+
"""
154+
todo switch to match statements when we don't build with python 3.9
144155
match self._lastMode:
145156
case IterativeRobotMode.kDisabled:
146157
self.disabledExit()
@@ -153,7 +164,24 @@ def loopFunc(self):
153164
LiveWindow.setEnabled(False)
154165
Shuffleboard.disableActuatorWidgets()
155166
self.testExit()
156-
167+
"""
168+
169+
if self._mode is IterativeRobotMode.kDisabled:
170+
self.disabledInit()
171+
self.watchdog.addEpoch("DisabledInit()")
172+
elif self._mode is IterativeRobotMode.kAutonomous:
173+
self.autonomousInit()
174+
self.watchdog.addEpoch("AutonomousInit()")
175+
elif self._mode is IterativeRobotMode.kTeleop:
176+
self.teleopInit()
177+
self.watchdog.addEpoch("TeleopInit()")
178+
elif self._mode is IterativeRobotMode.kTest:
179+
if self._lwEnabledInTest:
180+
LiveWindow.setEnabled(True)
181+
Shuffleboard.enableActuatorWidgets()
182+
self.testInit()
183+
self.watchdog.addEpoch("TestInit()")
184+
"""
157185
match self._mode:
158186
case IterativeRobotMode.kDisabled:
159187
self.disabledInit()
@@ -170,59 +198,61 @@ def loopFunc(self):
170198
Shuffleboard.enableActuatorWidgets()
171199
self.testInit()
172200
self._watchdog.addEpoch("TestInit()")
201+
"""
173202
self._lastMode = self._mode
174203

175204
# Call the appropriate function depending upon the current robot mode
176205
match self._mode:
177206
case IterativeRobotMode.kDisabled:
178207
observeUserProgramDisabled()
179208
self.disabledPeriodic()
180-
self._watchdog.addEpoch("DisabledPeriodic()")
209+
self.watchdog.addEpoch("DisabledPeriodic()")
181210
case IterativeRobotMode.kAutonomous:
182211
observeUserProgramAutonomous()
183212
self.autonomousPeriodic()
184-
self._watchdog.addEpoch("AutonomousPeriodic()")
213+
self.watchdog.addEpoch("AutonomousPeriodic()")
185214
case IterativeRobotMode.kTeleop:
186215
observeUserProgramTeleop()
187216
self.teleopPeriodic()
188-
self._watchdog.addEpoch("TeleopPeriodic()")
217+
self.watchdog.addEpoch("TeleopPeriodic()")
189218
case IterativeRobotMode.kTest:
190219
observeUserProgramTest()
191220
self.testPeriodic()
192-
self._watchdog.addEpoch("TestPeriodic()")
221+
self.watchdog.addEpoch("TestPeriodic()")
193222

194223
self.robotPeriodic()
195-
self._watchdog.addEpoch("RobotPeriodic()")
224+
self.watchdog.addEpoch("RobotPeriodic()")
196225

197226
SmartDashboard.updateValues()
198-
self._watchdog.addEpoch("SmartDashboard::UpdateValues()")
227+
self.watchdog.addEpoch("SmartDashboard::UpdateValues()")
199228

200229
LiveWindow.updateValues()
201-
self._watchdog.addEpoch("LiveWindow::UpdateValues()")
230+
self.watchdog.addEpoch("LiveWindow::UpdateValues()")
202231

203232
Shuffleboard.update()
204-
self._watchdog.addEpoch("Shuffleboard::Update()")
233+
self.watchdog.addEpoch("Shuffleboard::Update()")
205234

206235
if self.isSimulation():
207236
simPeriodicBefore()
208237
self.simulationPeriodic()
209238
simPeriodicAfter()
210-
self._watchdog.addEpoch("SimulationPeriodic()")
239+
self.watchdog.addEpoch("SimulationPeriodic()")
211240

212-
self._watchdog.disable()
241+
self.watchdog.disable()
213242

214243
# Flush NetworkTables
215244
if self._ntFlushEnabled:
216245
self._networkTableInstanceDefault.flushLocal()
217246

218247
# Warn on loop time overruns
219-
if self._watchdog.isExpired():
220-
self._watchdog.printEpochs()
248+
if self.watchdog.isExpired():
249+
self.printWatchdogEpochs()
221250

222-
def printLoopOverrunMessages(self):
251+
def printLoopOverrunMessage(self):
223252
reportWarning(
224253
f"Loop time of {self._periodS}s overrun\n", False
225254
)
255+
print("IN printLoopOverrunMessage\n\n", flush=True)
226256

227257
def printWatchdogEpochs(self):
228-
self._watchdog.printEpochs()
258+
self.watchdog.printEpochs()

0 commit comments

Comments
 (0)