Absolute encoder Values (Java)

We are currently setting up our swerve drive for this season. The absolute encoder is green but our programmers do not know how to actually define the absolute encoder. Is there a resource we can look at or is it super simple way to declare it in the code.

1 Like

There almost certainly is, but we would need to know what model encoder is in use.

1 Like

If you told us what swerve module you’re using, along with the exact combination of hardware (drive motor, rotate motor and encoder), someone will be able to point you to a repository that you can look at as an example for your exact situation.

CTRE Cancoder

MK4i
CTRE Cancoder
Neo motors
Spark Max Motor Controllers

To declare a cancoder in the code, you first need to make sure you have the phoenix 6 or 5 library installed.
In phoenix 6, it’s as simple as creating a Cancoder and applying a CancoderConfiguration to it. After that, use Status Signals to get its position.
From there, you’ll either need to reset the NEO’s position to the Cancoder’s position (with the required conversions), or do the pid in the code with a PIDController.

Declare a CANCoder variable

private int ENCODER_ID = 1; //your can id here
private CANCoder angleEncoder  = new CANCoder(ENCODER_ID);

Then you can get the reading with
angleEncoder.getAbsolutePosition();