Files
2022NoWayHome/src/main/java/frc4388/robot/subsystems/SwerveDrive.java
T

262 lines
9.8 KiB
Java
Raw Normal View History

2022-01-11 11:54:44 -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.
2021-11-15 17:52:28 -07:00
package frc4388.robot.subsystems;
2022-03-14 18:29:11 -06:00
import com.ctre.phoenix.sensors.WPI_Pigeon2;
2021-11-29 17:18:31 -07:00
2022-01-18 19:53:14 -07:00
import edu.wpi.first.math.VecBuilder;
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator;
import edu.wpi.first.math.geometry.Pose2d;
2022-01-11 11:05:52 -07:00
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
2022-02-05 11:50:49 -07:00
import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
2022-01-11 11:05:52 -07:00
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.smartdashboard.Field2d;
2022-01-27 18:58:22 -07:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2021-12-02 17:51:06 -07:00
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2022-01-29 14:49:18 -07:00
import frc4388.robot.Constants.OIConstants;
2021-11-15 17:52:28 -07:00
import frc4388.robot.Constants.SwerveDriveConstants;
2021-12-16 16:22:36 -07:00
import frc4388.utility.Gains;
2022-01-11 11:05:52 -07:00
public class SwerveDrive extends SubsystemBase {
2022-01-24 19:06:28 -07:00
private SwerveModule m_leftFront;
private SwerveModule m_leftBack;
private SwerveModule m_rightFront;
private SwerveModule m_rightBack;
2022-01-11 11:05:52 -07:00
double halfWidth = SwerveDriveConstants.WIDTH / 2.d;
double halfHeight = SwerveDriveConstants.HEIGHT / 2.d;
public static Gains m_swerveGains = SwerveDriveConstants.SWERVE_GAINS;
2022-03-10 16:47:32 -07:00
Translation2d m_frontLeftLocation = new Translation2d(Units.inchesToMeters(halfHeight), Units.inchesToMeters(-halfWidth));
Translation2d m_frontRightLocation = new Translation2d(Units.inchesToMeters(halfHeight), Units.inchesToMeters(halfWidth));
Translation2d m_backLeftLocation = new Translation2d(Units.inchesToMeters(-halfHeight), Units.inchesToMeters(-halfWidth));
Translation2d m_backRightLocation = new Translation2d(Units.inchesToMeters(-halfHeight), Units.inchesToMeters(halfWidth));
2022-03-05 11:12:33 -07:00
public SwerveDriveKinematics m_kinematics = new SwerveDriveKinematics(m_frontLeftLocation, m_frontRightLocation,
m_backLeftLocation, m_backRightLocation);
2022-01-18 19:53:14 -07:00
2022-01-11 11:05:52 -07:00
public SwerveModule[] modules;
2022-03-14 18:29:11 -06:00
public WPI_Pigeon2 m_gyro;
2022-01-11 19:57:50 -07:00
2022-01-29 14:39:46 -07:00
public SwerveDrivePoseEstimator m_poseEstimator;
2022-02-05 11:50:49 -07:00
public SwerveDriveOdometry m_odometry;
2022-01-11 19:57:50 -07:00
2022-01-15 14:55:07 -07:00
public double speedAdjust = SwerveDriveConstants.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
public boolean ignoreAngles;
2022-02-19 11:18:15 -07:00
public Rotation2d rotTarget = new Rotation2d();
2022-03-06 18:02:04 -07:00
private ChassisSpeeds chassisSpeeds = new ChassisSpeeds();
2022-01-15 14:55:07 -07:00
private final Field2d m_field = new Field2d();
2022-03-05 11:12:33 -07:00
public SwerveDrive(SwerveModule leftFront, SwerveModule leftBack, SwerveModule rightFront, SwerveModule rightBack,
2022-03-14 18:29:11 -06:00
WPI_Pigeon2 gyro) {
2022-02-28 19:25:07 -07:00
2022-01-24 19:06:28 -07:00
m_leftFront = leftFront;
m_leftBack = leftBack;
m_rightFront = rightFront;
m_rightBack = rightBack;
m_gyro = gyro;
2022-03-16 11:22:51 -06:00
modules = new SwerveModule[] {m_leftFront, m_rightFront, m_leftBack, m_rightBack};
2022-03-14 18:29:11 -06:00
2022-03-05 11:12:33 -07:00
m_poseEstimator = new SwerveDrivePoseEstimator(
m_gyro.getRotation2d(),
new Pose2d(),
m_kinematics,
2022-03-14 18:29:11 -06:00
VecBuilder.fill(1.0, 1.0, Units.degreesToRadians(1)), // TODO: tune
VecBuilder.fill(Units.degreesToRadians(1)), // TODO: tune
VecBuilder.fill(1.0, 1.0, Units.degreesToRadians(1))); // TODO: tune
2022-02-05 11:50:49 -07:00
m_odometry = new SwerveDriveOdometry(m_kinematics, m_gyro.getRotation2d());
2022-03-05 11:12:33 -07:00
m_gyro.reset();
SmartDashboard.putData("Field", m_field);
2022-01-11 11:05:52 -07:00
}
2022-03-05 11:12:33 -07:00
// https://github.com/ZachOrr/MK3-Swerve-Example
/**
* Method to drive the robot using joystick info.
*
* @param speeds[0] Speed of the robot in the x direction (forward).
* @param speeds[1] Speed of the robot in the y direction (sideways).
* @param rot Angular rate of the robot.
* @param fieldRelative Whether the provided x and y speeds are relative to the
* field.
*/
public void driveWithInput(double speedX, double speedY, double rot, boolean fieldRelative) {
if (speedX == 0 && speedY == 0 && rot == 0)
ignoreAngles = true;
else
ignoreAngles = false;
2022-03-10 16:47:32 -07:00
Translation2d speed = new Translation2d(speedX, speedY);
2022-01-29 14:49:18 -07:00
double mag = speed.getNorm();
speed = speed.times(mag * speedAdjust);
2022-03-10 16:47:32 -07:00
double xSpeedMetersPerSecond = speed.getX();
2022-01-29 14:49:18 -07:00
double ySpeedMetersPerSecond = speed.getY();
2022-03-06 18:02:04 -07:00
chassisSpeeds = fieldRelative
? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeedMetersPerSecond, ySpeedMetersPerSecond,
-rot * SwerveDriveConstants.ROTATION_SPEED * speedAdjust, new Rotation2d((360 - m_gyro.getRotation2d().getDegrees() + 90) * (Math.PI/180)))
2022-03-10 16:47:32 -07:00
: new ChassisSpeeds(ySpeedMetersPerSecond, -xSpeedMetersPerSecond,
-rot * SwerveDriveConstants.ROTATION_SPEED * speedAdjust);
2022-03-06 18:02:04 -07:00
SwerveModuleState[] states = m_kinematics.toSwerveModuleStates(chassisSpeeds);
2022-01-29 14:49:18 -07:00
setModuleStates(states);
2022-01-11 11:05:52 -07:00
}
2022-03-05 11:12:33 -07:00
public void driveWithInput(double leftX, double leftY, double rightX, double rightY, boolean fieldRelative) {
2022-01-30 21:27:50 -07:00
ignoreAngles = leftX == 0 && leftY == 0 && rightX == 0 && rightY == 0;
2022-03-10 16:47:32 -07:00
Translation2d speed = new Translation2d(leftX, leftY);
2022-01-30 21:27:50 -07:00
speed = speed.times(speed.getNorm() * speedAdjust);
2022-01-31 19:29:00 -07:00
if (Math.abs(rightX) > OIConstants.RIGHT_AXIS_DEADBAND || Math.abs(rightY) > OIConstants.RIGHT_AXIS_DEADBAND)
2022-03-10 16:47:32 -07:00
rotTarget = new Rotation2d(rightY, rightX);
2022-01-30 21:27:50 -07:00
double rot = rotTarget.minus(m_gyro.getRotation2d()).getRadians();
2022-03-10 16:47:32 -07:00
double xSpeedMetersPerSecond = speed.getX();
2022-01-30 21:27:50 -07:00
double ySpeedMetersPerSecond = speed.getY();
2022-02-11 18:53:13 -07:00
chassisSpeeds = fieldRelative
2022-03-05 11:12:33 -07:00
? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeedMetersPerSecond, ySpeedMetersPerSecond,
2022-03-08 16:14:34 -07:00
rot * SwerveDriveConstants.ROTATION_SPEED, new Rotation2d(m_gyro.getRotation2d().getDegrees()))
: new ChassisSpeeds(xSpeedMetersPerSecond, ySpeedMetersPerSecond, rightX * SwerveDriveConstants.ROTATION_SPEED * 8);
2022-03-05 11:12:33 -07:00
SwerveModuleState[] states = m_kinematics.toSwerveModuleStates(
chassisSpeeds);
2022-01-30 21:27:50 -07:00
setModuleStates(states);
2022-03-08 16:14:34 -07:00
// SmartDashboard.putNumber("rot", rot);
// SmartDashboard.putNumber("rotarget", rotTarget.getDegrees());
2022-01-11 19:57:50 -07:00
}
2022-01-11 11:05:52 -07:00
2022-03-01 19:28:22 -07:00
/**
* Set each module of the swerve drive to the corresponding desired state.
2022-03-05 11:12:33 -07:00
*
2022-03-01 19:28:22 -07:00
* @param desiredStates Array of module states to set.
*/
2022-01-29 14:39:46 -07:00
public void setModuleStates(SwerveModuleState[] desiredStates) {
2022-03-05 11:12:33 -07:00
SwerveDriveKinematics.desaturateWheelSpeeds(desiredStates,
Units.feetToMeters(SwerveDriveConstants.MAX_SPEED_FEET_PER_SEC));
2022-03-08 16:14:34 -07:00
// int i = 2; {
2022-01-29 14:39:46 -07:00
for (int i = 0; i < desiredStates.length; i++) {
SwerveModule module = modules[i];
SwerveModuleState state = desiredStates[i];
2022-03-07 15:26:05 -07:00
module.setDesiredState(state, ignoreAngles);
2022-01-14 17:48:53 -07:00
}
// modules[0].setDesiredState(desiredStates[0], false);
2022-01-11 11:05:52 -07:00
}
2022-02-15 11:05:59 -07:00
@Override
public void periodic() {
2022-02-17 19:52:05 -07:00
updateOdometry();
updateSmartDash();
2022-01-11 11:05:52 -07:00
2022-03-01 19:28:22 -07:00
SmartDashboard.putNumber("Pigeon Yaw", m_gyro.getYaw());
2022-03-12 14:05:33 -07:00
SmartDashboard.putNumber("Pigeon Yaw (0 to 360)", m_gyro.getYaw() % 360);
2022-01-11 11:05:52 -07:00
m_field.setRobotPose(m_poseEstimator.getEstimatedPosition());
super.periodic();
}
2022-01-11 11:05:52 -07:00
private void updateSmartDash() {
// odometry
SmartDashboard.putNumber("Odometry: X", getOdometry().getX());
SmartDashboard.putNumber("Odometry: Y", getOdometry().getY());
SmartDashboard.putNumber("Odometry: θ", getOdometry().getRotation().getDegrees());
// chassis speeds
2022-03-06 18:02:04 -07:00
// TODO: find the actual max velocity in m/s of the robot in fast mode to have accurate chassis speeds
SmartDashboard.putNumber("Chassis Vel: X", chassisSpeeds.vxMetersPerSecond);
SmartDashboard.putNumber("Chassis Vel: Y", chassisSpeeds.vyMetersPerSecond);
SmartDashboard.putNumber("Chassis Vel: ω", chassisSpeeds.omegaRadiansPerSecond);
}
2022-03-05 11:12:33 -07:00
2022-03-06 18:02:04 -07:00
/**
* Gets the current chassis speeds in m/s and rad/s.
* @return Current chassis speeds (vx, vy, ω)
*/
public double[] getChassisSpeeds() {
return new double[] {chassisSpeeds.vxMetersPerSecond, chassisSpeeds.vyMetersPerSecond, chassisSpeeds.omegaRadiansPerSecond};
}
/**
* Gets the current pose of the robot.
2022-03-05 11:12:33 -07:00
*
* @return Robot's current pose.
*/
public Pose2d getOdometry() {
2022-02-05 11:50:49 -07:00
// return m_odometry.getPoseMeters();
return m_poseEstimator.getEstimatedPosition();
2022-01-20 19:38:58 -07:00
}
2022-02-19 11:18:15 -07:00
/**
* Gets the current gyro using regression formula.
2022-03-05 11:12:33 -07:00
*
2022-02-19 11:18:15 -07:00
* @return Rotation2d object holding current gyro in radians
*/
public Rotation2d getRegGyro() {
double regCur = 0.6552670369 + m_gyro.getRotation2d().getDegrees() * 0.9926871527;
return new Rotation2d(regCur * Math.PI / 180);
}
/**
2022-02-17 19:52:05 -07:00
* Resets the odometry of the robot to the given pose.
*/
2022-01-29 14:39:46 -07:00
public void resetOdometry(Pose2d pose) {
m_poseEstimator.resetPosition(pose, m_gyro.getRotation2d());
}
2022-03-05 11:12:33 -07:00
/**
* Updates the field relative position of the robot.
*/
2022-01-18 19:53:14 -07:00
public void updateOdometry() {
2022-02-19 11:18:15 -07:00
m_poseEstimator.update( getRegGyro(),
2022-01-22 10:10:46 -07:00
modules[0].getState(),
modules[1].getState(),
modules[2].getState(),
modules[3].getState());
2022-03-06 00:24:51 -07:00
}
2022-01-18 19:53:14 -07:00
2022-03-01 19:28:22 -07:00
/**
* Resets pigeon.
*/
2022-03-05 11:12:33 -07:00
public void resetGyro() {
2022-02-08 19:42:54 -07:00
m_gyro.reset();
rotTarget = new Rotation2d(0);
}
2022-02-17 19:52:05 -07:00
/**
* Stop all four swerve modules.
*/
2022-02-03 20:53:43 -07:00
public void stopModules() {
modules[0].stop();
modules[1].stop();
modules[2].stop();
modules[3].stop();
}
2022-03-01 19:28:22 -07:00
/**
* Switches speed modes.
2022-03-05 11:12:33 -07:00
*
2022-03-01 19:28:22 -07:00
* @param shift True if fast mode, false if slow mode.
*/
2022-03-05 11:12:33 -07:00
public void highSpeed(boolean shift) {
if (shift) {
2022-01-15 14:55:07 -07:00
speedAdjust = SwerveDriveConstants.JOYSTICK_TO_METERS_PER_SECOND_FAST;
2022-03-05 11:12:33 -07:00
} else {
2022-01-15 14:55:07 -07:00
speedAdjust = SwerveDriveConstants.JOYSTICK_TO_METERS_PER_SECOND_SLOW;
}
}
2022-03-12 11:36:10 -07:00
public double getCurrent(){
return m_leftFront.getCurrent() + m_rightFront.getCurrent() + m_rightBack.getCurrent() + m_leftBack.getCurrent();
}
public double getVoltage(){
return m_leftFront.getVoltage() + m_rightFront.getVoltage() + m_rightBack.getVoltage() + m_leftBack.getVoltage();
}
2021-11-15 17:27:14 -07:00
}