Tuning feedforward and feedback controllers for swerve practically

Hello everyone we are planning to do swerve for the first time this upcoming year and I had a couple of questions on how to tune it. For the drive motor where you are trying to achieve a certain velocity I know it’s a good idea to implement a feedforward + feedback controller. My question is how do I tune this? Can I just lock the wheels and write code for it just treating it like a tank drive? And how do I tune pid for the turning motor? I’ve read that you can tune each module individually. I’m not sure if this is accurate. I suppose it’s important to say that because the motors are out of stock I don’t really have access to a swerve drive train. Any help is greatly appreciated.

1 Like

What motors are you waiting to come back in stock?

Kraken. We may have bought them already not sure but we are still waiting for them to get delivered. Many other teams in my area are also facing the same issue.

You may be interested in utilizing the swerve project generator for your swerve drive, does the tuning automatically. It is a really simple process Swerve Project Generator

This is how I would approach tuning a drive feedforward + feedback. I would start by trying to find kS, the static gain, which is how much voltage it takes to start moving. This can be found by slowly ramping up the voltage applied to the drivetrain until it moves, then taking just barely less than the minimum amount to move. Then I would find kV, the velocity feedforward. This can be found by applying a constant voltage and measuring the drivebase velocity once it accelerates up to speed. Velocity / Voltage should give you kV, although id test it at a few voltages to be sure its working as expected. After ive tuned the feedforward I would use a graphing application like advantagescope to see how well the drivetrain is following its target velocity, and add in a P controller as needed. If this caused overshoot I might add in a D controller, but at this point its going to depend on the response im seeing from the drivebase.

4 Likes

I think I understand how to tune the constants but I don’t understand how to physically tune the turning motor. Do I just try turning the turning motor for each module independently on the ground? What is the correct procedure?

each module should behave similarly enough to be able to use the same gains. Definitely tune it on the ground since that will change the amount of friction it experiences. The motor for this is pretty overpowered, so even for maxswerve (which used a 550 instead of a cim class for rotation) id just use a PD loop. Start by tuning P until it looks good or overshoots but doesnt oscillate, then if needed add some D to prevent overshoot.

edit: a static feedforward (kS) might be useful but probably isnt necessary, if you do want to use it do it before you tune the PID

3 Likes

That’s what I was thinking too. Thank you for confirming.

1 Like

Will check that out.

I’m a bit confused after calibration is complete does it output the feedforward and pid values?

I guess I should have clarified, the swerve project generator will create a new project with working code and precalibrated values for the feedforward and pid values. After the calibration step, there is a validation step and then a button to export working code for your swerve drive. From there you can copy and paste the relevant code, or just build out the rest of your robot from that same project. If you want to practice finding feedforward and pid values on your own, then definitely just ignore me and do exactly what apotato said above to tune it.

Oh I see thank you for clarifying. That will definitely make testing much faster. The documentation wasn’t very clear about what generating a project meant (or maybe I didn’t read enough).

Isn’t the feedforward and PID value from swerve generator same? I use it three times, and all the time are same.

I don’t think it’s supposed to be the same pid is a type of feedback control loop which is different from feedforward. The wpilib documentation explains this well.

1 Like