Skip to content
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

Gil swerve #4

Closed
wants to merge 161 commits into from
Closed
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
161 commits
Select commit Hold shift + click to select a range
81720cf
created SwerveModule.java
goooooooooooooooooose Jan 14, 2025
5c248ce
created Drivetrain.java
goooooooooooooooooose Jan 14, 2025
0f4944a
created Drivetrain.java
goooooooooooooooooose Jan 14, 2025
6fd98f7
created Drive.java
goooooooooooooooooose Jan 14, 2025
a4d20fd
created SwerveModule.java
goooooooooooooooooose Jan 14, 2025
e534b11
created SwerveModuleHolder.java
goooooooooooooooooose Jan 19, 2025
78708e0
created SwerveModuleHolder.java
goooooooooooooooooose Jan 19, 2025
c570351
removed blank lines.
goooooooooooooooooose Jan 19, 2025
2723810
renamed MAX_TURN to MAX_TURN_SPEED
goooooooooooooooooose Jan 19, 2025
2666684
removed blank line
goooooooooooooooooose Jan 19, 2025
26667de
unmagiced
goooooooooooooooooose Jan 19, 2025
9507f07
unmagiced 2.0
goooooooooooooooooose Jan 19, 2025
62e04c8
renamed ABSOLUTE_POSITION to ABSOLUTE_POSITION_DISCONTINUITY_POINT
goooooooooooooooooose Jan 19, 2025
ad643e9
made Translation2d fields private
goooooooooooooooooose Jan 20, 2025
9a55578
max and min speeds moved from SwerveModule.java to Drivetrain.java
goooooooooooooooooose Jan 20, 2025
0de041a
separated WHEEL_DIAMETER into WHEEL_DIAMETER_INCHES and INCHES_TO_METERS
goooooooooooooooooose Jan 20, 2025
bd0ba68
put ABSOLUTE_POSITION_DISCONTINUITY_POINT below DEGREES_TO_FLIP
goooooooooooooooooose Jan 20, 2025
d69be6d
put turnEncoder below turnFeedForwardController
goooooooooooooooooose Jan 20, 2025
0ae2b1e
ditto
goooooooooooooooooose Jan 20, 2025
0c2e260
turned Drivetrain.java into a singleton class
goooooooooooooooooose Jan 20, 2025
b0b90ff
committing just so I can make changes in Swerve
goooooooooooooooooose Jan 22, 2025
2c075e3
moved the gyro field right below the modules
goooooooooooooooooose Jan 22, 2025
f28c183
swapped the gyro initializer with the kinematics initializer
goooooooooooooooooose Jan 22, 2025
6c9c027
removed blank line
goooooooooooooooooose Jan 22, 2025
e17c3a2
separated the turnEncoder from the control loop related fields
goooooooooooooooooose Jan 22, 2025
a0a05a8
added a todo to check if new AHRS is correct as well as adding gyro a…
goooooooooooooooooose Jan 22, 2025
101369c
committing in order to change branch
goooooooooooooooooose Jan 23, 2025
3b3bb8e
added path planner
goooooooooooooooooose Jan 29, 2025
242e393
deleted extra line
goooooooooooooooooose Jan 29, 2025
2094d3b
changed navx port
goooooooooooooooooose Jan 29, 2025
162b57c
changed gear ratio from drive to turn
goooooooooooooooooose Jan 29, 2025
09ab476
updated SpikesLib2.json
goooooooooooooooooose Jan 29, 2025
fc7c0a8
added wait correct parameters
goooooooooooooooooose Jan 29, 2025
0120926
replaced motor controllers with wrappers
goooooooooooooooooose Jan 29, 2025
61677e1
made set angle efficient
goooooooooooooooooose Jan 29, 2025
759cd8b
set default value for pid and ff to 0
goooooooooooooooooose Jan 29, 2025
0aea626
reduced min speed
goooooooooooooooooose Jan 29, 2025
0863234
deleted todo
goooooooooooooooooose Jan 29, 2025
9599b6c
deleted extra line
goooooooooooooooooose Jan 29, 2025
ac64696
specified what about the gyro
goooooooooooooooooose Jan 29, 2025
59af30e
removed unnecessary fields
goooooooooooooooooose Jan 29, 2025
91b1fc8
removed unnecessary pid configurations
goooooooooooooooooose Jan 29, 2025
3476693
added blank line at the end of class
goooooooooooooooooose Jan 29, 2025
95d8ab0
renamed reset relative encoders func
goooooooooooooooooose Jan 29, 2025
f0edeea
fixed unnecessary this call
goooooooooooooooooose Jan 29, 2025
c336ded
added track variables in cm
goooooooooooooooooose Jan 29, 2025
d5fcbbe
removed unnecessary this call
goooooooooooooooooose Jan 29, 2025
3fe06a0
renamed to rotationSpeed
goooooooooooooooooose Jan 30, 2025
61bbb88
merged into 1 line
goooooooooooooooooose Jan 30, 2025
31d2100
accommodated minus
goooooooooooooooooose Jan 30, 2025
eba8568
0 default offset value
goooooooooooooooooose Jan 30, 2025
021b6f7
removed unnecessary values
goooooooooooooooooose Jan 30, 2025
906875a
ditto
goooooooooooooooooose Jan 30, 2025
437c665
unmerged
goooooooooooooooooose Jan 30, 2025
9ff00ed
deleted unnecessary imports
goooooooooooooooooose Jan 30, 2025
971f2e2
deleted weird file
goooooooooooooooooose Jan 30, 2025
dbda02b
deleted unnecessary given values
goooooooooooooooooose Jan 30, 2025
2390dd1
add requirements FIRST (get it??? like FIRST the program, do you get …
goooooooooooooooooose Jan 30, 2025
711b5f6
changed units to meters
goooooooooooooooooose Jan 30, 2025
d0fefec
called configureDashboard
goooooooooooooooooose Jan 30, 2025
bde93e5
ditto
goooooooooooooooooose Jan 30, 2025
8ec1fda
boom.
goooooooooooooooooose Jan 30, 2025
b00b683
updated libraries
TurtleMinecraft Jan 31, 2025
13d3635
bruh
TurtleMinecraft Jan 31, 2025
d70f941
not done yet
goooooooooooooooooose Jan 31, 2025
98a6e0f
updated CAN ports
goooooooooooooooooose Jan 31, 2025
55b057c
added reset gyro func
goooooooooooooooooose Jan 31, 2025
f0adb7e
added option to disable acceleration
goooooooooooooooooose Jan 31, 2025
60b7981
updated SpikesLib2.jsonl
goooooooooooooooooose Jan 31, 2025
4086d1b
updated SpikesLib2.json
goooooooooooooooooose Feb 1, 2025
7ca4f37
added override to drive command end func
goooooooooooooooooose Feb 1, 2025
912f211
limitAcceleration below usePID
goooooooooooooooooose Feb 1, 2025
8f7e367
initializing the values in the same order as received
goooooooooooooooooose Feb 1, 2025
b39317d
added blank line
goooooooooooooooooose Feb 1, 2025
f2754a4
added odometry
goooooooooooooooooose Feb 1, 2025
b881427
renamed currentPose and removed this
goooooooooooooooooose Feb 1, 2025
5f6eda1
renamed getModulePosition
goooooooooooooooooose Feb 1, 2025
77e7d06
currentPose not static
goooooooooooooooooose Feb 1, 2025
282616d
periodic below constructor
goooooooooooooooooose Feb 1, 2025
f920359
removed blank line
goooooooooooooooooose Jan 22, 2025
6ccb6e4
separated the turnEncoder from the control loop related fields
goooooooooooooooooose Jan 22, 2025
1e59a77
added a todo to check if new AHRS is correct as well as adding gyro a…
goooooooooooooooooose Jan 22, 2025
d01cefd
committing in order to change branch
goooooooooooooooooose Jan 23, 2025
77fb366
deleted extra line
goooooooooooooooooose Jan 29, 2025
2875295
changed navx port
goooooooooooooooooose Jan 29, 2025
d1448f8
changed gear ratio from drive to turn
goooooooooooooooooose Jan 29, 2025
083e781
updated SpikesLib2.json
goooooooooooooooooose Jan 29, 2025
7fc8d5f
added wait correct parameters
goooooooooooooooooose Jan 29, 2025
a274b75
replaced motor controllers with wrappers
goooooooooooooooooose Jan 29, 2025
524e6d9
weh
goooooooooooooooooose Feb 8, 2025
b03cb1c
set default value for pid and ff to 0
goooooooooooooooooose Jan 29, 2025
63354b4
reduced min speed
goooooooooooooooooose Jan 29, 2025
45fd537
deleted todo
goooooooooooooooooose Jan 29, 2025
6a04e1f
deleted extra line
goooooooooooooooooose Jan 29, 2025
dd40cd4
specified what about the gyro
goooooooooooooooooose Jan 29, 2025
197d95d
removed unnecessary fields
goooooooooooooooooose Jan 29, 2025
c8a4933
removed unnecessary pid configurations
goooooooooooooooooose Jan 29, 2025
5186e91
added blank line at the end of class
goooooooooooooooooose Jan 29, 2025
b9b1979
renamed reset relative encoders func
goooooooooooooooooose Jan 29, 2025
4c8eed6
fixed unnecessary this call
goooooooooooooooooose Jan 29, 2025
e5da446
added track variables in cm
goooooooooooooooooose Jan 29, 2025
1b0c287
removed unnecessary this call
goooooooooooooooooose Jan 29, 2025
f5f644b
renamed to rotationSpeed
goooooooooooooooooose Jan 30, 2025
68c45dc
merged into 1 line
goooooooooooooooooose Jan 30, 2025
25c7d1e
accommodated minus
goooooooooooooooooose Jan 30, 2025
9013a50
0 default offset value
goooooooooooooooooose Jan 30, 2025
58f96c5
removed unnecessary values
goooooooooooooooooose Jan 30, 2025
dfa9f73
ditto
goooooooooooooooooose Jan 30, 2025
6d50188
unmerged
goooooooooooooooooose Jan 30, 2025
5090961
deleted unnecessary imports
goooooooooooooooooose Jan 30, 2025
b6b5796
deleted weird file
goooooooooooooooooose Jan 30, 2025
35082ca
deleted unnecessary given values
goooooooooooooooooose Jan 30, 2025
60ebc9c
add requirements FIRST (get it??? like FIRST the program, do you get …
goooooooooooooooooose Jan 30, 2025
167bc74
changed units to meters
goooooooooooooooooose Jan 30, 2025
f0739dc
called configureDashboard
goooooooooooooooooose Jan 30, 2025
bd130aa
ditto
goooooooooooooooooose Jan 30, 2025
7b22dd9
boom.
goooooooooooooooooose Jan 30, 2025
ca04dd2
updated libraries
TurtleMinecraft Jan 31, 2025
f3b60a9
bruh
TurtleMinecraft Jan 31, 2025
d7607a0
not done yet
goooooooooooooooooose Jan 31, 2025
010249c
updated CAN ports
goooooooooooooooooose Jan 31, 2025
44fdd95
added reset gyro func
goooooooooooooooooose Jan 31, 2025
24a18dc
added option to disable acceleration
goooooooooooooooooose Jan 31, 2025
eccaa12
updated SpikesLib2.jsonl
goooooooooooooooooose Jan 31, 2025
aad018f
updated SpikesLib2.json
goooooooooooooooooose Feb 1, 2025
e4243ee
added override to drive command end func
goooooooooooooooooose Feb 1, 2025
6f04a7e
limitAcceleration below usePID
goooooooooooooooooose Feb 1, 2025
4efc0d9
initializing the values in the same order as received
goooooooooooooooooose Feb 1, 2025
3bee865
added blank line
goooooooooooooooooose Feb 1, 2025
2fb6e95
added odometry
goooooooooooooooooose Feb 1, 2025
b7bba40
renamed currentPose and removed this
goooooooooooooooooose Feb 1, 2025
2e1e3d9
renamed getModulePosition
goooooooooooooooooose Feb 1, 2025
7151a62
currentPose not static
goooooooooooooooooose Feb 1, 2025
7920f20
periodic below constructor
goooooooooooooooooose Feb 1, 2025
84729d8
committing just so I can make changes in Swerve
goooooooooooooooooose Jan 22, 2025
d907901
added path planner
goooooooooooooooooose Jan 29, 2025
2313d86
added named commands
goooooooooooooooooose Feb 8, 2025
d9e4f49
weh
goooooooooooooooooose Feb 8, 2025
ddd0042
add empty line
goooooooooooooooooose Feb 8, 2025
aece548
below final fields
goooooooooooooooooose Feb 8, 2025
383d716
kp is double
goooooooooooooooooose Feb 8, 2025
1d533cf
filenames are the same as their name
goooooooooooooooooose Feb 8, 2025
3e78524
auto files end with blank line
goooooooooooooooooose Feb 8, 2025
447b8e5
removed blank line
goooooooooooooooooose Feb 8, 2025
531f9ec
ctr alt l
goooooooooooooooooose Feb 8, 2025
870ab33
changed stupid pointless arrays name
goooooooooooooooooose Feb 8, 2025
6043255
correct port to gyro
goooooooooooooooooose Feb 8, 2025
9a75056
blank line at the end of path files
goooooooooooooooooose Feb 8, 2025
bb202d7
added blank line
goooooooooooooooooose Feb 8, 2025
04471c4
added blank line
goooooooooooooooooose Feb 8, 2025
e524b1d
changed stupid pointless array name 2.0
goooooooooooooooooose Feb 8, 2025
6edea84
initialized values correct order
goooooooooooooooooose Feb 8, 2025
5877bc2
ctr alt l
goooooooooooooooooose Feb 8, 2025
b2fe356
Merge branch 'gil-swerve' into gil-path-planner-swerve
goooooooooooooooooose Feb 13, 2025
9312d0b
ctr alt l
goooooooooooooooooose Feb 13, 2025
98cff70
ctr alt l
goooooooooooooooooose Feb 13, 2025
e8a5045
Merge pull request #7 from Spikes-2212-Programming-Guild/gil-path-pla…
goooooooooooooooooose Feb 13, 2025
c14787c
Merge branch 'dev' into gil-swerve
goooooooooooooooooose Feb 13, 2025
c325e7e
safd
goooooooooooooooooose Feb 13, 2025
828a37e
safd
goooooooooooooooooose Feb 13, 2025
a4a7e34
Merge pull request #14 from Spikes-2212-Programming-Guild/gil-path-pl…
goooooooooooooooooose Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ dependencies {
implementation group: 'com.ctre.phoenix6', name: 'wpiapi-java', version: '24.0.0-beta-8'
implementation group: 'com.ctre.phoenix', name: 'api-java', version: '5.32.0-beta-5'
implementation group: 'com.ctre.phoenix', name: 'wpiapi-java', version: '5.31.0'
implementation 'com.github.Spikes-2212-Programming-Guild:SpikesLib2:update-wpilib-25-SNAPSHOT'
}

test {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/frc/robot/RobotMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ public class RobotMap {

public interface CAN {

int FRONT_LEFT_DRIVE_TALON_FX = 1;
int FRONT_RIGHT_DRIVE_TALON_FX = 4;
int BACK_LEFT_DRIVE_TALON_FX = 7;
int BACK_RIGHT_DRIVE_TALON_FX = 10;

int FRONT_LEFT_TURN_SPARK_MAX = 2;
int FRONT_RIGHT_TURN_SPARK_MAX = 5;
int BACK_LEFT_TURN_SPARK_MAX = 8;
int BACK_RIGHT_TURN_SPARK_MAX = 11;

int FRONT_LEFT_ABSOLUTE_ENCODER = 3;
int FRONT_RIGHT_ABSOLUTE_ENCODER = 6;
int BACK_LEFT_ABSOLUTE_ENCODER = 9;
int BACK_RIGHT_ABSOLUTE_ENCODER = 12;
}

public interface DIO {
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/frc/robot/commands/Drive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package frc.robot.commands;

import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.Drivetrain;

import java.util.function.Supplier;

public class Drive extends Command {

private final double DRIVE_ACCELERATION_LIMIT = 2;
private final double TURN_ACCELERATION_LIMIT = 4;

private final Drivetrain drivetrain;
private final Supplier<Double> xSpeed;
private final Supplier<Double> ySpeed;
private final Supplier<Double> rotationSpeed;
private final boolean fieldRelative;
private final boolean usePID;

private final SlewRateLimiter xLimiter;
private final SlewRateLimiter yLimiter;
private final SlewRateLimiter rotationLimiter;
private final boolean limitAcceleration;

public Drive(Drivetrain drivetrain, Supplier<Double> xSpeed, Supplier<Double> ySpeed,
Supplier<Double> rotationSpeed, boolean fieldRelative, boolean usePID,
boolean limitAcceleration) {
addRequirements(drivetrain);
this.drivetrain = drivetrain;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.rotationSpeed = rotationSpeed;
this.usePID = usePID;
this.limitAcceleration = limitAcceleration;
this.fieldRelative = fieldRelative;
xLimiter = new SlewRateLimiter(DRIVE_ACCELERATION_LIMIT);
yLimiter = new SlewRateLimiter(DRIVE_ACCELERATION_LIMIT);
rotationLimiter = new SlewRateLimiter(TURN_ACCELERATION_LIMIT);
}

@Override
public void execute() {
double xSpeed = this.xSpeed.get();
double ySpeed = this.ySpeed.get();
double rotationSpeed = this.rotationSpeed.get();

if (limitAcceleration) {
xSpeed = xLimiter.calculate(this.xSpeed.get());
ySpeed = yLimiter.calculate(this.ySpeed.get());
rotationSpeed = rotationLimiter.calculate(this.rotationSpeed.get());
}

drivetrain.drive(xSpeed, ySpeed, rotationSpeed, fieldRelative, usePID);
}

public void end(boolean interrupted) {
drivetrain.stop();
}
}
102 changes: 102 additions & 0 deletions src/main/java/frc/robot/subsystems/Drivetrain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package frc.robot.subsystems;

import com.spikes2212.command.DashboardedSubsystem;
import com.studica.frc.AHRS;
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;

public class Drivetrain extends DashboardedSubsystem {

public static final double MAX_SPEED = 4;
public static final double MIN_SPEED = 0.05;
public static final double MAX_TURN_SPEED = 3;

private static final double TRACK_WIDTH = 0.6;
private static final double TRACK_LENGTH = 0.6;

private static final Translation2d CENTER_OF_ROBOT = new Translation2d(0, 0);
private static final Translation2d FRONT_LEFT_WHEEL_POSITION =
new Translation2d(TRACK_WIDTH / 2, TRACK_LENGTH / 2);
private static final Translation2d FRONT_RIGHT_WHEEL_POSITION =
new Translation2d(TRACK_WIDTH / 2, -TRACK_LENGTH / 2);
private static final Translation2d BACK_LEFT_WHEEL_POSITION =
new Translation2d(-TRACK_WIDTH / 2, TRACK_LENGTH / 2);
private static final Translation2d BACK_RIGHT_WHEEL_POSITION =
new Translation2d(-TRACK_WIDTH / 2, -TRACK_LENGTH / 2);

private static final String NAMESPACE_NAME = "drivetrain";

private final SwerveModule frontLeft;
private final SwerveModule frontRight;
private final SwerveModule backLeft;
private final SwerveModule backRight;
private final AHRS gyro;
private final SwerveDriveKinematics kinematics;

private static Drivetrain instance;

public static Drivetrain getInstance() {
if (instance == null) {
instance = new Drivetrain(SwerveModuleHolder.getFrontLeft(), SwerveModuleHolder.getFrontRight(),
SwerveModuleHolder.getBackLeft(), SwerveModuleHolder.getBackRight(),
new AHRS(AHRS.NavXComType.kMXP_SPI));
}
return instance;
}

private Drivetrain(SwerveModule frontLeft, SwerveModule frontRight, SwerveModule backLeft,
SwerveModule backRight, AHRS gyro) {
super(NAMESPACE_NAME);
this.frontLeft = frontLeft;
this.frontRight = frontRight;
this.backLeft = backLeft;
this.backRight = backRight;
this.gyro = gyro;
kinematics = new SwerveDriveKinematics(FRONT_LEFT_WHEEL_POSITION,
FRONT_RIGHT_WHEEL_POSITION, BACK_LEFT_WHEEL_POSITION, BACK_RIGHT_WHEEL_POSITION);
configureDashboard();
}

public void drive(double xSpeed, double ySpeed, double rotationSpeed, boolean fieldRelative,
boolean usePID) {
ChassisSpeeds speeds;
if (fieldRelative) {
speeds = ChassisSpeeds.fromFieldRelativeSpeeds(xSpeed, ySpeed, rotationSpeed,
gyro.getRotation2d());
}
else {
speeds = new ChassisSpeeds(xSpeed, ySpeed, rotationSpeed);
}
SwerveModuleState[] states = kinematics.toSwerveModuleStates(speeds, CENTER_OF_ROBOT);
SwerveDriveKinematics.desaturateWheelSpeeds(states, MAX_SPEED);
frontLeft.set(states[0], usePID);
frontRight.set(states[1], usePID);
backLeft.set(states[2], usePID);
backRight.set(states[3], usePID);
}

public void stop() {
frontLeft.stop();
frontRight.stop();
backLeft.stop();
backRight.stop();
}

public void resetRelativeEncoders() {
frontLeft.resetRelativeEncoder();
frontRight.resetRelativeEncoder();
backLeft.resetRelativeEncoder();
backRight.resetRelativeEncoder();
}

public void resetGyro() {
gyro.reset();
}

@Override
public void configureDashboard() {
namespace.putNumber("gyro yaw", gyro::getAngle);
}
}
144 changes: 144 additions & 0 deletions src/main/java/frc/robot/subsystems/SwerveModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package frc.robot.subsystems;

import com.ctre.phoenix6.configs.MagnetSensorConfigs;
import com.ctre.phoenix6.hardware.CANcoder;
import com.ctre.phoenix6.signals.SensorDirectionValue;
import com.spikes2212.command.DashboardedSubsystem;
import com.spikes2212.control.FeedForwardSettings;
import com.spikes2212.control.PIDSettings;
import com.spikes2212.util.UnifiedControlMode;
import com.spikes2212.util.smartmotorcontrollers.SparkWrapper;
import com.spikes2212.util.smartmotorcontrollers.TalonFXWrapper;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModuleState;

public class SwerveModule extends DashboardedSubsystem {

private static final double DRIVE_GEAR_RATIO = 1 / 6.12;
private static final double TURN_GEAR_RATIO = 1 / 12.8;
private static final double WHEEL_DIAMETER_INCHES = 4;
private static final double INCHES_TO_METERS = 0.0254;
private static final double DEGREES_IN_ROTATIONS = 360;
private static final double SECONDS_IN_MINUTE = 60;

private static final double MAX_DISTANCE_TO_ROTATE = 90;
private static final double DEGREES_TO_FLIP = 180;
private static final double ABSOLUTE_POSITION_DISCONTINUITY_POINT = 1;

private final TalonFXWrapper driveMotor;
private final SparkWrapper turnMotor;
private final CANcoder absoluteEncoder;

private final boolean cancoderInverted;
private final boolean driveInverted;
private final double offset;

private final PIDSettings drivePIDSettings;
private final PIDSettings turnPIDSettings;
private final FeedForwardSettings driveFeedForwardSettings;
private final FeedForwardSettings turnFeedForwardSettings;

public SwerveModule(String namespace, TalonFXWrapper driveMotor, SparkWrapper turnMotor, CANcoder absoluteEncoder,
boolean cancoderInverted, boolean driveInverted, double offset,
PIDSettings drivePIDSettings, PIDSettings turnPIDSettings,
FeedForwardSettings driveFeedForwardSettings,
FeedForwardSettings turnFeedForwardSettings) {
super(namespace);
this.driveMotor = driveMotor;
this.turnMotor = turnMotor;
this.absoluteEncoder = absoluteEncoder;
this.cancoderInverted = cancoderInverted;
this.driveInverted = driveInverted;
this.offset = offset;
this.drivePIDSettings = drivePIDSettings;
this.turnPIDSettings = turnPIDSettings;
this.driveFeedForwardSettings = driveFeedForwardSettings;
this.turnFeedForwardSettings = turnFeedForwardSettings;
configureDriveController();
configureTurnController();
configureAbsoluteEncoder();
configureDashboard();
}

public void configureDriveController() {
//@TODO check if this is correct
driveMotor.setEncoderConversionFactor(DRIVE_GEAR_RATIO * WHEEL_DIAMETER_INCHES
* INCHES_TO_METERS * Math.PI);
driveMotor.setInverted(driveInverted);
}

public void configureTurnController() {
turnMotor.setPositionConversionFactor(TURN_GEAR_RATIO * DEGREES_IN_ROTATIONS);
turnMotor.setVelocityConversionFactor((TURN_GEAR_RATIO * DEGREES_IN_ROTATIONS) / SECONDS_IN_MINUTE);
turnMotor.setInverted(cancoderInverted);
}

public void configureAbsoluteEncoder() {
MagnetSensorConfigs magnetConfigs = new MagnetSensorConfigs()
.withAbsoluteSensorDiscontinuityPoint(ABSOLUTE_POSITION_DISCONTINUITY_POINT)
.withSensorDirection(cancoderInverted ? SensorDirectionValue.Clockwise_Positive :
SensorDirectionValue.CounterClockwise_Positive).withMagnetOffset(offset);
absoluteEncoder.getConfigurator().apply(magnetConfigs);
}

private void setSpeed(double speed, boolean usePID) {
if (usePID) {
driveMotor.pidSet(UnifiedControlMode.VELOCITY, speed, drivePIDSettings, driveFeedForwardSettings,
true);
} else driveMotor.set(speed / Drivetrain.MAX_SPEED);
}

private void setAngle(double angle) {
turnMotor.pidSet(UnifiedControlMode.POSITION, angle, turnPIDSettings, turnFeedForwardSettings,
true);
}

public void stop() {
driveMotor.stopMotor();
turnMotor.stopMotor();
}

public void set(SwerveModuleState state, boolean usePID) {
if (Math.abs(state.speedMetersPerSecond) < Drivetrain.MIN_SPEED) {
stop();
return;
}
state = optimize(state, turnMotor.getPosition());
setAngle(state.angle.getDegrees());
setSpeed(state.speedMetersPerSecond, usePID);
}

private double normalizeAngleRelativeToEncoder(double currentAngle, double desiredAngle) {
int rotations = (int) (currentAngle / DEGREES_IN_ROTATIONS);
if (currentAngle < 0) rotations--;
desiredAngle += rotations * DEGREES_IN_ROTATIONS;
return desiredAngle;
}

private SwerveModuleState optimize(SwerveModuleState state, double currentAngle) {
double desiredAngle = normalizeAngleRelativeToEncoder(currentAngle, state.angle.getDegrees());
while (Math.abs(desiredAngle - currentAngle) > MAX_DISTANCE_TO_ROTATE) {
if (desiredAngle - currentAngle > 0) {
desiredAngle -= DEGREES_TO_FLIP;
} else {
desiredAngle += DEGREES_TO_FLIP;
}
state.speedMetersPerSecond *= -1;
}
return new SwerveModuleState(state.speedMetersPerSecond, Rotation2d.fromDegrees(desiredAngle));
}

public double getAbsoluteAngle() {
return absoluteEncoder.getAbsolutePosition().getValueAsDouble() * DEGREES_IN_ROTATIONS;
}

public void resetRelativeEncoder() {
turnMotor.setPosition(getAbsoluteAngle());
}

@Override
public void configureDashboard() {
namespace.putNumber("absolute angle", this::getAbsoluteAngle);
namespace.putNumber("relative angle", turnMotor::getPosition);
}
}
Loading