2024-01-08 11:38:08 -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;
|
|
|
|
|
|
2024-03-15 14:39:35 -06:00
|
|
|
import javax.swing.text.StyleContext.SmallAttributeSet;
|
|
|
|
|
|
|
|
|
|
import com.ctre.phoenix.ErrorCode;
|
2024-01-08 11:38:08 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.FeedbackDevice;
|
|
|
|
|
import com.ctre.phoenix.motorcontrol.RemoteSensorSource;
|
2024-04-04 17:55:16 -06:00
|
|
|
import com.ctre.phoenix.motorcontrol.TalonFXControlMode;
|
2024-04-09 17:50:41 -06:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
|
|
|
|
import com.ctre.phoenix.sensors.CANCoder;
|
2024-04-04 17:55:16 -06:00
|
|
|
import com.ctre.phoenix.sensors.CANCoderConfiguration;
|
|
|
|
|
import com.ctre.phoenix6.configs.CANcoderConfiguration;
|
|
|
|
|
import com.ctre.phoenix6.configs.FeedbackConfigs;
|
|
|
|
|
import com.ctre.phoenix6.configs.MagnetSensorConfigs;
|
2024-04-04 17:32:22 -06:00
|
|
|
// import com.ctre.phoenix.motorcontrol.TalonFXControlMode;
|
|
|
|
|
// import com.ctre.phoenix.motorcontrol.can.TalonFXConfiguration;
|
|
|
|
|
// import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
|
|
|
|
// import com.ctre.phoenix.sensors.CANCoder;
|
|
|
|
|
// import com.ctre.phoenix.sensors.SensorInitializationStrategy;
|
2024-04-04 08:31:23 -06:00
|
|
|
import com.ctre.phoenix6.configs.Slot0Configs;
|
2024-04-04 17:32:22 -06:00
|
|
|
import com.ctre.phoenix6.configs.TalonFXConfiguration;
|
2024-04-09 17:50:41 -06:00
|
|
|
import com.ctre.phoenix6.controls.PositionVoltage;
|
2024-04-04 08:31:23 -06:00
|
|
|
import com.ctre.phoenix6.hardware.CANcoder;
|
|
|
|
|
import com.ctre.phoenix6.hardware.TalonFX;
|
2024-04-04 17:55:16 -06:00
|
|
|
import com.ctre.phoenix6.signals.FeedbackSensorSourceValue;
|
2024-01-08 11:38:08 -07:00
|
|
|
|
|
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
|
|
|
|
import edu.wpi.first.math.kinematics.SwerveModulePosition;
|
|
|
|
|
import edu.wpi.first.math.kinematics.SwerveModuleState;
|
|
|
|
|
import edu.wpi.first.math.util.Units;
|
2024-03-15 14:39:35 -06:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2024-01-08 11:38:08 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
|
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
|
|
|
|
import frc4388.utility.Gains;
|
2024-03-12 16:10:20 -06:00
|
|
|
import frc4388.utility.configurable.ConfigurableDouble;
|
2024-01-08 11:38:08 -07:00
|
|
|
|
|
|
|
|
public class SwerveModule extends SubsystemBase {
|
2024-04-04 17:32:22 -06:00
|
|
|
// private WPI_TalonFX tal;
|
2024-04-04 08:31:23 -06:00
|
|
|
private TalonFX driveMotor;
|
|
|
|
|
private TalonFX angleMotor;
|
|
|
|
|
private CANcoder encoder;
|
2024-03-15 14:39:35 -06:00
|
|
|
private int selfid;
|
2024-03-21 09:01:42 -06:00
|
|
|
// private ConfigurableDouble offsetGetter;
|
2024-03-12 16:10:20 -06:00
|
|
|
private static int swerveId = 0;
|
2024-01-08 11:38:08 -07:00
|
|
|
public static Gains swerveGains = SwerveDriveConstants.PIDConstants.SWERVE_GAINS;
|
|
|
|
|
|
|
|
|
|
/** Creates a new SwerveModule. */
|
2024-04-04 08:31:23 -06:00
|
|
|
public SwerveModule(TalonFX driveMotor, TalonFX angleMotor, CANcoder encoder, double offset) {
|
2024-01-08 11:38:08 -07:00
|
|
|
this.driveMotor = driveMotor;
|
|
|
|
|
this.angleMotor = angleMotor;
|
|
|
|
|
this.encoder = encoder;
|
2024-03-21 09:01:42 -06:00
|
|
|
// this.offsetGetter = new ConfigurableDouble("Swerve id " + swerveId, offset);
|
2024-03-15 14:39:35 -06:00
|
|
|
this.selfid = swerveId;
|
2024-03-12 16:10:20 -06:00
|
|
|
swerveId++;
|
2024-04-04 17:32:22 -06:00
|
|
|
// TalonFXConfiguration angleConfig = new TalonFXConfiguration();
|
|
|
|
|
TalonFXConfiguration cfg = new TalonFXConfiguration()
|
|
|
|
|
.withSlot0(new Slot0Configs()
|
|
|
|
|
.withKP(swerveGains.kP)
|
|
|
|
|
.withKI(swerveGains.kI)
|
2024-04-04 17:55:16 -06:00
|
|
|
.withKD(swerveGains.kD))
|
|
|
|
|
.withFeedback(new FeedbackConfigs()
|
|
|
|
|
.withFeedbackRemoteSensorID(encoder.getDeviceID())
|
|
|
|
|
.withFeedbackSensorSource(FeedbackSensorSourceValue.RemoteCANcoder));
|
|
|
|
|
// angleConfig.slot0.kP = swerveGains.kP;
|
|
|
|
|
// angleConfig.slot0.kI = swerveGains.kI;
|
|
|
|
|
// angleConfig.slot0.kD = swerveGains.kD;
|
2024-01-08 11:38:08 -07:00
|
|
|
|
2024-04-04 17:55:16 -06:00
|
|
|
// // use the CANcoder as the remote sensor for the primary TalonFX PID
|
2024-04-04 08:31:23 -06:00
|
|
|
|
2024-04-04 17:55:16 -06:00
|
|
|
// new Slot0Configs().
|
2024-04-04 08:31:23 -06:00
|
|
|
|
2024-04-04 17:55:16 -06:00
|
|
|
// angleConfig.remoteFilter0.remoteSensorDeviceID = encoder.getDeviceID();
|
|
|
|
|
// angleConfig.remoteFilter0.remoteSensorSource = RemoteSensorSource.CANCoder;
|
|
|
|
|
// angleConfig.primaryPID.selectedFeedbackSensor = FeedbackDevice.RemoteSensor0;
|
|
|
|
|
angleMotor.getConfigurator().apply(cfg);
|
|
|
|
|
// angleMotor.configAllSettings(angleConfig);
|
2024-03-15 14:39:35 -06:00
|
|
|
|
|
|
|
|
//encoder.configSensorInitializationStrategy(SensorInitializationStrategy.BootToAbsolutePosition);
|
2024-04-04 17:55:16 -06:00
|
|
|
CANcoderConfiguration coder_cfg = new CANcoderConfiguration()
|
|
|
|
|
.withMagnetSensor(new MagnetSensorConfigs()
|
|
|
|
|
.withMagnetOffset(offset));
|
|
|
|
|
encoder.getConfigurator().apply(coder_cfg);
|
2024-03-15 14:39:35 -06:00
|
|
|
reset(0);
|
2024-04-04 17:55:16 -06:00
|
|
|
// encoder.configMagnetOffset(offset);
|
2024-04-09 17:50:41 -06:00
|
|
|
// driveMotor.
|
|
|
|
|
driveMotor.setPosition(0);
|
|
|
|
|
driveMotor.getConfigurator().apply(new Slot0Configs().withKP(0.2));
|
|
|
|
|
// driveMotor.setSelectedSensorPosition(0);
|
|
|
|
|
// driveMotor.config_kP(0, 0.2);
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
2024-03-12 16:10:20 -06:00
|
|
|
|
2024-03-15 14:39:35 -06:00
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
|
|
|
|
//encoder.configMagnetOffset(offsetGetter.get());
|
|
|
|
|
//SmartDashboard.putString("Error Code: " + selfid, getstuff());
|
|
|
|
|
// SmartDashboard.putNumber("Angular Position: " + selfid, getAngle().getDegrees());
|
|
|
|
|
// SmartDashboard.putNumber("Angular Velocity: " + selfid, getAngularVel());
|
|
|
|
|
// SmartDashboard.putNumber("Drive Position: " + selfid, getDrivePos());
|
|
|
|
|
// SmartDashboard.putNumber("Drive Velocity: " + selfid, getDriveVel());
|
|
|
|
|
}
|
2024-01-08 11:38:08 -07:00
|
|
|
/**
|
|
|
|
|
* Get the drive motor of the SwerveModule
|
|
|
|
|
* @return the drive motor of the SwerveModule
|
|
|
|
|
*/
|
2024-04-04 12:18:11 -06:00
|
|
|
public TalonFX getDriveMotor() {
|
2024-01-08 11:38:08 -07:00
|
|
|
return this.driveMotor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the angle motor of the SwerveModule
|
|
|
|
|
* @return the angle motor of the SwerveModule
|
|
|
|
|
*/
|
2024-04-04 12:18:11 -06:00
|
|
|
public TalonFX getAngleMotor() {
|
2024-01-08 11:38:08 -07:00
|
|
|
return this.angleMotor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the CANcoder of the SwerveModule
|
|
|
|
|
* @return the CANcoder of the SwerveModule
|
|
|
|
|
*/
|
2024-04-04 12:18:11 -06:00
|
|
|
public CANcoder getEncoder() {
|
2024-01-08 11:38:08 -07:00
|
|
|
return this.encoder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the angle of a SwerveModule as a Rotation2d
|
|
|
|
|
* @return the angle of a SwerveModule as a Rotation2d
|
|
|
|
|
*/
|
|
|
|
|
public Rotation2d getAngle() {
|
|
|
|
|
// * Note: This assumes that the CANCoders are setup with the default feedback coefficient and the sensor value reports degrees.
|
2024-04-04 12:18:11 -06:00
|
|
|
return Rotation2d.fromDegrees(encoder.getAbsolutePosition().getValue());
|
|
|
|
|
// return Rotation2d.fromDegrees(tal.get());
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getAngularVel() {
|
2024-04-04 12:18:11 -06:00
|
|
|
return this.angleMotor.getVelocity().getValue() * 360;
|
|
|
|
|
// return this.tal.getSelectedSensorVelocity();
|
|
|
|
|
// return this.angleMotor.getSelectedSensorVelocity();
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getDrivePos() {
|
2024-04-04 12:18:11 -06:00
|
|
|
return this.driveMotor.getPosition().getValue(); // TODO: with drive test, might have to multiply or divide by 2
|
|
|
|
|
// return this.tal.getSelectedSensorPosition() / SwerveDriveConstants.Conversions.TICKS_PER_MOTOR_REV;
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getDriveVel() {
|
2024-04-04 12:18:11 -06:00
|
|
|
return this.driveMotor.getVelocity().getValue() * 360;
|
|
|
|
|
// return this.driveMotor.getSelectedSensorVelocity(0);
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
driveMotor.set(0);
|
|
|
|
|
angleMotor.set(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rotateToAngle(double angle) {
|
2024-04-09 17:50:41 -06:00
|
|
|
angleMotor.setControl(new PositionVoltage(angle));
|
|
|
|
|
// angleMotor.set(TalonFXControlMode.Position, angle);
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get state of swerve module
|
|
|
|
|
* @return speed in m/s and angle in degrees
|
|
|
|
|
*/
|
|
|
|
|
public SwerveModuleState getState() {
|
|
|
|
|
return new SwerveModuleState(
|
2024-04-09 17:50:41 -06:00
|
|
|
Units.inchesToMeters(driveMotor.getVelocity().getValue() * SwerveDriveConstants.Conversions.INCHES_PER_TICK) * SwerveDriveConstants.Conversions.TICK_TIME_TO_SECONDS,
|
|
|
|
|
// Units.inchesToMeters(driveMotor.getSelectedSensorVelocity() * SwerveDriveConstants.Conversions.INCHES_PER_TICK) * SwerveDriveConstants.Conversions.TICK_TIME_TO_SECONDS,
|
2024-01-08 11:38:08 -07:00
|
|
|
getAngle()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
2024-04-09 17:50:41 -06:00
|
|
|
return new SwerveModulePosition(Units.inchesToMeters(driveMotor.getPosition().getValue() * SwerveDriveConstants.Conversions.INCHES_PER_TICK), getAngle());
|
2024-01-08 11:38:08 -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
|
2024-04-09 17:50:41 -06:00
|
|
|
// Rotation2d rotationDelta = state.angle.minus(currentRotation);
|
|
|
|
|
|
|
|
|
|
// rotationDelta.getDegrees();
|
2024-01-08 11:38:08 -07:00
|
|
|
|
|
|
|
|
// calculate the new absolute position of the SwerveModule based on the difference in rotation
|
2024-04-09 17:50:41 -06:00
|
|
|
// double deltaTicks = (rotationDelta.getDegrees() / 360.) * SwerveDriveConstants.Conversions.CANCODER_TICKS_PER_ROTATION;
|
2024-01-08 11:38:08 -07:00
|
|
|
|
|
|
|
|
// convert the CANCoder from its position reading to ticks
|
2024-04-09 17:50:41 -06:00
|
|
|
// (new CANCoder(13)).getPosition()
|
|
|
|
|
|
|
|
|
|
// double currentTicks = encoder.getPosition().getValue() / encoder.configGetFeedbackCoefficient();
|
|
|
|
|
|
|
|
|
|
// double currentTicks = encoder.getPosition().getValue() / 0.087890625;
|
|
|
|
|
|
|
|
|
|
// angleMotor.setControl(new PositionVoltage(currentTicks + deltaTicks));
|
2024-01-08 11:38:08 -07:00
|
|
|
|
2024-04-09 17:50:41 -06:00
|
|
|
System.out.println(state.angle.getDegrees());
|
|
|
|
|
angleMotor.setControl(new PositionVoltage(state.angle.getDegrees()));
|
|
|
|
|
// angleMotor.setControl(new PositionVoltage(0));
|
|
|
|
|
// angleMotor.set(TalonFXControlMode.Position, currentTicks + deltaTicks);
|
2024-01-08 11:38:08 -07:00
|
|
|
|
|
|
|
|
double feetPerSecond = Units.metersToFeet(state.speedMetersPerSecond);
|
|
|
|
|
|
|
|
|
|
driveMotor.set((feetPerSecond / SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void reset(double position) {
|
2024-04-09 17:50:41 -06:00
|
|
|
encoder.setPosition(encoder.getAbsolutePosition().getValue());
|
|
|
|
|
// (new CANCoder(13)).setPositionToAbsolute();
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getCurrent() {
|
2024-04-09 17:50:41 -06:00
|
|
|
// angleMotor.getSupplyVoltage()
|
|
|
|
|
// return angleMotor.getMotorVoltage().getValue() + driveMotor.getMotorVoltage().getValue();
|
|
|
|
|
return angleMotor.getSupplyCurrent().getValue() + driveMotor.getSupplyCurrent().getValue();
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getVoltage() {
|
2024-04-09 17:50:41 -06:00
|
|
|
return Math.abs(angleMotor.getMotorVoltage().getValue()) + Math.abs(driveMotor.getMotorVoltage().getValue());
|
|
|
|
|
// return (Math.abs((new WPI_TalonFX(1).getMotorOutputVoltage()) + Math.abs(driveMotor.getMotorOutputVoltage()));
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|
2024-03-15 14:39:35 -06:00
|
|
|
|
|
|
|
|
// public String getstuff() {
|
|
|
|
|
// encoder.getPosition();
|
|
|
|
|
// return "" + encoder.getLastError().value;
|
|
|
|
|
// }
|
2024-01-08 11:38:08 -07:00
|
|
|
}
|