-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new SparkMaxAnalogEncoderSwerve to refer to absolute encoder at…
…tached to spark max analog pins. Signed-off-by: thenetworkgrinch <thenetworkgrinch@users.noreply.github.com>
- Loading branch information
1 parent
cb559f1
commit f2bc382
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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
106 changes: 106 additions & 0 deletions
106
src/main/java/swervelib/encoders/SparkMaxAnalogEncoderSwerve.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,106 @@ | ||
package swervelib.encoders; | ||
|
||
import com.revrobotics.AbsoluteEncoder; | ||
import com.revrobotics.CANSparkMax; | ||
import com.revrobotics.REVLibError; | ||
import com.revrobotics.SparkMaxAnalogSensor; | ||
import com.revrobotics.SparkMaxAnalogSensor.Mode; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import java.util.function.Supplier; | ||
import swervelib.motors.SwerveMotor; | ||
|
||
/** | ||
* SparkMax absolute encoder, attached through the data port analog pin. | ||
*/ | ||
public class SparkMaxAnalogEncoderSwerve extends SwerveAbsoluteEncoder | ||
{ | ||
|
||
/** | ||
* The {@link AbsoluteEncoder} representing the duty cycle encoder attached to the SparkMax. | ||
*/ | ||
public SparkMaxAnalogSensor encoder; | ||
|
||
/** | ||
* Create the {@link SparkMaxAnalogEncoderSwerve} object as a analog sensor from the {@link CANSparkMax} motor data port analog pin. | ||
* | ||
* @param motor Motor to create the encoder from. | ||
*/ | ||
public SparkMaxAnalogEncoderSwerve(SwerveMotor motor) | ||
{ | ||
if (motor.getMotor() instanceof CANSparkMax) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
thenetworkgrinch
Author
Contributor
|
||
{ | ||
encoder = ((CANSparkMax) motor.getMotor()).getAnalog(Mode.kAbsolute); | ||
} 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) | ||
{ | ||
for (int i = 0; i < maximumRetries; i++) | ||
{ | ||
if (config.get() == REVLibError.kOk) | ||
{ | ||
return; | ||
} | ||
} | ||
DriverStation.reportWarning("Failure configuring encoder", 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) | ||
{ | ||
encoder.setInverted(inverted); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
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
id ont' know if it's a java 17 thing but you should be able to do (if motor.getMotor() instanceof CANSparkMax canSparkMaxMotor){ encoder = canSparkMaxMotor.getAnalag(Mode.kAbsolute); }
then you don't have to cast it.