mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[copybara] Import robotpy examples (#8608)
GitOrigin-RevId: 9ba4bc3040fa7e772f5a594039e78fc6c43d114e
This commit is contained in:
10
robotpyExamples/SelectCommand/constants.py
Normal file
10
robotpyExamples/SelectCommand/constants.py
Normal file
@@ -0,0 +1,10 @@
|
||||
#
|
||||
# Copyright (c) FIRST and other WPILib contributors.
|
||||
# Open Source Software; you can modify and/or share it under the terms of
|
||||
# the WPILib BSD license file in the root directory of this project.
|
||||
#
|
||||
|
||||
|
||||
class OIConstants:
|
||||
# Example: the port of the driver's controller
|
||||
kDriverControllerPort = 0
|
||||
67
robotpyExamples/SelectCommand/robot.py
Normal file
67
robotpyExamples/SelectCommand/robot.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) FIRST and other WPILib contributors.
|
||||
# Open Source Software; you can modify and/or share it under the terms of
|
||||
# the WPILib BSD license file in the root directory of this project.
|
||||
#
|
||||
|
||||
import wpilib
|
||||
import commands2
|
||||
import typing
|
||||
|
||||
from robotcontainer import RobotContainer
|
||||
|
||||
|
||||
class MyRobot(commands2.TimedCommandRobot):
|
||||
"""
|
||||
Command v2 robots are encouraged to inherit from TimedCommandRobot, which
|
||||
has an implementation of robotPeriodic which runs the scheduler for you
|
||||
"""
|
||||
|
||||
autonomousCommand: typing.Optional[commands2.Command] = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
This function is run when the robot is first started up and should be used for any
|
||||
initialization code.
|
||||
"""
|
||||
|
||||
super().__init__()
|
||||
# Instantiate our RobotContainer. This will perform all our button bindings, and put our
|
||||
# autonomous chooser on the dashboard.
|
||||
self.container = RobotContainer()
|
||||
|
||||
def disabledInit(self) -> None:
|
||||
"""This function is called once each time the robot enters Disabled mode."""
|
||||
pass
|
||||
|
||||
def disabledPeriodic(self) -> None:
|
||||
"""This function is called periodically when disabled"""
|
||||
pass
|
||||
|
||||
def autonomousInit(self) -> None:
|
||||
"""This autonomous runs the autonomous command selected by your RobotContainer class."""
|
||||
self.autonomousCommand = self.container.getAutonomousCommand()
|
||||
|
||||
if self.autonomousCommand:
|
||||
self.autonomousCommand.schedule()
|
||||
|
||||
def autonomousPeriodic(self) -> None:
|
||||
"""This function is called periodically during autonomous"""
|
||||
pass
|
||||
|
||||
def teleopInit(self) -> None:
|
||||
# This makes sure that the autonomous stops running when
|
||||
# teleop starts running. If you want the autonomous to
|
||||
# continue until interrupted by another command, remove
|
||||
# this line or comment it out.
|
||||
if self.autonomousCommand:
|
||||
self.autonomousCommand.cancel()
|
||||
|
||||
def teleopPeriodic(self) -> None:
|
||||
"""This function is called periodically during operator control"""
|
||||
pass
|
||||
|
||||
def testInit(self) -> None:
|
||||
# Cancels all running commands at the start of test mode
|
||||
commands2.CommandScheduler.getInstance().cancelAll()
|
||||
67
robotpyExamples/SelectCommand/robotcontainer.py
Normal file
67
robotpyExamples/SelectCommand/robotcontainer.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#
|
||||
# Copyright (c) FIRST and other WPILib contributors.
|
||||
# Open Source Software; you can modify and/or share it under the terms of
|
||||
# the WPILib BSD license file in the root directory of this project.
|
||||
#
|
||||
|
||||
import enum
|
||||
import commands2
|
||||
|
||||
|
||||
class RobotContainer:
|
||||
"""This class is where the bulk of the robot should be declared. Since Command-based is a
|
||||
"declarative" paradigm, very little robot logic should actually be handled in the :class:`.Robot`
|
||||
periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
|
||||
subsystems, commands, and button mappings) should be declared here.
|
||||
"""
|
||||
|
||||
# The enum used as keys for selecting the command to run.
|
||||
class CommandSelector(enum.Enum):
|
||||
ONE = enum.auto()
|
||||
TWO = enum.auto()
|
||||
THREE = enum.auto()
|
||||
|
||||
# An example selector method for the selectcommand.
|
||||
def select(self) -> CommandSelector:
|
||||
"""Returns the selector that will select which command to run.
|
||||
Can base this choice on logical conditions evaluated at runtime.
|
||||
"""
|
||||
return self.CommandSelector.ONE
|
||||
|
||||
def __init__(self) -> None:
|
||||
# An example selectcommand. Will select from the three commands based on the value returned
|
||||
# by the selector method at runtime. Note that selectcommand takes a generic type, so the
|
||||
# selector does not have to be an enum; it could be any desired type (string, integer,
|
||||
# boolean, double...)
|
||||
self.example_select_command = commands2.SelectCommand(
|
||||
# Maps selector values to commands
|
||||
{
|
||||
self.CommandSelector.ONE: commands2.PrintCommand(
|
||||
"Command one was selected!"
|
||||
),
|
||||
self.CommandSelector.TWO: commands2.PrintCommand(
|
||||
"Command two was selected!"
|
||||
),
|
||||
self.CommandSelector.THREE: commands2.PrintCommand(
|
||||
"Command three was selected!"
|
||||
),
|
||||
},
|
||||
self.select,
|
||||
)
|
||||
|
||||
# Configure the button bindings
|
||||
self.configureButtonBindings()
|
||||
|
||||
def configureButtonBindings(self) -> None:
|
||||
"""Use this method to define your button->command mappings. Buttons can be created by
|
||||
instantiating a {GenericHID} or one of its subclasses
|
||||
({edu.wpi.first.wpilibj.Joystick} or {XboxController}), and then calling passing it to a
|
||||
{edu.wpi.first.wpilibj2.command.button.JoystickButton}.
|
||||
"""
|
||||
|
||||
def getAutonomousCommand(self) -> commands2.Command:
|
||||
"""Use this to pass the autonomous command to the main {Robot} class.
|
||||
|
||||
:returns: the command to run in autonomous
|
||||
"""
|
||||
return self.example_select_command
|
||||
Reference in New Issue
Block a user