Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.ball_intake.run()
except:
self.onException()
# and so on...
:param forceReport: Always report the exception to the DS. Don't
set this to True
"""
# If the FMS is not attached, crash the robot program
if not self.ds.isFMSAttached():
raise
# Otherwise, if the FMS is attached then try to report the error via
# the driver station console. Maybe.
now = wpilib.Timer.getFPGATimestamp()
try:
if (
forceReport
or (now - self.__last_error_report) > self.error_report_interval
):
wpilib.DriverStation.reportError("Unexpected exception", True)
except:
pass # ok, can't do anything here
self.__last_error_report = now
import math
import wpilib
_getFPGATimestamp = wpilib.Timer.getFPGATimestamp
class LoopTimer:
"""
A utility class that measures the number of loops that a robot program
executes, and computes the min/max/average period for loops in the last
second.
Example usage::
class Robot(wpilib.IterativeRobot):
def teleopInit(self):
self.loop_timer = LoopTimer(self.logger)
def teleopPeriodic(self):
"""
:param delay_period: The amount of time (in seconds) to do a delay
:type delay_period: float
"""
warnings.warn(
"PreciseDelay is deprecated, use NotifierDelay instead.",
category=DeprecationWarning,
stacklevel=2,
)
# The WPILib sleep/etc functions are slightly less stable as
# they have more overhead, so only use them in simulation mode
if wpilib.RobotBase.isSimulation():
self.delay = wpilib.Timer.delay
self.get_now = wpilib.Timer.getFPGATimestamp
# Test to see if we're in a unit test, and switch the wait function
# to run more efficiently -- otherwise full tests are dog slow
try:
import pyfrc.config
if pyfrc.config.mode in ("test", "upload"):
self.wait = self._wait_unit_tests
except:
pass
else:
self.delay = time.sleep
self.get_now = time.monotonic
self.delay_period = float(delay_period)
def time(self):
return Timer.getFPGATimestamp()
"""
If you wish to just use your own robot program to use with the data
logging program, you only need to copy/paste the logic below into
your code and ensure it gets called periodically in autonomous mode
Additionally, you need to set NetworkTables update rate to 10ms using
the setUpdateRate call.
Note that reading/writing self.autospeed and self.telemetry are
NetworkTables operations (using pynetworktables's ntproperty), so
if you don't read/write NetworkTables in your implementation it won't
actually work.
"""
# Retrieve values to send back before telling the motors to do something
now = wpilib.Timer.getFPGATimestamp()
encoder_position = self.encoder_getpos()
encoder_rate = self.encoder_getrate()
battery = self.ds.getBatteryVoltage()
motor_volts = self.arm_motor.getMotorOutputVoltage()
# Retrieve the commanded speed from NetworkTables
autospeed = self.autospeed
self.prior_autospeed = autospeed
# command motors to do things
self.arm_motor.set(autospeed)
# send telemetry data array back to NT
self.telemetry = (
def _simulationPeriodic(self):
now = wpilib.Timer.getFPGATimestamp()
last_tm = self.last_tm
if last_tm is None:
self.last_tm = now
else:
# When using time, always do it based on a differential! You may
# not always be called at a constant rate
tm_diff = now - last_tm
# Don't run physics calculations more than 100hz
if tm_diff > 0.010:
try:
self.engine.update_sim(now, tm_diff)
except Exception as e:
raise Exception(
def autonomousPeriodic(self):
"""Called every 20ms in autonomous mode"""
self.useless += 1
# Obviously, this is fabricated... do something more useful!
data1 = self.useless
data2 = self.useless * 2
# Only write once per loop
wpilib.SmartDashboard.putNumberArray(
"log_data", [wpilib.Timer.getFPGATimestamp(), data1, data2]
)