Skip to content

Commit a95f79d

Browse files
authored
Merge pull request #201 from robotpy/fix-magicbot-ds
Update magicbot for new DriverStation threading model
2 parents ab6f21d + 49e82ed commit a95f79d

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

magicbot/magicrobot.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def startCompetition(self) -> None:
377377

378378
def endCompetition(self) -> None:
379379
self.__done = True
380+
self._automodes.endCompetition()
380381

381382
def autonomous(self) -> None:
382383
"""
@@ -434,9 +435,17 @@ def _disabled(self) -> None:
434435
self.onException(forceReport=True)
435436
watchdog.addEpoch("disabledInit()")
436437

438+
refreshData = wpilib.DriverStation.refreshData
439+
DSControlWord = wpilib.DSControlWord
440+
437441
with NotifierDelay(self.control_loop_wait_time) as delay:
438-
while not self.__done and self.isDisabled():
439-
if ds_attached != self.__is_ds_attached():
442+
while not self.__done:
443+
refreshData()
444+
cw = DSControlWord()
445+
if cw.isEnabled():
446+
break
447+
448+
if ds_attached != cw.isDSAttached():
440449
ds_attached = not ds_attached
441450
self.__nt_put_is_ds_attached(ds_attached)
442451

@@ -484,9 +493,15 @@ def _operatorControl(self) -> None:
484493
watchdog.addEpoch("teleopInit()")
485494

486495
observe = hal.observeUserProgramTeleop
496+
refreshData = wpilib.DriverStation.refreshData
497+
isTeleopEnabled = wpilib.DriverStation.isTeleopEnabled
487498

488499
with NotifierDelay(self.control_loop_wait_time) as delay:
489-
while not self.__done and self.isTeleopEnabled():
500+
while not self.__done:
501+
refreshData()
502+
if not isTeleopEnabled():
503+
break
504+
490505
observe()
491506
try:
492507
self.teleopPeriodic()
@@ -526,8 +541,16 @@ def _test(self) -> None:
526541
self.onException(forceReport=True)
527542
watchdog.addEpoch("testInit()")
528543

544+
refreshData = wpilib.DriverStation.refreshData
545+
DSControlWord = wpilib.DSControlWord
546+
529547
with NotifierDelay(self.control_loop_wait_time) as delay:
530-
while not self.__done and self.isTest() and self.isEnabled():
548+
while not self.__done:
549+
refreshData()
550+
cw = DSControlWord()
551+
if not (cw.isTest() and cw.isEnabled()):
552+
break
553+
531554
hal.observeUserProgramTest()
532555
try:
533556
self.testPeriodic()

robotpy_ext/autonomous/selector.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def __init__(self, autonomous_pkgname, *args, **kwargs):
8080

8181
self.modes = {}
8282
self.active_mode = None
83+
self.robot_exit = False
8384

8485
logger.info("Begin initializing autonomous mode switcher")
8586

@@ -210,6 +211,10 @@ def __init__(self, autonomous_pkgname, *args, **kwargs):
210211

211212
logger.info("Autonomous switcher initialized")
212213

214+
def endCompetition(self):
215+
"""Call this function when your robot's endCompetition function is called"""
216+
self.robot_exit = True
217+
213218
def run(
214219
self,
215220
control_loop_wait_time: float = 0.020,
@@ -272,10 +277,15 @@ def watchdog_check_expired():
272277
#
273278

274279
observe = hal.observeUserProgramAutonomous
280+
refreshData = wpilib.DriverStation.refreshData
275281
isAutonomousEnabled = wpilib.DriverStation.isAutonomousEnabled
276282

277283
with NotifierDelay(control_loop_wait_time) as delay:
278-
while isAutonomousEnabled():
284+
while not self.robot_exit:
285+
refreshData()
286+
if not isAutonomousEnabled():
287+
break
288+
279289
observe()
280290
try:
281291
self._on_iteration(timer.get())

0 commit comments

Comments
 (0)