Help With SDS Swerve (NEOs, CANcoders)

We are using SDS swerve modules (I believe they are MK4i but they could be MK4) with NEOs connected to Spark Max controllers as well as CANcoders for the absolute encoders. This is my first time trying to code for swerve, and I have almost no understanding of swerve drive as a concept and how the specifications of the robot fit in to the control code.

I am asking for a brief explanation of swerve conceptually and perhaps example could that could help us get our drivetrain up and running. I was given code that “worked last year”, but we were using Falcons last year and I assume somebody had (unsuccessfully) converted the code. This is our repo: GitHub - RambotsTeam1350/NeoSwerveTest2024: Testing neo swerve code 2024; to be used for Centerstage.

The code from last year is in the branch “myer”. We also tried to use YAGSL (see “desperation” branch), but I am unsure if I calculated my angle/drive conversion factors correctly. Using both last year’s code and YAGSL, we are getting an odd, inconsistent oscillating behavior on the angle motors. Sometimes, they seem to bounce back and forth with decreasing changes until they settle in on a fixed position, but any inputs will repeat this behavior. Other times, they simply wiggle around endlessly even without inputs.

Any help would be appreciated as I have been struggling with this for quite a while now.

I passed this on to our lead mentor. He posted our code not that long ago with your exact setup
Edit: GitHub - TorqueNados-FRC5090/PBOT-2024: Robot code for FRC 5090's practice robot in 2024

to me that sounds like your pid needs to be tuned, i could be wrong though

1 Like

Will definitely try this later.

Here is my version of your code that I tested yesterday: GitHub - RambotsTeam1350/SwerveRewrite at 5090

Here is the behavior that I observed:

looks pretty good to me, it does look like you need to set your encoder offsets to make the wheels go in the same direction when moving

i also notice the wheels turn opposite of your joystick, i believe that means some value is being flipped, im not sure which one though

Do you know how I should set these values? Right now, my CANcoders are set to signed minus one half to one half, and I am multiplying the absolute position value of each module by 360 to obtain my offset value. Is this correct?
I also think the wheels are not moving consistently between startups, and I wonder if there is some other code issue.

to set your cancoder offsets you can face your wheels forward, making sure the bevel gear is facing the same direction for all of the wheels, and then measure the cancoder position. when you have that position you can subtract it from your position readings to set forward as your zero

could you elaborate on what is happening here?

This is actively happening to our team just much more extreme. When we enable the robot the turn motors bounce around like in the video just much more violently.

In code, it seems like the position readings are multiplied by 360 for (I assume) a degrees value, but I’m not actually sure if this is the case and whether ±0.5 is the correct setting vs. the 0-1 setting. I’m setting my offsets as the absolute position value * 360 in my constants file and subtracting them in my getAbsoluteEncoderPosition().

As for the inconsistent behavior, I meant that after I enable teleop, inputting full forwards on my joystick does not always cause the wheels to make the same physical adjustments after making other inputs. For example, if I enable teleop and immediately input full forwards, there will be some reaction from the wheels. If I now make other directional inputs and then input full forwards again, the reaction from the wheels will not be the same as when I first enabled teleop. This is why I was convinced there was some other code issue, but I am using code that @Thor5090 linked above and should apparently work since they are using our exact setup.

Here’s our code if you’re still having the issue. It works great but a few things of note are that we are using PlayStation controllers so you’ll have to adjust for that and our robot is 29.5 inches x 29.5 inches.

Thank you for all your help. I was able to get everything working a few days ago using this code: GitHub - RambotsTeam1350/SwerveRewrite at pearadox

Later, I added FF and closed loop control for the driving (speed) motors as well, and I also changed all control to use voltage.

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