Analytical Introduction to PID controllers

V. Hunter Adams

Out[457]:

About this document

This document was assembled for students in ECE 4760, who are asked to construct and control a contrained drone like the one described below.

A separate document focuses on building a phenomenological understanding of PID controllers through demos. This document focuses on building an analytical understanding of these controllers. The hope is that the phenomenological document will help you debug your system based on the behavior that you observe in lab, and this one will help you understand that behavior.

System under consideration

Consider the system illustrated below. There is a drone motor rigidly attached to the end of a lever arm. The other end of this lever arm pivots about a hinge. There is an accelerometer/gyro rigidly attached to the arm, with which we estimate the arm angle with a complementary filter.

missing
System under consideration

As the motor spins increasingly quickly (ω rad/sec), the thrust that it generates (Fthrust) exceeds the force of gravity (Fg) and lifts the arm to increasingly large angles θ away from the vertical. This is the system that we will build and control in ECE 4760. The user will specify a target angle, θ, and a PID controller will control the motor speed to drive the arm to that angle. This document aims to systematically build an intuition for the system and for the controller that we'll use to control this system.

missing
Block diagram of system under consideration

Modeling the motor

Mechanical motor model

missing

We can mechanically model the motor as an inertial mass spinning about a shaft. This inertial mass has some moment of inertia J. An external torque, τ, rotates the shaft by some angle θ. As the shaft rotates, it experiences drag that is proportional to its rotation speed, b˙θ. The net torque on the shaft produces an angular acceleration according to τ=J¨θ.

τ=applied torqueb=drag coefficientθ=rotation angle

All of these can be combined into the following torque-balance equation:

τb˙θ=J¨θ

Electrical motor model

missing

A motor works by placing current through a wire coil armature. This armature rests in a static magnetic field, which exerts a Lorentz force on the current-carrying armature. As the armature spins, current is also induced in the opposite direction. As such, the electrical model for a DC motor is a resistor, inductor, and back-emf voltage sorce in series.

Using Kirchoff's voltage law, we arrive at an expression which relates the voltage among all the components in the electrical model of our motor:

VRiLdidtVemf=0

Rewriting Vemf

Within the operating limits of the motor (i.e. not at saturation), there is an approximately linear relationship between the voltage applied to the motor (V) and the rotation rate (˙θ). The slope of this line can be established experimentally for a particular motor, but we will use some general proportionality constant Kv.

˙θ=KvV

This is an expression that gives us rotation speed as a function of applied voltage. By rearranging this equation, we can solve for Vemf, which is generated voltage as a function of motor speed:

Vemf=1Kv˙θ

For tidyness, we'll define 1KvKe, giving us the expression:

Vemf=Ke˙θ

Substituting back into the electrical motor model yields:

VRiLdidtKe˙θ=0

Rewriting τ

We've seen that the output speed of the motor is proportional to applied voltage. Similarly, the output torque of the motor is proportional to current through the motor. The constant of proportionality is the torque constant Kt.

τ=Kti

Substituting into the mechanical motor model equation yields:

Ktib˙θ=J¨θ

Simplifying Kt and Ke

Note that in SI units, Kt=Ke. So, we'll define some term KKt=Ke=1Kv. Substituting this into our electrical and mechanical models for the motor yields:

Kib˙θ=J¨θ0=VRiLdidtK˙θ

Laplace transforms

Take the Laplace transform of each differential equation.

L(Kib˙θ=J¨θ)KI(s)bsΘ(s)=s2JΘ(s)L(0=VRiLdidtK˙θ)0=V(s)RI(s)LsI(s)KsΘ(s)

Transfer function

Let us define sΘ(s)Ω(s) and substitute/rearrange the above two equations to get our transfer function:

Ω(s)V(s)=K(Js+b)(Ls+R)+K2=KJLs2+(JR+bL)s+(bR+K2)

Bode plot

Let's take some reasonable guesses at these motor parameters and create a Bode plot for the motor response.

Step response

The motor's step response to a 1V square input:

Making some observations

What you will notice is that the motor looks like a 2-pole lowpass filter with a cutoff frequency that is quite high. When we apply a square wave input to the motor, the output speed of the motor increases effectively instantaneously from the standpoint of the pendulum. We're about to look at the pendulum dynamics, but we'll see that the pendulum is way slower than the motor. This means that the time constants are all dominated by the lowpass characteristics of the pendulum, and by the resonance of the pendulum.

