2021-12-16 21:48:59 -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;
|
|
|
|
|
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.FeedbackDevice;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.RemoteSensorSource;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXControlMode;
|
2022-02-24 18:54:58 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXFeedbackDevice;
|
2021-12-16 21:48:59 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.TalonFXConfiguration;
|
2021-12-27 17:38:12 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
2021-12-16 21:48:59 -07:00
|
|
|
import com.ctre.phoenix.sensors.CANCoder;
|
|
|
|
|
import com.ctre.phoenix.sensors.CANCoderConfiguration;
|
2021-12-27 17:38:12 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
|
|
|
|
import edu.wpi.first.math.kinematics.SwerveModuleState;
|
|
|
|
|
import edu.wpi.first.math.util.Units;
|
2022-02-19 11:16:24 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2021-12-27 17:38:12 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
2021-12-16 21:48:59 -07:00
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
|
|
|
|
import frc4388.utility.Gains;
|
|
|
|
|
|
|
|
|
|
public class SwerveModule extends SubsystemBase {
|
2022-02-24 18:54:58 -07:00
|
|
|
public WPI_TalonFX angleMotor;
|
|
|
|
|
public WPI_TalonFX driveMotor;
|
2022-01-11 11:05:52 -07:00
|
|
|
private CANCoder canCoder;
|
|
|
|
|
public static Gains m_swerveGains = SwerveDriveConstants.SWERVE_GAINS;
|
2021-12-16 21:48:59 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
private static double kEncoderTicksPerRotation = 4096;
|
2022-01-22 15:55:04 -07:00
|
|
|
private SwerveModuleState state;
|
2022-01-30 00:39:17 -07:00
|
|
|
private double canCoderFeedbackCoefficient;
|
2022-03-05 11:12:33 -07:00
|
|
|
|
2022-02-24 18:54:58 -07:00
|
|
|
public long m_currentTime;
|
|
|
|
|
public long m_lastTime;
|
|
|
|
|
public double m_deltaTime;
|
|
|
|
|
|
|
|
|
|
public double m_currentPos;
|
|
|
|
|
public double m_lastPos;
|
2022-01-11 19:57:50 -07:00
|
|
|
|
2021-12-16 21:48:59 -07:00
|
|
|
/** Creates a new SwerveModule. */
|
2022-01-11 19:57:50 -07:00
|
|
|
public SwerveModule(WPI_TalonFX driveMotor, WPI_TalonFX angleMotor, CANCoder canCoder, double offset) {
|
2021-12-16 21:48:59 -07:00
|
|
|
this.driveMotor = driveMotor;
|
|
|
|
|
this.angleMotor = angleMotor;
|
|
|
|
|
this.canCoder = canCoder;
|
2022-01-30 00:39:17 -07:00
|
|
|
canCoderFeedbackCoefficient = canCoder.configGetFeedbackCoefficient();
|
2021-12-16 21:48:59 -07:00
|
|
|
|
|
|
|
|
TalonFXConfiguration angleTalonFXConfiguration = new TalonFXConfiguration();
|
|
|
|
|
|
2022-03-05 11:07:54 -07:00
|
|
|
angleTalonFXConfiguration.slot0.kP = m_swerveGains.kP;
|
|
|
|
|
angleTalonFXConfiguration.slot0.kI = m_swerveGains.kI;
|
|
|
|
|
angleTalonFXConfiguration.slot0.kD = m_swerveGains.kD;
|
2021-12-16 21:48:59 -07:00
|
|
|
|
|
|
|
|
// Use the CANCoder as the remote sensor for the primary TalonFX PID
|
|
|
|
|
angleTalonFXConfiguration.remoteFilter0.remoteSensorDeviceID = canCoder.getDeviceID();
|
|
|
|
|
angleTalonFXConfiguration.remoteFilter0.remoteSensorSource = RemoteSensorSource.CANCoder;
|
|
|
|
|
angleTalonFXConfiguration.primaryPID.selectedFeedbackSensor = FeedbackDevice.RemoteSensor0;
|
|
|
|
|
angleMotor.configAllSettings(angleTalonFXConfiguration);
|
2022-03-05 11:12:33 -07:00
|
|
|
// angleMotor.setInverted(true);
|
2022-02-24 18:54:58 -07:00
|
|
|
// TalonFXConfiguration driveTalonFXConfiguration = new TalonFXConfiguration();
|
|
|
|
|
// driveTalonFXConfiguration.slot0.kP = 0.05;
|
|
|
|
|
// driveTalonFXConfiguration.slot0.kI = 0.0;
|
|
|
|
|
// driveTalonFXConfiguration.slot0.kD = 0.0;
|
2022-03-05 11:12:33 -07:00
|
|
|
// driveTalonFXConfiguration.primaryPID.selectedFeedbackSensor =
|
|
|
|
|
// FeedbackDevice.IntegratedSensor;
|
2022-02-24 18:54:58 -07:00
|
|
|
driveMotor.configFactoryDefault();
|
|
|
|
|
driveMotor.configSelectedFeedbackSensor(TalonFXFeedbackDevice.IntegratedSensor, 0, 30);
|
|
|
|
|
driveMotor.configNominalOutputForward(0, 30);
|
|
|
|
|
driveMotor.configNominalOutputReverse(0, 30);
|
|
|
|
|
driveMotor.configPeakOutputForward(1, 30);
|
|
|
|
|
driveMotor.configPeakOutputReverse(-1, 30);
|
|
|
|
|
driveMotor.configAllowableClosedloopError(0, 0, 30);
|
2022-03-05 11:12:33 -07:00
|
|
|
// driveMotor.setInverted(true);
|
|
|
|
|
driveMotor.config_kP(0, 0, 30);
|
2022-02-24 18:54:58 -07:00
|
|
|
driveMotor.config_kI(0, 0, 30);
|
|
|
|
|
driveMotor.config_kD(0, 0, 30);
|
|
|
|
|
// maybe try a feedforward value?
|
2022-03-05 11:12:33 -07:00
|
|
|
|
2022-02-24 18:54:58 -07:00
|
|
|
// driveMotor.configAllSettings(driveTalonFXConfiguration);
|
2021-12-16 21:48:59 -07:00
|
|
|
|
|
|
|
|
CANCoderConfiguration canCoderConfiguration = new CANCoderConfiguration();
|
2022-01-11 19:57:50 -07:00
|
|
|
canCoderConfiguration.magnetOffsetDegrees = offset;
|
2022-03-05 11:12:33 -07:00
|
|
|
canCoderConfiguration.sensorDirection = true;
|
2021-12-16 21:48:59 -07:00
|
|
|
canCoder.configAllSettings(canCoderConfiguration);
|
|
|
|
|
|
2022-02-24 18:54:58 -07:00
|
|
|
m_currentTime = System.currentTimeMillis();
|
|
|
|
|
m_lastTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
m_lastPos = driveMotor.getSelectedSensorPosition();
|
|
|
|
|
}
|
2022-01-11 19:57:50 -07:00
|
|
|
|
2022-01-22 15:55:04 -07:00
|
|
|
private Rotation2d getAngle() {
|
2022-03-05 11:12:33 -07:00
|
|
|
// Note: This assumes the CANCoders are setup with the default feedback
|
|
|
|
|
// coefficient
|
2022-02-17 19:52:05 -07:00
|
|
|
// and the sensor value reports degrees.
|
2021-12-16 21:48:59 -07:00
|
|
|
return Rotation2d.fromDegrees(canCoder.getAbsolutePosition());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the speed + rotation of the swerve module from a SwerveModuleState object
|
2022-03-05 11:12:33 -07:00
|
|
|
*
|
|
|
|
|
* @param desiredState - A SwerveModuleState representing the desired new state
|
|
|
|
|
* of the module
|
2021-12-16 21:48:59 -07:00
|
|
|
*/
|
2022-01-15 14:55:07 -07:00
|
|
|
public void setDesiredState(SwerveModuleState desiredState, boolean ignoreAngle) {
|
2021-12-16 21:48:59 -07:00
|
|
|
Rotation2d currentRotation = getAngle();
|
2022-03-05 11:12:33 -07:00
|
|
|
// SmartDashboard.putNumber("Motor " + angleMotor.getDeviceID(),
|
|
|
|
|
// currentRotation.getDegrees());
|
2022-01-22 15:55:04 -07:00
|
|
|
state = SwerveModuleState.optimize(desiredState, currentRotation);
|
2021-12-16 21:48:59 -07:00
|
|
|
|
2022-03-05 11:12:33 -07:00
|
|
|
// Find the difference between our current rotational position + our new
|
|
|
|
|
// rotational position
|
2021-12-16 21:48:59 -07:00
|
|
|
Rotation2d rotationDelta = state.angle.minus(currentRotation);
|
|
|
|
|
|
2022-03-05 11:12:33 -07:00
|
|
|
// Find the new absolute position of the module based on the difference in
|
|
|
|
|
// rotation
|
2021-12-27 17:38:12 -07:00
|
|
|
double deltaTicks = (rotationDelta.getDegrees() / 360.) * kEncoderTicksPerRotation;
|
2021-12-16 21:48:59 -07:00
|
|
|
// Convert the CANCoder from it's position reading back to ticks
|
2022-01-30 00:39:17 -07:00
|
|
|
double currentTicks = canCoder.getPosition() / canCoderFeedbackCoefficient;
|
2021-12-16 21:48:59 -07:00
|
|
|
double desiredTicks = currentTicks + deltaTicks;
|
2022-02-19 11:16:24 -07:00
|
|
|
|
2022-03-05 11:12:33 -07:00
|
|
|
if (!ignoreAngle) {
|
2022-01-15 14:55:07 -07:00
|
|
|
angleMotor.set(TalonFXControlMode.Position, desiredTicks);
|
|
|
|
|
}
|
2021-12-16 21:48:59 -07:00
|
|
|
|
2022-03-05 11:12:33 -07:00
|
|
|
// Please work
|
2022-02-24 18:54:58 -07:00
|
|
|
double ftPerSec = Units.metersToFeet(state.speedMetersPerSecond);
|
|
|
|
|
double normFtPerSec = ftPerSec / SwerveDriveConstants.MAX_SPEED_FEET_PER_SEC;
|
|
|
|
|
// double angleCorrection = angleMotor.getSelectedSensorVelocity() * 2.69;
|
2022-03-05 11:12:33 -07:00
|
|
|
|
|
|
|
|
// driveMotor.set(TalonFXControlMode.Velocity, angleCorrection +
|
|
|
|
|
// (Units.metersToInches(state.speedMetersPerSecond) *
|
|
|
|
|
// SwerveDriveConstants.TICKS_PER_INCH) / 10);
|
2022-02-24 18:54:58 -07:00
|
|
|
driveMotor.set(normFtPerSec);// - angleMotor.get());
|
2022-03-05 11:12:33 -07:00
|
|
|
// driveMotor.set(TalonFXControlMode.Velocity, angleCorrection); // Ratio
|
|
|
|
|
// between axis = 1/1.75 Ratio of wheel is 5.14/1 ratio of steer is 12.8/1
|
2022-02-19 15:48:39 -07:00
|
|
|
|
2022-02-24 18:54:58 -07:00
|
|
|
// m_currentTime = System.currentTimeMillis();
|
|
|
|
|
// m_deltaTime = (double) (m_currentTime - m_lastTime);
|
|
|
|
|
// m_deltaTime = m_deltaTime / 10.0;
|
|
|
|
|
|
|
|
|
|
// m_currentPos = driveMotor.getSelectedSensorPosition();
|
|
|
|
|
|
|
|
|
|
// double m_desiredCorrectionVel = 3.2 * angleMotor.getSelectedSensorVelocity();
|
2022-03-05 11:12:33 -07:00
|
|
|
// double m_desiredCorrectionPos = (m_deltaTime * m_desiredCorrectionVel) %
|
|
|
|
|
// 2048;
|
|
|
|
|
// double m_lastPos = (driveMotor.getSelectedSensorPosition() % 2048) -
|
|
|
|
|
// (m_deltaTime * driveMotor.getSelectedSensorVelocity());
|
|
|
|
|
// double m_actualDesiredPos = m_deltaTime *
|
|
|
|
|
// ((Units.metersToInches(state.speedMetersPerSecond) *
|
|
|
|
|
// SwerveDriveConstants.TICKS_PER_INCH) / 10);
|
|
|
|
|
|
2022-02-24 18:54:58 -07:00
|
|
|
// System.out.println("Current Pos: " + driveMotor.getSelectedSensorPosition());
|
|
|
|
|
// System.out.println("Desired Correction Pos: " + m_desiredCorrectionPos);
|
|
|
|
|
// System.out.println("Last Pos: " + m_lastPos);
|
|
|
|
|
|
|
|
|
|
// driveMotor.set(TalonFXControlMode.Position, 1500/*m_desiredCorrectionPos*/);
|
|
|
|
|
|
|
|
|
|
// m_lastTime = m_currentTime;
|
|
|
|
|
// m_lastPos = m_currentPos;
|
2021-12-16 21:48:59 -07:00
|
|
|
}
|
2022-01-18 19:53:14 -07:00
|
|
|
|
|
|
|
|
/**
|
2022-02-17 19:52:05 -07:00
|
|
|
* Get current module state.
|
2022-01-18 19:53:14 -07:00
|
|
|
*
|
2022-02-17 19:52:05 -07:00
|
|
|
* @return The current state of the module in m/s.
|
2022-01-18 19:53:14 -07:00
|
|
|
*/
|
|
|
|
|
public SwerveModuleState getState() {
|
2022-01-22 15:55:04 -07:00
|
|
|
// return state;
|
2022-03-05 11:12:33 -07:00
|
|
|
return new SwerveModuleState(driveMotor.getSelectedSensorVelocity() * SwerveDriveConstants.INCHES_PER_TICK
|
|
|
|
|
* SwerveDriveConstants.METERS_PER_INCH * 10, getAngle());
|
2022-01-18 19:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 19:52:05 -07:00
|
|
|
/**
|
|
|
|
|
* Stop the drive and steer motors of current module.
|
|
|
|
|
*/
|
2022-02-03 20:53:43 -07:00
|
|
|
public void stop() {
|
|
|
|
|
driveMotor.set(0);
|
|
|
|
|
angleMotor.set(0);
|
|
|
|
|
}
|
2022-03-05 11:12:33 -07:00
|
|
|
|
2022-02-19 11:16:24 -07:00
|
|
|
@Override
|
2022-03-05 11:12:33 -07:00
|
|
|
public void periodic() {
|
2022-02-19 11:16:24 -07:00
|
|
|
Rotation2d currentRotation = getAngle();
|
2022-02-19 15:48:39 -07:00
|
|
|
SmartDashboard.putNumber("Angle Motor " + angleMotor.getDeviceID(), currentRotation.getDegrees());
|
2022-03-05 11:12:33 -07:00
|
|
|
SmartDashboard.putNumber("Drive Motor " + driveMotor.getDeviceID(),
|
|
|
|
|
((driveMotor.getSelectedSensorPosition() / 2048) * 360) % 360);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
|
canCoder.setPositionToAbsolute();
|
|
|
|
|
// canCoder.configSensorInitializationStrategy(initializationStrategy)
|
2022-02-19 11:16:24 -07:00
|
|
|
}
|
2022-02-03 20:53:43 -07:00
|
|
|
|
2022-01-12 17:04:58 -07:00
|
|
|
}
|