-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving from just a wheel to the swerve drive
- Loading branch information
1 parent
6b889b9
commit 680ca35
Showing
4 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import typing | ||
import commands2 | ||
from subsystems.swerve_drive import SwerveDrive | ||
import wpilib | ||
import constants | ||
import conversions | ||
import math | ||
import ctre | ||
|
||
class DriveLeftFront(commands2.CommandBase): | ||
def __init__(self, swerveDrive: SwerveDrive, x: typing.Callable[[], float], y: typing.Callable[[], float]) -> None: | ||
super().__init__() | ||
self.drive = swerveDrive | ||
#self.units = conversions.convertDegreesToTalonFXUnits(conversions.convertJoystickInputToDegrees(x(), y())) | ||
self.x = x | ||
self.y = y | ||
self.addRequirements([self.drive]) | ||
|
||
def execute(self) -> None: | ||
self.angle = conversions.convertJoystickInputToDegrees(conversions.deadband(self.x(), constants.kdeadband), conversions.deadband(self.y(), constants.kdeadband)) | ||
self.magnitude = math.hypot(conversions.deadband(self.x(), constants.kdeadband), conversions.deadband(self.y(), constants.kdeadband)) | ||
self.drive.turnWheel(self.drive.leftFrontSwerveModule, self.angle, self.magnitude) | ||
|
||
|
||
def end(self, interrupted: bool) -> None: | ||
self.drive.stopAllMotors() | ||
|
||
def isFinished(self) -> bool: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import commands2 | ||
import ctre | ||
import constants | ||
import wpilib | ||
import conversions | ||
import math | ||
from subsystems.swerve_wheel import SwerveWheel | ||
|
||
class SwerveDrive(commands2.SubsystemBase): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
# init motors | ||
self.leftFrontDirection = ctre.TalonFX(constants.kleftFrontDirectionID) | ||
self.leftFrontSpeed = ctre.TalonFX(constants.kleftFrontSpeedID) | ||
|
||
self.leftRearDirection = ctre.TalonFX(constants.kleftRearDirectionID) | ||
self.leftRearSpeed = ctre.TalonFX(constants.kleftRearSpeedID) | ||
|
||
self.rightFrontDirection = ctre.TalonFX(constants.krightFrontDirectionID) | ||
self.rightFrontSpeed = ctre.TalonFX(constants.krightFrontSpeedID) | ||
|
||
self.rightRearDirection = ctre.TalonFX(constants.krightRearDirectionID) | ||
self.rightRearSpeed = ctre.TalonFX(constants.krightRearSpeedID) | ||
|
||
# init swerve modules | ||
self.leftFrontSwerveModule = SwerveWheel(self.leftFrontDirection, self.leftFrontSpeed) | ||
self.leftRearSwerveModule = SwerveWheel(self.leftRearDirection, self.leftRearSpeed) | ||
|
||
self.rightFrontSwerveModule = SwerveWheel(self.rightFrontDirection, self.rightFrontSpeed) | ||
self.rightRearSwerveModule = SwerveWheel(self.rightRearDirection, self.rightRearSpeed) | ||
|
||
def turnWheel(self, module: SwerveWheel, direction: float, magnitude: float): | ||
self.units = conversions.convertDegreesToTalonFXUnits(direction) | ||
if magnitude >= 1.0: | ||
magnitude = 1.0 | ||
# find current angle | ||
currentAngle = conversions.convertTalonFXUnitsToDegrees(module.directionMotor.getSelectedSensorPosition()) | ||
# see if the abs value is greater than 180 | ||
if math.fabs(direction) >= 180.0: | ||
# find the abs value of the opposite angle | ||
opposAngle = math.fabs(direction) - 180.0 | ||
else: | ||
# find the abs value of the opposite angle | ||
opposAngle = math.fabs(direction) + 180.0 | ||
# print some stats for debugging | ||
wpilib.SmartDashboard.putNumber(" Original Angle -", direction) | ||
wpilib.SmartDashboard.putNumber(" Abs Opposit Angle -", opposAngle) | ||
# check if the joystick is in use | ||
if magnitude != 0.0: | ||
# if the original angle is closer | ||
if math.fabs(currentAngle - direction) <= math.fabs(currentAngle - opposAngle): | ||
#turn to the original angle | ||
if direction == 0.0: | ||
if (2048*constants.ksteeringGearRatio) - self.units < module.directionMotor.getSelectedSensorPosition(): | ||
module.turn(2048*constants.ksteeringGearRatio) | ||
else: | ||
module.turn(0.0) | ||
else: | ||
module.turn(self.units*constants.ksteeringGearRatio) | ||
#move in the normal way | ||
module.move(magnitude) | ||
else: # the opposite angle is closer | ||
#turn to the other angle | ||
if direction == 0.0: | ||
if (2048*constants.ksteeringGearRatio) - conversions.convertDegreesToTalonFXUnits(opposAngle) < module.directionMotor.getSelectedSensorPosition(): | ||
module.turn(2048*constants.ksteeringGearRatio) | ||
else: | ||
module.turn(0.0) | ||
else: | ||
#change direction of the speed motor | ||
module.turn(conversions.convertDegreesToTalonFXUnits(opposAngle)*constants.ksteeringGearRatio) | ||
#move in the opposite directionj | ||
module.move(-magnitude) | ||
|
||
def stopAllMotors(self): | ||
self.leftFrontSwerveModule.stopAllMotors() | ||
self.leftRearSwerveModule.stopAllMotors() | ||
self.rightFrontSwerveModule.stopAllMotors() | ||
self.rightRearSwerveModule.stopAllMotors() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters