> For the complete documentation index, see [llms.txt](https://ascendrobotics.gitbook.io/ascend/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ascendrobotics.gitbook.io/ascend/vex-robotics/coding/vexcode-pro/advanced/coding-pids.md).

# Coding PIDs

Worth it.

A PID (proportional integral derivative) controller is an advanced coding technique to make the robot's motion consistent, reliable, and efficient.

## Theory

PIDs are used everywhere in industrial robotics--car factories, robot vacuums, and more. If a robot moves, it likely uses PIDs to move.

<figure><img src="https://4082406191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEGjozuVhiXJ51GOE7zdd%2Fuploads%2FMCSrPRF8CuKL4GwkLgF5%2Fimage.png?alt=media&amp;token=fb1fb87f-1c35-4c0b-a147-d1ede795dbab" alt="" width="375"><figcaption><p>See these robots? They use PIDs</p></figcaption></figure>

But why? PIDs do one thing, and they do it really well. They move a robot from point A to point B:

<figure><img src="https://4082406191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEGjozuVhiXJ51GOE7zdd%2Fuploads%2F2d57FzoBSe4mkmAcsKbs%2Fimage.png?alt=media&amp;token=62e4fc79-d576-452a-a9ee-02a6ab3462bc" alt=""><figcaption><p>That's a PID. Yep, it's not that bad.</p></figcaption></figure>

But how? This is where the PID algorithm comes in. There are three parts to a PID controller, given by the acronym. Each part applies a specific power to the drive motors based on certain factors:

* P (<mark style="color:red;">**Proportional**</mark>): Update motor power based on the <mark style="color:red;">**present**</mark>
* I (<mark style="color:blue;">**Integral**</mark>): Update motor power based on the <mark style="color:blue;">**past**</mark>
* D (<mark style="color:yellow;">**Derivative**</mark>): Update motor power based on the <mark style="color:yellow;">**future**</mark>

<details>

<summary>What each part really does</summary>

* <mark style="color:red;">**P**</mark>: if the robot is far from point B, set the motor power high so the robot gets there faster. If the robot is close, set the motor power low so the robot doesn't overshoot.
* <mark style="color:blue;">**I**</mark>: if the robot is close to point B, but not quite there, increase the motor power so the robot doesn't stall.
* <mark style="color:yellow;">**D**</mark>: if the robot is rapidly approaching point B, apply the brakes so the robot doesn't go too far (overshoot).

</details>

The motor power is calculated by the following line of code, which is the **heart of the PID algorithm:**

```cpp
float motorPower = (kP * proportional) + (kI * integral) + (kD * derivative);
```

That is, each part of the PID is added together to calculate motor power. `kP`, `kI`, and `kD` are constants that determine the relative weights of each portion of the PID.

#### Error

Error refers to how far the robot is from the target point. If the robot wants to go forward 20 inches, the PID algorithm would calculate the following errors.&#x20;

<figure><img src="https://4082406191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEGjozuVhiXJ51GOE7zdd%2Fuploads%2F2Po0PKuoRK2fNEvgwlky%2FScreenshot%202023-09-28%20at%208.14.44%20AM.png?alt=media&amp;token=d22e9a5f-ae2b-4798-b056-25dd3cfd61a9" alt=""><figcaption><p>Figure 3</p></figcaption></figure>

Error plays a major factor in the PID algorithm; we'll see how later. If you want some more explanation, here's a good eight-minute video explaining the theory behind PIDs:

{% embed url="<https://www.youtube.com/watch?v=UR0hOmjaHp0>" %}

Now, let's start by coding your drive PID!

{% content-ref url="/pages/ZL1GzPhtKCbKDWNSVIND" %}
[Drive PID Tutorial](/ascend/vex-robotics/coding/vexcode-pro/advanced/coding-pids/drive-pid-tutorial.md)
{% endcontent-ref %}
