Swerve Builder updates
This commit is contained in:
parent
9b7572f22e
commit
6f233e586f
|
|
@ -31,7 +31,8 @@ repositories {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
|
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
|
||||||
|
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||||
|
|
||||||
// WPI Lib
|
// WPI Lib
|
||||||
implementation "edu.wpi.first.cscore:cscore-java:$wpiVersion"
|
implementation "edu.wpi.first.cscore:cscore-java:$wpiVersion"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import edu.wpi.first.math.geometry.Translation2d;
|
||||||
public class SwerveBuilder {
|
public class SwerveBuilder {
|
||||||
|
|
||||||
ArrayList<SwerveModule> modules = new ArrayList<SwerveModule>();
|
ArrayList<SwerveModule> modules = new ArrayList<SwerveModule>();
|
||||||
String canBusName = "";
|
String canBusName = "rio";
|
||||||
|
|
||||||
private int pigeonId;
|
private int pigeonId;
|
||||||
|
|
||||||
|
|
@ -49,6 +49,7 @@ public class SwerveBuilder {
|
||||||
private Translation2d moduleLocation;
|
private Translation2d moduleLocation;
|
||||||
private double gearRatio;
|
private double gearRatio;
|
||||||
private double wheelDiameterInMeters;
|
private double wheelDiameterInMeters;
|
||||||
|
private String moduleName;
|
||||||
|
|
||||||
public SwerveModuleBuilder() {
|
public SwerveModuleBuilder() {
|
||||||
|
|
||||||
|
|
@ -114,9 +115,14 @@ public class SwerveBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SwerveModuleBuilder withModuleName(String moduleName){
|
||||||
|
this.moduleName = moduleName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public SwerveModule build() {
|
public SwerveModule build() {
|
||||||
return new SwerveModule(driveMotorId, driveKP, driveKI, driveKD, steerMotorId, steerKP, steerKI, steerKD,
|
return new SwerveModule(driveMotorId, driveKP, driveKI, driveKD, steerMotorId, steerKP, steerKI, steerKD,
|
||||||
steerEncoderId, steerOffset, canBusName, moduleLocation, gearRatio, wheelDiameterInMeters);
|
steerEncoderId, steerOffset, canBusName, moduleLocation, gearRatio, wheelDiameterInMeters, moduleName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ package org.fairportrobotics.frc.robolib.DriveSystems.SwerveDrive;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.littletonrobotics.junction.Logger;
|
|
||||||
|
|
||||||
import com.ctre.phoenix6.hardware.Pigeon2;
|
import com.ctre.phoenix6.hardware.Pigeon2;
|
||||||
|
|
||||||
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator3d;
|
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator3d;
|
||||||
|
|
@ -44,12 +42,12 @@ public class SwerveDriveSubsystem extends SubsystemBase{
|
||||||
gyro = new Pigeon2(pigeonId);
|
gyro = new Pigeon2(pigeonId);
|
||||||
|
|
||||||
poseEstimator = new SwerveDrivePoseEstimator3d(driveKiniematics, new Rotation3d(Rotation2d.fromRotations(getCurrentYaw().magnitude())), getModulePositions(), Pose3d.kZero);
|
poseEstimator = new SwerveDrivePoseEstimator3d(driveKiniematics, new Rotation3d(Rotation2d.fromRotations(getCurrentYaw().magnitude())), getModulePositions(), Pose3d.kZero);
|
||||||
|
chassisSpeeds = new ChassisSpeeds();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void periodic() {
|
public void periodic() {
|
||||||
SwerveModuleState[] moduleStates = driveKiniematics.toSwerveModuleStates(chassisSpeeds, centerOfRotation);
|
SwerveModuleState[] moduleStates = driveKiniematics.toSwerveModuleStates(chassisSpeeds, centerOfRotation);
|
||||||
Logger.recordOutput("Un-Optimized Swerve States", moduleStates);
|
|
||||||
|
|
||||||
for(int i=0; i<modules.length;i++){
|
for(int i=0; i<modules.length;i++){
|
||||||
moduleStates[i].optimize(modules[i].getCurrentSteerRotations());
|
moduleStates[i].optimize(modules[i].getCurrentSteerRotations());
|
||||||
|
|
@ -57,8 +55,6 @@ public class SwerveDriveSubsystem extends SubsystemBase{
|
||||||
|
|
||||||
poseEstimator.update(new Rotation3d(Rotation2d.fromRotations(getCurrentYaw().magnitude())), getModulePositions());
|
poseEstimator.update(new Rotation3d(Rotation2d.fromRotations(getCurrentYaw().magnitude())), getModulePositions());
|
||||||
|
|
||||||
Logger.recordOutput("Optimized Swerve States", moduleStates);
|
|
||||||
|
|
||||||
for(int i=0;i<modules.length;i++){
|
for(int i=0;i<modules.length;i++){
|
||||||
modules[i].setModuleState(moduleStates[i]);
|
modules[i].setModuleState(moduleStates[i]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ public class SwerveModule {
|
||||||
private PositionVoltage steerRequest = new PositionVoltage(0);
|
private PositionVoltage steerRequest = new PositionVoltage(0);
|
||||||
private CANcoder steerEncoder;
|
private CANcoder steerEncoder;
|
||||||
|
|
||||||
|
private String moduleName;
|
||||||
|
|
||||||
private Translation2d moduleLocation;
|
private Translation2d moduleLocation;
|
||||||
|
|
||||||
public SwerveModule(
|
public SwerveModule(
|
||||||
|
|
@ -49,7 +51,8 @@ public class SwerveModule {
|
||||||
String canBusName,
|
String canBusName,
|
||||||
Translation2d moduleLocation,
|
Translation2d moduleLocation,
|
||||||
double gearRatio,
|
double gearRatio,
|
||||||
double wheelDiameterInMeters
|
double wheelDiameterInMeters,
|
||||||
|
String moduleName
|
||||||
){
|
){
|
||||||
driveMotor = new TalonFX(driveMotorId, canBusName);
|
driveMotor = new TalonFX(driveMotorId, canBusName);
|
||||||
driveMotor.getVelocity().setUpdateFrequency(CAN_UPDATE_FREQUENCY);
|
driveMotor.getVelocity().setUpdateFrequency(CAN_UPDATE_FREQUENCY);
|
||||||
|
|
@ -69,6 +72,8 @@ public class SwerveModule {
|
||||||
|
|
||||||
this.gearRatio = gearRatio;
|
this.gearRatio = gearRatio;
|
||||||
this.wheelDiameterInMeters = wheelDiameterInMeters;
|
this.wheelDiameterInMeters = wheelDiameterInMeters;
|
||||||
|
|
||||||
|
this.moduleName = moduleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSteerRotations(double rotations){
|
public void setSteerRotations(double rotations){
|
||||||
|
|
@ -80,7 +85,7 @@ public class SwerveModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDriveSpeed(double speed){
|
public void setDriveSpeed(double speed){
|
||||||
double rotorVelocity = ((Math.PI * wheelDiameterInMeters) / speed) * gearRatio * TALON_BUILT_IN_ENCODER_UNITS_PER_ROTATION;
|
double rotorVelocity = ((Math.PI * wheelDiameterInMeters) / speed) * gearRatio;
|
||||||
driveMotor.setControl(driveRequest.withSlot(0).withVelocity(rotorVelocity));
|
driveMotor.setControl(driveRequest.withSlot(0).withVelocity(rotorVelocity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,7 +111,7 @@ public class SwerveModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
private double getModuleDistance(){
|
private double getModuleDistance(){
|
||||||
double distance = (this.driveMotor.getPosition(true).getValueAsDouble() / TALON_BUILT_IN_ENCODER_UNITS_PER_ROTATION) / gearRatio / (Math.PI * wheelDiameterInMeters);
|
double distance = (this.driveMotor.getPosition(true).getValueAsDouble()) / gearRatio / (Math.PI * wheelDiameterInMeters);
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* This Java source file was generated by the Gradle 'init' task.
|
* This Java source file was generated by the Gradle 'init' task.
|
||||||
*/
|
*/
|
||||||
package com.fairportrobotics.frc.robolib;
|
package org.fairportrobotics.frc.robolib;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue