runtime.api¶
Student API for controlling the robot.
The student provides a single Python file containing a minimum of four functions:
>>> def autonomous_setup():
... ...
>>> def autonomous_main():
... ...
>>> def teleop_setup():
... ...
>>> def teleop_main():
... ...
The *_setup functions—autonomous_setup and teleop_setup—run once at the
start of the autonomous and teleop phases, respectively. After the setup function runs,
the corresponding *_main functions run periodically until the end of the phase. The
frequency of main calls is configurable, but the default interval is 0.1s.
These interfaces are largely thin wrappers around runtime.buffer.BufferStore.
- class runtime.api.Actions[source]¶
Bases:
runtime.api.StudentAPIAPI for performing asynchronous execution.
An “action” is a special function defined with the
async defkeywords, like so:>>> async def wave_hand(): ... Robot.set(SERVO_UID, 'servo0', -0.5) ... await Actions.sleep(1) ... Robot.set(SERVO_UID, 'servo0', 0.5)
In this example, the robot’s servo rotates 45 degrees counterclockwise from the motor’s center, waits one second, and then rotates 90 degrees clockwise. Actions are useful for triggering time-sensitive events outside the
autonomous_mainorteleop_mainfunctions, which fire at a fixed frequency.To schedule an action to run, call
Actions.run()on the action’s name from one of the regular*_setupor*_mainfunctions, like so:>>> def autonomous_setup(): ... Actions.run(wave_hand) # Correct ... wave_hand() # Incorrect: will not run
Do not call an action like you would call a regular function.
- abstract is_running(action, /)[source]¶
Check whether an action is already running.
- Parameters
action (Callable[[...], Awaitable[None]]) – An action (coroutine function).
- abstract run(action, /, *args, timeout=30, periodic=False)[source]¶
Schedule an action to run outside of the
*_mainfunctions.- Parameters
action (Callable[[...], Awaitable[None]]) – An action (coroutine function).
args (Any) – Positional arguments to pass to the action.
timeout (float) – Maximum number of seconds the action should be allowed to run for. Must be a nonnegative number.
periodic (bool) – Whether to run the action repeatedly or not. A periodic action that completes before the timeout has elapsed is not rescheduled early.
- async static sleep(duration, /)[source]¶
Pause the current action for some amount of time.
- Parameters
duration (float) – The number of seconds to wait for. Must be a nonnegative number.
Note
Remember to use the
awaitkeyword beforeActions.sleep().
- class runtime.api.Alliance(value)[source]¶
Bases:
enum.IntEnumThe alliances that compete in a match.
- BLUE¶
The blue alliance.
- GOLD¶
The gold alliance.
- class runtime.api.BufferAPI(buffers, logger)[source]¶
Bases:
runtime.api.StudentAPIBase type for all APIs that access shared memory buffers.
- Parameters
buffers (runtime.buffer.BufferStore) – Buffer store.
logger (structlog.stdlib.BoundLogger) – Synchronous bound logger.
- class runtime.api.Field(buffers, logger, start=<factory>)[source]¶
Bases:
runtime.api.BufferAPIAPI for interacting with the field and other robots.
- Parameters
start (float) – The UNIX timestamp (in seconds) of the start of the current autonomous/teleop phase.
- property alliance: runtime.api.Alliance¶
The alliance this robot is a member of in this match.
- class runtime.api.Gamepad(buffers, logger, enabled=True)[source]¶
Bases:
runtime.api.BufferAPIAPI for reading game controller inputs.
- class runtime.api.Robot(buffers, logger, names=<factory>)[source]¶
Bases:
runtime.api.BufferAPIAPI for accessing Smart Devices.
- Parameters
names (Mapping[str, int]) – A mapping from human-readable device names (aliases) to UIDs that students can configure.
- runtime.api.safe(method)[source]¶
A decorator that wraps API methods to catch and log any exceptions.
- Parameters
method (Callable[[...], runtime.api.RT]) – API method to be wrapped.
- Returns
The method wrapper. If the wrapped method raises an
Exception, the wrapper’s return value will beNone.BaseExceptionis too broad to catch.