2023-12-05 16:25:42 -06:00
|
|
|
package swervelib.encoders;
|
|
|
|
|
|
|
|
|
|
import com.revrobotics.CANSparkMax;
|
|
|
|
|
import com.revrobotics.REVLibError;
|
2024-01-15 14:37:13 -06:00
|
|
|
import com.revrobotics.SparkAnalogSensor;
|
|
|
|
|
import com.revrobotics.SparkAnalogSensor.Mode;
|
2023-12-05 16:25:42 -06:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
import swervelib.motors.SwerveMotor;
|
2024-01-17 09:17:39 -06:00
|
|
|
import swervelib.telemetry.Alert;
|
2023-12-05 16:25:42 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SparkMax absolute encoder, attached through the data port analog pin.
|
|
|
|
|
*/
|
|
|
|
|
public class SparkMaxAnalogEncoderSwerve extends SwerveAbsoluteEncoder
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-15 14:37:13 -06:00
|
|
|
* The {@link SparkAnalogSensor} representing the duty cycle encoder attached to the SparkMax analog port.
|
2023-12-05 16:25:42 -06:00
|
|
|
*/
|
2024-01-17 09:17:39 -06:00
|
|
|
public SparkAnalogSensor encoder;
|
|
|
|
|
/**
|
|
|
|
|
* An {@link Alert} for if there is a failure configuring the encoder.
|
|
|
|
|
*/
|
|
|
|
|
private Alert failureConfiguring;
|
|
|
|
|
/**
|
|
|
|
|
* An {@link Alert} for if the absolute encoder does not support integrated offsets.
|
|
|
|
|
*/
|
|
|
|
|
private Alert doesNotSupportIntegratedOffsets;
|
|
|
|
|
|
2023-12-05 16:25:42 -06:00
|
|
|
|
|
|
|
|
/**
|
2023-12-12 10:48:54 -06:00
|
|
|
* Create the {@link SparkMaxAnalogEncoderSwerve} object as a analog sensor from the {@link CANSparkMax} motor data
|
|
|
|
|
* port analog pin.
|
2023-12-05 16:25:42 -06:00
|
|
|
*
|
2024-08-24 17:40:19 -05:00
|
|
|
* @param motor Motor to create the encoder from.
|
|
|
|
|
* @param maxVoltage Maximum voltage for analog input reading.
|
2023-12-05 16:25:42 -06:00
|
|
|
*/
|
2024-08-24 17:40:19 -05:00
|
|
|
public SparkMaxAnalogEncoderSwerve(SwerveMotor motor, double maxVoltage)
|
2023-12-05 16:25:42 -06:00
|
|
|
{
|
|
|
|
|
if (motor.getMotor() instanceof CANSparkMax)
|
|
|
|
|
{
|
|
|
|
|
encoder = ((CANSparkMax) motor.getMotor()).getAnalog(Mode.kAbsolute);
|
2024-08-24 17:40:19 -05:00
|
|
|
encoder.setPositionConversionFactor(360 / maxVoltage);
|
2023-12-05 16:25:42 -06:00
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
throw new RuntimeException("Motor given to instantiate SparkMaxEncoder is not a CANSparkMax");
|
|
|
|
|
}
|
2024-01-17 09:17:39 -06:00
|
|
|
failureConfiguring = new Alert(
|
|
|
|
|
"Encoders",
|
|
|
|
|
"Failure configuring SparkMax Analog Encoder",
|
|
|
|
|
Alert.AlertType.WARNING_TRACE);
|
|
|
|
|
doesNotSupportIntegratedOffsets = new Alert(
|
|
|
|
|
"Encoders",
|
|
|
|
|
"SparkMax Analog Sensors do not support integrated offsets",
|
|
|
|
|
Alert.AlertType.WARNING_TRACE);
|
|
|
|
|
|
2023-12-05 16:25:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-17 09:17:39 -06:00
|
|
|
failureConfiguring.set(true);
|
2023-12-05 16:25:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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()
|
|
|
|
|
{
|
2024-08-24 17:40:19 -05:00
|
|
|
return encoder.getPosition();
|
2023-12-05 16:25:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the instantiated absolute encoder Object.
|
|
|
|
|
*
|
|
|
|
|
* @return Absolute encoder object.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object getAbsoluteEncoder()
|
|
|
|
|
{
|
|
|
|
|
return encoder;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-12 10:48:54 -06:00
|
|
|
/**
|
|
|
|
|
* Sets the Absolute Encoder offset at the Encoder Level.
|
|
|
|
|
*
|
|
|
|
|
* @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)
|
|
|
|
|
{
|
2024-01-17 09:17:39 -06:00
|
|
|
doesNotSupportIntegratedOffsets.set(true);
|
2023-12-12 10:48:54 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 16:25:42 -06:00
|
|
|
/**
|
|
|
|
|
* Get the velocity in degrees/sec.
|
|
|
|
|
*
|
|
|
|
|
* @return velocity in degrees/sec.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public double getVelocity()
|
|
|
|
|
{
|
|
|
|
|
return encoder.getVelocity();
|
|
|
|
|
}
|
|
|
|
|
}
|