robotpy_ext.autonomous package¶
robotpy_ext.autonomous.selector module¶
-
class
robotpy_ext.autonomous.selector.AutonomousModeSelector(autonomous_pkgname, *args, **kwargs)[source]¶ Bases:
objectThis object loads all modules in a specified python package, and tries to automatically discover autonomous modes from them. Each module is added to a
SendableChooserobject, which allows the user to select one of them via the SmartDashboard/SFX.Autonomous mode objects must implement the following functions:
on_enable- Called when autonomous mode is initially enabledon_disable- Called when autonomous mode is no longer activeon_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 usersDISABLED- If True, don’t allow this mode to be selectedDEFAULT- If True, this is the default autonomous mode selected
AutonomousModeSelector can be easily used from either
IterativeRobotor fromSampleRobot. ForIterativeRobot, the simplest usage would look like so:class MyRobot(wpilib.IterativeRobot): def robotInit(self): self.automodes = AutonomousModeSelector('autonomous') def autonomousPeriodic(self): self.automodes.run()
For
SampleRobot, for the simplest usage you would do this:class MyRobot(wpilib.SampleRobot): def robotInit(self): self.automodes = AutonomousModeSelector('autonomous') def autonomous(self): self.automodes.run()
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_teststo your pyfrc unit tests like so:from robotpy_ext.autonomous.selector_tests import *
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
-
run(control_loop_wait_time=0.02, iter_fn=None, on_exception=None)[source]¶ This function does everything required to implement autonomous mode behavior. You should call this from your autonomous mode function –
autonomousPeriodicinIterativeRobot, orautonomousinSampleRobot.This function will NOT exit until autonomous mode has ended. If you need to execute code in all autonomous modes, pass a function as the
iter_fnparameter, and it will be called once per autonomous mode iteration.Parameters: - control_loop_wait_time – Amount of time between iterations
- iter_fn – Called at the end of every iteration while autonomous mode is executing
- on_exception – Called when an uncaught exception is raised, must take a single keyword arg “forceReport”
robotpy_ext.autonomous.selector_tests module¶
-
robotpy_ext.autonomous.selector_tests.test_all_autonomous(control, fake_time, robot)[source]¶ This test runs all possible autonomous modes that can be selected by the autonomous switcher.
This should work for most robots. If it doesn’t work for yours, and it’s not a code issue with your robot, please file a bug on github.
robotpy_ext.autonomous.stateful_autonomous module¶
-
class
robotpy_ext.autonomous.stateful_autonomous.StatefulAutonomous(components=None)[source]¶ Bases:
objectThis object is designed to be used to implement autonomous modes that can be used with the
AutonomousModeSelectorobject 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 thetimed_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 startedstate_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 -
next_state(name)[source]¶ Call this function to transition to the next state
Parameters: name – Name of the state to transition to
-
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_enablefunction: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\fooif add_prefix is specified, otherwise asfoo.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 thenext_statefunction is executed.The decorated function can have the following arguments in any order:
tm- The number of seconds since autonomous has startedstate_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 interruptedThe decorated function can have the following arguments in any order:
tm- The number of seconds since autonomous has startedstate_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