Skip to content

Commit

Permalink
Added check to warn if Tuner X swerve generator is usable with the cu…
Browse files Browse the repository at this point in the history
…rrent configuration.

Added utility functions to improve quality of life.

Signed-off-by: thenetworkgrinch <thenetworkgrinch@users.noreply.github.com>
  • Loading branch information
thenetworkgrinch committed Jan 18, 2024
1 parent 875d666 commit 5fbb2d0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/swervelib/SwerveDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import swervelib.encoders.CANCoderSwerve;
import swervelib.imu.Pigeon2Swerve;
import swervelib.imu.SwerveIMU;
import swervelib.math.SwerveMath;
import swervelib.motors.TalonFXSwerve;
import swervelib.parser.SwerveControllerConfiguration;
import swervelib.parser.SwerveDriveConfiguration;
import swervelib.simulation.SwerveIMUSimulation;
import swervelib.telemetry.Alert;
import swervelib.telemetry.Alert.AlertType;
import swervelib.telemetry.SwerveDriveTelemetry;
import swervelib.telemetry.SwerveDriveTelemetry.TelemetryVerbosity;

Expand Down Expand Up @@ -139,6 +144,13 @@ public class SwerveDrive
* Maximum speed of the robot in meters per second.
*/
private double maxSpeedMPS;
/**
* Alert to recommend Tuner X if the configuration is compatible.
*/
private final Alert tunerXRecommendation = new Alert("Swerve Drive",
"Your Swerve Drive is compatible with Tuner X swerve generator, please consider using that instead of YAGSL. More information here!\n" +
"https://pro.docs.ctr-electronics.com/en/latest/docs/tuner/tuner-swerve/index.html",
AlertType.WARNING);

/**
* Creates a new swerve drivebase subsystem. Robot is controlled via the {@link SwerveDrive#drive} method, or via the
Expand Down Expand Up @@ -224,6 +236,29 @@ public SwerveDrive(
odometryThread.startPeriodic(SwerveDriveTelemetry.isSimulation ? 0.01 : 0.02);
}

/**
* Check all components to ensure that Tuner X Swerve Generator is recommended instead.
*/
private void checkIfTunerXCompatible()
{
boolean compatible = imu instanceof Pigeon2Swerve;
for (SwerveModule module : swerveModules)
{
compatible = compatible && module.getDriveMotor() instanceof TalonFXSwerve &&
module.getAngleMotor() instanceof TalonFXSwerve &&
module.getAbsoluteEncoder() instanceof CANCoderSwerve;
if (!compatible)
{
break;
}
}
if (compatible)
{
tunerXRecommendation.set(true);
}

}

/**
* Set the odometry update period in seconds.
*
Expand Down Expand Up @@ -465,6 +500,29 @@ public void setMaximumSpeeds(
setMaximumSpeed(attainableMaxModuleSpeedMetersPerSecond);
this.attainableMaxTranslationalSpeedMetersPerSecond = attainableMaxTranslationalSpeedMetersPerSecond;
this.attainableMaxRotationalVelocityRadiansPerSecond = attainableMaxRotationalVelocityRadiansPerSecond;
this.swerveController.config.maxAngularVelocity = attainableMaxRotationalVelocityRadiansPerSecond;
}

/**
* Get the maximum velocity from {@link SwerveDrive#attainableMaxTranslationalSpeedMetersPerSecond} or
* {@link SwerveDrive#maxSpeedMPS} whichever is higher.
*
* @return Maximum speed in meters/second.
*/
public double getMaximumVelocity()
{
return Math.max(this.attainableMaxTranslationalSpeedMetersPerSecond, maxSpeedMPS);
}

/**
* Get the maximum angular velocity, either {@link SwerveDrive#attainableMaxRotationalVelocityRadiansPerSecond} or
* {@link SwerveControllerConfiguration#maxAngularVelocity}.
*
* @return Maximum angular velocity in radians per second.
*/
public double getMaximumAngularVelocity()
{
return Math.max(this.attainableMaxRotationalVelocityRadiansPerSecond, swerveController.config.maxAngularVelocity);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/swervelib/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,16 @@ public SwerveMotor getDriveMotor()
return driveMotor;
}

/**
* Get the {@link SwerveAbsoluteEncoder} for the {@link SwerveModule}.
*
* @return {@link SwerveAbsoluteEncoder} for the swerve module.
*/
public SwerveAbsoluteEncoder getAbsoluteEncoder()
{
return absoluteEncoder;
}

/**
* Fetch the {@link SwerveModuleConfiguration} for the {@link SwerveModule} with the parsed configurations.
*
Expand Down

0 comments on commit 5fbb2d0

Please sign in to comment.