8- Design Control Simulation

88

description

labview

Transcript of 8- Design Control Simulation

Page 1: 8- Design Control Simulation
Page 2: 8- Design Control Simulation

Introduction to simulation withControl Design and Simulation Module

• Develop dynamic systems such as motor controllers and hydraulic simulators with LabVIEW

• Implement your dynamic systems with real-time I/O using built-in LabVIEW data acquisition functions

• Simulate linear, nonlinear, and discrete systems with a wide array of solvers

• Deploy dynamic systems to real-time hardware with the NI LabVIEW Real-Time Module

• Translate models from The MathWorks, Inc. Simulink® into LabVIEW with built-in utility

Page 3: 8- Design Control Simulation

The Simulation Palette on the Functions Palette

Page 4: 8- Design Control Simulation

Overview over the Functions and possible Subpalettes on the Simulation Palette

1. Simulation Loop, which defines the borders of the simulation diagram on which the simulation functions are placed. Note that (most of) the functions found on the Simulation palette can be placed only inside a simulation loop.

2. Continuous palette containing the following functions and/or subpalettes:

Zero-Pole-Gain Function Space Delay , Derivative, Time Delay, State Space ,Transfer Function and Zero-Pole-Gain

3. Nonlinear palette containing the following functions: Backlash, Friction, Quantizer, Dead Zone, Rate Limiter, Relay, Saturation, Switch and Zero Cross Detection

4. Discrete palette containing the following functions: Discrete Integrator, Discrete Unit Delay, Discrete Zero-Order Hold, Discrete First-Order-Hold, Discrete State Space, Discrete Transfer Function, Discrete Zero-Pole-Gain and Discrete Filter

Page 5: 8- Design Control Simulation

Overview over the Functions and possible Subpalettes on the Simulation Palette

5. Signal Generation palette containing the following functions: – Chirp Signal, Ramp, Pulse, Signal Generator, Sine Wave , Step and indexer

6. Signal Arithmetic palette containing the following functions: – Gain – Summation, with any number of inputs. Inputs may also be subtracted. – Multiplication, with any number of inputs. Inputs can slo be used as divisors

7. Lookup Tables palette containing the following functions, which may be used to look up values from arrays of tabular (e.g. experimental) data:

– Lookup Table 1D (dimension one) – Lookup Table 2D (dimension two) – Lookup Table 3D (dimension three)

Page 6: 8- Design Control Simulation

Overview over the Functions and possible Subpalettes on the Simulation Palette

8) Graph Utilities palette containing the following functions:

8) SimTime Waveform, which is used to plot simulated variables continuously in a LabVIEW Chart.

9) Buffer XY Graph, which is used to collect arrays of signals to be plotted in a LabVIEW Graph.

9) Trim & Linearize palette containing several functions for calculating an operating point and to have LabVIEW develop a linear model in that operating point.

10) Optimal Design palette containing functions to calculate optimal PID settings for a process model from an objective function (optimization criterion).

11) CD Implementation palette containing functions to calculate state estimations using a Kalman Filter. (CD = Control Design. The same functions are found on the CD palette, too.)

Page 7: 8- Design Control Simulation

Overview over the Functions and possible Subpalettes on the Simulation Palette

12. Utilities palette containing the following functions:

– Collector, which collects a signal at each time step of the simulation and outputs a history of the signal value and the time at which this function recorded each value in the history. The Collector builds an array on its output from the input it receives at each simulation step.

– Memory, which stores the value of the Input signal from the previous iteration of the simulation. You can use this function to transfer values from one iteration of the Simulation Loop to the next.

– Indexer, which is used to index an array or waveform (like the ordinary array indexing function in LabVIEW) by the current simulation time.

– Simulation Parameters, which can be used to read or get (not set) simulation parameters.

– Simulation Time, whose output is the simulation time. It works like a simulation clock.

– Report Sim Error – Halt Simulation, which stops the simulation when its input becomes False.

Page 8: 8- Design Control Simulation

The Simulation Loop

Page 9: 8- Design Control Simulation

The Simulation Loop

• Executes the simulation diagram until the Simulation Loop reaches the simulation final time or until the Halt Simulation function stops the execution programmatically.

• You must place all Simulation functions within a Simulation Loop or in a simulation subsystem.

Page 10: 8- Design Control Simulation

The Simulation Loop• The Simulation Loop has an Input Node and an Output Node.

• Use the Input Node to configure simulation parameters programmatically.

• You also can configure these parameters interactively using the Configure Simulation Parameters dialog box.

• Access this dialog box by double-clicking the Input Node or by right-clicking the border and selecting Configure Simulation Parameters from the shortcut menu.

Page 11: 8- Design Control Simulation

The Simulation Loop

The simulation loop is the core component of the Simulation Module.

The loop consists of three main parts: Input node (Left)- allows simulation parameters to be programmatically defined. By default, these parameters are static and can be configured by double-clicking the input node.

Main loop – The system to be simulated is placed here.

Output node (Right) – returns any errors that may have happened in the loop, such as an improper transfer function.

Page 12: 8- Design Control Simulation

Simulation Loop Parameters

Page 13: 8- Design Control Simulation

Configuring a SimulationSimulation Parameters Tab

• Simulation Time – Specifies for what period of “simulation time” how long the simulation should run.

� Initial Time (s)—Specifies the time at which to start the ODE solver. The default is 0 seconds.

� Final Time (s)—Specifies the time at which to stop the ODE solver. The default is 10 seconds. Enter a value of Inf to run the simulation continuously until the Halt Simulation function stops the execution programmatically.

To make simulation execute each loop in certain time (to make delay) let the simulation frequency=1k Hz, and change the period value such that the one loop execution= period * 1msec

Page 14: 8- Design Control Simulation

Configuring a Simulation• Solver Method – Specifices what Ordinary

Differential Equation ODE) solver is used to solve integral and differential type blocks in the simulation.

• Time Step and Tolerance – These settings control the window of time steps used by LabVIEW. Typically, the default settings will suffice, but adjust them if necessary.

• Discrete Time – While the Default Auto Discrete Time option will typically work for most simulations, you can force LabVIEW to use a specific step size here.

Page 15: 8- Design Control Simulation

Configuring a Simulation• Timing Parameters Tab

Page 16: 8- Design Control Simulation

Configuring a SimulationTiming Parameters Tab

• Timing – The software timing option is always used when performing simulations. This will solve the equation as fast as the CPU can. To observe a simulation run in “real time”, disable the software timing option.

• Loop Timing Source – When implementing a simulation in hardware, change the loop timing source to an available hardware timing. This option can be used to sync the simulation execution with an external time source, such as the operating system clock or even a Data Acquisition board’s clock.

• Loop Timing Parameters – These options control how the loop executes with respect to the selected timing source.

• For more detailed information on these options, consult the LabVIEW help by clicking the Help button.

Page 17: 8- Design Control Simulation

Generating Simulation Input• Simulations can utilize a wide variety

of signals sources. Simulated signalsare useful for characterizing systemresponse and testing corner cases.

• Chirps provide a useful frequency sweep, and pulses provide good step response information.

• The indexer is useful for using a predetermined arbitrary signal, and can interpolate between samples for better resolution.

• Once the simulation is confirmed, a real world signal can be substituted for the simulation signal.

Page 18: 8- Design Control Simulation

Capturing Simulation Output• The Simulation Time Waveform chart is a special numeric

indicator that displays one or more plots.

• Plots are update as simulation loop executes

Page 19: 8- Design Control Simulation

Signal GenerationThis example demonstrates how to use the Signal Generator function, which you can use to generate sine, sawtooth, square, and random waveforms.

Page 20: 8- Design Control Simulation

How to show terminals of the VI

Page 21: 8- Design Control Simulation

How to show terminals of the VI

Page 22: 8- Design Control Simulation

Step Signal Generation

Page 23: 8- Design Control Simulation

Continuous VI (Derivative)

Page 24: 8- Design Control Simulation

Continuous VI (Integral)

Page 25: 8- Design Control Simulation

Applying Input signal to TF• This example applies a square wave input to a continuous transfer function

model.

Page 26: 8- Design Control Simulation

Applying Input signal to TF (zero-pole-gain) Model

Page 27: 8- Design Control Simulation

Timing (Built in Timer)This VI demonstrates how to use the built-in 1 kHz timer to run a simulation in real-time.

