LabVIEW Handout

Embed Size (px)

Citation preview

  • 8/3/2019 LabVIEW Handout

    1/52

  • 8/3/2019 LabVIEW Handout

    2/52

  • 8/3/2019 LabVIEW Handout

    3/52

  • 8/3/2019 LabVIEW Handout

    4/52

  • 8/3/2019 LabVIEW Handout

    5/52

  • 8/3/2019 LabVIEW Handout

    6/52

  • 8/3/2019 LabVIEW Handout

    7/52

  • 8/3/2019 LabVIEW Handout

    8/52

  • 8/3/2019 LabVIEW Handout

    9/52

  • 8/3/2019 LabVIEW Handout

    10/52

  • 8/3/2019 LabVIEW Handout

    11/52

  • 8/3/2019 LabVIEW Handout

    12/52

  • 8/3/2019 LabVIEW Handout

    13/52

  • 8/3/2019 LabVIEW Handout

    14/52

  • 8/3/2019 LabVIEW Handout

    15/52

  • 8/3/2019 LabVIEW Handout

    16/52

  • 8/3/2019 LabVIEW Handout

    17/52

  • 8/3/2019 LabVIEW Handout

    18/52

  • 8/3/2019 LabVIEW Handout

    19/52

  • 8/3/2019 LabVIEW Handout

    20/52

  • 8/3/2019 LabVIEW Handout

    21/52

  • 8/3/2019 LabVIEW Handout

    22/52

  • 8/3/2019 LabVIEW Handout

    23/52

  • 8/3/2019 LabVIEW Handout

    24/52

  • 8/3/2019 LabVIEW Handout

    25/52

  • 8/3/2019 LabVIEW Handout

    26/52

  • 8/3/2019 LabVIEW Handout

    27/52

  • 8/3/2019 LabVIEW Handout

    28/52

  • 8/3/2019 LabVIEW Handout

    29/52

  • 8/3/2019 LabVIEW Handout

    30/52

  • 8/3/2019 LabVIEW Handout

    31/52

  • 8/3/2019 LabVIEW Handout

    32/52

    Modeling a System

    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 direction, we want to apply control to the train so that it has a s mooth start -up and stop, along with a constant-

    speed ride.

    The mass of the engine and the car will be represented by M1 and M2, respectively. The two are held together by a

    spring, which has the stiffness coefficient of k. F represents the force applied by the engine, and the Greek letter, mu

    (which will also be represented by the letter u), represents the coefficient of rolling friction.

    Free Body Diagram and Newton's Law

    The system can be represented by following Free Body Diagrams .

    Figure 1: Free Body Diagrams

    From Newton's law, you know that the sum of forces acting on a mass equals the mass times its accelerat ion. In this

    case, the forces acting on M1 are the spring, the friction and the force applied by the engine. The forces acting on

    M2 are the spring and the friction. In the vertical direction, the gravitational force is canceled by the normal force

    applied by the ground, so that there will be no acceleration in the vertical direction. The equations of mot ion in thehorizontal d irection are the following:

    State-variable and Output Equations

    This set of system equations can now be manipulated into state-variable form. The state variables are the pos itions,X1 and X2, and the velocities, V1 and V2; the input is F. The s tate variable equations will look like the following:

  • 8/3/2019 LabVIEW Handout

    33/52

    Let the output of the system be the velocity of the engine. Then the output equation will be:

    Transfer Function

    To find the transfer function of the system, we first take the Laplace transforms of the differential equations.

    The output is Y(s) = V1(s) = s X1(s). The variable X1 should be algebraically eliminated to leave an e xpress ion for

    Y(s)/F(s). When finding the transfer function, zero initial conditions must be assumed. The transfer function should

    look like the one s hown below.

    LabVIEW Graphical Approach

    If you choos e to use the transfer function, create a blank VI and add the CD Construct Transfer Function Model VI

    to your block diagram. This VI is located in the Model Cons truction section of the Control Design palette.

    Click the drop-down bo x that shows SISO and select Single-Input Single-Output (Symbolic). To c reate inputs

    for this transfer function, right-click on the Symbolic Numerator te rminal and s elect Create Control. Repeat thisfor the Symbolic Denominator and Variables terminals. These controls will now appear on the front panel.

    Figure 2: Create Transfer Function

  • 8/3/2019 LabVIEW Handout

    34/52

    Next, add the CD Draw Transfer Function VI to your block diagram, located in the Model Const ruction section ofthe Control Design palette. Connect the Transfer Function Model output from the CD Create Transfer Function

    Model VI to the Transfer Function Model input on the CD Draw Transfer Function VI.

    Finally, create an indicator from the CD Draw Transfer Function VI. To do this, right -click on the Equation terminal

    and s elect CreateIndicator.

    Figure 3: Display Transfer Function

    Now create a While Loop, located in the Structures palette, and surround all of the code in the block diagram. Next,

    right-click on the Loop Condition terminal in the bottom-right corner of the While Loop, and select CreateControl.

    Figure 4: Transfer Function with While Loop

    With this VI, you can now create a transfer function for the train sys tem. Try changing the numerator and the

    denominator in the front panel, and obs erve the effects on the transfer function equation .

    Figure 5: Transfer Function Front Panel

    State-Space Model

  • 8/3/2019 LabVIEW Handout

    35/52

    Another method to solve the problem is to use the state-space form. Four matrices A, B, C, and D characterize thesystem behavior and will be used to solve the problem. The s tate-space form which is found from the state -variable

    and the output equations is shown below.

    LabVIEW Graphical Approach

    To model the system using the s tate-space form of the equations, use the CD Construct State-Space Model VI with

    the CD Draw State-Space Equation VI.

    Figure 6: State-Space Model Block Diagram

    With this VI, you can now create a state-space model for the train system. Try changing the terms in the front panel,

    and observe the effects on the state-space model.

  • 8/3/2019 LabVIEW Handout

    36/52

    Figure 7: State-Space Model Front Panel

    Hybrid Graphical/MathScript Approach

    Alternatively, you can use a MathScript Node to create the s tate-space model. To do this, create a blank VI and

    insert a MathScript Node from the Structures palette. Copy and pas te the following m-file code into the MathScriptNode:

    A=[ 0 1 0 0;

    -k/M1 -u*g k/M1 0; 0 0 0 1;

    k/M2 0 -k/M2 -u*g];

    B=[ 0; 1/M1; 0; 0];

    C=[0 1 0 0];

    D=[0];

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

    Next, r ight-click on the left border of the MathScript Node and s elect Add Input. Name the input M1. Repeat

    this process to create inputs for M2, k, u, and g.

    Figure 8: MathScript Node

  • 8/3/2019 LabVIEW Handout

    37/52

    Right-click on the right border of the MathScript Node and s elect Add Output to create an o utput called sys.After creating this output, right-click on it and select Choose Data Type Add-ons SS object.

    Next, right-click on each input and select Create Control.

    Figure 9: MathScript Node with Inputs

    Add the CD Draw State-Space Equation VI to the block diagram, and create an equation indicator. Connect thesys output from the MathScript Node to the State -Space Model input of the CD Draw State-Space Equation VI.

    Finally, create a While Loop around the code, and create a control for the Loop Condition terminal.

    Figure 10: Using MathScript Node to Create State-Space Equation

    With this VI, you can now create a state-space model for the train system. Try changing the terms in the frontpanel, and obs erve the effects on the state-space model.

    Figure 11: State-Space Equation Front Panel

  • 8/3/2019 LabVIEW Handout

    38/52

    The Three-Term Controller

    Consider the following unity feedback system:

    Figure 1: Unity Feedback System

    Plant:A system to be controlled

    Controller:Provides the e xcitation for the plant; Designed to control the overall system behavior

    The transfer function of the PID controller looks like the following:

    Kp = Proportional gain

    Ki = Integral gain

    Kd = Derivative gain

    First, let's take a look at how the PID controller works in a c losed-loop system using the schematic shown above.The variable (e) represents the tracking error, the difference between the desired input value (R) and the actual

    output (Y). This error signal (e) will be sent to the PID controller, and the controller co mputes both the derivative

    and the integral of this error signal. The signal (u) just past the controller is now equal to the proportional gain (Kp)

    times the magnitude of the erro r plus the integral gain (Ki) times the integral of the error plus the derivative gain

    (Kd) t imes the derivative of the error.

    This signal (u) will be sent to the plant, and the new output (Y) will be obtained. This new output (Y) will be sent

    back to the s ensor again to find the new error signal (e). The controller takes this new error s ignal and computes its

    derivative and its integral again . This process goes on and on .

    The Characteristics of P, I, and D Controllers

    A proportional controller (Kp) will have the effect of reducing the rise time and will reduce but never eliminate the

    steady-state error. An integral control (Ki) will have the effect of eliminating the s teady-state error, but it may make

    the trans ient response worse. A derivative control (Kd)will have the effect of increasing the s tability of the system,

  • 8/3/2019 LabVIEW Handout

    39/52

    reducing the overshoot, and improving the transient response. Effects of each of controllers Kp, Kd, and Ki on aclosed-loop system are summarized in the table shown below.

    CL RESPONSERISE TIME OVERSHOOTSETTLING TIMES-S ERROR

    Kp Decrease Increase Small Change Decrease

    Ki Decrease Increase Increase EliminateKd Small Change Decrease Decrease Small Change

    Figure 2: Effect of PID Controllers on Closed-Loop System

    Note that these correlations may not be e xactly accurate, because Kp, Ki, and Kd 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 beused as a reference when you are determining the values for Ki, Kp and Kd.

    Example Problem

    Suppose we have a s imple mass , spring, and damper problem.

    Figure 3: Mass, Spring, and Damper

    The modeling equation of this system is:

    Taking the Laplace transform of the modeling equation, we get:

    The transfer function between the displacement X(s) and the input F(s) then becomes:

    Let M = 1kg, b = 10 N.s/m, k = 20 N/m, and F(s) = 1. If we use these values in the above transfer function, the resultis:

  • 8/3/2019 LabVIEW Handout

    40/52

    The goal of this problem is to show you how each of Kp, Ki and Kd contr ibutes to obtain fast rise time, minimumovershoot, and no steady-state error.

    Open-Loop Step Response

    Let's first view the open-loop step response.

    LabVIEW Graphical Approach

    Create a new blank VI, and insert the CD Construct Transfer Function Model VI an d the CD Draw Transfer

    Function Equation VI, fro m the Model Construction section of the Control Design palette.

    Create controls for the Numerator and Deno minator terminals of the CD Construct Transfer Function Model VI.

    Connect the Transfer Function Model output from this VI to the input terminal of the CD Draw Transfer FunctionEquation VI. Finally, create an indicator from the Equation terminal of the CD Draw Transfer Function VI.

    Create a While Loop around this code, and create a control for the Loop Con dition terminal.

    Next, add the CD Step Response VI to the block diagram. Connect the Transfer Function Model output from the CD

    Construct Transfer Function Model VI to the Transfer Function Model input of the CD Step Response VI. Create anindicator from the Step Response Graph output of the CD Step Response VI.

    Figure 4: Step Response

    Hybrid Graphical/MathScript Approach

    Alternatively, we can use a MathScript Node with the CD Step Response VI to plot the open -loop s tep response, by

    using the following code:

    plant=tf(num,den);

    Make sure to change the MathScript Node output variable data type to TF object.

  • 8/3/2019 LabVIEW Handout

    41/52

    Figure 5: Step Reponse Using MathScript Node

    Result

    Running the VI from either Figure 4 or Figure 5 should return the plot shown below in Figure 6 .

    Figure 6: Step Response Graph

    The DC gain of the plant transfer function is 1/20, so 0.05 is the final value of the output to a 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 t ime, reduce the settling time,and eliminates the steady-state error.

    Proportional Control

    From the table in Figure 3, 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:

  • 8/3/2019 LabVIEW Handout

    42/52

    LabVIEW Graphical Approach

    Change the CD Construct Transfer Function Model VI to SISO (Symbolic) to allow for variables to be used. The

    resulting block diagram is shown in Figure 7.

    Figure 7: Closed-Loop System Using LabVIEW

    Now enter in the closed-loop trans fer function of the syst em with a proportional controller. Let the proportional gain(Kp) equal 300.

    Hybrid Graphical/MathScript Approach

    Alternatively, to achieve this result using a MathScript Node, use the following code:

    num=1;

    den=[1 10 20];

    plant=tf(num,den);

    Kp=300;

    contr=Kp;

    sys_cl=feedback(contr*plant,1);

  • 8/3/2019 LabVIEW Handout

    43/52

    Figure 8: Closed-Loop System Using LabVIEW MathScript

    Note: The m-file function called feedback was used to obtain a closed-loop transfer function directly from the open-loop transfer function (instead of computing closed-loop transfer function by hand).

    Result

    Both the LabVIEW approach and the hybrid approach should yield the graph shown below in Figure 9.

    Figure 9: Proportional Control

    The graph shows that the proportional controller reduced both the rise time and the steady -state error, increased theovershoot, and decreased the settling time by s mall amount.

    Proportional-Derivative Control

    Now, let's take a look at a PD control. From the table in Figure 3, we see that the derivative controller (Kd) reducesboth the overshoot and the settling time. The closed-loop transfer function of the given system with a PD controlleris:

    Let Kp equal 300 as before and let Kd equal 10.

  • 8/3/2019 LabVIEW Handout

    44/52

    LabVIEW Graphical Approach

    Using the VI from Figure 7, modify the input terms on the front panel to add the derivative element to the system.

    Hybrid Graphical/MathScript Approach

    Alternatively, to achieve this result using a MathScript Node, use the VI from Figure 8 with the following code:

    num=1;

    den=[1 10 20];

    plant=tf(num,den);

    Kp=300;

    Kd=10;

    contr=tf([Kd Kp],1);

    sys_cl=feedback(contr*plant,1);

    Result

    Both the LabVIEW approach and the hybrid approach should yield the graph shown below in Figure 10.

    Figure 10: Proportional-Derivative Control

    Compare the graph in Figure 10 to the graph in Figure 9. The s tep response 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 s teady-state error.

  • 8/3/2019 LabVIEW Handout

    45/52

    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:

    Let's reduce the Kp to 30, and let Ki equal 70.

    LabVIEW Graphical Approach

    Using the VI from Figure 7, modify the input terms on the front panel to add the derivative element to the system.

    Hybrid Graphical/MathScript Approach

    Alternatively, to achieve this result using a MathScript Node, use the VI from Figure 8 with the following code:

    num=1;

    den=[1 10 20];

    plant=tf(num,den);

    Kp=30;

    Ki=70;

    contr=tf([Kp Ki],[1 0]);

    sys_cl=feedback(contr*plant,1);

    Result

    Both the LabVIEW approach and the hybrid approach should yield the graph shown below in Figure 11.

  • 8/3/2019 LabVIEW Handout

    46/52

    Figure 11: Proportional-Integral Control

    We have reduced the proportional gain (Kp) becaus e the integral controller also reduces the rise time and increases

    the overshoot as the proportional controller does (double effect). The above response in Figure 11 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 c losed-loop transfer function of the given system with a PIDcontroller is:

    After several trial and error runs, the gains Kp=350, Ki=300, and Kd= 50 provided the desired response.< /p> < h3>

    LabVIEW Graphical Approach< /h3> < p> To confirm, tes t these terms in your VI, using the VI from Figure 7.

    < h3> Hybrid Graphical/MathScript Approach

    Alternatively, this result can be achieved with a MathScript Node, by us ing the VI from Figure 8 with the following

    code:

    num=1;

    den=[1 10 20];

    plant=tf(num,den);

    Kp=350;

    Ki=300;

    Kd=50;

    contr=tf([Kd Kp Ki],[1 0]);

  • 8/3/2019 LabVIEW Handout

    47/52

    sys_cl=feedback(contr*plant,1);

    Result

    Both the LabVIEW approach and the hybrid approach should yield the graph shown below in Figure 12.

    Figure 12: Proportional-Integral-Derivative Control

    Now, we have obtained a closed-loop s ystem with no overshoot, fast rise time, and no s teady-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 des ired

    response.

    1.

    Obtain an open-loop response and determine what needs to be improved2. Add a proportional control to improve the rise time3. Add a derivative control to improve the overshoot4. Add an integral control to eliminate the steady-state error5. 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.

    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 s ystem. Keep the controller as s imp le as

    possible.

    Closed-Loop Poles

    The root locus of an open -loop transfer function H(s) is a plot of the locations (locus) of all possible closed loop

    poles with proportional gain k and unity feedback:

  • 8/3/2019 LabVIEW Handout

    48/52

    Figure 1: Closed-Loop System

    The closed-loop transfer function is:

    Thus, the poles of the closed loop system are values of s such that 1 + K H(s) = 0.

    If we use the relation H(s) = b(s)/a(s), then the previous equation has the form:

    Let n = order of a(s) and m = order of b(s) [the order of a polynomial is the highest power of s that appears in it].

    We will cons ider all positive values of k. In the limit as k -> 0, the poles of the closed-loop system are a(s) = 0 or thepoles of H(s). In the limit as k -> infinity, the poles of the closed-loop system are b(s) = 0 or the zeros of H(s).

    No matter what we pick k to be, the closed -loop s ystem must always have n poles, where n is the number of poles of

    H(s). The root locus must have n branches, each branch starts at a pole of H(s) and goes to a zero of H(s). If H(s) hasmore poles than zeros (as is often the case), m < n and we say that H(s) has zeros at infin ity. In this case, the limit of

    H(s) as s -> infinity is zero. The nu mber of zeros at infinity is n-m, 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 poss ible 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 s elected poles are on the right half

    plane, the closed-loop system will be uns table. 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 l ike 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

  • 8/3/2019 LabVIEW Handout

    49/52

    How do we design a feedback controller for the system by using the root locus method? Say our des ign criteria are

    5% overshoot and 1 second rise time.

    LabVIEW Graphical Approach

    We can create a VI to plot the root locus, using the CD Root Locus VI from the Model Cons truction section of the

    Control Design palette.

    Figure 2: Plotting Root Locus

    Hybrid Graphical/MathScript Approach

    Alternatively, you can use a MathScript Node to plot the root locus, using the following code :

    num=[1 7];

    den=conv(conv([1 0],[1 5]),conv([1 15],[1 20]));

    sys=tf(num,den);

    Figure 3: Plotting Root Locus Using MathScript Node

  • 8/3/2019 LabVIEW Handout

    50/52

    LabVIEW MathScript Approach

    Yet another approach to this problem is to use the MathScript Window. Select Tools MathScript Window, and

    enter the following code in the Command Window:

    num=[1 7];

    den=conv(conv([1 0],[1 5]),conv([1 15],[1 20]));

    sys=tf(num,den);

    rlocus(sys)

    axis([-22 3 -15 15])

    Result

    Using either the LabVIEW g raphical approach, th e LabVIEW MathScript approach, or the hybrid

    graphical/MathScript approach should return a plot s imilar to the one s hown below in Figure 4.

    Figure 4: Root Locus Plot

    Choosing a Value of K from the Root Locus

    The plot in Figure 4 above shows all possible closed-loop pole locations for a pure proportional controller.Obvious ly not all of those closed-loop poles will satisfy our des ign criteria.

    To determine what part of the locus is acceptable, we can us e the command sgrid on to plot lines of constantdamping ratio and natural frequency. In our problem, we need an overshoot less than 5% (which means a da mpingratio Zeta of greater than 0.7) and a rise t ime of 1 second (which means a natural frequency Wn greater than 1.8).

    Enter the co mmand sgrid on in the MathScript co mmand window and press Enter.

  • 8/3/2019 LabVIEW Handout

    51/52

    The following figure shows the plot that you should see. The red and green lines have been superimp osed on the

    plot.

    Figure 6: Root Locus Plot with Grid Lines

    On the plot above, the diagonal lines indicate constant damping ratios (Zeta), and the semic ircles indicate lines of

    constant natural frequency (Wn). The red lines superimposed on the graph indicate pole locations with a damping

    ratio of 0.7. In between these lines, the poles will have Zeta > 0.7 and outs ide of the lines Zeta < 0.7. The green

    semicircle indicates pole locations with a natural frequency Wn = 1.8; inside the circle, Wn < 1.8 and outside thecircle Wn > 1.8.

    Going back to our problem, to make the overshoot less than 5%, the poles have to be in between the two red lines,

    and to make the rise t ime shorter than 1 second, the poles have to be outside of the green semicircle. So now we

    know only the part of the locus outside of the semicircle and in between the two lines are acceptable. All the poles inthis location are in the left-half p lane, so the closed-loop system will be s table.

    From the plot above we see that there is part of the root locus inside the desired region. So in this case we need onlya proportional controller to move the poles to the desired region.

    You can use rlocfind command in the MathScript Window to choose the desired poles on the locus :

    [k,poles] = rlocfind(sys)

    Click and drag the closed-loop poles on the plot to designate 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.

    Figure 7: Interactive Root Locus in MathScript Window

  • 8/3/2019 LabVIEW Handout

    52/52

    Note that since the root locus may have more than one branch, when you select a pole, you may want to find outwhere the other poles are. Remember they will affect the response too. From the plot above we see that all the poles

    selected are at reasonable pos itions. We can go ahead and use the chosen k as our proportional controller. Click OKto select these poles.

    Closed-Loop Response

    In order to find the step response, you need to know the closed-loop transfer function. You could compute this, or let

    LabVIEW do it for you in the MathScript Window.

    sys_cl= feedback(k*sys,1)

    The two arguments to the function feedbackare the nu merator and denominator of the open-loop system. Youneed to include the proportional gain that you have chosen. Unity feedback is assumed.

    Finally, check out the step response of your closed-loop system.

    step(sys_cl)

    Figure 8: Closed-Loop Response

    As we expected, this response has an overshoot less than 5% and a rise time less than 1 second.