4
4
import wpilib ._impl
5
5
import wpilib ._impl .report_error
6
6
7
+ from wpilib import DriverStation , DSControlWord
8
+ from wpilib .shuffleboard import Shuffleboard
9
+
7
10
8
11
from enum import IntEnum
9
12
@@ -20,15 +23,15 @@ class IterativeRobotPy(wpilib.RobotBase):
20
23
21
24
def __init__ (self , period : float ):
22
25
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 )
26
29
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
32
35
self ._robotPeriodicHasRun : bool = False
33
36
self ._simulationPeriodicHasRun : bool = False
34
37
self ._disabledPeriodicHasRun : bool = False
@@ -104,121 +107,121 @@ def testExit(self):
104
107
105
108
# todo @Deprecated(forRemoval=true, since="2025")
106
109
def setNetworkTablesFlushEnabled (self , enabled : bool ):
107
- self ._m_ntFlushEnabled = enabled
110
+ self ._ntFlushEnabled = enabled
108
111
109
112
def enableLiveWindowInTest (self , testLW : bool ):
110
113
if self .isTestEnabled ():
111
114
pass
112
115
# todo throw
113
116
# throw FRC_MakeError(err::IncompatibleMode,
114
117
# "Can't configure test mode while in test mode!")
115
- if not self ._m_reportedLw and testLW :
118
+ if not self ._reportedLw and testLW :
116
119
hal .report (
117
120
hal .tResourceType .kResourceType_SmartDashboard ,
118
121
hal .tInstances .kSmartDashboard_LiveWindow ,
119
122
)
120
- self ._m_reportedLw = True
121
- self ._m_lwEnabledInTest = testLW
123
+ self ._reportedLw = True
124
+ self ._lwEnabledInTest = testLW
122
125
123
126
def isLiveWindowEnabledInTest (self ) -> bool :
124
- return self ._m_lwEnabledInTest
127
+ return self ._lwEnabledInTest
125
128
126
129
def getPeriod (self ) -> float :
127
- return self ._m_period
130
+ return self ._periodS
128
131
129
132
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
147
150
self .driverStationConnected ()
148
151
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 :
152
155
self .disabledExit ()
153
- elif self ._m_lastMode == IterativeRobotMode .kAutonomous :
156
+ elif self ._lastMode == IterativeRobotMode .kAutonomous :
154
157
self .autonomousExit ()
155
- elif self ._m_lastMode == IterativeRobotMode .kTeleop :
158
+ elif self ._lastMode == IterativeRobotMode .kTeleop :
156
159
self .teleopExit ()
157
- elif self ._m_lastMode == IterativeRobotMode .kTest :
158
- if self ._m_lwEnabledInTest :
160
+ elif self ._lastMode == IterativeRobotMode .kTest :
161
+ if self ._lwEnabledInTest :
159
162
wpilib .LiveWindow .setEnabled (False )
160
- # todo Shuffleboard::DisableActuatorWidgets ()
163
+ Shuffleboard . disableActuatorWidgets ()
161
164
self .testExit ()
162
165
163
- if mode == IterativeRobotMode .kDisabled :
166
+ if self . _mode == IterativeRobotMode .kDisabled :
164
167
self .disabledInit ()
165
- self ._m_watchdog .addEpoch ("DisabledInit()" )
166
- elif mode == IterativeRobotMode .kAutonomous :
168
+ self ._watchdog .addEpoch ("DisabledInit()" )
169
+ elif self . _mode == IterativeRobotMode .kAutonomous :
167
170
self .autonomousInit ()
168
- self ._m_watchdog .addEpoch ("AutonomousInit()" )
169
- elif mode == IterativeRobotMode .kTeleop :
171
+ self ._watchdog .addEpoch ("AutonomousInit()" )
172
+ elif self . _mode == IterativeRobotMode .kTeleop :
170
173
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 :
174
177
wpilib .LiveWindow .setEnabled (True )
175
- # todo Shuffleboard::EnableActuatorWidgets ()
178
+ Shuffleboard . enableActuatorWidgets ()
176
179
self .testInit ()
177
- self ._m_watchdog .addEpoch ("TestInit()" )
178
- self ._m_lastMode = mode
180
+ self ._watchdog .addEpoch ("TestInit()" )
181
+ self ._lastMode = self . _mode
179
182
180
183
# Call the appropriate function depending upon the current robot mode
181
- if mode == IterativeRobotMode .kDisabled :
184
+ if self . _mode == IterativeRobotMode .kDisabled :
182
185
hal .observeUserProgramDisabled ()
183
186
self .disabledPeriodic ()
184
- self ._m_watchdog .addEpoch ("DisabledPeriodic()" )
185
- elif mode == IterativeRobotMode .kAutonomous :
187
+ self ._watchdog .addEpoch ("DisabledPeriodic()" )
188
+ elif self . _mode == IterativeRobotMode .kAutonomous :
186
189
hal .observeUserProgramAutonomous ()
187
190
self .autonomousPeriodic ()
188
- self ._m_watchdog .addEpoch ("AutonomousPeriodic()" )
189
- elif mode == IterativeRobotMode .kTeleop :
191
+ self ._watchdog .addEpoch ("AutonomousPeriodic()" )
192
+ elif self . _mode == IterativeRobotMode .kTeleop :
190
193
hal .observeUserProgramTeleop ()
191
194
self .teleopPeriodic ()
192
- self ._m_watchdog .addEpoch ("TeleopPeriodic()" )
193
- elif mode == IterativeRobotMode .kTest :
195
+ self ._watchdog .addEpoch ("TeleopPeriodic()" )
196
+ elif self . _mode == IterativeRobotMode .kTest :
194
197
hal .observeUserProgramTest ()
195
198
self .testPeriodic ()
196
- self ._m_watchdog .addEpoch ("TestPeriodic()" )
199
+ self ._watchdog .addEpoch ("TestPeriodic()" )
197
200
198
201
self .robotPeriodic ()
199
- self ._m_watchdog .addEpoch ("RobotPeriodic()" )
202
+ self ._watchdog .addEpoch ("RobotPeriodic()" )
200
203
#
201
204
wpilib .SmartDashboard .updateValues ()
202
- self ._m_watchdog .addEpoch ("SmartDashboard::UpdateValues()" )
205
+ self ._watchdog .addEpoch ("SmartDashboard::UpdateValues()" )
203
206
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()" )
207
210
if self .isSimulation ():
208
211
hal .simPeriodicBefore ()
209
212
self .simulationPeriodic ()
210
213
hal .simPeriodicAfter ()
211
- self ._m_watchdog .addEpoch ("SimulationPeriodic()" )
214
+ self ._watchdog .addEpoch ("SimulationPeriodic()" )
212
215
213
- self ._m_watchdog . Disable ()
216
+ self ._watchdog . disable ()
214
217
215
218
# // Flush NetworkTables
216
- if self ._m_ntFlushEnabled :
219
+ if self ._ntFlushEnabled :
217
220
ntcore .NetworkTableInstance .getDefault ().flushLocal ()
218
221
219
222
# 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 ()
222
225
223
226
def printLoopOverrunMessages (self ):
224
227
# todo ask about this
@@ -230,8 +233,8 @@ def printLoopOverrunMessages(self):
230
233
# DriverStation.reportWarning("Loop time of " + m_period + "s overrun\n", false);
231
234
# }
232
235
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
234
237
)
235
238
236
239
def printWatchdogEpochs (self ):
237
- self ._m_watchdog .printEpochs ()
240
+ self ._watchdog .printEpochs ()
0 commit comments