Tuning a PID Loop

I recently learned about PIDs and have implemented them for our tray, allowing it to slow down as it reaches the target. However, I do not know how to tune the Proportional, Integral, and Derivative. Is there a technique to know your PID has been tuned “perfectly”? Any help would be appreciated!

1 Like

I’ve heard you just play around with the different constants.
But take this with a grain of salt

2 Likes

In general you tune kP, kI, and kD by trial and error - I’d recommend tuning kP by itself first by setting kI and kD to zero (or just using the proportional part by itself, which should work fine in most cases).

If you’re looking for a mathematical model to tune your PID controller (Note: I’ve never tried using a math model for tuning), I’d recommend the Ziegler-Nichols Method but to be honest, just trial and error should be good enough for VRC purposes

3 Likes

Nah man I just trial and error until I feel like it’s good lol

1 Like

More seriously though, start by tuning the P value first, like how others have suggested. However, you want the system to overshoot the set point by a little bit. Then tune D to slow down the system so that it doesn’t overshoot. Finally the I value. This one is tricky since the I component of PID is mainly there to help the system overcome additional external forces that might get in the way (gravity, friction, etc). That’s why you tune I after you get your P and D down. Since it’s a tray that you’re tuning, I suggest adding the full amount of cubes that your tray can support and running it with P and D. If the tray can’t fully go up since the gravity of the additional cubes is too much, then you can start increasing I until then tray has sufficient force to stack the cubes.
TL;DR:
Tune P, then D, then after that I

8 Likes

How do you exactly tune the kP and how do you know when it works? I know you need to change the value to any number from 0 to 1 but I can’t tell a difference when the number is changed. My team’s pid loop goes on for forever when I run it and I am unsure how to fix this.

it depends on what kind of an error your using, typically for outputting the voltage to your motors in mV (1000mV to one volt) you would want something pretty high as vex limits your motors if they go past 12Volts. for kP I would recommend starting off really high at 25 and increment by 10 until it overshoots, then go back to a safe number then increment by 1

kI should be from 0-1
kD shouldnt typically never going past 2, as it can slow down much further than where it needs to be

just know the friction inside your bot and the motor config will heavily change these numbers
you should also exist the loop once it went to where it needs to go.

hope this helps!

For kP, start using increments of 10 to figure out when the robot starts moving:
0.0001
0.001
0.01
0.1

Stop immediately when the robot starts inching or moving. If it moves very rapidly then go back a decimal place and start incrementing by the designated place (for example if the robot starts moving at 0.01):

0.01
0.02
0.03

Woah! The robot became a lot more agressive at 0.3 and continues oscillating to a very agressive degree. Let’s lower it back down:

0.02

Next we shift over and increment the precision down by one

0.021
0.022
0.023
0.024

The robot may be a bit too agressive at 0.024 so let’s decrease it by one and shift down again

0.023
0.0231
0.0232

The robot seems to iscolate around 5 times at that amount before settling but it was 3 previously so let’s go back down one:

0.0231

Now we have our kP!

Repeat process for kD, except kD is a dampener. I find that kD is traditionally much higher than kP, sometimes 3-5x the amount of kP for a drivetrain. Keep that in mind and it’s better to stop the oscillation over precision in this instance, as the next step increases the precision

for kI, I would suggest introducing another variable “kI_activation_error” or something to that nature such that abs(error) < kI_activation_error you allow accumulation of totalError, otherwise set totalError to 0.

Set kI_activation_error to a reasonable amount. If the error is in inches for the drivetrain, perhaps a good number is around 3-4 inches. For turning I would suggest around the ballpark of 25-30 degrees.

for kI, you will increase it the same way similar to kP but the goal now is to have kI “snap” the robot to the center without oscillating. You will increase kI until the robot snaps to the position without oscillating. If it does oscillate, go down one.

8 Likes

Thanks so much for this dude. Our team is writing PID from scratch and we had no idea how to tune it. I really appreciate the step by step.

Is there anyway to predict your constants based on drive weight, speed, and friction? And will I have to re-tune once I add all my mechanisms because of the added weight?

3 Likes

When you posted this, the forum sent you an automated notification that this thread has been solved (and probably also that it is old).

When you see that the content is old, start a new thread. Note you were looking at this thread (with a link) then ask your question.

You got some good answers, but users have to scroll down on all the old content to get to your question. It’s annoying to wade through the old stuff, so good etiquette to start new threads.

4 Likes

They posted the question 2 years ago, what I’m more confused about is why someone decided to answer now.

4 Likes