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

107 lines
4.5 KiB
Java
Raw Normal View History

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;
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-01-11 19:57:50 -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-01-11 11:05:52 -07:00
private WPI_TalonFX driveMotor;
private WPI_TalonFX angleMotor;
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;
2021-12-16 21:48:59 -07:00
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;
TalonFXConfiguration angleTalonFXConfiguration = new TalonFXConfiguration();
angleTalonFXConfiguration.slot0.kP = m_swerveGains.m_kP;
angleTalonFXConfiguration.slot0.kI = m_swerveGains.m_kI;
angleTalonFXConfiguration.slot0.kD = m_swerveGains.m_kD;
// 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-01-11 19:57:50 -07:00
/*TalonFXConfiguration driveTalonFXConfiguration = new TalonFXConfiguration();
driveTalonFXConfiguration.slot0.kP = kDriveP;
driveTalonFXConfiguration.slot0.kI = kDriveI;
driveTalonFXConfiguration.slot0.kD = kDriveD;
driveTalonFXConfiguration.slot0.kF = kDriveF;
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;
2021-12-16 21:48:59 -07:00
canCoder.configAllSettings(canCoderConfiguration);
}
2022-01-11 19:57:50 -07:00
2021-12-16 21:48:59 -07:00
public Rotation2d getAngle() {
2022-01-11 19:57:50 -07:00
// Note: This assumes the CANCoders are setup with the default feedback coefficient
// and the sesnor 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-01-11 19:57:50 -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-01-14 17:48:53 -07:00
2022-01-11 19:57:50 -07:00
SmartDashboard.putNumber("Motor " + angleMotor.getDeviceID(), currentRotation.getDegrees());
2021-12-16 21:48:59 -07:00
SwerveModuleState state = SwerveModuleState.optimize(desiredState, currentRotation);
// Find the difference between our current rotational position + our new rotational position
Rotation2d rotationDelta = state.angle.minus(currentRotation);
// 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
double currentTicks = canCoder.getPosition() / canCoder.configGetFeedbackCoefficient();
double desiredTicks = currentTicks + deltaTicks;
2022-01-15 14:55:07 -07:00
if (!ignoreAngle){
angleMotor.set(TalonFXControlMode.Position, desiredTicks);
}
2021-12-16 21:48:59 -07:00
2022-01-11 19:57:50 -07:00
2021-12-16 21:48:59 -07:00
double feetPerSecond = Units.metersToFeet(state.speedMetersPerSecond);
2021-12-27 17:38:12 -07:00
driveMotor.set(angleMotor.get() + feetPerSecond / SwerveDriveConstants.SPEED_FEET_PER_SECOND_AT_FULL_POWER);
2021-12-16 21:48:59 -07:00
}
2022-01-18 19:53:14 -07:00
/**
* Returns the current state of the module.
*
* @return The current state of the module.
*/
public SwerveModuleState getState() {
return new SwerveModuleState(driveMotor.getSelectedSensorVelocity() * SwerveDriveConstants.INCHES_PER_TICK * SwerveDriveConstants.METERS_PER_INCH * 10, new Rotation2d(canCoder.getPosition()));
2022-01-18 19:53:14 -07:00
}
2022-01-12 17:04:58 -07:00
}