Page 28: 8- Design Control Simulation

Nonlinear for Simulation (Switch)

This example demonstrates how to use the Switch function, which returns one of twovalues based on whether a signal is above or below a threshold

Page 29: 8- Design Control Simulation

Nonlinear for Simulation (Relay)

Page 30: 8- Design Control Simulation
Page 31: 8- Design Control Simulation

Nonlinear for Simulation (Saturation)

This example demonstrates how to use the Saturation function, which limits the validrange of a signal.

Page 32: 8- Design Control Simulation

Nonlinear for Simulation (Dead Zone)This example demonstrates how to model a dead zone function, which occurs when function inputs do not affect the function outputs.

Page 33: 8- Design Control Simulation
Page 34: 8- Design Control Simulation

Nonlinear for Simulation (Quantizing)

Page 35: 8- Design Control Simulation

Nonlinear for Simulation (Rate Limiter)

Page 36: 8- Design Control Simulation

An example: Simulator of a liquid tank

1. Developing the mathematical model of the system to be simulated

The system to be simulated is a liquid tank. The simulator will calculate and display the level h at any instant of time. The simulation will run in Real time, with the possibility of scaled real time, thereby giving the feeling of a "real" system. T he user can adjust the inlet by adjusting the pumpcontrol signal, u.

Liquid tank

Page 37: 8- Design Control Simulation

Simulator of a liquid tank• Any simulator is based on a mathematical model of the system

to be simulated. Thus, we start by developing a mathematical model of the tank.

• The liquid density is the same in the inlet, in the outlet, and in the tank.

• The tank has straight, vertical walls.

• The liquid mass and level are related through

• The inlet volumetric flow through the pump is proportional to the pump control signal:

Page 38: 8- Design Control Simulation

Simulator of a liquid tank

• The outlet volumetric flow through the valve is proportional to the square root of the pressure drop over the valve.

• This pressure drop is assumed to be equal to the hydrostatic pressure at the bottom of the tank (sqrt means square root):

• Mass balance (i.e., rate of change of the mass is equal to the inflow minus the outflow) yields the following differential equation:

or, using the above relations,

Page 39: 8- Design Control Simulation

Simulator of a liquid tank

• As a proper starting point of drawing the mathematical block diagram, we write the differential equation as a state-space model, that is, as a differential equation having the first order time derivative alone on the left side.

• This can be done by pulling ρ and A outside the differentiation, then dividing both sides by ρA. The resulting differential equation becomes

Page 40: 8- Design Control Simulation

Simulator of a liquid tank

Mathematical block diagram of Differential Equation (3)

Page 41: 8- Design Control Simulation

Simulator of a liquid tank

• We will assume that there are level alarm limits to be displayed in the simulator. The limits are

• AH_h = 0.9m (Alarm High)• AL_h = 0.1m (Alarm Low)

• The block diagram developed above will be implemented in a Simulation Loop in the Block diagram of our simulation VI.

Page 42: 8- Design Control Simulation

Simulator of a liquid tank2. The Front panel and the Block diagram of the

simulator

Page 43: 8- Design Control Simulation
Page 44: 8- Design Control Simulation
Page 45: 8- Design Control Simulation

Simulator of a liquid tank• The Integrator function performs time-integration of the input to the

function. • Open the Configuration window of the Integrator block by double-clicking

the block. The figure below shows the Integrator Configuration window.

Page 46: 8- Design Control Simulation

Simulator of a liquid tankHere are comments to the Integrator Configuration window:

• The Parameters field contains a number of parameters that can be configured. Once you have selected a parameter in the list, you have two options in the Parameter source field:

1. By selecting Configuration Dialog Box, which is the default option, you can set the value of the selected parameter directly in the dialog box. This is an internal setting of the parameter.

2. By selecting Terminal an input terminal is created on the left part of the block in the Simulation Loop, and you can wire a value of the correct data type to that input. This is an external setting of the parameter. In our VI the following parameters have been set externally: Initial condition; Upper Limit; Lower Limit. The terminals wired to these three block inputs are h_max, h_min, and h_init.

Page 47: 8- Design Control Simulation

Simulator of a liquid tank• The Summation function can be configured to perform both summation

