Sie sind auf Seite 1von 507

Learning Control Engineering Using MATLAB

Introduction: System Modeling The first step in the control design process is to develop appropriate mathematical models of the system derived either from physical laws or experimental data. In this section, we introduce the state-space and transfer function representations of dynamic systems. We then review some basic approaches to modeling mechanical and electrical systems and show how to enter these models into MATLAB for further analysis. Key MATLAB commands used in this tutorial are: ss , tf Contents Dynamic Systems State-Space Representation Transfer Function Representation

Mechanical Systems Example: Mass-Spring-Damper System Entering State-Space Models into MATLAB Entering Transfer Function Models into MATLAB Electrical Systems Example: RLC Circuit System Identification System Conversions Dynamic Systems Dynamic systems are systems that change or evolve in time according to a fixed rule. For many physical systems, this rule can be stated as a set of first-order differential equations: (1) In the above equation, is the state vector, a set of variables representing the configuration of the system at time . For instance in a simple mechanical mass-spring-damper system, the two state variables could be the position and velocity of the mass. is the vector of control inputs at time , representing the externally applied "forces" on the system, and is a possibly nonlinear function giving the time derivative (rate of change) of the state vector, for a particular state, input, and time. The state at any future time, , may be determined exactly given knowledge of the initial state, , and the time history of the inputs, , between and by integrating Eq.(1). Though the state variables themselves are not unique, there is a minimum number of state variables, , required in a given system for the above to hold true. is referred to as the system order and determines the dimensionality of the statespace. The system order usually corresponds to the number of independent energy storage elements in the system. The relationship given in Eq.(1) is very general and can be used to describe a wide variety of different systems; unfortunately, it may be very difficult to analyze. There are two common simplifications which make the problem more tractable. First, if the function, , does not depend explicitly on time, i.e. , then the system is said to be time invariant. This is often a very reasonable assumption, since the underlying physical laws themselves do not typically depend on time. For time invariant systems, the parameters or coefficients of the function, , are constant. The control input, however, may still be time dependent, . The second common assumption concerns the linearity of the system. In reality, nearly every physical system is nonlinear. In other words, is typically some complicated function of the state and inputs. These nonlinearities arise in many different ways, one of the most common in control systems being "saturation" in which an element of the system reaches a hard physical limit to its operation. Fortunately, over a

sufficiently small operating range (think tangent line near a curve), the dynamics of most systems are approximately linear, that is . Until the advent of digital computers (and to a large extent thereafter), it was only practical to analyze linear time invariant (LTI) systems. Consequently, most of the results of control theory are based on these assumptions. Fortunately, as we shall see, these results have proven to be remarkably effective and many significant engineering challenges have been solved using LTI techniques. In fact, the true power of feedback control systems are that they work (are robust) in the presence of the unavoidable modeling uncertainty. State-Space Representation For continuous linear time invariant (LTI) systems, the standard state-space representation is given below: (2) (3) where is the vector of state variables (nx1), is the time derivative of state vector (nx1), is the input or control vector (px1), is the output vector (qx1), is the system matrix (nxn), is the input matrix (nxp), is the output matrix (qxn), is the feedforward matrix (qxp). The output equation, Eq.(3), is necessary because often there are state variables which are not directly observed or are otherwise not of interest. The output matrix, , is used to specify which state variables (or combinations thereof) are available for use by the controller. Also often there is no direct feedforward in which case is the zero matrix. The state-space representation, also referred to as the time-domain representation, can easily handle multiinput/multi-output (MIMO) systems, systems with non-zero initial conditions, and nonlinear systems via Eq.(1). Consequently, the state-space representation is used extensively in "modern" control theory. Transfer Function Representation LTI systems have the extremely important property that if the input to the system is sinusoidal, then the output will also be sinusoidal at the same frequency but in general with different magnitude and phase. These magnitude and phase differences as a function of frequency are known as the frequency response of the system. Using the Laplace transform, it is possible to convert a system's time-domain representation into a frequency-domain output/input representation, known as the transfer function. In so doing, it also transforms the governing differential equation into an algebraic equation which is often easier to analyze. The Laplace transform of a time domain function, (4) where the parameter is a complex frequency variable. It is very rare in practice that you will have to directly evaluate a Laplace transform (though you should certainly know how). It is much more , is defined below:

common to look up the transform of the function you are interested in in a table such as the one found here: Laplace Transform Table The Laplace transform of the nth derivative of a function is particularly important: (5) Frequency-domain methods are most often used for analyzing LTI single-input/single-output (SISO) systems, e.g. those governed by a constant coefficient differential equation as follows: (6) The Laplace transform of this equation is given below: (7) where and are the Laplace Transforms of and respectively. Note that when finding transfer functions, we always assume that the each of the initial conditions, , , , etc. is zero. The transfer function from input to output is therefore: (8) It is useful to factor the numerator and denominator of the transfer function into the so called zero-polegain form: (9) The zeros of the transfer function, , are the roots of the numerator polynomial, i.e. the values of s such that . The poles of the transfer function, , are the roots of the denominator polynomial, i.e. the values of s such that . Both the zeros and poles may be complex valued (have both real and imaginary parts). The systemGain is . Note that we can also determine the transfer function directly form the state-space representation as follows: (10) Mechanical Systems Newton's laws of motion form the basis for analyzing mechanical systems. Newtons second law, Eq. (11), states that the sum of the forces acting on a body equals its mass times acceleration. Newton's third law, for our purposes, states that if two bodies are connected, then they experience the same magnitude force acting in opposite directions. (11)

When applying this equation, it is best to construct a free body diagram (FBD) of the sysetm showing all applied forces. Example: Mass-Spring-Damper System

The free body diagram for this system is shown below. The spring force is proportional to the displacement of the mass, , and the viscous damping force is proportional to the velocity of the mass, . Both forces oppose the motion of the mass and are therefore shown in the negative -direction. Note also, that corresponds to the position of the mass when the spring is unstretched.

Now we proceed by summing the forces and applying Newtons second law, Eq. (11), in each direction of the problem. In this case, there are no forces acting in the -direction; however, in the -direction we have: (12) This equation, known as the governing equation, completely characterizes the dynamic state of the system. Later, we will see how to use this to calculate the response of the system to any external input, , as well as analyze system properties such as stability and performance. To determine the state-space representation of the mass-spring-damper system, we must reduce the second order governing equation to a set of two first order differential equations. To this end, we choose the position and velocity as our state variables. (13) Note also that these state variables correspond to the potential energy in the spring and the kinetic energy of the mass respectively. Often when choosing state variables it is helpful to consider the independent energy storage elements in the system. The state equation in this case is as follows:

(14) If, for instance, we are interested in controlling the position of the mass, then the output equation is as follows: (15) Entering State-Space Models into MATLAB Now we will show you how to enter the equations derived above into a m-file for MATLAB. Let's assign numerical values to each of the variables.
m k b F mass spring constant damping constant input force 1.0 kg 1.0 N/m 0.2 Ns/m 1.0 N

Create a new m-file and enter the following commands.


m = 1; k = 1; b = 0.2; F = 1;
A = [0 1; -k/m -b/m]; B = [0 1/m]'; C = [1 0]; D = [0];

sys = ss(A,B,C,D) sys =

a = x1 x1 x2 0 -1 x2 1 -0.2

b = u1 x1 0

x2

c = x1 y1 1 x2 0

d = u1 y1 0

Continuous-time state-space model.

The Laplace transform for this system assuming zero initial conditions is (16) and therefore the transfer function from force input to displacement output is (17) Entering Transfer Function Models into MATLAB Now we will show how to enter the transfer function derived above into MATLAB. Enter the following commands into the m-file in which you defined the system parameters.
s = tf('s'); sys = 1/(m*s^2+b*s+k) sys =

1 --------------s^2 + 0.2 s + 1

Continuous-time transfer function.

Note that we have used the symbolic s variable here to define our transfer function model. We recommend using this method most of the time; however, in some circumstances, for instance in older versions of MATLAB or when interfacing with SIMULINK, you may need to define the transfer function model using

the numerator and denominator polynomial coefficients directly. In these cases, use the following commands:
num = [1]; den = [m b k]; sys = tf(num,den) sys = 1 --------------s^2 + 0.2 s + 1

Continuous-time transfer function.

Electrical Systems Like Newtons laws in mechanical systems, Kirchoffs circuit laws are the basic analytical tool in electrical systems.Kirchoffs current law (KCL) states that the sum of the electrical currents entering and exiting a node in a circuit must be equal. Kirchoffs voltage law (KVL) states that the sum of voltage differences around any closed loop in the circuit is zero. When applying KVL, the source voltages are typically taken as positive and the load voltages taken as negative. Example: RLC Circuit We will now consider a simple series combination of three passive electrical elements: a resistor, an inductor, and a capacitor, known as an RLC Circuit.

Since this circuit is a single loop, each node only has one input and output; therefore, application of KCL simply shows that the current is the same throughout the circuit at any given time, . Now applying KVL around the loop and using the sign conventions indicated in the diagram, we arrive at the following governing equation.

(18) We note that that the governing equation for the RLC circuit has an analogous form to the mass-springdamper mechanical system. In particular, they are both second order systems where the charge (integral of current) corresponds to displacement, the inductance to mass, the resistance to viscous damping, and the inverse capacitance to the spring stiffness. These analogies and others like them turn out to be quite useful conceptually in understanding the behavior of dynamical systems. The state-space representation is found by choosing the charge and current as the state variables. (19) where, (20) The state equation is therefore: (21) We choose the current as ouput as follows: (22) The transfer function representation may be found by taking the Laplace transform as we did for the massspring-damper or from the state-space equation as follows: (23) (24) The RLC state-space and transfer fcuntion models can be entered into MATLAB using the same procedure as discussed for the mass-spring-damper system above. System Identification In this section, we have seen how to model systems using basic physical principles; however, often this is not possible either because the parameters of the system are uncertain, or the underlying processes are simply not known. In these cases, we must rely on experimental measurements and statistical techniques to develop a system model, a process known as system identification. System identification may be performed using either time-domain or frequency-domain data, see the Introduction: System Identification page.

Also refer to MATLABs System Identification Toolbox for more information on this subject. System Conversions Most operations in MATLAB can be performed on either the transfer function, the state-space model, or the zero-pole-gain form. Furthermore, it is simple to transfer between these if the other form of representation is required. If you need to learn how to convert from one representation to the other, see the Introduction: System Conversions page.

Introduction: System Analysis


Once appropriate mathematical models of a system have been obtained, either in state-space or transfer function form, we may then analyze these models to predict how the system will respond in both the time and frequency domains. To put this in context, control systems are often designed to improve stability, speed of response, steady-state error, or prevent oscillations. In this section, we will show how to determine these dynamic properties from the system models. Key MATLAB commands used are: tf , ssdata , pole , eig , step , pzmap , bode , ltiview Contents Time Response Overview Frequency Response Overview Stability System Order First Order Systems Second Order Systems Time Response Overview The time response represents how the state of a dynamic system changes in time when subjected to a particular input. Since the models we have derived consist of differential equations, some integration must be performed in order to determine the time response of the system. For some simple systems, a closedform analytical solution may be available. However, for most systems, especially nonlinear systems or those subject to complicated input forces, this integration must be carried out numerically. Fortunately, MATLAB provides many useful resources for calculating time responses for many types of inputs, as we shall see in the following sections. The time response of a linear dynamic system consists of the sum of the transient response which depends on the initial conditions and the steady-state response which depends on the system input. These correspond to the free (homogeneous or zero input) and the forced (inhomogeneous or non-zero input) solutions of the governing differential equations respectively. in this tutorial

Frequency Response Overview All the examples presented in this tutorial are modeled by linear constant coefficient differential equations and are thus linear time-invariant (LTI). LTI systems have the extremely important property that if the input to the system is sinusoidal, then the steady-state output will also be sinusoidal at the same frequency but in general with different magnitude and phase. These magnitude and phase differences as a function of frequency comprise the frequency response of the system. The frequency response of a system can be found from the transfer function in the following way: create a vector of frequencies (varying between zero or "DC" to infinity) and compute the value of the plant transfer function at those frequencies. If is the open-loop transfer function of a system and is the frequency vector, we then plot versus . Since is a complex number, we can plot both its magnitude and phase (the Bode Plot) or its position in the complex plane (the Nyquist Diagram). Both methods display the same information in different ways. Stability For our purposes, we will use the Bounded Input Bounded Output (BIBO) definition of stability which states that a system is stable if the output remains bounded for all bounded (finite) inputs. Practically, this means that the system will not blow up while in operation. The transfer function representation is especially useful when analyzing system stability. If all poles of the transfer function (values of s at which the denominator equals zero) have negative real parts, then the system is stable. If any pole has a positive real part, then the system is unstable. If we view the poles on the complex s-plane, then all poles must be in the left half plane (LHP) to ensure stability. If any pair of poles is on the imaginary axis, then the system is marginally stable and the system will oscillate. The poles of a LTI system model can easily be found in MATLAB using the pole command, an example if which is shown below:
s = tf('s'); G = 1/(s^2+2*s+5) pole(G) G = 1 ------------s^2 + 2 s + 5

Continuous-time transfer function. ans = -1.0000 + 2.0000i -1.0000 - 2.0000i

Thus this system is stable since the real parts of the poles are both negative. The stability of a system may also be found from the state-space representation. In fact, the poles of the transfer function are the eigenvalues of the system matrix A. We can use the eig command to calculate the eigenvalues using either the LTI system model directly, eig(G) or the system matrix as shown below.
[A,B,C,D] = ssdata(G); eig(A) ans = -1.0000 + 2.0000i -1.0000 - 2.0000i

System Order The order of a dynamic system is the order of the highest derivative of its governing differential equation. Equivalently, it is the highest power of s in the denominator of its transfer function. The important properties of first, second, and higher order systems will be reviewed in this section. First Order Systems First order systems are the simplest dynamic systems to analyze. Some common examples include cruise control systems and RC circuits. The general form of the first order differential equation is as follows (1) The first order transfer function is (2) DC Gain The DC gain, , is the ratio of the magnitude of the steady-state step response to the magnitude of the step input. From the Final Value Theorem, for stable transfer functions the DC gain is the value of the transfer function when s=0. For first order systems equal to . Time Constant The time constant is the time it takes for the system to reach 63% of the steady-state value for a step response or to decrease to 37% of the inital value for an impulse response. More generally, it represents the time scale for which the dynamics of the system are significant. Poles/Zeros There is a single real pole at negative. There are no zeros. Step Response . Therefore, the system is stable if is positive and unstable if is

We can calculate the system time response to a step input of magnitude u using the MATLAB following commands:
k_dc = 5; Tc = 10; u = 2; s = tf('s'); G = k_dc/(Tc*s+1) step(u*G) G = 5 -------10 s + 1

Continuous-time transfer function.

Note: MATLAB also provides a powerful GUI (LTI Viewer) for analyzing LTI systems which can be accessed using,ltiview('step',G). If you right click on the step response graph and select Characteristics, you can choose to have several system metrics overlaid on the response: peak response, settling time, rise time, and steady-state. Settling Time

The settling time, , is the time required for the system ouput to fall within a certain percentage (i.e. 2%) of the steady state value for a step input or equivalently to decrease to a certain percentage of the initial value for an impulse input. The settling times for first order system for the most common tolerances are provided in the table below. Note that the tighter the tolerance, the longer the system response takes to settle to within this tolerance, as expected. 10% Ts=2.3/a=2.3Tc Rise Time The rise time, , is the time required for the system output to rise from some lower level x% to some higher level y% of the final steady-state value. For first order systems, the typical range is 10% - 90%. Bode Plots The Bode Plots show the magnitude and phase of the system frequency response, the Bode plots in MATLAB using the bode(G) command. bode(G) . We can generate 5% Ts=3/a=3Tc 2% Ts=3.9/a=3.9Tc 1% Ts=4.6/a=4.6Tc

Again the same results could be obtained using the LTI viewer GUI, ltiview('bode',G)

The Bode plots use a logarithmic frequency scale, so that a larger range of frequencies are visible. Also, the magnitude is represented using the logarithmic decibel unit (dB) defined as: (3) Like frequency, the decibel scale allows us to view a much larger range of magnitudes on a single plot. Also, as we shall see in subsequent tutorials, when systems are combined or controllers are added, transfer functions are often multiplied together. Using the dB scale, we may simply add the magnitudes of the transfer functions. Note, we may also add the phase angles though these are not shown on a log scale. The low frequency magnitude of the first order bode plot is . The magnitude plot has a bend at the frequency equal to the absolute value of the pole (ie. ), and then decreases at 20dB for every factor of ten increase in frequency (-20dB/decade). The phase plot is asymptotic to 0 degrees at low frequency, and asymptotic to -90 degrees at high frequency. Between frequency 0.1a and 10a, the phase changes by approximately -45 degrees for every factor of ten increase in frequency (-45 degrees/decade). We will see in the Frequency Methods for Controller Design Section how to use Bode Plots to calculate closed loop stability and performance of feedback systems. Second Order Systems Second order systems are commonly encountered in practice, and are the simplest type of dynamic system to exhibit oscillations. In fact many real higher order systems are modeled as second order to facilitate analysis. Examples include mass-spring-damper systems and RLC circuits. The general form of the first order differential equation is as follows (4) The first order transfer function is (5) DC Gain The DC gain, , again is the ratio of the magnitude of the steady-state step response to the magnitude of the step input, and for stable systems it is the value of the transfer function when . For second order systems, (6) Damping Ratio The damping ratio is a dimensionless quantity charaterizing the energy losses in the system due to such effects as viscous friction or electrical resistance. From the above definitions, (7)

Natural Frequency The natural frequency is the frequency (in rad/s) that the system will oscillate at when there is no damping, . (8) Poles/Zeros The second order transfer function has two poles at: (9) Under Damped System If , then the system is under damped. Both poles are complex valued with negative real parts; therefore the system is stable but oscillates while approaching the steady-state value.
k_dc = 1; w_n = 10; zeta = 0.2; s = tf('s'); G1 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2); pzmap(G1) axis([-3 1 -15 15])

step(G1) axis([0 3 0 2])

Settling Time The settling time, , is the time required for the system ouput to fall within a certain percentage of the steady state value for a step input or equivalently to decrease to a certain percentage of the initial value for an impulse input. For a second order, underdamped system, the settling time can be approximated by the following equation: (10) The settling times for the most common tolerances are presented in the following table: 10% Ts=2.3/(zeta*wn) Percent Overshoot The percent overshoot is the percent by which a system exceeds its final steady-state value. For a second order under damped system, the percent overshoot is diretly related to the damping ratio by the following equation: (11) For second order under damped systems, the 2% settling time, , rise time, %OS, are related to the damping and natural frequency as shown below. (12) , and percent overshoot, 5% Ts=3/(zeta*w_n) 2% Ts=3.9/(zeta*w_n) 1% Ts=4.6/(zeta*w_n)

(13) (14) Over Damped Systems If , then the system is over damped. Both poles are real and negative; therefore the system is stable and does not oscillate. The step response and a pole-zero map of an over damped system are calculated below:
zeta = 1.2; G2 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2); pzmap(G2) axis([-20 1 -1 1])

step(G2) axis([0 1.5 0 1.5])

Critically Damped Systems If , then the system is critically damped. Both poles are real and have the same magnitude, . Critically damped systems approach steady-state quickest without oscillating. Now change the value of the damping to 1, and replot the step response and pole-zero map.
zeta = 1; G3 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2);

pzmap(G3) axis([-11 1 -1 1])

step(G3)

axis([0 1.5 0 1.5])

Undamped Systems If , then the system is undamped. In this case, the poles are purely imaginary; therefore the system is marginally stable and oscillates indefinitely.
zeta = 0;

G4 = k_dc*w_n^2/(s^2 + 2*zeta*w_n*s + w_n^2); pzmap(G4) axis([-1 1 -15 15])

step(G4) axis([0 5 -0.5 2.5])

Bode Plot We show the Bode Magnitude and Phase Plots for all damping conditions of a second order system below: bode(G1,G2,G3,G4) legend('under damped: zeta < 1','over damped: zeta > 1','critically damped: zeta = 1','undamped: zeta = 0')

The magnitude of the bode plot of a second order system drops off at -40dB per decade, while the relative phase changes from 0 to -180 degrees at -90 degrees per decade. For the under damped systems, we also see a resonance peak near the natural frequency, = 10 rad/s. The sharpness of the peak depends on the damping in the system, and is charaterized by the quality factor, or Q-Factor, defined below. The Q-factor is an important property in signal processing. (15)

Introduction: PID Controller Design


In this tutorial we will introduce a simple yet versatile feedback compensator structure, the ProportionalIntegral-Derivative (PID) controller. We will discuss the effect of each of the pid parameters on the closedloop dynamics and demonstrate how to use a PID controller to improve the system performance. Key MATLAB commands used in this tutorial are: tf , step , pid , feedback , pidtool , pidtune Contents PID Overview The Characteristics of P, I, and D Controllers Example Problem Open-Loop Step Response

Proportional Control Proportional-Derivative Control Proportional-Integral Control Proportional-Integral-Derivative Control General Tips for Designing a PID Controller Automatic PID Tuning PID Overview In this tutorial, we will consider the following unity feedback system:

The output of a PID controller, equal to the control input to the plant, in the time-domain is as follows: (1) First, let's take a look at how the PID controller works in a closed-loop system using the schematic shown above. The variable ( ) represents the tracking error, the difference between the desired input value ( ) and the actual output ( ). This error signal ( ) will be sent to the PID controller, and the controller computes both the derivative and the integral of this error signal. The control signal ( ) to the plant is equal to the proportional gain ( ) times the magnitude of the error plus the integral gain ( ) times the integral of the error plus the derivative gain ( ) times the derivative of the error. This control signal ( ) is sent to the plant, and the new output ( ) is obtained. The new output ( ) is then fed back and compared to the reference to find the new error signal ( ). The controller takes this new error signal and computes its derivative and its integral again, ad infinitum. The transfer function of a PID controller is found by taking the Laplace transform of Eq.(1). (2) = Proportional gain = Integral gain = Derivative gain

We can define a PID controller in MATLAB using the transfer function directly, for example:
Kp = 1; Ki = 1; Kd = 1; s = tf('s');

C = Kp + Ki/s + Kd*s C = s^2 + s + 1 ----------s Continuous-time transfer function.

Alternatively, we may use MATLAB's pid controller object to generate an equivalent continuous-time controller as follows:
C = pid(Kp,Ki,Kd) C = 1 Kp + Ki * --- + Kd * s s with Kp = 1, Ki = 1, Kd = 1 Continuous-time PID controller in parallel form.

Let's convert the pid object to a transfer function to see that it yields the same result as above: tf(C)
ans = s^2 + s + 1 ----------s Continuous-time transfer function.

The Characteristics of P, I, and D Controllers A proportional controller ( ) will have the effect of reducing the rise time and will reduce but never eliminate the steady-state error. An integral control ( ) will have the effect of eliminating the steady-state error for a constant or step input, but it may make the transient response slower. A derivative control ( ) will have the effect of increasing the stability of the system, reducing the overshoot, and improving the transient response. The effects of each of controller parameters, table below. , , and on a closed-loop system are summarized in the

CL RESPONSE Kp Ki Kd

RISE TIME Decrease Decrease Small Change

OVERSHOOT Increase Increase Decrease

SETTLING TIME Small Change Increase Decrease

S-S ERROR Decrease Eliminate No Change

Note that these correlations may not be exactly accurate, because , , and are dependent on each other. In fact, changing one of these variables can change the effect of the other two. For this reason, the table should only be used as a reference when you are determining the values for , and . Example Problem Suppose we have a simple mass, spring, and damper problem.

The modeling equation of this system is (3) Taking the Laplace transform of the modeling equation, we get (4) The transfer function between the displacement (5) Let
M = 1 kg b = 10 N s/m k = 20 N/m F = 1 N

and the input

then becomes

Plug these values into the above transfer function

(6) The goal of this problem is to show you how each of


Fast rise time Minimum overshoot No steady-state error

and

contributes to obtain

Open-Loop Step Response Let's first view the open-loop step response. Create a new m-file and run the following code:
s = tf('s'); P = 1/(s^2 + 10*s + 20); step(P)

The DC gain of the plant transfer function is 1/20, so 0.05 is the final value of the output to an unit step input. This corresponds to the steady-state error of 0.95, quite large indeed. Furthermore, the rise time is about one second, and the settling time is about 1.5 seconds. Let's design a controller that will reduce the rise time, reduce the settling time, and eliminate the steady-state error. Proportional Control From the table shown above, we see that the proportional controller (Kp) reduces the rise time, increases the overshoot, and reduces the steady-state error. The closed-loop transfer function of the above system with a proportional controller is: (7) Let the proportional gain ( ) equal 300 and change the m-file to the following:

Kp = 300; C = pid(Kp) T = feedback(C*P,1) t = 0:0.01:2; step(T,t) C = Kp = 300 P-only controller

T = 300 ---------------s^2 + 10 s + 320 Continuous-time transfer function.

The above plot shows that the proportional controller reduced both the rise time and the steady-state error, increased the overshoot, and decreased the settling time by small amount. Proportional-Derivative Control Now, let's take a look at a PD control. From the table shown above, we see that the derivative controller (Kd) reduces both the overshoot and the settling time. The closed-loop transfer function of the given system with a PD controller is: (8) Let equal 300 as before and let the MATLAB command window.
Kp = 300; Kd = 10; C = pid(Kp,0,Kd)

equal 10. Enter the following commands into an m-file and run it in

T = feedback(C*P,1) t = 0:0.01:2; step(T,t) C = Kp + Kd * s

with Kp = 300, Kd = 10 Continuous-time PD controller in parallel form. T = 10 s + 300 ---------------s^2 + 20 s + 320 Continuous-time transfer function.

This plot shows that the derivative controller reduced both the overshoot and the settling time, and had a small effect on the rise time and the steady-state error. Proportional-Integral Control Before going into a PID control, let's take a look at a PI control. From the table, we see that an integral controller (Ki) decreases the rise time, increases both the overshoot and the settling time, and eliminates the steady-state error. For the given system, the closed-loop transfer function with a PI control is: (9) Let's reduce the
Kp = 30; Ki = 70;

to 30, and let

equal 70. Create an new m-file and enter the following commands.

C = pid(Kp,Ki) T = feedback(C*P,1) t = 0:0.01:2; step(T,t) C = 1 Kp + Ki * --s with Kp = 30, Ki = 70 Continuous-time PI controller in parallel form. T = 30 s + 70 -----------------------s^3 + 10 s^2 + 50 s + 70 Continuous-time transfer function.

Run this m-file in the MATLAB command window, and you should get the following plot. We have reduced the proportional gain (Kp) because the integral controller also reduces the rise time and increases the overshoot as the proportional controller does (double effect). The above response shows that the integral controller eliminated the steady-state error. Proportional-Integral-Derivative Control Now, let's take a look at a PID controller. The closed-loop transfer function of the given system with a PID controller is: (10)

After several trial and error runs, the gains = 350, = 300, and = 50 provided the desired response. To confirm, enter the following commands to an m-file and run it in the command window. You should get the following step response.
Kp = 350; Ki = 300; Kd = 50; C = pid(Kp,Ki,Kd) T = feedback(C*P,1); t = 0:0.01:2; step(T,t) C = 1 Kp + Ki * --- + Kd * s s with Kp = 350, Ki = 300, Kd = 50 Continuous-time PID controller in parallel form.

Now, we have obtained a closed-loop system with no overshoot, fast rise time, and no steady-state error. General Tips for Designing a PID Controller When you are designing a PID controller for a given system, follow the steps shown below to obtain a desired response. Obtain an open-loop response and determine what needs to be improved Add a proportional control to improve the rise time Add a derivative control to improve the overshoot

Add an integral control to eliminate the steady-state error Adjust each of Kp, Ki, and Kd until you obtain a desired overall response. You can always refer to the table shown in this "PID Tutorial" page to find out which controller controls what characteristics. Lastly, please keep in mind that you do not need to implement all three controllers (proportional, derivative, and integral) into a single system, if not necessary. For example, if a PI controller gives a good enough response (like the above example), then you don't need to implement a derivative controller on the system. Keep the controller as simple as possible. Automatic PID Tuning MATLAB provides tools for automatically choosing optimal PID gains which makes the trial and error process described above unnecessary. You can access the tuning algorithm directly using pidtune or through a nice graphical user interface (GUI) using pidtool. The MATLAB automated tuning algorithm chooses PID gains to balance performance (response time, bandwidth) and robustness (stability margins). By default the algorthm designs for a 60 degree phase margin. Let's explore these automated tools by first generating a proportional controller for the mass-spring-damper system by entering the following commands: pidtool(P,'p') The pidtool GUI window, like that shown below, should appear.

Notice that the step response shown is slower than the proportional controller we designed by hand. Now click on theShow Parameters button on the top right. As expected the proportional gain constant, Kp, is lower than the one we used, Kp = 94.85 < 300. We can now interactively tune the controller parameters and immediately see the resulting response int he GUI window. Try dragging the resposne time slider to the right to 0.14s, as shown in the figure below. The response does indeeed speed up, and we can see Kp is now closer to the manual value. We can also see all the other performance and robustness parameters for the system. Note that the phase margin is 60 degrees, the default for pidtool and generally a good balance of robustness and performance.

Now let's try designing a PID controller for our system. By specifying the previously designed or (baseline) controller, C, as the second parameter, pidtool will design another PID controller (instead of P or PI) and will compare the response of the system with the automated controller with that of the baseline. pidtool(P,C) We see in the output window that the automated controller responds slower and exhibits more overshoot than the baseline. Now choose the Design Mode: Extended option at the top, which reveals more tuning parameters.

Now type in Bandwidth: 32 rad/s and Phase Margin: 90 deg to generate a controller similar in performance to the baseline. Keep in mind that a higher bandwidth (0 dB crossover of the open-loop) results in a faster rise time, and a higher phase margin reduces the overshoot and improves the system stability. Finally we note that we can generate the same controller using the command line tool pidtune instead of the pidtool GUI
opts = pidtuneOptions('CrossoverFrequency',32,'PhaseMargin',90); [C, info] = pidtune(P, 'pid', opts) C = 1 Kp + Ki * --- + Kd * s s with Kp = 320, Ki = 169, Kd = 31.5

Continuous-time PID controller in parallel form.

info = Stable: 1 CrossoverFrequency: 32 PhaseMargin: 90

Introduction: Root Locus Controller Design In this tutorial we will introduce the root locus, show how to create it using MATlAB, and demonstrate how to design feedback controllers using the root locus that satisfy certain performance criteria. Key MATLAB commands used in this tutorial are: feedback , rlocus , step , sisotool Contents Closed-Loop Poles Plotting the Root Locus of a Transfer Function Choosing a Value of K from the Root Locus Closed-Loop Response Using SISOTOOL for Root Locus Design Closed-Loop Poles The root locus of an (open-loop) transfer function is a plot of the locations (locus) of all possible closed-loop poles with proportional gain K and unity feedback.

The closed-loop transfer function is: (1) and thus the poles of the closed-loop poles of the closed-loop system are values of such that . If we write , then this equation has the form: (2) (3)

Let = order of in it).

and

= order of

(the order of a polynomial is the highest power of that appears

We will consider all positive values of K. In the limit as , the poles of the closed-loop system are or the poles of . In the limit as , the poles of the closed-loop system are or the zeros of . No matter what we pick K to be, the closed-loop system must always have poles, where is the number of poles of . The root locus must have branches, each branch starts at a pole of and goes to a zero of . If has more poles than zeros (as is often the case), and we say that has zeros at infinity. In this case, the limit of as is zero. The number of zeros at infinity is , the number of poles minus the number of zeros, and is the number of branches of the root locus that go to infinity (asymptotes). Since the root locus is actually the locations of all possible closed-loop poles, from the root locus we can select a gain such that our closed-loop system will perform the way we want. If any of the selected poles are on the right half plane, the closed-loop system will be unstable. The poles that are closest to the imaginary axis have the greatest influence on the closed-loop response, so even though the system has three or four poles, it may still act like a second or even first order system depending on the location(s) of the dominant pole(s). Plotting the Root Locus of a Transfer Function Consider an open-loop system which has a transfer function of (4) How do we design a feedback controller for the system by using the root locus method? Say our design criteria are 5% overshoot and 1 second rise time. Make a MATLAB file called rl.m. Enter the transfer function, and the command to plot the root locus:
s = tf('s'); sys = (s + 7)/(s*(s + 5)*(s + 15)*(s + 20)); rlocus(sys) axis([-22 3 -15 15])

Choosing a Value of K from the Root Locus The plot above shows all possible closed-loop pole locations for a pure proportional controller. Obviously not all of those closed-loop poles will satisfy our design criteria, To determine what part of the locus is acceptable, we can use the command sgrid(Zeta,Wn) to plot lines of constant damping ratio and natural frequency. Its two arguments are the damping ratio ( ) and natural frequency ( ) [these may be vectors if you want to look at a range of acceptable values]. In our problem, we need an overshoot less than 5% (which means a damping ratio of greater than 0.7) and a rise time of 1 second (which means a natural frequency greater than 1.8). Enter the following in the MATLAB command window:
Zeta = 0.7; Wn = 1.8; sgrid(Zeta,Wn)

On the plot above, the two dotted lines at about a 45 degree angle indicate pole locations with = 0.7; in between these lines, the poles will have > 0.7 and outside of the lines < 0.7. The semicircle indicates pole locations with a natural frequency = 1.8; inside the circle, < 1.8 and outside the circle > 1.8. Going back to our problem, to make the overshoot less than 5%, the poles have to be in between the two white dotted lines, and to make the rise time shorter than 1 second, the poles have to be outside of the white dotted semicircle. So now we know only the part of the locus outside of the semicircle and in betwen the two lines are acceptable. All the poles in this location are in the left-half plane, so the closed-loop system will be stable. From the plot above we see that there is part of the root locus inside the desired region. So in this case, we need only a proportional controller to move the poles to the desired region. You can use the rlocfind command in MATLAB to choose the desired poles on the locus: [k,poles] = rlocfind(sys) Click on the plot the point where you want the closed-loop pole to be. You may want to select the points indicated in the plot below to satisfy the design criteria.

Note that since the root locus may have more than one branch, when you select a pole, you may want to find out where the other pole (poles) are. Remember they will affect the response too. From the plot above, we see that all the poles selected (all the "+" signs) are at reasonable positions. We can go ahead and use the chosen K as our proportional controller. Closed-Loop Response

In order to find the step response, you need to know the closed-loop transfer function. You could compute this using the rules of block diagrams, or let MATLAB do it for you (there is no need to enter a value for K if the rlocfind command was used):
K = 350; sys_cl = feedback(K*sys,1) sys_cl = 350 s + 2450 -------------------------------------s^4 + 40 s^3 + 475 s^2 + 1850 s + 2450

Continuous-time transfer function.

The two arguments to the function feedback are the numerator and denominator of the open-loop system. You need to include the proportional gain that you have chosen. Unity feedback is assumed. If you have a non-unity feedback situation, look at the help file for the MATLAB function feedback, which can find the closed-loop transfer function with a gain in the feedback loop. Check out the step response of your closed-loop system: step(sys_cl)

As we expected, this response has an overshoot less than 5% and a rise time less than 1 second. Using SISOTOOL for Root Locus Design Another way to complete what was done above is to use the interactive MATLAB GUI called sisotool. Using the same model as above, first define the plant, .
s = tf('s');

plant = (s + 7)/(s*(s + 5)*(s + 15)*(s + 20));

The sisotool function can be used for analysis and design. In this case, we will focus on using the Root Locus as the design method to improve the step response of the plant. To begin, type the following into the MATLAB command window: sisotool(plant) The following window should appear. To start, select the tab labeled Graphical Tuning. Within this window, turn off Plot 2and make sure Plot 1 is the Root Locus and verify that Open Loop 1 is selected. Finally, click the button labeled Show Design Plot to bring up the tunable Root Locus plot.

In the same fashion, select the tab labeled Analysis Plots. Within this window, for Plot 1, select Step. In the Contents of Plots subwindow, select Closed Loop r to y for Plot 1. If the window does not automatically pop up, click the button labeled Show Analysis Plot.

The next thing to do is to add the design requirements to the Root Locus plot. This is done directly on the plot by right-clicking and selecting Design Requirements, New. Design requirements can be set for the Settling Time, the Percent Overshoot, the Damping Ratio, the Natural Frequency, or a Region Constraint. There is no direct requirement for Rise Time, but the natural frequency can be used for this. Here, we will set the design requirements for the damping ratio and the natural frequency just like was done with sgrid. Recall that the requirements call for = 0.7 and = 1.8. Set these within the design requirements. On the plot, any area which is still white, is an acceptable region for the poles. Zoom into the Root Locus by right-clicking on the axis and select Properties, then click the label Limits. Change the real axis to -25 to 5 and the imaginary to -2.5 to 2.5.

Also, we can see the current values of some key parameters in the response. In the Step response, rightclick on the plot and go to Characteristics and select Peak Response. Do the same for the Rise Time. There should now be two large dots on the screen indicating the location of these parameters. Click each of these dots to bring up a screen with information. Both plots should appear as shown here:

As the characteristics show on the Step response, the overshoot is acceptable, but the rise time is incredibly off. To fix this, we need to choose a new value for the gain K. Similarly to the rlocfind command, the gain of the controller can be changed directly on the root locus plot. Click and drag the pink box on the origin to the acceptable area where the poles have an imaginary component as shown below.

At the bottom of the plot, it can be seen that the loop gain has been changed to 361. Looking at the Step response, both of the values are acceptable for our requirements.

Introduction: Frequency Domain Methods for Controller Design The frequency response method of controller design may be less intuitive than other methods you have studied previously. However, it has certain advantages, especially in real-life situations such as modeling transfer functions from physical data. In this tutorial, we will see how we can use the open-loop frequency response of a system to predict its behavior in closed-loop. Key MATLAB commands used in are: bode , nyquist , margin , lsim , step , feedback , sisotool Contents Gain and Phase Margin Nyquist Diagram The Cauchy Criterion Closed-Loop Performance from Bode Plots Closed-Loop Stability from the Nyquist Diagram Gain and Phase Margin Consider the following unity feedback system: this tutorial

where is a variable (constant) gain and is the plant under consideration. The gain margin is defined as the change in open-loop gain required to make the system unstable. Systems with greater gain margins can withstand greater changes in system parameters before becoming unstable in closed-loop.

The phase margin is defined as the change in open-loop phase shift required to make a closed-loop system unstable. The phase margin also measures the system's tolerance to time delay. If there is a time delay greater than in the loop (where is the frequency where the phase shift is 180 deg), the system will become unstable in closed-loop. The time delay, can be thought of as an extra block in the forward path of the block diagram that adds phase to the system but has no effect on the gain. That is, a time delay can be represented as a block with magnitude of 1 and phase (in radians/second). For now, we won't worry about where all this comes from and will concentrate on identifying the gain and phase margins on a Bode plot. The phase margin is the difference in phase between the phase curve and -180 degrees at the point corresponding to the frequency that gives us a gain of 0 dB (the gain crossover frequency, ). Likewise, the gain margin is the difference between the magnitude curve and 0 dB at the point corresponding to the frequency that gives us a phase of -180 degrees (the phase crossover frequency, ).

One nice thing about the phase margin is that you don't need to replot the Bode in order to find the new phase margin when changing the gains. If you recall, adding gain only shifts the magnitude plot up. This is equivalent to changing the y-axis on the magnitude plot. Finding the phase margin is simply a matter of finding the new cross-over frequency and reading off the phase margin. For example, suppose you entered the command bode(sys). You will get the following bode plot:
s = tf('s'); sys = 50/(s^3 + 9*s^2 + 30*s +40); bode(sys) grid on title('Bode Plot with No Gain')

You should see that the phase margin is about 100 degrees. Now suppose you added a gain of 100, by entering the command bode(100*sys). You should get the following plot:
bode(100*sys) grid on title('Bode Plot with Gain = 100')

As you can see the phase plot is exactly the same as before, and the magnitude plot is shifted up by 40 dB (gain of 100). The phase margin is now about -60 degrees. This same result could be achieved if the y-axis of the magnitude plot was shifted down 40 dB. Try this, look at the first Bode plot, find where the curve

crosses the -40 dB line, and read off the phase margin. It should be about 90 degrees, the same as the second Bode plot. We can have MATLAB calculate and display the gain and phase margins using the margin(sys) command. This command returns the gain and phase margins, the gain and phase cross over frequencies, and a graphical representation of these on the Bode plot. Let's check it out: margin(100*sys)

Bandwidth Frequency The bandwidth frequency is defined as the frequency at which the closed-loop magnitude response is equal to -3 dB. However, when we design via frequency response, we are interested in predicting the closed-loop behavior from the open-loop response. Therefore, we will use a second-order system approximation and say that the bandwidth frequency equals the frequency at which the open-loop magnitude response is between -6 and -7.5 dB, assuming the open-loop phase response is between -135 deg and -225 deg. For a complete derivation of this approximation, consult your textbook. In order to illustrate the importance of the bandwidth frequency, we will show how the output changes with different input frequencies. We will find that sinusoidal inputs with frequency less than Wbw (the bandwidth frequency) are tracked "reasonably well" by the system. Sinusoidal inputs with frequency greater than Wbw are attenuated (in magnitude) by a factor of 0.707 or greater (and are also shifted in phase). Let's say we have the following closed-loop transfer function representing a system:

(1)
sys = 1/(s^2 + 0.5*s + 1); bode(sys)

Since this is the closed-loop transfer function, our bandwidth frequency will be the frequency corresponding to a gain of -3 dB. Looking at the plot, we find that it is approximately 1.4 rad/s. We can also read off the plot that for an input frequency of 0.3 radians, the output sinusoid should have a magnitude about one and the phase should be shifted by perhaps a few degrees (behind the input). For an input frequency of 3 rad/sec, the output magnitude should be about -20 dB (or 1/10 as large as the input) and the phase should be nearly -180 (almost exactly out-of-phase). We can use the lsim command to simulate the response of the system to sinusoidal inputs. First, consider a sinusoidal input with a frequency lower than Wbw. We must also keep in mind that we want to view the steady state response. Therefore, we will modify the axes in order to see the steady state response clearly (ignoring the transient response).
sys = 1/(s^2 + 0.5*s + 1); w = 0.3; t = 0:0.1:100; u = sin(w*t); [y,t] = lsim(sys,u,t); plot(t,y,t,u) axis([50 100 -2 2])

Note that the output (blue) tracks the input (green) fairly well; it is perhaps a few degrees behind the input as expected. However, if we set the frequency of the input higher than the bandwidth frequency for the system, we get a very distorted response (with respect to the input):
sys = 1/(s^2 + 0.5*s + 1); w = 3; t = 0:0.1:100; u = sin(w*t); [y,t] = lsim(sys,u,t); plot(t,y,t,u) axis([90 100 -1 1])

Again, note that the magnitude is about 1/10 that of the input, as predicted, and that it is almost exactly out of phase (180 degrees behind) the input. Feel free to experiment and view the response for several different frequencies , and see if they match the Bode plot. Nyquist Diagram

The Nyquist plot allows us to predict the stability and performance of a closed-loop system by observing its open-loop behavior. The Nyquist criterion can be used for design purposes regardless of open-loop stability (remember that the Bode design methods assume that the system is stable in open-loop). Therefore, we use this criterion to determine closed-loop stability when the Bode plots display confusing information. Note: The MATLAB nyquist command does not provide an adequate representation for systems that have open-loop poles in the jw-axis. Therefore, we suggest that you copy the nyquist1.m file as a new mfile. This m-filecreates more accurate Nyquist plots, since it correctly deals with poles and zeros on the jw-axis. The Nyquist diagram is basically a plot of where is the open-loop transfer function and is a vector of frequencies which encloses the entire right-half plane. In drawing the Nyquist diagram, both positive and negative frequencies (from zero to infinity) are taken into account. We will represent positive frequencies in red and negative frequencies in green. The frequency vector used in plotting the Nyquist diagram usually looks like this (if you can imagine the plot stretching out to infinity):

However, if we have open-loop poles or zeros on the jw axis, will not be defined at those points, and we must loop around them when we are plotting the contour. Such a contour would look as follows:

Please note that the contour loops around the pole on the jw axis. As we mentioned before, the MATLAB nyquistcommand does not take poles or zeros on the jw axis into account and therefore produces an incorrect plot. To correct this, please download and use nyquist1.m. If we have a pole on the jw axis, we have to use nyquist1. If there are no poles or zeros on the jw-axis, or if we have pole-zero cancellation, we can use either the nyquist command ornyquist1.m. The Cauchy Criterion The Cauchy criterion (from complex analysis) states that when taking a closed contour in the complex plane, and mapping it through a complex function , the number of times that the plot of encircles the origin is equal to the number of zeros of enclosed by the frequency contour minus the number of

poles of enclosed by the frequency contour. Encirclements of the origin are counted as positive if they are in the same direction as the original closed contour or negative if they are in the opposite direction. When studying feedback controls, we are not as interested in (2) If encircles the origin, then will enclose the point -1. Since we are interested in the closedloop stability, we want to know if there are any closed-loop poles (zeros of ) in the right-half plane. More details on how to determine this will come later. Therefore, the behavior of the Nyquist diagram around the -1 point in the real axis is very important; however, the axis on the standard nyquist diagram might make it hard to see what's happening around this point. To correct this, you can add the lnyquist.m function to your files. The lnyquist.m command plots the Nyquist diagram using a logarithmic scale and preserves the characteristics of the -1 point. To view a simple Nyquist plot using MATLAB, we will define the following transfer function and view the Nyquist plot: (3)
s = tf('s'); sys = 0.5/(s - 0.5); nyquist(sys) axis([-1 0 -1 1])

as in the closed-loop transfer function:

Now we will look at the Nyquist diagram for the following transfer function: (4) Note that this function has a pole at the origin. We will see the difference between using the nyquist, nyquist1, andlnyquist commands with this particular function.

sys = (s + 2)/(s^2); nyquist(sys)

nyquist1(sys)

lnyquist(sys)

Note that the nyquist plot is not the correct one, the nyquist1 plot is correct, but it's hard to see what happens close to the -1 point, and the lnyquist plot is correct and has an appropriate scale. Closed-Loop Performance from Bode Plots In order to predict closed-loop performance from open-loop frequency response, we need to have several concepts clear: The system must be stable in open-loop if we are going to design via Bode plots. If the gain crossover frequency is less than the phase crossover frequency (i.e. loop system will be stable. ), then the closed-

For second-order systems, the closed-loop damping ratio is approximately equal to the phase margin divided by 100 if the phase margin is between 0 and 60 degrees. We can use this concept with caution if the phase margin is greater than 60 degrees. For second-order systems, a relationship between damping ratio, bandwidth frequency, and settling time is given by an equation described on the Extras: Bandwidth page. A very rough estimate that you can use is that the bandwidth is approximately equal to the natural frequency. Let's use these concepts to design a controller for the following system:

Where

is the controller and

is: (5)

The design must meet the following specifications: Zero steady state error. Maximum overshoot must be less than 40%. Settling time must be less than 2 seconds. There are two ways of solving this problem: one is graphical and the other is numerical. Within MATLAB, the graphical approach is best, so that is the approach we will use. First, let's look at the Bode plot. Create a m-file with the following code:
sys = 10/(1.25*s + 1); bode(sys)

There are several characteristics of the system that can be read directly from this Bode plot. First of all, we can see that the bandwidth frequency is around 10 rad/sec. Since the bandwidth frequency is roughly the same as the natural frequency (for a first order system of this type), the rise time is 1.8/BW = 1.8/10 = 1.8 seconds. This is a rough estimate, so we will say the rise time is about 2 seconds. The phase margin for this system is approximately 95 degrees. The relation damping ratio = PM/100 only holds forPM < 60. Since the system is first-order, there should be no overshoot. The last major point of interest is steady-state error. The steady-state error can be read directly off the Bode plot as well. The constant ( , , or ) is found from the intersection of the low frequency asymptote

with the w = 1 line. Just extend the low frequency line to the w = 1 line. The magnitude at this point is the constant. Since the Bode plot of this system is a horizontal line at low frequencies (slope = 0), we know this system is of type zero. Therefore, the intersection is easy to find. The gain is 20 dB (magnitude 10). What this means is that the constant for the error function is 10. The steady-state error is 1/(1+Kp) = 1/(1+10) = 0.091. If our system was type one instead of type zero, the constant for the steady-state error would be found in a manner similar to the following.

Let's check our predictions by looking at a step response plot. This can be done by adding the following two lines of code into the MATLAB command window.
sys_cl = feedback(sys,1); step(sys_cl) title('Closed-Loop Step Response, No Controller')

As you can see, our predictions were very good. The system has a rise time of about 2 seconds, has no overshoot, and has a steady-state error of about 9%. Now we need to choose a controller that will allow us to meet the design criteria. We choose a PI controller because it will yield zero steady-state error for a step input. Also, the PI controller has a zero, which we can place. This gives us additional design flexibility to help us meet our criteria. Recall that a PI controller is given by: (6) The first thing we need to find is the damping ratio corresponding to a percent overshoot of 40%. Plugging in this value into the equation relating overshoot and damping ratio (or consulting a plot of this relation), we find that the damping ratio corresponding to this overshoot is approximately 0.28. Therefore, our phase margin should be at least 30 degrees. We must have a bandwidth frequency greater than or equal to 12 if we want our settling time to be less than 1.75 seconds which meets the design specs. Now that we know our desired phase margin and bandwidth frequency, we can start our design. Remember that we are looking at the open-loop Bode plots. Therefore, our bandwidth frequency will be the frequency corresponding to a gain of approximately -7 dB. Let's see how the integrator portion of the PI or affects our response. Change your m-file to look like the following (this adds an integral term but no proportional term):
plant = 10/(1.25*s + 1); contr = 1/s; bode(contr*plant, logspace(0,2))

Our phase margin and bandwidth frequency are too small. We will add gain and phase with a zero. Let's place the zero at 1 for now and see what happens. Change your m-file to look like the following:
plant = 10/(1.25*s + 1); contr = (s + 1)/s; bode(contr*plant, logspace(0,2))

It turns out that the zero at 1 with a unit gain gives us a satisfactory answer. Our phase margin is greater than 60 degrees (even less overshoot than expected) and our bandwidth frequency is approximately 11 rad/s, which will give us a satisfactory response. Although satisfactory, the response is not quite as good as we would like. Therefore, let's try to get a higher bandwidth frequency without changing the phase margin too much. Let's try to increase the gain to 5 and see what happens. This will make the gain shift and the phase will remain the same.
plant = 10/(1.25*s + 1); contr = 5 * ((s + 1)/s); bode(contr*plant, logspace(0,2))

That looks really good. Let's look at our step response and verify our results. Add the following two lines to your m-file:
sys_cl = feedback(contr*plant,1); step(sys_cl)

As you can see, our response is better than we had hoped for. However, we are not always quite as lucky and usually have to play around with the gain and the position of the poles and/or zeros in order to achieve our design requirements. Closed-Loop Stability from the Nyquist Diagram Consider the negative feedback system:

Remember from the Cauchy criterion that the number N of times that the plot of G(s)H(s) encircles -1 is equal to the number Z of zeros of 1 + G(s)H(s) enclosed by the frequency contour minus the number P of poles of 1 + G(s)H(s) enclosed by the frequency contour (N = Z - P). Keeping careful track of open- and closed-loop transfer functions, as well as numerators and denominators, you should convince yourself that: The zeros of 1 + G(s)H(s) are the poles of the closed-loop transfer function. The poles of 1 + G(s)H(s) are the poles of the open-loop transfer function. The Nyquist criterion then states that: P = the number of open-loop (unstable) poles of G(s)H(s).

N = the number of times the Nyquist diagram encircles -1. clockwise encirclements of -1 count as positive encirclements. counter-clockwise encirclements of -1 count as negative encirclements. Z = the number of right-half-plane (positive, real) poles of the closed-loop system. The important equation whih relates these three quantities is: (7) Note: This is only one convention for the Nyquist criterion. Another convention states that a positive N counts the counter-clockwise or anti-clockwise encirclements of -1. The P and Z variables remain the same. In this case the equation becomes Z = P - N. Throughout these tutorials, we will use a positive sign for clockwise encirclements. It is very important (and somewhat tricky) to learn how to count the number of times that the diagram encircles -1. Therefore, we will go into some detail to help you visualize this. You can view this movie as an example. Another way of looking at it is to imagine you are standing on top of the -1 point and are following the diagram from beginning to end. Now ask yourself: How many times did I turn my head a full 360 degrees? Again, if the motion was clockwise, N is positive, and if the motion is anti-clockwise, N is negative. Knowing the number of right-half plane (unstable) poles in open loop (P), and the number of encirclements of -1 made by the Nyquist diagram (N), we can determine the closed-loop stability of the system. If Z = P + N is a positive, nonzero number, the closed-loop system is unstable. We can also use the Nyquist diagram to find the range of gains for a closed-loop unity feedback system to be stable. The system we will test looks like this:

where G(s) is: (8) This system has a gain K which can be varied in order to modify the response of the closed-loop system. However, we will see that we can only vary this gain within certain limits, since we have to make sure that our closed-loop system will be stable. This is what we will be looking for: the range of gains that will make this system stable in the closed-loop. The first thing we need to do is find the number of positive real poles in our open-loop transfer function:

roots([1 -8 15]) ans = 5 3

The poles of the open-loop transfer function are both positive. Therefore, we need two anti-clockwise (N = -2) encirclements of the Nyquist diagram in order to have a stable closed-loop system (Z = P + N). If the number of encirclements is less than two or the encirclements are not anti-clockwise, our system will be unstable. Let's look at our Nyquist diagram for a gain of 1:
sys = (s^2 + 10*s + 24)/(s^2 - 8*s + 15); nyquist(sys)

There are two anti-clockwise encirclements of -1. Therefore, the system is stable for a gain of 1. Now we will see how the system behaves if we increase the gain to 20:
nyquist(20*sys)

The diagram expanded. Therefore, we know that the system will be stable no matter how much we increase the gain. However, if we decrease the gain, the diagram will contract and the system might become unstable. Let's see what happens for a gain of 0.5: nyquist(0.5*sys)

The system is now unstable. By trial and error we find that this system will become unstable for gains less than 0.80. We can verify our answers by zooming in on the Nyquist plots as well as by looking at the closed-loop steps responses for gains of 0.79, 0.80, and 0.81. Gain Margin We already defined the gain margin as the change in open-loop gain expressed in decibels (dB), required at 180 degrees of phase shift to make the system unstable. Now we are going to find out where this comes from. First of all, let's say that we have a system that is stable if there are no Nyquist encirclements of -1, such as: (9) Looking at the roots, we find that we have no open loop poles in the right half plane and therefore no closed-loop poles in the right-half-plane if there are no Nyquist encirclements of -1. Now, how much can we vary the gain before this system becomes unstable in closed-loop? Let's look at the following figure:

The open-loop system represented by this plot will become unstable in closed loop if the gain is increased past a certain boundary. The negative real axis area between -1/a (defined as the point where the 180

degree phase shift occurs...that is, where the diagram crosses the real axis) and -1 represents the amount of increase in gain that can be tolerated before closed-loop instability. If we think about it, we realize that if the gain is equal to a, the diagram will touch the -1 point: (10) or (11) Therefore, we say that the gain margin is a units. However, we mentioned before that the gain margin is usually measured in decibels. Hence, the gain margin is: (12) We will now find the gain margin of the stable, open-loop transfer function we viewed before. Recall that the function is: (13) and that the Nyquist diagram can be viewed by typing:
sys = 50/(s^3 + 9*s^2 + 30*s + 20); nyquist(sys)

As we discussed before, all that we need to do to find the gain margin is find a, as defined in the preceding figure. To do this, we need to find the point where there is exactly 180 degrees of phase shift. This means that the transfer function at this point is real (has no imaginary part). The numerator is already real, so we

just need to look at the denominator. Whens = jw, the only terms in the denominator that will have imaginary parts are those which are odd powers of s. Therefore, for G(jw) to be real, we must have: (14) which means w = 0 (this is the rightmost point in the Nyquist diagram) or w = sqrt(30). We can then find the value ofG(jw) at this point using polyval:
w = sqrt(30); polyval(50,j*w)/polyval([1 9 30 40],j*w) ans = -0.2174

The answer is: -0.2174 + 0i. The imaginary part is zero, so we know that our answer is correct. We can also verify by looking at the Nyquist plot again. The real part also makes sense. Now we can proceed to find the gain margin. We found that the 180 degrees phase shift occurs at -0.2174 + 0i. This point was previously defined as -1/a. Therefore, we now have a, which is the gain margin. However, we need to express the gain margin in decibels: (15) (16) (17) We now have our gain margin. Let's see how accurate it is by using a gain of a = 4.6 and zooming in on the Nyquist plot:
a = 4.6; nyquist(a*sys)

The plot appears to go right through the -1 point. We will now verify the accuracy of our results by viewing the zoomed Nyquist diagrams and step responses for gains of 4.5, 4.6, and 4.7.

Phase Margin We have already discussed the importance of the phase margin. Therefore, we will only talk about where this concept comes from. We have defined the phase margin as the change in open-loop phase shift required at unity gain to make a closed-loop system unstable. Let's look at the following graphical definition of this concept to get a better idea of what we are talking about.

Let's analyze the previous plot and think about what is happening. From our previous example we know that this particular system will be unstable in closed-loop if the Nyquist diagram encircles the -1 point. However, we must also realize that if the diagram is shifted by theta degrees, it will then touch the -1 point at the negative real axis, making the system marginally stable in closed-loop. Therefore, the angle required to make this system marginally stable in closed-loop is called the phase margin (measured in degrees). In order to find the point we measure this angle from, we draw a circle with radius of 1, find the point in the Nyquist diagram with a magnitude of 1 (gain of zero dB), and measure the phase shift needed for this point to be at an angle of 180 degrees.

Introduction: State-Space Methods for Controller Design


In this section, we will show how to design controllers and observers using state-space (or time-domain) methods. Key MATLAB commands used in this tutorial are: eig , ss , lsim , place , acker Contents Modeling Stability Controllability and Observability Control Design Using Pole Placement Introducing the Reference Input Observer Design

Modeling There are several different ways to describe a system of linear differential equations. The state-space representationwas introduced in the Introduction: System Modeling section. For a SISO LTI system, the state-space form is given below: (1) (2) where x is a n by 1 vector representing the state (commonly position and velocity variable in mechanical systems), u is a scalar representing the input (commonly a force or torque in mechanical systems), and y is a scalar representing the output. The matrices A (n by n), B (n by 1), and C (1 by n) determine the relationships between the state and input and output variables. Note that there are n first-order differential equations. State space representation can also be used for systems with multiple inputs and outputs (MIMO), but we will only use single-input, single-output (SISO) systems in these tutorials. To introduce the state space design method, we will use the magnetically suspended ball as an example. The current through the coils induces a magnetic force which can balance the force of gravity and cause the ball (which is made of a magnetic material) to be suspended in midair. The modeling of this system has been established in many control text books (including Automatic Control Systems by B. C. Kuo, the seventh edition).

The equations for the system are given by: (3) (4) where h is the vertical position of the ball, i is the current through the electromagnet, V is the applied voltage, M is the mass of the ball, g is gravity, L is the inductance, R is the resistance, and K is a coefficient

that determines the magnetic force exerted on the ball. For simplicity, we will choose values M = 0.05 Kg, K = 0.0001, L = 0.01 H, R = 1 Ohm,g = 9.81 m/sec^2. The system is at equilibrium (the ball is suspended in midair) whenever h = K i^2/Mg (at which point dh/dt = 0). We linearize the equations about the point h = 0.01 m (where the nominal current is about 7 amp) and get the state space equations: (5) (6) where:

(7) is the set of state variables for the system (a 3x1 vector), u is the input voltage (delta V), and y (the output), is delta h. Enter the system matricies into a m-file.
A = [ 0 980 0 B = [ 0 0 100 ]; C = [ 1 0 0 ]; 1 0 0 0 -2.8 -100 ];

Stability One of the first things we want to do is analyze whether the open-loop system (without any control) is stable. As discussed in the Introduction: System Analysis section, the eigenvalues of the system matrix, A, (equivalent to the poles of the transfer fucntion) determine the stability. The eigenvalues of the A matrix are the values of s where det(sI - A) = 0.
poles = eig(A) poles = 31.3050 -31.3050 -100.0000

One of the poles is in the right-half plane, (i.e. has positive real part which means that the system is unstable in open-loop. To check out what happens to this unstable system when there is a nonzero initial condition, add the following lines to yourm-file and it again:

t = 0:0.01:2; u = zeros(size(t)); x0 = [0.01 0 0]; sys = ss(A,B,C,0); [y,t,x] = lsim(sys,u,t,x0); plot(t,y) title('Open-Loop Response to Non-Zero Initial Condition') xlabel('Time (sec)') ylabel('Ball Position (m)')

It looks like the distance between the ball and the electromagnet will go to infinity, but probably the ball hits the table or the floor first (and also probably goes out of the range where our linearization is valid). Controllability and Observability A system is controllable if there exists a control input, u(t), that transfers any state of the system to zero in finite time. It can be shown that an LTI system is controllable if and only if its controllabilty matrix, CO, has full rank (i.e. if rank(CO) = n where n is the number of states ). The rank of the controllability matrix of an LTI model can be determined in MATLAB using the commands rank(ctrb(A,B)) or rank(ctrb(sys)). (8) All the state variables of a system may not be directly measurable, for instance if the component is in an inaccessible location. In these cases it is neccesary to estimate the values of the unknown internal state variables using only the available system outputs. A system is observable if the initial state, x(t_0), can be

determined from the system output, y(t), over some finite time t_0 < t < t_f. For LTI systems, the system is observable if and only if the observability matrix, OB, has full rank (i.e. if rank(OB) = n where n is the number of states). The observability of an LTI model can be determined in MATLAB using the command rank(obsv(A,C)) or rank(obsv(sys)).

(9) Controllability and observability are dual concepts. A system (A,B) is controllable if and only if a system (A',C,B',D) is observable. This fact will be useful when designing an observer, as we shall see below. Control Design Using Pole Placement Let's build a controller for this system using pole placement. The schematic of a full-state feedback system is shown below. By full-state, we mean that all state variables are known to the controller at all times. For instance in this system, we would need a sensor measuring the ball position, another measuring velocity, and a third measuring current in the electro-magnet.

For simplicity, let's assume the reference is zero, R=0. The input is then (10) The state-space equations for the closed-loop feedback system are therefore (11) (12) The stability and time domain performance of the closed-loop feedback system are determined primarily by the location of the poles (eigenvalues) of the matrix (A-BK). Since the matrices A and B*K are both 3 by 3 matrices, there will be 3 poles for the system. By choosing an appropriate K matrix we can place these closed-loop poles anywhere we want. We can use the MATLAB function place to find the control matrix, K, which will give the desired poles. Before attempting this method, we have to decide where we want the closed-loop poles to be. Suppose the criteria for the controller were settling time < 0.5 sec and overshoot < 5%, then we might try to place the

two dominant poles at -10 +/- 10i (at zeta = 0.7 or 45 degrees with sigma = 10 > 4.6*2). The third pole we might place at -50 to start, and we can change it later depending on what the closed-loop behavior is. Remove the lsim command from your m-file and everything after it, then add the following lines to your m-file:
p1 = -10 + 10i; p2 = -10 - 10i; p3 = -50; K = place(A,B,[p1 p2 p3]); sys_cl = ss(A-B*K,B,C,0); lsim(sys_cl,u,t,x0); xlabel('Time (sec)') ylabel('Ball Position (m)')

The overshoot is too large (there are also zeros in the transfer function which can increase the overshoot; you do not see the zeros in the state-space formulation). Try placing the poles further to the left to see if the transient response improves (this should also make the response faster).
p1 = -20 + 20i; p2 = -20 - 20i; p3 = -100; K = place(A,B,[p1 p2 p3]); sys_cl = ss(A-B*K,B,C,0);

lsim(sys_cl,u,t,x0); xlabel('Time (sec)')

ylabel('Ball Position (m)')

This time the overshoot is smaller. Consult your textbook for further suggestions on choosing the desired closed-loop poles. Compare the control effort required (K) in both cases. In general, the farther you move the poles, the more control effort it takes. Note: If you want to place two or more poles at the same position, place will not work. You can use a function calledacker which works similarly to place: K = acker(A,B,[p1 p2 p3]) Introducing the Reference Input Now, we will take the control system as defined above and apply a step input (we choose a small value for the step, so we remain in the region where our linearization is valid). Replace t,|u|, and lsim in your mfile with the following:
t = 0:0.01:2; u = 0.001*ones(size(t)); sys_cl = ss(A-B*K,B,C,0); lsim(sys_cl,u,t); xlabel('Time (sec)') ylabel('Ball Position (m)') axis([0 2 -4E-6 0])

The system does not track the step well at all; not only is the magnitude not one, but it is negative instead of positive! Recall the schematic above, we don't compare the output to the reference; instead we measure all the states, multiply by the gain vector K, and then subtract this result from the reference. There is no reason to expect that K*x will be equal to the desired output. To eliminate this problem, we can scale the reference input to make it equal to K*x steadystate. This scale factor is often called Nbar; it is introduced as shown in the following schematic:

We can get Nbar from MATLAB by using the function rscale (place the following line of code after K = ...).
Nbar = rscale(sys,K) Nbar = -285.7143

Note that this function is not standard in MATLAB. You will need download it here, rscale.m, and save it to your current workspace. Now, if we want to find the response of the system under state feedback with this introduction of the reference, we simply note the fact that the input is multiplied by this new factor, Nbar:

lsim(sys_cl,Nbar*u,t) title('Linear Simulation Results (with Nbar)') xlabel('Time (sec)') ylabel('Ball Position (m)') axis([0 2 0 1.2*10^-3])

and now a step can be tracked reasonably well. Observer Design When we can't measure all the states x (often the case in practice), we can build an observer to estimate them, while measuring only the output y = C x. For the magnetic ball example, we will add three new, estimated states to the system. The schematic is as follows:

The observer is basically a copy of the plant; it has the same input and almost the same differential equation. An extra term compares the actual measured output y to the estimated output y_hat; this will

cause the estimated states \hat{x} to approach the values of the actual states x. The error dynamics of the observer are given by the poles of (A-LC). First, we need to choose the observer gain L. Since we want the dynamics of the observer to be much faster than the system itself, we need to place the poles at least five times farther to the left than the dominant poles of the system. If we want to use place, we need to put the three observer poles at different locations.
op1 = -100; op2 = -101; op3 = -102;

Because of the duality between controllability and observability, we can use the same technique used to find the control matrix, but replacing the matrix B by the matrix C and taking the transposes of each matrix
L = place(A',C',[op1 op2 op3])';

The equations in the block diagram above are given for \hat{x}. It is conventional to write the combined equations for the system plus observer using the original state x plus the error state: e = x - \hat{x}. We use as state feedback u = -K \hat{x}. After a little bit of algebra (consult your textbook for more details), we arrive at the combined state and error equations with the full-state feedback and an observer.
At = [ A-B*K zeros(size(A)) Bt = [ B*Nbar zeros(size(B)) ]; Ct = [ C zeros(size(C)) ]; B*K A-L*C ];

To see how the response looks to a nonzero initial condition with no reference input, add the following lines into your m-file. We typically assume that the observer begins with zero initial condition, \hat{x} = 0. This gives us that the initial condition for the error is equal to the initial condition of the state.
sys = ss(At,Bt,Ct,0); lsim(sys,zeros(size(t)),t,[x0 x0]); title('Linear Simulation Results (with observer)') xlabel('Time (sec)') ylabel('Ball Position (m)')

Responses of all the states are plotted below. Recall that lsim gives us x and e; to get \hat{x}, we need to compute x - e.
t = 0:1E-6:0.1; x0 = [0.01 0.5 -5]; [y,t,x] = lsim(sys,zeros(size(t)),t,[x0 x0]); n = 3; e = x(:,n+1:end); x = x(:,1:n); x_est = x - e; % Save state variables explicitly to aid in plotting h = x(:,1); h_dot = x(:,2); i = x(:,3); h_est = x_est(:,1); h_dot_est = x_est(:,2); i_est = x_est(:,3); plot(t,h,'-r',t,h_est,':r',t,h_dot,'-b',t,h_dot_est,':b',t,i,'-g',t,i_est,':g') xlabel('Time (sec)')

We can see that the observer estimates the states quickly and tracks the states well in the steady-state. Introduction: Digital Controller Design In this section we will discuss converting continuous time models into discrete time (or difference equation) models. We will also introduce the z-transform and show how to use it to analyze and design controllers for discrete time systems. Key MATLAB commands used in this tutorial are: c2d , pzmap , zgrid , step , rlocus Contents Introduction Zero-Hold Equivalence Conversion Using c2d Example: Mass-Spring-Damper Stability and Transient Response Discrete Root Locus Introduction The figure below shows the typical continuous feedback system that we have been considering so far in this tutorial. Almost all of the continuous controllers can be built using analog electronics.

The continuous controller, enclosed in the dashed square, can be replaced by a digital controller, shown below, that performs same control task as the continuous controller. The basic difference between these controllers is that the digital system operates on discrete signals (or samples of the sensed signal) rather than on continuous signals.

Different types of signals in the above digital schematic can be represented by the following plots.

The purpose of this Digital Control Tutorial is to show you how to use MATLAB to work with discrete functions either in transfer function or state-space form to design digital control systems. Zero-Hold Equivalence In the above schematic of the digital control system, we see that the digital control system contains both discrete and the continuous portions. When designing a digital control system, we need to find the discrete equivalent of the continuous portion so that we only need to deal with discrete functions. For this technique, we will consider the following portion of the digital control system and rearrange as follows.

The clock connected to the D/A and A/D converters supplies a pulse every T seconds and each D/A and A/D sends a signal only when the pulse arrives. The purpose of having this pulse is to require that Hzoh(z) have only samples u(k) to work on and produce only samples of output y(k); thus, Hzoh(z) can be realized as a discrete function.

The philosophy of the design is the following. We want to find a discrete function Hzoh(z) so that for a piecewise constant input to the continuous system H(s), the sampled output of the continuous system equals the discrete output. Suppose the signal u(k) represents a sample of the input signal. There are techniques for taking this sample u(k) and holding it to produce a continuous signal uhat(t). The sketch below shows that the uhat(t) is held constant at u(k) over the interval kT to (k+1)T. This operation of holding uhat(t) constant over the sampling time is called zero-order hold.

The zero-order held signal uhat(t) goes through H2(s) and A/D to produce the output y(k) that will be the piecewise same signal as if the discrete signal u(k) goes through Hzoh(z) to produce the discrete output y(k).

Now we will redraw the schematic, placing Hzoh(z) in place of the continuous portion.

By placing Hzoh(z), we can design digital control systems dealing with only discrete functions. Note: There are certain cases where the discrete response does not match the continuous response due to a hold circuit implemented in digital control systems. For information, see Lagging effect associated with the hold. Conversion Using c2d

There is a MATLAB function called c2d that converts a given continuous system (either in transfer function or state-space form) to a discrete system using the zero-order hold operation explained above. The basic command for this in MATLAB issys_d = c2d(sys,Ts,'zoh') The sampling time (Ts in sec/sample) should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency. Example: Mass-Spring-Damper Transfer Function Suppose you have the following continuous transfer function (1) Assuming the closed-loop bandwidth frequency is greater than 1 rad/sec, we will choose the sampling time (Ts) equal to 1/100 sec. Now, create an new m-file and enter the following commands.
M = 1; b = 10; k = 20; s = tf('s'); sys = 1/(M*s^2+b*s+k); Ts = 1/100; sys_d = c2d(sys,Ts,'zoh') sys_d = 4.837e-05 z + 4.678e-05 ----------------------z^2 - 1.903 z + 0.9048

Sample time: 0.01 seconds Discrete-time transfer function.

State-Space The continuous time state-space model is as follows: (2) (3)

All constants are the same as before. The following m-file converts the above continuous state-space to discrete state-space.
A = [0 -k/M B = [ 0; 1; -b/M];

1/M]; C = [1 0]; D = [0]; Ts = 1/100; sys = ss(A,B,C,D); sys_d = c2d(sys,Ts,'zoh') sys_d = a = x1 x1 x2 b = u1 x1 x2 c = x1 y1 d = u1 y1 0 1 x2 0 4.837e-05 0.009513 0.999 -0.1903 x2 0.009513 0.9039

Sample time: 0.01 seconds Discrete-time state-space model.

From these matrices, the discrete state-space can be written as (4) (5) Now you have the discrete time state-space model.

Stability and Transient Response For continuous systems, we know that certain behaviors results from different pole locations in the s-plane. For instance, a system is unstable when any pole is located to the right of the imaginary axis. For discrete systems, we can analyze the system behaviors from different pole locations in the z-plane. The characteristics in the z-plane can be related to those in the s-plane by the expression (6) T = Sampling time (sec/sample) s = Location in the s-plane z = Location in the z-plane The figure below shows the mapping of lines of constant damping ratio (zeta) and natural frequency (Wn) from the s-plane to the z-plane using the expression shown above.

If you noticed in the z-plane, the stability boundary is no longer imaginary axis, but is the unit circle z=1. The system is stable when all poles are located inside the unit circle and unstable when any pole is located outside. For analyzing the transient response from pole locations in the z-plane, the following three equations used in continuous system designs are still applicable. (7) (8)

(9) where, zeta = Damping ratio Wn = Natural frequency (rad/sec) Ts = Settling time Tr = Rise time Mp = Maximum overshoot Important: The natural frequency (Wn) in z-plane has the unit of rad/sample, but when you use the equations shown above, the Wn must be in the unit of rad/sec. Suppose we have the following discrete transfer function (10) Create an new m-file and enter the following commands. Running this m-file in the command window gives you the following plot with the lines of constant damping ratio and natural frequency.
numDz = 1; denDz = [1 -0.3 0.5]; sys = tf(numDz,denDz,-1); % the -1 indicates that the sample time is undetermined pzmap(sys) axis([-1 1 -1 1]) zgrid

From this plot, we see poles are located approximately at the natural frequency of 9pi/20T (rad/sample) and the damping ratio of 0.25. Assuming that we have a sampling time of 1/20 sec (which leads to Wn = 28.2 rad/sec) and using three equations shown above, we can determine that this system should have the rise time of 0.06 sec, a settling time of 0.65 sec and a maximum overshoot of 45% (0.45 more than the steady-state value). Let's obtain the step response and see if these are correct. Add the following commands to the above m-file and rerun it in the command window. You should get the following step response.
sys = tf(numDz,denDz,1/20); step(sys,2.5);

As you can see from the plot, the rise time, settling time and overshoot came out to be what we expected. This shows how you can use the locations of poles and the above three equations to analyze the transient response of the system. Discrete Root Locus The root-locus is the locus of points where roots of characteristic equation can be found as a single gain is varied from zero to infinity. The characteristic equation of an unity feedback system is (11) where G(z) is the compensator implemented in the digital controller and Hzoh(z) is the plant transfer function in z. The mechanics of drawing the root-loci are exactly the same in the z-plane as in the s-plane. Recall from the continuous Root-Locus Tutorial, we used the MATLAB function called sgrid to find the root-locus region that gives an acceptable gain (K). For the discrete root-locus analysis, we will use the function zgrid that has the same characteristics as sgrid. The command zgrid(zeta, Wn) draws lines of constant damping ratio (zeta) and natural frequency (Wn).

Suppose we have the following discrete transfer function (12) and the requirements are a damping ratio greater than 0.6 and a natural frequency greater than 0.4 rad/sample (these can be found from design requirements, sampling time (sec/sample) and three equations shown in the previous section). The following commands draw the root-locus with the lines of constant damping ratio and natural frequency. Create an new m-file and enter the following commands. Running this m-file should give you the following root-locus plot.
numDz = [1 -0.3]; denDz = [1 -1.6 0.7]; sys = tf(numDz,denDz,-1); rlocus(sys) axis([-1 1 -1 1]) zeta = 0.4; Wn = 0.3; zgrid(zeta,Wn)

From this plot, you should realize that the system is stable because all poles are located inside the unit circle. Also, you see two dotted lines of constant damping ratio and natural frequency. The natural frequency is greater than 0.3 outside the constant-Wn line, and the damping ratio is greater than 0.4 inside the constant-zeta line. In this example, we do have the root-locus drawn in the desired region. Therefore, a gain (K) chosen from one of the loci in the desired region should give you the response that satisfies design requirements.

Introduction: Simulink Modeling In Simulink, it is very straightforward to represent and then simulate a mathematical model representing a physical system. Models are represented graphically in Simulink as block diagrams. A wide array of blocks are available to the user in provided libraries for representing various phenomena and models in a range of formats. One of the primary advantages of employing Simulink (and simulation in general) for the analysis of dynamic systems is that it allows us to quickly analyze the response of complicated systems that may be prohibitively difficult to analyze analytically. Simulink is able to numerically approximate the solutions to mathematical models that we are unable to, or don't wish to, solve "by hand." In general, the mathematical equations representing a given system that serve as the basis for a Simulink model can be derived from physical laws. In this page we will demonstrate how to derive a mathematical model and then implement that model in Simulink. This model is then employed in the Introduction: Simulink Control page in order to demonstrate how to employ Simulink to design and simulate the control for a system. Contents Train system Free-body diagram and Newton's second law Constructing the Simulink model Running the model Train system In this example, we will consider a toy train consisting of an engine and a car. Assuming that the train only travels in one dimension (along the track), we want to apply control to the train so that it starts and comes to rest smoothly, and so that it can track a constant speed command with minimal error in steady state. The mass of the engine and the car will be represented by and , respectively. Furthermore, the engine and car are connected via a coupling with stiffness . In other words, the coupling is modeled as a spring with a spring constant . The force represents the force generated between the wheels of the engine and the track, while represents the coefficient of rolling friction.

Free-body diagram and Newton's second law The first step in deriving the mathematical equations that govern a physical system is to draw the free-body diagram(s) representing the system. This is done below for our train system.

From Newton's second law, we know that the sum of the forces acting on a body is equal to the product of the mass of the body and its acceleration. In this case, the forces acting on the engine in the horizontal direction are the spring force, the rolling resistance, and the force generated at the wheel/track interface. The forces acting on the train car in the horizontal direction are the spring force and the rolling resistance. In the vertical direction, the weight forces are balanced by the normal forces applied by the ground . Therefore, there will be no acceleration in the vertical direction. We will model the spring as generating a force that is linearly proportional to the deformation of the spring, , where and are the displacements of the engine and car, respectively. Here it is assumed that the spring is undeformed when and equal zero. The rolling resistance forces are modeled as being linearly proportional to the product of the corresponding velocities and normal forces (which are equal to the weight forces). Applying Newton's second law in the horizontal direction based on the above free-body diagrams leads to the following governing equations for the train system. (1) (2) Constructing the Simulink model This set of system equations can now be represented graphically without further manipulation. Specifically, we will construct two copies (one for each mass) of the general expression or . First, open Simulink and open a new model window. Then drag two Sum blocks (from the Math Operations library) into your model window and place them approximately as shown in the figure below.

The outputs of each of these Sum blocks represents the sum of the forces acting on each mass. Multiplying each output signal by will give us the corresponding acceleration of each mass. Now drag two Gain blocks (from the Math Operations Library) into your model and attach each one with a line from the output of one of the Sum blocks. Label these two signals as "Sum_F1" and "Sum_F2" in order to make your model more clear. This is accomplished by double-clicking in the space above each of the two signal lines and entering the desired label.

These Gain blocks should contain for each of the masses. We will define the variables M1 and M2 in the MATLAB workspace, so we can just enter the corresponding variable names in each of the Gain

blocks. Double-click on the upper Gain block and enter "1/M1" into the Gain field. Similarly, enter "1/M2" in the Gain field of the second Gain block. You will notice that the gains did not appear in the image of the Gain blocks, rather the blocks display a value of -K-. This is because the blocks are too small on the screen to show the full variable name inside the triangle. The blocks can be resized so that the actual gain value can be seen. To resize a block, select it by clicking on it once. Small squares will appear at the corners. Drag one of these squares to stretch the block. Your model should appear as below.

The outputs of these gain blocks are the accelerations of each of the masses (the train engine and car). The governing equations we derived above depend on the velocities and displacements of the masses. Since velocity can be determined by integrating acceleration, and position can be determined by integrating velocity, we can generate these signals employing integrator blocks. Drag a total of four Integrator blocks from the Continuous library into your model, two for each of our two accelerations. Connect these blocks and label the signals as shown below. Specifically, the first inegrator takes the acceleration of mass 1 ("x1_ddot") as an input and generates the velocity of mass 1 ("x1_dot"). The second integrator then takes this velocity and outputs the displacement of the first mass ("x1"). The same pattern holds for the integrators for the second mass.

Now, drag two Scopes from the Sinks library into your model and connect them to the outputs of these integrators. Label them "x1" and "x2".

Now we are ready to add the forces acting on each mass. First, we need to adjust the inputs on each Sum block to represent the proper number of forces (we will worry about the signs later). Since there is a total of three forces acting on mass 1, double-click on the corresponding Sum block and change the List of

signs field to "|+++". The symbol "|" serves as a spacer. There are only 2 forces acting on mass 2, therefore, we can leave that Sum block alone for now.

The first force acting on mass 1 is just the input force, . Drag a Signal Generator block from the Sources library and connect it to the uppermost input of the corresponding Sum block. Label this signal as "F".

The next force acting on mass 1 is the rolling resistance force. Recall that this force is modeled as follows. (3) To generate this force, we can tap off the velocity signal and multiply by an appropriate gain. Drag a Gain block into your model window. Tap off the "x1_dot" signal and connect it to the input of this new Gain block (draw this line in several steps if necessary). Connect the output of the Gain block to the second input of the Sum block. Double-click the Gain block and enter "mu*g*M1" into the Gain field. The rolling resistance force, however, acts in the negative direction. Therefore, change the list of signs of the Sum block to "|+-+". Next, resize the Gain block to display the full gain and label the output of the Gain block "Frr1". Your model should now appear as follows.

The last force acting on mass 1 is the spring force. Recall that this force is equal to the following. (4) Therefore, we need to generate a signal which we can then be multiplied by a gain to create the force. Drag a Subtraction block (or a Sum block or an Addition block) below the rest of your model. In order to change the direction of this block, right-click on the block and choose Format > Flip block from the resulting menu. Alternatively, you can select the block then hit Ctrl-I. Now, tap off the "x2" signal and connect it to the negative input of the Subtract block. Also, tap off the "x1" signal and connect it to the positive input. This will cause signal lines to cross. Lines may cross, but they are only actually connected where a small block appears (such as at a tap point).

Now, we can multiply this difference by the spring constant to generate the spring force. Drag a Gain block into your model to the left of the Subtraction block. Change the value of the Gain block to "k" and connect the output of the Subtract block to its input. Then connect the output of the Gain block to the third input of the Sum block for mass 1 and label the signal "Fs". Since the spring force acts on mass 1 in the negative direction, it is necessary to again change the list of signs of the Sum block to "|+--". Your model should appear as follows.

We can now apply forces to mass 2. For the first force, we will use the same spring force we just generated, except that it is applied to mass 2 in the positive direction. Simply tap off the spring force signal "Fs" and connect it to the first input of the Sum block for mass 2.

The last force applied to mass 2 is its rolling resistance force. This force is generated in an analogous manner to the rolling resistance force applied to mass 1. Tap off the signal "x2_dot" and multiply it by a Gain block with value "mu*g*M2". Then connect the output of the Gain block to the second input of the corresponding Sum block and label the signal "Frr2". Changing the second input of the Sum block to be negative will lead to the following model.

Now the model is complete. We simply need to supply the proper input and define the output of interest. The input to the system is the force generated by the engine. Within the Simulink model, we have already defined the force to be the output of a Signal Generator block. The output of the system, which we will observe and ultimately try to control, will be the velocity of the train engine. Add another Scope block to your model from the Sinks library. Tap a line from the "x1_dot" signal and connect it to the Scope block. Label this scope as "x1_dot" and your model should appear as in the following.

Now, the model is complete and should be saved. You can also download the completed model here.

Running the model Before running the model, we need to assign numerical values to each of the variables used in the model. For the train system, we will employ the following values. = 1 kg = 0.5 kg = 1 N/sec =1N = 0.02 sec/m = 9.8 m/s^2 Create a new m-file and enter the following commands. M1 = 1; M2 = 0.5; k F = 1; = 1;

mu = 0.02; g = 9.8;

Execute your m-file in the MATLAB command window to define these values. Simulink will recognize these MATLAB variables for use in the model. Now, we need to give an appropriate input to the engine. Double-click on the Signal Generator block (outputs "F"). Selectsquare from the Wave form drop-down menu and set the Frequency field to equal "0.001". You may leave the Units as the default Hertz. Also enter "-1" into the Amplitude field (positive amplitude steps negative before stepping positive).

The last step before running the simulation is to select an appropriate simulation time. To view one cycle of the 0.001 Hz square wave, we should simulate the model for 1000 seconds. Select Parameters from the Simulation menu at the top of the model window and change the Stop Time field to "1000". Close the dialog box. Now, run the simulation and open the "x1_dot" scope to examine the velocity output (hit autoscale). The input was a square wave with two steps, one positive and one negative. Physically, this means that the engine first went forward, then backward. The velocity output reflects this.

In this page we derived the mathematical model for the train system from first principles and then represented the derived equations within Simulink. An alternative option that is now available is to represent a dynamic system employing the physical modeling tool Simscape. Simscape is an addition to Simulink that allows a user to model a system employing blocks that represent physical quantities (and objects) such as inertias and joints, or resistances and inductors. Employing Simscape allows the user to simulate a physical system without deriving the governing mathematical equations. Continuing on to the Introduction: Simulink Control page, we will employ the model derived in this page to demonstrate how to use Simulink to design the control for our train system.

Introduction: Simulink Control


Contents The open-loop plant model Implementing a PID controller in Simulink Running the closed-loop model Extracting a model into MATLAB Controller design within Simulink

The open-loop plant model In the Introduction: Simulink Modeling page we demonstrated how Simulink can be employed to simulate a physical system. More generally, Simulink can also simulate the complete control system, including the control algorithm in addition to the physical plant. As mentioned previously, Simulink is especially useful for generating the approximate solutions of mathematical models that may be prohibitively difficult to solve "by hand." For example, consider that you have a nonlinear plant. A common approach is to generate a linear approximation of the plant and then use the linearized model to design a controller using analytical techniques. Simulink can then be employed to simulate the performance of your controller when applied to the full nonlinear model. Simulink can be employed for generating the linearized model and MATLAB can be employed for designing the controller as described in the other Introduction pages. Various control design facilities of MATLAB can also be accessed directly from within Simulink. We will demonstrate both approaches in this page. Recall the Simulink model of the toy train system derived in the Introduction: Simulink Modeling page and pictured below.

You can generate this model yourself, or you can download the completed model here. Assuming that the train only travels in one dimension (along the track), we want to apply control to the train engine so that it starts and comes to rest smoothly, and so that it can track a constant speed command with minimal error in steady state. Implementing a PID controller in Simulink Let us first create the structure for simulating the train system in unity feedback with a PID controller. In order to make our Simulink model more understandable, we will first save the train model into its own

subsystem block. To accomplish this, delete the three scope blocks and replace each one by an Out1 block from the Sinks library. Label each Out1 block with the corresponding variable name, "x1_dot", "x1", and "x2". Then delete the Signal Generator block and replace it with an In1 block from the Sources library. Label this input "F" for the force generated between the train engine and the railroad track. Your model should now appear as follows.

Next select all of the blocks in your model (Ctrl A) and select Create Subsystem from the Edit menu at the top of the model window. With a little rearranging and relabeling, your model will appear as shown below.

Now we can add a controller to our system. We will employ a PID controller which can be implemented using a PID Controller block from the Continuous library. Placing this block in series with the train subsystem, your model will appear as follows. In the following, we model the controller as generating the force "F" directly. This neglects the dynamics with which the train engine generates the torque applied to the wheels, and subsequently neglects the dynamics of how the force is generated at the wheel/track interface. This simplified approach is taken at this point since we only wish to introduce the basic functionality of Simulink for controller design and analysis.

Double-clicking on the PID Controller block, we will initially set the Integral (I) gain field equal to 0 and will leave theProportional (P) and Derivative (D) gains as their defaults of 1 and 0, respectively. Next add a Sum block from the Math Operations library. Double-click on this block and modify the List of signs field to "|+-". Since we wish to control the velocity of the toy train engine, we will feedback the engine's velocity. This is accomplished by 4ring a line off of the "x1_dot" signal and connecting it to the negative sign of the Sum block. The output of the Sum block will be the velocity error for the train engine and should be connected to the input of the PID Controller block. Connecting the blocks as described and adding labels, your model should appear as follows.

Next add a Signal Builder block from the Sources library to represent the velocity commanded to the train. Since we wish to design a controller to bring the train smoothly up to speed and smoothly to rest, we will test the system with a velocity command that steps up to 1 m/s followed by a step back down to 0 m/s (recall that our system is a toy train). To generate this type of command signal, double-click on the Signal Builder block. Then choose Change time range from the Axesmenu at the top of the block's dialog window. Set the Max time field to "300" seconds. Next, set the step up to occur at 10 seconds and the step down to occur at 150 seconds. This is accomplished by clicking on the corresponding portions of the signal graph (left and right vertical lines) and either dragging the line to the desired position, or entering the desired time in the T field at the bottom of the window. When done, your signal should appear as follows.

Also add a Scope block from the Sinks library and use it to replace the Out1 block for the train's velocity. Relabeling the blocks, your model will appear as follows.

We are now ready to run the closed-loop simulation. If you wish to skip the above steps, you may download the completed model with control here. Running the closed-loop model Before running the model, we need to assign numerical values to each of the variables used in the model. For the train system, we will employ the following values.
= 1 kg = 0.5 kg = 1 N/sec =1N = 0.02 sec/m = 9.8 m/s^2 Create a new m-file and enter the following commands. M1 = 1; M2 = 0.5; k F = 1; = 1;

mu = 0.02; g = 9.8;

Execute your m-file in the MATLAB command window to define these values. Simulink will recognize these MATLAB variables for use in the model. Next we need to set the time for which our simulation will run to match the time range of the command from the Signal Builder block. This is accomplished by selecting Parameters from the Simulation menu at the top of the model window and changing the Stop Time field to "300". Now, run the simulation and open the "x1_dot" scope to examine the velocity output (hit autoscale). The result as shown below demonstrates that the closed-loop system is unstable for this controller.

Since the performance achieved above is unsatisfactory, we need to redesign our controller. We will first demonstrate how to extract a model from Simulink into MATLAB for analysis and design. Then we will demonstrate how to design the control from directly within Simulink. Extracting a model into MATLAB The Simulink Control Design toolbox offers the functionality to extract a model from Simulink into the MATLAB workspace. This is especially useful for complicated, or nonlinear simulation models. This is also useful for generating discrete-time (sampled) models. For this example, let us extract a continous-time model of our train subsystem. First we need to identify the inputs and outputs of the model we wish to extract. The input to the train system is the force . We can designate this fact by right-clicking on the signal representing "F" (output of the PID block) and choosing Linearization Points > Input Point from the resulting menu. Likewise, we can designate the output of the train system by right-clicking on the "x1_dot" signal and choosing Linearization Points > Output Point from the resulting menu. These inputs and outputs will now be indicated by small arrow symbols as shown in the following figure. Since we wish to extract a model of the train by itself, without control, we need to further delete the feedback signal, otherwise we will extract the closed-loop model from to . Your model should now appear as follows.

We can now extract the model by opening the Linear Analysis Tool. This is accomplished by selecting Control Design > Linear Analysis from under the Tools menu at the top of the model window. Following these steps will open the window shown below.

This tool generates an LTI object from a (possibly nonlinear) Simulink model and allows you to specify the point about which the linearization is performed. Since our Simulink model is already linear, our choice of operating point will have no effect and we can leave it as the default Model Initial Condition. In order to generate the linearized model, select the Linearize button in the above figure, which is indicated by the green triangle. The Linear Analysis Tool window should now appear as shown below.

Inspecting the above, the step response of the linearized model was automatically generated. Comparing this step response to the one generated by the simulation of the open-loop train system in the Introduction: Simulink Modeling page, you can see that the responses are identical. This makes sense since the simulation model was already linear. Additionally, the linearization process generated the object linsys1 shown in the Linear Analysis Workspace above. This LTI object can be exported for use within MATLAB by simply dragging the object into the MATLAB Workspacewindow. Having extracted this model, we can now employ all of the facilities that MATLAB offers for controller design. For example, let us employ the following commands to generate and analyze the closed-loop system reflecting the Simulink model created above.
sys_cl = feedback(linsys1,1); pole(sys_cl)

ans = -1.5261 0.0000 0.0670 + 1.1977i 0.0670 - 1.1977i

Examination of the above demonstrates that the closed-loop system in its current state has poles with positive real part and, therefore, is unstable. This agrees with the result of our closed-loop simulation from above. We can then employ MATLAB to design a new controller. Instead, we will demonstrate how to access some of MATLAB's functionality from directly within Simulink. Controller design within Simulink Rather than performing the controller design in MATLAB, we can also launch interactive tools to tune our controller from within Simulink. One manner in which this can be done is to double-click on the PID Controller in the model and select theTune button to launch the PID Tuner GUI. Rather than do this, will launch the more general Simulink Control Design GUI by selecting Linear Analysis > Compensator Design from under the Tools menu located at the top of the model window. Following these steps will open the Control and Estimation Tools Manager window shown below.

The first thing that needs to be done is to identify the controller block that is to be tuned. This is accomplished by first clicking on the Select Blocks button, and then selecting the PID Controller block from the resulting window as shown below. Next click the OK button. Note that controllers represented in other types of blocks (Transfer Function, State Space, etc.) can also be tuned.

Before we proceed to tune our controller, we must first identify the inputs and outputs of the closed-loop system we wish to analyze. This is done in the same manner we did when extracting a model into MATLAB. Specifically, right-click on the velocity command signal (output of the Signal Builder block) and choose Linearization Points > Input Point from the resulting menu to identify the input of our closedloop system. Similarly, right-click on the train engine velocity signal ("x1_dot") and select Linearization Points > Output Point from the menu to choose the output of our system. Your model should now appear as follows where the small arrow symbols identify the input and output of the model.

Now that we have identified the block to tune and our input and output signals, we can now commence with tuning the controller. Select the Tune Blocks button in the Control and Estimation Tools

Manager window. This will open theDesign Configuration Window shown below which provides some introduction on how to employ the interactive design tool. In essence, this GUI is the SISO Design Tool that is available from within MATLAB as well.

Clicking the Next button, we will choose the design plots we wish to employ for designing our controller. In this example, we will employ a root locus design approach and hence will choose a Plot Type of Root Locus for Plot 1 as shown below. Since the root locus approach to design employs a plot from the openloop system for placing the closed-loop poles, we will leave the choice of Open/Closed Loops as Open Loop 1 (this is our only choice!).

Clicking the Next button again will allow us to choose our analysis plots. We use the step response plot to assess how well we are able to meet our goal of bringing the train up to speed smoothly with minimal steady-state error to a constant speed command. Therefore, we will choose a Plot Type of Step from the drop-down menu under Plot 1 as shown below. We will also select Plot 1 under the Plot Contents portion of the window for the only system that is defined. There is only one system available because we have defined only a single input and single output for our system.

Selecting the Next Button will then open the SISO Design Tool with a root locus plot and a step response plot. The root locus plot shown below displays the closed-loop pole locations of the train system plant under simple proportional control. Examining the plot, one can see that many values of loop gain will place closed-loop poles in the right-half plane leading to an unstable response.

If we decrease the loop gain sufficiently, we can move the closed-loop poles into the left-half plane and we can stabilize our system. This can be accomplished graphically by "grabbing" the pink boxes marking the closed-loop pole locations and dragging them toward the open-loop pole locations (marked by x's). A loop gain of approximately 0.1 will stabilize the system. Examining the corresponding step response, which will change automatically in response to the gain change if theReal-Time Update box is checked in the LTI Viewer window, you can see that while the response is stable its steady-state error is quite large.

Recall that adding integral control is one way to reduce the steady-state error for a system. In this case, adding an integrator via the controller will make the system type 1, where type 1 systems can track step references with zero steady-state error. Recall the following form of a PI controller. (1) Therefore, a PI controller will add an integrator and a zero to our open-loop system. The integrator can be added to the system by right-clicking in the field of the root locus plot and selecting Add Pole/Zero > Integrator from the resulting menu. Similarly, the zero can be added by right-clicking on the root locus plot and selecting Add Pole/Zero > Real zerofrom the resulting menu. Then click where along the real axis you wish to place the zero. We will place the zero just to the right of the plant pole on the real axis. You can move the zero by clicking on it and dragging it to a new location. Once you have placed the zero, then grab the pink boxes representing the closed-loop poles and attempt to line the three dominant poles up so that they have the same real part. There also will be a real closed-loop pole to the left that is "faster" than the

rest, and a closed-loop pole at the origin that is cancelled by a closed-loop zero at the origin. The resulting root locus plot is shown below.

The compensator can also be edited by directly typing in pole and zero locations. This can be done by choosing Edit Compensator from the Design menu located at the top of the SISO Design Task window. The window that opens is shown below. We will more precisely place the zero at -0.15 and will choose a loop gain equal to 0.01.

The resulting closed-loop step response plot is shown below demonstrating that the train engine is brought to speed smoothly and with zero steady-state error for a constant speed command.

The control gains that have been chosen can then be applied to the Simulink model by clicking the Update Simulink Block Parameters button under the Compensator Editor tab of the Control and Estimation Tools Manager window (see above). The simulation can then be run with this newly tuned controller. Clicking on the Scope block for the train engine's velocity and selecting autoscale will produce a plot like the one shown below.

Overall this response seems to meet our goals of bringing the train up to speed and to rest smoothly, while maintaining minimal steady-state error. This response matches the result generated with the SISO Design Tool above because that analysis and the Simulink model used the exact same linear model.

Cruise Control: System Modeling


Key MATLAB commands used in this tutorial are: ss , tf Contents Physical setup System equations System parameters State-space model Transfer function model Physical setup Automatic cruise control is an excellent example of a feedback control system found in many modern vehicles. The purpose of the cruise control system is to maintain a constant vehicle speed despite external disturbances, such as changes in wind or road grade. This is accomplished by measuring the vehicle speed, comparing it to the desired or reference speed, and automatically adjusting the throttle according to a control law.

We consider here a simple model of the vehicle dynamics, shown in the free-body diagram (FBD) above. The vehicle, of mass m, is acted on by a control force, u. The force u represents the force generated at the road/tire interface. For this simplified model we will assume that we can control this force directly and will neglect the dynamics of the powertrain, tires, etc., that go into generating the force. The resistive forces, bv, due to rolling resistance and wind drag, are assumed to vary linearly with the vehicle velocity, v, and act in the direction opposite the vehicle's motion. System equations With these assumptions we are left with a first-order mass-damper system. Summing forces in the xdirection and applying Newton's 2nd law, we arrive at the following system equation: (1) Since we are interested in controlling the speed of the vehicle, the output equation is chosen as follows (2) System parameters

For this example, let's assume that the parameters of the system are:
(m) (b) vehicle mass damping coefficient 1000 kg 50 N.s/m

State-space model First-order systems have only has a single energy storage mode, in this case the kinetic energy of the car, and therefore only one state variable is needed, the velocity. The state-space representation is therefore: (3) (4) We enter this state-space model into MATLAB using the following commands:
m = 1000; b = 50; A = -b/m; B = 1/m; C = 1; D = 0;

cruise_ss = ss(A,B,C,D); Transfer function model Taking the Laplace transform of the governing differential equation and assuming zero initial conditions, we find the transfer function of the cruise control system to be: (5) We enter the transfer function model into MATLAB using the following commands:
s = tf('s'); P_cruise = 1/(m*s+b);

DC Motor Speed: System Modeling Key MATLAB commands used in this tutorial are: tf , ss Contents Physical setup System equations Design requirements MATLAB representation

Physical setup A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide translational motion. The electric equivalent circuit of the armature and the free-body diagram of the rotor are shown in the following figure.

For this example, we will assume that the input of the system is the voltage source (V) applied to the motor's armature, while the output is the rotational speed of the shaft d(theta)/dt. The rotor and shaft are assumed to be rigid. We further assume a viscous friction model, that is, the friction torque is proportional to shaft angular velocity. The physical parameters for our example are:
(J) (b) (Ke) (Kt) (R) (L) moment of inertia of the rotor motor viscous friction constant electromotive force constant motor torque constant electric resistance electric inductance 0.01 kg.m^2 0.1 N.m.s 0.01 V/rad/sec 0.01 N.m/Amp 1 Ohm 0.5 H

System equations In general, the torque generated by a DC motor is proportional to the armature current and the strength of the magnetic field. In this example we will assume that the magnetic field is constant and, therefore, that the motor torque is proportional to only the armature current i by a constant factor Kt as shown in the equation below. This is referred to as an armature-controlled motor. (1) The back emf, e, is proportional to the angular velocity of the shaft by a constant factor Ke. (2) In SI units, the motor torque and back emf constants are equal, that is, Kt = Ke; therefore, we will use K to represent both the motor torque constant and the back emf constant. From the figure above, we can derive the following governing equations based on Newton's 2nd law and Kirchhoff's voltage law.

(3) (4) 1. Transfer Function Applying the Laplace transform, the above modeling equations can be expressed in terms of the Laplace variable s. (5) (6) We arrive at the following open-loop transfer function by eliminating I(s) between the two above equations, where the rotational speed is considered the output and the armature voltage is considered the input. (7) 2. State-Space In state-space form, the governing equations above can be expressed by choosing the rotational speed and electric current as the state variables. Again the armature voltage is treated as the input and the rotational speed is chosen as the output.

(8)

(9) Design requirements First consider that our uncompensated motor rotates at 0.1 rad/sec in steady state for an input voltage of 1 Volt (this is demonstrated in the DC Motor Speed: System Analysis page where the system's open-loop response is simulated). Since the most basic requirement of a motor is that it should rotate at the desired speed, we will require that the steady-state error of the motor speed be less than 1%. Another performance requirement for our motor is that it must accelerate to its steady-state speed as soon as it turns on. In this case, we want it to have a settling time less than 2 seconds. Also, since a speed faster than the reference may damage the equipment, we want to have a step response with overshoot of less than 5%. In summary, for a unit step command in motor speed, the control system's output should meet the following requirements. Settling time less than 2 seconds Overshoot less than 5%

Steady-state error less than 1% MATLAB representation 1. Transfer Function We can represent the above open-loop transfer function of the motor in MATLAB by defining the parameters and transfer function as follows. Running this code in the command window produces the output shown below.
J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2) P_motor = 0.01 --------------------------0.005 s^2 + 0.06 s + 0.1001 Continuous-time transfer function.

2. State Space We can also represent the system using the state-space equations. The following additional MATLAB commands create a state-space model of the motor and produce the output shown below when run in the MATLAB command window.
A = [-b/J -K/L B = [0 1/L]; C = [1 D = 0; motor_ss = ss(A,B,C,D) motor_ss = a = x1 x1 x2 -10 -0.02 x2 1 -2 0]; K/J -R/L];

b = u1 x1 x2 c = x1 y1 d = u1 y1 0 1 x2 0 0 2

Continuous-time state-space model.

The above state-space model can also be generated by converting your existing transfer function model into state-space form. This is again accomplished with the ss command as shown below. motor_ss = ss(P_motor); DC Motor Position: System Modeling Key MATLAB commands used in this tutorial are: tf , ss Contents Physical setup System equations Design requirements MATLAB representation Physical setup A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide translational motion. The electric equivalent circuit of the armature and the free-body diagram of the rotor are shown in the following figure.

For this example, we will assume the following values for the physical parameters. These values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab.
(J) (b) (Kb) (Kt) (R) (L) moment of inertia of the rotor motor viscous friction constant electromotive force constant motor torque constant electric resistance electric inductance 3.2284E-6 kg.m^2 3.5077E-6 N.m.s 0.0274 V/rad/sec 0.0274 N.m/Amp 4 Ohm 2.75E-6H

In this example, we assume that the input of the system is the voltage source (V) applied to the motor's armature, while the output is the position of the shaft (theta). The rotor and shaft are assumed to be rigid. We further assume a viscous friction model, that is, the friction torque is proportional to shaft angular velocity. System equations In general, the torque generated by a DC motor is proportional to the armature current and the strength of the magnetic field. In this example we will assume that the magnetic field is constant and, therefore, that the motor torque is proportional to only the armature current i by a constant factor Kt as shown in the equation below. This is referred to as an armature-controlled motor. (1) The back emf, e, is proportional to the angular velocity of the shaft by a constant factor Kb. (2) In SI units, the motor torque and back emf constants are equal, that is, Kt = Ke; therefore, we will use K to represent both the motor torque constant and the back emf constant. From the figure above, we can derive the following governing equations based on Newton's 2nd law and Kirchhoff's voltage law. (3) (4) 1. Transfer Function Applying the Laplace transform, the above modeling equations can be expressed in terms of the Laplace variable s. (5) (6)

We arrive at the following open-loop transfer function by eliminating I(s) between the two above equations, where the rotational speed is considered the output and the armature voltage is considered the input. (7) However, during this example we will be looking at the position as the output. We can obtain the position by integrating the speed, therefore, we just need to divide the above transfer function by s. (8) 2. State-Space The differential equations from above can also be expressed in state-space form by choosing the motor position, motor speed and armature current as the state variables. Again the armature voltage is treated as the input and the rotational position is chosen as the output.

(9)

(10) Design requirements We will want to be able to position the motor very precisely, thus the steady-state error of the motor position should be zero when given a commanded position. We will also want the steady-state error due to a constant disturbance to be zero as well. The other performance requirement is that the motor reaches its final position very quickly without excessive overshoot. In this case, we want the system to have a settling time of 40 ms and an overshoot smaller than 16%. If we simulate the reference input by a unit step input, then the motor position output should have: Settling time less than 40 milliseconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input MATLAB representation 1. Transfer Function

We can represent the above open-loop transfer function of the motor in MATLAB by defining the parameters and transfer function as follows. Running this code in the command window produces the output shown below.
J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2)) P_motor = 0.0274 ------------------------------------------8.878e-12 s^3 + 1.291e-05 s^2 + 0.0007648 s Continuous-time transfer function.

2. State Space We can also represent the system using the state-space equations. The following additional MATLAB commands create a state-space model of the motor and produce the output shown below when run in the MATLAB command window.
A = [0 1 0 0 -b/J K/J 0 -K/L -R/L]; B = [0 ; 0 ; 1/L]; C = [1 0 0]; D = [0]; motor_ss = ss(A,B,C,D) motor_ss =

a = x1 x1 x2 x3 0 0 0 x2 1 -1.087 -9964 x3 0 8487 -1.455e+06

b = u1 x1 x2 x3 0 0 3.636e+05

c = x1 y1 1 x2 0 x3 0

d = u1 y1 0

Continuous-time state-space model.

The above state-space model can also be generated by converting your existing transfer function model into state-space form. This is again accomplished with the ss command as shown below. motor_ss = ss(P_motor); Suspension: System Modeling Key MATLAB commands used in this tutorial are: ss , step Contents Physical setup System parameters Equations of motion Transfer function models Entering equations in MATLAB Physical setup

Designing an automotive suspension system is an interesting and challenging control problem. When the suspension system is designed, a 1/4 model (one of the four wheels) is used to simplify the problem to a 1D multiple spring-damper system. A diagram of this system is shown below. This model is for an active suspension system where an actuator is included that is able to generate the control force U to control the motion of the bus body.

System parameters
(M1) (M2) (K1) (K2) (b1) (b2) (U) 1/4 bus body mass suspension mass spring constant of suspension system spring constant of wheel and tire damping constant of suspension system damping constant of wheel and tire control force 2500 kg 320 kg 80,000 N/m 500,000 N/m 350 N.s/m 15,020 N.s/m

Equations of motion From the picture above and Newton's law, we can obtain the dynamic equations as the following: (1) (2) Transfer function models Assume that all of the initial conditions are zero, so that these equations represent the situation where the vehicle wheel goes up a bump. The dynamic equations above can be expressed in the form of transfer functions by taking the Laplace Transform. The specific derivation from the above equations to the transfer functions G1(s) and G2(s) is shown below where each transfer function has an output of, X1-X2, and inputs of U and W, respectively. (3) (4)

(5) (6) (7) or (8) Find the inverse of matrix A and then multiply with inputs U(s)and W(s) on the righthand side as follows: (9) (10)

When we want to consider the control input U(s) only, we set W(s) = 0. Thus we get the transfer function G1(s) as in the following: (11) When we want to consider the disturbance input W(s) only, we set U(s) = 0. Thus we get the transfer function G2(s) as in the following: (12) Entering equations in MATLAB We can generate the above transfer function models in MATLAB by entering the following commands in the MATLAB command window.
M1 = 2500; M2 = 320; K1 = 80000; K2 = 500000; b1 = 350; b2 = 15020; s = tf('s'); G1 = ((M1+M2)*s^2+b2*s+K2)/((M1*s^2+b1*s+K1)*(M2*s^2+(b1+b2)*s+(K1+K2))-(b1*s+K1)*(b1*s+K1)); G2 = (-M1*b2*s^3-M1*K2*s^2)/((M1*s^2+b1*s+K1)*(M2*s^2+(b1+b2)*s+(K1+K2))-(b1*s+K1)*(b1*s+K1));

Inverted Pendulum: System Modeling Key MATLAB commands used in this tutorial are: tf , ss , set Contents Problem setup and design requirements Force analysis and system equations MATLAB representation Problem setup and design requirements The system in this example consists of an inverted pendulum mounted to a motorized cart. The inverted pendulum system is an example commonly found in control system textbooks and research literature. Its popularity derives in part from the fact that it is unstable without control, that is, the pendulum will simply fall over if the cart isn't moved to balance it. Additionally, the dynamics of the system are nonlinear. The objective of the control system is to balance the inverted pendulum by applying a force to the cart that the pendulum is attached to. A real-world example that relates directly to this inverted pendulum system is the attitude control of a booster rocket at takeoff. In this case we will consider a two-dimensional problem where the pendulum is contrained to move in the vertical plane shown in the figure below. For this system, the control input is the force that moves the cart horizontally and the outputs are the angular position of the pendulum and the horizontal position of the cart .

For this example, let's assume the following quantities: (M) (m) (b) (l) mass of the cart mass of the pendulum coefficient of friction for cart length to pendulum center of mass 0.5 kg 0.2 kg 0.1 N/m/sec 0.3 m

(I) (F) (x) (theta)

mass moment of inertia of the pendulum force applied to the cart cart position coordinate pendulum angle from vertical (down)

0.006 kg.m^2

For the PID, root locus, and frequency response sections of this problem, we will be interested only in the control of the pendulum's position. This is because the techniques used in these sections are best-suited for single-input, single-output (SISO) systems. Therefore, none of the design criteria deal with the cart's position. We will, however, investigate the controller's effect on the cart's position after the controller has been designed. For these sections, we will design a controller to restore the pendulum to a vertically upward position after it has experienced an impulsive "bump" to the cart. Specifically, the design criteria are that the pendulum return to its upright position within 5 seconds and that the pendulum never move more than 0.05 radians away from vertical after being disturbed by an impulse of magnitude 1 Nsec. The pendulum will initially begin in the vertically upward equilibrium, = . In summary, the design requirements for this system are: Settling time for of less than 5 seconds Pendulum angle never more than 0.05 radians from the vertical Employing state-space design techniques, we are more readily able to address a multi-output system. In our case, the inverted pendulum system is single-input, multi-output (SIMO). Therefore, for the state-space section of the Inverted Pendulum example, we will attempt to control both the pendulum's angle and the cart's position. To make the design more challenging in this section, we will command a 0.2-meter step in the cart's desired position. Under these conditions, it is desired that the cart achieve its commanded position within 5 seconds and have a rise time under 0.5 seconds. It is also desired that the pendulum settle to its vertical position in under 5 seconds, and further, that the pendulum angle not travel more than 20 degrees (0.35 radians) way from the vertically upward position. In summary, the design requirements for the inverted pendulum state-space example are: Settling time for Rise time for and of less than 5 seconds

of less than 0.5 seconds

Pendulum angle never more than 20 degrees (0.35 radians) from the vertical Steady-state error of less than 2% for Force analysis and system equations Below are the free-body diagrams of the two elements of the inverted pendulum system. and

Summing the forces in the free-body diagram of the cart in the horizontal direction, you get the following equation of motion. (1) Note that you can also sum the forces in the vertical direction for the cart, but no useful information would be gained. Summing the forces in the free-body diagram of the pendulum in the horizontal direction, you get the following expression for the reaction force . (2) If you substitute this equation into the first equation, you get one of the two governing equations for this system. (3) To get the second equation of motion for this system, sum the forces perpendicular to the pendulum. Solving the system along this axis greatly simplifies the mathematics. You should get the following equation. (4) To get rid of the and terms in the equation above, sum the moments about the centroid of the pendulum to get the following equation. (5) Combining these last two expressions, you get the second governing equation. (6)

Since the analysis and control design techniques we will be employing in this example apply only to linear systems, this set of equations needs to be linearized. Specifically, we will linearize the equations about the vertically upward equillibrium position, = , and will assume that the system stays within a small neighborhood of this equillbrium. This assumption should be reasonably valid since under control we desire that the pendulum not deviate more than 20 degrees from the vertically upward position. Let represent the deviation of the pedulum's position from equilibrium, that is, = + . Again presuming a small deviation ( ) from equilibrium, we can use the following small angle approximations of the nonlinear functions in our system equations: (7) (8) (9) After substiting the above approximations into our nonlinear governing equations, we arrive at the two linearized equations of motion. Note has been substituted for the input . (10) (11) 1. Transfer Function To obtain the transfer functions of the linearized system equations, we must first take the Laplace transform of the system equations assuming zero initial conditions. The resulting Laplace transforms are shown below. (12) (13) Recall that a transfer function represents the relationship between a single input and a single output at a time. To find our first transfer function for the output and an input of we need to eliminate from the above equations. Solve the first equation for . (14) Then substitute the above into the second equation. (15) Rearranging, the transfer function is then the following

(16) where,

(17) From the transfer function above it can be seen that there is both a pole and a zero at the origin. These can be canceled and the transfer function becomes the following.

(18) Second, the transfer function with the cart position arrive at the following. as the output can be derived in a similar manner to

(19) 2. State-Space The linearized equations of motion from above can also be represented in state-space form if they are rearranged into a series of first order differential equations. Since the equations are linear, they can then be put into the standard matrix form shown below.

(20)

(21) The matrix has 2 rows because both the cart's position and the pendulum's position are part of the output. Specifically, the cart's position is the first element of the output and the pendulum's deviation from its equilibrium position is the second element of . MATLAB representation 1. Transfer Function We can represent the transfer functions derived above for the inverted pendulum system within MATLAB employing the following commands. Note that you can give names to the outputs (and inputs) to differentiate between the cart's position and the pendulum's position. Running this code in the command window produces the output shown below. M = 0.5; m = 0.2; b = 0.1; I = 0.006;

g = 9.8; l = 0.3; q = (M+m)*(I+m*l^2)-(m*l)^2; s = tf('s');

P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q ((M + m)*m*g*l)*s^2/q - b*m*g*l*s/q);

P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q b*m*g*l/q);

sys_tf = [P_cart ; P_pend];

inputs = {'u'}; outputs = {'x'; 'phi'};

set(sys_tf,'InputName',inputs) set(sys_tf,'OutputName',outputs)

sys_tf sys_tf =

From input "u" to output... 4.182e-06 s^2 - 0.0001025 x: --------------------------------------------------------2.3e-06 s^4 + 4.182e-07 s^3 - 7.172e-05 s^2 - 1.025e-05 s

1.045e-05 s phi: ----------------------------------------------------2.3e-06 s^3 + 4.182e-07 s^2 - 7.172e-05 s - 1.025e-05

Continuous-time transfer function.

2. State-Space We can also represent the system using the state-space equations. The following additional MATLAB commands create a state-space model of the inverted pendulum and produce the output shown below when run in the MATLAB command window. Again note that the names of the inputs, outputs, and states can be specified to make the model easier to understand. M = .5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3;

p = I*(M+m)+M*m*l^2; %denominator for the A and B matrices

A = [0

0 (m^2*g*l^2)/p 0 m*g*l*(M+m)/p

0; 0; 1; 0];

0 -(I+m*l^2)*b/p 0 0

0 -(m*l*b)/p B = [ 0; (I+m*l^2)/p; 0; m*l/p]; C = [1 0 0 0; 0 0 1 0]; D = [0; 0];

states = {'x' 'x_dot' 'phi' 'phi_dot'};

inputs = {'u'}; outputs = {'x'; 'phi'};

sys_ss = ss(A,B,C,D,'statename',states,'inputname',inputs,'outputname',outputs) sys_ss =

a = x x x_dot phi phi_dot 0 0 0 0 x_dot 1 -0.1818 0 -0.4545 phi 0 2.673 0 31.18 phi_dot 0 0 1 0

b = u x x_dot phi phi_dot 0 1.818 0 4.545

c = x x phi 1 0 x_dot 0 0 phi 0 1 phi_dot 0 0

d = u x phi 0 0

Continuous-time state-space model.

The above state-space model can also be converted into transfer function form employing the tf command as shown below. Conversely, the transfer function model can be converted into state-space form using the ss command. sys_tf = tf(sys_ss) sys_tf =

From input "u" to output... 1.818 s^2 - 44.55 x: -------------------------------------s^4 + 0.1818 s^3 - 31.18 s^2 - 4.455 s

4.545 s + 2.539e-16 phi: ---------------------------------s^3 + 0.1818 s^2 - 31.18 s - 4.455

Continuous-time transfer function.

Examining the above, note the existance of some terms with very small coefficients. These terms should actually be zero and show up due to numerical round-off errors that accumulate in the conversion algorithms that MATLAB employs. If you set these coefficients to zero, then the above transfer function models will match those generated earlier in the Transfer Function section of the example. Cruise Control: System Analysis Key MATLAB commands used in this tutorial are: ss , step Contents System model and parameters Performance specifications Open-loop step response Open-loop poles/zeros

Open-loop Bode plot System model and parameters The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System Modeling page for the derivation. (1) The parameters used in this example are as follows: (m) (b) (u) vehicle mass damping coefficient nominal control force 1000 kg 50 N.s/m 500 N

Performance specifications The next step is to come up with some design criteria that the compensated system should achieve. When the engine gives a 500 Newton force, the car will reach a maximum velocity of 10 m/s (22 mph), see openloop step response section below. An automobile should be able to accelerate up to that speed in less than 5 seconds. In this application, a 10% overshoot and 2% steady-state error on the velocity are sufficient. Keeping the above in mind, we have proposed the following design criteria for this problem: Rise time < 5 s Overshoot < 10% Steady-state error < 2% Open-loop step response The open-loop response of the system, without any feedback control, to a step input force of 500 Newtons is simulated in MATLAB as follows: m = 1000; b = 50; u = 500;

s = tf('s'); P_cruise = 1/(m*s+b);

step(u*P_cruise)

We see that the open-loop system exhibits no overshoot or oscillations (characteristic of first-order systems), and does reach the desired steady-state speed of 10 m/s; however, the rise time is much too slow, ~60 s. Therefore we need to design a feedback controller which speeds up the response significantly without negatively affecting the other dynamic performance metrics. Open-loop poles/zeros The cruise control system has a single pole at s = -b/m which we can see plotted on the s-plane using the following MATLAB commands: pzmap(P_cruise) axis([-1 1 -1 1])

We observe that the open-loop system is stable and does not oscillate since the pole is real and negative. Furthermore, the speed of response is determined by the magnitude of this pole, b/m: the larger the magnitude, the quicker the system approaches the steady-state value. Since we're typically not able to change the system parameters to change the dynamic response of the system, we must instead design controllers which alter the poles and zeros of the closed-loop system to meet the desired performance specifications. Open-loop Bode plot We are also interested in the open-loop frequency response of the system which we find using the following MATLAB command: bode(P_cruise)

We see that the Bode plots exhibit the definitive features of first-order systems, including a -3 dB magnitude and -45 deg phase at the corner frequency of w = b/m = 0.05 rad/s and -20 dB/dec roll-off at high frequencies. DC Motor Speed: System Analysis Key MATLAB commands used in this tutorial are: tf , ltiview Contents Open-loop response LTI model characteristics Response to other types of inputs From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer function of the DC Motor are the following. (1) (2) (3)

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page. For a 1-rad/sec step reference, the design criteria are the following. Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1% Open-loop response First create a new m-file and type in the following commands (refer to the main problem for the details of getting these commands). J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2); Now let's see how the original open-loop system performs. Add the following ltiview command onto the end of the m-file and run it in the MATLAB command window. The string 'step' passed to the function specifies to generate a unit step response plot for the system P_motor. The range of numbers 0:0.1:5 specify that the step response plot should include data points for times from 0 to 5 seconds in steps of 0.1 seconds. The resulting plot is shown in the figure below, where you can view some of the system's characteristics by right clicking on the figure and choosing from theCharacteristics menu such performance aspects as Settling Time and Steady State. ltiview('step', P_motor, 0:0.1:5);

From the plot we see that when 1 Volt is applied to the system the motor can only achieve a maximum speed of 0.1 rad/sec, ten times smaller than our desired speed. Also, it takes the motor 2.07 seconds to reach its steady-state speed; this does not satisfy our 2 second settling time criterion. LTI model characteristics Since our open-loop transfer function has the form of a canonical second-order system, we should be able to accurately predict the step response characteristics observed above based on the transfer function's pole locations. You can graphically see the location of the poles (and zeros) of theP_motor system from within the LTI Viewer by right-clicking on the plot area and selecting Plot Types > Pole/Zero from the resulting menu. Performing this action will change the LTI Viewer to the following map where the blue x's identify the locations of poles.

From the above you can see that the open-loop transfer function has two real poles, one at s = -2 and one at s = -10. Since both poles are real, there is no oscillation in the step response (or overshoot) as we have already seen. Futhermore, since the one pole is 5 times more negative than the other, the slower of the two poles will dominate the dynamics. That is, the pole at s = -2 primarily determines the speed of response of the system and the system behaves similarly to a first-order system. Let's see just how closely a first-order model approximates our original motor model. Enter the following command at the MATLAB command line to build a first-order transfer function with pole at s = -2 and steady-state value matching the original transfer function. rP_motor = 0.1/(0.5*s+1) rP_motor =

0.1 --------0.5 s + 1

Continuous-time transfer function.

We can then import this new model into the LTI Viewer. This is accomplished by selecting Import from the File menu at the top of the LTI Viewerwindow. From the resulting window choose rP_motor from the Systems in Workspace area and then click the OK button. The LTI Viewer will now show plots of both the original and the reduced transfer functions. You can then switch back to step response plots by again choosing Plot Types from the right-click menu. You can remove the plot annotations by right-clicking on the plot and using the Characteristics submenu. You can also add a legend by clicking the legend icon on the toolbar. Now the LTI Viewer should appear as shown below.

From the above, we can see that a first-order approximation of our motor system is relatively accurate. The primary difference can be seen at t = 0 where a second order system will have a derivative of zero, but our first-order model will not. With a first-order system, the settling time is equal to

(4) where tau is the time constant which in this case is 0.5. Therefore, our first-order model has a settling time of 2 seconds which is close to the 2.07 seconds of our actual system. Throughout the rest of the pages of this example, different controllers will be designed to reduce the steady-state error significantly and the settling time slightly while still meeting the given overshoot requirement. Response to other types of inputs While the requirements for this example are given in terms of the system's step response, it is likely that the system will in practice be subject to other types of inputs. Even so, a system's step response can give insight into how the system will respond to other types of signals. In order to determine the system's specific response to other types of inputs, you can employ Simulink or the MATLAB command lsim. Furthermore, you can simulate the system's response to other types of inputs straight from the LTI Viewer. This is accomplished by right-clicking on the displayed plots and choosing Plot Types > Linear Simulation. The following window will then appear.

Within this window set the End time (sec) to "5" and the Interval (sec) to "0.1". Then under the System inputs section of the window, you can import an input signal, or design one from a select set of choices. In this instance, click the Design signal button and choose a Signal type of Sine wave from within the window that appears. Then change the Frequency (Hz) to "0.2" and leave the Amplitude and Duration (secs) as their default values. Then click the Insert button at the bottom of the Signal Designer window and the Simulate button at the bottom of the Linear Simulation Tool window. The responses of our two currently identified systems to the sine wave input are then produced in the LTI Viewer window. If you double-click on the y-axis of the plot, you can then change the limits to match the figure shown below.

DC Motor Position: System Analysis Key MATLAB commands used are: tf , step , isstable , pole , feedback , pzmap , damp Contents Open-loop response Closed-loop response in this tutorial

From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer function of the DC Motor are the following. (1) (2) (3) For the original problem setup and the derivation of the above equations, please refer to the DC Motor Position: System Modeling page. For a 1-radian step reference, the design criteria are given are the following. Settling time less than 0.040 seconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input Open-loop response First create a new m-file and type in the following commands (refer to the main problem for the details of getting these commands). J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2)); Now let's see how the uncompensated open-loop system performs. Specifically, we will use the MATLAB command step to analyze the open-loop step response. Add the following commands onto the end of the m-file and run it in the MATLAB command window and you will get the associated plot shown below. t = 0:0.001:0.2; step(P_motor,t)

From the above plot, we can see that when 1 volt is applied to the system the motor position grows unbounded. This is obviously at odds with the given requirements, in particular, that there be no steadystate error. The open-loop response of the system is not even stable. Stability of a system can be verified with the MATLAB command isstable where a returned value of TRUE (1) indicates that the system is stable and a returned value of FALSE (0) indicates that the system is not stable. isstable(P_motor) ans = 0 Stability of the system can also be determined from the poles of the transfer function where the poles can be identified using the MATLAB command poleas shown below. pole(P_motor) ans = 1.0e+06 * 0 -1.4545 -0.0001

As indicated by this function, one of the poles of the open-loop transfer function is on the imaginary axis while the other two poles are in the left half of the complex s-plane. A pole on the imaginary axis indicates that the free response of the system will not grow unbounded, but also will not decay to zero. Even though the free response will not grow unbounded, a system with a pole on the imaginary axis can grow unbounded when given an input, even when the input is bounded. This fact is in agreement with what we have already seen. In this particular case, the pole at the origin behaves like an integrator. Therefore, when the system is given a step input its output continues to grow to infinity in the same manner that an integral of a constant would grow to infinity as the upper limit of the integral is made larger. Closed-loop response Let's now consider the closed-loop response of the system where the system schematic has the following structure.

The closed-loop transfer function for the above with the controller C(s) simply set equal to 1 can be generated using the MATLAB command feedbackas shown below. sys_cl = feedback(P_motor,1) sys_cl =

0.0274 ---------------------------------------------------8.878e-12 s^3 + 1.291e-05 s^2 + 0.0007648 s + 0.0274

Continuous-time transfer function.

The corresponding unit step response can be generated by adding the above and following command to your m-file. The annotations for the peak response, settling time, and final value can be added to the plot from the right-click menu under Characteristics. step(sys_cl,t)

Examining the above closed-loop step response, the addition of feedback has stabilized the system. In fact, the steady-state error appears to be driven to zero and the overshoot is less than 16%, though the settle time requirement is not met. The character of the resulting step response is again indicated by the location of the poles of the system's transfer function just like the system's stability properties were. The MATLAB command pzmap will plot the poles (and zeros) of a given transfer function as shown below. pzmap(sys_cl)

The above plot shows that the closed-loop system has one real pole at -1.45e6 and a pair of complex poles at -29.6+35.3j and -29.6-35.3j as indicated by the locations of the blue x's. The damping and natural frequencies associated with these poles can be determined by right-clicking on the associated poles in the resulting plot. This information can also be determined using the MATLAB command damp as shown below. damp(sys_cl)

Eigenvalue

Damping

Frequency

-2.96e+01 + 3.53e+01i -2.96e+01 - 3.53e+01i -1.45e+06

6.43e-01 6.43e-01 1.00e+00

4.61e+01 4.61e+01 1.45e+06

(Frequencies expressed in rad/seconds)

Since the one real pole is so much faster than the complex conjugate poles (its real part is much more negative) its effect on the dynamic response of the system will be mimimal. Therefore, the damping (zeta = 0.643) and the natural frequency (Wn = 46.1) of the complex conjugate poles will primarily indicate the response of the closed-loop system. Adding the following commands to your m-file will calculate the overshoot and 2% settle time predicted by these poles assuming that they dominate, in other words, that we have a canonical underdamped second-order system. [Wn,zeta,poles] = damp(sys_cl); OS = exp((-zeta(1)*pi)/sqrt(1-zeta(1)^2)) Ts = 4/(zeta(1)*Wn(1)) OS = 0.0716 Ts = 0.1351 The above results closely match the overshoot and settle time from the step response plot above which explicitly captured the effect of the third, non-dominant pole. Throughout the rest of the pages of this example, different controllers will be designed to reduce the settle time of the closed-loop step response to meet the given 40 millisecond requirement while still meeting the other system requirements, including the zero steady-state error in the presence of a step disturbance. Suspension: System Analysis Key MATLAB commands used in this tutorial are: ss , step Contents System model System parameters Design requirements Open-loop step response System model The state-space and transfer function models of the bus suspension problem were derived in the Suspension: System Modeling page. System parameters (M1) (M2) 1/4 bus body mass suspension mass 2500 kg 320 kg

(K1) (K2) (b1) (b2) (U)

spring constant of suspension system spring constant of wheel and tire damping constant of suspension system damping constant of wheel and tire control force

80,000 N/m 500,000 N/m 350 N.s/m 15,020 N.s/m

Design requirements A good bus suspension system should have satisfactory road holding ability, while still providing comfort when riding over bumps and holes in the road. When the bus is experiencing any road disturbance (i.e. pot holes, cracks, and uneven pavement),the bus body should not have large oscillations, and the oscillations should dissipate quickly. Since the distance X1-W is very difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the distance X1-X2 instead of X1-W as the output in our problem. Keep in mind that this is an estimation. The road disturbance (W) in this problem will be simulated by a step input. This step could represent the bus coming out of a pothole. We want to design a feedback controller so that the output (X1-X2) has an overshoot less than 5% and a settling time shorter than 5 seconds. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and return to a smooth ride within 5 seconds. Open-loop step response We can use MATLAB to display how the original open-loop system performs (without any feedback control). Add the following commands into the m-file and run it in the MATLAB command window to see the response of unit step actuated force input, U(s). Note that the step command will generate the unit step inputs for each input. M1 = 2500; M2 = 320; K1 = 80000; K2 = 500000; b1 = 350; b2 = 15020;

s = tf('s'); G1 ((M1+M2)*s^2+b2*s+K2)/((M1*s^2+b1*s+K1)*(M2*s^2+(b1+b2)*s+(K1+K2))(b1*s+K1)*(b1*s+K1)); =

step(G1)

From this graph of the open-loop response for a unit step actuated force, we can see that the system is under-damped. People sitting in the bus will feel very small amount of oscillation. Moreover, the bus takes an unacceptably long time to reach the steady state (the settling time is very large). Now enter the following commands to see the response for a step disturbance input, W(s), with magnitude 0.1 m. G2 = M1*K2*s^2)/((M1*s^2+b1*s+K1)*(M2*s^2+(b1+b2)*s+(K1+K2))(b1*s+K1)*(b1*s+K1)); (-M1*b2*s^3-

step(0.1*G2)

From this graph of the open-loop response for 10 cm step disturbance, we can see that when the bus passes a 10 cm high bump on the road, the bus body will oscillate for an unacceptably long time(~50 seconds) with an initial amplitude of 8 cm. People sitting in the bus will not be comfortable with such an oscillation due to the large overshoot and long settling time. The solution to these problems is to add a feedback controller into the system to improve the performance. The schematic of the closed-loop system is the following, which will be discussed in much more detail in the controller design sections.

Published with MATLAB 7.14 Inverted Pendulum: System Analysis Key MATLAB commands used in this tutorial are: tf , ss , zpkdata , impulse , lsim Contents Open-loop impulse response Open-loop step response From the main problem, we derived the open-loop transfer functions of the inverted pendulum system as the following.

(1)

(2) where (3) Recall that the above two transfer functions are valid only for small values of the angle phi where is the deviation of the pendulum from the vertically upward position. Also, the absolute pendulum angle is equal to + . For the original problem setup and the derivation of the above transfer functions, please refer to the Inverted Pendulum: System Modeling page. Considering the response of the pendulum to a 1-Nsec impulse applied to the cart, the design requirements for the pendulum are: Settling time for of less than 5 seconds Pendulum angle never more than 0.05 radians from the vertical Additionally, the requirements for the response of the system to a 0.2-meter step command in cart position are: Settling time for Rise time for and of less than 5 seconds

of less than 0.5 seconds

Pendulum angle never more than 20 degrees (0.35 radians) from the vertical Open-loop impulse response

We will begin by looking at the open-loop response of the inverted pendulum system. Create a new mfile and type in the following commands to create the system model (refer to the main problem for the details of getting these commands). M = 0.5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3; q = (M+m)*(I+m*l^2)-(m*l)^2; s = tf('s');

P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q ((M + m)*m*g*l)*s^2/q - b*m*g*l*s/q);

P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q b*m*g*l/q);

sys_tf = [P_cart ; P_pend];

inputs = {'u'}; outputs = {'x'; 'phi'};

set(sys_tf,'InputName',inputs) set(sys_tf,'OutputName',outputs) We can now examine the open-loop impulse response of the system. Specifically, we will examine how the system responds to an impulsive force applied to the cart employing the MATLAB command impulse. Add the following commands onto the end of the m-file and run it in the MATLAB command window to get the associated plot shown below. t=0:0.01:1; impulse(sys_tf,t); title('Open-Loop Impulse Response')

As you can see from the plot, the system response is entirely unsatisfactory. In fact, it is not stable in open loop. Although the pendulum's position is shown to increase past 100 radians (15 revolutions), the model is only valid for small . You can also see that the cart's position moves infinitely far to the right, though there is no requirement on cart position for an impulsive force input. The poles of a system can also tell us about its time response. Since our system has two outputs and one input, it is described by two transfer functions. In general, all transfer functions from each input to each output of a multi-input, multi-output (MIMO) system will have the same poles (but different zeros) unless there are pole-zero cancellations. We will specifically examine the poles and zeros of the system using the MATLAB function zpkdata. The parameter 'v' shown below returns the poles and zeros as column vectors rather than as cell arrays. The zeros and poles of the system where the pendulum position is the output are found as shown below: [zeros poles] = zpkdata(P_pend,'v') zeros = 0 poles = 5.5651 -5.6041

-0.1428 Likewise, the zeros and poles of the system where the cart position is the output are found as follows: [zeros poles] = zpkdata(P_cart,'v') zeros = 4.9497 -4.9497 poles = 0 5.5651 -5.6041 -0.1428 As predicted, the poles for both transfer functions are identical. The pole at 5.5651 indicates that the system is unstable since the pole has positive real part. In other words, the pole is in the right half of the complex s-plane. This agrees with what we observed above. Open-loop step response Since the system has a pole with positive real part its response to a step input will also grow unbounded. We will verify this using the lsim command which can be employed to simulate the response of LTI models to arbitrary inputs. In this case, a 1-Newton step input will be used. Adding the following code to your m-file and running it in the MATLAB command window will generate the plot shown below. t = 0:0.05:10; u = ones(size(t)); [y,t] = lsim(sys_tf,u,t); plot(t,y) title('Open-Loop Step Response') axis([0 3 0 50]) legend('x','phi')

You can also identify some important characteristics of the response using the lsiminfo command as shown. step_info = lsiminfo(y,t); cart_info = step_info(1) pend_info = step_info(2) cart_info = SettlingTime: 9.9959 Min: 0 MinTime: 0 Max: 8.7918e+21 MaxTime: 10 pend_info = SettlingTime: 9.9959 Min: 0 MinTime: 0

Max: 1.0520e+23 MaxTime: 10 The above results confirm our expectation that the system's response to a step input is unstable. It is apparent from the analysis above that some sort of control will need to be designed to improve the response of the system. Four example controllers are included with these tutorials: PID, root locus, frequency response, and state space. You may select a choice from the menu to the left for further details. Note: The solutions shown in the PID, root locus, and frequency response examples may not yield a workable controller for the inverted pendulum problem. As stated previously, when we treat the inverted pendulum as a single-input, single-output system, we ignore the position of the cart, . Where possible in these examples, we will show what happens to the cart's position when a controller is implemented on the system. Inverted Pendulum: PID Controller Design Key MATLAB commands used in this tutorial are: tf , impulse , feedback , pid Contents System structure PID control What happens to the cart's position? In this page we will design a PID controller for the inverted pendulum system. In the design process we will assume a single-input, single-output plant as described by the following transfer function. Otherwise stated, we will attempt to control the pendulum's angle without regard for the cart's position.

(1) where, (2) More specifically, the controller will attempt to maintain the pendulum vertically upward when the cart is subjected to a 1-Nsec impulse. Under these conditions, the design criteria are: Settling time of less than 5 seconds Pendulum should not move more than 0.05 radians away from the vertical For the original problem setup and the derivation of the above transfer function, please consult the Inverted Pendulum: System Modeling page. System structure

The structure of the controller for this problem is a little different than the standard control problems you may be used to. Since we are attempting to control the pendulum's position, which should return to the vertical after the initial disturbance, the reference signal we are tracking should be zero. This type of situation is often referred to as a regulator problem. The external force applied to the cart can be considered as an impulsive disturbance. The schematic for this problem is depicted below.

You may find it easier to analyze and design for this system if we first rearrange the schematic as follows.

The resulting transfer function for the closed-loop system from an input of force pendulum angle is then determined to be the following. (3)

to an output of

Before we begin designing our PID controller, we first need to define our plant within MATLAB. Create a new m-file and type in the following commands to create the plant model (refer to the main problem for the details of getting these commands). M = 0.5; m = 0.2;

b = 0.1; I = 0.006; g = 9.8; l = 0.3; q = (M+m)*(I+m*l^2)-(m*l)^2; s = tf('s'); P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q b*m*g*l/q); Next we will define a PID controller. PID control This closed-loop transfer function can be modeled in MATLAB by copying the following code to the end of your m-file (whether you're using the transfer function form or the state-space representation of the plant). Specifically, we define our controller using the pid object within MATLAB. We then use thefeedback command to generate the closed-loop transfer function as depicted in the figure above where the disturbance force is the input and the deviation of the pendulum angle from the vertical is the output. Kp = 1; Ki = 1; Kd = 1; C = pid(Kp,Ki,Kd); T = feedback(P_pend,C); Now we can begin to tune our controller. First let's examine the response of the closed-loop system to an impulse disturbance for this initial set of control gains. Enter the following code to the end of your m-file and run in the MATLAB command window. You should generate the response plot shown below. t=0:0.01:10; impulse(T,t) title('Response of Pendulum Position to an Impulse Disturbance under PID Control: Kp = 1, Ki = 1, Kd = 1');

This response is still not stable. Let's begin to modify the response by increasing the proportional gain. Increase the variable to see what effect it has on the response. If you modify your m-file to the following where = 100 and run in the command window, you should get the response plot shown below. Kp = 100; Ki = 1; Kd = 1; C = pid(Kp,Ki,Kd); T = feedback(P_pend,C); t=0:0.01:10; impulse(T,t) axis([0, 2.5, -0.2, 0.2]); title('Response of Pendulum Position to an Impulse Disturbance under PID Control: Kp = 100, Ki = 1, Kd = 1');

Right-clicking on the resulting plot and choosing Characteristics from the resulting menu allows you to identify important characteristics of the response. Specifically, the settling time of the response is determined to be 1.64 seconds, which is less than the requirement of 5 seconds. Since the steady-state error approaches zero in a sufficiently fast manner, no additional integral action is needed. You can set the integral gain constant to zero to see for yourself that some integral control is needed. The peak response, however, is larger than the requirement of 0.05 radians. Recall that overshoot often can be reduced by increasing the amount of derivative control. After some trial and error it is found that a derivative gain of = 20 provides a satisfactory response. Modifying your m-file as follows and rerunning should produce the response plot shown below Kp = 100; Ki = 1; Kd = 20; C = pid(Kp,Ki,Kd); T = feedback(P_pend,C); t=0:0.01:10; impulse(T,t) axis([0, 2.5, -0.2, 0.2]);

title('Response of Pendulum Position to an Impulse Disturbance under PID Control: Kp = 100, Ki = 1, Kd = 20');

As you can see, the overshoot has been reduced so that the pendulum does not move more than 0.05 radians away from the vertical. Since all of the given design requirements have been met, no further iteration is needed. What happens to the cart's position? At the beginning of this page, a block diagram for the inverted pendulum system was given. The diagram was not entirely complete. The block representing the response of the cart's position was not included because that variable is not being controlled. It is interesting though, to see what is happening to the cart's position when the controller for the pendulum's angle is in place. To see this we need to consider the full system block diagram as shown in the following figure.

Rearranging, we get the following block diagram.

In the above, the block loop transfer function given by the following. (4)

is the controller designed for maintaining the pendulum vertical. The closedfrom an input force applied to the cart to an output of cart position is, therefore,

Referring to the Inverted Pendulum: System Modeling page, the transfer function for follows.

is defined as

(5) where,

(6) Adding the following commands to your m-file (presuming and are still defined) will generate the response of the cart's position to the same impulsive disturbance we have been considering. P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q ((M + m)*m*g*l)*s^2/q - b*m*g*l*s/q); T2 = feedback(1,P_pend*C)*P_cart; t = 0:0.01:5; impulse(T2, t); title('Response of Cart Position to an Impulse Disturbance under PID Control: Kp = 100, Ki = 1, Kd = 20');

As you can see, the cart moves in the negative direction with approximately constant velocity. Therefore, although the PID controller stabilizes the angle of the pendulum, this design would not be feasible to implement on an actual physical system. Suspension: PID Controller Design Key MATLAB commands used in this tutorial are: tf , feedback , step , rlocus Contents

Adding a PID controller Plotting the closed-loop response Choosing the gains for the PID controller From the main problem, the dynamic equations in transfer function form are the following: (1) (2) where, (3) and the system schematic is the following where F(s)G1(s) = G2(s).

For the original problem and the derivation of the above equations and schematic, please refer to the Suspension: System Modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to the main problem for the details of getting those commands). m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000;

b1 = 350; b2 = 15020;

nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (b1*k2)+(b2*k1) k1*k2]; G1=tf(nump,denp); (m1*(k1+k2))+(m2*k1)+(b1*b2)

num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (b1*k2)+(b2*k1) k1*k2]; G2=tf(num1,den1); (m1*(k1+k2))+(m2*k1)+(b1*b2)

numf=num1; denf=nump; F=tf(numf,denf); Adding a PID controller Recall that the transfer function for a PID controller is: (4) where Kp is the proportional gain, Ki is the integral gain, and Kd is the derivative gain. Let's assume that we will need all three of these gains in our controller. To begin, we might start with guessing a gain for each: Kp=208025, Ki=832100 and Kd=624075. This can be implemented into MATLAB by adding the following code into your m-file: Kd = 208025; Kp = 832100; Ki = 624075; C = pid(Kp,Ki,Kd); Now let's simulate the response of the system (the distance X1-X2) to a step disturbance on the road. From the schematic above we can find the transfer function from the road disturbance W to the output(X1-X2), and simulate: sys_cl=F*feedback(F*G1,C);

Plotting the closed-loop response Now we have created the closed-loop transfer function in MATLAB that will represent the plant, the disturbance, as well as the controller. Let's see what the closed-loop step response for this system looks like before we begin the control process. Keep in mind that we are going to use a 0.1 m high step as our disturbance, to simulate this, all we need to do is to multiply sys_cl by 0.1. Add the following code your m-file. You should see the response (X1-X2) to a step W like this: t=0:0.05:5; step(0.1*sys_cl,t) title('Closed-Loop Response to 0.1-m High Step w/ PID Controller')

From the graph, the percent overshoot is 9mm, which is larger than the 5mm requirement, but the settling time is satisfied, less than 5 seconds. To choose the proper gain that yields reasonable output from the beginning, we start with choosing a pole and two zeros for PID controller. A pole of this controller must be at zero and one of the zeros has to be very close to the pole at the origin, at 1. The other zero, we will put further from the first zero, at 3, actually we can adjust the second-zero's position to get the system to fulfill the requirement. Add the following command in the m-file, so you can adjust the second-zero's location and choose the gain to have a rough idea what gain you should use for Kd, Kp, and Ki. z1=1; z2=3;

p1=0; C = ((s+z1)*(s+z2))/(s+p1); rlocus(C*G1) title('root locus with PID controller')

Add the code [k,poles]=rlocfind(C*G1) onto the end of your m-file to help you choose a specific loop gain. After running in the command window, go to the root locus plot and select a point near those indicated by the cross marks on the plot below.

After doing this, you should see the following output in the MATLAB command window. Select a point in the graphics window

selected_point =

-4.3957 + 9.3168i

k =

4.7992e+04

poles =

1.0e+02 * -2.0116 -0.0415 + 0.0932i -0.0415 - 0.0932i -0.0729 -0.0059

Note that the values returned in your MATLAB command window may not be exactly the same, but should at least have the same order of magnitude. We will explain the root locus method in more detail in the Suspension: Root Locus Controller Design page. Choosing the gains for the PID controller Now that we have the closed-loop transfer function, controlling the system is simply a matter of tuning the Kd, Kp, and Ki gains. From the figure above, we can see that the system has larger damping than required, but the settling time is very short. This response still doesn't satisfy the 5% overshoot requirement. As mentioned before, this can be rectified by adjusting the Kp, Ki and Kd gains to obtain a better response. Let's increase Kp, Ki, and KD by a factor of 2 to see what will happen. Go back to your m-file and multiply Kp, Ki, Kd by 2 and then rerun the program, you should get the following plot. Kd=2*Kd; Kp=2*Kp; Ki=2*Ki; C=pid(Kp,Ki,Kd); sys_cl=F*feedback(F*G1,C); step(0.1*sys_cl,t) title('Closed-Loop Controller') Response to 0.1-m High Step w/ High-Gain PID

To compare this graph with the graph of low-gain PID controller, you can change the axis: axis([0 5 -.01 .01])

Now we see that the percent overshoot and settling time meet the requirements of the system. The percent overshoot is about 5% of the input's amplitude and settling time is 2 seconds which is less than the 5 second requirement. For this problem, it turns out that the PID design method adequately controls the system. This can been seen by looking at the root locus plot. Such a task can be achieved by simply changing only the gains of a PID controller. Feel free to play around with all three of the parameters, Kp, Ki, and Kd, as we suggested, but you will most likely get the response to have either a large percent overshoot or a long settling time. DC Motor Position: PID Controller Design Key MATLAB commands used in this tutorial are: tf , step , feedback Contents Proportional control PI control PID control From the main problem, the open-loop transfer function of the DC Motor is given as follows. (1)

The structure of the control system has the form shown in the figure below.

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Position: System Modeling page. For a 1-radian step reference, the design criteria are the following. Settling time less than 0.040 seconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input Now let's design a PID controller and add it into the system. First create a new m-file and type in the following commands (refer to main problem for the details of getting these commands). J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2)); Recall that the transfer function for a PID controller has the following form. (2) Proportional control Let's first try using a proportional controller with gain ranging from 1 to 21. An array of LTI models, each with a different proportional gain, can be built using a for loop. The closed-loop transfer functions can be

generated using the feedback command. Add the following code to the end of your m-file and run it in the MATLAB command window: Kp = 1; for i = 1:3 C(:,:,i) = pid(Kp); Kp = Kp + 10; end sys_cl = feedback(C*P_motor,1); Now let's see what the step responses look like. Add the following code to the end of your m-file and again run it in the command window. You should generate the plot shown in the figure below. t = 0:0.001:0.2; step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Reference with Different Values of K_p') legend('K_p = 1', 'K_p = 11', 'K_p = 21')

Let's also consider the system's response to a step disturbance. In this case, we will assume a reference of zero and look at the how the system responds to the disturbance by itself. The feedback command can still be employed for generating the closed-loop transfer function where there is still negative feedback, however, now only the plant transfer function P(s) is in the forward path and the controller C(s) is considered to be in the feedback path. Refer back to the block diagram at the top of this page to see the structure of the system. Add the following to the end of your m-file and run it in the command window. You should generate the plot shown in the figure below. dist_cl = feedback(P_motor,C); step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Disturbance with Different Values of K_p') legend('K_p = 1', 'K_p = 11','K_p = 21')

The above plots show that the system has no steady-state error in response to the step reference by itself, no matter the choice of proportional gain Kp. This is due to the fact that the plant has an integrator, that is, the system is type 1. However, the system has significant steady-state error when the disturbance is added. Specifically, the response due to the reference and disturbance applied simultaneously is equal to the sum of the two graphs shown above. This follows from the property of superposition that holds for linear systems. Therefore, to have zero steady-state error in the presence of a disturbance, we need the

disturbance response to decay to zero. The larger the value of Kp the smaller the steady-state error is due to the disturbance, but it never reaches zero. Furthermore, employing increasingly larger values of Kp has the adverse effect of increasing the overshoot and settle time as can be seen from the step reference plot. Recall from the DC Motor Position: System Modeling page that adding an integral term will eliminate the steady-state error and a derivative term can reduce the overshoot and settling time. PI control Let's first try a PI controller to get rid of the steady-state error due to the disturbance. We will set Kp = 21 and test integral gains Ki ranging from 100 to 500. Change your m-file to the following and run in the command window. You should generate a figure like the one shown below. Kp = 21; Ki = 100; for i = 1:5 C(:,:,i) = pid(Kp,Ki); Ki = Ki + 200; end

sys_cl = feedback(C*P_motor,1); t = 0:0.001:0.4; step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Reference with K_p = 21 and Different Values of K_i') legend('K_i = 100', 'K_i = 300', 'K_i = 500')

Now let's see what happened to the step disturbance response. Change the following commands in your mfile and re-run in the command window. You should generate a plot like the one shown in the figure below. dist_cl = feedback(P_motor,C); step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response Values of K_i') to a Step Disturbance with K_p = 21 and Different

legend('K_i = 100',

'K_i = 300',

'K_i = 500')

The integral control has reduced the steady-state error to zero, even when a step disturbance is present; that was the goal for adding the integral term. For the response to the step reference, all of the reponses look similar with the amount of oscillation increasing slightly as Ki is made larger. However, the response due to the disturbance changes significantly as the integral gain Ki is changed. Specifically, the larger the value of Ki employed, the faster the error decays to zero. We will choose Ki = 500 because the error due to the disturbance decays to zero quickly, even though the response to the reference has a longer settling time and more overshoot. We will attempt to reduce the settling time and overshoot by adding a derivative term to the controller. PID control Adding a derivative term to the controller means that we now have all three terms of the PID controller. We will investigate derivative gains Kd ranging from 0.05 to 0.25. Go back to the m-file and make the following changes. Running the altered m-file will generate a graph like the one shown below. Kp = 21; Ki = 500; Kd = 0.05;

for i = 1:3

C(:,:,i) = pid(Kp,Ki,Kd); Kd = Kd + 0.1; end

sys_cl = feedback(C*P_motor,1); t = 0:0.001:0.1; step(sys_cl(:,:,1), sys_cl(:,:,2), sys_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Reference with K_p = 21, K_i = Different Values of K_d') legend('K_d = 0.05', 'K_d = 0.15', 'K_d = 0.25') 500 and

Let's see what happened to the step disturbance response, change the following commands in your m-file and re-run at the command line. dist_cl = feedback(P_motor,C); t = 0:0.001:0.2;

step(dist_cl(:,:,1), dist_cl(:,:,2), dist_cl(:,:,3), t) ylabel('Position, \theta (radians)') title('Response to a Step Disturbance with K_p = 21, K_i = 500 and Different values of K_d') legend('K_d = 0.05', 'K_d = 0.15', 'K_d = 0.25')

It looks like when Kd = 0.15, we can meet our design requirements. To determine the precise characteristics of the step response you can use the right-click menu of the step response plot, or you can use the MATLAB command stepinfo as shown below. stepinfo(sys_cl(:,:,2)) ans = RiseTime: 0.0046 SettlingTime: 0.0338 SettlingMin: 0.9183 SettlingMax: 1.1211 Overshoot: 12.1139 Undershoot: 0

Peak: 1.1211 PeakTime: 0.0121 From the above, we see that the response to a step reference has a settling time of roughly 34ms (< 40 ms), overshoot of 12% (< 16%), and no steady-state error. Additionally, the step disturbance response also has no steady-state error. So now we know that if we use a PID controller with Kp = 21, Ki = 500, and Kd = 0.15, all of our design requirements will be satisfied. DC Motor Speed: PID Controller Design Key MATLAB commands used in this tutorial are: tf , step , feedback Contents Proportional control PID control Tuning the gains From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer function of the DC Motor are the following. (1) (2) (3) The structure of the control system has the form shown in the figure below.

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page. For a 1-rad/sec step reference, the design criteria are the following.

Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1% Now let's design a controller using the methods introduced in the Introduction: PID Controller Design page. Create a new m-file and type in the following commands. J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2); Recall that the transfer function for a PID controller is: (4) Proportional control Let's first try employing a proportional controller with a gain of 100, that is, C(s) = 100. To determine the closed-loop transfer function, we use thefeedback command. Add the following code to the end of your m-file. Kp = 100; C = pid(Kp); sys_cl = feedback(C*P_motor,1); Now let's examine the closed-loop step response. Add the following commands to the end of your m-file and run it in the command window. You should generate the plot shown below. You can view some of the system's characteristics by right-clicking on the figure and choosing Characteristics from the resulting menu. In the figure below, annotations have specifically been added for Settling Time, Peak Response, and Steady State. t = 0:0.01:5; step(sys_cl,t) grid title('Step Response with Proportional Control')

From the plot above we see that both the steady-state error and the overshoot are too large. Recall from the Introduction: PID Controller Design page that increasing the proportional gain Kp will reduce the steady-state error. However, also recall that increasing Kp often results in increased overshoot, therefore, it appears that not all of the design requirements can be met with a simple proportional controller. This fact can be verified by experimenting with different values of Kp. Specifically, you can employ the SISO Design Tool by entering the commandsisotool(P_motor) then opening a closed-loop step response plot from the Analysis Plots tab of the Control and Estimation Tools Managerwindow. With the Real-Time Update box checked, you can then vary the control gain in the Compensator Editor tab and see the resulting effect on the closed-loop step response. A little experimentation verifies what we anticipated, a proportional controller is insufficient for meeting the given design requirements; derivative and/or integral terms must be added to the controller. PID control Recall from the Introduction: PID Controller Design page adding an integral term will eliminate the steady-state error to a step reference and a derivative term will often reduce the overshoot. Let's try a PID controller with small Ki and Kd. Modify your m-file so that the lines defining your control are as follows. Running this new m-file gives you the plot shown below. Kp = 75; Ki = 1;

Kd = 1; C = pid(Kp,Ki,Kd); sys_cl = feedback(C*P_motor,1); step(sys_cl,[0:1:200]) title('PID Control with Small Ki and Small Kd')

Inspection of the above indicates that the steady-state error does indeed go to zero for a step input. However, the time it takes to reach steady-state is far larger than the required settling time of 2 seconds. Tuning the gains In this case, the long tail on the step response graph is due to the fact that the integral gain is small and, therefore, it takes a long time for the integral action to build up and eliminate the steady-state error. This process can be sped up by increasing the value of Ki. Go back to your m-file and change Kito 200 as in the following. Rerun the file and you should get the plot shown below. Again the annotations are added by right-clicking on the figure and choosing Characteristics from the resulting menu. Kp = 100; Ki = 200; Kd = 1;

C = pid(Kp,Ki,Kd); sys_cl = feedback(C*P_motor,1); step(sys_cl, 0:0.01:4) grid title('PID Control with Large Ki and Small Kd')

As expected, the steady-state error is now eliminated much more quickly than before. However, the large Ki has greatly increased the overshoot. Let's increase Kd in an attempt to reduce the overshoot. Go back to the m-file and change Kd to 10 as shown in the following. Rerun your m-file and the plot shown below should be generated. Kp = 100; Ki = 200; Kd = 10; C = pid(Kp,Ki,Kd); sys_cl = feedback(C*P_motor,1); step(sys_cl, 0:0.01:4)

grid title('PID Control with Large Ki and Large Kd')

As we had hoped, the increased Kd reduced the resulting overshoot. Now we know that if we use a PID controller with Kp = 100, Ki = 200, and Kd = 10, all of our design requirements will be satisfied. Cruise Control: PID Controller Design Key MATLAB commands used in this tutorial are: tf , step , feedback Contents System model and parameters Performance specifications PID overview Proportional control PI control

PID control System model and parameters The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System Modeling page for the derivation. (1) The parameters used in this example are as follows: (m) (b) (r) vehicle mass damping coefficient reference speed 1000 kg 50 N.s/m 10 m/s

Performance specifications Rise time < 5 s Overshoot < 10% Steady-state error < 2% PID overview The block diagram of a typical unity feedback system is shown below.

Recall from the Introduction: PID Controller Design page, the transfer function of a PID controller is (2) We can define a PID controller in MATLAB using the transfer function directly: Kp = 1; Ki = 1; Kd = 1;

s = tf('s'); C = Kp + Ki/s + Kd*s

C =

s^2 + s + 1 ----------s

Continuous-time transfer function.

Alternatively, we may use MATLAB's pid controller object to generate an equivalent continuous time controller as follows: C = pid(Kp,Ki,Kd) C =

1 Kp + Ki * --- + Kd * s s

with Kp = 1, Ki = 1, Kd = 1

Continuous-time PID controller in parallel form.

Proportional control The first thing to do in this problem is to find a closed-loop transfer function with a proportional control (C = Kp) added. By reducing the unity feedback block diagram, the closed-loop transfer function with a proportional controller becomes: (3) Recall from the Introduction: PID Controller Design page, a proportional controller, Kp, decreases the rise time, which is desirable in this case.

For now, use Kp equal 100 and a reference speed of 10 m/s. Create a new m-file and enter the following commands. m = 1000; b = 50; r = 10;

s = tf('s'); P_cruise = 1/(m*s + b);

Kp = 100; C = pid(Kp);

T = feedback(C*P_cruise,1)

t = 0:0.1:20; step(r*T,t) axis([0 20 0 10]) T =

100 -----------1000 s + 150

Continuous-time transfer function.

Note that we have used the MATLAB feedback command to simplify the block diagram reduction of the closed-loop system. Please verify for yourself that the result agrees with the closed-loop transfer function, T, derived above. Running the m-file in MATLAB should give you the step response above. As you can see from the plot, neither the steady-state error nor the rise time satisfy our design criteria. You can increase the proportional gain, Kp, to reduce the rise time and the steady-state error. Change the existing m-file so that Kp equals 5000 and rerun it in the MATLAB command window. You should see the following plot. Kp = 5000; C = pid(Kp); T = feedback(C*P_cruise,1);

step(r*T,t) axis([0 20 0 10])

The steady-state error is now essentially zero, and the rise time has been reduced substantially. However, this response is unrealistic because a real cruise control system generally can not change the speed of the vehicle from 0 to 10 m/s in less than 0.5 seconds due to power limitations of the engine and drivetrain. Actuator limitations are very frequently encountered in practice in control systems engineering, and consequently, the required control action must always be considered when proposing a new controller. We will discuss this issue much more in subsequent tutorials. The solution to this problem in this case is to choose a lower proportional gain, Kp, that will give a reasonable rise time, and add an integral controller to eliminate the steady-state error. PI control The closed-loop transfer function of this cruise control system with a PI controller (C = Kp + Ki/s) is: (4) Recall from the Introduction: PID Controller Design page, an addition of an integral controller to the system eliminates the steady-state error. For now, let Kp equal 600 and Ki equal 1 and see what happens to the response. Change your m-file to the following. Kp = 600; Ki = 1;

C = pid(Kp,Ki);

T = feedback(C*P_cruise,1);

step(r*T,t) axis([0 20 0 10])

Now adjust both the proportional gain, Kp, and the integral gain, Ki, to obtain the desired response. When you adjust the integral gain, Ki, we suggest you to start with a small value since a large Ki can destabilize the response. When Kp equals 800 and Ki equals 40, the step response will look like the following: Kp = 800; Ki = 40; C = pid(Kp,Ki);

T = feedback(C*P_cruise,1);

step(r*T,t) axis([0 20 0 10])

PID control For this particular example, no implementation of a derivative controller was needed to obtain the required output. However, you might want to see how to work with a PID control for the future reference. The closed-loop transfer function for this cruise control system with a PID controller (C = Kp + Ki/s + Kd*s) is: (5) Let Kp equal 1, Ki equal 1, and Kd equal 1 and enter the following commands into an new m-file. Kp = 1; Ki = 1; Kd = 1; C = pid(Kp,Ki,Kd);

T = feedback(C*P_cruise,1);

Plot the step response and adjust all of Kp, Kd, and Ki until you obtain satisfactory results. We will leave this as an exercise for you to work on. Suggestion: Usually choosing appropriate gains requires a trial and error process. The best way to attack this tedious process is to adjust one variable (Kp, Ki, or Kd) at a time and observe how changing one variable influences the system output. The characteristics of Kp, Ki, and Kd are summarized in the Introduction: PID Controller Design page. Cruise Control: Root Locus Controller Design Key MATLAB commands used in this tutorial are: tf , rlocus , feedback , step Contents System model System parameters Performance specifications Proportional control Lag controller System model The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System Modeling page for the derivation. (1) System parameters For this example, let's assume that the parameters of the system are (m) (b) (r) vehicle mass damping coefficient reference speed 1000 kg 50 N.s/m 10 m/s

and the block diagram of a typical unity feedback system is shown below.

Performance specifications Rise time < 5 sec

Overshoot < 10% Steady-state error < 2% Proportional control Recall from the Introduction: Root Locus Controller Design page, the root-locus plot shows the locations of all possible closed-loop poles when a single gain is varied from zero to infinity. Thus, only a proportional controller, K_P, will be considered to solve this problem. The closed-loop transfer function becomes: (2) Also, from the Introduction: Root Locus Controller Design page, we know that the MATLAB command sgrid can be used to display an acceptable region of the root-locus plot. To use the sgrid, both the damping ratio, zeta, and the natural frequency, Wn, need to be determined first. The following two equations will be used to find the damping ratio and the natural frequency: (3)

(4) where wn = Natural Frequency [rad\s] zeta = Damping Ratio Tr = Rise time [s] Mp = Maximum Overshoot One of our design criteria is to have a rise time of less than 5 seconds. From the first equation, we see that the natural frequency must be greater than 0.36. Also using the second equation, we see that the damping ratio must be greater than 0.6, since the maximum overshoot must be less than 10%. Now, we are ready to generate a root-locus plot and use the sgrid to find an acceptable region on the root-locus. Create a new m-file and enter the following commands. m = 1000; b = 50; r = 10;

s = tf('s'); P_cruise = 1/(m*s+b);

rlocus(P_cruise) axis([-0.6 0 -0.6 0.6]); sgrid(0.6, 0.36)

The two dotted lines in an angle indicate the locations of constant damping ratio (zeta=0.6); the damping ratio is greater than 0.6 in between these lines and less than 0.6 outside the lines. The semi-ellipse indicates the locations of constant natural frequency (Wn=0.36); the natural frequency is greater than 0.36 outside the semi-ellipse, and smaller than 0.36 inside. We can then find a gain to place the closed-loop poles in the desired region by employing the rlocfind command. Add the code[Kp,poles]=rlocfind(P_cruise) onto the end of your m-file to help you choose a specific loop gain. After running in the command window, you should see a prompt asking you to pick a point on the root-locus plot. Since you want to pick a point in between dotted lines (zeta>0.6) and outside the semi-ellipse (Wn>0.36), click on the real axis just outside the semi-ellipse (around -0.4) as indicated by the cross mark in the following figure.

After doing this, you should see the following output in the MATLAB command window. Select a point in the graphics window

selected_point =

-0.4002 + 0.0019i

Kp =

350.2419

poles =

-0.4002

Note that the value returned from your MATLAB command window may not be exactly the same, but should at least have the same order of magnitude. This returned value can be used as the gain for the compensator and the closed-loop step response can be generated as follows. Kp = 350.2419; sys_cl = feedback(Kp*P_cruise,1); t = 0:0.1:20; step(r*sys_cl,t)

With the gain Kp you just chose, the rise time and the overshoot criteria have been met; however, a steadystate error of more than 10% remains. Lag controller

To reduce the steady-state error, a lag controller will be added to the system. The transfer function of the lag controller is: (5) The closed-loop transfer function (not including Kp) now becomes: (6) Finally, including the loop gain Kp, the closed-loop transfer function becomes: (7) If you read the "Lag or Phase-Lag Compensator using Root-Locus" section in the Lead and Lag Compensator Design page, the pole and the zero of a lag controller need to be placed close together. Also, it states that the steady-state error will be reduced by a factor of zo/po. For these reasons, let zo equal 0.3 and po equal 0.03. Create a new m-file, and enter the following commands. zo = 0.3; po = 0.03;

s = tf('s'); C_lag = (s+zo)/(s+po);

rlocus(C_lag*P_cruise); axis([-0.6 0 -0.4 0.4]) sgrid(0.6,0.36);

Using the rlocfind command again, we can choose a new loop gain Kp. Enter the code [Kp,poles]=rlocfind(C_lag*P_cruise) into the command window and click on the real axis around -0.4 as shown in the following figure.

After doing this, you should see the following output in the MATLAB command window. Select a point in the graphics window

selected_point =

-0.4002 - 0.0012i

Kp =

1.2936e+03

poles =

-0.9733 -0.4003

We can then generate the new closed-loop step response as follows. Kp = 1293.6; sys_cl = feedback(Kp*C_lag*P_cruise,1); t = 0:0.1:20; step(r*sys_cl,t) axis([0 20 0 12])

As you can see, the steady-state error has been reduced to near zero. The overshoot is a result of the zero added in the lag controller. For now all of the design criteria have been met and no further iterations are

needed; however, you should experiment with different zo and po values to see what their effect is on the closed-loop system response. DC Motor Speed: Root Locus Controller Design Key MATLAB commands used in this tutorial are: tf , sisotool Contents Drawing the open-loop root locus Finding the loop gain Adding a lag controller Finding the loop gain with a lag controller From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer function of the DC Motor are the following. (1) (2) (3) The structure of the control system has the form shown in the figure below.

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page. For a 1-rad/sec step reference, the design criteria are the following. Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1%

Now let's design a controller using the methods introduced in the Introduction: Root Locus Controller Design page. Create a new m-file and type in the following commands. J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2); Drawing the open-loop root locus The main idea of root locus design is to predict the closed-loop response from the root locus plot which depicts possible closed-loop pole locations and is drawn from the open-loop transfer function. Then by adding zeros and/or poles via the controller, the root locus can be modified in order to achieve a desired closed-loop response. We will use for our design the SISO Design Tool graphical user interface. This tool allows the you to graphically tune the controller via the root locus plot. Let's first view the root locus for the uncompenstated plant. This is accomplished by adding the command sisotool('rlocus', P_motor) to the end of your m-file and running the file at the command line. Two windows will initially open, one is the SISO Design Task which will open with the root locus of the uncompensated plant, and the other is Control and Estimation Tool Manager which allows you to design compensators, analyze plots, etc. Right-click on the root locus plot and click on Grid. Your plot will then appear as follows.

Finding the loop gain Recall that our design requirements specify that the settling time be less than 2 seconds and that the overshoot be less than 5%. The location of the system's closed-loop poles provide information regarding the system's transient response. The SISO Designt Tool allows you to specify the region in the complex splane corresponding to specific design requirements. The provided regions correspond to a canonical second-order system, but in general are a good place to start from even for higher-order systems or systems with zeros. These desired regions can be added to the root locus plot by right-clicking on the plot and choosing Design Requirements > New from the resulting menu. You can add many design requirements including Settling time, Percent overshoot, Damping ratio, Natural frequency, and generic Region constraint.

Adding our settling time and percent overshoot requirements to the root locus plot produces the following figure.

The resulting desired region for the closed-loop poles is shown by the unshaded region of the above figure. More specifically, the two rays centered at the origin represent the overshoot requirement; the smaller the angle these rays make with the negative real-axis, the less overshoot is allowed. The vertical line at s = -2 represents the settling time requirement, where the farther to left the closed-loop poles are located the smaller the settling time is. From examination of the above figure, there are values of the loop gain that will place both closed-loop poles in the desired region. This can be seen from the fact that the two branches of the root locus are symmetric and pass through the unshaded region. Furthermore, since the closed-loop system has two poles with no zeros, placing the closed-loop poles in the shown region will guarantee satisfaction of our transient response requirements.

You can select a specific pair of closed-loop poles from the resulting figure in order to determine the corresponding loop gain that places the poles at that location. For our system, let's choose to place the closed-loop poles so that they are located on the vertical branches of the root-locus between the real axis and the damping requirement. The pink boxes on the root locus indicate the location of the closed-loop poles for the current loop gain. Clicking on the pink boxes and dragging them along the root locus to the desired location automatically modifies the controller to place the closed-loop poles at the indicated position. Let us drag a closed-loop pole to a location near -6 + 2i. The pole location will be indicated at the bottom of the window along with the corresponding damping ratio and natural frequency. Releasing the mouse button will further show at the bottom of the window the corresponding loop gain, which in this case is approximately 10. We can also generate the closed-loop step response for the system with this new gain. From the Control and Estimation Tool Manager, click on theAnalysis Plots tab and under Plot1, choose Step, a blank window titled LTI Viewer for SISO Design Task will appear. Right-click on this window and then from Systems menu choose the first item which is Closed Loop r to y (blue). The closed-loop step response will then appear in the figure. You can also identify some characteristics of the step response. Specifically, right-click on the figure and under Characteristics choose Settling Time. Then repeat for Steady State. Your figure will appear as shown below.

From inspection of the above, one can see that there is no overshoot and the settling time is less than one second, therefore, the overshoot and settling time requirements are satisfied. However, we can also observe that the steady-state error is approximately 50%. If we increase the loop gain to reduce the steady-state error, the overshoot will become too large. You can see this for yourself by graphically moving the closedloop poles vertically upward along the root locus, this corresponds to increasing the loop gain. The step response plot will change automatically to reflect the modified loop gain. We will attempt to add a lag controller to reduce the steady-state error requirement while still satisfying the transient requirements. Adding a lag controller In the above we saw that the overshoot and settling time criteria were met with the proportional controller, but the steady-state error requirement was not. A lag compensator is one type of controller known to be

able to reduce steady-state error. However, we must be careful in our design to not increase the settling time too much. Let's first try adding a lag compensator of the form given below. (4) We can use the SISO Design Tool to design our lag compensator. To make the SISO Design Tool have a compensator parameterization corresponding to the one shown above, click on the Edit menu at the top of the Control and Estimation Tools Manager window and choose SISO Tool Preferences. Then From the Options tab, select a Zero/pole/gain parameterization as shown below.

You can then add the lag compensator from under the Compensator Editor tab of the Control and Estimation Tools Manager window. Specifically, right-click in the Dynamics section of the window and select Add Pole/Zero > Lag. Then enter the Real Zero and Real Pole locations as shown in the following figure.

Note that the phase lag contributed by the compensator and the frequency where it is located are updated to match the pole and zero locations chosen. Finding the loop gain with a lag controller Notice how the root locus has changed to reflect the addition of the pole and zero from the lag compensator as shown in the figure below. We can again choose closed-loop pole locations to attempt to achieve our desired transient requirements. Let's attempt to place two of the closed-loop poles in our desired region near the boundary of the overshoot requirement. For example, a loop gain of approximately 20 will place the poles at the positions shown in the figure below.

The corresponding closed-loop step response will then update automatically to match the figure shown below.

As you can see, the response is not quite satisfactory even though two of the closed-loop poles were placed in the desired region. The reason for this is because the closed-loop system no longer has the form of a canonical second-order system. Specifically, there is a third pole on the real axis indicated in the root locus plot above that is outside of the desired region. The fact that this third pole is to the right of the two conjugate poles placed above means that it will slow the system response down, that is why the settling time requirement is no longer met. Additionally, the overshoot requirement is met easily even though the two conjugate poles are near the edge of the allowed region. This is due again to the third pole which is well damped and tends to dominate the response because it is "slower" than the other poles. What this means is that we can further increase the loop gain such that the conjugate poles move beyond the diagonal lines while still meeting the overshoot requirement.

You can now return to the root locus plot and graphically move the conjugate poles farther away from the real axis; this corresponds to increasing the loop gain. Before you do this, however, you likely need to change the limits on the imaginary axis so that you can move the poles a sufficient distance. In order to change these limits, double-click on the root locus plot to open the Property Editor, then click on the Limits tab and change the imaginary axis limits to [-15,15] as shown below.

Experiment with different gains (closed-loop pole locations) until you achieve the desired response. Below is the root locus with a loop gain of 44 and the corresponding closed-loop step response.

Now the settling time is less than 2 seconds and the steady-state error and overshoot requirements are still met. As you can see, the root locus design process requires some trial and error. The SISO Design Tool is very helpful in this process. Using the SISO Design Tool, it is very easy to tune your controller and immediately see the effect on the root locus and various analysis plots, like the closed-loop step response. If we had not been able to get a satisfactory response by tuning the loop gain, we could have tried moving the pole and zero of the lag compensator or we could have tried a different type of dynamic compensator (additional poles and/or zeros).

DC Motor Position: Root Locus Controller Design Key MATLAB commands used are: tf , rlocus , sgrid , pole , minreal , feedback , step Contents Drawing the open-loop root locus Model reduction in this tutorial

Integral control PI control PID control Determining gain using rlocfind command From the main problem, the open-loop transfer function of the DC Motor is given as follows. (1) The structure of the control system has the form shown in the figure below.

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Position: System Modeling page. With a 1-radian step reference, the design criteria are the following. Settling time less than 0.040 seconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input Now let's design a controller using the root locus method. Create a new m-file and type in the following commands (refer to main problem for the details of getting these commands). J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6;

s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2)); Drawing the open-loop root locus The main idea of root locus design is to predict the closed-loop response from the root locus plot which depicts possible closed-loop pole locations and is drawn from the open-loop transfer function. Then by adding zeros and/or poles via the controller, the root locus can be modified in order to achieve a desired closed-loop response. Let's first view the root locus for the plant. Add the following commands to the end of your m-file and run it in the MATLAB command window to generate a plot like the one shown below. rlocus(P_motor) title('Root Locus - P Control') sgrid(.5, 0) sigrid(100)

The commands sgrid and sigrid are functions that mark regions of the complex plane corresponding to certain parameters. sgrid is a function in the Control System Toolbox; however, to use sigrid you will have to download it here. The variables in the sgrid command are the damping ratio (zeta = 0.5) and the natural frequency (Wn = 0). The variable in the sigrid command is the sigma term. The zeta and sigma used above correspond to an overshoot of 16% and a settling time of 0.040 seconds,

respectively, for a canonical second-order system. Even though our motor transfer function is third order, it will be explained that these second-order based definitions will work well. No value is given for Wn since we have no requirement on rise time. From the above figure, the two open-loop poles near the origin cannot be distinguished because the scale of the axes is set to show the third pole which is much farther to the left than the other two poles. The MATLAB command pole can be employed to determine the exact values of the open-loop poles. poles = pole(P_motor) poles = 1.0e+06 * 0 -1.4545 -0.0001 The open-loop pole located very far to the left (further than -1e6) does not affect the closed-loop dynamics unless very large gains are used. These large gains place two of the closed-loop poles in the right-half complex s-plane where the system becomes unstable. Since we will not use gains that will make the closed-loop system unstable, we can neglect this pole by performing a model reduction. Model reduction In general, the real part of a pole indicates how quickly the transient portion of the corresponding mode decays to zero (assuming negative real part). Therefore, if you have a transfer function which has one (or more) poles much farther to the left in the complex plane (more negative) than the other poles, their effect on the dynamic response will be hidden by the slower, more dominant poles. In the case of our motor position example, the transient closed-loop response for small gains will not be affected much by the openloop pole at -1.45e6. The correct way to neglect this pole in order to maintain its steady-state contribution is to keep the DC gain of the transfer function the same, as follows: (2) As shown above, the poles of the open-loop transfer function can be identified using the MATLAB command pole. The two poles that dominate are difficult to identify from above because of the scientific notation, but they can be seen more clearly by recognizing that they are the first and third elements of the resulting vector which we have named poles. poles(1), poles(3) ans = 0 ans =

-59.2260 We would then like to construct the reduced transfer function to neglect the pole at 1.45e-6 without affecting the steady-state behavior of the system. This can be accomplished with the MATLAB command minreal. Add the following commands to your m-file and re-run. poles = pole(P_motor); rP_motor = minreal(P_motor*(s/max(abs(poles)) + 1)) rP_motor =

2122 ------------s^2 + 59.23 s

Continuous-time transfer function.

You can then check that the other poles have not been affected by again using the pole command as shown below. pole(rP_motor) ans = 0 -59.2260 Now we can draw the root locus of the reduced system. Add the following commands to the end of your m-file and run it in the command window. rlocus(rP_motor) title('Root Locus - P Control') axis([ -300 100 -200 200]) sgrid(.5, 0) sigrid(100)

If you examined the original root locus near the origin, it would closely approximate the locus of the reduced transfer function shown above. We can see from this new plot that the closed-loop poles are never fast enough to meet the settling time requirement (that is, they never move to the left of the sigma= 100 vertical line). Also, recall that we need an integrator in the controller (not just in the system) to remove the steady-state error due to a constant disturbance. Integral control Now, let's try using integral control to remove the steady-state error due to a constant disturbance. Modify your m-file such that it appears like the following. Note that this adds a 1 / s term to the forward path of the system. Run this m-file and you will obtain a plot like the one shown below. C = 1/s; rlocus(C*rP_motor) title('Root Locus - I Control') axis([ -300 100 -200 200]) sgrid(.5, 0) sigrid(100)

From this root locus we can see that the closed-loop system under integral control is never stable, therefore, a different controller must be employed. PI control Now, let's modify the integral controller to a PI controller. Using PI instead of I control adds a zero to the open-loop system. We'll place this zero at s = -20. The zero must lie between the open-loop poles of the system in this case so that the closed-loop system will be stable. Change the lines defining the controller in your m-file to the following. Re-run your m-file and you will obtain a plot like the one shown below. C = (s + 20) / s; rlocus(C*rP_motor) title('Root Locus - PI Control') axis([ -300 100 -200 200]) sgrid(.5, 0) sigrid(100)

We have managed to stabilize the system and achieve zero steady-state error to a constant disturbance, but the system is still not fast enough. PID control In order to pull the root locus further to the left, to make it faster, we need to place a second open-loop zero, resulting in a PID controller. After some experimentation, we place the two PID zeros at s = -60 and s = -70. Change the lines defining the controller in your m-file to the following. Re-run your m-file and you will generate a plot like the one shown below. C = (s + 60)*(s + 70) / s; rlocus(C*rP_motor) title('Root Locus - PID Control') axis([ -300 100 -200 200]) sgrid(.5, 0) sigrid(100)

Now, we can see that two of the closed-loop poles can be placed well within both the settling time and percent overshoot requirements. The third closed-loop pole moves from the open-loop pole at s = -59.2 to the open-loop zero at s = -60. This closed-loop pole nearly cancels with the zero (which remains in the closed-loop transfer function) because they are so close together. Therefore, we can neglect its effect. Let's reduce our new model again by performing the zero-pole cancelation using the minreal command. We pass 0.1 as a tolerance parameter as follows. The root locus for this further reduced system with controller is shown below. Note how closely it resembles the root locus without the pole-zero cancelation. rsys_ol = minreal(C*rP_motor, 0.1); rlocus(rsys_ol) title('Root Locus - PID Control') axis([ -300 100 -200 200]) sgrid(.5, 0) sigrid(100)

Even though the one open-loop zero was canceled, the other open-loop zero remains in the closed-loop transfer function and cannot be neglected. The effect of an additional zero (if there is no cancellation) is in general to speed up the response and add overshoot. Therefore, we have to be conservative in picking where on the root locus we want the closed-loop poles to lie. Determining gain using rlocfind command If you recall, we need the settling time and the overshoot to be as small as possible, particularly because of the effect of the extra zero. Large damping corresponds to points on the root locus near the real axis. A fast response corresponds to points on the root locus far to the left of the imaginary axis. To find the gain corresponding to a point on the root locus, we can use the rlocfind command. Specifically, enter the command [k,poles] = rlocfind(rsys_ol) in the MATLAB command window. Then go to the plot and select a point on the root locus on left side of the loop, close to the real axis as shown below with the small + marks. This will ensure that the response will be nearly as fast as possible with minimal overshoot. These pole locations indicate that the response would have almost no overshoot if it were a canonical second-order system. Recall, however, that the presence of the zero will add some overshoot.

After doing this, you should see the following output in the MATLAB command window. Select a point in the graphics window

selected_point =

-1.3744e+002 +1.3043e+001i

k =

0.1308

poles =

1.0e+002 *

-1.3874 + 0.1323i -1.3874 - 0.1323i

Note that the values returned in your MATLAB command window may not be exactly the same, but should at least have the same order of magnitude. You can also get the step response plots for the reference and disturbance with this specific controller and loop gain by executing the following code in the command window. These commands should produce the plots shown below where the annotations to the figures are added by choosingCharacteristics from the right-click menu of each of the plots. sys_cl = feedback(k*rsys_ol,1); t = 0:0.0001:0.1; step(sys_cl, t) grid ylabel('Position, \theta (radians)') title('Response to a Step Reference with PID Control')

dist_cl = feedback(P_motor,k*C); figure;step(dist_cl, t) grid ylabel('Position, \theta (radians)') title('Response to a Step Disturbance with PID Control')

From the above, you can see that in response to a step reference the system has an overshoot of approximately 14%, a settling time just under 0.04 seconds, and no steady-state error. Also, the response to a step disturbance reaches a steady-state value of zero. Therefore, all of the design requirements have been met. In this example we placed the zeros of our compensator in order to reshape the root locus so that the closed-loop poles could be placed in the region of the complex plane that would achieve our given design requirements. It is in general helpful to understand the principles of how the root locus is drawn, however, MATLAB can be very helpful in refining the design and verifying the resulting performance. Suspension: Root Locus Controller Design Key MATLAB commands used in this tutorial are: tf , roots , rlocus , sgrid , step Contents Plotting the root locus Adding a notch filter Finding the gain from the root locus

Plotting the closed-loop response From the main problem, the dynamic equations in transfer function form are the following: (1) (2) where, (3) and the system schematic is the following where F(s)G1(s) = G2(s).

For the original problem and the derivation of the above equations and schematic, please refer to the Suspension: System Modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to the main problem for the details of getting those commands). m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020;

nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (b1*k2)+(b2*k1) k1*k2]; G1=tf(nump,denp); (m1*(k1+k2))+(m2*k1)+(b1*b2)

num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (b1*k2)+(b2*k1) k1*k2]; G2=tf(num1,den1); (m1*(k1+k2))+(m2*k1)+(b1*b2)

numf=num1; denf=nump; F=tf(numf,denf); We are now ready to design a controller using the root locus design method. First let's see what the open-loop poles of the system are: R = roots(denp) R = -23.9758 +35.1869i -23.9758 -35.1869i -0.1098 + 5.2504i -0.1098 - 5.2504i Therefore, the dominant poles are the roots -0.1098+/-5.2504i, which are close to the imaginary axis with a small damping ratio. Plotting the root locus The main idea of root locus design is to estimate the closed-loop response from the open-loop root locus plot. By adding zeros and/or poles to the original system (adding a compensator), the root locus and thus the closed-loop response will be modified. Let's first view the root locus for the plant. In your m-file, add the following command and then run the file, you should get the root locus plot below: rlocus(G1) z=-log(0.05)/sqrt(pi^2+(log(0.05)^2))

sgrid(z,0) z = 0.6901

Note from the specification, we required the overshoot,

, to be less than 5% and damping ratio, , can

be find from approximation damping ratio equation, . The command sgrid is used to overlay the desired percent overshoot line on the close-up root locus; you can find more information from commands list. From the plot above, we see that there are two pair of poles and zeros that are very close together. These poles and zeros are almost on the imaginary axis, they might make the bus system marginally stable, which might cause a problem. We have to make all of the poles and zeros move into the left-half plane as far as possible to avoid an unstable system. We have to put two zeros very close to the two poles on the imaginary axis of uncompensated system for pole-and-zero cancellation. Moreover, we will put another two poles further to the left on the real axis to get fast response. Adding a notch filter We will probably need two zeros near the two poles on the complex axis to draw the root locus, leading those poles to the compensator zeros instead of to the plant zeros on the imaginary axis. We'll also need two poles placed far to the left to pull the locus to the left. It seems that a notch filter (2-lead controller)

will probably do the job. Let's try putting the poles at 30 and 60 and the zeros at 3+/-3.5i. In your m-file add the following lines of code: z1=3+3.5i; z2=3-3.5i; p1=30; p2=60; numc=conv([1 z1],[1 z2]); denc=conv([1 p1],[1 p2]); C=tf(numc,denc);

rlocus(C*G1)

Now let's change the axis to see the details of the root locus. axis([-40 10 -30 30]) z=-log(0.05)/sqrt(pi^2+(log(0.05)^2)) sgrid(z,0)

z = 0.6901

Finding the gain from the root locus Now that we have moved the root locus across the 5% damping ratio line, we can choose a gain that will satisfy the design requirements. Recall that we want the settling time and the overshoot to be as small as possible. Generally, to get a small overshoot and a fast response, we need to select a gain corresponding to a point on the root locus near the real axis and far from the imaginary axis or the point that the root locus crosses the desired damping ratio line. But in this case, we need the cancellation of poles and zeros near the imaginary axis, so we need to select a gain corresponding to a point on the root locus near the zeros and percent overshoot line. There is a method to do this with the rlocfind command in MATLAB. Add the code[k,poles]=rlocfind(C*G1) onto the end of your m-file to help you choose a specific loop gain. After running in the command window, go to the root locus plot and select a point near those indicated by the cross marks on the plot below.

After doing this, you should see the following output in the MATLAB command window. Select a point in the graphics window

selected_point =

-2.8555 + 3.6335i

k =

1.0030e+08

poles =

1.0e+02 * -0.6321 + 5.9650i -0.6321 - 5.9650i -0.0296 + 0.1306i -0.0296 - 0.1306i -0.0292 + 0.0368i -0.0292 - 0.0368i

Note that the value returned from your MATLAB command window may not be exactly the same, but should at least have the same order of magnitude. This returned value can be used as the gain for the compensator. Recall that the schematic of the system is the following:

and the closed-loop transfer function can be derived as following: k = 1.0030e+08; sys_cl=F*feedback(G1,k*C); Plotting the closed-loop response Let's see what the closed-loop step response looks like with this compensator. Keep in mind that we are going to use a 0.1-m high step as the disturbance. To simulate this, simply multiply sys_cl by 0.1. Add the following commands into the m-file and put % marks in front of all rlocus andrlocfind commands.

t=0:0.01:2; step(0.1*sys_cl,t) title('Closed-Loop Response to 0.1-m High Step w/ Notch Filter')

From this plot we see that when the bus encounters a 0.1 m step on the road, the maximum deviation of the bus body from the wheel (or the road) is about 3.75 mm, and the oscillations settle in 2 seconds. Thus this response is satisfactory. Inverted Pendulum: Root Locus Controller Design Key MATLAB commands used are: tf , rlocus , pole , zero , zpk , feedback , impulse Contents System structure Root locus design PID control What happens to the cart's position? In this page we will design a controller for the inverted pendulum system using the root locus design method. In the design process we will assume a single-input, single-output plant as described by the in this tutorial

following transfer function. Otherwise stated, we will attempt to control the pendulum's angle without regard for the cart's position.

(1) where, (2) More specifically, the controller will attempt to maintain the pendulum vertically upward when the cart is subjected to a 1-Nsec impulse. Under these conditions, the design criteria are: Settling time of less than 5 seconds Pendulum should not move more than 0.05 radians away from the vertical For the original problem setup and the derivation of the above transfer function, please consult the Inverted Pendulum: System Modeling page. System structure The structure of the controller for this problem is a little different than the standard control problems you may be used to. Since we are attempting to control the pendulum's position, which should return to the vertical after the initial disturbance, the reference signal we are tracking should be zero. This type of situation is often referred to as a regulator problem. The external force applied to the cart can be considered as an impulsive disturbance. The schematic for this problem is depicted below.

You may find it easier to analyze and design for this system if we first rearrange the schematic as follows.

The resulting transfer function for the closed-loop system from an input of force pendulum angle is then determined to be the following. (3)

to an output of

Before we begin designing our controller, we first need to define our plant within MATLAB. Create a new m-file and type in the following commands to create the plant model (refer to the main problem for the details of getting these commands). M = 0.5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3; q = (M+m)*(I+m*l^2)-(m*l)^2; s = tf('s'); P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q b*m*g*l/q); Root locus design We will now begin to design a controller for our system employing a root locus design method. We can use the MATLAB command rlocus for generating the root locus plots. Adding the following commands to your m-file and running it in the MATLAB command window will create the root locus plot shown below. This plot displays all possible closed-loop pole locations as a simple proportional control gain is varied from 0 to infinity. The root locus is the same whether the multiplicative gain is in the forward or feedback path of the closed-loop system.

rlocus(P_pend) title('Root Locus of Plant (under Proportional Control)')

As you can see, one of the branches of the root locus is entirely in the right-half of the complex -plane. This means that no matter the choice of gain , there will always be a closed-loop pole in the right-half plane making the system's impulse response unstable. To solve this problem, we need to add a pole at the origin (an integrator) via the controller to cancel the plant zero at the origin. This addition will produce two closed-loop poles in the right-half plane. In our subsequent design we can then modify our controller to draw these poles into the left-half plane, thereby stabilizing the closed-loop system. Modifying your m-file with the following commands and re-running in the MATLAB command window will produce the root locus plot shown below. C = 1/s; rlocus(C*P_pend) title('Root Locus with Integral Control')

Let's also examine the locations of the system's open-loop poles and zeros so that we may begin to think about how to draw the root locus branches into the left-half plane. Entering the following commands into the MATLAB command window will generate the following output. zeros = zero(C*P_pend) poles = pole(C*P_pend) zeros = 0 poles = 0 5.5651 -5.6041 -0.1428 As you can see, there are four poles and only one zero. This means that the root locus will have three asymptotes: one along the real axis in the negative direction, and the other two at 120 degree angles to this one.

This configuration is also unsatisfactory because we still have branches of the root locus that are entirely in the right-half complex plane. In general, we can pull the branches of our root locus to the left in the complex plane by adding zeros to our system. Adding a zero to our controller will reduce the number of asymptotes from three to two. These two asymptotes will be parallel to the imaginary axis and will intersect the real axis at the location calculated from the following expression. (4) Therefore, for our system as described so far, we have the following assuming a minimum-phase zero (negative). (5) Based on the above, the farthest we can pull the asymptotes to the left in the complex plane is approximately -0.1 for a negligibly small zero. Recall that 2% settling time can be estimated from the following equation. (6) Therefore, dominant closed-loop poles with real parts that approach -0.1 will not be sufficient to meet the 5 second settling time that we require. PID control In the above discussion we demonstrated that adding a zero to our integral controller could pull the branches of the root locus to the left in the complex plane, but we were not able to the pull the dominant branches far enough to the left. A possible solution is to add yet another zero. If we place both zeros on the negative real axis between the two plant poles, then the two branches in the right-half plane will be pulled into the left-half plane and will terminate at these two zeros. Let's specifically evaluate the root locus for a controller with an integrator and zeros at -3 and -4. Note that this controller is actually a PID controller. We can create this controller within MATLAB using the zpk command which creates a model by specifying the zeros, poles, and gain of the system. Modifying your m-file with the following commands and re-running will produce the root locus plot shown below. z = [-3 -4]; p = 0; k = 1; C = zpk(z,p,k); rlocus(C*P_pend) title('Root Locus with PID Controller')

Examining the above root locus helps us to determine whether or not our given requirements can be met. Specifically, since it is desired that the settling time of the system be less than 5 seconds, the real parts of our dominant closed-loop poles should be less than approximately -4/5 = -0.8. In other words, our dominatnt closed-loop poles should be located in the complex -plane to the left of a vertical line at . Inspection of the above shows that this is possible. Since it is also desired that the pendulum not move more than 0.05 radians away from vertical, we also want to ensure that the closed-loop system has sufficient damping. Placing the dominant closed-loop poles near the real axis will increase the system's damping (small ). To find the gain corresponding to a specific point on the root locus, we can use the rlocfind command. Specifically, enter the command [k,poles] = rlocfind(C*P_pend) in the MATLAB command window. Then go to the plot and select a point on the root locus on left side of the loop, close to the real axis as shown below with the small + marks. Selecting these poles will ensure that the system settles sufficiently fast and, hopefully, that it has sufficient damping.

After doing this, you should see an output like the following in the MATLAB command window. Select a point in the graphics window

selected_point =

-3.5367 + 0.7081i

k =

20.2396

poles =

0 -85.1333 -3.5232 + 0.7086i -3.5232 - 0.7086i

Note that the values returned in your MATLAB command window may not be exactly the same, but they should at least have the same order of magnitude. Then we can check the impulse response of our closed-loop system to see if our requirements are actually met for a gain of approximately 20. Add the following commands to your m-file and re-run to generate a closed-loop impulse response like the one shown below. K = 20; T = feedback(P_pend,K*C); impulse(T) title('Response of Pendulum Angle to an Impulse Disturbance under PID Control');

Examination of the above demonstrates that all of the given requirements are met. What happens to the cart's position? At the beginning of this page, a block diagram for the inverted pendulum system was given. The diagram was not entirely complete. The block representing the response of the cart's position was not included because that variable is not being controlled. It is interesting though, to see what is happening to the cart's position when the controller for the pendulum's angle is in place. To see this we need to consider the full system block diagram as shown in the following figure.

Rearranging, we get the following block diagram.

In the above, the block loop transfer function given by the following. (7)

is the controller designed for maintaining the pendulum vertical. The closedfrom an input force applied to the cart to an output of cart position is, therefore,

Referring to the Inverted Pendulum: System Modeling page, the transfer function for follows.

is defined as

(8) where,

(9) Adding the following commands to your m-file (presuming and are still defined) will generate the response of the cart's position to the same impulsive disturbance we have been considering. P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q ((M + m)*m*g*l)*s^2/q - b*m*g*l*s/q); T2 = feedback(1,P_pend*C)*P_cart; t = 0:0.01:8.5; impulse(T2, t); title('Response of Cart Position to an Impulse Disturbance under PID Control');

As you can see, the cart's position goes unstable for this impulse disturbance. Therefore, although the PID controller stabilizes the angle of the pendulum, this design would not be feasible to implement on an actual physical system. Aircraft Pitch: Frequency Domain Methods for Controller Design Key MATLAB commands used are: tf , step , feedback , pole , margin , stepinfo in this tutorial

Contents Open-loop response Closed-loop response Lead compensator From the main problem, the open-loop transfer function for the aircraft pitch dynamics is (1) where the input is elevator deflection angle and the output is the aircraft pitch angle . For the original problem setup and the derivation of the above transfer function please refer to the Aircraft Pitch: System Modeling page For a step reference of 0.2 radians, the design criteria are the following. Overshoot less than 10% Rise time less than 2 seconds Settling time less than 10 seconds Steady-state error less than 2% Open-loop response Let's first begin by examining the behavior of the open-loop plant. Specifically, create a new m-file, and enter the following commands. Note the scaling of the step response by 0.2 to account for the fact that the input is a step of 0.2 radians (11 degrees). Running this m-file in the MATLAB command window should give you the step response plot shown below. t = [0:0.01:10]; s = tf('s'); P_pitch = (1.151*s + 0.1774)/(s^3 + 0.739*s^2 + 0.921*s); step(0.2*P_pitch,t); axis([0 10 0 0.8]); ylabel('pitch angle (rad)'); title('Open-loop Step Response'); grid

Examination of the above plot indicates that the open-loop system is unstable for a step input, that is, its output grows unbounded when given a step input. This is due to the fact that the transfer function has a pole at the origin. Closed-loop response Let's now close the loop on our plant and see if that stabilizes the system. Consider the following unity feedback architecture for our system.

The following code entered in the MATLAB command window generates the closed-loop transfer function assuming the unity-feedback architecture above and a unity-gain controller, C(s) = 1.

sys_cl = feedback(P_pitch,1) sys_cl =

1.151 s + 0.1774 ---------------------------------s^3 + 0.739 s^2 + 2.072 s + 0.1774

Continuous-time transfer function.

Examining the poles of this transfer function using the pole command as shown below, it can be seen that this closed-loop system is indeed stable since all of the poles have negative real part. pole(sys_cl) ans = -0.3255 + 1.3816i -0.3255 - 1.3816i -0.0881 Stability of this closed-loop system can also be determined using the frequency response of the open-loop system. The margin command generates the Bode plot for the given transfer function with annotations for the gain margin and phase margin of the system when the loop is closed as demonstrated below. margin(P_pitch), grid

Examination of the above demonstrates that the closed-loop system is indeed stable since the phase margin and gain margin are both positive. Specifically, the phase margin equals 46.9 degrees and the gain margin is infinite. It is good that this closed-loop system is stable, but does it meet our requirements? Add the following code to your m-file and re-run and you will generate the step response plot shown below. sys_cl = feedback(P_pitch,1); step(0.2*sys_cl), grid ylabel('pitch angle (rad)'); title('Closed-loop Step Response')

Examination of the above demonstrates that the settle time requirement of 10 seconds is not close to being met. One way to address this is to make the system response faster, but then the overshoot shown above will likely become a problem. Therefore, the overshoot must be reduced in conjunction with making the system response faster. We can accomplish these goals by adding a compensator to reshape the Bode plot of the open-loop system. The Bode plot of the open-loop system indicates behavior of the closed-loop system. More specifically, the gain crossover frequency is directly related to the closed-loop system's speed of response, and the phase margin is inversely related to the closed-loop system's overshoot. Therefore, we need to add a compensator that will increase the gain crossover frequency and increase the phase margin as indicated in the Bode plot of the open-loop system. Lead compensator A type of compensator that can accomplish both of our goals is a lead compensator. Referring to the Lead and Lag Compensators page, a lead compensator adds positive phase to the system. Additional positive phase increases the phase margin, thus, increasing the damping. The lead compensator also generally increases the magnitude of the open-loop frequency response at higher frequencies, thereby, increasing the gain crossover frequency and overall speed of the system. Therefore, the settling time should decrease as a result of the addition of a lead compensator. The general form of the transfer function of a lead compensator is the following.

(2) We thus need to find , T and K. Typically, the gain K is set to satisfy requirements on steady-state error. Since our system is already type 1 (the plant has an integrator) the steady-state error for a step input will be zero for any value of K. Even though the steady-state error is zero, the slow tail on the response can be attributed to the fact the velocity-error constant is too small. This deficiency can be addressed by employing a value of K that is greater than 1, in other words, a value of K that will shift the magnitude plot upward. Through some trial and error, we will somewhat arbitrarily choose K = 10. Running the following code in the MATLAB window will demonstrate the effect of adding this K. K = 10; margin(K*P_pitch), grid figure; sys_cl = feedback(K*P_pitch,1); step(0.2*sys_cl), grid title('Closed-loop Step Response with K = 10')

From examination of the above Bode plot, we have increased the system's magnitude at all frequencies and have pushed the gain crossover frequency higher. The effect of these changes are evident in the closedloop step response shown above. Unfortunately, the addition of the K has also reduced the system's phase margin as evidenced by the increased overshoot in the system's step response. As mentioned previously, the lead compensator will help add damping to the system in order to reduce the overshoot in the step response. Continuing with the design of our compensator, we will next address the parameter which is defined as the ratio between the zero and pole. The larger the separation between the zero and the pole the greater the bump in phase where the maximum amount of phase that can be added with a single pole-zero pair is 90 degrees. The following equation captures the maximum phase added by a lead compensator as a function of . (3) Relationships between the time response and frequency response of a standard underdamped second-order system can be derived. One such relationship that is a good approximation for damping ratios less than approximately 0.6 or 0.7 is the following. (4) While our system does not have the form of a standard second-order system, we can use the above relationship as a starting point in our design. As we are required to have overshoot less than 10%, we need our damping ratio to be approximately larger than 0.59 and thus need a phase margin greater than about 59 degrees. Since our current phase margin (with the addition of K) is approximately 10.4 degrees, an additional 50 degrees of phase bump from the lead compensator should be sufficient. Since it is known that the lead compensator will further increase the magnitude of the frequency response, we will need to add more than 50 degrees of phase lead to account for the fact that the gain crossover frequency will increase to a point where the system has more phase lag. We will somewhat arbitrarily add 5 degrees and aim for a total bump in phase of 50+5 = 55 degrees. We can then use this number to solve the above relationship for (5) From the above, we can calculate that must be less than approximately 0.10. For this value of , the following relationship can be used to determine the amount of magnitude increase that will be supplied by the lead compensator at the location of the maximum bump in phase. (6) Examining the Bode plot shown above, the magnitude of the uncompensated system equals -10 dB at approximately 6.1 rad/sec. Therefore, the addition of our lead compensator will move the gain crossover as shown below.

frequency from 3.49 rad/sec to approximately 6.1 rad/sec. Using this information, we can then calculate a value of T from the following in order to center the maximum bump in phase at the new gain crossover frequency in order to maximize the system's resulting phase margin. (7) With the values K = 10, = 0.10, and T = 0.52 calculated above, we now have a first attempt at our lead compensator. Adding the following lines to your m-file and running at the command line will generate the plot shown below demonstrating the effect of your lead compensator on the system's frequency response. K = 10; alpha = 0.10; T = 0.52; C_lead = K*(T*s + 1) / (alpha*T*s + 1); margin(C_lead*P_pitch), grid

Examination of the above demonstrates that the lead compensator increased the system's phase margin and gain crossover frequency as desired. We now need to look at the actual closed-loop step response in order to determine if we are close to meeting our requirements. Replace the step response code in your m-file with the following and re-run in the MATLAB command window.

sys_cl = feedback(C_lead*P_pitch,1); step(0.2*sys_cl), grid title('Closed-loop Step Response with K = 10, alpha = 0.10, and T = 0.52')

Examination of the above demonstrates that we are close to meeting our requirements. Using the MATLAB command stepinfo as shown below we can see precisely the characteristics of the closedloop step response. stepinfo(0.2*sys_cl) ans = RiseTime: 0.2073 SettlingTime: 8.9835 SettlingMin: 0.1819 SettlingMax: 0.2240 Overshoot: 11.9781 Undershoot: 0 Peak: 0.2240

PeakTime: 0.4870 From the above, all of our requirements are met except for the overshoot which is a bit larger than the requirement of 10%. Iterating on the above design process, we arrive at the parameters K = 10, = 0.04, and T = 0.55. The performance achieved with this controller can then be verified by modifying the code in your m-file as follows. K = 10; alpha = 0.04; T = 0.55; C_lead = K*(T*s + 1) / (alpha*T*s + 1); sys_cl = feedback(C_lead*P_pitch,1); step(0.2*sys_cl), grid title('Closed-loop Step Response with K = 10, \alpha = 0.04, and T = 0.55')

Examination of the above step response demonstrates that the requirements are now met. Using the stepinfo command again more clearly demonstrates that the requirements are met. stepinfo(0.2*sys_cl)

ans = RiseTime: 0.2202 SettlingTime: 9.0427 SettlingMin: 0.1828 SettlingMax: 0.2137 Overshoot: 6.8495 Undershoot: 0 Peak: 0.2137 PeakTime: 0.5344 Therefore, the following lead compensator is able to satisfy all of our design requirements. (8)

Inverted Pendulum: Frequency Domain Methods for Controller Design Key MATLAB commands used in this tutorial are: tf , zpkdata , sisotool , feedback , impulse Contents System structure Closed-loop response without compensation Closed-loop response with compensation What happens to the cart's position? In this page we will design a controller for the inverted pendulum system using a frequency response design method. In the design process we will assume a single-input, single-output plant as described by the following transfer function. Otherwise stated, we will attempt to control the pendulum's angle without regard for the cart's position.

(1) where, (2) The controller we are designing will specifically attempt to maintain the pendulum vertically upward when the cart is subjected to a 1-Nsec impulse. Under these conditions, the design criteria are: Settling time of less than 5 seconds

Pendulum should not move more than 0.05 radians away from the vertical For the original problem setup and the derivation of the above transfer function, please consult the Inverted Pendulum: System Modeling page. Note: Applying a frequency response design approach is relatively challenging in the case of this example because the open-loop system is unstable. That is, the open-loop transfer function has a pole in the righthalf complex plane. For this reason, attempting this example is not recommended if you are just attempting to learn the basics of applying frequency response techniques. This problem is better suited for more advanced students who wish to learn about some nuances of the frequency response design approach. System structure The structure of the controller for this problem is a little different than the standard control problems you may be used to. Since we are attempting to control the pendulum's position, which should return to the vertical after the initial disturbance, the reference signal we are tracking should be zero. This type of situation is often referred to as a regulator problem. The external force applied to the cart can be considered as an impulsive disturbance. The schematic for this problem is depicted below.

You may find it easier to analyze and design for this system if we first rearrange the schematic as follows.

The resulting transfer function for the closed-loop system from an input of force pendulum angle is then determined to be the following. (3)

to an output of

Before we begin designing our controller, we first need to define our plant within MATLAB. Create a new m-file and type in the following commands to create the plant model (refer to the main problem for the details of getting these commands). M = 0.5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3; q = (M+m)*(I+m*l^2)-(m*l)^2; s = tf('s'); P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q b*m*g*l/q); As mentioned above, this system is unstable without control. We can prove this to ourselves by employing the MATLAB command zpkdata. In this case,zpkdata returns the zeros and poles for the transfer function. The added parameter 'v' returns the outputs in the form of vectors instead of cell arrays and can only be employed with single-input, single-output models. Entering the following code in the MATLAB command window generates the output shown below. [zeros poles] = zpkdata(P_pend,'v') zeros = 0 poles = 5.5651 -5.6041 -0.1428 Closed-loop response without compensation We will now examine the response of the closed-loop system without compensation before we begin to design our controller. In this example we will employ the SISO Design Tool for examining the various analysis plots rather than employing individual commands such as bode, nyquist, andimpulse.

The SISO Design Tool is an interactive tool with graphical user interface (GUI) which can be launched by the MATLAB command sisotool as shown below. sisotool('bode',P_pend)

The additional parameter 'bode' opens the SISO Design for SISO Design Task window with the bode plot of the system (which was passed to the function) as shown below.

We can then modify the system architecture being employed to reflect the fact that our controller is in the feedback path of our system as discussed above. This is accomplished from within the Control and Estimation Tools Manager window by clicking on the tab labeled Architecture. Then click on the Control Architecture button and modify the default configuration to match the form shown below.

Next we will begin to examine some of the analysis plots for our system. Recall that we can assess the closed-loop stability of our system based on the open-loop frequency response. In the case of this example, the open-loop transfer function of our system is given by the following. (4) Note, this is true even though the controller is in the feedback path of the system. In other examples we have specifically employed a Bode plot representation of the open-loop frequency response. For our system without compensation, we could employ the MATLAB code bode(P_Pend) to generate this Bode plot. Instead, we will use the SISO Design Tool that we are employing currently. The open-loop Bode plot of our system is already open, but if it weren't, or if we

wished to change the type of plot we are employing for design, we could open a new plot from under the Graphical Tuning tab of the Control and Estimation Tools Manager window as shown below.

Examination of the above Bode plot shows that the magnitude is less than 0 dB and the phase is greater than -180 degrees for all frequencies. For a minimum-phase system this would indicate that the closed-loop system is stable with infinite gain margin and infinite phase margin. However, since our system has a pole in the right-half complex plane, our system is nonminimum phase and the closed-loop system is actually unstable. We will prove this to ourselves by examining a couple of other analysis plots. In general, when dealing with nonminimum-phase systems it is preferrable to analyze relative stability using the Nyquist plot of the open-loop transfer function. The Nyquist plot is also preferred when analyzing higher-order systems. This is because the Bode plot shows frequencies that are 360 degrees apart

as being different when in fact they are the same. Since the Nyquist plot is a polar-type plot, this ambiguity is removed. In order to generate additional plots to better understand the closed-loop performance of the system, click on the Analysis Plots tab in the Control and Estimation Tools Manager window. The SISO Design Tool allows the user to view up to six plots at the same time for analysis. These plots can be viewed for a number of options, such as open-loop and closed-loop. We will view the Nyquist plot for the open-loop system and the impulse response of the closed-loop system by following the steps given below. 1. Under the Analysis Plots tab, select a Plot Type of Nyquist for Plot 1. A plot window with a blank Nyquist plot should appear. Make sure that theReal-Time Update box is checked in the bottom right corner of the plot window. 2. Select a Plot Type of Impulse for Plot 2. 3. In the Contents of Plots section of the window, check the square for Open Loop L for plot 1 and the square for Closed Loop r to y for plot 2.

Then click the Show Analysis Plot button to generate the figure shown below.

Examination of the above impulse response plot shows that the closed-loop system is unstable. This can also be verified from the open-loop Nyquist plot by applying the Nyquist stability criterion which is stated below. (5) Where is the number of closed-loop poles in the right-half plane, is the number of open-loop poles in the right-half plane, and is the number of clockwise encirclements of the point -1 by the open-loop Nyquist plot. From our previous discussion we know that our system has one open-loop pole in the right-half plane ( = 1) and from examination of the open-loop Nyquist plot above we can see that there are no encirclements of the point -1 ( = 0). Therefore, = 0 + 1 = 1 and the closed-loop system has 1 pole in the right-half plane indicating that it is indeed unstable. Closed-loop response with compensation Since the closed-loop system is unstable without compensation, we need to use our controller to stabilize the system and meet the given requirements. Our first step will be to add an integrator to cancel the zero at

the origin. To add an integrator, you may right-click on the Bode plot that is already open and choose Add Pole/Zero > Integrator from the resulting menu. The result is a figure like the one shown below.

Examination of the above shows that at small frequencies the phase plot goes to -270 degrees when in fact it should remain at -180 degrees. This behavior arises because of numerical errors associated with the pole/zero cancellation at the origin. To correct this error, exit out of the SISO Design Tool and re-open the tool with the integrator already added to the plant as shown below. sisotool('bode',P_pend*(1/s))

Since the integrator is bundled with the plant which is in the forward path, while our controller is actually in the feedback path, we will not analyze the closed-loop response of this system from within the SISO

Design Tool. However, the open-loop transfer function is unchanged by whether the controller is in the forward or feedback path, therefore, we can still use the plots of the open-loop system for analysis and design. The resulting bode plot that is generated is shown below and reflects the low frequency behavior we would expect.

Even with the addition of this integrator, the closed-loop system is still unstable. We can attempt to better understand the instability (and how to resolve it) by looking more closely at the Nyquist plot. We can generate this analysis plot in the same manner we did previously. Instead, we can also generate this plot by selecting Open-Loop Nyquist from the Analysis menu accessed from the top of the window containing our Bode plot. Following these steps generates a figure like the one given below.

Notice that the open-loop Nyquist plot now encircles the -1 point in the clockwise direction. This means that the closed-loop system now has two poles in the right-half plane ( ). Hence, the closed-loop system is still unstable. We need to add phase in order to get a counterclockwise encirclement. We will do this by adding a zero to our controller. For starters, we will place this zero at -1 and view the resulting plots. This action can be achieved graphically by right-clicking on the Bode plot as we did previously. Instead, we will add the zero from the Compensator Editor tab of the Control and Estimation Tools Manager window. Right-click in the Dynamics section of the window and select Add Pole/Zero > Real Zero from the resulting menu. By default, the location of the resulting zero is -1. The resulting window should appear as shown in the figure below.

This additional zero will change the Bode plot and Nyquist plots that are open as long as the Real-time Update box is checked. The resulting Nyquist plot should appear as shown below.

As you can see, this change did not provide enough phase. The encirclement around -1 is still clockwise. We will try adding a second zero at -1 in the same manner as was described above. The resulting Nyquist diagram is shown below.

We still have one clockwise encirclement of the -1 point. However, if we add some gain we can increase the magnitude of each point of the Nyquist plot, thereby increasing the radius of the counterclockwise circle such that it encircles the -1 point. This results in = -1 where is negative because the encirclement is counterclockwise. To achieve this, you can manually enter a new gain value in the Compensator Editor tab of the Control and Estimation Tools Manager window. Alternatively, you can modify the gain graphically from the Bode plot. Specifically, go to the Bode plot window, click on the magnitude plot, and drag the curve up until you have shifted the Nyquist plot far enough to the left that the counterclockwise circle encompasses the point -1. Note that this is achieved for a gain value of approximately 3.8. We will continue to increase the gain to a magnitude of approximately 10 as indicated at the bottom of the Bode plot window. The resulting Bode plot should appear as in the figure below.

The corresponding Nyquist plot should then appear as in the figure below.

From our previous discussion we know that = 1, now that = -1, we have = -1 + 1 = 0 closed-loop poles in the right-half plan indicating that our closed-loop system is stable. We can verify the stability of our system and determine whether or not the other requirements are met by examining the system's response to a unit impulse force disturbance. Since the integrator of our controller is currently bundled with the plant, we will exit from the SISO Design Tool and generate the closed-loop impulse response form the command line. So far, the controller we have designed has the form given below. (6) Adding the following code to your m-file will construct the closed-loop transfer function from an input of to an output of . Running your m-file at the command line will then generate an impulse response plot as shown below. K = 10; C = K*(s+1)^2/s; T = feedback(P_pend,C);

t = 0:0.01:10; impulse(T,t), grid title('Response of Pendulum Position to an Impulse Disturbance under Closed-loop Control');

From examination of the figure above, it is apparent that the response of the system is now stable. However, the pendulum position overshoots past the required limit of 0.05 radians and the settle time is on the verge of being greater than the requirement of 5 seconds. Therefore, we will now concentrate on improving the response. We can use the SISO Design Tool to see how changing the controller gain and the location of the zeros affects the system's frequency response plots. In this case, increasing the controller gain increases the system's phase margin which should help reduce the overshoot of the response. Furthermore, trial and error shows that moving one of the zeros farther to the left in the complex plane (more negative) makes the response faster. This change also increases the overshoot, but this is offset by the increase in gain. Experimentation demonstrates that the following controller satisfies the given requirements. (7) Modify your m-file as shown and re-run at the command line to generate the impulse response plot given below.

K = 35; C = K*(s+1)*(s+2)/s; T = feedback(P_pend,C); t = 0:0.01:10; impulse(T, t), grid title('Response of Pendulum Position to an Impulse Disturbance under Closed-loop Control');

Our response has met our design goals. Feel free to vary the parameters further to observe what happens. Note that it also possible to generate the system's response to an impulse disturbance from within the SISO Design Tool. Including the integrator with the controller, rather than with the plant, as is proper will result in the numerical abberation observed above, but will not significantly affect the impulse response generated by MATLAB. What happens to the cart's position? At the beginning of this page, a block diagram for the inverted pendulum system was given. The diagram was not entirely complete. The block representing the response of the cart's position was not included because that variable is not being controlled. It is interesting though, to see what is happening to the cart's

position when the controller for the pendulum's angle is in place. To see this we need to consider the full system block diagram as shown in the following figure.

Rearranging, we get the following block diagram.

In the above, the block loop transfer function given by the following. (8)

is the controller designed for maintaining the pendulum vertical. The closedfrom an input force applied to the cart to an output of cart position is, therefore,

Referring to the Inverted Pendulum: System Modeling page, the transfer function for follows.

is defined as

(9) where, (10) Adding the following commands to your m-file (presuming and are still defined) will generate the response of the cart's position to the same impulsive disturbance we have been considering. P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q ((M + m)*m*g*l)*s^2/q - b*m*g*l*s/q); T2 = feedback(1,P_pend*C)*P_cart; T2 = minreal(T2); t = 0:0.01:10; impulse(T2, t), grid title('Response of Cart Closed-loop Control'); Position to an Impulse Disturbance under

The command minreal effectively cancels out all common poles and zeros in the closed-loop transfer function. This gives the impulse function better numerical properties. As you can see, the cart moves in

the negative direction and stabilizes at about -0.14 meters. This design might work pretty well for the actual controller, assuming that the cart had that much room to move. Keep in mind that this was pure luck. We did not design our controller to stabilize the cart's position, the fact that we have is a fortunate side effect.

Suspension: Frequency Response Controller Design Key MATLAB commands used in this tutorial are: tf , conv , bode , margin , feedback , step Contents Plotting the frequency response in MATLAB Adding lead control Plotting the closed-loop response From the main problem, the dynamic equations in transfer function form are the following: (1) (2) where, (3) and the system schematic is the following where F(s)G1(s) = G2(s).

For the original problem and the derivation of the above equations and schematic, please refer to the Suspension: System Modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For

example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to the main problem for the details of getting those commands). m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020;

nump=[(m1+m2) b2 k2]; denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (b1*k2)+(b2*k1) k1*k2]; G1=tf(nump,denp); (m1*(k1+k2))+(m2*k1)+(b1*b2)

num1=[-(m1*b2) -(m1*k2) 0 0]; den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (b1*k2)+(b2*k1) k1*k2]; G2=tf(num1,den1); (m1*(k1+k2))+(m2*k1)+(b1*b2)

numf=num1; denf=nump; F=tf(numf,denf); Plotting the frequency response in MATLAB The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot so that the closed-loop response will also change. Let's first draw the Bode plot for the original open-loop transfer function. Add the following line of code to your m-file and rerun. You should get the following Bode plot: w = logspace(-1,2); bode(G1,w)

For convenience in representing systems with different natural frequencies of the system, we normalize and scale our findings before plotting the Bode plot, so that the low-frequency asymptote of each term is at 0 dB. This normalization by adjusting the gain, K, makes it easier to add the components of the Bode plot. The effect of K is to move the magnitude curve up (increasing K) or down (decreasing K) by an amount 20*logK, but the gain, K, has no effect on the phase curve. Therefore from the previous plot, K must be equal to 100 dB or 100,000 to move the magnitude curve up to 0 dB at 0.1 rad/s. Go back to your m-file and add the following line of code to your m-file before the bode command and rerun. You should get the following Bode plot: K=100000; bode(K*G1,w)

Adding lead control From the Bode plot above, we see that the phase curve is concave at about 5 rad/sec. First, we will try to add positive phase around this region, so that the phase will remain above the -180 degree line. Since a large phase margin leads to a small overshoot, we will want to add at least 140 degrees of positive phase at the area near 5 rad/sec. Since one lead controller can add no more than +90 degrees, we will use a two-lead controller. To obtain T and a, the following steps can be used: 1. Determine the positive phase needed: Since we want 140 degrees total, we will need 70 degrees from each controller. 2. Determine the frequency where the phase should be added: In our case this frequency should be 5.0 rad/sec. 3. Determine the constant a from the equation below: This determines the required space between the zero and the pole for the desired maximum phase added. (4) 4. Determine T and aT from the following equation: These determine the corner frequencies so that the maximum phase will be added at the desired frequency.

(5) (6) Now let's put our 2-lead controller into the system and see what the Bode plot looks like. Add the following code to your m-file, and add a % in front of the previous bode command (if there is one). You should get the following Bode plot: a = (1-sin(70/180*pi))/(1+sin(70/180*pi)); w=5; T=1/(w*sqrt(a)); aT=sqrt(a)/w; numc = conv([T 1], [T 1]); denc = conv([aT 1], [aT 1]); C = tf(numc,denc); margin(K*C*G1)

From this plot we see that the concave portion of the phase plot is above -180 degrees now, and the phase margin is large enough for the design criteria. Let's see how the output (the distance X1-X2) responds to a bump on the road (W). Recall that the schematic of the system is:

and the closed-loop transfer function can be derived as follows: sys_cl = F*feedback(G1,K*C); Plotting the closed-loop response Let's see what the step response looks like now. Keep in mind that we are using a 0.1 m high step as the disturbance. To simulate this, simply multiply the system by 0.1. Add the following code into the m-file and rerun it. Don't forget to put % mark in front of all bode and margin commands! t=0:0.01:5; step(0.1*sys_cl,t) axis([0 5 -.01 .01])

The amplitude of response is a lot smaller than the percent overshoot requirement and the settling time also is less than 5 seconds. Since we can see that an amplitude of the output's response less than 0.0001 m or 1% of input magnitude after 4 seconds. Therefore we can say that the settling time is 4 seconds from the above plot. From the Bode plot above, we see that increasing the gain will increase the crossover frequency and thus make the response faster. We will increase the gain and see if we can get a better response. Go back to your m-file and change numc as shown below to generate the following plot. numc = 4*conv([T 1], [T 1]); denc = conv([aT 1], [aT 1]); C = tf(numc,denc); sys_cl = F*feedback(G1,K*C); t=0:0.01:5; step(0.1*sys_cl,t) axis([0 5 -.01 .01])

From this plot we can see that the percent overshoot is about 0.15 mm less than the previous plot's and the settling time also less than 5 seconds. This response is now satisfactory and no more design iteration is needed. DC Motor Position: Frequency Domain Methods for Controller Design Key MATLAB commands used in this tutorial are: tf , sisotool Contents Drawing the uncompensated system's Bode plot Adding an integrator Gain and phase margin specifications and controller design From the main problem, the open-loop transfer function of the DC Motor is given as follows. (1) The structure of the control system has the form shown in the figure below.

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Position: System Modeling page. With a 1-radian step reference, the design criteria are the following. Settling time less than 0.040 seconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input First create a new m-file and type in the following commands (refer to main problem for the details of getting these commands). J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2)); Drawing the uncompensated system's Bode plot The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the system's closed-loop response. Adding a controller to the system changes the open-loop Bode plot, thereby changing the closed-loop response. Let's first draw the Bode plot for the original open-loop transfer function. Throughout this section, we will use the interactive SISO Design Tool to design our controller. This tool can be opened for the uncompensated plant by adding the command sisotool('bode',P_motor) to your m-file. Two windows will open initially with the SISO Design for SISO Design Task window having the form shown in the figure below. This figure shows the open-loop Bode plot of the transfer function passed via

the sisotool function. This figure appears initially due to the addition of the string 'bode' to the function call. If the string 'bode' is omitted, the default initial window includes the root locus plot in addition to the open-loop Bode plot.

From previous pages we know that the open-loop plant in unity feedback does not satisfy the given system requirements. We will, therefore, add different compensators to our system in order to reshape the openloop Bode plot to give the desired closed-loop performance. Adding an integrator Now let's add a pure integrator compensator to our system. The architecture of the feedback system can be modified within the SISO Design Tool GUI, but the default compensator C is placed in series with the plant G in the forward path of the block diagram. This corresponds to the architecture we are assuming, as

shown in the schematic at the top of this page. Placing an integral compensator in this position will achieve the goal of zero steady-state error in the presence of a step disturbance input. The form of our controller is then modified under the Compensator Editor tab of the Control and Estimation Tools Manager window. Specifically, an integrator is added to our controller by right-clicking in the Dynamics section of the window and selecting Add Pole/Zero > Integrator from the resulting menu. The figure below illustrates what the Control and Estimation Tools Manager window should look like.

The Bode plot that is already open should then change as shown below to reflect the addition of the integrator.

Gain and phase margin specifications and controller design Recall that we additionally desire an overshoot of less than 16%. Overshoot of a step response correlates to a system's phase margin. We will apply the relationship that phase margin approximately equals 100 times the damping ratio (zeta) to estimate the desired phase margin of our system. We will additionally use our 0.040 second settling time requirement to estimate the desired bandwidth frequency (Wbw) of our closedloop system. Add the following code to your m-file to estimate the desired phase margin and bandwidth. Note that these relationships are intended for a second-order canonical system, which we do not have, but the results will serve as a good starting point for our design. zeta = -log(.16) / sqrt( pi^2 + (log(.16))^2 ); PM = 100*zeta

Wbw = (4/(0.04*zeta))*sqrt((1 4*zeta^2+2)) PM = 50.3868 Wbw = 251.5743

2*zeta^2)

sqrt(4*zeta^4

The above results demonstrate that we desire to have at least 50 degrees of phase margin in an attempt to meet the overshoot requirement. Also, a closed-loop bandwidth of 250 rad/sec is desired to additionally achieve the settle time requirement. We will estimate the closed-loop bandwidth as the point on the openloop Bode plot where the magnitude is between -6 and -7.5 dB. From the Bode plot we see that we must add about 110 degrees of phase and 80 dB of gain at a frequency of 250 rad/sec in order to move the gain crossover frequency to 250 rad/sec and provide 50 degrees of phase margin. The magnitude plot will then also lie between -6 and -7.5 dB at a frequency greater than 250 rad/sec. From the Bode phase plot we can see that there is a pole near -60 as indicated by the blue x on the Bode plot near the frequency 60 rad/sec (as well as the change in the magnitude and phase plots). We will, therefore, begin to modify our compensator by adding a zero at s = -60 in order to flatten out the phase curve. A real zero can be added again by right-clicking in the Dynamics section of the Compensator Editor tab of the Control and Estimation Tools Manager window. The location of the zero can then be set by typing -60 into the Location cell of the Edit Selected Dynamics portion of the window and pressing Enter. Your compensator now has the form of a PI controller and the compensated open-loop Bode plot should have the modified form shown below where the red o shows the location of the new zero. To achieve the limits on the axes shown below, right-click on the Bode plot and select Properties from the resulting menu.

From the Bode plot we can see that we need at least 50 additional degrees of phase if we move our crossover frequency to 250 rad/sec. Let's then add a lead compensator to add exactly 50 degrees of phase at this frequency. The following equations provide the pole and zero locations of the lead compensator. a = (1 - sin(PM*pi/180))/(1 + sin(PM*pi/180)); T = 1/(Wbw*sqrt(a)); zero = -1/T pole = -1/(a*T) zero = -90.6050

pole = -698.5222 The pole and zero of the lead compensator can then be added to our system in the same manner that the integrator and real zero were added above. Alternatively, we can add a lead compensator by specifiying the desired maximum phase and frequency location within the SISO Design Tool GUI. This is accomplished also by right-clicking in the Dynamics section of the Compensator Editor tab of the Control and Estimation Tools Manager window. Selecting the lead compensator in the Dynamics section of the window then opens four cells in the Edit Selected Dynamics portion of the window. These four cells allow you to specify the lead portion of the compensator either in terms of the pole and zero locations, or in terms of the phase and frequency location. If we enter 50 into the Max Delta Phase (deg) cell and 250 into the at Frequency cell and hit Enter, we will get a pole and zero location that closely (though not exactly) matches those we calculated above. The modified Bode plot should then have a similar appearance to the one shown below.

This new Bode plot now shows that the phase margin is about right at 250 rad/sec, but the gain is too small by about 55 dB if we wish for the gain crossover to occur at 250 rad/sec. Note that each pole and zero we have added to the compensator has the form (2) rather than (s+z) or (s+p). This default form has a DC gain of 1. Therefore, your magnitude plot will be shifted up or down compared to the figure shown above if the terms of your compensator have the form (s+z) and (s+p). Converting from decibels determines that a gain of 600 will shift the magnitude plot up approximately 55 dBs. This change in gain can be accomplished by typing 600 into the cell multiplying the compensator at the top of the Edit Compensator tab and pressing Enter. The resulting Bode plot is shown

below. As indicated on the plot, the phase margin is 49.8 degrees and the gain crossover frequency is 236 rad/sec which are close to our goals of 50 degrees and 250 rad/sec.

Let's now check the resulting step response of the closed-loop system. This plot can be generated under the Analysis Plots tab of the Control and Estimation Tools Manager window. Specifically, under this tab choose Plot 1 to be a Step from the drop-down menu in the Analysis Plots section of the tab. Then check Closed Loop r to y for Plot 1 in the Contents of Plots section of the window. See the figure below for an illustration of this process.

The resulting step response plot is shown below, where the details of the peak response and the settling time are added from the right-click menu underCharacteristics.

From the above figure, it can be seen that the overshoot is too large and the settling time is better than expected. This is attributable to the fact that our system does not actually have the form of a canonical second-order system and because we did not exactly achieve a phase margin of 50 degrees and a bandwidth of 250 rad/sec. In order to reduce the overshoot, we will adjust the lead portion of our compensator to provide more phase margin. Specifically, within the Compensator Editor tab you can change the value of the Max Delta Phase (deg). By increasing this value, you should see the bump in the phase plot increase and the overshoot in the step response plot decrease (as long as the box for Real-Time Update is checked). This change will of course also affect the gain crossover frequency. Therefore, we need to change the value of the compensator gain to shift the magnitude plot such that the gain crossover frequency is still near 250 rad/sec. This can be accomplished by manually typing in different values for the gain in the Edit Compensator tab as we have done previously. Another option is to use the Bode plot since plots in the SISO Design for SISO Design Task allow for graphical tuning. Specifically, if you place your cursor over the magnitude plot it will turn into a hand and you can "grab" the plot and move it up or down. The gain in the compensator will automatically change to reflect the movement of the plot. In the end a choice of 70 degrees of additional

phase at 250 rad/sec and a gain of 315 give us a phase margin of 70 degrees and an overshoot less than 16% as shown below.

From the above step response we see that even though the overshoot is fine, the settling time is too long. We can speed up the system response by trying a slightly higher bandwidth. This can again be accomplished from the Edit Compensator tab by increasing the frequency where the peak phase of the lead compensator is positioned. Through trial and error we choose an at Frequency of 350 rad/sec. We then use the graphical tuning technique to "grab" the magnitude plot of the Bode diagram to shift the crossover frequency to a location that provides approximately 70 degrees of phase margin. A gain of 670 seems to work well. Throughout the tuning, the Bode diagram and step response plot changed according to the changes being made to the compensator. Your final plots should be similar to the two shown below.

Now we have the performance we desire. We have less than 16% overshoot and a settling time less than 40 milliseconds. Furthermore, the integral action of the compensator provides zero steady-state error to a step reference, even in the presence of a step disturbance input. DC Motor Speed: Frequency Domain Methods for Controller Design Key MATLAB commands used in this tutorial are: tf , bode , >margin , step , feedback Contents Drawing the original Bode plot Adding proportional gain Plotting the closed-loop response Adding a lag compensator From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer function of the DC Motor are the following. (1)

(2) (3) The structure of the control system has the form shown in the figure below.

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page For a 1-rad/sec step reference, the design criteria are the following. Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1% Now let's design a controller using the methods introduced in the Introduction: Frequency Domain Methods for Controller Design page. Create a new m-file and type in the following commands. J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2); Drawing the original Bode plot The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot, thereby changing the closed-loop response. It is our goal to design the controller to shape the open-loop

Bode plot in such a way that the closed-loop system behaves in a desired manner. Let's first draw the Bode plot for the original open-loop plant transfer function. Add the following code to the end of your m-file and run it in the MATLAB command window. You should generate the Bode plot shown below. bode(P_motor) grid title('Bode Plot of the Original Plant')

Adding proportional gain From the Bode plot above, it appears that the gain margin and phase margin of this system are currently infinite which indicates the system is robust and has minimal overshoot. The problem with this is that the phase margin is infinite because the magnitude plot is below 0 dB at all frequencies. This indicates that the system will have trouble tracking various reference signals without excessive error. Therefore, we would like to increase the gain of the system while still achieving enough phase margin. A phase margin of 60 degrees is generally sufficient for stability margin. From the above Bode plot, this phase margin is achieved for a crossover frequency of approximately 10 rad/sec. The gain needed to raise the magnitude plot so that the gain crossover frequency occurs at 10 rad/sec appears to be approximately 40 dB. The exact phase and gain of the Bode plot at a given frequency can be determined by clicking on the graph at the corresponding frequency. The bode command, invoked with left-hand arguments, can also be used to provide the exact phase and magnitude at 10 rad/sec as shown below.

[mag,phase,w] = bode(P_motor,10) mag = 0.0139 phase = -123.6835 w = 10 Therefore, the exact phase margin for a gain crossover frequency of 10 rad/sec is 180 - 123.7 = 56.3 degrees. Since the exact magnitude at this frequency is 20 log 0.0139 = -37.1 dB, 37.1 dB of gain must be added to the system. Otherwise stated, a proportional gain of 1/0.0139 = 72 will achieve an open-loop gain of 1 at 10 rad/sec. Add the following commands to your m-file to observe the effect of this proportional controller on the system. In this case, we use the margin command instead of the bode command in order to explicitly see the new gain and phase margins and crossover frequencies. C = 72; margin(C*P_motor);

Plotting the closed-loop response

From the plot above we see that the resulting phase margin and gain crossover frequency are as we expected. Let's see what the closed-loop response look like. Add a % in front of the bode and margin commands to comment them out, then add the following code to the end of your m-file. Rerunning the m-file will produce the step response shown below where the annotations were added by right-clicking on the plot and choosing Characteristics from the resulting menu. sys_cl = feedback(C*P_motor,1); t = 0:0.01:10; step(sys_cl,t), grid title('Step Response with Proportional Gain = 72')

Note that the settling time is fast enough, but the overshoot and the steady-state error are too high. The overshoot can be reduced by decreasing the gain in order to achieve a larger phase margin, but this would cause the steady-state error to become even larger. A lag compensator could be helpful here in that it can decrease the gain crossover frequency in order to increase the phase margin without decreasing the system's DC gain. Adding a lag compensator Consider the following lag compensator:

(4) This lag compensator has a DC gain of 1/0.01 = 100 which means it will increase the system's static position error constant by a factor of 100 and will reduce the steady-state error associated with the system's closed-loop step response. In fact, it allows us to reduce the proportional gain of 72 used earlier, while still meeting the requirement on steady-state error. We will employ a gain of 45. Furthermore, since the corner frequencies of the pole and zero are a decade or more below the current gain crossover frequency of 10 rad/sec, the phase lag contributed by the compensator shouldn't adversely affect performance much. A Bode plot of the lag compensator can be generated employing the following commands. C = 45*(s + 1)/(s + 0.01); bode(C) grid title('Bode Plot of the Lag Compensator')

The resulting step response can then be observed by modifying the code in your m-file as follows. sys_cl = feedback(C*P_motor,1); t = 0:0.01:10; step(sys_cl,t), grid

title('Step Response with Lag Compensator')

Inspection of the above demonstrates that all of the given requirements are now met when the lag compensator described above is employed. Cruise Control: Frequency Domain Methods for Controller Design Key MATLAB commands used in this tutorial are: tf , feedback , step Contents System model System parameters Performance specifications Bode plot and open-loop response Proportional controller Lag compensator System model

The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System Modeling page for the derivation. (1) System parameters For this example, let's assume that the parameters of the system are (m) (b) (r) (u) vehicle mass damping coefficient reference speed nominal control force 1000 kg 50 N.s/m 10 m/s 500 N

and the block diagram of an typical unity feedback system is shown below.

Performance specifications Rise time < 5 sec Overshoot < 10% Steady-state error < 2% Bode plot and open-loop response The first step in solving this problem using frequency response is to determine what open-loop transfer function to use. Just like for the Root-Locus design method, we will only use a proportional controller to solve the problem. The block diagram and the open-loop transfer function are shown below.

(2) In order to use a Bode plot, the open-loop response must be stable. Let Kp equal 1 for now and see how the open-loop response looks like. Create a new m-file and enter the following commands. m = 1000;

b = 50; u = 500;

Kp = 1; s = tf('s'); P_cruise = 1/(m*s+b); C = Kp; step(u*C*P_cruise)

As you can see, the open-loop system is stable; thus, we can go ahead and generate the Bode plot. Change the above m-file by deleting the stepcommand and adding in the following command. bode(C*P_cruise);

Proportional controller Refer to the Introduction: Frequency Domain Methods for Controller Design page, and let's see what system characteristics we can determine from the above Bode plot. The steady-state error can be found from the following equation: (3) For this system, the low frequency gain is -34dB = 0.02; therefore, the steady-state error should be 98%. We can confirm this by generating a closed-loop step response as follows. r = 10; sys_cl = feedback(C*P_cruise,1); step(r*sys_cl);

We need to increase the low frequency gain in order to improve the steady-state error. Specifically, the error needs to be < 2%; therefore, 1/(1+M_{w=0}) < 0.02 -> M_{w=0} > 49 = 33.8 dB. So to reach the desired steady-state error using proportional control only requires a Kp > 67.8 dB = 2455. Let's look at the Bode diagram of the compensated open-loop system. Kp = 2500; C = Kp;

bode(C*P_cruise);

As you can see from the Bode plot above, the low frequency magnitude is now, 34 dB. Now let's simulate the step response of the closed loop system with this gain. sys_cl = feedback(C*P_cruise,1); step(r*sys_cl);

The steady-state error meets the requirements; however, the rise time is much shorter than is needed and is unreasonable in this case since the car can not accelerate to 10 m/s in 2 sec. Therefore, we will try using a smaller proportional gain to reduce the control action required along with a lag compensator to reduce the steady-state error. Lag compensator If you take a look at the "Lag or Phase-Lag Compensator using Frequency Response" section of the Lead and Lag Compensator Design page, the lag compensator adds gain at the low frequencies while keeping the bandwidth frequency at the same place. This is actually what we need: Larger low frequency gain to reduce the steady-state error and keep the same bandwidth frequency to maintain the desired rise time. The transfer function of the lag controller is: (4) If you read the "Lag or Phase-Lag Compensator using Root-Locus" section in Lead and Lag Compensator Design page, the pole and the zero of a lag controller need to be placed close together. Also, it states that the steady-state error will be reduce by a factor of zo/po. For these reasons, let zo equal 0.1 and po equal 0.02. The proportional gain, Kp = 1000 was chosen by trial-and-error. Kp = 1000; zo = 0.1;

po = 0.02;

C_lag = (s+zo)/(s+po); bode(Kp*C_lag*P_cruise);

Let's confirm the performance by generating a closed-loop step response. sys_cl = feedback(Kp*C_lag*P_cruise,1); t = 0:0.1:20; step(r*sys_cl,t);

As you can see, there is a very slight overshoot, the steady state error is close to zero, and the rise time is under 5 seconds. The system has now met all of the design requirements. No more iteration is needed. Cruise Control: State-Space Methods for Controller Design In this tutorial we will design a controller and observer for the cruise control system using the state-space model. Key MATLAB commands used in this tutorial are: ss , feedback Contents State-space equations Design requirements Control design using pole placement Reference input State-space equations The equations of motion in state-space form are as follows: (1)

(2) where (m) (b) (u) (v) vehicle mass damping coefficient nominal control force 1000 kg 50 N.s/m 500 N

vehicle velocity where y=v is the system output

Design requirements Rise time < 5 s Overshoot < 10% Steady-state error < 2% To see the original problem setup, see the Cruise Control: System Modeling page. Control design using pole placement The schematic of a full state-feedback system is shown below.

(3) where K = state-feedback gain matrix u = r-K.v = control input Recall from the State-Space Tutorial page, we can use a "pole placement" technique to obtain the desired output. Poles of a closed-loop system can be found from the characteristic equation: the determinant of the [sI-(A-B*K)] matrix. If the poles the system can be placed in the desired location by designing an appropriate control matrix (K), then the desired output can be obtained. In this tutorial, poles will be chosen first, then we will use MATLAB to find the corresponding control matrix (K).

Now, we need to determine where to place poles for our system. Since our [sI-(A-B*K)] matrix is 1x1, we have only one pole to place. Let the pole be at -1.5 (arbitrary). Just as in the State-Space Tutorial, the MATLAB command place will be used to find the control matrix K. Create a new m-file and enter the following commands. Running the m-file in the MATLAB command window should give you the control matrix and step response shown below. m = 1000; b = 50; t = 0:0.1:10; u = 500*ones(size(t));

A = [-b/m]; B = [1/m]; C = [1]; D = [0]; sys = ss(A,B,C,D);

x0 = [0];

p1 = -1.5;

K = place(A,B,[p1])

sys_cl = ss(A-B*K,B,C,D); lsim(sys_cl,u,t,x0); axis([0 10 0 0.35]) K = 1450

As you can see, the rise time is satisfactory, but the steady-state error is too large. Reference input Once again from the State-Space Tutorial page, a scaling factor called Nbar (the schematic is shown below) can be used to eliminate the steady-state error. We can use the rscale function to compute the scaling factor. Download it here, rscale.m. The input is already multiplied by 500, and we want the steadystate speed to be 10 m/sec, so we need to account for these factors as well.

Copy the following commands to an m-file and run it in the MATLAB command window. You should get the step response shown below. Nbar = rscale(sys,K)*10/500;

sys_cl = ss(A-B*K,B*Nbar,C,D);

lsim(sys_cl,u,t,x0); axis([0 10 0 11])

As you can see, the steady-state error has been eliminated. The rise time is less than 5 seconds and the overshoot is, in fact, zero. All the design requirements are satisfied. DC Motor Speed: State-Space Methods for Controller Design Key MATLAB commands used in this tutorial are: ss , >order , rank , ctrb , place , step Contents Designing the full-state feedback controller Adding a precompensator From the main problem, the dynamic equations in state-space form are given below.

(1)

(2) For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page. These state-space equations have the standard form shown below where the state vector x = [ theta_dot i ] and the input u = V. (3) (4) For a 1-rad/sec step reference, the design criteria are the following. Settling time less than 2 seconds Overshoot less than 5% Steady-stage error less than 1% Now let's design a controller using the methods introduced in the Introduction: State-Space Methods for Controller Design page. Create a new m-file and type in the following commands. J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; A = [-b/J -K/L B = [0 1/L]; C = [1 D = 0; sys = ss(A,B,C,D); Designing the full-state feedback controller Since both of the state variables in our problem are easy to measure (simply add an ammeter for current and a tachometer for the speed), we can design a full-state feedback controller for the system without worrying about having to add an observer. The control law for a full-state feedback system has the form u = r - Kc x and the associated schematic is shown below. 0]; K/J -R/L];

Recall that the characteristic polynomial for this closed-loop system is the determinant of sI - (A - B*Kc) where s is the Laplace variable. Since the matrices A and B*Kc are both 2x2 matrices, there should be 2 poles for the system. This fact can be verified with the MATLAB command order. If the given system is controllable, then by designing a full-state feedback controller we can move these two poles anywhere we'd like. Whether the given system is controllable or not can be determined by checking the rank of the controllability matrix [ B AB A^2B ...]. The MATLAB command ctrb constructs the controllability matrix given matrices A and B. Additionally, the command rank determines the rank of a given matrix. The following commands executed at the command line will verify the system's order and whether or not the system is controllable. sys_order = order(sys) sys_rank = rank(ctrb(A,B)) sys_order = 2 sys_rank = 2 From the above, we know that our system is controllable since the controllability matrix is full rank. We will first place the poles at -5+i and -5-i (note that this corresponds to a zeta = 0.98 which gives close to 0% overshoot and a sigma = 5 which provides a 0.8 second settling time). Once we have determined the pole locations we desire, we can use the MATLAB commands place or acker to determine the controller gain matrix, Kc, to achieve these poles. We will use the command place since it is numerically better conditioned than acker. However, if we wished to place a pole with multiplicity greater than the rank of the matrix B, then we would have to use the command acker. Add the following code to the end of your m-file. Running in the command window will generate the feedback gain matrix output below. p1 = -5 + 1i; p2 = -5 - 1i;

Kc = place(A,B,[p1 p2]) Kc = 12.9900 -1.0000

Referring back to the state-space equations at the top of the page, we see that substituting the statefeedback law u = r - Kc x for u leads to the following expression. (5) (6) We can then see the closed-loop response by simply adding the following lines to the end of your m-file. Running your m-file in the command window will then give the plot shown below. t = 0:0.01:3; sys_cl = ss(A-B*Kc,B,C,D); step(sys_cl,t) grid title('Step Response with State-Feedback Controller')

Adding a precompensator

From this plot we see that the steady-state error is too large. One approach for eliminating the steady-state error is to simply scale the input so that the output in turn is scaled to the desired level. This is a little challenging in our example because we have two states to consider. Therefore, we need to compute what the steady-state values of both states should be, multiply them by the chosen gain Kc, and use the result as our "reference" for computing the input u. This can be done in one step by adding a constant gain precompensator Nbar after the reference as shown in the following schematic.

We can find this Nbar factor by employing the used-defined function rscale.m as shown below. Nbar = rscale(sys,Kc) Nbar = 13.0000 Note that the function rscale.m is not a standard function in MATLAB. You will have to download it and place it in your current directory. Click here for further information. Now you can plot the step response by adding the above and following lines of code to your m-file and re-running at the command line. t = 0:0.01:10; step(sys_cl*Nbar,t) grid title('Step Response Precompensator') with State-Feedback Controller and

This time, the steady-state error is much less than 1%, and all the other design criteria have been met as well. Note that the precompensator Nbar employed above is calculated based on the model of the plant and further that the precompensator is located outside of the feedback loop. Therefore, if there are errors in the model (or unknown disturbances) the precompensator will not correct for them and there will be steadystate error. You may recall that the addition of integral control may also be used to eliminate steady-state error, even in the presence of model uncertainty and step disturbances. For an example of how to implement integral control in the state space setting, see the DC Motor Position: State-Space Methods for Controller Design example. The tradeoff with using integral control is that the error must first develop before it can be corrected for, therefore, the system may be slow to respond. The precompensator on the other hand is able to anticipitate the steady-state offset using knowledge of the plant model. A useful technique is to combine the precompensator with integral control to leverage the advantages of each approach. DC Motor Position: State-Space Methods for Controller Design Key MATLAB commands used in this tutorial are: ss , order , det , ctrb , place , step Contents Designing the full state-feedback controller Disturbance response

Adding integral action From the main problem, the dynamic equations in state-space form are given below.

(1)

(2) The above has the form of a standard set of state-space equations as described below. (3) (4) For the original problem setup and the derivation of the above equations, please refer to the DC Motor Position: System Modeling page With a 1-radian step reference, the design criteria are the following. Settling time less than 0.040 seconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input First create a new m-file and type in the following commands (refer to main problem for the details of getting these commands). J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6;

A = [0 1 0 0 -b/J K/J 0 -K/L -R/L]; B = [0 ; 0 ; 1/L];

C = [1 D = 0;

0];

motor_ss = ss(A,B,C,D); Designing the full state-feedback controller Since all of the state variables in our problem are very easy to measure (simply add an ammeter for current, a tachometer for speed, and a potentiometer for position), we can design a full-state feedback controller for the system without worrying about having to add an observer. The control law for a full-state feedback system has the form u = r - Kc x. The associated block diagram is given below.

Recall that the characteristic polynomial for this closed-loop system is the determinant of sI-(A-B*Kc) where s is the Laplace variable. Since the matrices Aand B*Kc are both 3x3 matrices, there should be 3 poles for the system. This fact can be verified with the MATLAB command order. If the given system is controllable, then by designing a full state-feedback controller we can move these three poles anywhere we'd like. Whether the given system is controllable or not can be determined by checking the rank of the controllability matrix [ B AB A^2B ...]. The MATLAB command ctrb constructs the controllability matrix given A and B. Additionally, the command rank determines the rank of a given matrix, though it can be numerically unreliable. Therefore, we will use the command det to calculate the determinant of the controllability matrix where a full rank matrix has a non-zero determinant. The following commands executed at the command line will verify the system's order and whether or not it is controllable. sys_order = order(motor_ss) determinant = det(ctrb(A,B)) sys_order = 3 determinant = -3.4636e+24

From the above, we know that our system is controllable since the determinant of the controllability matrix is not zero and hence we can place the system's closed-loop poles anywhere in the s-plane. We will first place the poles at -200, -100+100i and -100-100i. By ignoring the effect of the first pole (since it is faster than the other two poles), the dominant poles correspond to a second-order system with zeta = 0.5 corresponding to 0.16% overshoot and sigma = 100 which corresponding to a settling time of 0.040 seconds. Once we have determined the pole locations we desire, we can use the MATLAB commands place or acker to determine the controller gain matrix, Kc, to achieve these poles. We will use the command place since it is numerically better conditioned than acker. However, if we wished to place a pole with multiplicity greater than the rank of the matrix B, then we would have to use the command acker. Add the following code to the end of your m-file. p1 = -100+100i; p2 = -100-100i; p3 = -200; Kc = place(A,B,[p1, p2, p3]) Kc = 0.0013 -0.0274 -3.9989

Referring back to the equations and schematic at the top of the page, we see that employing a statefeedback law u = r - Kc x, the state-space equations become the following. (5) (6) We can generate the closed-loop response to a step reference by adding the following lines to the end of your m-file. Run your m-file in the command window and you should generate a plot like the one shown below. t = 0:0.001:0.05; sys_cl = ss(A-B*Kc,B,C,D); step(sys_cl,t)

Note that our given requirements are not met, specifically, the steady-state error is much too large. Before we address this, let's first look at the system's disturbance response. Disturbance response In order to observe the system's disturbance response, we must provide the proper input to the system. In this case, a disturbance is physically a load torque that acts on the inertia of the motor. This load torque acts as an additive term in the second state equation (which gets divided by J, as do all the other terms in this equation). We can simulate this simply by modifying our closed-loop input matrix, B, to have a 1 / J in the second row assuming that our current input is only the disturbance. Add the following lines to your m-file and re-run. dist_cl = ss(A-B*Kc,[0; 1/J ; 0], C, D); step(dist_cl,t)

Notice that the error due to the step disturbance is non-zero. Therefore, this will also need to be compensated for. Adding integral action From prior examples, we know that if we put an extra integrator in series with the plant it can remove the steady-state error due to a step reference. If the integrator comes before the injection of the disturbance, it will also cancel a step disturbance input in steady state. This changes our control structure so that it now resembles the block diagram shown in the following figure.

We can model the addition of this integrator by augmenting our state equations with an extra state for the integral of the error which we will identify with the variable w. This adds an extra state equation, where the derivative of this state is then just the error, e = y - r where y = theta. This equation will be placed at the bottom of our matrices. The reference r, therefore, now appears as an additional input to our system. The output of the system remains the same.

(7)

(8)

(9) These equations represent the dynamics of the system before the loop is closed. We will refer to the system matrices in this equation that are augmented with the additional integrator state as Aa, Ba, Ca, and Da. The vector multiplying the reference input r will be referred to as Br. We will refer to the state vector of the

augmented system as xa. Note that the reference, r, does not affect the states (except the integrator state) or the output of the plant. This is expected since there is no path from the reference to the plant input, u, without implementing the state-feedback gain matrix Kc. In order to find the closed-loop equations, we have to look at how the input, u, affects the plant. In this case, it affects the system in exactly the same manner as in the unaugmented equations except now u = Kc x - Ki w. We can also rewrite this in terms of our augmented state as u = -Ka xa where Ka = [ Kc Ki ]. Substituting this u into the equations above provides the following closed-loop equations. (10) (11) In the above, the integral of the error will be fed back, and will result in the steady-state error being reduced to zero. Now we must redesign our controller to account for the augmented state vector. Since we need to place each pole of the system, we will place the pole associated with the additional integrator state at -300, which will be faster than the other poles. Add the following lines to your m-file which reflect the closed-loop equations presented above. Note that since the closed-loop transition matrix Aa-Ba*Kadepends on Ba, it will be used in the place command rather than Br. Running your m-file will then produce the plot shown below. Aa = [0 1 0 0 0 -b/J K/J 0 0 -K/L -R/L 0 1 0 0 0]; Ba = [0 ; 0 ; 1/L ; 0 ]; Br = [0 ; 0 ; 0; -1]; Ca = [1 0 0 0]; Da = [0];

p4 = -300; Ka = place(Aa,Ba,[p1,p2,p3,p4]);

t = 0:0.001:.05; sys_cl = ss(Aa-Ba*Ka,Br,Ca,Da); step(sys_cl,t)

To observe the disturbance response, we use a similar approach to that used without the integral action. dist_cl = ss(Aa-Ba*Ka,[0 ; 1/J ; 0; 0],Ca,Da); step(dist_cl,t)

We can see that all of the design specifications are close to being met by this controller. The settle time may be a little large, but by placing the closed-loop poles a little farther to the left in the complex s-plane, this requirement can also be met. Suspension: State-Space Controller Design Key MATLAB commands used in this tutorial are: ss , step Contents Designing the full state-feedback controller Plotting the closed-loop response From the main problem, the dynamic equations in state-space form are the following where Y1 = X1 - X2.

(1)

(2)

(3) For the original problem and the derivation of the above equations and schematic, please refer to the Suspension: System Modeling page. We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds. The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to main problem for the details of getting those commands). We need to define the A, B, C, D matrices by entering the following into the m-file: m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020;

A=[0 0 -(b1*b2)/(m1*m2) -(b1/m1) b2/m2 1 k2/m2 0]; B=[0 1/m1 0 (b1*b2)/(m1*m2) 0 0

((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) 0 -((b1/m1)+(b1/m2)+(b2/m2)) -((k1/m1)+(k1/m2)+(k2/m2))

0 (1/m1)+(1/m2) C=[0 0 1 0]; D=[0 0]; sys=ss(A,B,C,D);

-(b2/m2) -(k2/m2)];

Designing the full state-feedback controller First, let's design a full state-feedback controller for the system. Assuming for now that all the states can be measured (this assumption is probably not true but is sufficient for this problem), the schematic of the system is shown below.

The characteristic polynomial for this closed-loop system is the determinant of (sI-(A-B[1,0]'K)). Note that it's not sI-(A-BK) because the controller K can only control the force input u but not the road disturbance W. Recall that our B matrix is a 4 x 2 matrix, and we only need the first column of B to control U. For this example, we have to use integral action to achieve zero steady-state error, so we add an extra state which is int(X1-X2) = int(Y1). In reality the bus will eventually reach an equilibrium that yields a zero steady-state error. The new states are X1, X1_dot, Y1, Y1_dot, and Y2. Also the state-space matrices, A, B, and C, become the following after the addition of the new state. Aa=[0 0 1 0 0 ((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) 0 0 k2/m2 0 0 0 -((k1/m1)+(k1/m2)+(k2/m2)) -((b1/m1)+(b1/m2)+(b2/m2)) 0

-(b1*b2)/(m1*m2) -(b1/m1) 0 b2/m2 1

0 0 Ba=[0 1/m1 0 (1/m1)+(1/m2) 0 Ca=[0 0 1 0 0]; Da=[0 0]; sys=ss(Aa,Ba,Ca,Da); Actually, there is a shortcut for MATLAB to achieve the same result. Aa = [[A,[0 0 0 0]'];[C, 0]]; Ba = [B;[0 0]]; Ca = [C,0]; Da = D; sys=ss(Aa,Ba,Ca,Da); 0]; 0 (b1*b2)/(m1*m2) -(b2/m2) -(k2/m2) 0];

Add the above MATLAB code into the m-file. In this case, we treat the problem like a PID controller design. The integral control is obtained from the new state. The proportional control is obtained from a gain on Y1 or X1-X2. The direct derivative control of the output isn't possible, since derivative of Y1 or X1-X2 isn't a state. Instead we use the derivative of X1, which is available for feedback. (While X1 maybe hard to measure, X1_dot could be obtained by integrating the output of an accelerometer mounted on the bus.) It is similar to adding more damping to the velocity of the oscillation of the bus suspension. Add the following MATLAB code for controller K in the m-file: K = [0 2.3e6 5e8 0 8e6] K = 0 2300000 500000000 0 8000000

We arrived at this value of the K, matrix by trial and error, adjusting the gain for derivative of X1, Y1 and integral of Y1, as previously mentioned. Plotting the closed-loop response Looking at the schematic above again, we see that after adding the K matrix into the system, the statespace equations become:

(4) (5) We can now obtain the closed-loop response by simply adding the following code into the m-file. Note that we need to multiply B matrix by 0.1 to simulate the 0.1-m high step disturbance. t = 0:0.01:2; sys_cl = ss(Aa-Ba(:,1)*K,-0.1*Ba,Ca,Da); step(sys_cl*[0;1],t) title('Closed-Loop Response to a 0.1-m Step')

From the plot we see that the percent overshoot and settling time requirements are satisfied. Moreover the steady-state error approaches zero as well. Therefore, we will determine that the response is satisfactory. Feel free to play around with the gain for matrix K. Inverted Pendulum: State-Space Methods for Controller Design Key MATLAB commands used are: ss , eig , lsim , lqr , ctrb , plotyy , obsv , place Contents in this tutorial

Open-loop poles Linear Quadratic Regulation (LQR) Adding precompensation Observer-based control From the main problem, the dynamic equations of the inverted pendulum system in state-space form are the following:

(1)

(2) To see how this problem was originally set up and the system equations were derived, consult the Inverted Pendulum: System Modeling page. For this problem the outputs are the cart's displacement ( in meters) and the pendulum angle ( in radians) where represents the deviation of the pedulum's position from equilibrium, that is, . The design criteria for this system for a 0.2-m step in desired cart position Settling time for Rise time for and of less than 5 seconds are as follows:

of less than 0.5 seconds

Pendulum angle never more than 20 degrees (0.35 radians) from the vertical Steady-state error of less than 2% for and

As you may have noticed if you went through some of the other inverted pendulum examples, the design criteria for this example are different. In the other examples we were attemping to keep the pendulum vertical in response to an impulsive disturbance force applied to the cart. We did not attempt to control the cart's position. In this example, we are attempting to keep the pendulum vertical while controlling the cart's position to move 0.2 meters to the right. A state-space design approach is well suited to the control of multiple outputs as we have here. This problem can be solved using full-state feedback. The schematic of this type of control system is shown below where is a matrix of control gains. Note that here we feedback all of the system's states, rather than using the system's outputs for feedback.

Open-loop poles In this problem, represents the step command of the cart's position. The 4 states represent the position and velocity of the cart and the angle and angular velocity of the pendulum. The output contains both the position of the cart and the angle of the pendulum. We want to design a controller so that when a step reference is given to the system, the pendulum should be displaced, but eventually return to zero (i.e. vertical) and the cart should move to its new commanded position. To view the system's open-loop response please refer to the Inverted Pendulum: System Analysis page. The first step in designing a full-state feedback controller is to determine the open-loop poles of the system. Enter the following lines of code into an m-file. After execution in the MATLAB command window, the output will list the open-loop poles (eigenvalues of ) as shown below. M = 0.5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3;

p = I*(M+m)+M*m*l^2; %denominator for the A and B matrices

A = [0

0 (m^2*g*l^2)/p 0 m*g*l*(M+m)/p

0; 0; 1; 0];

0 -(I+m*l^2)*b/p 0 0

0 -(m*l*b)/p

B = [

0; (I+m*l^2)/p; 0; m*l/p];

C = [1 0 0 0; 0 0 1 0]; D = [0; 0];

states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'u'}; outputs = {'x'; 'phi'};

sys_ss = ss(A,B,C,D,'statename',states,'inputname',inputs,'outputname',outputs);

poles = eig(A) poles = 0 -5.6041 -0.1428 5.5651 As you can see, there is one right-half plane pole at 5.5651. This should confirm your intuition that the system is unstable in open loop. Linear Quadratic Regulation (LQR) The next step in the design process is to find the vector of state-feedback control gains assuming that we have access (i.e. can measure) all four of the state variables. This can be accomplished in a number of ways. If you know the desired closed-loop pole locations, you can use the MATLAB commands place or acker. Another option is to use the lqr command which returns the optimal controller gain assuming a linear plant, quadratic cost function, and reference equal to zero (consult your textbook for more details).

Before we design our controller, we will first verify that the system is controllable. Satisfaction of this property means that we can drive the state of the system anywhere we like in finite time (under the physical constraints of the system). For the system to be completely state controllable, the controllability matrix must have rank where the rank of a matrix is the number of independent rows (or columns). The controllability matrix of the system takes the form shown below. The number corresponds to the number of state variables of the system. Adding additional terms to the controllability matrix with higher powers of the matrix will not increase the rank of the controllability matrix since these additional terms will just be linear combinations of the earlier terms. (3) Since our controllability matrix is 4x4, the rank of the matrix must be 4. We will use the MATLAB command ctrb to generate the controllability matrix and the MATLAB command rank to test the rank of the matrix. Adding the following additional commands to your m-file and running in the MATLAB command window will produce the following output. co = ctrb(sys_ss); controllability = rank(co) controllability = 4 Therefore, we have verified that our system is controllable and thus we should be able to design a controller that achieves the given requirements. Specifically, we will use the linear quadratic regulation method for determining our state-feedback control gain matrix . The MATLAB function lqrallows you to choose two parameters, and , which will balance the relative importance of the control effort ( ) and error (deviation from 0), respectively, in the cost function that you are trying to optimize. The simplest case is to assume , and . The cost function corresponding to this and places equal importance on the control and the state variables which are outputs (the pendulum's angle and the cart's position). Essentially, the lqr method allows for the control of both outputs. In this case, it is pretty easy to do. The controller can be tuned by changing the nonzero elements in the matrix to achieve a desirable response. To observe the structure of , enter the following into the MATLAB command window to see the output given below. Q = C'*C Q = 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

The element in the (1,1) position of represents the weight on the cart's position and the element in the (3,3) position represents the weight on the pendulum's angle. The input weighting will remain at 1. Ultimately what matters is the relative value of and , not their absolute values. Now that we know how to interpret the matrix, we can experiment to find the matrix that will give us a "good" controller. We will go ahead and find the matrix and plot the response all in one step so that changes can be made in the control and seen automatically in the response. Add the following commands to the end of your m-file and run in the MATLAB command window to get the following value for and the response plot shown below. Q = C'*C; R = 1; K = lqr(A,B,Q,R)

Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D];

states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'r'}; outputs = {'x'; 'phi'};

sys_cl = ss(Ac,Bc,Cc,Dc,'statename',states,'inputname',inputs,'outputname',outpu ts);

t = 0:0.01:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with LQR Control')

K = -1.0000 -1.6567 18.6854 3.4594

The curve in green represents the pendulum's angle in radians, and the curve in blue represents the cart's position in meters. As you can see, this plot is not satisfactory. The pendulum and cart's overshoot appear fine, but their settling times need improvement and the cart's rise time needs to be reduced. As I'm sure you have noticed, the cart's final position is also not near the desired location but has in fact moved in the opposite direction. This error will be dealt with in the next section and right now we will focus on the settling and rise times. Go back to your m-file and change the matrix to see if you can get a better response. You will find that increasing the (1,1) and (3,3) elements makes the settling and rise times go down, and lowers the angle the pendulum moves. In other words, you are putting more weight on the errors at the cost of increased control effort . Modifying your m-file so that the (1,1) element of is 5000 and the (3,3) element is 100, will produce the following value of and the step response shown below. Q = C'*C; Q(1,1) = 5000; Q(3,3) = 100 R = 1; K = lqr(A,B,Q,R)

Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D];

states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'r'}; outputs = {'x'; 'phi'};

sys_cl = ss(Ac,Bc,Cc,Dc,'statename',states,'inputname',inputs,'outputname',outpu ts);

t = 0:0.01:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with LQR Control') Q = 5000 0 0 0 K = -70.7107 -37.8345 105.5298 20.9238 0 0 0 0 0 0 100 0 0 0 0 0

You may have noted that if you increased the values of the elements of even higher, you could improve the response even more. The reason this weighting was chosen was because it just satisfies the transient design requirements. Increasing the magnitude of more would make the tracking error smaller, but would require greater control force . More control effort generally corresponds to greater cost (more energy, larger actuator, etc.). Adding precompensation The controller we have designed so far meets our transient requirements, but now we must address the steady-state error. In contrast to the other design methods, where we feedback the output and compare it to the reference input to compute an error, with a full-state feedback controller we are feeding back all of the states. We need to compute what the steady-state value of the states should be, multiply that by the chosen gain , and use a new value as our "reference" for computing the input. This can be done by adding a constant gain after the reference. The schematic below shows this relationship:

We can find this factor by employing the used-defined function rscale.m as shown below. The is modified to reflect the fact that the reference is a command only on cart position. Cn = [1 0 0 0]; sys_ss = ss(A,B,Cn,0); Nbar = rscale(sys_ss,K) Nbar = -70.7107

matrix

Note that the function rscale.m is not a standard function in MATLAB. You will have to download it here and place it in your current directory. More information can be found here, Extras: rscale.m. Now you can plot the step response by adding the above and following lines of code to your m-file and rerunning at the command line. sys_cl = ss(Ac,Bc*Nbar,Cc,Dc,'statename',states,'inputname',inputs,'outputname', outputs);

t = 0:0.01:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with Precompensation and LQR Control')

Now, the steady-state error is within our limits, the rise and settle times are met, and the pendulum's overshoot is within range of the design criteria. Note that the precompensator employed above is calculated based on the model of the plant and further that the precompensator is located outside of the feedback loop. Therefore, if there are errors in the model (or unknown disturbances) the precompensator will not correct for them and there will be steady-state error. You may recall that the addition of integral control may also be used to eliminate steady-state error, even in the presence of model uncertainty and step disturbances. For an example of how to implement integral control in the state space setting, see the Motor Position: State-Space Methods example. The tradeoff with using integral control is that the error must first develop before it can be corrected for, therefore, the system may be slow to respond. The precompensator on the other hand is able to anticipitate the steady-state offset using knowledge of the plant model. A useful technique is to combine the precompensator with integral control to leverage the advantages of each approach. Observer-based control The response achieved above is good, but was based on the assumption of full-state feedback, which is not necessarily valid. To address the situation where not all state variables are measured, a state estimator must be designed. A schematic of state-feedback control with a full-state estimator is shown below, without the precompensator .

Before we design our estimator, we will first verify that our system is observable. The property of observability determines whether or not based on the measured outputs of the system we can estimate the state of the system. Similar to the process for verifying controllability, a system is observable if its observability matrix is full rank. The observability matrix is defined as follows.

(4) We can employ the MATLAB command obsv to the rank command to check its rank as shown below. ob = obsv(sys_ss); observability = rank(ob) observability = 4 contruct the observability matrix and

Since the observability matrix is 8x4 and has rank 4, it is full rank and our system is observable. The observability matrix in this case is not square since our system has two outputs. Note that if we could only measure the pendulum angle output, we would not be able to estimate the full state of the system. This can be verified by the fact that obsv(A,C(2,:)) produces an observability matrix that is not full rank. Since we know that we can estimate our system state, we will now describe the process for designing a state estimator. Based on the above diagram, the dynamics of the state estimate are described by the following equation. (5) The spirit of this equation is similar to that of closed-loop control in that last term is a correction based on feedback. Specifically, the last term corrects the state estimate based on the difference between the actual output and the estimated output . Now let's look at the dynamics of the error in the state estimate. (6) Therefore, the state estimate error dynamics are described by (7) and the error will approach zero ( will approach ) if the matrix is stable (has negative eigenvalues). As is with the case for control, the speed of convergence depends on the poles of the estimator (eigenvalues of ). Since we plan to use the state estimate as the input to our controller, we would like the state estimate to converge faster than is desired from our overall closed-loop system. That is, we would like the observer poles to be faster than the controller poles. A common guideline is to make the estimator poles 4-10 times faster than the slowest controller pole. Making the estimator poles too fast can be problematic if the measurement is corrupted by noise or there are errors in the sensor measurement in general. Based on this logic, we must first find the controller poles. To do this, copy the following code to the end of your m-file. If you employed the updated matrix, you should see the following poles in the MATLAB command window. poles = eig(Ac) poles = -8.4910 + 7.9283i -8.4910 - 7.9283i -4.7592 + 0.8309i -4.7592 - 0.8309i The slowest poles have real part equal to -4.7592, therefore, we will place our estimator poles at -40. Since the closed-loop estimator dynamics are determined by a matrix ( ) that has a similar form to the matrix that determines the dynamics of the state-feedback system ( ), we can use the same

commands for finding the estimator gain as we can for finding the state-feedback gain . Specifically, since taking the transpose of leaves the eigenvalues unchanged and produces a result that exactly matches the form of , we can use the acker orplace commands. Recalling that the place command cannot place poles of multiplicity greater than one, we will place the observer poles as follows. Add the following commands to your m-file to calculate the matrix and generate the output shown below. P = [-40 -41 -42 -43]; L = place(A',C',P)' L = 1.0e+03 * 0.0826 1.6992 -0.0014 -0.0762 -0.0010 -0.0402 0.0832 1.7604

We are using both outputs (the angle of the pendulum and the position of the cart) to design the observer. Now we will combine our state-feedback controller from before with our state estimator to get the full compensator. The resulting closed-loop system is described by the following matrix equations. (8) (9) The closed-loop system described above can be implemented in MATLAB by adding the following commands to the end of your m-file. After running the m-file the step response shown will be generated. Ace = [(A-B*K) (B*K); zeros(size(A)) (A-L*C)]; Bce = [B*Nbar; zeros(size(B))]; Cce = [Cc zeros(size(Cc))]; Dce = [0;0];

states = {'x' 'x_dot' 'phi' 'phi_dot' 'e1' 'e2' 'e3' 'e4'}; inputs = {'r'};

outputs = {'x'; 'phi'};

sys_est_cl = ss(Ace,Bce,Cce,Dce,'statename',states,'inputname',inputs,'outputname',o utputs);

t = 0:0.01:5; r = 0.2*ones(size(t)); [y,t,x]=lsim(sys_est_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with Observer-Based State-Feedback Control')

This response is almost identical to the response achieved when it was assumed that we had full access to the state variables. This is because the observer poles are fast, and because the model we assumed for the observer is identical to the model of the actual plant (including the same initial conditions). Therefore, all

of the design requirements have been met with the minimal control effort expended. No further iteration is needed. This example demonstrates that it is much easier to control multi-input, multi-output systems with the state-space method than with the other methods we have presented. Inverted Pendulum: Digital Controller Design Key MATLAB commands used in are: ss , c2d , ctrb , obsv , dlqr , lsim , plotyy , eig , place Contents Discrete state-space Controllability and observability Control design via pole placement Precompensator design Observer design In this digital control version of the inverted pendulum problem, we will use the state-space method to design the digital controller. If you refer to theInverted Pendulum: System Modeling page, the linearized state-space equations were derived as: this tutorial

(1)

(2) where: (M) (m) (b) (l) (I) (F) (x) (theta) mass of the cart mass of the pendulum coefficient of friction for cart length to pendulum center of mass mass moment of inertia of the pendulum force applied to the cart cart position coordinate pendulum angle from vertical (down) 0.5 kg 0.2 kg 0.1 N/m/sec 0.3 m 0.006 kg.m^2

For this problem the outputs are the cart's displacement ( in meters) and the pendulum angle ( in radians) where represents the deviation of the pedulum's position from equilibrium, that is, = + . The design criteria for this system for a 0.2-m step in desired cart position Settling time for Rise time for and theta of less than 5 seconds are as follows:

of less than 0.5 seconds

Pendulum angle never more than 20 degrees (0.35 radians) from the vertical Steady-state error of less than 2% for Discrete state-space Our first step in designing a digital controller is to convert the above continuous state-space equations to a discrete form. We will accomplish this employing the MATLAB function c2d. This function requires that we specify three arguments: a continuous system model, the sampling time (Ts in sec/sample), and the 'method'. You should already be familiar with how to construct a state-space system from , , , and matrices. In choosing a sample time, note that it is desired that the sampling frequency be fast compared to the dynamics of the system. One measure of a system's "speed" is its closed-loop bandwidth. A good rule of thumb is that the sampling time be smaller than 1/30th of the closed-loop bandwidth frequency which can be determined from the closed-loop Bode plot. Assuming that the closed-loop bandwidth frequencies are around 1 rad/sec for both the cart and the pendulum, let the sampling time be 1/100 sec/sample. The discretization method we will use is the zeroorder hold ('zoh'). For further details, refer to the Introduction: Digital Controller Designpage. Now we are ready to use c2d function. Enter the following commands into an m-file. Running this m-file in the MATLAB command window gives you the following four matrices representing the discrete time statespace model. M = 0.5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3; and

p = I*(M+m)+M*m*l^2; %denominator for the A and B matrices

A = [0

0;

0 -(I+m*l^2)*b/p 0 0

(m^2*g*l^2)/p 0 m*g*l*(M+m)/p

0; 1; 0];

0 -(m*l*b)/p B = [ 0; (I+m*l^2)/p; 0; m*l/p]; C = [1 0 0 0; 0 0 1 0]; D = [0; 0];

states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'u'}; outputs = {'x'; 'phi'};

sys_ss = ss(A,B,C,D,'statename',states,'inputname',inputs,'outputname',outputs);

Ts = 1/100;

sys_d = c2d(sys_ss,Ts,'zoh') sys_d =

a = x x x_dot phi phi_dot 1 0 0 0 x_dot 0.009991 0.9982 -2.272e-05 -0.004544 phi 0.0001336 0.02672 1.002 0.3119 phi_dot 4.453e-07 0.0001336 0.01001 1.002

b = u x x_dot phi phi_dot 9.086e-05 0.01817 0.0002272 0.04544

c = x x phi 1 0 x_dot 0 0 phi 0 1 phi_dot 0 0

d = u x phi 0 0

Sample time: 0.01 seconds Discrete-time state-space model.

Now we have obtained the discrete state-space model of the form:

(3)

(4) Controllability and observability

The next step is to check the controllability and the observability of the system. For the system to be completely state controllable, the controllability matrix (5) must have the rank of n. The rank of the matrix is the number of independent rows (or columns). In the same token, for the system to be completely state observable, the observability matrix

(6) must also have the rank of n. These tests for controllability and observability are identical to the situation of continuous control except that now the state space model is discrete. Since the number of state variables in our system is 4, the rank of both matrices must be 4. The function rank can give you the rank of each matrix. Adding the following commands to your m-file and running in the MATLAB command window will generate the results shown below. co = ctrb(sys_d); ob = obsv(sys_d);

controllability = rank(co) observability = rank(ob) controllability = 4 observability = 4 This proves that our discrete system is both completely state controllable and completely state observable. Control design via pole placement The schematic of a full-state feedback control system is shown below.

The next step is to assume that all four states are measurable and design the control gain matrix . If you refer to the continuous Inverted Pendulum: State-Space Methods for Controller Design page the Linear Quadratic Regulator (LQR) method was used to find the control gain matrix . In this digital version, we will use the same LQR method. This method allows you to find the control gain that results in the optimal balance between system errors and control effort. Please consult your control textbook for details. To use this LQR method, we need to specify two parameters, the performance index matrix and the state-cost matrix . For simplicity, we will initially choose the performance index matrix equal to 1, and the statecost matrix equal to . The relative weightings of these two matrices will then be tuned by trial and error. The state-cost matrix has the following structure.

(7) The element in the (1,1) position of represents the weight on the cart's position and the element in the (3,3) position represents the weight on the pendulum's angle. Now we are ready to find the control gain matrix and observe the resulting closed-loop response of the system. Since we are designing a digital controller, we will specificially employ the MATLAB function dlqr. Add the following commands to a your m-file and run it in the MATLAB command window. Note that in the following we are overwriting the values of the state-space matrices , , , and with their discrete-time equivalents using the model derived with the c2d command above. A = sys_d.a; B = sys_d.b; C = sys_d.c; D = sys_d.d; Q = C'*C

R = 1; [K] = dlqr(A,B,Q,R)

Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D];

states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'r'}; outputs = {'x'; 'phi'};

sys_cl = ss(Ac,Bc,Cc,Dc,Ts,'statename',states,'inputname',inputs,'outputname',ou tputs);

t = 0:0.01:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with Digital LQR Control') Q = 1 0 0 0 K = -0.9384 -1.5656 18.0351 3.3368 0 0 0 0 0 0 1 0 0 0 0 0

The curve in green represents the pendulum's angle in radians, and the curve in blue represents the cart's position in meters. As you can see, this plot is not satisfactory. The pendulum and cart's overshoot appear fine, but their settling times need improvement and the cart's rise time needs to be reduced. As I'm sure you have noticed, the cart's final position is also not near the desired location but has in fact moved in the opposite direction. This error will be dealt with in the next section and right now we will focus on the settling and rise times. Go back to your m-file and change the matrix to see if you can get a better response. You will find that increasing the (1,1) and (3,3) elements makes the settling and rise times go down, and lowers the angle the pendulum moves. In other words, you are putting more weight on the errors at the cost of increased control effort . Modifying your m-file so that the (1,1) element of is 5000 and the (3,3) element is 100, will produce the following value of and the step response shown below. A = sys_d.a; B = sys_d.b; C = sys_d.c; D = sys_d.d; Q = C'*C; Q(1,1) = 5000; Q(3,3) = 100

R = 1; [K] = dlqr(A,B,Q,R)

Ac = [(A-B*K)]; Bc = [B]; Cc = [C]; Dc = [D];

states = {'x' 'x_dot' 'phi' 'phi_dot'}; inputs = {'r'}; outputs = {'x'; 'phi'};

sys_cl = ss(Ac,Bc,Cc,Dc,Ts,'statename',states,'inputname',inputs,'outputname',ou tputs);

t = 0:0.01:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with Digital LQR Control') Q = 5000 0 0 0 K = -61.9933 -33.5040 95.0597 18.8300 0 0 0 0 0 0 100 0 0 0 0 0

From this plot, we see that all design requirements are satisfied except the steady-state error of the cart position . We can easily correct this by introducing a feedforward scaling factor . Precompensator design Unlike other design methods, the full-state feedback system does not compare the output directly to the reference, rather, it compares the state vector multiplied by the control matrix ( ) to the reference (see the schematic shown above). Thus, we should not expect the output to converge to the commanded reference. To obtain the desired output, we need to scale the reference input so that the output equals the reference. This can be easily done by introducing a feedforward scaling factor . The basic full statefeedback schematic with scaling factor is shown below.

Unfortunately, we cannot use our user-defined function rscale to find because this function was defined for continuous-time single-output systems. We can, however, find the scaling factor by trial and error. After several trials, equal to -61.55 provided a satisfactory response. Adding the following commands to your m-file and running in the command window will generate the response shown below. Nbar = -61.55; sys_cl = ss(Ac,Bc*Nbar,Cc,Dc,Ts,'statename',states,'inputname',inputs,'outputnam e',outputs);

t = 0:0.01:5; r =0.2*ones(size(t)); [y,t,x]=lsim(sys_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot'); set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Response with Digital LQR Control and Precompensation')

Notice that the steady-state error of the cart's position has been eliminated. Now we have designed a system that satisfies all of the design requirements. Note, however, that the scaling factor was designed based on a model of the system. If our model is in error or there are unknown disturbances, then the steady-state error will no longer be driven to zero. Observer design The above response satisfies all of the design requirements, however, it was found assuming all state variables of the system are measurable. This assumption may not be valid for all systems. In this section, we develop a technique for estimating the state of the system based on the measured outputs and a model of the plant. The object that estimates the state of system is called an observer. Thus, in this section we will design a full-order state observer to estimate all of the system's state variables, including those that are measured. For further explanation on how an observer works, please consult your control textbook. A basic schematic of the observer-based state-feedback system is shown below.

Designing the observer equates to finding the observer gain matrix . To accomplish this, we need to first determine the closed-loop poles of the system without the observer (the eigenvalues of ). This can be achieved using the MATLAB command eig as shown below. poles = eig(A-B*K) poles = 0.9157 + 0.0728i 0.9157 - 0.0728i 0.9535 + 0.0079i 0.9535 - 0.0079i Since the observer is attempting to estimate the values of state variables which are themselves changing, it is desired that the dynamics of the observer be significantly faster than the dynamics of the closed-loop system without the observer. A common guideline is to make the estimator poles (eigenvalues of ) 4-10 times faster than the slowest controller pole (eigenvalue of ). Making the estimator poles too fast can be problematic if the measurement is corrupted by noise or there are errors in the sensor measurement in general. Based on the poles found above, we will place the observer poles at [-0.2 -0.21 -

0.22 -0.23]. These poles can be modified later, if necessary. We will use the MATLAB function place to find the matrix. Add the following code to your m-file and re-run in the command window to generate the observer gain matrix shown below. P = [-0.2 -0.21 -0.22 -0.23]; L = place(A',C',P)' L = 2.4308 147.6324 -0.0131 -1.8079 -0.0104 -1.2418 2.4305 147.9057

Now we will obtain the overall system response including the observer. Add the following commands to your m-file and run to generate the response shown below. Ace = [(A-B*K) (B*K); zeros(size(A)) (A-L*C)]; Bce = [B*Nbar; zeros(size(B))]; Cce = [Cc zeros(size(Cc))]; Dce = [0;0];

states = {'x' 'x_dot' 'phi' 'phi_dot' 'e1' 'e2' 'e3' 'e4'}; inputs = {'r'}; outputs = {'x'; 'phi'};

sys_est_cl = ss(Ace,Bce,Cce,Dce,Ts,'statename',states,'inputname',inputs,'outputname ',outputs);

t = 0:0.01:5; r = 0.2*ones(size(t)); [y,t,x]=lsim(sys_est_cl,r,t); [AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');

set(get(AX(1),'Ylabel'),'String','cart position (m)') set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)') title('Step Control') Response with Digital Observer-Based State-Feedback

This response is almost identical to the response achieved when it was assumed that we had full access to the state variables. This is because the observer poles are fast, and because the model we assumed for the observer is identical to the model of the actual plant (including the same initial conditions). Therefore, all of the design requirements have been met with the minimal control effort expended. No further iteration is needed. Suspension: Digital Controller Design Key MATLAB commands used are: ss , step , c2d , ssdata , tfdata , roots , place Contents Sampling time selection Continuous to discrete conversion Adding integral control in this tutorial

Designing the controller Simulating the closed-loop response In this example, we will design a digital state-space controller for the bus suspension control example. First we will convert the continuous-time model to a discrete-time model, and then use the pole placement method to design the controller. From the Suspension: System Modeling page. the state space model of the system is where Y1 = X1-X2.

(1)

(2)

(3) Where: (m1) (m2) (k1) (k2) (b1) (b2) (U) 1/4 bus body mass suspension mass spring constant of suspension system spring constant of wheel and tire damping constant of suspension system damping constant of wheel and tire control force 2500 kg 320 kg 80,000 N/m 500,000 N/m 350 N.s/m 15,020 N.s/m

The design requirements are: Overshoot: Output (X1-X2) less than 5% of disturbance (W) Settling time: Less than 5 seconds Sampling time selection The first step in the design of a discrete-time controller is to convert the continuous plant to its discrete time equivalent. First, we need to pick an appropriate sampling time, T. In this example, selection of sampling time is very important since a step in the road surface very quickly affects the output. Physically,

what happens is the road surface suddenly lifts the wheel, compressing the spring, K2, and the damper, b2. Since the suspension mass is relatively low, and the spring fairly stiff, the suspension mass rises quickly, increasing X2 almost immediately. Since the controller can only see the effect of the disturbance after a complete sampling period, we have to pick a sampling time, T, short enough so that the output (X1-X2) does not exceed the 5% requirement in one sampling period. To pick the sampling period, we need to closely examine the beginning of the step response. If you remember from the Suspension: System Analysis page, the output quickly goes negative in response to a step disturbance, and then begins to oscillate. We will simulate just the beginning of this response by setting the time vector input to the step function to range from 0 to .005. The response to a .1m step input is simulated by multiplying the B matrix by .1. Create a new m-file and enter the following code: m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020;

A 0

[0 0

-(b1*b2)/(m1*m2) -(b1/m1) b2/m2 1 k2/m2 0]; B = [0 1/m1 0 (1/m1)+(1/m2) C = [0 D = [0 0 0]; 1 0];

((b1/m1)*((b1/m1)+(b1/m2)+(b2/m2)))-(k1/m1) 0 0 -((b1/m1)+(b1/m2)+(b2/m2)) -((k1/m1)+(k1/m2)+(k2/m2))

0 (b1*b2)/(m1*m2) -(b2/m2) -(k2/m2)];

sys = ss(A,B,C,D);

step(sys*[0;1]*.1,0:0.0001:.005);

This plot shows that the spring, K1 compresses very quickly, and exceeds our requirement of 5mm in response to a 0.1-m step after only a little more than 0.001s. Therefore, we will set T=.0005s in order to give the controller a chance to respond. Continuous to discrete conversion Now that we have selected a sampling time, we can convert the plant to discrete time. MATLAB can be used to convert the above state space model, sus, to a discrete state space model, d_sys, by using the c2d command. The c2d command takes three arguments: the continuous time system, the sampling time, T, and the type of hold circuit. In this example we will use zero-order hold ('zoh'). Refer to the Introduction: Digital Controller Design tutorial. Add the following code to your m-file. Running in the command window, MATLAB should return the following, which represents the new discrete-time state-space model. T = .0005; d_sys = c2d(sys,T,'zoh') d_sys =

a =

x1 x1 x2 x3 x4 1 -0.003461 0.02338 0.7705

x2 0.0005 1 5.852e-06 0.0001935

x3 -3.126e-06 -0.01244 0.976 -0.9112

x4 -1.802e-08 -7.313e-05 0.000494 0.9998

b = u1 x1 x2 x3 x4 4.999e-11 1.999e-07 4.375e-10 1.762e-06 u2 8.506e-07 0.003461 -0.02338 -0.7705

c = x1 y1 0 x2 0 x3 1 x4 0

d = u1 y1 0 u2 0

Sample time: 0.0005 seconds Discrete-time state-space model.

Adding integral control In this example, we will need to add an integrator to the system in order to drive the steady-state response to zero. We will add this integrator in series with the plant. This will have the effect of adding another state to the plant. An integrator in discrete-time state space can be represented as a trapezoidal approximation of integration over each sample period as follows: (4)

(5) To add this, add the following commands in your m-file: Ai = 1; Bi = T; Ci = 1; Di = T/2; [As,Bs,Cs,Ds]=ssdata(d_sys); Aa = [As, zeros(4,1); Bi*Cs, Ba = [Bs; 0,0]; Ca = [Cs, 0]; Da = Ds; Ai];

d_sys_int = ss(Aa,Ba,Ca,Da,T); [Ad,Bd,Cd,Dd] = ssdata(d_sys_int); Designing the controller The structure of the controller is similar to the structure of the continuous-time state-space controller. We will now use the place command to compute the gain matrix, K, which will, in feedback, give us the desired closed-loop poles. We first need to decide where to place the closed-loop poles. Since we get to place all five of the closedloop poles, we can be very selective about where they go. In particular, we can place them to cancel all of the plant zeros, as well as give us the desired response. First, we will find the plant zeros by converting the plant's digital state equations to a transfer function, and then finding the roots of the numerator. We will use the tfdata command which takes a system (d_sys) as its argument and outputs a transfer function numerator and denominator. Since the suspension system (d_sys) has two inputs, only one input must be selected. Notice how the first input is selected in the following code. Add the following code to your m-file: sys1 = d_sys_int*[1;0]; %select the first input [num,den] = tfdata(sys1,'v'); z = roots(num)

z = -0.9929 0.9986 + 0.0065i 0.9986 - 0.0065i We will select these three zeros as three of our desired closed-loop poles. One of the other two will be selected at .9992 since a pole there settles in approximately 10000 samples (or 5 seconds). The last pole will be selected at z=.5 since this is sufficiently fast to be insignificant. Add the following code to your mfile: p1 = z(1); p2 = z(2); p3 = z(3); p4 = .9992; p5 = .5;

K=place(Ad,Bd*[1;0],[p1 p2 p3 p4 p5]) K = 1.0e+09 * 0.0534 0.0000 1.0898 0.0011 1.8286

Simulating the closed-loop response We can use the step command to simulate the closed-loop response. Since multiplying the state vector by K in our controller only returns a single signal, U, we need to add a row of zeros to K by multiplying it by [1 0]T. This is identical to what was done in the continuous design to compensate for the fact that there are two inputs to the plant, but only one is a control input. We will simulate with a negative 0.1-m step disturbance in the road to give us a positive deflection of the bus for aesthetic reasons. Enter the following code into your m-file: d_sys_cl = ss(Ad-Bd*[1;0]*K,Bd,Cd,Dd,T); step(-.1*d_sys_cl*[0;1],5);

We can see in this plot, that the overshoot is less than 5mm, and the response settles well within 5 seconds. Changing the axes allows a better view of the suspension's initial response. d_sys_cl = ss(Ad-Bd*[1;0]*K,Bd,Cd,Dd,T); step(-.1*d_sys_cl*[0;1],5); axis([0 0.1 -.01 .01])

Published with MATLAB 7.14

DC Motor Position: Digital Controller Design Key MATLAB commands used are: tf , c2d , minreal , zpk , feedback , stairs , step Contents Creating a sampled-date model of the plant Root locus design In this example, the controller will be designed applying a Root Locus method. A sample-data DC motor model can be obtained from conversion of an analog DC motor model, as we will describe. According to the DC Motor Position: System Modeling page, the continuos open-loop transfer function for DC motor's position in the Laplace domain is the following. (1) The structure of the control system has the form shown in the figure below. in this tutorial

Also from the main page, the design requirements for a 1-radian step reference are the following. Settling time less than 0.040 seconds Overshoot less than 16% No steady-state error, even in the presence of a step disturbance input Creating a sampled-date model of the plant The first step in the design of a digital control system is to generate a sampled-data model of the plant. Therefore, it is necessary to choose a frequency with which the continuous-time plant is sampled. In choosing a sampling period, it is desired that the sampling frequency be fast compared to the dynamics of the system in order that the sampled output of the system captures the system's full behavior, that is, so that significant inter-sample behavior isn't missed. Let's create a continuous-time model of the plant. Create a new m-file and add the following MATLAB code (refer to the main problem for the details of getting these commands). Running the m-file within the MATLAB command window will generate the output shown below. J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; s = tf('s'); P_motor = K/(s*((J*s+b)*(L*s+R)+K^2)); zpk(P_motor) ans =

3086245930.9988 -----------------------s (s+1.454e06) (s+59.23)

Continuous-time zero/pole/gain model.

The use of the zpk command above transforms the transfer function into a form where the zeros, poles, and gain can be seen explicitly. Examining the poles of the plant (or its frequency response), it is clear that the pole at -1.45e06 contributes very little to the response of the plant. The gain crossover frequency of the plant is approximately 5 Hz. Therefore, choosing a sampling period of 0.001 seconds (frequency of 1000 Hz) is significantly faster than the dynamics of the plant. This sampling period is also fast compared to the speed that will be achieved by the resultant closed-loop system. A sample time of 0.001 seconds is specifically 1/100 of the required time constant and 1/40 of the required settling time. In this case, we will convert the given transfer function from the continuous Laplace domain to the discrete z-domain. MATLAB can be used to achieve this conversion through the use of the c2d command. The c2d command requires three arguments: a system model, the sampling time (Ts), and the type of hold circuit. In this example, we will assume a zero-order hold (zoh) circuit. Refer to the Introduction: Digital Controller Design page for further details. Adding the following commands to your m-file and running in the MATLAB command window generates the sampled-data model shown below. Ts = 0.001; dP_motor = c2d(P_motor, Ts, 'zoh'); zpk(dP_motor) ans =

0.0010389 (z+0.9831) (z+9.256e-07) ---------------------------------z (z-1) (z-0.9425)

Sample time: 0.001 seconds Discrete-time zero/pole/gain model.

From the above, notice that there is a pole and zero very near to z = 0 that effectively cancel. This cancellation in the transfer function can be accomplished by applying the minreal command with a

tolerance of 0.001. Cancellation of this pole and zero will reduce the order of our transfer function and will help to avoid numerical difficulties in MATLAB. Applying the minreal command, therefore, produces the following reduced order transfer function. Note the absence of the pole and zero near z = 0. dP_motor = minreal(dP_motor,0.001); zpk(dP_motor) ans =

0.0010389 (z+0.9831) -------------------(z-1) (z-0.9425)

Sample time: 0.001 seconds Discrete-time zero/pole/gain model.

We would now like to analyze the closed-loop response of the system without any additional compensation. First, we have to close the loop of the transfer function by using the feedback command. After closing the loop, let's inspect the closed-loop step response with the zero-order hold. This can be accomplished by using the step and stairs commands. Since the step command is fed a discrete model, it will output a vector of discrete samples at the sample time Ts associated with the model (click here for further details). The syntax below specifies to simulate the step response for 0.5 seconds. The stairs command draws these discrete data points as a stairstep, just like what would be produced by a zero-order hold circuit. Add the following code at the end of your previous m-file and rerun it. You should generate a plot like the one shown below. sys_cl = feedback(dP_motor,1); [x1,t] = step(sys_cl,.5); stairs(t,x1) xlabel('Time (seconds)') ylabel('Position (radians)') title('Stairstep Response: Original') grid

Root locus design We will now employ a root locus design methodology to generate a digital controller for our system. The main idea of a root locus approach to design is to modify the closed-loop response by placing the closedloop poles of the system at desired locations. Adding poles and zeros to the the original open-loop transfer function via the controller allows us to reshape the root locus in order to place the closed-loop poles at the desired locations. This approach can also be used for discrete-time models. We will use the SISO Design Tool graphical user interface (GUI) for designing our controller. Adding the command sisotool('rlocus',dP_motor) to your m-file will open the SISO Design Tool when run at the command line. Two windows will open initially with the SISO Design for SISO Design Task window having the form shown in the figure below. This figure shows the root locus of the transfer function d_Pmotor passed through the sisotool function. This figure appears initially due to the addition of the string 'rlocus' to the function call. If the string 'rlocus' is omitted, the default initial window includes the open-loop Bode plot in addition to the root locus plot.

Recall from our previous controller designs that we have added an integrator to our compensator in order to reject the effect of a constant disturbance in steady state. We can add integral control through the SISO Design Tool interface under the Compensator Editor tab of the Control and Estimation Tools Manager window. Specifically, an integrator is added to our controller by right-clicking on the Dynamics section of the window and selecting Add Pole/Zero > Integrator from the resulting menu. The figure below illustrates what the Control and Estimation Tools Manager window should look like.

Integral control in the continuous-time domain is 1 / s. The SISO tool uses a forward difference approximation for mapping from the s-plane to the z-plane as described by s = (z-1) / Ts, where Ts is the sample time being employed. Therefore, the addition of the integrator will add an additional pole at 1 on the root locus plot. See below for the resulting root locus plot where the red x denotes the location of the compensator pole at z = 1.

The default format for compensators in the SISO Design Tool is Time constant form. For the remainder of this problem, we will design the compensator in Zero/pole/gain form. This change can be made by choosing SISO Tool Preferences ... from the Edit menu of the Control and Estimation Tools Manager window. The compensator parameterization can then be changed under the Options tab. By default, the root locus plot includes the unit circle shown in the above figure by the dashed line. Recall that for a discrete-time system, closed-loop poles located within the unit circle indicate a stable closed-loop system. From the above, we can see that the after adding the extra pole at 1, the root locus had three poles near 1. This caused the root locus to move to the right (outside of the unit circle) indicating that the closedloop response will be more unstable. Therefore, we will add a zero near 1, inside the unit circle, to cancel one of the poles and pull the root locus in. We will specifically add a zero at z = 0.95. In general, we must add at least as many poles as zeros for

the controller to be causal. This zero can be added to our compensator in the same manner as the integrator was added above. The only difference is that you will choose Real Zero from the right-click menu and then must specify in theLocation cell that the zero is to be placed at 0.95. The root locus plot should now appear as shown below.

The addition of the integrator has helped with the requirement on disturbance rejections, however, the above root locus indicates that the closed-loop system cannot be made robustly stable through further tuning of the loop gain. We also have not addressed our other design requirements. To help us in this regard, let's identify the region of the complex plane where the closed-loop poles must be located. These regions can be added to our root locus by choosing Design Requirements from the right-click menu. Then choose new and set the overshoot to be less than 16%. This process can then be repeated to add the settling time requirement of less than 0.04 seconds. The resulting figure is shown below where the

unshaded region identifies where the closed-loop poles must be located to meet our given transient requirements. These regions assume a canonical second-order system, which we do not have currently, but the regions can still help guide our design.

From the above figure, we can see that the two dominant branches of our root locus do not pass through the required region. Therefore, we need to add poles and/or zeros to our compensator to bend the root locus into the desired region. Let's first try cancelling the zero at approximately -0.98 by adding a pole nearby. This can be done in the same manner described above for the integrator and the real zero. It is desirable to cancel this zero since it will add overshoot to the step response. The addition of this pole will result in two of the branches of the root locus going off to the right and outside of the unit circle. We will attempt to pull these branches in by placing two additional zeros near the desired closed-loop pole locations. These zeros can be added via theCompensator Editor tab as done above. In this case, however,

we will place the zeros using a graphical tuning approach. Specifically, select a single ofrom the tool bar at the top of the SISO Design for SISO Design Task window. Then click the resulting "pointer" on the real axis to place the zero. Repeat this process to add a second zero. The effect of moving these two zeros can then be seen by moving the pointer over the corresponding o on the root locus and clicking on it. While holding down the mouse button, you can then move the zero along the real axis to visualize the effect of different locations. Through trial and error we arrive at a location of 0.8 for the two zeros. In order to make the controller causal, we need to add an additional pole. Using a graphical approach again, we can add a real pole by choosing an x from the tool bar at the top of the SISO Design for SISO Design Taskwindow. Through trial and eror we arrive at a pole location of 0.6. The resulting root locus is shown below.

Next we must choose a loop gain in order to move the closed-loop pole locations along the root locus to the desired location. Since our closed-loop system is not canonical second order, we will have to employ

some trial and error to identify the exact pole locations. In order to assist us in this regard, we can open a plot for the closed-loop step response so that we can observe the effect of the gain changes on the actual step response, without having to rely on a second-order idealization. The closed-loop step response plot is opened from the Control and Estimation Tools Manager window under the Analysis Plots tab as shown below. In this tab, select Step from the drop-down window for Plot 1 under the Analysis Plots section of the window. Then under the Contents of Plotssection of the window check the box for Plot 1 on the first row, which corresponds to a plot for the Closed Loop r to y. Finally, click the Show Analysis Plots button to produce the closed-loop step response.

The resulting step response plot with loop gain of 1 appears to meet the settling time requirement, but not the overshoot requirement. In order to be certain, we can define the allowed step response shape by choosing Design Requirements from the right-click menu of the step response plot and choosing the

overshoot to be 16% and the settling time to be 0.040 seconds. Note that you will need to choose a rise time of less than 0.040 seconds, even though we have no such requirement. We will now modify the loop gain to meet the overshoot requirement. Using a graphical tuning approach, grab one of the pink boxes on the root locus plot by clicking on it, then drag the box along the locus. Each box represents the location of a closed-loop pole and dragging the box along the locus changes the loop gain to the value that places that closed-loop pole at that specified location. Through trial and error, a loop gain of 800 reduces the overshoot of the associated step response to approxomately 10% while achieving a settling time of approximately 0.02 seconds. Furthermore, the steady-state error goes to zero when there is no disturbance present. The resulting step response plot is shown below.

The only thing now left to verify is the system's disturbance rejection properties. A plot of the system's response to a step disturbance can be generated from the Analysis menu of the SISO Design for SISO Design Task window. Instead, we will generate the disturbance response plot from the MATLAB command line. Before we do that, however, we must first export the compensator we have designed in the SISO Design Tool to the workspace. This is accomplished by selecting Export from the File menu of the Control and Estimation Tools Manager window. You then select Compensator C and click on

the Export to Workspace button. Once you have done this, then enter the following instructions in the command window to produce the response plot shown below. dist_cl = feedback(dP_motor,C); [x2,t] = step(dist_cl,.25); stairs(t,x2) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Disturbance Response with Compensation') grid

Examination of the above shows that for a step disturbance, the steady-state error will still go to zero. Employing the property of superposition, it is also apparent that the total response will have overshoot less than 16% and will reach steady-state well within the 40 millisecond requirement. DC Motor Speed: Digital Controller Design Key MATLAB commands used in this tutorial are: tf , c2d , feedback , step , rlocus Contents

Creating a sampled-data model of the plant PID Controller In this page, we will consider the digital version of the DC motor speed control problem. A sampled-data DC motor model can be obtained from conversion of the analog model, as we will describe. In this example we will design a PID controller. Thr continuous open-loop transfer function for an input of armature voltage and an output of angular speed was derived previously as the following. (1) For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page. For a 1-rad/sec step reference, the design criteria are the following. Settling time less than 2 seconds Overshoot less than 5% Steady-state error less than 1% Creating a sampled-data model of the plant The first step in the design of a digital control system is to generate a sampled-data model of the plant. Therefore, it is necessary to choose a frequency with which the continuous-time plant is sampled. In choosing a sampling period, it is desired that the sampling frequency be fast compared to the dynamics of the system in order that the sampled output of the system captures the system's full behavior, that is, so that significant inter-sample behavior isn't missed. Let's create a continuous-time model of the plant. Create a new m-file and add the following MATLAB code (refer to the main problem for the details of getting these commands). Running the m-file within the MATLAB command window will generate the output shown below. J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2); zpk(P_motor) ans =

2 ------------------(s+9.997) (s+2.003)

Continuous-time zero/pole/gain model.

The use of the zpk command above transforms the transfer function into a form where the zeros, poles, and gain can be seen explicitly. Examining the poles of the plant (or its frequency response), the dominant pole of the plant (sigma approximately equal to 2) corresponds to a settle time of approximately 2 seconds (4 / sigma). Therefore, choosing a sampling period of 0.05 seconds is significantly faster than the dynamics of the plant. This sampling period is also fast compared to the speed that will be achieved by the resultant closed-loop system. In this case, we will convert the given transfer function from the continuous Laplace domain to the discrete z-domain. MATLAB can be used to achieve this conversion through the use of the c2d command. The c2d command requires three arguments: a system model, the sampling time (Ts), and the type of hold circuit. In this example, we will assume a zero-order hold (zoh) circuit. Refer to the Introduction: Digital Controller Design page for further details. Adding the following commands to your m-file and running in the MATLAB command window generates the sampled-data model shown below. Ts = 0.05; dP_motor = c2d(P_motor,Ts,'zoh'); zpk(dP_motor) ans =

0.0020586 (z+0.8189) --------------------(z-0.9047) (z-0.6066)

Sample time: 0.05 seconds Discrete-time zero/pole/gain model.

We would now like to analyze the closed-loop response of the system without any additional compensation. First, we have to close the loop of the transfer function by using the feedback command. After closing the loop, let's inspect the closed-loop step response with the zero-order hold. This can be accomplished by using the step and stairs commands. Since the step command is fed a discrete model, it will output a vector of discrete samples at the sample time Ts associated with the model (click here for further details). The syntax below specifies to simulate the step response for 0.5 seconds. The stairs command draws these discrete data points as a stairstep, just like what would be produced by a zero-order hold circuit. Add the following MATLAB code at the end of your previous m-file and rerun it. You should generate the plot shown below. sys_cl = feedback(dP_motor,1); [y,t] = step(sys_cl,12); stairs(t,y); xlabel('Time (s)') ylabel('Velocity (rad/s)') title('Stairstep Response: Original')

Examination of the above shows that the steady-state error is much too large and the settle time may be a little slow.

PID Controller Recall that the continuous-time transfer function for a PID controller is: (2) There are several ways for mapping from the s-plane to z-plane. Above we used a zero-order hold conversion for the plant model because that reflected the type of hold circuit that would be used in sampling the signals from the plant in a physical implementation of the control system. For the controller, we may prefer to use a conversion that more accurately approximates the behavior that would be achieved with a continuous, rather than digital, controller. The exact conversion between the Laplace and z-domains is given below. (3) This conversion, however, involves a trancendental function and the resulting transfer function cannot be represented as a ratio of polynomials. This makes it difficult to implement such a control algorithm on a digital computer. Therefore, we will use an approximate conversion. In particular, we are going to use the bilinear transformation shown below. (4) Equivalently, we will again use the c2d command in MATLAB to convert the continuous-time PID compensator to a discrete-time PID compensator by specifying 'tustin' as the conversion method. Tustin's method uses the bilinear transformation to convert a continuous model to discrete time. According to the DC Motor Speed: PID Controller Design page , Kp = 100, Ki = 200 and Kd = 10 were found to satisfy all of the given design requirements. We will use these gains again for this example. Now add the following MATLAB commands to your previous m-file and rerun it in the MATLAB command window. Kp = 100; Ki = 200; Kd = 10;

C = Kp + Ki/s + Kd*s; dC = c2d(C,Ts,'tustin') dC =

505 z^2 - 790 z + 305 --------------------z^2 - 1

Sample time: 0.05 seconds Discrete-time transfer function.

Let's see if the performance of the closed-loop response with PID compensator satisfies the given design requirements. Add the following code to the end of your m-file and rerun it. You should get the following closed-loop stairstep response. sys_cl = feedback(dC*dP_motor,1); [x2,t] = step(sys_cl,12); stairs(t,x2) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response: with PID controller')

As you can see from the above plot, the closed-loop response of the system is unstable. Therefore, something must be wrong with the compensated system. We should take a look at the root locus of the compensated system. Add the following MATLAB commands onto the end of your m-file and rerun it.

rlocus(dC*dP_motor) axis([-1.5 1.5 -1 1]) title('Root Locus of Compensated System')

From this root-locus plot, we see that the denominator of the PID controller has a pole at -1 in the z-plane. We know that if a pole of a system is outside the unit circle, the system will be unstable. This compensated system will always be unstable for any positive gain because there are an even number of poles and zeros to the right of the pole at -1. Therefore, a closed-loop pole will always move to the left and outside the unit circle as the loop gain is increased. The pole at -1 comes from the compensator, hence we can change its location by changing the compensator design. In this case we choose to cancel the zero at -0.82, this will make the system stable for at least some gains. Add the following code to your m-file and rerun it at the command line to generate the root locus plot shown below. z = tf('z',Ts); dC = dC/(z+0.82); rlocus(dC*dP_motor); axis([-1.5 1.5 -1 1]) title('Root Locus of Compensated System');

The new closed-loop system will have a pole near -0.82 instead of -1, which almost cancels the zero of the uncompensated plant. Click on the root locus curve in several places to see the gain that places a closedloop pole at a particular location, along with the corresponding overshoot, damping, etc., as shown in the figure below. Of course, the damping and overshoot numbers correspond to the actual response only if the selected closed-loop pole is dominant.

You can also determine the gain corresponding to a specfic closed-loop pole location using the rlocfind command. Typing [K,poles] = rlocfind(dC*dP_motor) at the command line will generate a cursor that you can then use to click on the point of interest on the root locus. MATLAB will then return the appropriate gain K and all of the corresponding closed-loop poles. This is useful in that it lists all of the closed-loop poles, not just the point you clicked on. We will choose a gain of 0.8 and examine the resulting closed-loop step response by typing the following commands at the MATLAB command window. sys_cl = feedback(0.8*dC*dP_motor,1); [x3,t] = step(sys_cl,8); stairs(t,x3) xlabel('Time (seconds)') ylabel('Velocity (rad/s)') title('Stairstep Response: with Modified PID controller')

The plot above shows that the settling time is less than 2 seconds and the percent overshoot is around 2%. Additionally, the steady-state error is zero. Therefore, this response satisfies all of the given design requirements. Cruise Control: Digital Controller Design In this digital control version of the cruise control problem, we are going to use the root-locus design method to design the digital controller. Key MATLAB commands used in this tutorial are: tf , c2d , rlocus , zgrid , feedback , step Contents System model System parameters Performance specifications Discrete-time transfer function Root locus in the z-Plane Compensation using a digital controller System model

The transfer function model for the cruise control problem is given below. Please see the Cruise Control: System Modeling page for the derivation. (1) System parameters For this example, let's assume that parameters of the system are (m) (b) (r) (u) vehicle mass damping coefficient reference speed nominal control force 1000 kg 50 N.s/m 10 m/s 500 N

Performance specifications We need to design a controller to meet the following design criteria for this problem: Rise time < 5 s Overshoot < 10% Steady-state error < 2% Discrete-time transfer function The first step in performing a discrete analysis of a system is to find the discrete-time equivalent transfer function of the continuous portion. We will convert the above transfer function (Y(s)/U(s)) to a discretetime transfer function using the MATLAB function c2d. To use this function, you need to specify three arguments: system, sampling time (Ts), and the 'method'. You should already be familiar with how to enter num and den matrices. The sampling time (Ts), in the unit of sec/sample, should be smaller than 1/(30*BW), where BW is the closed-loop bandwidth frequency. For the method, we will use the zero-order hold ('zoh'). Let the sampling time equal 1/50 sec; this is sufficiently fast assuming that the bandwidth frequency is 1 rad/sec. Now enter the following commands into an m-file and run it in the command window. m = 1000; b = 50; u = 500;

s = tf('s'); P_cruise = 1/(m*s+b);

Ts = 1/50;

dP_cruise = c2d(P_cruise,Ts,'zoh') dP_cruise =

1.999e-05 --------z - 0.999

Sample time: 0.02 seconds Discrete-time transfer function.

Root locus in the z-Plane Recall from the Digital Control Tutorial page, the MATLAB function zgrid can be used to find an acceptable region of the discrete root-locus that gives the desired gain (K). The zgrid command requires two arguments: Natural frequency (Wn) and damping ratio (zeta). These two arguments can be found from the rise time and the overshoot requirements and the following two equations. (2) (3) where, zeta = Damping ratio Wn = Natural frequency (rad/sec) Tr = Rise time %OS = Maximum overshoot Since our rise time and overshoot requirements are 5 seconds and 10%, respectively, we can determine that the natural frequency (Wn) must be greater than 0.36 rad/sec and the damping ratio (zeta) must be greater than 0.6. Let's generate the root-locus and use the zgrid command to find the acceptable region of the root-locus. But before doing that, if you refer to the Digital Control Tutorial, the natural frequency argument

for zgrid needs to be in units of rad/sample, so let Wn = 0.36*Ts = 0.0072 rad/sample. Now add the following commands to the above m-file and rerun it. You should get the following plot. Wn = 0.0072; zeta = 0.6;

rlocus(dP_cruise) zgrid(zeta, Wn) axis ([-1 1 -1 1])

The region of the complex plane which interests us is that which is near the point (1,0), so you should zoom in on this point. Rerun using the following axis command and your figure should now appear as shown below. axis ([0.95 1 -.1 .1])

The dotted line on the right, indicates the locations of constant natural frequency (Wn), and the natural frequency is greater than 0.0072 outside the line. The other dotted line indicates the locations of constant damping ratio (zeta), and the damping ratio is greater than 0.6 inside the line. The jagged vertical line is a portion of the unit circle which is calculated at low resolution (hence the jaggedness). In the above plot, you can see that part of the root locus is inside the desired region. Let's find a specific gain (K) using the MATLAB function rlocfindand then obtain the corresponding step response. Running the command [K,poles] = rlocfind(dP_cruise) in the MATLAB command window will generate a prompt directing you to select a point on the root locus. Remember that if you choose a pole which is too far inside the unit circle, then the step response will be too fast indicating a physically unreasonable acceleration. Therefore, you should choose the pole which is near the intersection of the constant natural frequency and the real axis. Select the point near 0.99 as shown by the cross mark on the following root locus graph.

After selecting a point, you should see the following output in the MATLAB command window indicating the point you chose, the point on the root locus closest to that point (the pole), and the gain K that places the closed-loop pole at that location. Select a point in the graphics window

selected_point =

0.9900 - 0.0003i

K =

451.1104

poles =

0.9900

Then to see the closed-loop step response, add the following code to your m-file. K = 451.1104; sys_cl = feedback(K*dP_cruise,1); r = 10; figure step(r*sys_cl,10);

This response satisfies the rise time and overshoot requirements. But the steady-state error is about 11%. To obtain the desired steady-state error, we will modify the digital controller. Compensation using a digital controller Recall from the continuous Cruise Control: Root Locus page, a lag compensator was added to the system to obtain the desired response. In this digital control version of the cruise control problem, we will modify the existing digital controller by adding additional lag compensation of the form shown below. (4) There is a guideline for the design digital lead and lag compensators and a guideline for the design of continuous-time lead and lag compensators. The discrete design method described says that the zero of the lag compensator should be chosen to (approximately) cancel one of the plant poles, as long as it is stable. Thus, we choose the zero to be at zo = 0.999. To reduce the steady-state error, we note that the low frequency gain of the discrete-time control system with a lag compensator is increased by a factor of (1 - zo)/(1 - po). To reduce the steady-state error by a factor of 5, we choose po = 0.9998. To have a gain of 1 at zero frequency, the numerator is multiplied by Kd = (1 - zp)/(1 - zo) = 0.2 before using the root locus. Note that the whole compensator is multiplied by the loop gain determined from the root locus. Now we have the discrete-time compensator transfer function. Let's generate the root-locus and obtain the step response. First create a new m-file and enter the following commands. m = 1000; b = 50; u = 500;

s = tf('s'); P_cruise = 1/(m*s+b); Ts = 1/50; dP_cruise = c2d(P_cruise,Ts,'zoh');

z = tf('z',Ts); C = 0.2*(z - 0.999)/(z - 0.9998);

Wn = 0.0072; zeta = 0.6;

rlocus(C*dP_cruise) zgrid(zeta, Wn) axis([0.98 1 -0.01 0.01])

Then enter the command [K,poles] = rlocfind(C*dP_cruise) at the command line and again choose a pole location near 0.99 as indicated by the cross marks in the following figure.

After selecting a point, you should see the following output in the MATLAB command window indicating the point you chose, the point on the root locus closest to that point (the pole), and the gain K that places the closed-loop pole at that location. Select a point in the graphics window

selected_point =

0.9900 - 0.0000i

K =

2.4454e+03

poles =

0.9900 0.9900

Finally, to see the closed-loop step response, add the following code to your m-file. K = 2.4454e+03; sys_cl = feedback(K*C*dP_cruise,1); r = 10; step(r*sys_cl,10);

This response rises about as fast as before, but the steady-state error has been reduced to 2%. This system satisfies all of the design requirements while requiring a reasonable amount of control effort. Note: A design problem does not necessarily have a unique answer. For practice, you may try other compensators to obtain a better response than the one shown above.

Cruise Control: Simulink Modeling Contents Physical setup and system equations Building the model Open-loop response Physical setup and system equations The model of the cruise control system is relatively simple. If it is assumed that rolling resistance and air drag are proportional to the car's speed, then the problem is reduced to the simple mass and damper system shown below.

Using Newton's 2nd law, the governing equation for this system becomes: (1) where u is the force generated between the road/tire interface and can be controlled directly. For this example, let's assume that m = 1000 kg b = 50 N.sec/m u = 500 N Building the model

This system will be modeled by summing the forces acting on the mass and integrating the acceleration to give the velocity. Open Simulink and open a new model window. First, we will model the integral of acceleration. (2) Insert an Integrator Block (from the Continuous library) and draw lines to and from its input and output terminals. Label the input line "vdot" and the output line "v" as shown below. To add such a label, double click in the empty space just above the line.

Since the acceleration (dv/dt) is equal to the sum of the forces divided by mass, we will divide the incoming signal by the mass. Insert a Gain block (from the Math Operations library) connected to the Integrator block input line and draw a line leading to the input of the Gain block. Edit the Gain block by double-clicking on it and change its value to "1/m". Change the label of the Gain block to "inertia" by clicking on the word "Gain" underneath the block.

Now, we will add in the forces which are represented in Equation (1). First, we will add in the damping force. Attach a Sum block (from the Math Operations library) to the line leading to the inertia Gain block. Change the signs of the Sum block to "+-". Insert a Gain block below the Inertia block, select it by single-clicking on it, and select Flip from the Format menu (or type Ctrl-F) to flip it left-to-right. Set the block's value to "b" and rename this block to "damping". Tap a line (hold Ctrl while drawing) off the Integrator block's output and connect it to the input of the damping Gain block. Draw a line from the damping Gain block output to the negative input of the Sum Block.

The second force acting on the mass is the control input, u. We will apply a step input. Insert a Step block (from the Sources library) and connect it with a line to the positive input of the Sum Block. To view the output velocity, insert a Scope block (from the Sinks library) connected to the output of the Integrator.

To provide an appropriate step input of 500 at time equal zero, double-click the Step block and set the Step Time to "0" and the Final Value to "u".

You can download a model file for the complete system here, ccmodel.mdl. Open-loop response To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "120" in the Stop Time field. 120 seconds is long enough to view the open-loop response.

The physical parameters must now be set. Run the following commands at the MATLAB prompt: m = 1000; b = 50; u = 500; Run the simulation (hit Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-click on the Scope and hit its autoscale button. You should see the following output.

Observing the above, we would like to improve the response of the cruise control system. The model created here will be employed for controller design and analysis within Simulink in the Cruise Control: Simulink Controller Design page.

DC Motor Speed: Simulink Modeling Contents Physical setup Building the model with Simulink Building the model with Simscape Physical setup A common actuator in control systems is the DC motor. It directly provides rotary motion and, coupled with wheels or drums and cables, can provide translational motion. The electric circuit of the armature and the free-body diagram of the rotor are shown in the following figure:

For this example, we will assume that the input of the system is the voltage source (V) applied to the motor's armature, while the output is the rotational speed of the shaft d(theta)/dt. The rotor and shaft are assumed to be rigid. We further assume a viscous friction model, that is, the friction torque is proportional to shaft angular velocity. The physical parameters for our example are: (J) (b) (Ke) (Kt) (R) (L) moment of inertia of the rotor motor viscous friction constant electromotive force constant motor torque constant electric resistance electric inductance 0.01 kg.m^2 0.1 N.m.s 0.01 V/rad/sec 0.01 N.m/Amp 1 Ohm 0.5 H

In general, the torque generated by a DC motor is proportional to the armature current and the strength of the magnetic field. In this example we will assume that the magnetic field is constant and, therefore, that the motor torque is proportional to only the armature current i by a constant factor Kt as shown in the equation below. This is referred to as an armature-controlled motor. (1) The back emf, e, is proportional to the angular velocity of the shaft by a constant factor Ke. (2) In SI units, the motor torque and back emf constants are equal, that is, Kt = Ke; therefore, we will use K to represent both the motor torque constant and the back emf constant. Building the model with Simulink

This system will be modeled by summing the torques acting on the rotor inertia and integrating the acceleration to give velocity. Also, Kirchoff's laws will be applied to the armature circuit. First, we will model the integrals of the rotational acceleration and of the rate of change of the armature current. (3) (4) To build the simulation model, open Simulink and open a new model window. Then follow the steps listed below. Insert an Integrator block from the Simulink/Continuous library and draw lines to and from its input and output terminals. Label the input line "d2/dt2(theta)" and the output line "d/dt(theta)" as shown below. To add such a label, double-click in the empty space just below the line. Insert another Integrator block above the previous one and draw lines to and from its input and output terminals. Label the input line "d/dt(i)" and the output line "i".

Next, we will apply Newton's law and Kirchoff's law to the motor system to generate the following equations:

(5) (6) The angular acceleration is equal to 1 / J multiplied by the sum of two terms (one positive, one negative). Similarly, the derivative of current is equal to 1 /L multiplied by the sum of three terms (one positive, two negative). Continuing to model these equations in Simulink, follow the steps given below. Insert two Gain blocks from the Simulink/Math Operations library, one attached to each of the integrators. Edit the Gain block corresponding to angular acceleration by double-clicking it and changing its value to "1/J". Change the label of this Gain block to "Inertia" by clicking on the word "Gain" underneath the block. Similarly, edit the other Gain's value to "1/L" and its label to "Inductance". Insert two Add blocks from the Simulink/Math Operations library, one attached by a line to each of the Gain blocks. Edit the signs of the Add block corresponding to rotation to "+-" since one term is positive and one is negative. Edit the signs of the other Add block to "-+-" to represent the signs of the terms in the electrical equation.

Now, we will add in the torques which are represented in the rotational equation. First, we will add in the damping torque.

Insert a Gain block below the "Inertia" block. Next right-click on the block and select Format > Flip Block from the resulting menu to flip the block from left to right. You can also flip a selected block by holding down Ctrl-I. Set the Gain value to "b" and rename this block to "Damping". Tap a line (hold Ctrl while drawing or right-click on the line) off the rotational Integrator's output and connect it to the input of the "Damping" block. Draw a line from the "Damping" block output to the negative input of the rotational Add block. Next, we will add in the torque from the armature. Insert a Gain block attached to the positive input of the rotational Add block with a line. Edit its value to "K" to represent the motor constant and Label it "Kt". Continue drawing the line leading from the current Integrator and connect it to the "Kt" block.

Now, we will add in the voltage terms which are represented in the electrical equation. First, we will add in the voltage drop across the armature resistance. Insert a Gain block above the "Inductance" block and flip it from left to right. Set the Gain value to "R" and rename this block to "Resistance". Tap a line off the current Integrator's output and connect it to the input of the "Resistance" block.

Draw a line from the "Resistance" block's output to the upper negative input of the current equation Add block. Next, we will add in the back emf from the motor. Insert a Gain block attached to the other negative input of the current Add block with a line. Edit it's value to "K" to represent the motor back emf constant and Label it "Ke". Tap a line off the rotational Integrator's output and connect it to the "Ke" block. Add In1 and Out1 blocks from the Simulink/Ports & Subsystems library and respectively label them "Voltage" and "Speed". The final design should look like the example shown in the figure below.

In order to save all of these components as a single subsystem block, first select all of the blocks, then select Create Subsystem from the Edit menu. Name the subsystem "DC Motor" and then save the model. Your model should appear as follows. You can also download the file for this system here,Motor_Model.mdl. We use this model in the DC Motor Speed: Simulink Controller Design section.

Building the model with Simscape In this section, we alternatively show how to build the DC Motor model using the physical modeling blocks of the Simscape extension to Simulink. The blocks in the Simscape library represent actual physical components; therefore, complex multi-domain models can be built without the need to build mathematical equations from physical principles as was done above by applying Newton's laws and Kirchoff's laws. Open a new Simulink model and insert the following blocks to represent the electrical and mechanical elements of the DC motor. Resistor, Inductor and Rotational Electromechanical Converter blocks from the Simscape/Foundation Library/Electrical/Electrical Elements library Rotational Damper and Inertia blocks from the Simscape/Foundation Library/Mechanical/Rotational Elements library Four Connection Port blocks from the Simscape/Utilities library Double-click on the Connection Port blocks to make the location of ports 1 and 2 from the Left and the location of ports 3 and 4 from the Right. Connect and label the components as shown in the following figure. You can rotate a block in a similar manner to the way you flipped blocks, that is, by right-clicking on the block then selecting Rotate Block from the Format menu.

Complete the design of the DC motor Simscape model by assigning values to the physical parameters of each of the blocks to match our assumed values. To assign the necessary values, double-click on the Resistor, Inductor, Inertia, Rotational Damper, and Rotational Electromechanical Converter blocks and enter the following parameters and associated units: Resistance Inductance Inertia Constant of Proportionality Damping coefficient = R [Ohm] = L [H] = J [kg*m^2] = K [V/(rad/s)] = b [N*m/(rad/s)]

The Rotational Damper block serves to model the viscous friction of the motor. This type of friction model was chosen because it is linear. In most cases real friction is more complicated than this. If you wish to employ a more complicated friction model, for instance to add Coulomb friction to the model, then you may use the Rotational Friction block from the Simscape/Foundation Library/Mechanical/Rotational Elements library. Also note that in the above you generated a DC Motor model from the individual mechanical and electrical aspects of the motor. The Simscape library also includes a DC Motor block under the Simscape/SimElectronics/Actuators library. This block is used in the DC Motor Position: Simulink Modelingsection.

The physical parameters must now be set. Enter the following commands at the MATLAB prompt. J = 0.01; b = 0.1; K = 0.01; R = 1; L = 0.5; These values are the same ones listed in the physical setup section. You can then save these components in a single subsystem. Select all of the blocks and then choose Create Subsystem from the Edit menu. You can also change the subsystem block color by right-clicking on the block and choosing Format > Background Color from the resulting menu. This subsystem block can then be used to simulate the DC motor.

In order to simulate the response of this system it is further necessary to add sensor blocks to the model to simulate the measurement of various physical parameters and a voltage source to provide excitation to the motor. Furthermore, blocks are needed to interface Simscape blocks with tradtional Simulink blocks since the Simscape signals represent physical quantities with units, while the Simulink signals are dimensionless numbers. Add the following blocks to the model you just built to address these functions. Current Sensor block from the Simscape/Foundation Library/Electrical/Electrical Sensors library

Controlled Voltage Source block from the Simscape/Foundation Library/Electrical/Electrical Sources library Two PS-Simulink Converter blocks and a Solver Configuration block from the Simscape/Utilities library Electrical Reference block from the Simscape/Foundation Library/Electrical/Electrical Elements library Ideal Rotational Motion Sensor block from the Simscape/Foundation Library/Mechanical/Mechanical Sensors library Mechanical Rotational Reference block from the Simscape/Foundation Library/Mechanical/Rotational Elements library Three Out1 blocks and one In1 block from the Simulink/Ports & Subsystems library The Ideal Rotational Motion Sensor block represents a device that measures the difference in angular position and angular velocity between two nodes. In this case, we employ the block to measure the position and velocity of the motor shaft as compared to a fixed reference represented by the Mechanical Rotational Reference block. You can leave the Initial angle of the Rotational Motion Sensor block as the default 0 radians. The Current Sensor block represents another sensor, specifically it measures the current drawn by the motor. The ground for the electrical portion of our system is defined by the Electrical Reference block. The Controlled Voltage Source block serves as the power source for the motor where you can externally define the voltage signal by connecting an input to the block. The PS-Simulink blocks convert physical signals to Simulink output signals, while the Simulink-PS block conversely converts a Simulink input signal to a physical signal. These blocks can be employed to convert the Simscape signals, which represent physical quantities with units, to Simulink signals, which don't explicitly have units attached to them. These blocks, in essence, can perform a units conversion between the physical signals and the Simulink signals. In our case, we can leave the units undefined since the input and output of each of the conversion blocks have the same units. In general, the Simscape blockset is employed to model the physical plant, while the Simulink blockset is employed to model the controller. The Solver Configuration block is employed for defining the details of the numerical solver employed in running the Simscape simulation. We will use the default settings for this block. Next, connect and label the components so that they appear as in the figure below. Double-click on the lines which are connected to the Out1 blocks and label them "Current", "Position", and "Speed". Also double-click on the In1 block and label it "Voltage".

You can save these components in a single subsystem with one input and three outputs. Select all of the blocks and then choose Create Subsystemfrom the Edit menu. Also label the subsystem and signals as shown in the following figure.

You can download the complete model file here, Motor_Pos_Simscape.mdl, but note that you will need the Simscape addition to Simulink in order to run the file. Building the model through its LTI representation You can also build the DC motor model in Simulink by importing one of the models we created in MATLAB in the DC Motor Position: System Modelingpage. In this page we specifically created a transfer function model and a state-space model, both of which may be imported. A zero-pole-gain format model can also be imported. Recall the following commands used to build a state-space model of the DC motor in MATLAB. A = [0 1 0 0 -b/J K/J 0 -K/L -R/L]; B = [0 ; 0 ; 1/L]; C = [1 0 0]; D = 0; This model can then be imported using the LTI System block from the Control System Toolbox library as shown below. This figure demonstrates how the model is defined by entering "ss(A,B,C,D)" into the LTI system variable cell, though the tf and zpk commands could also be employed.

Adding In1 and Out1 ports from the Ports & Subsystems library then creates the model shown below.

Note that all three of the models generated above will behave equivalently as long as they are built using the same parameter values. The difference between them is then only the ease with which they are built and interfaced with, and how transparent they are in presenting information to the user. uspension: Simulink Modeling

Contents Physical setup Design requirements Building the Model Open-loop response Physical setup Designing an automotive suspension system is an interesting and challenging control problem. When the suspension system is designed, a 1/4 model (one of the four wheels) is used to simplify the problem to a 1D multiple spring-damper system. A diagram of this system is shown below.

The system parameters are as follows. (m1) (m2) (k1) (k2) (b1) (b2) body mass suspension mass spring constant of suspension system spring constant of wheel and tire damping constant of suspension system damping constant of wheel and tire 2500 kg 320 kg 80,000 N/m 500,000 N/m 350 N.s/m 15,020 N.s/m

(u) design

control force = force from the controller we are going to

Design requirements A good automotive suspension system should have satisfactory road holding ability, while still providing comfort when riding over bumps and holes in the road. When the vehicle is experiencing any road disturbance (i.e. pot holes, cracks, and uneven pavement),the vehicle body should not have large oscillations, and the oscillations should dissipate quickly. Since the distance X1-W is very difficult to measure, and the deformation of the tire (X2-W) is negligible, we will use the distance X1-X2 instead of X1-W as the output in our problem. Keep in mind that this is an estimation. The road disturbance (W) in this problem will be simulated by a step input. This step could represent the vehicle coming out of a pothole. We want to design a feedback controller so that the output (X1-X2) has an overshoot less than 5% and a settling time shorter than 5 seconds. For example, when the vehicle runs onto a 10 cm high step, the vehicle body will oscillate within a range of +/- 5 mm and return to a smooth ride within 5 seconds. Building the Model This system will be modeled by summing the forces acting on both masses (body and suspension) and integrating the accelerations of each mass twice to give velocities and positions. Newton's law will be applied to each mass. Open Simulink and open a new model window. First, we will model the integrals of the accelerations of the masses. (1) (2) Insert an Integrator block (from the Continuous library) and draw lines to and from its input and output terminals. Label the input line "a1" (for acceleration) and the output line "v1" (for velocity) To add such a label, double click in the empty space just above the line. Insert another Integrator block and connect it to the output of the first. Draw a line from its output and label it "x1" (for position). Insert a second pair of Integrators below the first with lines labeled "a2", "v2", and "x2".

Next, we will start to model Newton's law. Newton's law for each of these masses can be expressed as: (3) (4) These equations can be represented with gain blocks (for 1/M1 and 1/M2) and two summation blocks. Insert two Gain blocks, (from the Math Operations library) one attached to the inputs of each of the integrator pairs. Edit the gain block corresponding to M1 by double-clicking it and changing its value to "1/m1". Change the label of this Gain block to "Mass 1" by clicking on the word "Gain" underneath the block. Similarly, edit the other Gain's value to "1/m2" and it's label to "Mass 2". (You may want to resize the gain blocks to view the contents. To do this, single click on the block to highlight it, and drag one of the corners to the desired size.) There are three forces acting on M1 (one spring, one damper, and the input, u) and five forces acting on M2 (two springs, two dampers, and the input, u).

Insert two Sum blocks (from the Math Operations library), one attached by a line to each of the Gain blocks. Edit the signs of the Sum block corresponding to M1 to "+--" to represent the three forces (two of which will be negative). Edit the signs of the other Sum block to "++-++" to represent the five forces, one of which will be negative.

Now, we will add in the forces acting on each mass. First, we will add in the force from Spring 1. This force is equal to a constant, k1 times the difference X1-X2. Insert a Sum block after the upper pair of integrators. Edit its signs to "+-" and connect the "x1" signal to the positive input and the "x2" signal to the negative input. Draw a line leading from the output of the Sum block. Insert a Gain block above the "Mass 1" block. Flip it left-to-right by single-clicking on it and selecting Flip Block from the Format menu (or hit Ctrl-F). Edit the value of this gain to "k1" and label the block "Spring 1".

Tap a line off the output of the last Sum block and connect it to the input of this Gain block. Connect the output of this Gain block (the spring force) to the second input of the Mass 1 Sum block. This input should be negative since the Spring 1 pulls down on Mass 1 when X1 > X2. Tap a line off the spring force line and connect it to the second input of the Mass 2 Sum block. This input is positive since Spring 1 pulls up on Mass 2.

Now, we will add in the force from Damper 1. This force is equal to b1 times V1-V2. Insert a Sum block below the Mass 1's first integrator. Flip it left-to-right, and edit its signs to "+-". Tap a line off the "v1" line and connect it to the positive input of this Sum block. Tap a line off the "v2" line and connect it to the negative input of this Sum block. Insert a Gain block to the left of this Sum block and flip it left-to-right. Edit its value to "b1" and label it "Damper 1". Connect the output of the new Sum block to the input of this gain block. Connect the output of this gain block (the damper force) to the third input of the Mass 1 Sum block. This input is negative, similar to Spring 1's force on Mass 1.

Tap a line off Damper 1's force line and connect it to the first input (which is positive) of Mass 2's Sum block.

Now we will add in the force from Spring 2. This force acts only on Mass 2, but depends on the ground profile, W. Spring 2's force is equal to X2-W. Insert a Step block in the lower left area of your model window. Label it "W". Edit it's Step Time to "0" and it's Final Value to "0". (We will assume a flat road surface for now). Insert a Sum block to the right of the W Step block and edit its signs to "-+". Connect the output of the Step block to the positive input of this Sum block. Tap a line off the "x2" signal and connect it to the negative input of the new Sum block. Insert a Gain block to the right of this Sum block and connect the Sum's output to the new Gain's input. Change the value of the gain to "k2" and label it "Spring 2". Connect the output of this block (Spring 2's force) to the fourth input of Mass 2's Sum block. This force adds in in the positive sense.

Next, we will add in the force from Damper 2. This force is equal to b2 times V2-d/dt(W). Since there is no existing signal representing the derivative of W we will need to generate this signal. Insert a Derivative block (from the Continuous library) to the right of the W step block. Tap a line of the Step's output and connect it to the input of the Derivative block. Insert a Sum block after the Derivative block and edit it's signs to "+-". Connect the Derivative's output to the positive input of the new Sum block. Tap a line off the "v2" line and connect it to the negative input of this Sum block. Connect the output of this Sum block (Damper 2's force) to the fifth input of Mass 2's Sum block. This force also adds in with positive sign.

The last force is the input U acting between the two masses. Insert a Step block (from the Sources library) in the upper left of the model window. Connect its output to the remaining input of Mass 1's Sum block (with positive sign). Tap a line off this signal and connect it to the remaining input of Mass 2's Sum block (with negative sign). Edit this Step block's Step Time to "0" and leave its Final Value "1". Label this Step block "U". Finally, to view the output (X1-X2) insert a Scope block (from the Sinks library) and connect it to the output of the rightmost Sum block.

You can download a model file for the complete system here, suspmod.mdl. Open-loop response To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "50" in the Stop Time field. 50 seconds is long enough to view the open-loop response. The physical parameters must now be set. Run the following commands at the MATLAB prompt: m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; Run the simulation (Ctrl-T or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Observing the above, we would like to improve the response of the suspension through the control of the suspension control force U. The model created here will be employed for controller design and analysis within Simulink in the Suspension: Simulink Controller Design page. Inverted Pendulum: Simulink Modeling Contents Physical setup and system equations Building the nonlinear model with Simulink Building the nonlinear model with Simscape Generating the open-loop response Extracting a linear model from the simulation In this page we outline how to build a model of our inverted pendulum system for the purposes of simulation using Simulink and its add-ons. A great advantage of simulation, as will be demonstrated in this example, is that it can generate numerical solutions to nonlinear equations for which closed-form solutions cannot be generated. The nonlinear simulation can then be employed to test the validity of a linearized version of the model. The simulation model can also be used to evaluate the performance of the control scheme designed based on the linearized model. Physical setup and system equations In this example we will consider a two-dimensional version of the inverted pendulum system with cart where the pendulum is contrained to move in the vertical plane shown in the figure below. For this system, the control input is the force that moves the cart horizontally and the outputs are the angular position of the pendulum and the horizontal position of the cart .

For this example, let's assume the following quantities: (M) (m) (b) (l) (I) (F) (x) (theta) mass of the cart mass of the pendulum coefficient of friction for cart length to pendulum center of mass mass moment of inertia of the pendulum force applied to the cart cart position coordinate pendulum angle from vertical (down) 0.5 kg 0.2 kg 0.1 N/m/sec 0.3 m 0.006 kg.m^2

Below are the two free-body diagrams of the system.

This system is challenging to model in Simulink because of the physical constraint (the pin joint) between the cart and pendulum which reduces the degrees of freedom in the system. Both the cart and the pendulum have one degree of freedom ( and , respectively). We will generate the differential equations for these degrees of freedom from first principles employing Newton's second law ( ) as shown below. (1) (2) It is necessary, however, to include the interaction forces and between the cart and the pendulum in order to fully model the system's dynamics. The inclusion of these forces requires modeling the - and components of the the translation of the pendulum's center of mass in addition to its rotational dynamics. In the Inverted Pendulum: System Modeling tutorial, the interaction forces and were solved for algebraically. In general, we would like to exploit the modeling power of Simulink to take care of the algebra for us. Therefore, we will model the additional - and -component equations for the pendulum as shown below. (3) (4) (5) (6)

However, the position coordinates and are exact functions of . Therefore, we can represent their derivatives in terms of the derivatives of . First addressing the -component equations we arrive at the following. (7) (8) (9) Then addressing the -component equations gives us the following. (10) (11) (12) These expressions can then be substituted into the expressions for (13) (14) We can now represent these equations within Simulink. Simulink can work directly with nonlinear equations, so it is unnecessary to linearize these equations as was done in the Inverted Pendulum: System Modeling page. Building the nonlinear model with Simulink We can build the inverted pendulum model in Simulink employing the equations derived above by following the steps given below. Begin by typing simulink into the MATLAB command window to open the Simulink environment. Then open a new model window in Simulink by choosing New > Model from the File menu at the top of the open Simulink Library Browser window or by pressing Ctrl-N. Insert four Fcn Blocks from the Simulink/User-Defined Functions library. We will build the equations for , , , and employing these blocks. Change the label of each Fcn block to match its associated function. Insert four Integrator blocks from the Simulink/Continuous library. The output of each Integrator block is going to be a state variable of the system: , , , and . Double-click on each Integrator block to add the State Name: of the associated state variable. See the following figure for an example. Also change the Initial condition: for (pendulum angle) to "pi" to represent that the pendulum begins pointing straight up. and from above as follows.

Insert four Multiplexer (Mux) blocks from the Simulink/Signal Routing library, one for each Fcn block. Insert two Out1 blocks and one In1 block from the Simulink/Sinks and Simulink/Sources libraries, respectively. Then double-click on the labels for the blocks to change their names. The two outputs are for the "Position" of the cart and the "Angle" of the pendulum, while the one input is for the "Force" applied to the cart. Connect each output of the Mux blocks to the input of the corresponding Fcn block.

Connect the output of the and Fcn blocks to two consecutive integrators to generate the cart's position and the pendulum's angle. Your current model should now appear as follows.

Now we will enter each of the four equations (1), (2), (13), and (14) into a Fcn block. Let's start with equation (1) which is repeated below. (15) This equation requires three inputs: , Mux block and change the Number of inputs: to "3". , and . Double-click on the corresponding

Connect these three inputs to this Mux block in the order prescribed in the previous step. Double-click on the first Fcn block and enter the equation for xddot as shown below.

Now, let's enter equation (2) which is repeated below. (16) This equation also requires three inputs: , , and .

Enter the above equation into the Fcn block, change the number of inputs of the Mux block, and connect the correct signals to the Mux block in the correct order. Repeat this process for equations (13) and (14) repeated below. (17) (18) When all of these steps are completed, the resulting model should appear as follows.

In order to save all of these components as a single subsystem block, first select all of the blocks, then select Create Subsystem from the Edit menu. Your model should appear as follows. You can also download the file for this system here, Pend_Model.mdl.

Building the nonlinear model with Simscape In this section, we alternatively show how to build the inverted pendulum model using the physical modeling blocks of the Simscape extension to Simulink. The blocks in the Simscape library represent actual physical components; therefore, complex multi-body dynamic models can be built without the need to build mathematical equations from physical principles as was done above by applying Newton's laws. Open a new Simulink model and follow the steps below to create the inverted pendulum model in Simscape. In order to orient oneself, we will assume a coordinate system where the cart moves in the direction (positive to the right) and the positive -direction is directed up. Following standard convention, the positive -direction is then pointed out of the plane of motion. Insert a Body block from the Simscape/SimMechanics/Mechanical/Bodies library to represent the cart. Following the system parameters given at the top of this page, double-click on the block and set the Mass: to "0.5" with units of kg. The Body block by default includes two ports. Since we need ports to define where the pendulum is connected to the cart and where the external force and the frictional force are applied, a third port must be added. This can be accomplished from the button on the right-side of the Position tab. Since the cart will only move in one dimension, the two forces must be co-located along

that dimension (the -direction). Since we are in essence modeling the cart as a point mass that can only translate, you do not have to change any of the other default parameters. However, we plan to use Simscape to animate the motion of the system and hence will create additional ports to define the four corners of the cart (2-dimensional only) with respect to its center of gravity (CG). The following figure shows a possible definition of the cart body.

Insert a second Body block to represent the pendulum. Double-click on the block and set the Mass: to "0.2" with units of kg. Since the pendulum can only rotate about the -axis, the inertia associated with that principle direction is the only one that needs to be defined. For simplicity, define theInertia: equal to "0.006*eye(3)" with units of kg*m^2. Since we are modeling the pendulum as a rigid body that has size as well as mass, the body can rotate and it is important to define the location of the pendulum's attachment to the cart and its CG correctly. Specifically, define the point of attachment CS1 to have a position "[0 0 0]" and an origin that is Adjoining and define the CG to be 0.3 meters away from the attachment CS1 (as defined above). Also define the four corners of the pendulum. Make sure to show the port defining the

attachment point. Under the Visualizationtab, you can also change the pendulum's color to make it stand out from the cart.

Next add a Revolute block from the Simscape/SimMechanics/Joints library to define the joint connecting the pendulum to the cart. By default, the joint will be defined to rotate about the -axis which matches the situation we are modeling. Connect the Body block corresponding to the cart to the base port (B) of the joint and the Body block corresponding to the pendulum to the follower port (F) of the joint. Double-click on the Revolute block and set the Number of sensor / actuator ports: to "2". Then add a Joint Initial Condition block and a Joint Sensor block from the Simscape/SimMechanics/Sensors & Actuators library and connect these blocks to the Revolute block. Double-click on the Joint Initial Condition block and check the Enable box. We can use the default values for initial position and velocity of the joint. Employing an initial position of 0 degrees corresponds to the pendulum being pointed vertically upward based on the definition of the pendulum body above. This isn't consistent with the original definition of , but it will make the response results consistent with those generated from the linearized model in the other pages of this example. Next double-click on the Joint

Sensor block and change the units on the Angle measurement to rad. Angular position is the only measurement that is needed for this joint, the other boxes may remain unchecked. Add two Prismatic blocks from the Simscape/SimMechanics/Joints library to define the translational degree of freedom of the cart and the application of the forces to the cart. Since the cart is technically a point mass we need only one Prismatic block, but by employing two we can apply the forces at different locations. Double-click on each Prismatic block and change the Axis of Action to "[1 0 0]" to reflect the fact that the two forces act in the -direction. Then connect the follower port (F) of each block to the ports for the applied force (CS1) and frictional force (CS2) on the Body block representing the cart. Next add two Ground blocks from the Simscape/SimMechanics/Bodies library to define the base for the motion of the cart. Specifically, connect the output of each ground block to the base port (B) of each Prismatic block. For one of the Ground blocks you just created, double-click on the block and check the Show Machine Environment port box. Then add a Machine Environment block from the Simscape/SimMechanics/Bodies library and connect it to the Ground block for which you just added the port. The Machine Environment block allows us to define the gravitational force in the simulation. In this case the default direction (negative -direction) and magnitude ("9.81") for units of m/s^2 are correct. This block also allows us to define the parameters for visualization and the numerical solver. The default parameters are fine for this example. Next add two Joint Actuator blocks and one Joint Sensor block from Simscape/SimMechanics/Sensors & Actuators library. The Joint Actuator blocks will be employed for generating the external applied force and the frictional force, while the Joint Sensor block will sense the motion of the cart. Note, there is also a Translational Friction block that is available, but we will calculate the frictional force ourselves since we are employing only a simple viscous model. Double-click on one of the Prismatic blocks and set the Number of sensor / actuator ports: to "1" (for the force actuator). For the other Prismatic block, set the Number of sensor / actuator ports: to "2" (one for the force actuator and one for the cart sensor). Then connect the Joint Actuator and Joint Sensor blocks as described. The default values for the Joint Actuator blocks are sufficient for this case, but we must change the Joint Sensor block to output position and velocity since the velocity is needed for calculating the frictional force. Double-click on the Joint Sensor block and check the box for Velocity while leaving the box for Position checked. The default metric units do not need to be changed. Also uncheck the box for Output selected parameters as one signal. Add a Gain block from the Simulink/Math Operations library to represent the viscous friction coefficient . Set the Gain to "0.1" as defined at the top of this page and connect the input to the velocity output of the Joint Sensor block for the cart and connect the output of the gain to the Joint Actuator for the frictional force. Next, add two Out1 blocks and one In1 block from the Simulink/Ports & Subsystems library. Connect the Out1 blocks to the remaining Joint Sensor block outputs and the In1 block to the remaining Joint Actuator input.

Finally, connect and label the components as shown in the following figure. You can rotate a block in a similar manner to the way you flipped blocks, that is, by right-clicking on the block then selecting Rotate Block from the Format menu.

You can also save this model as a single subsystem block as described in the previous section. You can change the color of the subsystem by right-clicking on the block and choosing Background Color from the resulting menu. You can download the complete model file here,Pend_Model_Simscape.mdl, but note that you will need the Simscape addition to Simulink in order to run the file. We use this model in the Inverted Pendulum: Simulink Controller Design page.

Generating the open-loop response We will now simulate the response of the inverted pendulum system to an impulsive force applied to the cart. This simulation requires an impulse input. Since there is no such block in the Simulink library, we will use the Pulse Generator block to approximate a unit impulse input. We could use either of the models we generated above, however, we will use the Simscape model in this case because it will allow us to visualize the motion of the inverted pendulum system. Follow the steps given below. Open the inverted pendulum simscape model generated above. Add a Pulse Generator block from the Simulink/Sources library. Double-click on the block and change the parameters as shown below. In particular, change the Period: to "10". Since we will run our simulation for 10 seconds, this will ensure that only a single "pulse" is generated. Also change theAmplitude to "1000" and the Pulse Width (% of period): to "0.01". Together, these settings generate a pulse that approximates a unit impulse in that the magnitude of the input is very large for a very short period of time and the area of the pulse equals 1.

Add a Scope block from the Simulink/Sinks library. In order display two inputs on the scope, double-click on the Scope block, choose the Parameters icon, and change the Number of axes: to "2". Connect the blocks and label the signals connected to the Scope block as shown.

Save this system as Pend_Openloop.mdl, or download it here Before we start the simulation, we would like to enable the visualization of the inverted pendulum system. From the menus at the top of the model window choose Simulation > Configuration Parameters. Then from the from the directory on the left-side of the window choose Simscape > SimMechanics. Then check the box for Show animation during simulation as shown in the figure below.

Now, start the simulation (select Start from the Simulation menu or enter Ctrl-T). As the simulation runs, an animation of the inverted pendulum like the one shown below will visualize the system's resulting motion.

Then open the Scope and click the Autoscale button. You will see the following output for the pendulum angle and the cart position.

Notice that the pendulum repeatedly swings through full revolutions where the angle rolls over /$-\pi$ radians. Furthermore, the cart's position grows unbounded, but oscillates under the influence of the swinging pendulum. These results differ quite a bit from the results of the open-loop simulation shown in the Inverted Pendulum: System Analysis page. This is due of course to the fact that this simulation employed a fully nonlinear model, while the previous analysis had relied on a linear approximation of the inverted pendulum model. In order to compare the results of the simulation model more directly to the prior results, we will extract a linear model from our simulation model. Extracting a linear model from the simulation Aside from comparing our simulation model to our prior results, it may also be desirable to extract a linear model for the purposes of analysis and design. Much of the analytical techniques that are commonly applied to the analysis of dynamic systems and the design of their associated control can only be applied to linear models. Therefore, it may be desirable to extract an approximate linear model from the nonlinear simulation model. We will accomplish this from within Simulink. To begin, open either of Pend_Model_Simscape.mdl. the Simulink models generated above, Pend_Model.mdl or

If you generated your simulation model using variables, it is necessary to define the physical constants in the MATLAB workspace before performing the linearization. This can be accomplished by entering the following commands in the MATLAB command window. M = 0.5;

m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3; Next choose from the menus at the top of the model window Tools > Control Design > Linear Analysis. This will cause the Linear Analysis Toolwindow to open. In order to perform our linearization, we need to first identify the inputs and outputs for the model and the operating point that we wish to perform the linearization about. First right-click on the signal representing the Force input in the Simulink/Simscape model. Then choose Linearization Points > Input Point from the resulting menu. Similarly, right-click on each of the two output signals of the model (pendulum angle and cart position) and select Linearization Points > Output Point from the resulting menu in each case. The resulting inputs and outputs should now be identified on your model by arrow symbols as shown in the figure below.

Next we need to identify the operating point to be linearized about. From the Operating Point: menu choose Linearize At... > Trim model... as shown in the figure below. This will open the TRIM

MODEL tab. Within this tab, select the Trim button indicated by the green triangle. This will create the operating point op_trim1. Since we wish to examine the impulse response of this system, return to the EXACT LINEARIZATION tab and choose New Impulse from the Plot Result: drop-down menu near the top window as shown in the figure below.

Finally, choose op_trim1 from the Operating Point: drop-down menu and click the Linearize button indicated by the green triangle. This automatically generates an impulse response plot and the linearized model linsys1. In order to compare the results to those plots generated in the Inverted Pendulum: System Analysis page, it is necessary to change the -axis scaling. This can be achieved from by choosing Properties from the rightclick menu. The resulting window should then appear as follows, where the top plot is response of the pendulum angle and the bottom plot is the response of the cart position.

These plots are very similar, though not exactly the same, as those generated in the Inverted Pendulum: System Analysis page. We can also export the resulting linearized model into the MATLAB workspace for further analysis and design. This can be accomplished by simply right-clicking on the linsys1 object in the Linear Analysis Workspace to copy the object. Then right-click within the MATLAB Workspace to paste the object. Inverted Pendulum: Simulink Controller Design From the Inverted Pendulum: Simulink Modeling page we generated two different simulation models. We will now employ these models within Simulink to design different approaches to control and simulate the resulting closed-loop behavior.

Contents Problem setup and design requirements Implementing PID control for the nonlinear model Nonlinear closed-loop response Problem setup and design requirements In this problem, the cart with an inverted pendulum, shown below, is "bumped" with an impulse force, .

For this example, let's assume that (M) (m) (b) (l) (I) (F) (x) (theta) mass of the cart mass of the pendulum friction of the cart length to pendulum center of mass inertia of the pendulum force applied to the cart cart position coordinate pendulum angle from vertical 0.5 kg 0.2 kg 0.1 N/m/sec 0.3 m 0.006 kg*m^2 N m radians

In the design process we will develop a PID controller and apply it to a single-input, single-output plant. More specifically, the controller will attempt to maintain the pendulum vertically upward when the cart is subjected to a 1-Nsec impulse. The cart's position will be ignored. Under these conditions, the design criteria are:

Settling time of less than 5 seconds Pendulum should not move more than 0.05 radians away from the vertical Implementing PID control for the nonlinear model In the Inverted Pendulum: PID Controller Design page a PID controller was designed with proportional, integral, and derivative gains equal to 100, 1, and 20, respectively. To implement this closed-loop system, we will start with one of our plant models from the Inverted Pendulum: Simulink Modeling page. Following the steps below, we will build a closed-loop model with reference input of pendulum position and a disturbance force applied to the cart. To begin, open either of the Simulink models generated previously, Pend_Model.mdl or Pend_Model_Simscape.mdl. We will use the Simscape model to leverage the animation capabilities it offers. Insert two Add blocks from Simulink/Math Operations library. Change the List of signs: of one of the Add blocks to "+-". Insert a Constant block from Simulink/Sources library. Change its value to 0. This is the reference input that corresponds to the pendulum vertically upward. Note, the non-Simscape model (and the rest of the pages in this example) define the pendulum angle to equal pi when pointing straight up. Insert a PID Controller block from the Simulink/Continuous library. Edit the PID block by doubleclicking on it. Change the Proportional (P): gain to "100", leave the Integral (I): gain as "1", and change theDerivative (D): gain to "20". Now connect the blocks as they appear in the following figure:

You can download our version here. Nonlinear closed-loop response We can now simulate the closed-loop system. Be sure that the physical parameters are set as follows. M = 0.5; m = 0.2; b = 0.1; I = 0.006; g = 9.8; l = 0.3; Now, start the simulation (select Start from the Simulation menu or enter Ctrl-T). As the simulation runs, an animation of the inverted pendulum will visualize the system's resulting motion. Recall that the Show animation during simulation option must be checked under the Simulation > Configuration Parameters menu. After the simulation has run, double-click on the scope and hit the Autoscale button. You should see the following response.

This response is almost identical to the closed-loop response obtained in the MATLAB tutorials (for example, in the Inverted Pendulum: PID Controller Design page). Note that the PID controller handles the nonlinear system very well because the deviation of the angle from the operating point is very small (approximately .05 radians). Suspension: Simulink Controller Design Contents Simulink Model Extracting a linear model into MATLAB Implementing full state-feedback control Closed-loop response Simulink Model The Simulink model for the suspension system was developed in the Suspension: Simulink Modeling section, and can be downloaded here: suspmod.mdl. Extracting a linear model into MATLAB A linear model of the system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. This is done through the use of In1 and Out1 blocks and the MATLAB function linmod. We will extract only the model from the input U to the output X1-X2. First, replace the U Step block with an In1 block. Also, replace the Scope block with an Out1 block. (These blocks can be found in the Ports & Subsystems library). This defines the input and output of the system for the extraction process.

Save your file as "suspmod.mdl" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands. You should see the following output, providing both state-space and transfer function models of the system. [A,B,C,D]=linmod('suspmod') [num,den]=ss2tf(A,B,C,D) A = 1.0e+03 * 0 0 -0.0320 0.2500 B = 0 0 0 0 0.0320 -1.8125 0.0010 0 -0.0001 0.0011 0 0.0010 0.0001 -0.0480

0.0004 -0.0031 C = 1 D = 0 num = 0 den = 1.0e+04 * 0.0001 0.0048 0.1851 0.1721 5.0000 0 0.0035 0.0188 0.6250 -1 0 0

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. Enter the following command in MATLAB. You should see the following plot which is equivalent to the Scope's output. step(num,den);

Implementing full state-feedback control In the Suspension: State-Space Controller Design page a full state-feedback controller was designed feeding back the following five states: (1) The controller used the following feedback gain matrix: K = [ 0 2.3E6 5E8 0 8E6 ] To implement this in Simulink, we will contain the open-loop system from earlier in this page in a Subsystem block. Create a new model window. Drag a Subsystem block from the Subsystems & Ports library into your new model window.

Double-click on this block. You will see a blank window representing the contents of the subsystem (which is currently empty). Open your previously saved model of the vehicle suspension system, suspmod.mdl. Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or Ctrl-C).

Select the blank subsystem window from your new model and select Paste from the Edit menu (or Ctrl-V). You should see your original system in this new subsystem window (you may need to use the scroll bars to center on it). Label the In1 block "U", and the Out1 block "y1". Replace the W Step block with an In1 block and label this block "W".

Now we will generate the other state outputs from the subsystem. Insert an Out1 block below the "y1" block and label it "d/dt(y1)", Tap a line off the line leading into the Damper 1 gain block (V1-V2) and connect it to the d/dt(y1) Out block. Insert another Out1 block below the "d/dt(y1)" Out1 block and label it "x1". Tap a line off the "x1" line and connect it to this Out1 block. Insert another Out1 block below the "x1" Out1 block and label it "d/dt(x1)". Tap a line off the "v1" line and connect it to this Out1 block.

The final, extra, state needs to be generated, which is the integral of Y1. Insert an Integrator block above the "y1" Out block and connect its input with a line tapped of the input to the "y1" Out1 block. Insert an Out1 block, label it "int(y1)", and connect it to the output of the new Integrator block.

Since the state outputs will be used to form a vector, it is important that they be numbered in the right order. Edit the "x1" Out1 block and change its Port Number to "1". Similarly, change the "d/dt(x1)" Out1 block's port number to "2", "y1" Out1 block's port number to "3", "d/dt(y1)" Out1 block's port number to "4", and "int(y1)" Out1 block's port number to "5". The In1 blocks should be numbered such that "U" is "1" and "W" is "2". Some of these numbers may already be correct.

Close the Subsystem window. You should now see input and output terminals on the Subsystem block. Name this block "Suspension Model". You should resize this block so that you can read all of the labels. To do this, highlight it by single-clicking on it and drag one of the highlighted corners to the right size. Notice that the model has two inputs and five outputs. Each input and output is a scalar signal in this model.

Now, we will build a full state-feedback controller around the plant model. First, we need to create a vector signal out of the five scalar outputs in order to multiply by the feedback gain matrix K. Insert a Mux block (from the Signal Rounting library) to the right of the Suspension Model block. The Mux takes multiple inputs and combines them into a vector signal. By default, the Mux has two inputs. Edit the Mux block and change the Number of Inputs to "5". Resize the Mux so that it is the same height as the Suspension Model block.

Now, we will close the loop. Insert a Matrix Gain block (from the Math Operations library) below the Suspension Model block. Flip the Matrix Gain left-to-right and edit its value to "K". Insert a Sum block to the left of the Suspension Model block. Edit its signs to "+-". Connect the output of the Matrix Gain to the negative input of the Sum block. Connect the output of the Sum block to the "U" input of the Suspension Model.

Insert a Step block and connect it to the positive input of the Sum block. Label the step block "r" and edit its Step Time to "0" and its Final Value to "0" (we are commanding the vehicle to stay level). Insert a Step block and connect it to the "W" input of the Suspension Model. Edit its Step Time to "0" and its Final Value to "-.1" (we are now assuming a 10cm deep pothole). Insert a Scope block and tap a line off the "y1" output of the Suspension Model and connect it to the Scope.

Closed-loop response To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "2" in the Stop Time field. The design requirements included a settling time of less than 5 sec, and the system actually settles in 2 sec. The physical parameters must now be set. Run the following commands at the MATLAB prompt: m1 = 2500; m2 = 320; k1 = 80000; k2 = 500000; b1 = 350; b2 = 15020; The last step is to assign values to the feedback gain matrix K. Execute the following command at the MATLAB prompt. K = [ 0 2.3e6 5e8 0 8e6 ]; Run the simulation (Ctrl-T or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

This response agrees with the one found in the Suspension: State-Space Controller Design page. DC Motor Position: Simulink Controller Design Contents Open-loop response Extracting a linear sampled model into MATLAB Implementing digital control in Simulink Converting a continuous-time model to discrete-time within Simulink In the DC Motor Position: Simulink Modeling section, we developed a Simulink model of the DC motor system using three different methods. You can download one of these models here, Motor_Pos.mdl. In this section, we will employ this model within Simulink to simulate and design different approaches to control. Specifically, we will explore the design of a digital control system. Open-loop response

Begin with the above model saved as a subsystem and follow the steps given below. Remove the In and Out ports. Insert a Step block from the Simulink/Sources library and connect it with a line to the Voltage input. To view the output position, insert a Scope from the Simulink/Sinks library and connect it to the Position output. To provide an appropriate unit step input at t=0, double-click the Step block and set the Step time to "0". The system should now appear as in the following figure.

In order to simulate this system, the details of the simulation must first be set. This can be accomplished by selecting Configuration Parameters from the Simulation menu. Within the resulting menu, define the length for which the simulation is to run in the Stop time field. We will enter "0.2" since 0.2 seconds will be long enough for the step response to reach steady state. Also in the Configuration Parameters dialog box, the numerical solver employed in the simulation can be specified. Go ahead and change the Solver field from the default ode45 (Dormand-Prince) to the ode15s (stiff/NDF) solver. Since the time scales in this example are very small, this stiff system integration method is more efficient than the default integration method.

The physical parameters must now be set. Enter the following commands at in the MATLAB command window. J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6;

Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output which goes unstable and obviously does not meet our design requirements. Note that this response is consistent with the results we achieved from the MATLAB command line in the DC Motor Position: System Analysis page.

Extracting a linear sampled model into MATLAB We will now extract a linear sampled version of this continuous-time model into the MATLAB workspace. The extraction of a sampled model can be performed from the MATLAB command line using the command dlinmod, though it is preferrable to perform the extraction from directly within Simulink as we will demonstrate here. We will use the model from above. The first thing that we need to do is to identify the inputs and outputs of the model we wish to extract. First right-click on the signal representing the Voltage input in the Simulink model. Then choose Linearization > Input Point from the resulting menu. Similarly, right-click on the signal representing the Position output and select Linearization > Output Point from the resulting menu. The input and output signals should now be identified on your model by arrow symbols as shown in the figure below.

In order to perform the extraction, select from the menus at the top of the model window Tools > Control Design > Linear Analysis. This will cause theLinear Analysis Tool to open. Next follow the steps given below. From the Linear Analysis Tool window, click the Options button. From the resulting window, change the sample time to "0.001" as shown in the figure below. The rate conversion method can remain the default Zero-Order Hold.

Within the Linear Analysis Tool window, the Operating Point to be linearized about can remain the default, Model Initial Condition. In order to perform the linearization/discretization, next click the Linearize button identified by the green triangle. The result of this linearization/discretization is the linsys1 object which now appears in the Linear Analysis Workspace. This model can be exported by simply dragging the object into the MATLAB Workspace.

This object can then be used within MATLAB in the same manner as an object created directly from the MATLAB command line. Specifically, entering the command zpk(linsys1) in the MATLAB command window demonstrates that the resulting model has the following form. (1) Note that this model is identical to the model generated from the conversion performed in DC Motor Position: Digital Controller Design page. This makes sense since the same sample time and discretization method were used, and because the original model was already linear. To further verify the validity of the model extraction, we will generate an open-loop step response of the discrete-time transfer function in MATLAB. Enter the following commands at the prompt of the MATLAB command window. You should see the following plot which is in agreement with the output from the continous Simulink model evaluated above. t = 0:0.001:0.2; step(linsys1,t); grid

The extracted discrete models can then be used within MATLAB for various analysis and controller design tasks. In the following, we directly employ the Simulink model from above to simulate the implementation of a digital controller without explicitly discretizing the continuous-time plant model. Implementing digital control in Simulink In the DC Motor Position: Digital Controller Design page a digital controller was designed with the following transfer function. (2) We can use this compensator to simulate the associated closed-loop digital control system in Simulink. Starting from the Simulink model we used above for extraction, delete the Input and Output ports and add the following: Two Zero Order Hold blocks from the Simulink/Discrete library Discrete Zero Pole block from the Simulink/Discrete library Sum block from Simulink/Math Operations library

Place one Zero Order Hold block on the input of the Motor_pos subsystem which is a continuous model of the plant. This Zero Order Hold blocks converts a discrete-time signal to a stepwise-constant continuous signal. The other Zero Order Hold block is placed on the output of the Motor_pos subsystem and serves to take discrete samples of the output signal of the plant. Edit the Zero Order Hold blocks such that the Sample time fields are set to "0.001" (this is fast compared to the desired step response in the MATLAB tutorial). Next edit the Discrete Zero Pole block to model the discrete controller transfer function described above. Specifically, edit the Zeros field to "[0.95 0.80 0.80]", the Poles field to "[-0.98 0.6 1]", the Gain field to "800", and the Sample time field to "0.001". Then label the block "Controller" and resize it to view its entire contents. Next, enter "|+-" to the List of signs field of the Sum block. The character "|" serves as a spacer between input ports. Now we need to set up the simulation to observe the motor's position for a step input. Attach a Step block to the positve input of the Sum block and attach a Scope block to the plant output. Double-click the Step block and set the Step time to "0". Finally, connect the blocks as shown below.

Before we can simulate the closed-loop system, we again need to set an appropriate simulation time. Select Configuration Parameters... from theSimulation menu and enter "0.08" in the Stop time field. We choose this stop time in order to observe the system long enough to see if it satisfies the 0.04 seconds settling time requirement. If you have started a new session of MATLAB or skipped the open-loop response part of this example, then the physical parameters for the simulation must be set again. Run the following commands at the MATLAB prompt if necessary.

J = 3.2284E-6; b = 3.5077E-6; K = 0.0274; R = 4; L = 2.75E-6; Now the simulation can finally be run. Recall that this can be accomplished by pressing Ctrl-T or selecting Start from the Simulation menu. When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

From the above figure, it appears that the overshoot is less than 16% and the settling time is less than 0.04 seconds as required. This is consistent with the analysis employed in designing the digital controller. We have not, however, assessed the response of the system with a disturbance present. A step disturbance can be added in a similar manner to the way that the step reference was added as shown in the following figure. Sum the disturbance between the controller and plant following the Zero Order Hold block since the disturbance signal is continous.

Within the model, set the disturbance Step time to occur at "0.03" seconds. Then re-run the simulation and observe the scope output as described above. The resulting position graph should appear as shown below.

From inspection of the above, you can see that there is a slight bump beginning at 0.03 seconds due to the disturbance, but the system is able to reject its effect and the steady-state error still goes to zero as required. If you have not done so already, save your Simulink model. You can also download our version of this model here, Motor_Pos_cl.mdl. Converting a continuous-time model to discrete-time within Simulink In the above, we extracted a linear sampled model of our plant from our Simulink model into the MATLAB workspace using the Linear Analysis Tool. We also, in effect, discretized the plant for the purposes of simulation using Zero Order Hold blocks within Simulink. In this section, we will generate a discrete-time model of our plant without the use of the Zero Order Hold blocks. This approach offers a wider array of discretization techniques than can be achieved through Simulink blocks, which are limited to Zero Order and First Order Hold. This is especially useful in generating a digital controller from a continous design. So far we have chosen to employ the first principles simulink model but we could have just as easily used the Simscape model or the LTI System model. One thing to be careful of, however, is that if you were to use the Simscape model of the plant in the above, the physical Simscape signals would need to be converted to dimensionless Simulink signals in order to interface with the Simulink blocks we employed. This process is described in the DC Motor Position: Simulink Modeling page. In the following, we will discretize the LTI System model which can be downloaded here, Motor_Pos_LTI.mdl, or you can create it yourself as DC Motor Position: Simulink Modeling page. Recall that the LTI System block imports a model from the MATLAB workspace. Therefore, you need to enter the following commands at the command line where it is presumed that values have already been entered for the various physical parameters. A = [0 1 0 0 -b/J K/J 0 -K/L -R/L]; B = [0 ; 0 ; 1/L]; C = [1 0 0]; D = 0; Your model should appear as follows.

To discretize this model: Remove the In1 and Out1 ports. Click on Tools > Control Design > Model Discretizer and the LTI System block will turn red. Enter a Sample time of 0.001 and leave the Transform method as Zero-order hold. Then choose Discretize selected blocks from the Discretize menu.

The LTI model now is converted to discrete. Let's add a digital controller and simulate the closed-loop response of this model. Employ the same controller we used above. The final model should appear as follows.

Simulate the model as we have done previously. The resulting position response should appear as follows.

Note the agreement with the closed-loop simulation results we found previously. This makes sense since the first principles model and the LTI System block model are equivalent and both Simulink models used a zero-order hold type sampling to discretize the plant. You can download our version of this model here, Motor_Pos_LTI_cl.mdl. DC Motor Speed: Simulink Controller Design Contents

Extracting a linear model into MATLAB Open-loop response Closed-loop response with lag compensator Closed-loop response with lead compensator From the DC Motor Speed: Simulink Modeling page we generated two different DC motor models in Simulink. We will now employ these models within Simulink to simulate the system response and design different approaches to control. Extracting a linear model into MATLAB A linear model of the system can be extracted from the Simulink model into the MATLAB workspace. This can accomplished employing the MATLAB command linmod or from directly within Simulink as we will do here. We will specifically use the base Simulink model developed from first principles shown below. You can download this model here, or you can refer to the DC Motor Speed: Simulink Modeling page to recreate the model yourself.

Recall that the physical parameters have to be set if they have not previously been defined in the workspace. J = 0.01; b = 0.1; K = 0.01;

R = 1; L = 0.5; We then need to identify the inputs and outputs of the model we wish to extract. First right-click on the signal representing the Voltage input in the Simulink model. Then choose Linearization > Input Point from the resulting menu. Similarly, right-click on the signal representing the Speed output and select Linearization > Output Point from the resulting menu. The input and output signals should now be identified on your model by arrow symbols as shown in the figure below.

In order to perform the extraction, select from the menus at the top of the model window Tools > Control Design > Linear Analysis. This will cause theLinear Analysis Tool to open. Within the Linear Analysis Tool window, the Operating Point to be linearized about can remain the default, Model Initial Condition. In order to perform the linearization, next click the Linearize button identified by the green triangle. The result of this linearization is the linsys1 object which now appears in the Linear Analysis Workspace as shown below. Furthermore, the open-loop step response of the linearized system was also generated automatically.

The open-loop step response above is consistent with the response generated in the DC Motor Speed: System Analysis page The reason the responses match so closely is because this Simulink model uses only linear components. Note that this process can be used extract linear approximations of models with nonlinear elements too. We will further verify the model extraction by looking at the model itself. The linearized model can be exported by simply dragging the object into theMATLAB Workspace. This object can then be used within

MATLAB in the same manner as an object created directly from the MATLAB command line. Specifically, entering the command zpk(linsys1) in the MATLAB command window demonstrates that the resulting model has the following form. (1) This model matches the one generated in the DC Motor Speed: System Modeling page. This can be seen by repeating the MATLAB commands given below. s = tf('s'); P_motor = K/((J*s+b)*(L*s+R)+K^2); zpk(P_motor) ans =

2 ------------------(s+9.997) (s+2.003)

Continuous-time zero/pole/gain model.

Open-loop response The open-loop step response can also be generated directly within Simulink, without extracting any models to the MATLAB workspace. In order to simulate the step response, the details of the simulation must first be set. This can be accomplished by selecting Configuration Parameters from theSimulation menu. Within the resulting menu, define the length for which the simulation is to run in the Stop time field. We will enter "3" since 3 seconds will be long enough for the step response to reach steady state. Within this window you can also specify various aspects of the numerical solver, but we will just use the default values for this example. Next we need to add an input signal and a means for displaying the output of our simulation. This is done by doing the following: Remove the In1 and Out1 blocks. Insert a Step block from the Simulink/Sources library and connect it with a line to the Voltage input of the motor subsystem. To view the Speed output, insert a Scope from the Simulink/Sinks library and connect it to the Speed output of the motor subsystem.

To provide a appropriate unit step input at t=0, double-click the Step block and set the Step time to "0". The final model should appear as shown in the following figure.

Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

This response is identical to that obtained by MATLAB above using the extracted model. This is again to be expected because this Simulink model includes only linear blocks.

Closed-loop response with lag compensator In the DC Motor Speed: Root Locus Controller Design page a lag compensator was designed with the following transfer function. (2) To generate the closed-loop step response with this compensator in Simulink, we will begin with the "Motor_Model.mdl" file described above. We will then put the lag compensator in series with the motor subsystem and will feed back the motor's speed for comparison to a desired reference. More specifically, follow the steps given below: Remove the Input and Output ports of the model. Insert a Sum block from the Simulink/Math Operations library. Then double-click on the block and enter "|+-" for its List of signs where the symbol "|" serves as a spacer between ports of the block. Insert a Transfer Function block from the Simulink/Continuous library. Then double-click on the block and edit the Numerator coefficients field to "[44 44]" and the Denominator coefficients field to "[1 0.01]". Insert a Step block from the Simulink/Sources library. Then double-click on the block and set the Step time to "0". Insert a Scope block from the Simulink/Sinks library. Then connect and label the components as shown in the following figure

You can download our version of the closed-loop system model here, Motor_Model_lag.mdl.

Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

This step response matches exactly the closed-loop performance observed in the DC Motor Speed: Root Locus Controller Design page where the lag compensator was originally designed. Note that while we used the physics-based Simulink model developed in the DC Motor Speed: Simulink Modelingpage for simulating the closed-loop system, we could have equivalently used the Simscape version of the DC motor model. Closed-loop response with lead compensator We have shown in the above and in other pages of this example that the lag compensator we have designed meets all of the given design requirements. Instead of a lag compensator, we could have also designed a lead compensator to meet the given requirements. More specifically, we could have designed a lead compensator to achieve a similar DC gain and phase margin to that achieved by the lag compensator, but with a larger gain crossover frequency. You can refer back to the DC Motor Speed: Frequency Domain Methods for Controller Design page for more details on the design of the lag compensator, but the fact that the DC gains and phase margins are similar indicate that the responses under lag and lead control would have similar amounts of error in steady state and similar amounts of overshoot. The difference in response would come in that the larger gain crossover frequency provided by the lead compensator would make the system response faster than with the lag compensator. We will specifically use the following lead compensator. (3) To see the precise effect of the lead compensator as compared to our lag compensator, let's modify our Simulink model from above as follows: Disconnect the Step block and Scope block from the rest of the model.

Copy the blocks forming the closed-loop of the model: the Sum block, the Transfer Function block, and the DC Motor subsystem. Then paste a copy of this loop below the original blocks. Double-click on the Transfer Function block and edit the Numerator coefficients field to "[160000 5.6e6]" and the Denominator coefficientsfield to "[1 1035]". Insert a Mux block from the Simulink\Signal Routing library and connect the outputs of the two Motor subsystem blocks to the inputs of the Mux and connect the output of the Mux to the Scope. Connect the Step block to the Sum block of the original feedback system. Then branch off from this line and connect it to the Sum block of the lead compensated system as well. The Mux block serves to bundle the two signals into a single line, this way the Scope will plot both speed signals on the same set of axes. When you are done, your model should appear as follows.

Running the simulation and observing the output produced by the scope, you will see that both responses have a steady-state error that approaches zero. Zooming in on the graphs you can generate a figure like the one shown below. Comparing the two graphs, the purple response belonging to the lead compensated system has a much smaller settle time and slightly larger, but similar, overshoot as compared to the yellow response produced by the lag compensated system.

It is generally preferred that a system respond to a command quickly. Why then might we prefer to use the lag compensator even though it is slower than the lead compensator? The advantage of the lag compensator in this case is that by responding more slowly it requires less control effort than the lead compensator. Less control effort means that less power is consumed and that the various components can be sized smaller since they do not have to supply as much energy or withstand the higher voltages and current required of the lead compensator. We will now modify our simulation to explicitly observe the control effort requirements of our two feedback systems. We will do this by sending our various signals to the workspace for plotting and further manipulation if desired. Specifically, delete the Scope and Mux blocks from your Simulink model. Then insert four To Workspace blocks from the Simulink\Sinks library. Double-click on each of the blocks and change their Save format from Structure toArray. Also provide a Variable name within each block that will make sense to you. You can then connect the blocks to the existing model and label them as shown below. You can download our version of this Simulink model here, Motor_Model_leadlag.mdl.

Then change the simulation stop time to 1 second and run the model. The act of running the simulation will send to the MATLAB workspace a series of arrays corresponding to the variables set-up in your model with the To Workspace blocks. Furthermore, the time vector used by that run of the simulation is stored in the default variable tout. You can now plot the results of your simulation from the workspace. Enter the following code to see how to specifically plot the control effort variables. subplot(2,1,1) plot(tout,ulag); xlabel('time (seconds)') ylabel('control effort (volts)') title('Control Effort Under Lag Compensation') subplot(2,1,2)

plot(tout,ulead); xlabel('time (seconds)') ylabel('control effort (volts)') title('Control Effort Under Lead Compensation')

Examination of the above shows that the control effort required by the lead compensator is above 150,000 Volts, which is well above anything that could be supplied or withstood by a typical DC motor. This exemplifies the tradeoff inherent between achieving small tracking error and keeping the amount of control effort required small. Optimal control techniques have been developed to achieve an optimal balance between competing goals. One such technique is explored in the Aircraft Pitch: State Space Methods for Controller Design page. Cruise Control: Simulink Controller Design Contents Extracting a linear model into MATLAB Implementing PI control Closed-loop response

In the Cruise Control: Simulink Modeling page we created a Simulink model of the cruise control system. You can recreate the model or download it here. In this section, we will show how to implement a feedback controller in Simulink to meet the performance specifications for the system. Extracting a linear model into MATLAB A linear model of the system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. This is done through the use of In1 and Out1 blocks and the MATLAB function linmod. Replace the Step Block and Scope Block with an In1 and an Out1 block, respectively (these blocks can be found in the Ports & Subsystems library). This defines the input and output of the system for the extraction process.

Save your file as "ccmodel.mdl" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands: m = 1000; b = 50; u = 500; [A,B,C,D] = linmod('ccmodel') cruise_ss = ss(A,B,C,D);

A = -0.0500 B = 1.0000e-03 C = 1 D = 0 To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. We will multiply the numerator by 500 to simulate a step input of 500 N. Enter the following command in MATLAB. step(u*cruise_ss)

Implementing PI control In the Cruise Control: PID Control page a PI controller was designed with Kp = 800 and Ki = 40 to give the desired response. We will implement this in Simulink by first containing the open-loop system from earlier in this page in a Subsystem block.

Create a new model window. Drag a Subsystem block from the Ports & Subsystems library into your new model window.

Double-click on this block. You will see a blank window representing the contents of the subsystem (which is currently empty). Open your previously saved model of the cruise control system, ccmodel.mdl. Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or Ctrl-C). Select the blank subsystem window from your new model and select Paste from the Edit menu (or Ctrl-V). You should see your original system in this new subsystem window. Close this window. You should now see input and output terminals on the Subsystem block. Name this block "plant model".

Now, we will build a PI controller around the plant model. First, we will feed back the plant output. Draw a line extending from the plant output. Insert a Sum block and assign "+-" to it's inputs. Tap a line of the output line and draw it to the negative input of the Sum block.

The output of the Sum block will provide the error signal. From this, we will generate proportional and integral components. Insert an Integrator block after the Sum block and connect them with a line. Insert and connect a Gain block after the Integrator block to provide the integral gain. Label this Integrator "Ki" and assign it a value of "Ki". Insert a new Gain block and connect it with a line tapped off the output of the Sum block. Label this gain "Kp" and assign it a value of "Kp".

Now we will add the proportional and integral components and apply the sum to the plant. Insert a Sum block between the Ki block and the plant model and connect the outputs of the two Gain blocks to the Sum block inputs. Connect the Sum block output to the input of the plant block.

Finally, we will apply a step input and view the output with a Scope block. Attach a Step block to the free input of the feedback Sum block. Attach a Scope block to the plant output. Double-click the Step block and set the Step Time to "0" and the Final Value to "u". This allows the input magnitude to be changed outside of Simulink.

You can download our version of the closed-loop system model here. In this example, we constructed a PI controller from fundamental blocks. As an alternative, we could have used a Transfer Function block (from the Continuous library) to implement this in one step, as shown below.

You can download this model here. Closed-loop response To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "10" in the Stop Time field. The design requirements included a rise time of less than 5 sec, so we simulate for 10 seconds to view the output. The physical parameters must now be set. Run the following commands at the MATLAB prompt: m = 1000; b = 50; u = 10; Kp = 800; Ki = 40; Run the simulation (hit Ctrl-T or select Start from the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

Das könnte Ihnen auch gefallen