Permalink
...
Comparing changes
Open a pull request
- 3 commits
- 3 files changed
- 0 commit comments
- 1 contributor
Unified
Split
Showing
with
26 additions
and 6 deletions.
- +1 −1 wpilib/setup.py
- +7 −5 wpilib/wpilib/motorsafety.py
- +18 −0 wpilib/wpilib/samplerobot.py
View
2
wpilib/setup.py
| @@ -49,7 +49,7 @@ | ||
| url='https://github.com/robotpy/robotpy-wpilib', | ||
| keywords='frc first robotics wpilib', | ||
| packages=find_packages(), | ||
| - install_requires=['pynetworktables>=2015.1.2'], | ||
| + install_requires=['pynetworktables>=2015.2.1'], | ||
| license="BSD License", | ||
| classifiers=[ | ||
| "Development Status :: 5 - Production/Stable", | ||
View
12
wpilib/wpilib/motorsafety.py
| @@ -6,6 +6,7 @@ | ||
| #---------------------------------------------------------------------------- | ||
| import weakref | ||
| +import threading | ||
| from .robotstate import RobotState | ||
| from .timer import Timer | ||
| @@ -27,6 +28,7 @@ class MotorSafety: | ||
| """ | ||
| DEFAULT_SAFETY_EXPIRATION = 0.1 | ||
| helpers = weakref.WeakSet() | ||
| + helpers_lock = threading.Lock() | ||
| def __init__(self): | ||
| """The constructor for a MotorSafety object. | ||
| @@ -39,7 +41,8 @@ def __init__(self): | ||
| self.safetyEnabled = False | ||
| self.safetyExpiration = MotorSafety.DEFAULT_SAFETY_EXPIRATION | ||
| self.safetyStopTime = Timer.getFPGATimestamp() | ||
| - MotorSafety.helpers.add(self) | ||
| + with MotorSafety.helpers_lock: | ||
| + MotorSafety.helpers.add(self) | ||
| def feed(self): | ||
| """Feed the motor safety object. | ||
| @@ -111,7 +114,6 @@ def checkMotors(): | ||
| This static method is called periodically to poll all the motors and | ||
| stop any that have timed out. | ||
| """ | ||
| - # TODO: these should be synchronized with the setting methods | ||
| - # in case it's called from a different thread | ||
| - for msh in MotorSafety.helpers: | ||
| - msh.check() | ||
| + with MotorSafety.helpers_lock: | ||
| + for msh in MotorSafety.helpers: | ||
| + msh.check() | ||
View
18
wpilib/wpilib/samplerobot.py
| @@ -38,6 +38,10 @@ class SampleRobot(RobotBase): | ||
| #: is recommended to use this instead of print statements. | ||
| logger = logging.getLogger("robot") | ||
| + def prestart(self): | ||
| + """Don't immediately say that the robot's ready to be enabled, see below""" | ||
| + pass | ||
| + | ||
| def robotInit(self): | ||
| """Robot-wide initialization code should go here. | ||
| @@ -96,6 +100,14 @@ def robotMain(self): | ||
| called. If it has not been overridden by a user subclass (i.e. the | ||
| default version runs), then the robotInit(), disabled(), autonomous() | ||
| and operatorControl() methods will be called. | ||
| + | ||
| + If you override this function, you must call ``hal.HALNetworkCommunicationObserveUserProgramStarting()`` | ||
| + to indicate that your robot is ready to be enabled, as it will not | ||
| + be called for you. | ||
| + | ||
| + .. warning:: Nobody actually wants to override this function. Neither | ||
| + do you. | ||
| + | ||
| """ | ||
| self._no_robot_main = True | ||
| @@ -117,6 +129,12 @@ def startCompetition(self): | ||
| # first and one-time initialization | ||
| LiveWindow.setEnabled(False) | ||
| self.robotInit() | ||
| + | ||
| + #We call this now (not in prestart like default) so that the robot | ||
| + #won't enable until the initialization has finished. This is useful | ||
| + #because otherwise it's sometimes possible to enable the robot before | ||
| + #the code is ready. | ||
| + hal.HALNetworkCommunicationObserveUserProgramStarting() | ||
| while True: | ||
| if self.isDisabled(): | ||