and subtraction. • Open the Configuration window of the Summation block by double-

clicking the block. The figure below shows the Summation Configuration window.

Here are comments to the Summation Configuration window:

• You can select the number of inputs via the Inputs list, and select Add or Subtract or Disable for each input (by clicking on the symbols inside the circle).

• You can also select among Rectangle and Circle Icon shape, and whether the input is a scalar (single signal) or a vector (multiple signals).

Page 48: 8- Design Control Simulation

Simulator of a liquid tank

• The Multiplication function is configured in a similar matter as the Summation block. You can add and remove inputs, and select Multiply or Divide or Disable for each input.

• The SimTime Waveform function is copied from the Graph Utilities palette. The default time format is Absolute Time which is not particularly convenient since it displays 1.1.1904 as a reference time. I suggest using the Floating point format in stead (this is set in the Format & Precision tab of the Property window of the Waveform Chart).

• The Chart is automatically emptied before the VI starts running, so you do not have to create any Property node for this Chart for this purpose

Page 49: 8- Design Control Simulation

3. Configuring the simulation By expanding the Input Node outside the left part of the Simulation Node (by pulling the bottom of the node downwards).

Here are comments to the parameters that are wired:

The Intial Time defines the initial simulation time, typically zero.

The Final Time defines the simulation time when the simulation will stop. In our VI the final time is set to Inf (infinity). Will the simulation never stop, then? It will stop, due to the Halt Simulation function in the block diagram..

The Time Step is the resolution of the simulation time scale, see the figure below.

Page 50: 8- Design Control Simulation

• In general, the smaller time step the better accuracy, but the larger the total number of calculations needed to calulate the simulated response for a given simulation time range. The main rule of selecting the time step is: Select the largest time step that does not influence the accuracy of the simulated response. You may iterate to find this largest time step. Start with some initial guess of the time step, and try increased values until you observe that the response is influenced by the time step. As an initial guess you may select the time step as Ts = 0.1*Tmin

• where Tmin is the smallest time constant of the model. If I do not know the smallest time constant, I set h = 0.05s which gived a fairly smooth update of the plotted simulation response on the screen.

• The ODE Solver is the numerical method used to calculate - or solve for - the values of the state variables of the model. (ODE is abbreviation for Ordinary Differential Equation.) In general, the simpler solver, the less burden on the LabVIEW and the computer to perform the simulation, but also, unfortunately, the less accuracy of the simulated response. This selecting the solver method is a trade-off. From experience, using the Runge-Kutta second order method (with a fixed time step) works fine in most situations. (This method is quite similar to numerical integration using the trapezoid rule.)

• There are fixed-step solvers and variable-step solvers. In variable-step solvers LabVIEW automatically calculates the time step. However, it is my experience that the simulation runs smoother and that is it easier to post-process the simulation data if you use a fixed-step solver. Therefore I suggest using a fixed-step solver.

Page 51: 8- Design Control Simulation

• Software Timing set to True makes LabVIEW run the simulation as fast as it can, i.e. the simulation time is not equal to the real time. Software Timing set to False gives you the possibility to set how fast the simulation will run. This is done with the Period parameter described below.

• The Discrete Time Step is the time step used for simulating dicrete-time functions (blocks). In the block diagram of our example the discrete time step is the ∆tzparameter. Examples of discrete-time functions are discrete-time signal filters. Typically the discrete time step is set equal to the simulation time step, as in our example.

• The Period is the amount of real time between two subsequent simulated time points. By setting the Period equal to the simulation time step, Ts, the simulation runs in real time.

• By giving the Period some other value, the simulation time scale is proportional to real time. For example, if the simulation time step is 0.05s, setting Period equal to 0.01s causes the simulation to run 5 times faster than real time (thereby speeding up the simulation for a slow system), while setting Period equal to 0.25s causes the simulation to run 5 times slower than real time (thereby slowing down the simulation for a fast system). Setting Period to 0 causes LabVIEW to run the simulation as fast as possible (on the given computer).

Page 52: 8- Design Control Simulation

• Note that on a PC Period is the number of milliseconds, since the PC clock runs with a frequency of 1kHz. For example, Period = 10 corresponds to Period equal to 0.01s. (It is however possible to use some other (faster) timing source than the PC clock.)

