I don’t have any practical experience in tuning PIDs. I believe most use th **Ziegler-Nichols** method. For this you need to set your motor’s brake mode to coast so you get overshoot and oscillations.

I can think of other ideas based on P-only. That would be make sure kP is big enough to move the motors at least to the dead band. Determine your how far from your setPos you want to slow down and make sure kP*error is 12 at that point.

However, now that you have it working I have some optimization for you.

add a clamp function so the PID output and Integral output don’t exceed +/-12V

VexCode is using an archaic version of C++ so you need to make your own. Google cpp clamp to see how it is called and match that so when VEX gets around to updating you can just remove your version.

fix integral:

```
// Integral
iTerm += kI * error; // allow live tuning for kI
// there is accumulative windup so clamp output
// if error is large for long time the integral can be come
// very large and overwhelm the output
iTerm = clamp(iTerm, -12.0, 12.0);
```

Fix derivative (which is really delta)

```
// Derivative Term
// delta is de/dt (delta error w.r.t. time) = dsP/dt - dcP/dt
// if setPosition is not changing dsP/dt = 0
// and when sP is changing treat it as if it isn't so it doesn't
// cause a large delta in error (i.e. derivative kick)
// then dcP/dt is (curPos - lastPos)
delta = -(currentPosition - lastPosition);
dTerm = kD * delta;
```

finally clamp the output

```
double lateralMotorPower = clamp( pTerm + iTerm + dTerm, -12.0, 12.0);
```

notice i’ve changed some of the variables to read more like PID code