Modeling the pendulum physics

We will solve for the pendulum equation of motion by setting up a torque balance. There are three contributions to the torque on the pendulum: gravity, friction, and thrust. Thrust is always pointing in the direction that the motor is pointing, and gravity is always pointing toward the center of Earth, and friction acts opposite the direction of motion.

Thrust

Thrust is approximately proporitonal to the square of the rotor rotation speed. The constant of proportionality depends on lots of stuff (properties of the propeller, the fluid, etc.), but we'll just call it KT and play with it in simulation until we get a response that looks pretty good.

τthrust=KTω2L

Gravity

The force from gravity is always toward the center of the Earth, so we must account for the angle of the beam in our torque calculation.

τg=mgLsinθ
missing

Friction

And the torque from friction is proportional to the rotation rate, ˙θ:

τf=Kf˙θ

Equation of motion

We know that the net torque equals the moment of inertia times the angular acceleration, as shown below:

τ=J¨θ

If we assume that the mass of the beam is negligible compared to the mass of the motor, then the moment of inertia is simply J=mL2. Thus:

τ=mL2¨θ

Substitute the torque expressions:

τthrustτgτf=mL2¨θKTω2LmgLsinθKf˙θ=mL2¨θ

Solve for the angular acceleration:

¨θ=f(ω,θ,˙θ)=KTmLω2gLsinθKfmL2˙θ

Linearization

That's a nonlinear equation! For sake of deploying some standard linear analysis, let's linearize this system about θ=0 (hanging vertically down), and some rotor rate ω0. We're going to study the change in pendulum angle from small changes in motor rate.

Consider the full nonlinear equation:

¨θ=f(ω,θ,˙θ)=KTmLω2gLsinθKfmL2˙θ

Take the first-order Taylor Series expansion:

¨θ+Δ¨θ=f(ω0+Δω,θ0+Δθ,˙θ0+Δ˙θ)=f(ω0,θ0,˙θ0)+fω|ω0,θ0,˙θ0Δω+fθ|ω0,θ0,˙θ0Δθ+f˙θ|ω0,θ0,˙θ0Δ˙θ

Simplify (in the case that the motor is spinning at a low rate and the arm is stationary, hanging vertically at θ0=0): Δ¨θ=fω|ω0,θ0,˙θ0Δω+fθ|ω0,θ0,˙θ0Δθ+f˙θ|ω0,θ0,˙θ0Δ˙θ

Solve for angular acceleration: Δ¨θ=2Ktω0mLΔωgL(cosθ0)ΔθKfmL2Δ˙θ

Substitute in 0 for θ0: Δ¨θ=2Ktω0mLΔωgLΔθKfmL2Δ˙θ

Pendulum Laplace transform

As with the motor, we Laplace transform this differential equation to get an algebraic equation:

s2Θ(s)=2KTω0mLΩ(s)sKfmL2Θ(s)gLΘ(s)
s2Θ(s)+sKfmL2Θ(s)+gLΘ(s)=2KTω0mLΩ(s)
Θ(s)(s2+sKfmL2+gL)=2KTω0mLΩ(s)

Pendulum transfer function

And we can rearrange this algebraic equation to arrive that the transfer function from motor rate to arm angle:

Θ(s)Ω(s)=2KTω0mL(s2+sKfmL2+gL)

Pendulum Bode plot

Let's take a look at the open-loop Bode plot for the pendulum. Note the resonant peak at ω=gL=9.80.3=5.7 rad/sec. Note also that the amplitude response falls of at -40db/decade after resonance. We have a 90 degree phase shift at the resonant frequency and then a 180 degree phase shift at high frequencies.

The pendulum has infinite phase margin, and nearly 150 dB of phase margin. That's a stable system, as evidenced by its step response:

Open-loop transfer function

We can combine the transfer functions from voltage to motor rate, and from motor rate to pendulum angle, to one tranfer function from voltage to pendulum angle. We simply multiply the two transfer functions, yielding the nightmare below:

G(s)=2KKtω0hJLMs4+(bhLm+KfJLh+hJmR)s3+(bKfLh+bhmR+KfJRh+hK2m+gJLm)s2+(bKfRh+bgLm+KfK2h+gJmR)s+(bgmR+gK2m)

