robotpy_ext.autonomous package

robotpy_ext.autonomous.selector module

class robotpy_ext.autonomous.selector.AutonomousModeSelector(autonomous_pkgname, *args, **kwargs)[source]

Bases: object

This object loads all modules in a specified python package, and tries to automatically discover autonomous modes from them. Each module is added to a SendableChooser object, which allows the user to select one of them via SmartDashboard.

Autonomous mode objects must implement the following functions:

  • on_enable - Called when autonomous mode is initially enabled

  • on_disable - Called when autonomous mode is no longer active

  • on_iteration - Called for each iteration of the autonomous control loop

Your autonomous object may have the following attributes:

  • MODE_NAME - The name of the autonomous mode to display to users

  • DISABLED - If True, don’t allow this mode to be selected

  • DEFAULT - If True, this is the default autonomous mode selected

Here is an example of using AutonomousModeSelector in TimedRobot:

class MyRobot(wpilib.TimedRobot):

    def robotInit(self):
        self.automodes = AutonomousModeSelector('autonomous')

    def autonomousInit(self):
        self.automodes.start()

    def autonomousPeriodic(self):
        self.automodes.periodic()

    def disabledInit(self):
        self.automodes.disable()

If you use AutonomousModeSelector, you may also be interested in the autonomous state machine helper (StatefulAutonomous).

Check out the samples in our github repository that show some basic usage of AutonomousModeSelector.

Note

If you use AutonomousModeSelector, then you should add robotpy_ext.autonomous.selector_tests to your pyfrc unit tests like so:

from robotpy_ext.autonomous.selector_tests import *

Note

For your autonomous mode’s on_disable method to be called, you must call disable() in disabledInit.

It is okay to not call disable() if you do not need on_disable.

Parameters:
  • autonomous_pkgname – Module to load autonomous modes from

  • args – Args to pass to created autonomous modes

  • kwargs – Keyword args to pass to created autonomous modes

disable()[source]

Disables the active autonomous mode.

You can call this from your disabledInit method to call your autonomous mode’s on_disable method. :rtype: None

New in version 2020.1.5.

endCompetition()[source]

Call this function when your robot’s endCompetition function is called

periodic()[source]

Execute one control loop iteration of the active autonomous mode.

Call this from your autonomousPeriodic method. :rtype: None

New in version 2020.1.5.

run(control_loop_wait_time=0.02, iter_fn=None, on_exception=None, watchdog=None)[source]

This method implements the entire autonomous loop.

Do not call this from TimedRobot as this will break the timing of your control loop when your robot switches to teleop.

This function will NOT exit until autonomous mode has ended. If you need to execute code in all autonomous modes, pass a function or list of functions as the iter_fn parameter, and they will be called once per autonomous mode iteration.

Parameters:
  • control_loop_wait_time (float) – Amount of time between iterations in seconds

  • iter_fn (Union[Callable[[], None], Sequence[Callable[[], None]]]) – Called at the end of every iteration while autonomous mode is executing

  • on_exception (Callable) – Called when an uncaught exception is raised, must take a single keyword arg “forceReport”

  • watchdog (Union[Watchdog, SimpleWatchdog]) – a WPILib Watchdog to feed every iteration

Return type:

None

start()[source]

Start autonomous mode.

This initialises the selected autonomous mode. Call this from your autonomousInit method. :rtype: None

New in version 2020.1.5.

robotpy_ext.autonomous.stateful_autonomous module

class robotpy_ext.autonomous.stateful_autonomous.StatefulAutonomous(components=None)[source]

Bases: object

This object is designed to be used to implement autonomous modes that can be used with the AutonomousModeSelector object to select an appropriate autonomous mode. However, you don’t have to.

This object is designed to meet the following goals:

  • Supports simple built-in tuning of autonomous mode parameters via SmartDashboard

  • Easy to create autonomous modes that support state machine or time-based operation

  • Autonomous modes that are easy to read and understand

