PID一般试图解决什么问题?简单地说,PID 是回答这个问题的一种方法:从一个状态到达目标状态需要施加多少“力”?
一些例子:
- 调整AC的功率以达到温度
- 使用油门或刹车达到一定速度
- 调节无人机电机转速,使机体达到指定的角速度。
一些概念:
- “力”:PID系统的输出,又名PID和,字面意思是P/I/D三个分量相加。
- 目标状态:设定点
一些事实:
- 这是一个迭代算法,我们需要根据当前状态不断重新评估输出
- 频率越高,控制信号越平滑(受信号执行部件——执行器的限制)
下面是PID算法框图
下面是算法的简单实现
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);