Has anyone Swervedrive in robotpy using NEOs and CTRE encoders?

My team is new to swerve. We are using brushless NEOs with RevSparkMax motor controllers. The swerve module is MK4i and CTRE Mag encoders are being used for the turning motors. All of the encoders and motor controllers are connected through CAN (the pdp also).

We are having trouble with trying to control swerve (specifically the turning motors). Whenever we try to turn, our turning motors keep turning left and right uncontrollably (each motor going in a different direction to). The set point of the robot also changes to different values randomly when the robot is enabled.

Our code is based off of WPImath’s example code. I have tried tweaking the PID Controller and FeedForward values but I have not gotten anywhere with it yet. I have a feeling it has to do something with the fact that WPI’s example uses PWM and a different encoder that passes different values.

If anyone has any idea on how to fix this please let me know! Also, if you need me to clarify my stated problem anywhere I can do that too.

WPI code/original code:

Our code (main branch):

P.S. You can ignore any uncompleted code that has to do with joystick buttons and shooter.py we are still working on that and I will try cleaning it up too.

It sounds like you are having some very similar problems to what we are struggling with too. I have gone through the same example and done the conversion to the CAN bus with the exact same motors and motor controllers you are using. Our problem is also the turning motors just seeming to be moving in random directions. I went through the code and displayed values starting with joystick inputs a couple days ago and verified that the swerve module states are being calculated correctly. However, we haven’t been able to get the turning motor to behave based on those swerve module states (the driving one does seem to work).

I think we are using a different encoder, but I wanted to first verify that. Do you have a pic of your encoder? I think we’re using a different one and will check on that tomorrow.

It seems like you’re missing angle offsets for your swerve modules. They don’t inherently know which way is forward, so you have to subtract a certain value from each raw encoder angle to get the angle relative to whatever the front of your robot is. Let me know if you have any questions about the process.

This is our team’s first year in swerve as well. Except for the mag encoders we have a working swerve matching your config. We have absolute encoders attached directly to the SparkMax. The code is loosely based on YAGSL but all of the ‘Generic’ aspect was dropped.

You can check swervedrive.py and swervemodule.py. Key breakthroughs were being methodical by adding a series of small tests for each motor function in the test-driver class, and using the rev utility to examine firmware settings on the sparkmax and ensuring they made sense. We don’t have angle motor offsets because the rev hardware tool allows us to set the offset. The code for a manual offset is there but isn’t well tested. I’m too new to understand how the CAN encoders would work compared to directly attached.

Hope it helps, good luck!

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