You use this by defining a class that inherits from StatefulAutonomous. To define each state, you use the timed_state() decorator on a function. When each state is run, the decorated function will be called. Decorated functions can receive the following parameters:

  • tm - The number of seconds since autonomous has started

  • state_tm - The number of seconds since this state has been active (note: it may not start at zero!)

  • initial_call - Set to True when the state is initially called, False otherwise. If the state is switched to multiple times, this will be set to True at the start of each state.

An example autonomous mode that drives the robot forward for 5 seconds might look something like this:

from robotpy_ext.autonomous import StatefulAutonomous

class DriveForward(StatefulAutonomous):

    MODE_NAME = 'Drive Forward'

    def initialize(self):
        pass

    @timed_state(duration=0.5, next_state='drive_forward', first=True)
    def drive_wait(self):
        pass

    @timed_state(duration=5)
    def drive_forward(self):
        self.drive.move(0, 1, 0)

Note that in this example, it is assumed that the DriveForward object is initialized with a dictionary with a value ‘drive’ that contains an object that has a move function:

components = {'drive': SomeObject() }
mode = DriveForward(components)

If you use this object with AutonomousModeSelector, make sure to initialize it with the dictionary, and it will be passed to this autonomous mode object when initialized.

See also

Check out the samples in our github repository that show some basic usage of AutonomousModeSelector.

Parameters:

components (dict) – A dictionary of values that will be assigned as attributes to this object, using the key names in the dictionary

done()[source]

Call this function to indicate that no more states should be called

next_state(name)[source]

Call this function to transition to the next state

Parameters:

name – Name of the state to transition to

on_disable()[source]

Called when the autonomous mode is disabled

on_enable()[source]

Called when autonomous mode is enabled, and initializes the state machine internals.

If you override this function, be sure to call it from your customized on_enable function:

super().on_enable()
on_iteration(tm)[source]

This function is called by the autonomous mode switcher, should not be called by enduser code. It is called once per control loop iteration.

register_sd_var(name, default, add_prefix=True, vmin=-1, vmax=1)[source]

Register a variable that is tunable via NetworkTables/SmartDashboard

When this autonomous mode is enabled, all of the SmartDashboard settings will be read and stored as attributes of this object. For example, to register a variable ‘foo’ with a default value of 1:

self.register_sd_var('foo', 1)

This value will show up on NetworkTables as the key MODE_NAME\foo if add_prefix is specified, otherwise as foo.

Parameters:
  • name – Name of variable to display to user, cannot have a space in it.

  • default – Default value of variable

  • add_prefix (bool) – Prefix this setting with the mode name

  • vmin – For tuning: minimum value of this variable

  • vmax – For tuning: maximum value of this variable

robotpy_ext.autonomous.stateful_autonomous.state(f=None, first=False)[source]

If this decorator is applied to a function in an object that inherits from StatefulAutonomous, it indicates that the function is a state. The state will continue to be executed until the next_state function is executed.

The decorated function can have the following arguments in any order:

  • tm - The number of seconds since autonomous has started

  • state_tm - The number of seconds since this state has been active (note: it may not start at zero!)

  • initial_call - Set to True when the state is initially called, False otherwise. If the state is switched to multiple times, this will be set to True at the start of each state.

Parameters:

first (bool) – If True, this state will be ran first

robotpy_ext.autonomous.stateful_autonomous.timed_state(f=None, duration=None, next_state=None, first=False)[source]

If this decorator is applied to a function in an object that inherits from StatefulAutonomous, it indicates that the function is a state that will run for a set amount of time unless interrupted

The decorated function can have the following arguments in any order:

  • tm - The number of seconds since autonomous has started

  • state_tm - The number of seconds since this state has been active (note: it may not start at zero!)

  • initial_call - Set to True when the state is initially called, False otherwise. If the state is switched to multiple times, this will be set to True at the start of each state.

Parameters:
  • duration (float) – The length of time to run the state before progressing to the next state

  • next_state (str) – The name of the next state. If not specified, then this will be the last state executed if time expires

  • first (bool) – If True, this state will be ran first