What a mess! Let's define some constants to tidy this up:

α=2KKTω0β=hJLmγ=(bhLm+KfJLh+hJmR)ϵ=(bKfLh+bhmR+KfJRh+hK2m+gJLm)λ=(bKfRh+bgLm+KfK2h+gJmR)μ=(bgmR+gK2m)

So then the above simplifies to:

G(s)=αβs4+γs3+ϵs2+λs+μ

Let's take at the open-loop Bode plot for this combined system, and its step response, and make some observations.

Here is the open-loop step response:

Open-loop observations

  • Note that our open-loop system is Type 0 (no free integrators in the denominator). As such, it is able to track a step input, but with a steady-state error.
  • Note that the phase margin of the open-loop system is infinite because the gain is always less than 0dB, and the gain margin is approximately 80dB. The open-loop system is stable, but it is very slow. We will increase the speed of the system by adding proportional gain, but this will start to destabilize the system by decreasing our gain margin.
  • We can use a differentiator to increase the phase as we add gain.

Building a PID controller

We are going to add a controller of the form shown below, where Kd is the derivative gain, Kp is the proportional gain, and Ki is the integral gain. The plant physics is given by the transfer function above. The objective is to increase its speed (increase the magnitude of the output) without destabilizing it.

Note that the specific values for the gains will not be the same as in lab. The objective here is to see how the open-loop Bode plot changes as we add proportional, integral, and derivative terms to our PID controller.

missing

Proportional term

Let us start by adding proportional gain. This has the effect of lifting the magnitude plot to greater values without affecting the phase plot. This decreases (but does not eliminate) steady state error and increases the system response speed. But! It also decreases our gain margin! Note that the gain margin decreases as the magnitude plot increases, and eventually the system goes unstable (has a gain of 0dB at 180 degrees of phase). This results in the persistent oscillations shown below.

The Bode plot for the proportionally-compensated (Kp=7000) system below is stable, but barely. How far above -180 degrees is the phase plot when the gain crosses 0dB? How far below 0dB is the gain plot when the phase crosses -180? We get significant oscillation, as shown in the video.

This is the Bode plot for the open-loop system. But the step response is for the closed-loop system. Recall that, for unity gain feedback, the closed loop transfer function is given by:

H(s)=G(s)1+G(s)

So, in this case, the closed-loop transfer function is:

H(s)=Kpαβs4+γs3+ϵs2+λs+μ+Kpα

Step response, look at all that bouncing! We're approaching marginal stability.

What would happen if we increased Kp too much? Our phase margin would eventually become negative, and we go unstable! We can see that in the plot below, which uses Kp=8000, making the system unstable.

Derivative term

We can stabilize the system by adding a derivative term to the controller. The derivative term will add phase, improving our gain and phase margins and thus improving stability (at the cost of system speed). The first Bode plot below shows the gain/phase of the PD controller, and the second shows the controller+physics open-loop system. Note that we begin adding phase at approximately the resonant frequency of the pendulum. See the Bode plot below, and notice that the gain and phase margins are much improved.

What's our closed-loop step response look like now? We have much less ringing! I've also increased Kp, so we have some overshoot, but the oscillations get damped out. Our closed-loop PD-compensated transfer function is:

H(s)=Kdαs+Kpαβs5+γs4+ϵs3+(λ+Kdα)s2+(μ+Kpα)s

If our Kd were smaller, we'd see more overshoot and less damping:

Integral term

Finally, we must increase the system type (the number of free integrators in the open-loop transfer function) by 1 in order to eliminate steady-state error. We do this by adding an integral term to the controller. The relative magnitude of this term will be quite small because the integral term will remove phase and thus destabilize our system. This is a small term that, over time, will creep up on the target step input.

The first Bode is that of the PID controller, the second is that of the open-loop controller+physics.

In the video below, note that the integral term very slowly creeps the arm to the target hover angle.

Here is the Bode-plot for the open-loop, PID compensated system:

And the step response for that closed-loop system. The closed-loop transfer function for the PID-compensated system is given by:

H(s)=Kdαs+Kpα+Kiαβs5+γs4+ϵs3+(λ+Kdα)s2+(μ+Kpα)s+Kiα

Note how the steady-state error is eliminated!