• In our example the Period is calculated from the Time Step by dividing the Time Step by the terminal labeled Time_scaling_factor. The user can adjust the value of Time_scaling_factor on the front panel. The Round to Nearest (integer) function is used to make the result of the division an integer.

• Disconnect each of the wires now connected to the Input Node of the Simulation loop, and remove brolen wires (keyboard shortcut Ctrl + B). Then, reconnect each of them, cf. this figure. Ensure the VI is without errors, then save the VI.

Page 53: 8- Design Control Simulation

• Setting simulation parameters in a dialog window• By right-clicking on the border of the Simulation loop and selecting

Configure Simulation Parameters in the menu that is opened,the Configure Simulation Parameters dialog window with theSimulation Parameters tab is opened, see the figure.

Page 54: 8- Design Control Simulation

Setting simulation parameters in a dialog window

Most of the setting parameters in the Simulation Parameterstab are as for the programmatic parameter settings describe before.

Here are some additional comments:

If Auto Discrete Time is selected, the Discrete Time Step is automatically set equal to the simulation time step if you use a fixed-step solver, but it is automatically set to the initial time step for variable-step solvers.

By default Auto Discrete Time is selected. Suggested that you uncheck the Auto Discrete Time checkbox and set the Discrete Time Step explicitly.

Page 55: 8- Design Control Simulation

Setting simulation parameters in a dialog window

• The figure shows the Timing Parameters tab.

Comments to the Timing Parameters tab:

The Period is the same as for the programmatic parameter settings describe before.

The Software Timing is the same as for the programmatic settings described before. By default Software Timing is selected.

If Auto Period is selected LabVIEW automatically sets the period of the simulation equal to the simulation time step. By default Auto Period is not selected.

Page 56: 8- Design Control Simulation

The modeled example in previous presentation (Motor Control System)

A common control system consists of a controller model and a plant model. The output of the controller is sent to the plant. In a closed loop system, the output of the plant is subtracted from the input (set point) of the system, producing an error value, which acts as the controller input. This is known as feedback.

Page 57: 8- Design Control Simulation

– Plant (Motor) Model• The plant model is a mathematical representation of the system in

question. In this case, the plant is a motor.• The input to the motor is voltage (Vm), and the output from the

motor is angular velocity in radians per second (ωm).

– Controller Model: PID• The controller model contains a mathematical algorithm that

supplies an input to the plant model based on the error.• PID (Proportional, Integral, Derivative) is a common algorithm

used in control systems.• The input to the PID controller is error (setpoint – output) in

radians (θ). The output from the PID controller is voltage (Vm).

– Integrator: 1/s• The integrator is used to convert the output from the motor plant

(angular velocity) to have units consistent with the setpoint of the system (angular position).

Page 58: 8- Design Control Simulation
Page 59: 8- Design Control Simulation
Page 60: 8- Design Control Simulation

Now Simulate A Motor Control System

• Build a PID controller with Simulation Module.• Create a state model that combines PID controller with existing transfer function.• Use different solver methods in the Simulation loop to optimize performance.

Page 61: 8- Design Control Simulation

Simulate A Motor Control System by Step Signal

Page 62: 8- Design Control Simulation

Simulate A Motor Control Systemby Step Signal

Page 63: 8- Design Control Simulation

Simulate A Motor Control System by any Reference Voltage

Download the VI

Page 64: 8- Design Control Simulation

Hardware DC Motor Simulation

Download the VI

1 sample ondemand

Can be increasedTo have more

accurate response

LP filter toreduce noise

Page 65: 8- Design Control Simulation

Example of Hardware-in-the-loop (HIL) simulation

• A Fuji PYX5 PID controller controls a simulated first order plus time-delay process

Fuji PYX5 Process Controller

Page 66: 8- Design Control Simulation

Example of Hardware-in-the-loop (HIL) simulation

• The Fuji controller controls a simulated process. The simulator runs in real time and is implemented in LabVIEW Simulation Module running on a PC.

• The analog control signal from the Fuji controller controls the simulated process via one of the analog input channels on the USB-6008 device, and the simulated process measurement signal is connected to the controller via one of the analog output channels on the USB-6008 device.

