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-04 11:22:43 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXControlMode;
|
|
|
|
|
|
|
|
|
|
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;
|
2023-02-04 11:22:43 -07:00
|
|
|
import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
|
|
|
|
|
import edu.wpi.first.math.kinematics.SwerveModulePosition;
|
2023-01-14 11:42:03 -07:00
|
|
|
import edu.wpi.first.math.kinematics.SwerveModuleState;
|
|
|
|
|
import edu.wpi.first.math.util.Units;
|
2023-02-04 11:22:43 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2023-01-14 11:42:03 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
|
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
2023-02-04 11:22:43 -07:00
|
|
|
import frc4388.utility.RobotGyro;
|
2023-01-14 11:42:03 -07:00
|
|
|
|
|
|
|
|
public class SwerveDrive extends SubsystemBase {
|
|
|
|
|
|
2023-02-04 11:22:43 -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));
|
|
|
|
|
|
2023-02-04 11:22:43 -07:00
|
|
|
private SwerveDriveKinematics kinematics = new SwerveDriveKinematics(leftFrontLocation, rightFrontLocation, leftBackLocation, rightBackLocation);
|
|
|
|
|
|
|
|
|
|
private RobotGyro gyro;
|
|
|
|
|
|
|
|
|
|
private SwerveDriveOdometry odometry;
|
2023-01-14 11:42:03 -07:00
|
|
|
|
2023-01-15 16:32:41 -07:00
|
|
|
public double speedAdjust = SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW; // * slow by default
|
|
|
|
|
|
2023-01-14 11:42:03 -07:00
|
|
|
/** Creates a new SwerveDrive. */
|
2023-02-04 11:22:43 -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;
|
2023-02-04 11:22:43 -07:00
|
|
|
this.gyro = gyro;
|
|
|
|
|
|
|
|
|
|
this.odometry = new SwerveDriveOdometry(
|
|
|
|
|
kinematics,
|
|
|
|
|
gyro.getRotation2d(),
|
|
|
|
|
new SwerveModulePosition[] {
|
|
|
|
|
leftFront.getPosition(),
|
|
|
|
|
rightFront.getPosition(),
|
|
|
|
|
leftBack.getPosition(),
|
|
|
|
|
rightBack.getPosition()
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-01-14 11:42:03 -07:00
|
|
|
|
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-02 20:54:12 -07:00
|
|
|
// WPILib swerve drive example
|
2023-01-24 17:57:48 -07:00
|
|
|
public void drive(double xSpeed, double ySpeed, double rot, boolean fieldRelative) {
|
|
|
|
|
// SwerveModuleState[] states = kinematics.toSwerveModuleStates(
|
2023-02-04 11:22:43 -07:00
|
|
|
// fieldRelative ? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeed, ySpeed, rot, Rotation2d.fromDegrees(-gyro.getRotation2d().getDegrees()))
|
2023-01-24 17:57:48 -07:00
|
|
|
// : new ChassisSpeeds(xSpeed, ySpeed, rot)
|
|
|
|
|
// );
|
2023-02-04 11:22:43 -07:00
|
|
|
|
|
|
|
|
SwerveModuleState[] states = kinematics.toSwerveModuleStates(new ChassisSpeeds(0.3, 0, 0));
|
|
|
|
|
|
|
|
|
|
// // SwerveModuleState[] states = kinematics.toSwerveModuleStates(new ChassisSpeeds(xSpeed, ySpeed, rot * SwerveDriveConstants.ROTATION_SPEED));
|
2023-01-24 17:57:48 -07:00
|
|
|
setModuleStates(states);
|
2023-02-04 11:22:43 -07:00
|
|
|
// modules[0].getDriveMotor().set(0.2);
|
|
|
|
|
// modules[1].getDriveMotor().set(0.2);
|
|
|
|
|
// modules[2].getDriveMotor().set(0.2);
|
|
|
|
|
// modules[3].getDriveMotor().set(0.2);
|
|
|
|
|
|
|
|
|
|
// modules[0].getAngleMotor().set(TalonFXControlMode.Position, 1017);
|
|
|
|
|
// modules[1].getAngleMotor().set(TalonFXControlMode.Position, 509);
|
|
|
|
|
// modules[2].getAngleMotor().set(TalonFXControlMode.Position, 683);
|
|
|
|
|
// modules[3].getAngleMotor().set(TalonFXControlMode.Position, 1816);
|
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-01-21 15:16:22 -07:00
|
|
|
SwerveDriveKinematics.desaturateWheelSpeeds(desiredStates, Units.metersToFeet(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];
|
|
|
|
|
module.setDesiredState(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 11:22:43 -07:00
|
|
|
public double getGyroAngle() {
|
|
|
|
|
return gyro.getAngle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetGyro() {
|
|
|
|
|
gyro.reset();
|
|
|
|
|
setOdometry(getOdometry());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 20:25:07 -07:00
|
|
|
/**
|
|
|
|
|
* Updates the odometry of the SwerveDrive.
|
|
|
|
|
*/
|
2023-02-04 11:22:43 -07:00
|
|
|
public void updateOdometry() {
|
|
|
|
|
odometry.update(
|
|
|
|
|
gyro.getRotation2d(),
|
|
|
|
|
new SwerveModulePosition[] {
|
|
|
|
|
leftFront.getPosition(),
|
|
|
|
|
rightFront.getPosition(),
|
|
|
|
|
leftBack.getPosition(),
|
|
|
|
|
rightBack.getPosition()
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-01-15 20:25:07 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the odometry of the SwerveDrive.
|
|
|
|
|
* @return The odometry of the SwerveDrive as a Pose2d object (xMeters, yMeters, theta).
|
|
|
|
|
*/
|
2023-02-04 11:22:43 -07:00
|
|
|
public Pose2d getOdometry() {
|
|
|
|
|
return odometry.getPoseMeters();
|
|
|
|
|
}
|
2023-01-15 20:25:07 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the odometry of the SwerveDrive.
|
|
|
|
|
* @param pose Pose to set the odometry to.
|
|
|
|
|
*/
|
2023-02-04 11:22:43 -07:00
|
|
|
public void setOdometry(Pose2d pose) {
|
|
|
|
|
odometry.resetPosition(
|
|
|
|
|
gyro.getRotation2d(),
|
|
|
|
|
new SwerveModulePosition[] {
|
|
|
|
|
leftFront.getPosition(),
|
|
|
|
|
rightFront.getPosition(),
|
|
|
|
|
leftBack.getPosition(),
|
|
|
|
|
rightBack.getPosition()
|
|
|
|
|
},
|
|
|
|
|
pose
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-01-15 20:25:07 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the odometry of the SwerveDrive to 0.
|
2023-02-04 11:22:43 -07:00
|
|
|
* *NOTE: If you reset your gyro, this method MUST be called with the new gyro angle and wheel encoder positions.
|
2023-01-15 20:25:07 -07:00
|
|
|
*/
|
2023-02-04 11:22:43 -07:00
|
|
|
public void resetOdometry() {
|
|
|
|
|
setOdometry(new Pose2d());
|
|
|
|
|
}
|
2023-01-20 21:04:58 -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-02-04 11:22:43 -07:00
|
|
|
updateOdometry();
|
|
|
|
|
|
|
|
|
|
// SmartDashboard.putNumberArray("Odometry", new double[] {getOdometry().getX(), getOdometry().getY(), getOdometry().getRotation().getDegrees()});
|
|
|
|
|
|
|
|
|
|
// SmartDashboard.putNumber("LF CC Angle", modules[0].getAngle().getDegrees());
|
|
|
|
|
// SmartDashboard.putNumber("RF CC Angle", modules[1].getAngle().getDegrees());
|
|
|
|
|
// SmartDashboard.putNumber("LB CC Angle", modules[2].getAngle().getDegrees());
|
|
|
|
|
// SmartDashboard.putNumber("RB CC Angle", modules[3].getAngle().getDegrees());
|
|
|
|
|
|
|
|
|
|
SmartDashboard.putNumber("LF Vel", modules[0].getDriveVel());
|
|
|
|
|
SmartDashboard.putNumber("RF Vel", modules[1].getDriveVel());
|
|
|
|
|
SmartDashboard.putNumber("LB Vel", modules[2].getDriveVel());
|
|
|
|
|
SmartDashboard.putNumber("RB Vel", modules[3].getDriveVel());
|
|
|
|
|
|
|
|
|
|
SmartDashboard.putNumber("Gyro Angle", getGyroAngle());
|
|
|
|
|
|
|
|
|
|
SmartDashboard.putNumber("LF Pos", modules[0].getDrivePos());
|
|
|
|
|
SmartDashboard.putNumber("RF Pos", modules[1].getDrivePos());
|
|
|
|
|
SmartDashboard.putNumber("LB Pos", modules[2].getDrivePos());
|
|
|
|
|
SmartDashboard.putNumber("RB Pos", modules[3].getDrivePos());
|
|
|
|
|
|
2023-01-14 11:42:03 -07:00
|
|
|
}
|
2023-01-15 16:32:41 -07:00
|
|
|
|
2023-01-15 20:25:07 -07:00
|
|
|
/**
|
|
|
|
|
* Shifts gear from high to low, or vice versa.
|
|
|
|
|
* @param shift true to shift to high, false to shift to low
|
|
|
|
|
*/
|
2023-01-15 16:32:41 -07:00
|
|
|
public void highSpeed(boolean shift) {
|
|
|
|
|
this.speedAdjust = shift ? SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_FAST : SwerveDriveConstants.Conversions.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-14 11:42:03 -07:00
|
|
|
}
|