mirror of
https://github.com/BroncBotz3481/YAGSL
synced 2026-06-23 06:41:41 +00:00
Updated YAGSL to support CANandEncoders.
This commit is contained in:
80
swervelib/encoders/CanAndCoderSwerve.java
Normal file
80
swervelib/encoders/CanAndCoderSwerve.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package swervelib.encoders;
|
||||
|
||||
import com.revrobotics.AbsoluteEncoder;
|
||||
import com.revrobotics.CANSparkMax;
|
||||
import com.revrobotics.SparkMaxAbsoluteEncoder.Type;
|
||||
import swervelib.motors.SwerveMotor;
|
||||
|
||||
/**
|
||||
* SparkMax absolute encoder, attached through the data port.
|
||||
*/
|
||||
public class CanAndCoderSwerve extends SwerveAbsoluteEncoder {
|
||||
|
||||
/**
|
||||
* The {@link AbsoluteEncoder} representing the duty cycle encoder attached to
|
||||
* the SparkMax.
|
||||
*/
|
||||
public AbsoluteEncoder encoder;
|
||||
|
||||
/**
|
||||
* Create the {@link AbsoluteEncoder} object as a duty cycle. from the
|
||||
* {@link CANSparkMax} motor.
|
||||
*
|
||||
* @param motor Motor to create the encoder from.
|
||||
*/
|
||||
public CanAndCoderSwerve(SwerveMotor motor) {
|
||||
if (motor.getMotor() instanceof CANSparkMax) {
|
||||
encoder = ((CANSparkMax) motor.getMotor()).getAbsoluteEncoder(Type.kDutyCycle);
|
||||
encoder.setPositionConversionFactor(360);
|
||||
encoder.setVelocityConversionFactor(360);
|
||||
} else {
|
||||
throw new RuntimeException("Motor given to instantiate SparkMaxEncoder is not a CANSparkMax");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user