Issue with Swerve Angle Setpoints after switching to Canandcoders

We recently built an offseason Swerve bot for programming practice and to test out some new hardware (namely Canandcoders). For the code, we essentially reused our Swerve code from last year (which we have verified is fully functional), and replaced the CANCoders with SparkMaxAbsoluteEncoders in code, and then proceeded to set the offsets to zero (since the Canandcoders have a physical reset). We deployed the code to the bot, and the swerve controls seem to be functional; however, the wheels seem to go to some random position instead of all facing the same direction.

For testing, we started by setting all the wheels straight and resetting the encoders, and I am able to see that the encoder values are all being read at 0. When I enable teleop, however, some of the wheels set to random positions (usually one at 0, one at 0.2, one at 0.6, and one at 0.8). The code is clearly aware that the encoders are not at zero, and the offsets have been set to zero, so I have no idea why this kind of issue is occurring. I would greatly appreciate if you could take a look at this code for any issues that I might be missing: GitHub - Vector8177/CubeBot24

Any other feedback/criticism is also welcome, and feel free to ping me if you need any clarification regarding the code.

Just to help narrow down the issue a little bit, is the issue that the encoders themselves (i.e. angleEncoder.getPosition() in ModuleIOSparkMax) is reading incorrectly, or that the encoder reads correctly and the offset is being applied correctly but it ends up at a non-zero position anyway during TeleOp

The encoders themselves are getting the correct reading, but the modules still end up at a non-zero position regardless (even though the offset is set to zero).

Not sure if this is possible, but could there be an offset burned into the SMAX flash somehow? Is there a way to reset the flash to default through the desktop app?

Your code is a little hard to follow (lots of constants and IO classes) but it seems to be correct. You do appear to burn flash right before calling resetToAbsolute() in your ModuleIOSparkMax class - I would recommend commenting that out or adding a delay before and after it. It could be that you’re not properly zeroing the SMAX at the beginning?

The other question I have is if you’re using the integrated NEO encoder to closed loop, or if you’re looping directly off the Canandcoder pulse width. If it’s the former, check to make sure your gear ratios work out properly.

I haven’t worked with the CANandCoders, but I think you have a unit error:

    @Override
    public void resetToAbsolute() {
        double absolutePosition = angleEncoder.getPosition() - moduleConstants.angleOffset.getDegrees();
        setPosition(absolutePosition);
    }

The first line gets the position of the encoder, and the javadoc says this is in rotations. Then you subtract the offset in degrees. Finally, this goes to setPosition, which takes rotations.

4 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.