1- import hal
2- import ntcore
3- import wpilib
4- import wpilib ._impl
5- import wpilib ._impl .report_error
1+ from enum import Enum
62
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
88from wpilib .shuffleboard import Shuffleboard
99
10+ _kResourceType_SmartDashboard = tResourceType .kResourceType_SmartDashboard
11+ _kSmartDashboard_LiveWindow = tInstances .kSmartDashboard_LiveWindow
1012
11- from enum import IntEnum
12-
13-
14- class IterativeRobotMode (IntEnum ):
13+ class IterativeRobotMode (Enum ):
1514 kNone = 0
1615 kDisabled = 1
1716 kAutonomous = 2
1817 kTeleop = 3
1918 kTest = 4
2019
21-
22- class IterativeRobotPy (wpilib .RobotBase ):
20+ class IterativeRobotPy (RobotBase ):
2321
2422 def __init__ (self , period : float ):
2523 super ().__init__ ()
26- self ._word = DSControlWord ()
2724 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 ()
2927 self ._mode : IterativeRobotMode = IterativeRobotMode .kNone
3028 self ._lastMode : IterativeRobotMode = IterativeRobotMode .kNone
3129 self ._ntFlushEnabled : bool = True
@@ -111,15 +109,9 @@ def setNetworkTablesFlushEnabled(self, enabled: bool):
111109
112110 def enableLiveWindowInTest (self , testLW : bool ):
113111 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!" )
118113 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 )
123115 self ._reportedLw = True
124116 self ._lwEnabledInTest = testLW
125117
@@ -133,106 +125,102 @@ def loopFunc(self):
133125 DriverStation .refreshData ()
134126 self ._watchdog .reset ()
135127
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 :
140130 self ._mode = IterativeRobotMode .kDisabled
141- elif self . _word . isAutonomous () :
131+ elif isAutonomous :
142132 self ._mode = IterativeRobotMode .kAutonomous
143- elif self ._word .isTeleop ():
144- self ._mode = IterativeRobotMode .kTeleop
145- elif self ._word .isTest ():
133+ elif isTest :
146134 self ._mode = IterativeRobotMode .kTest
135+ else :
136+ self ._mode = IterativeRobotMode .kTeleop
147137
148- if not self ._calledDsConnected and self . _word .isDSAttached ():
138+ if not self ._calledDsConnected and DSControlWord () .isDSAttached ():
149139 self ._calledDsConnected = True
150140 self .driverStationConnected ()
151141
152142 # 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()" )
181173 self ._lastMode = self ._mode
182174
183175 # 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()" )
200193
201194 self .robotPeriodic ()
202195 self ._watchdog .addEpoch ("RobotPeriodic()" )
203- #
204- wpilib . SmartDashboard .updateValues ()
196+
197+ SmartDashboard .updateValues ()
205198 self ._watchdog .addEpoch ("SmartDashboard::UpdateValues()" )
206- wpilib .LiveWindow .updateValues ()
199+
200+ LiveWindow .updateValues ()
207201 self ._watchdog .addEpoch ("LiveWindow::UpdateValues()" )
202+
208203 Shuffleboard .update ()
209204 self ._watchdog .addEpoch ("Shuffleboard::Update()" )
205+
210206 if self .isSimulation ():
211- hal . simPeriodicBefore ()
207+ simPeriodicBefore ()
212208 self .simulationPeriodic ()
213- hal . simPeriodicAfter ()
209+ simPeriodicAfter ()
214210 self ._watchdog .addEpoch ("SimulationPeriodic()" )
215211
216212 self ._watchdog .disable ()
217213
218- # // Flush NetworkTables
214+ # Flush NetworkTables
219215 if self ._ntFlushEnabled :
220- ntcore . NetworkTableInstance . getDefault () .flushLocal ()
216+ self . _networkTableInstanceDefault .flushLocal ()
221217
222218 # Warn on loop time overruns
223219 if self ._watchdog .isExpired ():
224220 self ._watchdog .printEpochs ()
225221
226222 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 (
236224 f"Loop time of { self ._periodS } s overrun\n " , False
237225 )
238226
0 commit comments