2023-01-13 18:59:50 -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-01-13 19:39:24 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.FeedbackDevice;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.RemoteSensorSource;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXControlMode;
|
2023-01-13 18:59:50 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.TalonFXConfiguration;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
|
|
|
|
import com.ctre.phoenix.sensors.CANCoder;
|
|
|
|
|
|
2023-01-13 19:39:24 -07:00
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
2023-01-15 20:25:07 -07:00
|
|
|
import edu.wpi.first.math.kinematics.SwerveModulePosition;
|
2023-01-13 19:39:24 -07:00
|
|
|
import edu.wpi.first.math.kinematics.SwerveModuleState;
|
|
|
|
|
import edu.wpi.first.math.util.Units;
|
2023-01-13 18:59:50 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
|
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
2023-01-13 19:39:24 -07:00
|
|
|
import frc4388.utility.Gains;
|
2023-01-13 18:59:50 -07:00
|
|
|
|
|
|
|
|
public class SwerveModule extends SubsystemBase {
|
2023-02-04 11:22:43 -07:00
|
|
|
private WPI_TalonFX driveMotor;
|
|
|
|
|
private WPI_TalonFX angleMotor;
|
2023-01-21 15:16:22 -07:00
|
|
|
private CANCoder encoder;
|
2023-01-13 19:39:24 -07:00
|
|
|
|
|
|
|
|
public static Gains swerveGains = SwerveDriveConstants.PIDConstants.SWERVE_GAINS;
|
2023-01-13 18:59:50 -07:00
|
|
|
|
|
|
|
|
/** Creates a new SwerveModule. */
|
2023-02-02 19:10:08 -07:00
|
|
|
public SwerveModule(WPI_TalonFX driveMotor, WPI_TalonFX angleMotor, CANCoder encoder, double offset) {
|
2023-01-13 18:59:50 -07:00
|
|
|
this.driveMotor = driveMotor;
|
|
|
|
|
this.angleMotor = angleMotor;
|
2023-01-21 15:16:22 -07:00
|
|
|
this.encoder = encoder;
|
2023-01-13 18:59:50 -07:00
|
|
|
|
|
|
|
|
TalonFXConfiguration angleConfig = new TalonFXConfiguration();
|
2023-01-13 19:39:24 -07:00
|
|
|
angleConfig.slot0.kP = swerveGains.kP;
|
|
|
|
|
angleConfig.slot0.kI = swerveGains.kI;
|
|
|
|
|
angleConfig.slot0.kD = swerveGains.kD;
|
|
|
|
|
|
|
|
|
|
// use the CANcoder as the remote sensor for the primary TalonFX PID
|
2023-01-21 15:16:22 -07:00
|
|
|
angleConfig.remoteFilter0.remoteSensorDeviceID = encoder.getDeviceID();
|
2023-01-13 19:39:24 -07:00
|
|
|
angleConfig.remoteFilter0.remoteSensorSource = RemoteSensorSource.CANCoder;
|
|
|
|
|
angleConfig.primaryPID.selectedFeedbackSensor = FeedbackDevice.RemoteSensor0;
|
|
|
|
|
angleMotor.configAllSettings(angleConfig);
|
2023-01-13 18:59:50 -07:00
|
|
|
|
2023-02-02 19:10:08 -07:00
|
|
|
encoder.configMagnetOffset(offset);
|
2023-02-04 11:22:43 -07:00
|
|
|
|
|
|
|
|
driveMotor.setSelectedSensorPosition(0);
|
|
|
|
|
driveMotor.config_kP(0, 0.2);
|
2023-01-13 18:59:50 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-14 11:42:03 -07:00
|
|
|
/**
|
|
|
|
|
* Get the drive motor of the SwerveModule
|
|
|
|
|
* @return the drive motor of the SwerveModule
|
|
|
|
|
*/
|
|
|
|
|
public WPI_TalonFX getDriveMotor() {
|
|
|
|
|
return this.driveMotor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the angle motor of the SwerveModule
|
|
|
|
|
* @return the angle motor of the SwerveModule
|
|
|
|
|
*/
|
|
|
|
|
public WPI_TalonFX getAngleMotor() {
|
|
|
|
|
return this.angleMotor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the CANcoder of the SwerveModule
|
|
|
|
|
* @return the CANcoder of the SwerveModule
|
|
|
|
|
*/
|
|
|
|
|
public CANCoder getEncoder() {
|
2023-01-21 15:16:22 -07:00
|
|
|
return this.encoder;
|
2023-01-14 11:42:03 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-13 19:39:24 -07:00
|
|
|
/**
|
|
|
|
|
* Get the angle of a SwerveModule as a Rotation2d
|
|
|
|
|
* @return the angle of a SwerveModule as a Rotation2d
|
|
|
|
|
*/
|
|
|
|
|
public Rotation2d getAngle() {
|
2023-02-02 20:54:12 -07:00
|
|
|
// * Note: This assumes that the CANCoders are setup with the default feedback coefficient and the sensor value reports degrees.
|
2023-01-21 15:16:22 -07:00
|
|
|
return Rotation2d.fromDegrees(encoder.getAbsolutePosition());
|
2023-01-13 19:39:24 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-04 11:22:43 -07:00
|
|
|
public double getAngularVel() {
|
|
|
|
|
return this.angleMotor.getSelectedSensorVelocity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getDrivePos() {
|
|
|
|
|
return this.driveMotor.getSelectedSensorPosition() / SwerveDriveConstants.Conversions.TICKS_PER_MOTOR_REV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getDriveVel() {
|
|
|
|
|
return this.driveMotor.getSelectedSensorVelocity(0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-14 15:30:38 -07:00
|
|
|
public void stop() {
|
|
|
|
|
driveMotor.set(0);
|
|
|
|
|
angleMotor.set(0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 17:50:24 -07:00
|
|
|
public void rotateToAngle(double angle) {
|
2023-01-14 15:30:38 -07:00
|
|
|
angleMotor.set(TalonFXControlMode.Position, angle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get state of swerve module
|
|
|
|
|
* @return speed in m/s and angle in degrees
|
|
|
|
|
*/
|
|
|
|
|
public SwerveModuleState getState() {
|
|
|
|
|
return new SwerveModuleState(
|
|
|
|
|
Units.inchesToMeters(driveMotor.getSelectedSensorVelocity() * SwerveDriveConstants.Conversions.INCHES_PER_TICK) * SwerveDriveConstants.Conversions.TICK_TIME_TO_SECONDS,
|
|
|
|
|
getAngle()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 20:25:07 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the current position of the SwerveModule
|
|
|
|
|
* @return The current position of the SwerveModule in meters traveled by the driveMotor and the angle of the angleMotor.
|
|
|
|
|
*/
|
|
|
|
|
public SwerveModulePosition getPosition() {
|
|
|
|
|
return new SwerveModulePosition(Units.inchesToMeters(driveMotor.getSelectedSensorPosition() * SwerveDriveConstants.Conversions.INCHES_PER_TICK), getAngle());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 19:39:24 -07:00
|
|
|
/**
|
|
|
|
|
* Set the speed and rotation of the SwerveModule from a SwerveModuleState object
|
|
|
|
|
* @param desiredState a SwerveModuleState representing the desired new state of the module
|
|
|
|
|
*/
|
|
|
|
|
public void setDesiredState(SwerveModuleState desiredState) {
|
|
|
|
|
Rotation2d currentRotation = this.getAngle();
|
|
|
|
|
|
|
|
|
|
SwerveModuleState state = SwerveModuleState.optimize(desiredState, currentRotation);
|
|
|
|
|
|
|
|
|
|
// calculate the difference between our current rotational position and our new rotational position
|
2023-02-04 11:22:43 -07:00
|
|
|
Rotation2d rotationDelta = state.angle.minus(currentRotation); // ? might need to be negative
|
2023-01-13 19:39:24 -07:00
|
|
|
|
|
|
|
|
// calculate the new absolute position of the SwerveModule based on the difference in rotation
|
|
|
|
|
double deltaTicks = (rotationDelta.getDegrees() / 360.) * SwerveDriveConstants.Conversions.CANCODER_TICKS_PER_ROTATION;
|
|
|
|
|
|
|
|
|
|
// convert the CANCoder from its position reading to ticks
|
2023-02-04 11:22:43 -07:00
|
|
|
double currentTicks = encoder.getPosition() / encoder.configGetFeedbackCoefficient(); // ? why feedback coefficient
|
2023-01-13 19:39:24 -07:00
|
|
|
angleMotor.set(TalonFXControlMode.Position, currentTicks + deltaTicks);
|
|
|
|
|
|
|
|
|
|
double feetPerSecond = Units.metersToFeet(state.speedMetersPerSecond);
|
2023-02-04 11:22:43 -07:00
|
|
|
// double inchesPerSecond = Units.metersToFeet(state.speedMetersPerSecond) * 12;
|
2023-01-31 19:24:12 -07:00
|
|
|
|
|
|
|
|
// driveMotor.set(TalonFXControlMode.Velocity, inchesPerSecond * SwerveDriveConstants.Conversions.TICKS_PER_INCH * SwerveDriveConstants.Conversions.SECONDS_TO_TICK_TIME);
|
2023-02-04 11:22:43 -07:00
|
|
|
// driveMotor.set(0.1);
|
|
|
|
|
// double angleCorrection = getAngularVel() * 2.69;
|
|
|
|
|
driveMotor.set((feetPerSecond / SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
|
|
|
|
|
// driveMotor.set(TalonFXControlMode.Velocity, angleCorrection + inchesPerSecond * SwerveDriveConstants.Conversions.TICKS_PER_INCH * SwerveDriveConstants.Conversions.SECONDS_TO_TICK_TIME);
|
2023-01-13 18:59:50 -07:00
|
|
|
}
|
2023-01-13 19:39:24 -07:00
|
|
|
|
2023-01-21 15:16:22 -07:00
|
|
|
public void reset(double position) {
|
|
|
|
|
encoder.setPositionToAbsolute();
|
2023-01-15 15:15:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getCurrent() {
|
|
|
|
|
return angleMotor.getSupplyCurrent() + driveMotor.getSupplyCurrent();
|
|
|
|
|
}
|
2023-01-14 15:30:38 -07:00
|
|
|
|
2023-01-15 15:15:22 -07:00
|
|
|
public double getVoltage() {
|
|
|
|
|
return (Math.abs(angleMotor.getMotorOutputVoltage()) + Math.abs(driveMotor.getMotorOutputVoltage()));
|
|
|
|
|
}
|
|
|
|
|
}
|