What does PID try to solve in general? Simply put, PID is a way to answer the question - how much “force” to apply to reach a target state from one state?
- Adjust the power of an AC to reach a temperature
- Apply throttle or brake to reach a speed
- Adjust the motor RPM of a drone, to reach certain rotation speed
- The “force”: output of PID system, a.k.a PID sum, it's literally the P/I/D three components added together.
- The target state: setpoint
- It’s a iterative algorithm, we need to reevaluate the output constantly based on current status
- The higher the frequency the smoother the control signal would be (subject to limitations on the signal execution component - the actuator)
The following is a block diagram of PID algorithm
The following is a simple implementation of the algorithm
const dt = time - this._prevTime; const error = setpoint - measurement; this._i += error * dt; const p = this.kp * error; const i = this.ki * this._i; const d = this.kd * (measurement - this._prevMeasurement) / dt; const f = this.kf * (command - this._prevCommand) / dt; this._prevCommand = command; this._prevTime = time; return (p + i + d + f);