Files
2023WayOfTheRobot/src/main/java/frc4388/robot/subsystems/SwerveDrive.java
T

131 lines
5.4 KiB
Java
Raw Normal View History

2023-01-14 11:42:03 -07: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;
2023-02-10 17:12:53 -07:00
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
2023-01-14 11:42:03 -07:00
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;
2023-03-15 13:03:02 -06:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2023-01-14 11:42:03 -07:00
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2023-02-27 18:18:29 -07:00
import edu.wpi.first.math.kinematics.SwerveModulePosition;
2023-01-14 11:42:03 -07:00
import frc4388.robot.Constants.SwerveDriveConstants;
2023-02-27 17:48:04 -07:00
import frc4388.utility.RobotGyro;
2023-01-14 11:42:03 -07:00
public class SwerveDrive extends SubsystemBase {
2023-02-27 18:35:20 -07:00
private SwerveModule leftFront;
private SwerveModule rightFront;
private SwerveModule leftBack;
private SwerveModule rightBack;
2023-01-14 11:42:03 -07:00
private SwerveModule[] modules;
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);
private RobotGyro gyro;
2023-01-15 16:32:41 -07:00
public double speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW; // * slow by default
2023-02-27 18:20:24 -07:00
2023-03-15 13:03:02 -06:00
public double rotTarget = 0;
2023-02-27 18:20:24 -07:00
public ChassisSpeeds chassisSpeeds = new ChassisSpeeds();
2023-01-15 16:32:41 -07:00
2023-01-14 11:42:03 -07:00
/** Creates a new SwerveDrive. */
2023-02-27 18:24:07 -07:00
public SwerveDrive(SwerveModule leftFront, SwerveModule rightFront, SwerveModule leftBack, SwerveModule rightBack, RobotGyro gyro) {
2023-01-14 11:42:03 -07:00
this.leftFront = leftFront;
this.rightFront = rightFront;
this.leftBack = leftBack;
this.rightBack = rightBack;
this.gyro = gyro;
2023-01-15 15:58:40 -07:00
this.modules = new SwerveModule[] {this.leftFront, this.rightFront, this.leftBack, this.rightBack};
2023-01-14 11:42:03 -07:00
}
2023-02-04 15:06:42 -07:00
public void driveWithInput(Translation2d leftStick, Translation2d rightStick, boolean fieldRelative) {
if (fieldRelative) {
2023-03-11 14:03:36 -07:00
double rot = 0;
2023-03-15 13:03:02 -06:00
if (rightStick.getNorm() > 0.05) {
rotTarget = gyro.getAngle();
2023-03-15 09:57:01 -06:00
rot = rightStick.getX() * SwerveDriveConstants.ROTATION_SPEED;
2023-03-15 13:03:02 -06:00
SmartDashboard.putBoolean("drift correction", false);
2023-03-11 14:03:36 -07:00
} else {
2023-03-15 13:03:02 -06:00
SmartDashboard.putBoolean("drift correction", true);
rot = ((rotTarget - gyro.getAngle()) / 360) * SwerveDriveConstants.ROT_CORRECTION_SPEED;
2023-02-04 15:06:42 -07:00
}
2023-03-11 14:03:36 -07:00
// Use the left joystick to set speed. Apply a cubic curve and the set max speed.
2023-02-04 15:06:42 -07:00
Translation2d speed = leftStick.times(leftStick.getNorm() * speedAdjust);
2023-03-06 16:35:20 -07:00
Translation2d cubedSpeed = new Translation2d(Math.pow(speed.getX(), 3.00), Math.pow(speed.getY(), 3.00));
2023-02-04 15:06:42 -07:00
// Convert field-relative speeds to robot-relative speeds.
2023-03-15 09:57:01 -06:00
chassisSpeeds = ChassisSpeeds.fromFieldRelativeSpeeds(-1 * cubedSpeed.getX(), cubedSpeed.getY(), rot, gyro.getRotation2d().times(-1));
2023-02-07 23:32:19 -07:00
} else {
2023-02-04 15:06:42 -07:00
// Create robot-relative speeds.
chassisSpeeds = new ChassisSpeeds(-1 * leftStick.getX(), leftStick.getY(), rightStick.getX() * SwerveDriveConstants.ROTATION_SPEED);
}
setModuleStates(kinematics.toSwerveModuleStates(chassisSpeeds));
2023-01-24 17:57:48 -07:00
}
2023-01-14 11:42:03 -07: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) {
2023-02-27 18:35:20 -07:00
SwerveDriveKinematics.desaturateWheelSpeeds(desiredStates, Units.feetToMeters(SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
2023-01-14 11:42:03 -07:00
for (int i = 0; i < desiredStates.length; i++) {
SwerveModule module = modules[i];
SwerveModuleState state = desiredStates[i];
2023-02-16 18:46:43 -07:00
module.setDesiredState(state);
2023-01-14 11:42:03 -07:00
}
}
public double getGyroAngle() {
return gyro.getAngle();
}
public void resetGyro() {
gyro.reset();
2023-03-15 13:03:02 -06:00
rotTarget = 0;
}
2023-02-17 19:53:58 -07:00
public void stopModules() {
for (SwerveModule module : this.modules) {
module.stop();
}
}
2023-02-10 17:12:53 -07:00
2023-01-31 19:24:12 -07:00
public SwerveDriveKinematics getKinematics() {
return this.kinematics;
}
2023-01-14 11:42:03 -07:00
@Override
public void periodic() {
// This method will be called once per scheduler run
}
2023-01-15 16:32:41 -07:00
2023-03-15 13:03:02 -06:00
public void toggleGear(double angle) {
if (this.speedAdjust == SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW
&& Math.abs(angle) < 2) {
2023-03-11 14:03:36 -07:00
this.speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_FAST;
2023-03-15 13:03:02 -06:00
SwerveDriveConstants.ROT_CORRECTION_SPEED = SwerveDriveConstants.CORRECTION_MAX;
2023-03-11 14:03:36 -07:00
} else {
this.speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
2023-03-15 13:03:02 -06:00
SwerveDriveConstants.ROT_CORRECTION_SPEED = SwerveDriveConstants.CORRECTION_MIN;
2023-03-11 14:03:36 -07:00
}
2023-01-15 16:32:41 -07:00
}
2023-01-14 11:42:03 -07:00
}