PID简介

备注

For a guide on implementing PID control with WPILib, see WPILib中的PID控制.

This page explains the conceptual and mathematical workings of a PID controller. A video explanation from WPI is also available.

What is a PID Controller?

The PID controller is a common feedback controller consisting of proportional, integral, and derivative terms, hence the name. This article will build up the definition of a PID controller term by term while trying to provide some intuition for how each term behaves.

First, we’ll get some nomenclature for PID controllers out of the way. In a PID context, we use the term reference or setpoint to mean the desired state of the mechanism, and the term output or process variable to refer to the measured state of the mechanism. Below are some common variable naming conventions for relevant quantities.

\(r(t)\)

setpoint, reference

\(u(t)\)

control effort

\(e(t)\)

error

\(y(t)\)

output, process variable

The error \(e(t)\) is the difference between the reference and the output, \(r(t) - y(t)\).

For those already familiar with PID control, this interpretation may not be consistent with the classical explanation of the P, I, and D terms corresponding to response to “past”, “present”, and “future” errors. While that model has merit, we will instead be approaching PID control from the viewpoint of modern control theory, as proportional controllers applied to different physical quantities we care about. This will provide a more complete explanation of the derivative term’s behavior for constant and moving setpoints.

Roughly speaking: the proportional term drives the position error to zero, the derivative term drives the velocity error to zero, and the integral term drives the total accumulated error-over-time to zero. All three terms are added together to produce the control signal. We’ll go into more detail on each of these below.

备注

Throughout the WPILib documentation, you’ll see two ways of writing the tunable constants of the PID controller.

For example, for the proportional gain:

  • \(K_p\) is the standard math-equation-focused way to notate the constant.

  • kP is a common way to see it written as a variable in software.

Despite the differences in capitalization, the two formats refer to the same concept.

比例项

The Proportional term attempts to drive the position error to zero by contributing to the control signal proportionally to the current position error. Intuitively, this tries to move the output towards the reference.

\[u(t)= K_p e(t)\]

其中 \(K_p\) 是成比例增益,\(e(t)\) 是当前时间:math:t.的误差。

下图显示了由P控制器控制的:term:`system的框图。

Diagram with a P controller

比例增益就像“软件定义的弹簧”一样,将: system`拉向所需位置。从物理学中回想起,我们将弹簧建模为 :math:`F = - kx,其中:math:F`是施加的力, :math:`k`是比例常数,:math:`x 是位移。从平衡点开始这可以用另一种方式写成:\(F = k(0-x)\) 其中:math:0 是平衡点。如果我们将平衡点设为反馈控制器的:term:setpoint,则方程具有一对一的对应关系。

\[\begin{split}F &= k(r - x) \\ u(t) &= K_p e(t) = K_p(r(t) - y(t))\end{split}\]

因此,比例控制器将 system’s 1 output`拉向 :term:`setpoint 的“力”与 :term:`error`成正比,就像弹簧一样。

导数项

The Derivative term attempts to drive the derivative of the error to zero by contributing to the control signal proportionally to the derivative of the error. Intuitively, this tries to make the output move at the same rate as the reference.

\[u(t) = K_p e(t) + K_d \frac{de}{dt}\]

其中 \(K_p`是比例增益,:math:`K_d`是微分增益,:math:`e(t)\) 是当前时间 :math:`t`的误差。

下图显示了由PD控制器控制的:term:`system的框图。

Diagram with a PD controller

PD控制器具有用于位置的比例控制器(\(K_p\)) 和用于速度的比例控制器 (\(K_d\))。速度:term:setpoint`是由位置 :term:`setpoint 随时间变化的方式隐式提供的。为了证明这一点,我们将重新排列PD控制器的公式。

\[u_k = K_p e_k + K_d \frac{e_k - e_{k-1}}{dt}\]

where \(u_k\) is the control effort at timestep \(k\) and \(e_k\) is the error at timestep \(k\). \(e_k\) is defined as \(e_k = r_k - x_k\) where \(r_k\) is the setpoint and \(x_k\) is the current state at timestep \(k\).

