-
Notifications
You must be signed in to change notification settings - Fork 2
Swerve gil #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Swerve gil #302
Changes from 111 commits
4219d25
5fab74d
d725672
bc914e9
9065982
6702cfb
bf05cac
3756d36
eb2de33
ea24ec5
17c046c
f001d16
b6a1393
eaf9da3
4047983
0770816
bf01952
6ebdbb8
20ca952
c46cf7b
8c571e3
c17ee7c
aa924ac
d3c4756
afb20e4
8271e3e
6399dec
27db4fa
32ebf67
b7ffe31
2cadf78
c8dfa94
0f62d78
0560d12
0ceaf4d
8fa285b
aec42fa
7fb759d
c19a62f
82dc7c7
c0b5607
69b1c87
5c15aea
d58a9f2
ac6ca14
828033b
1ed442d
f6269db
b97e61a
ca629b8
28b0a5d
3b6d3cb
9440253
bf95116
0c4025f
417506e
6bc545d
aedcbef
2900ddb
72cdf92
11b0a64
a1b72ce
acb7a83
3342f62
5224847
3b2f383
fe2423b
456469c
2117fb7
ab7694c
4414cb1
e412324
d11b474
99f4df7
e14a258
76b4eec
8386fa6
23828f0
99d392f
0be1eab
0a9e79b
6f47afb
163b6b6
904fe76
53d1bbe
dd1a22b
140fb7e
046477e
c35cb56
07dad17
d990e3d
ff75776
7f295c0
d5e15ff
65dd37f
2993fae
05c1136
2f87fd9
36e2f19
58b73be
928d5b0
8bb566d
6aecfb6
78d1f6d
182ec6e
3d47272
0b34981
1ef3b9a
f392fd5
287b627
27ae22e
fc29896
d22d7ff
349af62
5a3b216
f8a774a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| package com.spikes2212.command.drivetrains.swerve; | ||
|
|
||
| import com.spikes2212.command.DashboardedSubsystem; | ||
| import edu.wpi.first.math.geometry.Rotation2d; | ||
| import edu.wpi.first.math.geometry.Translation2d; | ||
| import edu.wpi.first.math.kinematics.ChassisSpeeds; | ||
| import edu.wpi.first.math.kinematics.SwerveDriveKinematics; | ||
| import edu.wpi.first.math.kinematics.SwerveModuleState; | ||
|
|
||
| /** | ||
| * This class represents a swerve drivetrain that uses vector calculations to combine the robot’s desired translation | ||
| * and rotation, determining each wheel’s speed and direction for smooth, omnidirectional movement.* | ||
| * | ||
| * @author Gil Ein-Gar | ||
| * @see DashboardedSubsystem | ||
| */ | ||
| public abstract class SwerveDrivetrain extends DashboardedSubsystem { | ||
|
|
||
| private static final String DEFAULT_NAMESPACE_NAME = "drivetrain"; | ||
|
|
||
| protected final SwerveModule frontLeftModule; | ||
| protected final SwerveModule frontRightModule; | ||
| protected final SwerveModule backLeftModule; | ||
| protected final SwerveModule backRightModule; | ||
|
|
||
| protected final double halfDrivetrainTrackWidth; | ||
| protected final double halfDrivetrainTrackLength; | ||
| protected final double maxPossibleVelocity; | ||
| protected final SwerveDriveKinematics kinematics; | ||
|
|
||
| /** | ||
| * Constructs a new instance of {@link SwerveDrivetrain}. | ||
| * | ||
| * @param namespaceName the namespace name for the drivetrain | ||
| * @param frontLeftModule the front left module using {@link SwerveModule} | ||
| * @param frontRightModule the front right module using {@link SwerveModule} | ||
| * @param backLeftModule the back left module using {@link SwerveModule} | ||
| * @param backRightModule the back right module using{@link SwerveModule} | ||
| * @param halfDrivetrainTrackWidth half of the width of the drivetrain | ||
| * @param halfDrivetrainTrackLength half of the length of the drivetrain | ||
| * @param maxPossibleVelocity the maximum possible velocity of the drive motors | ||
| */ | ||
| public SwerveDrivetrain(String namespaceName, SwerveModule frontLeftModule, SwerveModule frontRightModule, | ||
| SwerveModule backLeftModule, SwerveModule backRightModule, double halfDrivetrainTrackWidth, | ||
| double halfDrivetrainTrackLength, double maxPossibleVelocity) { | ||
| super(namespaceName); | ||
| this.frontLeftModule = frontLeftModule; | ||
| this.frontRightModule = frontRightModule; | ||
| this.backLeftModule = backLeftModule; | ||
| this.backRightModule = backRightModule; | ||
| this.halfDrivetrainTrackWidth = halfDrivetrainTrackWidth; | ||
| this.halfDrivetrainTrackLength = halfDrivetrainTrackLength; | ||
| this.maxPossibleVelocity = maxPossibleVelocity; | ||
| kinematics = calculateKinematics(); | ||
|
|
||
| resetRelativeEncoders(); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new instance of {@link SwerveDrivetrain} with the default name of "drivetrain". | ||
| * | ||
| * @param frontLeftModule the front left module using {@link SwerveModule} | ||
| * @param frontRightModule the front right module using {@link SwerveModule} | ||
| * @param backLeftModule the back left module using {@link SwerveModule} | ||
| * @param backRightModule the back right module using {@link SwerveModule} | ||
| * @param halfDrivetrainTrackWidth half the width of the drivetrain | ||
| * @param halfDrivetrainTrackLength half the length of the drivetrain | ||
| * @param maxPossibleVelocity the maximum possible velocity of the drive motors | ||
| */ | ||
| public SwerveDrivetrain(SwerveModule frontLeftModule, SwerveModule frontRightModule, SwerveModule backLeftModule, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| SwerveModule backRightModule, double halfDrivetrainTrackWidth, double halfDrivetrainTrackLength, | ||
| double maxPossibleVelocity) { | ||
| this(getClassName(DEFAULT_NAMESPACE_NAME), frontLeftModule, frontRightModule, backLeftModule, backRightModule, | ||
| halfDrivetrainTrackWidth, halfDrivetrainTrackLength, maxPossibleVelocity); | ||
| } | ||
|
|
||
| /** | ||
| * Function that moves the robot in a swerve motion. | ||
| * | ||
| * @param xSpeed the desired speed on the x-axis. | ||
| * @param ySpeed the desired speed on the y-axis. | ||
| * @param rotationSpeed the desired speed for the drivetrain rotation. | ||
| * @param isFieldRelative whether the drive should be relative to the field or to the robot. | ||
| * @param timeStep the amount of time between calculations, representing how long the speed value is applied | ||
| * before updating again. | ||
| * @param useVelocityPID whether the robot velocity will be applied using P.I.D or not. | ||
| */ | ||
| public void drive(double xSpeed, double ySpeed, double rotationSpeed, boolean isFieldRelative, | ||
goooooooooooooooooose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| double timeStep, boolean useVelocityPID) { | ||
| SwerveModuleState[] states; | ||
| if (isFieldRelative) { | ||
| states = getFieldRelativeSpeeds(xSpeed, ySpeed, rotationSpeed, timeStep); | ||
| } else { | ||
| states = getRobotRelativeSpeeds(xSpeed, ySpeed, rotationSpeed, timeStep); | ||
| } | ||
| SwerveDriveKinematics.desaturateWheelSpeeds(states, maxPossibleVelocity); | ||
| setTargetModuleStates(states, useVelocityPID); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the desired module states. | ||
| * | ||
| * @param targetModuleStates an array containing the desired speed and angle for each swerve module | ||
| * @param usePIDVelocity whether the modules will drive with P.I.D for the velocity | ||
goooooooooooooooooose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| protected void setTargetModuleStates(SwerveModuleState[] targetModuleStates, boolean usePIDVelocity) { | ||
| frontLeftModule.setTargetState(targetModuleStates[0], maxPossibleVelocity, usePIDVelocity); | ||
| frontRightModule.setTargetState(targetModuleStates[1], maxPossibleVelocity, usePIDVelocity); | ||
| backLeftModule.setTargetState(targetModuleStates[2], maxPossibleVelocity, usePIDVelocity); | ||
| backRightModule.setTargetState(targetModuleStates[3], maxPossibleVelocity, usePIDVelocity); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current robot angle using an external sensor. | ||
| * | ||
| * @return the current angle of the robot | ||
| */ | ||
| public abstract Rotation2d getCurrentRobotAngle(); | ||
goooooooooooooooooose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Resets the field relativity to be the same as the robot relativity. | ||
| */ | ||
| public abstract void resetFieldRelative(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to help both generality and simplicity for the user, I suggest creating an abstract method called
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but the whole point of this library is that fit's anything. if i knew i had a gyro this whole library would have been so much easier to write, but since i made this library general and non specific my functions are non specific as well and allow the user to use whichever sensor and method they would like |
||
|
|
||
| /** | ||
| * Set the field relative speeds | ||
goooooooooooooooooose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
goooooooooooooooooose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param xSpeed the desired speed on the x-axis. | ||
| * @param ySpeed the desired speed on the y-axis. | ||
| * @param rotationSpeed the desired speed for the drivetrain rotation. | ||
| * @param timeStep the derivation of time the speed should be applied. | ||
| * @return the necessary {@link SwerveModuleState[]} for field relative movement | ||
| */ | ||
| protected SwerveModuleState[] getFieldRelativeSpeeds(double xSpeed, double ySpeed, double rotationSpeed, | ||
goooooooooooooooooose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| double timeStep) { | ||
| return kinematics.toSwerveModuleStates(ChassisSpeeds.discretize(ChassisSpeeds.fromFieldRelativeSpeeds(xSpeed, | ||
| ySpeed, rotationSpeed, getCurrentRobotAngle()), timeStep)); | ||
| } | ||
|
|
||
| /** | ||
| * get the robot relative speeds | ||
goooooooooooooooooose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param xSpeed the desired speed on the x-axis. | ||
| * @param ySpeed the desired speed on the y-axis. | ||
| * @param rotationSpeed the desired speed for the drivetrain rotation. | ||
| * @param timeStep the derivation of time the speed should be applied. | ||
| * @return the necessary {@link SwerveModuleState[]} for robot relative movement | ||
| */ | ||
| protected SwerveModuleState[] getRobotRelativeSpeeds(double xSpeed, double ySpeed, double rotationSpeed, | ||
goooooooooooooooooose marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| double timeStep) { | ||
| return kinematics.toSwerveModuleStates(ChassisSpeeds.discretize(new ChassisSpeeds(xSpeed, ySpeed, | ||
| rotationSpeed), timeStep)); | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the 2d positions of each wheel in relation to the center of the robot. | ||
| * | ||
| * @return a {@link SwerveDriveKinematics} according to the wheels positions in relation to the center | ||
| */ | ||
| protected SwerveDriveKinematics calculateKinematics() { | ||
| Translation2d frontLeftWheelDistanceFromCenter = | ||
| new Translation2d(halfDrivetrainTrackWidth, halfDrivetrainTrackLength); | ||
| Translation2d frontRightWheelDistanceFromCenter = | ||
| new Translation2d(halfDrivetrainTrackWidth, -halfDrivetrainTrackLength); | ||
| Translation2d backLeftWheelDistanceFromCenter = | ||
| new Translation2d(-halfDrivetrainTrackWidth, halfDrivetrainTrackLength); | ||
| Translation2d backRightWheelDistanceFromCenter = | ||
| new Translation2d(-halfDrivetrainTrackWidth, -halfDrivetrainTrackLength); | ||
|
|
||
| return new SwerveDriveKinematics(frontLeftWheelDistanceFromCenter, | ||
| frontRightWheelDistanceFromCenter, backLeftWheelDistanceFromCenter, | ||
| backRightWheelDistanceFromCenter); | ||
| } | ||
|
|
||
| /** | ||
| * @return the robots current {@link ChassisSpeeds} | ||
| */ | ||
| protected ChassisSpeeds getSpeeds() { | ||
| return kinematics.toChassisSpeeds(frontLeftModule.getModuleState(), frontRightModule.getModuleState(), | ||
| backLeftModule.getModuleState(), backRightModule.getModuleState()); | ||
| } | ||
|
|
||
| /** | ||
| * @return returns the front left {@link SwerveModule} | ||
| */ | ||
| public SwerveModule getFrontLeftModule() { | ||
| return frontLeftModule; | ||
| } | ||
|
|
||
| /** | ||
| * @return returns the front right {@link SwerveModule} | ||
| */ | ||
| public SwerveModule getFrontRightModule() { | ||
| return frontRightModule; | ||
| } | ||
|
|
||
goooooooooooooooooose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * @return returns the back left {@link SwerveModule} | ||
| */ | ||
| public SwerveModule getBackLeftModule() { | ||
| return backLeftModule; | ||
| } | ||
|
|
||
| /** | ||
| * @return returns the back right {@link SwerveModule} | ||
| */ | ||
| public SwerveModule getBackRightModule() { | ||
| return backRightModule; | ||
| } | ||
|
|
||
| /** | ||
| * @return the robot's swerve kinematics | ||
| */ | ||
| protected SwerveDriveKinematics getKinematics() { | ||
| return kinematics; | ||
| } | ||
|
|
||
| /** | ||
| * Resets the relative encoders of every module according to their absolute encoder. | ||
| */ | ||
| public void resetRelativeEncoders() { | ||
| frontLeftModule.resetRelativeEncoder(); | ||
| frontRightModule.resetRelativeEncoder(); | ||
| backLeftModule.resetRelativeEncoder(); | ||
| backRightModule.resetRelativeEncoder(); | ||
| } | ||
|
|
||
| /** | ||
| * Stops all modules. | ||
goooooooooooooooooose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public void stop() { | ||
| frontLeftModule.stop(); | ||
| frontRightModule.stop(); | ||
| backLeftModule.stop(); | ||
| backRightModule.stop(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
giving half of a value as a parameter is clunky imo. it's not horrible, but i think you should change it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i have no need for the full parameters, besides this allows me to write the position formula in a much nicer way