commands.autoDrive
1import commands2 2import commands2.cmd 3import wpilib 4from subsystem.drivetrain.swerve_drivetrain import SwerveDrivetrain 5from wpimath.geometry import Pose2d, Transform2d 6 7class AutoDrive(commands2.Command): 8 targetPos = Pose2d() 9 tolerance = Transform2d(0.5, 0.5, 0) 10 def __init__(self, timeS: float, speedX: float, speedY: float, dir: float, fieldRel: bool, drive: SwerveDrivetrain) -> None: 11 """Creates a new DriveDistance. This command will drive your your robot to a designated cordinate. 12 targetPos: The X and Y pos that the bot will drive to(in meters) 13 drive: The drivetrain subsystem on which this command will run 14 """ 15 super().__init__() 16 self.speedX = speedX 17 self.speedY = speedY 18 self.dir = dir 19 self.drive = drive 20 self.timeS = timeS 21 self.fieldRel = fieldRel 22 self.addRequirements(drive) 23 24 def initialize(self) -> None: 25 """Called when the command is initially scheduled.""" 26 self.drive.reset_heading() 27 self.timer = wpilib.Timer() 28 self.timer.start() 29 def execute(self) -> None: 30 """Called every time the scheduler runs while the command is scheduled.""" 31 self.drive.drive(self.speedX, self.speedY, self.dir, self.fieldRel) 32 self.drive.update_pose_estimator() 33 34 def end(self, interrupted: bool) -> None: 35 """Called once the command ends or is interrupted.""" 36 self.drive.drive(0,0,0,False) 37 38 def isFinished(self) -> bool: 39 """Returns true when the command should end.""" 40 # Compare distance travelled from start to desired distance 41 return self.timer.hasElapsed(self.timeS)
8class AutoDrive(commands2.Command): 9 targetPos = Pose2d() 10 tolerance = Transform2d(0.5, 0.5, 0) 11 def __init__(self, timeS: float, speedX: float, speedY: float, dir: float, fieldRel: bool, drive: SwerveDrivetrain) -> None: 12 """Creates a new DriveDistance. This command will drive your your robot to a designated cordinate. 13 targetPos: The X and Y pos that the bot will drive to(in meters) 14 drive: The drivetrain subsystem on which this command will run 15 """ 16 super().__init__() 17 self.speedX = speedX 18 self.speedY = speedY 19 self.dir = dir 20 self.drive = drive 21 self.timeS = timeS 22 self.fieldRel = fieldRel 23 self.addRequirements(drive) 24 25 def initialize(self) -> None: 26 """Called when the command is initially scheduled.""" 27 self.drive.reset_heading() 28 self.timer = wpilib.Timer() 29 self.timer.start() 30 def execute(self) -> None: 31 """Called every time the scheduler runs while the command is scheduled.""" 32 self.drive.drive(self.speedX, self.speedY, self.dir, self.fieldRel) 33 self.drive.update_pose_estimator() 34 35 def end(self, interrupted: bool) -> None: 36 """Called once the command ends or is interrupted.""" 37 self.drive.drive(0,0,0,False) 38 39 def isFinished(self) -> bool: 40 """Returns true when the command should end.""" 41 # Compare distance travelled from start to desired distance 42 return self.timer.hasElapsed(self.timeS)
A state machine representing a complete action to be performed by the robot. Commands are run by
the commands2.CommandScheduler, and can be composed into CommandGroups to allow users to build
complicated multistep actions without the need to roll the state machine logic themselves.
Commands are run synchronously from the main robot loop; no multithreading is used, unless specified explicitly from the command implementation.
11 def __init__(self, timeS: float, speedX: float, speedY: float, dir: float, fieldRel: bool, drive: SwerveDrivetrain) -> None: 12 """Creates a new DriveDistance. This command will drive your your robot to a designated cordinate. 13 targetPos: The X and Y pos that the bot will drive to(in meters) 14 drive: The drivetrain subsystem on which this command will run 15 """ 16 super().__init__() 17 self.speedX = speedX 18 self.speedY = speedY 19 self.dir = dir 20 self.drive = drive 21 self.timeS = timeS 22 self.fieldRel = fieldRel 23 self.addRequirements(drive)
Creates a new DriveDistance. This command will drive your your robot to a designated cordinate. targetPos: The X and Y pos that the bot will drive to(in meters) drive: The drivetrain subsystem on which this command will run
25 def initialize(self) -> None: 26 """Called when the command is initially scheduled.""" 27 self.drive.reset_heading() 28 self.timer = wpilib.Timer() 29 self.timer.start()
Called when the command is initially scheduled.
30 def execute(self) -> None: 31 """Called every time the scheduler runs while the command is scheduled.""" 32 self.drive.drive(self.speedX, self.speedY, self.dir, self.fieldRel) 33 self.drive.update_pose_estimator()
Called every time the scheduler runs while the command is scheduled.