1
- import hal
2
- import ntcore
3
- import wpilib
4
- import wpilib ._impl
5
- import wpilib ._impl .report_error
1
+ from enum import Enum
6
2
7
- from wpilib import DriverStation , DSControlWord
3
+ from hal import report , tResourceType , tInstances , observeUserProgramDisabled , \
4
+ observeUserProgramTest , observeUserProgramAutonomous , \
5
+ observeUserProgramTeleop , simPeriodicBefore , simPeriodicAfter
6
+ from ntcore import NetworkTableInstance
7
+ from wpilib import DriverStation , DSControlWord , Watchdog , LiveWindow , RobotBase , SmartDashboard , reportWarning
8
8
from wpilib .shuffleboard import Shuffleboard
9
9
10
+ _kResourceType_SmartDashboard = tResourceType .kResourceType_SmartDashboard
11
+ _kSmartDashboard_LiveWindow = tInstances .kSmartDashboard_LiveWindow
10
12
11
- from enum import IntEnum
12
-
13
-
14
- class IterativeRobotMode (IntEnum ):
13
+ class IterativeRobotMode (Enum ):
15
14
kNone = 0
16
15
kDisabled = 1
17
16
kAutonomous = 2
18
17
kTeleop = 3
19
18
kTest = 4
20
19
21
-
22
- class IterativeRobotPy (wpilib .RobotBase ):
20
+ class IterativeRobotPy (RobotBase ):
23
21
24
22
def __init__ (self , period : float ):
25
23
super ().__init__ ()
26
- self ._word = DSControlWord ()
27
24
self ._periodS = period
28
- self ._watchdog = wpilib .Watchdog (self ._periodS , self .printLoopOverrunMessage )
25
+ self ._watchdog = Watchdog (self ._periodS , self .printLoopOverrunMessage )
26
+ self ._networkTableInstanceDefault = NetworkTableInstance .getDefault ()
29
27
self ._mode : IterativeRobotMode = IterativeRobotMode .kNone
30
28
self ._lastMode : IterativeRobotMode = IterativeRobotMode .kNone
31
29
self ._ntFlushEnabled : bool = True
@@ -111,15 +109,9 @@ def setNetworkTablesFlushEnabled(self, enabled: bool):
111
109
112
110
def enableLiveWindowInTest (self , testLW : bool ):
113
111
if self .isTestEnabled ():
114
- pass
115
- # todo throw
116
- # throw FRC_MakeError(err::IncompatibleMode,
117
- # "Can't configure test mode while in test mode!")
112
+ raise RuntimeError ("Can't configure test mode while in test mode!" )
118
113
if not self ._reportedLw and testLW :
119
- hal .report (
120
- hal .tResourceType .kResourceType_SmartDashboard ,
121
- hal .tInstances .kSmartDashboard_LiveWindow ,
122
- )
114
+ report (_kResourceType_SmartDashboard , _kSmartDashboard_LiveWindow )
123
115
self ._reportedLw = True
124
116
self ._lwEnabledInTest = testLW
125
117
@@ -133,106 +125,102 @@ def loopFunc(self):
133
125
DriverStation .refreshData ()
134
126
self ._watchdog .reset ()
135
127
136
- self ._word = DSControlWord () # todo switch to a version that does refresh()
137
-
138
- self ._mode = IterativeRobotMode .kNone
139
- if self ._word .isDisabled ():
128
+ isEnabled , isAutonomous , isTest = self .getControlState ()
129
+ if not isEnabled :
140
130
self ._mode = IterativeRobotMode .kDisabled
141
- elif self . _word . isAutonomous () :
131
+ elif isAutonomous :
142
132
self ._mode = IterativeRobotMode .kAutonomous
143
- elif self ._word .isTeleop ():
144
- self ._mode = IterativeRobotMode .kTeleop
145
- elif self ._word .isTest ():
133
+ elif isTest :
146
134
self ._mode = IterativeRobotMode .kTest
135
+ else :
136
+ self ._mode = IterativeRobotMode .kTeleop
147
137
148
- if not self ._calledDsConnected and self . _word .isDSAttached ():
138
+ if not self ._calledDsConnected and DSControlWord () .isDSAttached ():
149
139
self ._calledDsConnected = True
150
140
self .driverStationConnected ()
151
141
152
142
# If self._mode changed, call self._mode exit and entry functions
153
- if self ._lastMode != self ._mode :
154
- if self ._lastMode == IterativeRobotMode .kDisabled :
155
- self .disabledExit ()
156
- elif self ._lastMode == IterativeRobotMode .kAutonomous :
157
- self .autonomousExit ()
158
- elif self ._lastMode == IterativeRobotMode .kTeleop :
159
- self .teleopExit ()
160
- elif self ._lastMode == IterativeRobotMode .kTest :
161
- if self ._lwEnabledInTest :
162
- wpilib .LiveWindow .setEnabled (False )
163
- Shuffleboard .disableActuatorWidgets ()
164
- self .testExit ()
165
-
166
- if self ._mode == IterativeRobotMode .kDisabled :
167
- self .disabledInit ()
168
- self ._watchdog .addEpoch ("DisabledInit()" )
169
- elif self ._mode == IterativeRobotMode .kAutonomous :
170
- self .autonomousInit ()
171
- self ._watchdog .addEpoch ("AutonomousInit()" )
172
- elif self ._mode == IterativeRobotMode .kTeleop :
173
- self .teleopInit ()
174
- self ._watchdog .addEpoch ("TeleopInit()" )
175
- elif self ._mode == IterativeRobotMode .kTest :
176
- if self ._lwEnabledInTest :
177
- wpilib .LiveWindow .setEnabled (True )
178
- Shuffleboard .enableActuatorWidgets ()
179
- self .testInit ()
180
- self ._watchdog .addEpoch ("TestInit()" )
143
+ if self ._lastMode is not self ._mode :
144
+ match self ._lastMode :
145
+ case IterativeRobotMode .kDisabled :
146
+ self .disabledExit ()
147
+ case IterativeRobotMode .kAutonomous :
148
+ self .autonomousExit ()
149
+ case IterativeRobotMode .kTeleop :
150
+ self .teleopExit ()
151
+ case IterativeRobotMode .kTest :
152
+ if self ._lwEnabledInTest :
153
+ LiveWindow .setEnabled (False )
154
+ Shuffleboard .disableActuatorWidgets ()
155
+ self .testExit ()
156
+
157
+ match self ._mode :
158
+ case IterativeRobotMode .kDisabled :
159
+ self .disabledInit ()
160
+ self ._watchdog .addEpoch ("DisabledInit()" )
161
+ case IterativeRobotMode .kAutonomous :
162
+ self .autonomousInit ()
163
+ self ._watchdog .addEpoch ("AutonomousInit()" )
164
+ case IterativeRobotMode .kTeleop :
165
+ self .teleopInit ()
166
+ self ._watchdog .addEpoch ("TeleopInit()" )
167
+ case IterativeRobotMode .kTest :
168
+ if self ._lwEnabledInTest :
169
+ LiveWindow .setEnabled (True )
170
+ Shuffleboard .enableActuatorWidgets ()
171
+ self .testInit ()
172
+ self ._watchdog .addEpoch ("TestInit()" )
181
173
self ._lastMode = self ._mode
182
174
183
175
# Call the appropriate function depending upon the current robot mode
184
- if self ._mode == IterativeRobotMode .kDisabled :
185
- hal .observeUserProgramDisabled ()
186
- self .disabledPeriodic ()
187
- self ._watchdog .addEpoch ("DisabledPeriodic()" )
188
- elif self ._mode == IterativeRobotMode .kAutonomous :
189
- hal .observeUserProgramAutonomous ()
190
- self .autonomousPeriodic ()
191
- self ._watchdog .addEpoch ("AutonomousPeriodic()" )
192
- elif self ._mode == IterativeRobotMode .kTeleop :
193
- hal .observeUserProgramTeleop ()
194
- self .teleopPeriodic ()
195
- self ._watchdog .addEpoch ("TeleopPeriodic()" )
196
- elif self ._mode == IterativeRobotMode .kTest :
197
- hal .observeUserProgramTest ()
198
- self .testPeriodic ()
199
- self ._watchdog .addEpoch ("TestPeriodic()" )
176
+ match self ._mode :
177
+ case IterativeRobotMode .kDisabled :
178
+ observeUserProgramDisabled ()
179
+ self .disabledPeriodic ()
180
+ self ._watchdog .addEpoch ("DisabledPeriodic()" )
181
+ case IterativeRobotMode .kAutonomous :
182
+ observeUserProgramAutonomous ()
183
+ self .autonomousPeriodic ()
184
+ self ._watchdog .addEpoch ("AutonomousPeriodic()" )
185
+ case IterativeRobotMode .kTeleop :
186
+ observeUserProgramTeleop ()
187
+ self .teleopPeriodic ()
188
+ self ._watchdog .addEpoch ("TeleopPeriodic()" )
189
+ case IterativeRobotMode .kTest :
190
+ observeUserProgramTest ()
191
+ self .testPeriodic ()
192
+ self ._watchdog .addEpoch ("TestPeriodic()" )
200
193
201
194
self .robotPeriodic ()
202
195
self ._watchdog .addEpoch ("RobotPeriodic()" )
203
- #
204
- wpilib . SmartDashboard .updateValues ()
196
+
197
+ SmartDashboard .updateValues ()
205
198
self ._watchdog .addEpoch ("SmartDashboard::UpdateValues()" )
206
- wpilib .LiveWindow .updateValues ()
199
+
200
+ LiveWindow .updateValues ()
207
201
self ._watchdog .addEpoch ("LiveWindow::UpdateValues()" )
202
+
208
203
Shuffleboard .update ()
209
204
self ._watchdog .addEpoch ("Shuffleboard::Update()" )
205
+
210
206
if self .isSimulation ():
211
- hal . simPeriodicBefore ()
207
+ simPeriodicBefore ()
212
208
self .simulationPeriodic ()
213
- hal . simPeriodicAfter ()
209
+ simPeriodicAfter ()
214
210
self ._watchdog .addEpoch ("SimulationPeriodic()" )
215
211
216
212
self ._watchdog .disable ()
217
213
218
- # // Flush NetworkTables
214
+ # Flush NetworkTables
219
215
if self ._ntFlushEnabled :
220
- ntcore . NetworkTableInstance . getDefault () .flushLocal ()
216
+ self . _networkTableInstanceDefault .flushLocal ()
221
217
222
218
# Warn on loop time overruns
223
219
if self ._watchdog .isExpired ():
224
220
self ._watchdog .printEpochs ()
225
221
226
222
def printLoopOverrunMessages (self ):
227
- # todo ask about this
228
- # cpp has this as a error, java as a warning, is this the right way to call?
229
- # void IterativeRobotBase::PrintLoopOverrunMessage() {
230
- # FRC_ReportError(err::Error, "Loop time of {:.6f}s overrun", m_period.value())
231
- # }
232
- # private void printLoopOverrunMessage() {
233
- # DriverStation.reportWarning("Loop time of " + m_period + "s overrun\n", false);
234
- # }
235
- wpilib ._impl .report_error .reportWarning (
223
+ reportWarning (
236
224
f"Loop time of { self ._periodS } s overrun\n " , False
237
225
)
238
226
0 commit comments