2021-11-15 17:52:28 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
package frc4388.robot.subsystems;
|
|
|
|
|
|
2021-12-06 17:23:08 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXControlMode;
|
2021-11-29 17:18:31 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
2021-12-02 17:24:41 -07:00
|
|
|
import com.ctre.phoenix.sensors.CANCoder;
|
2021-11-29 17:18:31 -07:00
|
|
|
|
2021-11-15 17:52:28 -07:00
|
|
|
import edu.wpi.first.wpilibj.geometry.Rotation2d;
|
|
|
|
|
import edu.wpi.first.wpilibj.geometry.Translation2d;
|
|
|
|
|
import edu.wpi.first.wpilibj.kinematics.ChassisSpeeds;
|
|
|
|
|
import edu.wpi.first.wpilibj.kinematics.SwerveDriveKinematics;
|
|
|
|
|
import edu.wpi.first.wpilibj.kinematics.SwerveModuleState;
|
2021-12-16 21:48:59 -07:00
|
|
|
import edu.wpi.first.wpilibj.util.Units;
|
2021-12-02 17:51:06 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
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;
|
2021-12-16 21:48:59 -07:00
|
|
|
import frc4388.utility.RobotGyro;
|
2021-11-15 17:52:28 -07:00
|
|
|
|
2021-12-02 17:51:06 -07:00
|
|
|
public class SwerveDrive extends SubsystemBase
|
2021-11-15 17:27:14 -07:00
|
|
|
{
|
|
|
|
|
SwerveDriveKinematics m_kinematics;
|
2021-11-29 17:18:31 -07:00
|
|
|
private WPI_TalonFX m_leftFrontSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_leftFrontWheelMotor;
|
|
|
|
|
private WPI_TalonFX m_rightFrontSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_rightFrontWheelMotor;
|
|
|
|
|
private WPI_TalonFX m_leftBackSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_leftBackWheelMotor;
|
|
|
|
|
private WPI_TalonFX m_rightBackSteerMotor;
|
|
|
|
|
private WPI_TalonFX m_rightBackWheelMotor;
|
2021-12-02 17:24:41 -07:00
|
|
|
private CANCoder m_leftFrontEncoder;
|
|
|
|
|
private CANCoder m_rightFrontEncoder;
|
|
|
|
|
private CANCoder m_leftBackEncoder;
|
2021-12-06 17:53:57 -07:00
|
|
|
private CANCoder m_rightBackEncoder;
|
2021-12-16 21:48:59 -07:00
|
|
|
double halfWidth = SwerveDriveConstants.WIDTH / 2.d;
|
|
|
|
|
double halfHeight = SwerveDriveConstants.HEIGHT / 2.d;
|
2021-12-06 17:53:57 -07:00
|
|
|
public static Gains m_swerveGains = SwerveDriveConstants.SWERVE_GAINS;
|
|
|
|
|
|
2021-12-16 21:48:59 -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));
|
|
|
|
|
//setSwerveGains();
|
|
|
|
|
|
|
|
|
|
private SwerveDriveKinematics kinematics = new SwerveDriveKinematics(m_frontLeftLocation, m_frontRightLocation, m_backLeftLocation, m_backRightLocation);
|
|
|
|
|
public SwerveModule[] modules;
|
|
|
|
|
public RobotGyro gyro; //TODO Add Gyro Lol
|
|
|
|
|
|
|
|
|
|
|
2021-11-29 17:18:31 -07:00
|
|
|
public SwerveDrive(WPI_TalonFX leftFrontSteerMotor,WPI_TalonFX leftFrontWheelMotor,WPI_TalonFX rightFrontSteerMotor,WPI_TalonFX rightFrontWheelMotor,
|
2021-12-02 17:24:41 -07:00
|
|
|
WPI_TalonFX leftBackSteerMotor,WPI_TalonFX leftBackWheelMotor,WPI_TalonFX rightBackSteerMotor,WPI_TalonFX rightBackWheelMotor, CANCoder leftFrontEncoder,
|
|
|
|
|
CANCoder rightFrontEncoder,
|
|
|
|
|
CANCoder leftBackEncoder,
|
|
|
|
|
CANCoder rightBackEncoder)
|
2021-11-15 17:27:14 -07:00
|
|
|
{
|
2021-11-29 17:18:31 -07:00
|
|
|
m_leftFrontSteerMotor = leftFrontSteerMotor;
|
|
|
|
|
m_leftFrontWheelMotor = leftFrontWheelMotor;
|
|
|
|
|
m_rightFrontSteerMotor = rightFrontSteerMotor;
|
|
|
|
|
m_rightFrontWheelMotor = rightFrontWheelMotor;
|
|
|
|
|
m_leftBackSteerMotor = leftBackSteerMotor;
|
|
|
|
|
m_leftBackWheelMotor = leftBackWheelMotor;
|
|
|
|
|
m_rightBackSteerMotor = rightBackSteerMotor;
|
|
|
|
|
m_rightBackWheelMotor = rightBackWheelMotor;
|
2021-12-02 17:24:41 -07:00
|
|
|
m_leftFrontEncoder = leftFrontEncoder;
|
|
|
|
|
m_rightFrontEncoder = rightFrontEncoder;
|
|
|
|
|
m_leftBackEncoder = leftBackEncoder;
|
|
|
|
|
m_rightBackEncoder = rightBackEncoder;
|
2021-11-15 17:27:14 -07:00
|
|
|
|
2021-12-16 21:48:59 -07:00
|
|
|
modules = new SwerveModule[] {
|
|
|
|
|
new SwerveModule(m_leftFrontWheelMotor, m_leftFrontSteerMotor, m_leftFrontEncoder), // Front Left
|
|
|
|
|
new SwerveModule(m_rightFrontWheelMotor, m_rightFrontSteerMotor, m_rightFrontEncoder), // Front Right
|
|
|
|
|
new SwerveModule(m_leftBackWheelMotor, m_leftBackSteerMotor, m_leftBackEncoder), // Back Left
|
|
|
|
|
new SwerveModule(m_rightFrontWheelMotor, m_rightFrontSteerMotor, m_rightFrontEncoder) // Back Right
|
|
|
|
|
};
|
|
|
|
|
//gyro.reset();
|
2021-11-15 17:27:14 -07:00
|
|
|
}
|
2021-12-24 14:19:09 -07:00
|
|
|
//https://github.com/ZachOrr/MK3-Swerve-Example
|
2021-12-24 14:00:56 -07:00
|
|
|
/**
|
|
|
|
|
* Method to drive the robot using joystick info.
|
|
|
|
|
*
|
|
|
|
|
* @param xSpeed Speed of the robot in the x direction (forward).
|
|
|
|
|
* @param ySpeed 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.
|
|
|
|
|
*/
|
2021-12-16 21:48:59 -07:00
|
|
|
public void driveWithInput(double xSpeed, double ySpeed, double rot, boolean fieldRelative)
|
2021-11-15 17:27:14 -07:00
|
|
|
{
|
2021-12-16 21:48:59 -07:00
|
|
|
/*var speeds = new ChassisSpeeds(strafeX, strafeY, rotate * SwerveDriveConstants.ROTATION_SPEED //in rad/s );
|
|
|
|
|
driveFromSpeeds(speeds);*/
|
|
|
|
|
SwerveModuleState[] states =
|
|
|
|
|
kinematics.toSwerveModuleStates(
|
|
|
|
|
fieldRelative
|
|
|
|
|
? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeed, ySpeed, rot, gyro.getRotation2d())
|
|
|
|
|
: new ChassisSpeeds(xSpeed, ySpeed, rot));
|
|
|
|
|
SwerveDriveKinematics.normalizeWheelSpeeds(states, SwerveDriveConstants.MAX_SPEED_FEET_PER_SEC);
|
|
|
|
|
for (int i = 0; i < states.length; i++) {
|
|
|
|
|
SwerveModule module = modules[i];
|
|
|
|
|
SwerveModuleState state = states[i];
|
|
|
|
|
module.setDesiredState(state);
|
|
|
|
|
}
|
2021-12-02 17:51:06 -07:00
|
|
|
}
|
2021-12-06 17:23:08 -07:00
|
|
|
//Converts a ChassisSpeed to SwerveModuleStates (targets)
|
2021-12-02 17:51:06 -07:00
|
|
|
public void driveFromSpeeds(ChassisSpeeds speeds)
|
|
|
|
|
{
|
|
|
|
|
//https://docs.wpilib.org/en/stable/docs/software/kinematics-and-odometry/swerve-drive-kinematics.html
|
2021-11-15 17:27:14 -07:00
|
|
|
// Convert to module states
|
|
|
|
|
SwerveModuleState[] moduleStates = m_kinematics.toSwerveModuleStates(speeds);
|
|
|
|
|
|
|
|
|
|
// Front left module state
|
2021-12-06 16:41:28 -07:00
|
|
|
SwerveModuleState leftFront = SwerveModuleState.optimize(moduleStates[0], Rotation2d.fromDegrees(m_leftFrontEncoder.getPosition()));
|
2021-11-15 17:27:14 -07:00
|
|
|
// Front right module state
|
2021-12-06 16:41:28 -07:00
|
|
|
SwerveModuleState rightFront = SwerveModuleState.optimize(moduleStates[1], Rotation2d.fromDegrees(m_rightFrontEncoder.getPosition()));
|
2021-11-15 17:27:14 -07:00
|
|
|
// Back left module state
|
2021-12-06 16:41:28 -07:00
|
|
|
SwerveModuleState leftBack = SwerveModuleState.optimize(moduleStates[2], Rotation2d.fromDegrees(m_leftBackEncoder.getPosition()));
|
2021-11-15 17:27:14 -07:00
|
|
|
// Back right module state
|
2021-12-06 16:41:28 -07:00
|
|
|
SwerveModuleState rightBack = SwerveModuleState.optimize(moduleStates[3], Rotation2d.fromDegrees(m_rightBackEncoder.getPosition()));
|
2021-12-02 17:36:28 -07:00
|
|
|
|
2021-12-06 17:23:08 -07:00
|
|
|
//Set the motors
|
|
|
|
|
setSwerveMotors(leftFront, leftBack, rightFront, rightBack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Sets steering motors to PID values
|
|
|
|
|
public void setSwerveMotors(SwerveModuleState leftFront, SwerveModuleState leftBack, SwerveModuleState rightFront, SwerveModuleState rightBack)
|
|
|
|
|
{
|
2021-12-16 21:48:59 -07:00
|
|
|
/*//Set the Wheel motor speeds
|
2021-12-02 17:36:28 -07:00
|
|
|
m_leftFrontWheelMotor.set(m_leftFrontSteerMotor.get() + leftFront.speedMetersPerSecond * SwerveDriveConstants.WHEEL_SPEED);
|
|
|
|
|
m_rightFrontWheelMotor.set(m_rightFrontSteerMotor.get() + rightFront.speedMetersPerSecond * SwerveDriveConstants.WHEEL_SPEED);
|
2021-12-02 17:56:35 -07:00
|
|
|
m_leftBackWheelMotor.set(m_leftBackSteerMotor.get() + leftBack.speedMetersPerSecond * SwerveDriveConstants.WHEEL_SPEED);
|
2021-12-02 17:36:28 -07:00
|
|
|
m_rightBackWheelMotor.set(m_rightBackSteerMotor.get() + rightBack.speedMetersPerSecond * SwerveDriveConstants.WHEEL_SPEED);
|
2021-12-06 16:38:29 -07:00
|
|
|
|
2021-12-06 17:23:08 -07:00
|
|
|
//PID
|
2021-12-16 21:48:59 -07:00
|
|
|
m_leftFrontSteerMotor.set(TalonFXControlMode.Position, leftFront.angle.getDegrees() * 12000);
|
|
|
|
|
m_rightFrontSteerMotor.set(TalonFXControlMode.Position, rightFront.angle.getDegrees() * 12000);
|
|
|
|
|
m_leftBackSteerMotor.set(TalonFXControlMode.Position, leftBack.angle.getDegrees() * 12000);
|
2021-12-06 17:23:08 -07:00
|
|
|
m_rightBackSteerMotor.set(TalonFXControlMode.Position, rightBack.angle.getDegrees());
|
2021-12-16 21:48:59 -07:00
|
|
|
|
|
|
|
|
System.out.println("Target: " + leftFront.angle.getDegrees());*/
|
2021-12-16 16:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:48:59 -07:00
|
|
|
/*public void setSwerveGains(){
|
|
|
|
|
m_leftFrontSteerMotor.selectProfileSlot(SwerveDriveConstants.SWERVE_SLOT_IDX, SwerveDriveConstants.SWERVE_PID_LOOP_IDX);
|
|
|
|
|
m_leftFrontSteerMotor.config_kF(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kF, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_leftFrontSteerMotor.config_kP(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kP, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_leftFrontSteerMotor.config_kI(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kI, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_leftFrontSteerMotor.config_kD(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kD, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
|
|
|
|
|
m_rightFrontSteerMotor.selectProfileSlot(SwerveDriveConstants.SWERVE_SLOT_IDX, SwerveDriveConstants.SWERVE_PID_LOOP_IDX);
|
|
|
|
|
m_rightFrontSteerMotor.config_kF(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kF, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_rightFrontSteerMotor.config_kP(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kP, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_rightFrontSteerMotor.config_kI(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kI, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_rightFrontSteerMotor.config_kD(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kD, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
|
|
|
|
|
m_leftBackSteerMotor.selectProfileSlot(SwerveDriveConstants.SWERVE_SLOT_IDX, SwerveDriveConstants.SWERVE_PID_LOOP_IDX);
|
|
|
|
|
m_leftBackSteerMotor.config_kF(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kF, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_leftBackSteerMotor.config_kP(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kP, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_leftBackSteerMotor.config_kI(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kI, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_leftBackSteerMotor.config_kD(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kD, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
|
|
|
|
|
m_rightBackSteerMotor.selectProfileSlot(SwerveDriveConstants.SWERVE_SLOT_IDX, SwerveDriveConstants.SWERVE_PID_LOOP_IDX);
|
|
|
|
|
m_rightBackSteerMotor.config_kF(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kF, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_rightBackSteerMotor.config_kP(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kP, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_rightBackSteerMotor.config_kI(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kI, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
|
|
|
|
m_rightBackSteerMotor.config_kD(SwerveDriveConstants.SWERVE_SLOT_IDX, m_swerveGains.m_kD, SwerveDriveConstants.SWERVE_TIMEOUT_MS);
|
2021-12-06 17:53:57 -07:00
|
|
|
|
2021-12-16 21:48:59 -07:00
|
|
|
}*/
|
2021-11-15 17:27:14 -07:00
|
|
|
|
2021-12-06 17:23:08 -07:00
|
|
|
|
|
|
|
|
|
2021-11-15 17:52:28 -07:00
|
|
|
// public void driveFieldRelative(double awayFromStation, double towardLeftBoundary, double rotate)
|
|
|
|
|
// {
|
|
|
|
|
// var speeds = ChassisSpeeds.fromFieldRelativeSpeeds(awayFromStation, towardLeftBoundary,
|
|
|
|
|
// rotate * SwerveDriveConstants.RotationSpeed, /*get odometry angle*/)
|
|
|
|
|
// }
|
2021-11-15 17:27:14 -07:00
|
|
|
}
|