2023-08-09 13:15:02 -05:00
|
|
|
package swervelib.encoders;
|
|
|
|
|
|
2024-08-24 17:27:03 -05:00
|
|
|
import com.reduxrobotics.sensors.canandmag.Canandmag;
|
2025-01-06 15:44:15 +00:00
|
|
|
import com.reduxrobotics.sensors.canandmag.CanandmagSettings;
|
2023-08-09 13:15:02 -05:00
|
|
|
|
|
|
|
|
/**
|
2024-08-24 17:27:03 -05:00
|
|
|
* HELIUM {@link Canandmag} from ReduxRobotics absolute encoder, attached through the CAN bus.
|
2023-08-09 13:15:02 -05:00
|
|
|
*/
|
2024-09-03 14:46:24 -05:00
|
|
|
public class CanAndMagSwerve extends SwerveAbsoluteEncoder
|
2023-08-09 15:05:33 -05:00
|
|
|
{
|
2023-08-09 13:15:02 -05:00
|
|
|
|
|
|
|
|
/**
|
2024-09-03 14:46:24 -05:00
|
|
|
* The {@link Canandmag} representing the CANandMag on the CAN bus.
|
2023-08-09 13:15:02 -05:00
|
|
|
*/
|
2024-08-24 17:27:03 -05:00
|
|
|
public Canandmag encoder;
|
2025-01-06 15:44:15 +00:00
|
|
|
/**
|
|
|
|
|
* The {@link Canandmag} settings object to use.
|
|
|
|
|
*/
|
|
|
|
|
public CanandmagSettings settings;
|
2023-08-09 13:15:02 -05:00
|
|
|
|
|
|
|
|
/**
|
2024-08-24 17:27:03 -05:00
|
|
|
* Create the {@link Canandmag}
|
2023-08-09 13:15:02 -05:00
|
|
|
*
|
2024-09-03 14:46:24 -05:00
|
|
|
* @param canid The CAN ID whenever the CANandMag is operating on the CANBus.
|
2023-08-09 13:15:02 -05:00
|
|
|
*/
|
2024-09-03 14:46:24 -05:00
|
|
|
public CanAndMagSwerve(int canid)
|
2023-08-09 15:05:33 -05:00
|
|
|
{
|
2024-08-24 17:27:03 -05:00
|
|
|
encoder = new Canandmag(canid);
|
2025-02-22 06:15:56 +00:00
|
|
|
settings = new CanandmagSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void close()
|
|
|
|
|
{
|
|
|
|
|
encoder.close();
|
2023-08-09 13:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the encoder to factory defaults.
|
2024-01-17 09:17:39 -06:00
|
|
|
* <p>
|
2024-01-16 16:53:07 -06:00
|
|
|
* This will not clear the stored zero offset.
|
2023-08-09 13:15:02 -05:00
|
|
|
*/
|
|
|
|
|
@Override
|
2023-08-09 15:05:33 -05:00
|
|
|
public void factoryDefault()
|
|
|
|
|
{
|
|
|
|
|
encoder.resetFactoryDefaults(false);
|
2023-08-09 13:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear sticky faults on the encoder.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2023-08-09 15:05:33 -05:00
|
|
|
public void clearStickyFaults()
|
|
|
|
|
{
|
|
|
|
|
encoder.clearStickyFaults();
|
2023-08-09 13:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-09-03 14:46:24 -05:00
|
|
|
* Configure the CANandMag to read from [0, 360) per second.
|
2023-08-09 13:15:02 -05:00
|
|
|
*
|
|
|
|
|
* @param inverted Whether the encoder is inverted.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2023-08-09 15:05:33 -05:00
|
|
|
public void configure(boolean inverted)
|
|
|
|
|
{
|
2025-01-06 15:44:15 +00:00
|
|
|
settings.setInvertDirection(inverted);
|
|
|
|
|
encoder.setSettings(settings);
|
2023-08-09 13:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the absolute position of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return Absolute position in degrees from [0, 360).
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2023-08-09 15:05:33 -05:00
|
|
|
public double getAbsolutePosition()
|
|
|
|
|
{
|
2024-01-16 16:53:07 -06:00
|
|
|
return encoder.getAbsPosition() * 360;
|
2023-08-09 13:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the instantiated absolute encoder Object.
|
|
|
|
|
*
|
|
|
|
|
* @return Absolute encoder object.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2023-08-09 15:05:33 -05:00
|
|
|
public Object getAbsoluteEncoder()
|
|
|
|
|
{
|
2023-08-09 13:15:02 -05:00
|
|
|
return encoder;
|
|
|
|
|
}
|
2023-12-05 16:25:42 -06:00
|
|
|
|
2023-12-12 10:48:54 -06:00
|
|
|
/**
|
2024-09-03 14:46:24 -05:00
|
|
|
* Cannot set the offset of the CANandMag.
|
2023-12-12 10:48:54 -06:00
|
|
|
*
|
|
|
|
|
* @param offset the offset the Absolute Encoder uses as the zero point.
|
2024-01-16 16:53:07 -06:00
|
|
|
* @return true if setting the zero point succeeded, false otherwise
|
2023-12-12 10:48:54 -06:00
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public boolean setAbsoluteEncoderOffset(double offset)
|
|
|
|
|
{
|
2025-01-06 15:44:15 +00:00
|
|
|
settings.setZeroOffset(offset);
|
|
|
|
|
return encoder.setSettings(settings);
|
2023-12-12 10:48:54 -06:00
|
|
|
}
|
|
|
|
|
|
2023-12-05 16:25:42 -06:00
|
|
|
/**
|
|
|
|
|
* Get the velocity in degrees/sec.
|
|
|
|
|
*
|
|
|
|
|
* @return velocity in degrees/sec.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public double getVelocity()
|
|
|
|
|
{
|
2024-01-16 16:53:07 -06:00
|
|
|
return encoder.getVelocity() * 360;
|
2023-12-05 16:25:42 -06:00
|
|
|
}
|
2023-08-09 13:15:02 -05:00
|
|
|
}
|