2026-02-20 18:30:35 -05:00
|
|
|
#
|
|
|
|
|
# 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 math
|
|
|
|
|
import commands2
|
|
|
|
|
|
|
|
|
|
from subsystems.drivetrain import Drivetrain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TurnDegrees(commands2.Command):
|
2026-03-06 14:19:15 -08:00
|
|
|
def __init__(self, velocity: float, degrees: float, drive: Drivetrain) -> None:
|
2026-02-20 18:30:35 -05:00
|
|
|
"""Creates a new TurnDegrees. This command will turn your robot for a desired rotation (in
|
2026-03-06 14:19:15 -08:00
|
|
|
degrees) and rotational velocity.
|
2026-02-20 18:30:35 -05:00
|
|
|
|
2026-03-06 14:19:15 -08:00
|
|
|
:param velocity: The velocity which the robot will drive. Negative is in reverse.
|
2026-02-20 18:30:35 -05:00
|
|
|
:param degrees: Degrees to turn. Leverages encoders to compare distance.
|
|
|
|
|
:param drive: The drive subsystem on which this command will run
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.degrees = degrees
|
2026-03-06 14:19:15 -08:00
|
|
|
self.velocity = velocity
|
2026-02-20 18:30:35 -05:00
|
|
|
self.drive = drive
|
|
|
|
|
self.addRequirements(drive)
|
|
|
|
|
|
|
|
|
|
def initialize(self) -> None:
|
|
|
|
|
"""Called when the command is initially scheduled."""
|
|
|
|
|
# Set motors to stop, read encoder values for starting point
|
|
|
|
|
self.drive.arcadeDrive(0, 0)
|
|
|
|
|
self.drive.resetEncoders()
|
|
|
|
|
|
|
|
|
|
def execute(self) -> None:
|
|
|
|
|
"""Called every time the scheduler runs while the command is scheduled."""
|
2026-03-06 14:19:15 -08:00
|
|
|
self.drive.arcadeDrive(0, self.velocity)
|
2026-02-20 18:30:35 -05:00
|
|
|
|
|
|
|
|
def end(self, interrupted: bool) -> None:
|
|
|
|
|
"""Called once the command ends or is interrupted."""
|
|
|
|
|
self.drive.arcadeDrive(0, 0)
|
|
|
|
|
|
|
|
|
|
def isFinished(self) -> bool:
|
|
|
|
|
"""Returns true when the command should end."""
|
|
|
|
|
|
|
|
|
|
# Need to convert distance travelled to degrees. The Standard
|
|
|
|
|
# Romi Chassis found here, https://www.pololu.com/category/203/romi-chassis-kits,
|
|
|
|
|
# has a wheel placement diameter (149 mm) - width of the wheel (8 mm) = 141 mm
|
|
|
|
|
# or 5.551 inches. We then take into consideration the width of the tires.
|
|
|
|
|
inchPerDegree = math.pi * 5.551 / 360.0
|
|
|
|
|
|
|
|
|
|
# Compare distance travelled from start to distance based on degree turn
|
|
|
|
|
return self._getAverageTurningDistance() >= inchPerDegree * self.degrees
|
|
|
|
|
|
|
|
|
|
def _getAverageTurningDistance(self) -> float:
|
|
|
|
|
leftDistance = abs(self.drive.getLeftDistanceInch())
|
|
|
|
|
rightDistance = abs(self.drive.getRightDistanceInch())
|
|
|
|
|
return (leftDistance + rightDistance) / 2.0
|