-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from WispySparks/srx-abs
Add support to Talon SRX for an integrated absolute encoder
- Loading branch information
Showing
3 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/swervelib/encoders/TalonSRXEncoderSwerve.java
This file contains 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,79 @@ | ||
package swervelib.encoders; | ||
|
||
import com.ctre.phoenix.motorcontrol.FeedbackDevice; | ||
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX; | ||
|
||
import swervelib.motors.SwerveMotor; | ||
import swervelib.motors.TalonSRXSwerve; | ||
|
||
/** | ||
* Talon SRX attached absolute encoder. | ||
*/ | ||
public class TalonSRXEncoderSwerve extends SwerveAbsoluteEncoder { | ||
|
||
/** | ||
* Multiplying by this converts native Talon SRX units into degrees. | ||
*/ | ||
private final double degreesPerSensorUnit; | ||
/** | ||
* Reference to a Talon SRX for polling its attached absolute encoder. | ||
*/ | ||
private final WPI_TalonSRX talon; | ||
|
||
/** | ||
* Creates a {@link TalonSRXEncoderSwerve}. | ||
* @param motor motor to poll the sensor from. | ||
* @param feedbackDevice the feedback device the sensor uses e.g. PWM or Analog. | ||
*/ | ||
public TalonSRXEncoderSwerve(SwerveMotor motor, FeedbackDevice feedbackDevice) { | ||
if (motor instanceof TalonSRXSwerve talonSRXSwerve) { | ||
talonSRXSwerve.setSelectedFeedbackDevice(feedbackDevice); | ||
this.talon = (WPI_TalonSRX) talonSRXSwerve.getMotor(); | ||
// https://v5.docs.ctr-electronics.com/en/stable/ch14_MCSensor.html#sensor-resolution | ||
degreesPerSensorUnit = switch (feedbackDevice) { | ||
case Analog -> 360.0 / 1024.0; | ||
default -> 360.0 / 4096.0; | ||
}; | ||
} | ||
else { | ||
throw new RuntimeException("Motor given to instantiate TalonSRXEncoder is not a WPI_TalonSRX"); | ||
} | ||
} | ||
|
||
@Override | ||
public void factoryDefault() { | ||
// Handled in TalonSRXSwerve | ||
} | ||
|
||
@Override | ||
public void clearStickyFaults() { | ||
// Handled in TalonSRXSwerve | ||
} | ||
|
||
@Override | ||
public void configure(boolean inverted) { | ||
talon.setSensorPhase(inverted); | ||
} | ||
|
||
@Override | ||
public double getAbsolutePosition() { | ||
return (talon.getSelectedSensorPosition() * degreesPerSensorUnit) % 360; | ||
} | ||
|
||
@Override | ||
public Object getAbsoluteEncoder() { | ||
return talon; | ||
} | ||
|
||
@Override | ||
public boolean setAbsoluteEncoderOffset(double offset) { | ||
talon.setSelectedSensorPosition(talon.getSelectedSensorPosition() + offset / degreesPerSensorUnit); | ||
return true; | ||
} | ||
|
||
@Override | ||
public double getVelocity() { | ||
return talon.getSelectedSensorVelocity() * 10 * degreesPerSensorUnit; | ||
} | ||
|
||
} |
This file contains 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 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