Skip to content

Commit 3f1e12c

Browse files
committed
Simulates, but does not update robot position in simulation.
Signed-off-by: Mike Stitt <[email protected]>
1 parent 7042b96 commit 3f1e12c

File tree

1 file changed

+73
-70
lines changed

1 file changed

+73
-70
lines changed

subprojects/robotpy-wpilib/wpilib/iterativerobotpy.py

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import wpilib._impl
55
import wpilib._impl.report_error
66

7+
from wpilib import DriverStation, DSControlWord
8+
from wpilib.shuffleboard import Shuffleboard
9+
710

811
from enum import IntEnum
912

@@ -20,15 +23,15 @@ class IterativeRobotPy(wpilib.RobotBase):
2023

2124
def __init__(self, period: float):
2225
super().__init__()
23-
self._m_word = wpilib.DSControlWord()
24-
self._m_period = period
25-
self._m_watchdog = wpilib.Watchdog(self._m_period, self.printLoopOverrunMessage)
26+
self._word = DSControlWord()
27+
self._periodS = period
28+
self._watchdog = wpilib.Watchdog(self._periodS, self.printLoopOverrunMessage)
2629
self._mode: IterativeRobotMode = IterativeRobotMode.kNone
27-
self._m_lastMode: IterativeRobotMode = IterativeRobotMode.kNone
28-
self._m_ntFlushEnabled: bool = True
29-
self._m_lwEnabledInTest: bool = False
30-
self._m_calledDsConnected: bool = False
31-
self._m_reportedLw: bool = False
30+
self._lastMode: IterativeRobotMode = IterativeRobotMode.kNone
31+
self._ntFlushEnabled: bool = True
32+
self._lwEnabledInTest: bool = False
33+
self._calledDsConnected: bool = False
34+
self._reportedLw: bool = False
3235
self._robotPeriodicHasRun: bool = False
3336
self._simulationPeriodicHasRun: bool = False
3437
self._disabledPeriodicHasRun: bool = False
@@ -104,121 +107,121 @@ def testExit(self):
104107

105108
# todo @Deprecated(forRemoval=true, since="2025")
106109
def setNetworkTablesFlushEnabled(self, enabled: bool):
107-
self._m_ntFlushEnabled = enabled
110+
self._ntFlushEnabled = enabled
108111

109112
def enableLiveWindowInTest(self, testLW: bool):
110113
if self.isTestEnabled():
111114
pass
112115
# todo throw
113116
# throw FRC_MakeError(err::IncompatibleMode,
114117
# "Can't configure test mode while in test mode!")
115-
if not self._m_reportedLw and testLW:
118+
if not self._reportedLw and testLW:
116119
hal.report(
117120
hal.tResourceType.kResourceType_SmartDashboard,
118121
hal.tInstances.kSmartDashboard_LiveWindow,
119122
)
120-
self._m_reportedLw = True
121-
self._m_lwEnabledInTest = testLW
123+
self._reportedLw = True
124+
self._lwEnabledInTest = testLW
122125

123126
def isLiveWindowEnabledInTest(self) -> bool:
124-
return self._m_lwEnabledInTest
127+
return self._lwEnabledInTest
125128

126129
def getPeriod(self) -> float:
127-
return self._m_period
130+
return self._periodS
128131

129132
def loopFunc(self):
130-
wpilib.DriverStation.refreshData()
131-
self._m_watchdog.reset()
132-
133-
self._m_word.refresh() # todo from Java implementation
134-
135-
mode = IterativeRobotMode.kNone
136-
if self._m_word.IsDisabled():
137-
mode = IterativeRobotMode.kDisabled
138-
elif self._m_word.IsAutonomous():
139-
mode = IterativeRobotMode.kAutonomous
140-
elif self._m_word.IsTeleop():
141-
mode = IterativeRobotMode.kTeleop
142-
elif self._m_word.IsTest():
143-
mode = IterativeRobotMode.kTest
144-
145-
if not self._m_calledDsConnected and self._m_word.IsDSAttached():
146-
self._m_calledDsConnected = True
133+
DriverStation.refreshData()
134+
self._watchdog.reset()
135+
136+
self._word = DSControlWord() # todo switch to a version that does refresh()
137+
138+
self._mode = IterativeRobotMode.kNone
139+
if self._word.isDisabled():
140+
self._mode = IterativeRobotMode.kDisabled
141+
elif self._word.isAutonomous():
142+
self._mode = IterativeRobotMode.kAutonomous
143+
elif self._word.isTeleop():
144+
self._mode = IterativeRobotMode.kTeleop
145+
elif self._word.isTest():
146+
self._mode = IterativeRobotMode.kTest
147+
148+
if not self._calledDsConnected and self._word.isDSAttached():
149+
self._calledDsConnected = True
147150
self.driverStationConnected()
148151

149-
# If mode changed, call mode exit and entry functions
150-
if self._m_lastMode != mode:
151-
if self._m_lastMode == IterativeRobotMode.kDisabled:
152+
# If self._mode changed, call self._mode exit and entry functions
153+
if self._lastMode != self._mode:
154+
if self._lastMode == IterativeRobotMode.kDisabled:
152155
self.disabledExit()
153-
elif self._m_lastMode == IterativeRobotMode.kAutonomous:
156+
elif self._lastMode == IterativeRobotMode.kAutonomous:
154157
self.autonomousExit()
155-
elif self._m_lastMode == IterativeRobotMode.kTeleop:
158+
elif self._lastMode == IterativeRobotMode.kTeleop:
156159
self.teleopExit()
157-
elif self._m_lastMode == IterativeRobotMode.kTest:
158-
if self._m_lwEnabledInTest:
160+
elif self._lastMode == IterativeRobotMode.kTest:
161+
if self._lwEnabledInTest:
159162
wpilib.LiveWindow.setEnabled(False)
160-
# todo Shuffleboard::DisableActuatorWidgets()
163+
Shuffleboard.disableActuatorWidgets()
161164
self.testExit()
162165

