mirror of
https://github.com/BroncBotz3481/YAGSL
synced 2026-06-24 06:51:39 +00:00
Addressing issue #7 by reading CANCoder values until successful with 10ms delay between readings. Fall back to reading relative encoder.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package swervelib.encoders;
|
||||
|
||||
import com.ctre.phoenix.ErrorCode;
|
||||
import com.ctre.phoenix.sensors.AbsoluteSensorRange;
|
||||
import com.ctre.phoenix.sensors.CANCoderConfiguration;
|
||||
import com.ctre.phoenix.sensors.MagnetFieldStrength;
|
||||
@@ -69,24 +70,61 @@ public class CANCoderSwerve extends SwerveAbsoluteEncoder
|
||||
CANCoderConfiguration canCoderConfiguration = new CANCoderConfiguration();
|
||||
canCoderConfiguration.absoluteSensorRange = AbsoluteSensorRange.Unsigned_0_to_360;
|
||||
canCoderConfiguration.sensorDirection = inverted;
|
||||
canCoderConfiguration.initializationStrategy = SensorInitializationStrategy.BootToAbsolutePosition;
|
||||
canCoderConfiguration.initializationStrategy =
|
||||
SensorInitializationStrategy.BootToAbsolutePosition;
|
||||
canCoderConfiguration.sensorTimeBase = SensorTimeBase.PerSecond;
|
||||
encoder.configAllSettings(canCoderConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute position of the encoder.
|
||||
* Get the absolute position of the encoder. Sets {@link SwerveAbsoluteEncoder#readingError} on erroneous readings.
|
||||
*
|
||||
* @return Absolute position in degrees from [0, 360).
|
||||
*/
|
||||
@Override
|
||||
public double getAbsolutePosition()
|
||||
{
|
||||
if (encoder.getMagnetFieldStrength() != MagnetFieldStrength.Good_GreenLED)
|
||||
readingError = false;
|
||||
MagnetFieldStrength strength = encoder.getMagnetFieldStrength();
|
||||
|
||||
if (strength != MagnetFieldStrength.Good_GreenLED)
|
||||
{
|
||||
DriverStation.reportWarning("CANCoder " + encoder.getDeviceID() + " magnetic field is less than ideal.\n", false);
|
||||
DriverStation.reportWarning(
|
||||
"CANCoder " + encoder.getDeviceID() + " magnetic field is less than ideal.\n", false);
|
||||
}
|
||||
return encoder.getAbsolutePosition();
|
||||
if (strength == MagnetFieldStrength.Invalid_Unknown || strength == MagnetFieldStrength.BadRange_RedLED)
|
||||
{
|
||||
readingError = true;
|
||||
return 0;
|
||||
}
|
||||
double angle = encoder.getAbsolutePosition();
|
||||
|
||||
// Taken from democat's library.
|
||||
// Source: https://github.com/democat3457/swerve-lib/blob/7c03126b8c22f23a501b2c2742f9d173a5bcbc40/src/main/java/com/swervedrivespecialties/swervelib/ctre/CanCoderFactoryBuilder.java#L51-L74
|
||||
ErrorCode code = encoder.getLastError();
|
||||
int ATTEMPTS = 3;
|
||||
for (int i = 0; i < ATTEMPTS; i++)
|
||||
{
|
||||
if (code == ErrorCode.OK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e)
|
||||
{
|
||||
}
|
||||
angle = encoder.getAbsolutePosition();
|
||||
code = encoder.getLastError();
|
||||
}
|
||||
if (code != ErrorCode.OK)
|
||||
{
|
||||
readingError = true;
|
||||
DriverStation.reportWarning("CANCoder " + encoder.getDeviceID() + " reading was faulty, ignoring.\n", false);
|
||||
}
|
||||
|
||||
return angle;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user