# How to use Built in PID in vex v5?

So I heard that there is a built-in PID in the vex v5 and i wanted to know how to use it in autonomous? can someone help me and correct me if i am wrong.

Well depends on what you want to use it for. There are many threads on this topic, look at those first

i wanted to use the built in PID for turning. and driving forward. I dont understand how to implement that in our autonomous.

The built in PID is automatically used when you use any non-voltage motor movement functions. If you are using the normal motor position and velocity movement functions, you are already using the built in PID.

2 Likes

I encourage you to look at this explanation I made of what PID is doing (a more simplified version of PID known as a proportional loop). Maybe even try to use it to get a custom, more consistent loop

1 Like

From @Anomalocaris

How it works

Let’s start with the basic calculus involved: integrals and derivatives. Derivatives find the rate of change of a graph, while integrals find the area between a graph and the X axis. Lets take the graph y=x^2 for example. How could we determine the slope of the graph at any given point, if it changes as the value of X changes? A simple yet inefficient method of doing this is by taking tangent lines. As you can see here, the slope of the graph at X=1 is 2, and the slope at X=3 is 6. How can we make a computer do this though? With derivatives. The simplest way to take the derivative of a function f(X) = aX^n, is anX^(n-1). If we plug that into y=X^2, we get 1(2)(X)^(2-1), or y= 2X. Comparing it to what we got with tangent lines, we see this is correct, and we now have a way to find the rate of change of our position. The next thing we need to establish is integrals. Also known as anti-derivatives, they do the exact opposite of derivatives, and find the area bound by a graph and the X axis. To calculate this simply, we will do something called taking Riemann sums. This method of estimating definite integrals takes rectangles that each have one point touching the curve, and adds their area together. As you can see, it will underestimate the true integral of this graph, but a computer running this multiple times every fraction of a second will minimize the underestimation. For this specific example, only the area after the graph reaches integralActiveZone is shaded, since the integral term in our PID loop will accumulate much too quickly to control should we try and estimate the entire integral. In the loop I sent, you will also notice a variable called integralPowerLimit. This is just another limit on how much the integral term can add to the final power of the motor since again, it accumulates really quickly.

Now that we have this covered, lets look at what the actual loop is doing. We’ll start with the proportion term. Proportion is the term that looks at the current state of the robot. If you look at the loop I sent, you’ll see that to get the value of proportion, we simply multiply error by kp. kp is the proportion constant, and acts as the gain for the term so that you dont try to apply too much power to the motors, especially if you pass targets that are relatively far away. This is what the graph of a P loop looks like: Xi in this graph is the initial position of the bot. The problem with this graph is that target becomes asymptotic; since the power given to the motor decreases proportionally to error, the error will never reach 0, and the loop will run infinitely.

To solve this, we have the integral term. This looks at the past state of the robot. As stated before, this term accumulates the error as the function runs. As the bot slows down on approach to the target, this term will give it extra power, which will allow error to actually reach 0. Here is a graph of what it looks like when the I term is added: *Note: you can tune a PI loop to not oscillate like the graph shown above.
The problem with the integral term, is there is nothing to slow down the increase in power given to the motor, which can cause the bot to oscillate around error, until finally driving it to 0. This is where the derivative term comes into play.

The derivative term is looking at the future state of the robot. As stated before, derivatives find the rate of change of a graph. With this, if the integral term tries to add too much power to the motor and eventually bring the bot past the target, the derivative term will slow it down, but will still allow error to be driven to 0. Here is a graph showing what the full PID loop looks like. And just for reference, here is a comparison of all three graphs: As you can see, the PID loop is the most accurate and will drive the error to 0 quicker than the other two. These loops are very powerful, and will make your autons insanely more accurate. They were originally used on drivetrains, but are easily adaptable to any mechanism, as you can see by the fact that mine is for a tilter. However, it is important to take the time to tune it correctly. This part could take multiple hours. Start by setting ki to an insignificant number like .0000001 and kd to 0. Increase kp until the bot has consistent indefinite oscillation about target. After that, increase ki (probably start at .01) until the oscillation stops. Then increase kd to decrease the time it takes to reach the target, but not too much that it oscillates again.

3 Likes