163-
if mode == IterativeRobotMode.kDisabled:
166+
if self._mode == IterativeRobotMode.kDisabled:
164167
self.disabledInit()
165-
self._m_watchdog.addEpoch("DisabledInit()")
166-
elif mode == IterativeRobotMode.kAutonomous:
168+
self._watchdog.addEpoch("DisabledInit()")
169+
elif self._mode == IterativeRobotMode.kAutonomous:
167170
self.autonomousInit()
168-
self._m_watchdog.addEpoch("AutonomousInit()")
169-
elif mode == IterativeRobotMode.kTeleop:
171+
self._watchdog.addEpoch("AutonomousInit()")
172+
elif self._mode == IterativeRobotMode.kTeleop:
170173
self.teleopInit()
171-
self._m_watchdog.addEpoch("TeleopInit()")
172-
elif mode == IterativeRobotMode.kTest:
173-
if self._m_lwEnabledInTest:
174+
self._watchdog.addEpoch("TeleopInit()")
175+
elif self._mode == IterativeRobotMode.kTest:
176+
if self._lwEnabledInTest:
174177
wpilib.LiveWindow.setEnabled(True)
175-
# todo Shuffleboard::EnableActuatorWidgets()
178+
Shuffleboard.enableActuatorWidgets()
176179
self.testInit()
177-
self._m_watchdog.addEpoch("TestInit()")
178-
self._m_lastMode = mode
180+
self._watchdog.addEpoch("TestInit()")
181+
self._lastMode = self._mode
179182

180183
# Call the appropriate function depending upon the current robot mode
181-
if mode == IterativeRobotMode.kDisabled:
184+
if self._mode == IterativeRobotMode.kDisabled:
182185
hal.observeUserProgramDisabled()
183186
self.disabledPeriodic()
184-
self._m_watchdog.addEpoch("DisabledPeriodic()")
185-
elif mode == IterativeRobotMode.kAutonomous:
187+
self._watchdog.addEpoch("DisabledPeriodic()")
188+
elif self._mode == IterativeRobotMode.kAutonomous:
186189
hal.observeUserProgramAutonomous()
187190
self.autonomousPeriodic()
188-
self._m_watchdog.addEpoch("AutonomousPeriodic()")
189-
elif mode == IterativeRobotMode.kTeleop:
191+
self._watchdog.addEpoch("AutonomousPeriodic()")
192+
elif self._mode == IterativeRobotMode.kTeleop:
190193
hal.observeUserProgramTeleop()
191194
self.teleopPeriodic()
192-
self._m_watchdog.addEpoch("TeleopPeriodic()")
193-
elif mode == IterativeRobotMode.kTest:
195+
self._watchdog.addEpoch("TeleopPeriodic()")
196+
elif self._mode == IterativeRobotMode.kTest:
194197
hal.observeUserProgramTest()
195198
self.testPeriodic()
196-
self._m_watchdog.addEpoch("TestPeriodic()")
199+
self._watchdog.addEpoch("TestPeriodic()")
197200

198201
self.robotPeriodic()
199-
self._m_watchdog.addEpoch("RobotPeriodic()")
202+
self._watchdog.addEpoch("RobotPeriodic()")
200203
#
201204
wpilib.SmartDashboard.updateValues()
202-
self._m_watchdog.addEpoch("SmartDashboard::UpdateValues()")
205+
self._watchdog.addEpoch("SmartDashboard::UpdateValues()")
203206
wpilib.LiveWindow.updateValues()
204-
self._m_watchdog.addEpoch("LiveWindow::UpdateValues()")
205-
# todo Shuffleboard::Update()
206-
self._m_watchdog.addEpoch("Shuffleboard::Update()")
207+
self._watchdog.addEpoch("LiveWindow::UpdateValues()")
208+
Shuffleboard.update()
209+
self._watchdog.addEpoch("Shuffleboard::Update()")
207210
if self.isSimulation():
208211
hal.simPeriodicBefore()
209212
self.simulationPeriodic()
210213
hal.simPeriodicAfter()
211-
self._m_watchdog.addEpoch("SimulationPeriodic()")
214+
self._watchdog.addEpoch("SimulationPeriodic()")
212215

213-
self._m_watchdog.Disable()
216+
self._watchdog.disable()
214217

215218
# // Flush NetworkTables
216-
if self._m_ntFlushEnabled:
219+
if self._ntFlushEnabled:
217220
ntcore.NetworkTableInstance.getDefault().flushLocal()
218221

219222
# Warn on loop time overruns
220-
if self._m_watchdog.IsExpired():
221-
self._m_watchdog.PrintEpochs()
223+
if self._watchdog.isExpired():
224+
self._watchdog.printEpochs()
222225

223226
def printLoopOverrunMessages(self):
224227
# todo ask about this
@@ -230,8 +233,8 @@ def printLoopOverrunMessages(self):
230233
# DriverStation.reportWarning("Loop time of " + m_period + "s overrun\n", false);
231234
# }
232235
wpilib._impl.report_error.reportWarning(
233-
f"Loop time of {self._m_period}s overrun\n", False
236+
f"Loop time of {self._periodS}s overrun\n", False
234237
)
235238

236239
def printWatchdogEpochs(self):
237-
self._m_watchdog.printEpochs()
240+
self._watchdog.printEpochs()

0 commit comments

Comments
 (0)