Mag Encoder Position not holding

Hey everyone, while working with our Swerve have noticed an odd idiosyncrasy with our MAG Encoders losing their position after the robot has been off for a little. For example we’ll run the swerve where everything works correctly but upon powering down and trying to drive it later sometimes a couple of the modules lose their heading and turn to seemingly random angles until re calibrated. This seems to only happen while the robot has been off for 20 mins or so. One re-calibrated and the robot remains on we see no issues in the modules heading. We’re using PWM readings from the mag. Not detrimental but definitely annoying, Thanks!.

Here’s a snippet from the SwerveModule.java class if it helps
https://pastebin.com/gxdVqqGM

Looks like you’re seeding your sensor incorrectly. On line 40, you set the position of your SelectedSensor (relative mag encoder) using absolutePosition, but absolutePosition is your SelectedSensorPosition so you set it to what it already was.

It looks like you’re trying to imitate this in the Phoenix Examples:

I would read over this example and probably update your code to match it a little better. There’s a nice description at the top that describes the exact use case, you can modify it to match yours better.

You can use mag encoders as absolute rather than relative. However you are setting your sensor:

steer.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, kPIDLoopIdx, kTimeoutMs);

which it seems like you guys have some kind of math converting to see the orientation. However, because you guys are running the encoder as relative, it loses its position once the robot is turned off. I suggest that you guys change the mode to absolute which will save the position of your robot even after the robot is turned off.