This repository was archived by the owner on Feb 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Adding PigeonViaTalonSRX and attached SparkFlex #292
Merged
thenetworkgrinch
merged 15 commits into
Yet-Another-Software-Suite:dev
from
konnorreynolds:dev
Jan 23, 2025
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
831c680
Added PigeonViaTalonSRX
konnorreynolds 55a9ebd
Merge branch 'BroncBotz3481:dev' into dev
konnorreynolds fe4204f
Update build.gradle
konnorreynolds 68da0fc
Merge branch 'dev' of https://github.com/konnorreynolds/YAGSL-Example…
konnorreynolds f0abb6e
Added swerveDrive.getPigeonAttachedTalonSRX
konnorreynolds d43c8f2
Canceled Talon Detection
konnorreynolds 9e725a5
Added the can id
konnorreynolds 5e9fb6e
First Actual Code
konnorreynolds 280561b
Revert "First Actual Code"
konnorreynolds 468de2b
Merge branch 'BroncBotz3481:dev' into dev
konnorreynolds 7e8df72
Added SparkFlexEncoderSwerve
konnorreynolds 23f821a
Merge branch 'dev' of https://github.com/konnorreynolds/YAGSL-Example…
konnorreynolds 73f6173
Reverted gePigeonAttachedTalonSRX
konnorreynolds 69c3d93
Reformat
konnorreynolds 466a6cf
Fixed incomplete SparkFlex implementation
konnorreynolds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
src/main/java/swervelib/encoders/SparkFlexEncoderSwerve.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package swervelib.encoders; | ||
|
|
||
| import com.revrobotics.AbsoluteEncoder; | ||
| import com.revrobotics.REVLibError; | ||
| import com.revrobotics.spark.SparkAbsoluteEncoder; | ||
| import com.revrobotics.spark.SparkFlex; | ||
| import com.revrobotics.spark.config.SparkMaxConfig; | ||
| import edu.wpi.first.wpilibj.Alert; | ||
| import edu.wpi.first.wpilibj.Alert.AlertType; | ||
| import swervelib.motors.SparkMaxBrushedMotorSwerve; | ||
| import swervelib.motors.SparkMaxSwerve; | ||
| import swervelib.motors.SwerveMotor; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * SparkMax absolute encoder, attached through the data port. | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public class SparkFlexEncoderSwerve extends SwerveAbsoluteEncoder | ||
| { | ||
|
|
||
| /** | ||
| * The {@link AbsoluteEncoder} representing the duty cycle encoder attached to the SparkMax. | ||
| */ | ||
| public SparkAbsoluteEncoder encoder; | ||
| /** | ||
| * An {@link Alert} for if there is a failure configuring the encoder. | ||
| */ | ||
| private Alert failureConfiguring; | ||
| /** | ||
| * An {@link Alert} for if there is a failure configuring the encoder offset. | ||
| */ | ||
| private Alert offsetFailure; | ||
| /** | ||
| * {@link SparkMaxBrushedMotorSwerve} or {@link SparkMaxSwerve} instance. | ||
| */ | ||
| private SwerveMotor sparkFlex; | ||
|
|
||
| /** | ||
| * Create the {@link SparkFlexEncoderSwerve} object as a duty cycle from the {@link SparkFlex} | ||
| * motor. | ||
| * | ||
| * @param motor Motor to create the encoder from. | ||
| * @param conversionFactor The conversion factor to set if the output is not from 0 to 360. | ||
| */ | ||
| public SparkFlexEncoderSwerve(SwerveMotor motor, int conversionFactor) | ||
| { | ||
| failureConfiguring = new Alert( | ||
| "Encoders", | ||
| "Failure configuring SparkMax Absolute Encoder", | ||
| AlertType.kWarning); | ||
| offsetFailure = new Alert( | ||
| "Encoders", | ||
| "Failure to set Absolute Encoder Offset", | ||
| AlertType.kWarning); | ||
| if (motor.getMotor() instanceof SparkFlex) | ||
| { | ||
| sparkFlex = motor; | ||
| encoder = ((SparkFlex) motor.getMotor()).getAbsoluteEncoder(); | ||
| motor.setAbsoluteEncoder(this); | ||
| motor.configureIntegratedEncoder(conversionFactor); | ||
| } else | ||
| { | ||
| throw new RuntimeException("Motor given to instantiate SparkMaxEncoder is not a CANSparkMax"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run the configuration until it succeeds or times out. | ||
| * | ||
| * @param config Lambda supplier returning the error state. | ||
| */ | ||
| private void configureSparkMax(Supplier<REVLibError> config) | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| for (int i = 0; i < maximumRetries; i++) | ||
| { | ||
| if (config.get() == REVLibError.kOk) | ||
| { | ||
| return; | ||
| } | ||
| } | ||
| failureConfiguring.set(true); | ||
| } | ||
|
|
||
| /** | ||
| * Reset the encoder to factory defaults. | ||
| */ | ||
| @Override | ||
| public void factoryDefault() | ||
| { | ||
| // Do nothing | ||
| } | ||
|
|
||
| /** | ||
| * Clear sticky faults on the encoder. | ||
| */ | ||
| @Override | ||
| public void clearStickyFaults() | ||
| { | ||
| // Do nothing | ||
| } | ||
|
|
||
| /** | ||
| * Configure the absolute encoder to read from [0, 360) per second. | ||
| * | ||
| * @param inverted Whether the encoder is inverted. | ||
| */ | ||
| @Override | ||
| public void configure(boolean inverted) | ||
| { | ||
| if (sparkFlex instanceof SparkMaxSwerve) | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| SparkMaxConfig cfg = ((SparkMaxSwerve) sparkFlex).getConfig(); | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cfg.absoluteEncoder.inverted(inverted); | ||
| ((SparkMaxSwerve) sparkFlex).updateConfig(cfg); | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the absolute position of the encoder. | ||
| * | ||
| * @return Absolute position in degrees from [0, 360). | ||
| */ | ||
| @Override | ||
| public double getAbsolutePosition() | ||
| { | ||
| return encoder.getPosition(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the instantiated absolute encoder Object. | ||
| * | ||
| * @return Absolute encoder object. | ||
| */ | ||
| @Override | ||
| public Object getAbsoluteEncoder() | ||
| { | ||
| return encoder; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the Absolute Encoder Offset inside of the SparkMax's Memory. | ||
| * | ||
| * @param offset the offset the Absolute Encoder uses as the zero point. | ||
| * @return if setting Absolute Encoder Offset was successful or not. | ||
| */ | ||
| @Override | ||
| public boolean setAbsoluteEncoderOffset(double offset) | ||
| { | ||
| if (sparkFlex instanceof SparkMaxSwerve) | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| SparkMaxConfig cfg = ((SparkMaxSwerve) sparkFlex).getConfig(); | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cfg.absoluteEncoder.zeroOffset(offset); | ||
| ((SparkMaxSwerve) sparkFlex).updateConfig(cfg); | ||
konnorreynolds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Get the velocity in degrees/sec. | ||
| * | ||
| * @return velocity in degrees/sec. | ||
| */ | ||
| @Override | ||
| public double getVelocity() | ||
| { | ||
| return encoder.getVelocity(); | ||
| } | ||
| } | ||
152 changes: 152 additions & 0 deletions
152
src/main/java/swervelib/imu/PigeonViaTalonSRXSwerve.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package swervelib.imu; | ||
|
|
||
| import com.ctre.phoenix.motorcontrol.can.TalonSRX; | ||
| import com.ctre.phoenix.sensors.WPI_PigeonIMU; | ||
| import edu.wpi.first.math.geometry.Quaternion; | ||
| import edu.wpi.first.math.geometry.Rotation3d; | ||
| import edu.wpi.first.math.geometry.Translation3d; | ||
| import edu.wpi.first.units.measure.AngularVelocity; | ||
| import edu.wpi.first.units.measure.MutAngularVelocity; | ||
| import edu.wpi.first.wpilibj.DriverStation; | ||
| import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static edu.wpi.first.units.Units.DegreesPerSecond; | ||
|
|
||
| /** | ||
| * SwerveIMU interface for the {@link WPI_PigeonIMU}. | ||
| */ | ||
| public class PigeonViaTalonSRXSwerve extends SwerveIMU | ||
| { | ||
|
|
||
|
|
||
| /** | ||
| * {@link TalonSRX} TalonSRX the IMU is attached to. | ||
| */ | ||
| private final TalonSRX talon; | ||
|
|
||
| /** | ||
| * {@link WPI_PigeonIMU} IMU device. | ||
| */ | ||
| private final WPI_PigeonIMU imu; | ||
| /** | ||
| * Mutable {@link AngularVelocity} for readings. | ||
| */ | ||
| private final MutAngularVelocity yawVel = new MutAngularVelocity(0, 0, DegreesPerSecond); | ||
| /** | ||
| * Offset for the {@link WPI_PigeonIMU}. | ||
| */ | ||
| private Rotation3d offset = new Rotation3d(); | ||
| /** | ||
| * Inversion for the gyro | ||
| */ | ||
| private boolean invertedIMU = false; | ||
|
|
||
| /** | ||
| * Generate the SwerveIMU for {@link WPI_PigeonIMU} attached to a {@link TalonSRX}. | ||
| * | ||
| * @param canid CAN ID for the {@link TalonSRX} the {@link WPI_PigeonIMU} is attached to, does not support CANBus. | ||
| */ | ||
| public PigeonViaTalonSRXSwerve(int canid) | ||
| { | ||
| talon = new TalonSRX(canid); | ||
| imu = new WPI_PigeonIMU(talon); | ||
| offset = new Rotation3d(); | ||
| SmartDashboard.putData(imu); | ||
| } | ||
|
|
||
| /** | ||
| * Reset IMU to factory default. | ||
| */ | ||
| @Override | ||
| public void factoryDefault() | ||
| { | ||
| imu.configFactoryDefault(); | ||
| } | ||
|
|
||
| /** | ||
| * Clear sticky faults on IMU. | ||
| */ | ||
| @Override | ||
| public void clearStickyFaults() | ||
| { | ||
| imu.clearStickyFaults(); | ||
| } | ||
|
|
||
| /** | ||
| * Set the gyro offset. | ||
| * | ||
| * @param offset gyro offset as a {@link Rotation3d}. | ||
| */ | ||
| public void setOffset(Rotation3d offset) | ||
| { | ||
| this.offset = offset; | ||
| } | ||
|
|
||
| /** | ||
| * Set the gyro to invert its default direction | ||
| * | ||
| * @param invertIMU invert gyro direction | ||
| */ | ||
| public void setInverted(boolean invertIMU) | ||
| { | ||
| invertedIMU = invertIMU; | ||
| } | ||
|
|
||
| /** | ||
| * Fetch the {@link Rotation3d} from the IMU without any zeroing. Robot relative. | ||
| * | ||
| * @return {@link Rotation3d} from the IMU. | ||
| */ | ||
| @Override | ||
| public Rotation3d getRawRotation3d() | ||
| { | ||
| double[] wxyz = new double[4]; | ||
| imu.get6dQuaternion(wxyz); | ||
| Rotation3d reading = new Rotation3d(new Quaternion(wxyz[0], wxyz[1], wxyz[2], wxyz[3])); | ||
| return invertedIMU ? reading.unaryMinus() : reading; | ||
| } | ||
|
|
||
| /** | ||
| * Fetch the {@link Rotation3d} from the IMU. Robot relative. | ||
| * | ||
| * @return {@link Rotation3d} from the IMU. | ||
| */ | ||
| @Override | ||
| public Rotation3d getRotation3d() | ||
| { | ||
| return getRawRotation3d().minus(offset); | ||
| } | ||
|
|
||
| /** | ||
| * Fetch the acceleration [x, y, z] from the IMU in meters per second squared. If acceleration isn't supported returns | ||
| * empty. | ||
| * | ||
| * @return {@link Translation3d} of the acceleration as an {@link Optional}. | ||
| */ | ||
| @Override | ||
| public Optional<Translation3d> getAccel() | ||
| { | ||
| short[] initial = new short[3]; | ||
| imu.getBiasedAccelerometer(initial); | ||
| return Optional.of(new Translation3d(initial[0], initial[1], initial[2]).times(9.81 / 16384.0)); | ||
| } | ||
|
|
||
| @Override | ||
| public MutAngularVelocity getYawAngularVelocity() | ||
| { | ||
| return yawVel.mut_setMagnitude(imu.getRate()); | ||
| } | ||
|
|
||
| /** | ||
| * Get the instantiated {@link WPI_PigeonIMU} IMU object. | ||
| * | ||
| * @return IMU object. | ||
| */ | ||
| @Override | ||
| public Object getIMU() | ||
| { | ||
| return imu; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.