robotpy_ext.misc package

robotpy_ext.misc.asyncio_policy module

This is a replacement event loop and policy for asyncio that uses FPGA time, rather than native python time.

class robotpy_ext.misc.asyncio_policy.FPGATimedEventLoop(selector=None)[source]

Bases: asyncio.unix_events._UnixSelectorEventLoop

An asyncio event loop that uses wpilib time rather than python time

time()[source]

Return the time according to the event loop’s clock.

This is a float expressed in seconds since an epoch, but the epoch, precision, accuracy and drift are unspecified and may differ per event loop.

class robotpy_ext.misc.asyncio_policy.FPGATimedEventLoopPolicy[source]

Bases: asyncio.events.AbstractEventLoopPolicy

An asyncio event loop policy that uses FPGATimedEventLoop

robotpy_ext.misc.asyncio_policy.patch_asyncio_policy()[source]

Sets an instance of FPGATimedEventLoopPolicy as the default asyncio event loop policy

robotpy_ext.misc.looptimer module

class robotpy_ext.misc.looptimer.LoopTimer(logger)[source]

Bases: object

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):
        self.loop_timer.measure()

Mainly intended for debugging purposes to measure how much lag.

measure()[source]

Computes loop performance information and periodically dumps it to the info logger.

reset()[source]

robotpy_ext.misc.precise_delay module

class robotpy_ext.misc.precise_delay.NotifierDelay(delay_period)[source]

Bases: object

Synchronizes a timing loop against interrupts from the FPGA.

This will delay so that the next invocation of your loop happens at precisely the same period, assuming that your loop does not take longer than the specified period.

Example:

with NotifierDelay(0.02) as delay:
    while something:
        # do things here
        delay.wait()
Parameters

delay_period (float) – The period’s amount of time (in seconds).

free()[source]

Clean up the notifier.

Do not use this object after this method is called.

Return type

None

wait()[source]

Wait until the delay period has passed.

Return type

None

class robotpy_ext.misc.precise_delay.PreciseDelay(delay_period)[source]

Bases: object

Used to synchronize a timing loop. Will delay precisely so that the next invocation of your loop happens at the same period, as long as your code does not run longer than the length of the delay.

Our experience has shown that 25ms is a good loop period.

Usage:

delay = PreciseDelay(time_to_delay)

while something:
    # do things here
    delay.wait()

Deprecated since version 2019: PreciseDelay is terribly inefficient. Use NotifierDelay instead.

Parameters

delay_period (float) – The amount of time (in seconds) to do a delay

wait()[source]

Waits until the delay period has passed

robotpy_ext.misc.periodic_filter module

class robotpy_ext.misc.periodic_filter.PeriodicFilter(period, bypass_level=30)[source]

Bases: object

Periodic Filter to help keep down clutter in the console. Simply add this filter to your logger and the logger will only print periodically.

The logger will always print logging levels of WARNING or higher, unless given a different bypass level

Example:

class Component1:

    def setup(self):
        # Set period to 3 seconds, set bypass_level to WARN
        self.logger.addFilter(PeriodicFilter(3, bypass_level=logging.WARN))

    def execute(self):
        # This message will be printed once every three seconds
        self.logger.info('Component1 Executing')

        # This message will be printed out every loop
        self.logger.warn("Uh oh, this shouldn't have happened...")
Parameters
  • period – Wait period (in seconds) between logs

  • bypass_level – Lowest logging level that the filter should not catch

filter(record)[source]

Performs filtering action for logger

robotpy_ext.misc.simple_watchdog module

class robotpy_ext.misc.simple_watchdog.SimpleWatchdog(timeout)[source]

Bases: object

A class that’s a wrapper around a watchdog timer.

When the timer expires, a message is printed to the console and an optional user-provided callback is invoked.

The watchdog is initialized disabled, so the user needs to call enable() before use.

Note

This is a simpler replacement for the wpilib.Watchdog, and should function mostly the same (except that this watchdog will not detect infinite loops).

Warning

This watchdog is not threadsafe

Watchdog constructor.

Parameters

timeout (float) – The watchdog’s timeout in seconds with microsecond resolution.

addEpoch(epochName)[source]

Adds time since last epoch to the list printed by printEpochs().

Epochs are a way to partition the time elapsed so that when overruns occur, one can determine which parts of an operation consumed the most time.

Parameters

epochName (str) – The name to associate with the epoch.

Return type

None

disable()[source]

Disables the watchdog timer.

Return type

None

enable()[source]

Enables the watchdog timer.

Return type

None

getTime()[source]

Returns the time in seconds since the watchdog was last fed.

Return type

float

getTimeout()[source]

Returns the watchdog’s timeout in seconds.

Return type

float

isExpired()[source]

Returns true if the watchdog timer has expired.

Return type

bool

kMinPrintPeriod = 1000000
printIfExpired()[source]

Prints list of epochs added so far and their times.

Return type

None

reset()[source]

Resets the watchdog timer.

This also enables the timer if it was previously disabled.

Return type

None

setTimeout(timeout)[source]

Sets the watchdog’s timeout.

Parameters

timeout (float) – The watchdog’s timeout in seconds with microsecond resolution.

Return type

None