Files
Robot-Essentials/src/main/java/frc4388/robot/subsystems/SwerveDrive.java
T

196 lines
7.2 KiB
Java
Raw Normal View History

2022-11-01 17:43:01 -06: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.
package frc4388.robot.subsystems;
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;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc4388.robot.Constants.SwerveDriveConstants;
2024-01-05 13:56:01 -07:00
import frc4388.utility.RobotGyro;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2022-11-01 17:43:01 -06:00
public class SwerveDrive extends SubsystemBase {
2024-01-05 13:56:01 -07:00
private SwerveModule leftFront;
private SwerveModule rightFront;
private SwerveModule leftBack;
private SwerveModule rightBack;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
private SwerveModule[] modules;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
private Translation2d leftFrontLocation = new Translation2d(Units.inchesToMeters(SwerveDriveConstants.HALF_HEIGHT), Units.inchesToMeters(SwerveDriveConstants.HALF_WIDTH));
private Translation2d rightFrontLocation = new Translation2d(Units.inchesToMeters(SwerveDriveConstants.HALF_HEIGHT), -Units.inchesToMeters(SwerveDriveConstants.HALF_WIDTH));
private Translation2d leftBackLocation = new Translation2d(-Units.inchesToMeters(SwerveDriveConstants.HALF_HEIGHT), Units.inchesToMeters(SwerveDriveConstants.HALF_WIDTH));
private Translation2d rightBackLocation = new Translation2d(-Units.inchesToMeters(SwerveDriveConstants.HALF_HEIGHT), -Units.inchesToMeters(SwerveDriveConstants.HALF_WIDTH));
private SwerveDriveKinematics kinematics = new SwerveDriveKinematics(leftFrontLocation, rightFrontLocation, leftBackLocation, rightBackLocation);
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
private RobotGyro gyro;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
public double speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW; // * slow by default
public double rotTarget = 0.0;
public ChassisSpeeds chassisSpeeds = new ChassisSpeeds();
/** Creates a new SwerveDrive. */
public SwerveDrive(SwerveModule leftFront, SwerveModule rightFront, SwerveModule leftBack, SwerveModule rightBack, RobotGyro gyro) {
this.leftFront = leftFront;
this.rightFront = rightFront;
this.leftBack = leftBack;
this.rightBack = rightBack;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
this.gyro = gyro;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
this.modules = new SwerveModule[] {this.leftFront, this.rightFront, this.leftBack, this.rightBack};
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
boolean stopped = false;
public void driveWithInput(Translation2d leftStick, Translation2d rightStick, boolean fieldRelative) {
if (fieldRelative) {
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
double rot = 0;
if (rightStick.getNorm() > 0.05) {
rotTarget = gyro.getAngle();
rot = rightStick.getX() * SwerveDriveConstants.ROTATION_SPEED;
SmartDashboard.putBoolean("drift correction", false);
stopped = false;
} else if(leftStick.getNorm() > 0.05) {
if (!stopped) {
stopModules();
stopped = true;
}
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
SmartDashboard.putBoolean("drift correction", true);
rot = ((rotTarget - gyro.getAngle()) / 360) * SwerveDriveConstants.ROT_CORRECTION_SPEED;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
}
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
// Use the left joystick to set speed. Apply a cubic curve and the set max speed.
Translation2d speed = leftStick.times(leftStick.getNorm() * speedAdjust);
// Translation2d cubedSpeed = new Translation2d(Math.pow(speed.getX(), 3.00), Math.pow(speed.getY(), 3.00));
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
// Convert field-relative speeds to robot-relative speeds.
chassisSpeeds = ChassisSpeeds.fromFieldRelativeSpeeds(-1 * speed.getX(), speed.getY(), rightStick.getX() * SwerveDriveConstants.ROTATION_SPEED, gyro.getRotation2d().times(-1));
} else {
// Create robot-relative speeds.
chassisSpeeds = new ChassisSpeeds(-1 * leftStick.getX(), leftStick.getY(), rightStick.getX() * SwerveDriveConstants.ROTATION_SPEED);
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
setModuleStates(kinematics.toSwerveModuleStates(chassisSpeeds));
2022-11-01 17:43:01 -06:00
}
/**
* Set each module of the swerve drive to the corresponding desired state.
* @param desiredStates Array of module states to set.
*/
public void setModuleStates(SwerveModuleState[] desiredStates) {
2024-01-05 13:56:01 -07:00
SwerveDriveKinematics.desaturateWheelSpeeds(desiredStates, Units.feetToMeters(SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
2022-11-01 17:43:01 -06:00
for (int i = 0; i < desiredStates.length; i++) {
SwerveModule module = modules[i];
SwerveModuleState state = desiredStates[i];
2024-01-05 13:56:01 -07:00
module.setDesiredState(state);
2022-11-01 17:43:01 -06:00
}
}
2024-01-05 13:56:01 -07:00
public boolean rotateToTarget(double angle) {
double currentAngle = getGyroAngle();
double error = angle - currentAngle;
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
driveWithInput(new Translation2d(0, 0), new Translation2d(error / Math.abs(error) * 0.3, 0), true);
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
if (Math.abs(angle - getGyroAngle()) < 5.0) {
return true;
}
2022-11-01 17:43:01 -06:00
2024-01-05 13:56:01 -07:00
return false;
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public double getGyroAngle() {
return gyro.getAngle();
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void resetGyro() {
gyro.reset();
rotTarget = 0.0;
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void stopModules() {
for (SwerveModule module : this.modules) {
module.stop();
}
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public SwerveDriveKinematics getKinematics() {
return this.kinematics;
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
@Override
public void periodic() {
// This method will be called once per scheduler run\
SmartDashboard.putNumber("Gyro", getGyroAngle());
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void shiftDown() {
if (Math.abs(this.speedAdjust - SwerveDriveConstants.SLOW_SPEED) < .01) {
} else if (Math.abs(this.speedAdjust - SwerveDriveConstants.FAST_SPEED) < .01) {
this.speedAdjust = SwerveDriveConstants.SLOW_SPEED;
} else {
this.speedAdjust = SwerveDriveConstants.FAST_SPEED;
}
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void setToSlow() {
this.speedAdjust = SwerveDriveConstants.SLOW_SPEED;
System.out.println("SLOW");
System.out.println("SLOW");
System.out.println("SLOW");
System.out.println("SLOW");
System.out.println("SLOW");
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void setToFast() {
this.speedAdjust = SwerveDriveConstants.FAST_SPEED;
System.out.println("FAST");
System.out.println("FAST");
System.out.println("FAST");
System.out.println("FAST");
System.out.println("FAST");
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void setToTurbo() {
this.speedAdjust = SwerveDriveConstants.TURBO_SPEED;
System.out.println("TURBO");
System.out.println("TURBO");
System.out.println("TURBO");
System.out.println("TURBO");
System.out.println("TURBO");
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
public void shiftUp() {
if (Math.abs(this.speedAdjust - SwerveDriveConstants.SLOW_SPEED) < .01) {
this.speedAdjust = SwerveDriveConstants.FAST_SPEED;
} else if (Math.abs(this.speedAdjust - SwerveDriveConstants.FAST_SPEED) < .01) {
this.speedAdjust = SwerveDriveConstants.TURBO_SPEED;
2022-11-01 17:43:01 -06:00
} else {
2024-01-05 13:56:01 -07:00
2022-11-01 17:43:01 -06:00
}
}
2024-01-05 13:56:01 -07:00
public void toggleGear(double angle) {
if (Math.abs(this.speedAdjust - SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW) < .01 && Math.abs(angle) < 10) {
this.speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_FAST;
SwerveDriveConstants.ROT_CORRECTION_SPEED = SwerveDriveConstants.CORRECTION_MIN;
} else {
this.speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
SwerveDriveConstants.ROT_CORRECTION_SPEED = SwerveDriveConstants.CORRECTION_MIN;
}
2022-11-01 17:43:01 -06:00
}
2024-01-05 13:56:01 -07:00
}