\[\begin{split}u_k &= K_p (r_k - x_k) + K_d \frac{(r_k - x_k) - (r_{k-1} - x_{k-1})}{dt} \\ u_k &= K_p (r_k - x_k) + K_d \frac{r_k - x_k - r_{k-1} + x_{k-1}}{dt} \\ u_k &= K_p (r_k - x_k) + K_d \frac{r_k - r_{k-1} - x_k + x_{k-1}}{dt} \\ u_k &= K_p (r_k - x_k) + K_d \frac{(r_k - r_{k-1}) - (x_k - x_{k-1})}{dt} \\ u_k &= K_p (r_k - x_k) + K_d \left(\frac{r_k - r_{k-1}}{dt} - \frac{x_k - x_{k-1}}{dt}\right)\end{split}\]

注意:math:frac{r_k - r_{k-1}}{dt}`是:term:`setpoint`的速度的方式。出于相同的原因,:math:frac{x_k - x_{k-1}}{dt}` 是 system’s 在给定时间步长下的速度。这意味着PD控制器的:math:`K_d`项将估计的速度驱动到:term:`setpoint 速度。

如果:term:`setpoint`是常数,则隐式速度:term:`setpoint`为零,因此:math:`K_d`项在移动时会降低:term:`system的速度。这就像一个“软件定义的阻尼器”。这些通常在闭门器上看到,它们的阻尼力随速度线性增加。

积分项

重要

Integral gain is generally not recommended for FRC® use. It is almost always better to use a feedforward controller to eliminate steady-state error. If you do employ integral gain, it is crucial to provide some protection against integral windup.

The Integral term attempts to drive the total accumulated error to zero by contributing to the control signal proportionally to the sum of all past errors. Intuitively, this tries to drive the average of all past output values towards the average of all past reference values.

\[u(t) = K_p e(t) + K_i \int_0^t e(\tau) \,d\tau\]

其中:math:K_p`是比例增益,:math:`K_i`是积分增益,:math:`e(t)`是当前时间 :math:`t`的误差`和:math:tau` 是集成变量。

积分从时间:math:0`到当前时间 :math:`t`进行积分。我们使用:math:tau`来进行积分,因为我们需要一个变量来在整个积分中采用多个值,但是不能使用:math:`t`来实现,因为我们已经将其定义为当前时间。

下图显示了由PI控制器控制的 system 的框图。

Block diagram of a PI controller

system 处于稳定状态时关闭系统 setpoint`时,比例项可能太小而无法将输出一直拉至 :term:`setpoint,导数项为零。这可能导致 steady-state error ,如图2.4所示。

PI controller with steady-state

A common way of eliminating steady-state error is to integrate the error and add it to the control effort. This increases the control effort until the system converges. Figure 2.4 shows an example of steady-state error for a flywheel, and figure 2.5 shows how an integrator added to the flywheel controller eliminates it. However, too high of an integral gain can lead to overshoot, as shown in figure 2.6.

Figure 2.6 and 2.6 graphs

Putting It All Together

备注

有关使用WPILib提供的PIDController的信息,请参见 relevant article

When these terms are combined by summing them all together, one gets the typical definition for a PID controller.

\[u(t) = K_p e(t) + K_i \int_0^t e(\tau) \,d\tau + K_d \frac{de}{dt}\]

其中 \(K_p\) 是比例增益,\(K_i\) 是积分增益,\(K_d`是微分增益, :math:`e(t)\) 是当前时间的误差。 \(t`和 :math:\)tau`是积分变量。

下图显示了PID控制器的框图。

Block diagram of a PID controller

回应类型

由PID控制器驱动的 system 通常具有三种类型的响应:欠阻尼,过阻尼和临界阻尼。这些如图2.8所示。

对于图2.7中的 step responses <step response>,:term:`rise time 是指:term:system`在应用 :term:`step input`后初始到达参考所花费的时间。:term:`Settling time<settling time>是指在应用:term:`step input 之后,systemreference 考建立时所花费的时间。

在建立之前,*衰减*的响应会在 reference 附近振荡。 *过度*响应

PID controller response types

上升缓慢,并且不会超出 reference。 “临界阻尼”响应具有最快的 rise time ,而不会超出 reference