Page 67: 8- Design Control Simulation

Example of Hardware-in-the-loop (HIL) simulation

USB-6008 device

Page 68: 8- Design Control Simulation

Spring Example• The following differential equation describes the dynamic

system.F(t) – cx'(t) – kx(t) = mx''(t) where t is the simulation timeF(t) is the external applied forcec is the damping constant of the springk is the stiffness of the spring m is a mass and x(t) is the position of the massx' is the first derivative of the position, which equals the velocity of the mass. x'' is the second derivative of the position, which equals the acceleration of the mass.

Page 69: 8- Design Control Simulation

Spring Example• The goal is to use the LabVIEW Simulation Module to view

the position x(t) of the mass m with respect to time t.

• Substituting terms from the differential equation above yields the following equation:x''(t) = (F(t) – cx'(t) – kx(t))/m

• will construct a simulation diagram that iterates the following steps over a period of time.

– Divides a known force by a known mass to calculate the acceleration of the mass.

– Integrates acceleration to calculate the velocity of the mass. – Integrates velocity to calculate the position of the mass.

Page 70: 8- Design Control Simulation

Block Diagram of Spring ExampleAssuming c & k have zero values

Page 71: 8- Design Control Simulation

Configuring Simulation Parameters• Ensure the value of the Final Time (s) numeric control is 10, which specifies that

this tutorial simulates ten seconds of time.

• Click the ODE Solver pull-down list to view the list of ODE solvers the Simulation Module includes. If the term (variable) appears next to an ODE solver, that solver has a variable step size. The other ODE solvers have a fixed step size. Ensure a checkmark is beside the default ODE solver Runge-Kutta 23 (variable)

• Because this ODE solver is a variable step-size solver, you can specify the Minimum Step Size and Maximum Step Size this ODE solver can take. Enter 0.01 in the Maximum Step Size (s) numeric control to limit the size of the time step this ODE solver can take.

• Click the Timing Parameters tab to access parameters that control how often the simulation executes.

• Ensure the Software Timing checkbox contains a checkmark. This option specifies that the simulation executes without any timing restrictions. Note Use this option when you want the simulation to run as fast as possible. If you were running this simulation on a real-time (RT) target, you can remove the checkmark from this checkbox and configure how often the simulation executes.

Page 72: 8- Design Control Simulation

Simulation Output of of Spring Example

Page 73: 8- Design Control Simulation

Representing Damping “c” and Stiffness “k”

Page 74: 8- Design Control Simulation

Simulation Output of of Spring Example

Page 75: 8- Design Control Simulation

Simulation Modeling for DC Motor Position

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 transitional motion.

Page 76: 8- Design Control Simulation

Simulation Modeling for DC Motor Position

• The electric circuit of the armature and the free body diagram of the rotor are shown in the following figure:

Page 77: 8- Design Control Simulation

Simulation Modeling for DC Motor Position

For this example, we will assume the following values for the physical parameters. These values were derived by

• moment of inertia of the rotor (J) = 3.2284E-6 kg.m^2/s^2 • damping ratio of the mechanical system (b) = 3.5077E-6

Nms• electromotive force constant (K=Ke=Kt) = 0.0274

Nm/Amp • electric resistance (R) = 4 ohm • electric inductance (L) = 2.75E-6 H • input (V): Source Voltage• output (theta): position of shaft • The rotor and shaft are assumed to be rigid

Page 78: 8- Design Control Simulation

Simulation Modeling for DC Motor Position

Page 79: 8- Design Control Simulation

Simulation Modeling for DC Motor Position

Next, we will start to model both Newton's law and Kirchoff'slaw. These laws applied to the motor system give thefollowing equations:

Page 80: 8- Design Control Simulation

Open Loop Response

Page 81: 8- Design Control Simulation
Page 82: 8- Design Control Simulation

Open Loop Response

Page 83: 8- Design Control Simulation

Implementing Digital Control

In the Motor Position Digital Control example, a digital controller was designed with the following transfer function:

Page 84: 8- Design Control Simulation

Closed-Loop Response

Page 85: 8- Design Control Simulation
Page 86: 8- Design Control Simulation
Page 87: 8- Design Control Simulation
Page 88: 8- Design Control Simulation

Closed-Loop Response