Вы находитесь на странице: 1из 13

MEEN 364

J. Mlcak
Last Update: 8/27/2006
Simulation Interface Toolkit (SIT) Tutorial
Simulink is an add-on toolkit for MATLAB, and it provides a graphical
environment for the design and interactive execution of dynamic system models. The
user can create models in Simulink using customizable blocks. Using graphical wires to
connect the blocks, the user can specify data flow. In some computer systems, Simulink
can connect directly to the data acquisition board, but in this lab, the National Instruments
PXI system is the hardware that performs data acquisition. The PXI system is a National
Instruments product, and LabVIEW must be used to perform data acquisition with this
system. Therefore, a method is needed to allow a Simulink model to perform data
acquisition. You should bring this tutorial to lab whenever Simulink will be used to
perform data acquisition operations.

1. About SIT

The Simulation Interface Toolkit (SIT) provides a way to create a LabVIEW user
interface that you can use to interact with a Simulink model. With a LabVIEW user
interface, you can manipulate the model parameters and view the output data of the
Simulink model immediately. With a different type of system, Simulink can function
independent of SIT or LabVIEW, but the current lab configuration with low-profile
computers requires the use of SIT and LabVIEW.

For LabVIEW to communicate successfully with MATLAB and Simulink, you


must have MATLAB running at all times. When you launch MATLAB, you also start
the Simulation Interface Toolkit (SIT) Server, which enables LabVIEW and MATLAB to
communicate with each other. Figure 1 displays the interaction between LabVIEW and
Simulink.

LabVIEW

Host VI

Front Panel
MATLAB and Simulink
Block Diagram
SIT Server
model
TCP/IP

Host Computer
Host Computer
Figure 1. Running a Simulink Model using LabVIEW

The LabVIEW/SIT interface behaves differently when data acquisition is


involved in the Simulink model. Simulink can function independently as a modeling and
calculation tool, so SIT is not needed when data acquisition is not involved.

1
MEEN 364
J. Mlcak
Last Update: 8/27/2006
2. SIT Example

In the following example, a signal is acquired, and the acquired signal is


amplified by a user-defined amount. Finally, the amplified signal is output from the data
acquisition terminal. The same procedure can be applied when Simulink is used to
sample or generate a signal. A schematic of the system setup is given in Figure 2.

LabVIEW

Host VI

Front Panel
MATLAB and Simulink
Block Diagram
SIT Server
model
TCP/IP

Host Computer
TCP/IP

Host Computer

Function
Generator
National ACH0
Instruments
Ch1

NI PXI-1031
DAC0
Oscilloscope
Ch2
Figure 2. Schematic of system setup.

Begin by creating a file folder for your project. Open MATLAB and change the
working directory to your project folder. The next step is to create a Simulink diagram.
Open Simulink by starting MATLAB and typing Simulink at the command prompt.
In the Simulink Library Browser window, go to File >> New >> Model. The model
window will appear, and this is similar to a LabVIEW front panel and block diagram.
The Simulink Library Browser contains the blocks that are added to the diagram window.
The diagram should consist of an input, a gain, and an output as shown in Figure 3. For
any data that will be displayed as an indicator (numerical or graphical) on the LabVIEW
front panel or logged using LabVIEW, a NI Sink block must be added to the Simulink
diagram.

2
MEEN 364
J. Mlcak
Last Update: 8/27/2006

Figure 3. Simulink diagram

*NOTE: When using SIT, at least one pair of “In” and “Out” blocks must appear
on every diagram regardless if the blocks are used or not. If an “In” or “Out” block
is not used for analog input, analog output, or a subsystem, do not connect it to
anything.

The next step is to set the solver type, simulation step size, sampling frequency,
and the real-time workshop type. By selecting Simulation >> Simulation Parameters,
change the settings under various tabs within the Simulation Parameters dialog box as
shown in Figure 4 a, b, and c. In the solver tab, leave the default value for stop time.
Under the solver options, select the type as fixed-step with the ode5 (Dormand-Prince)
solver. Under the fixed step size, type in the sampling period in seconds. For example, if
sampling at 100 Hz, type 0.01 seconds.

In the Workspace I/O tab, uncheck all boxes in the Save to workspace area, and
uncheck the “Limit data points to last” dialog box in the Save options area. Select the
Real-Time Workshop tab and select browse. In the dialog box, select the LabVIEW DLL
Target, nidll.tlc and click OK. Click Apply and OK to close the Simulation
Parameters dialog box.

3
MEEN 364
J. Mlcak
Last Update: 8/27/2006

(a) (b)

(c)
Figure 4. a, b, and c. Simulation parameters.

In the Simulink diagram, select the drop down menu in the upper toolbar and
choose “External” as shown in Figure 5.

Figure 5. Diagram window setting of external mode

4
MEEN 364
J. Mlcak
Last Update: 8/27/2006

*NOTE: For best results, label all input blocks, output blocks, gains, constants, and
NI Sinks with a unique name that will allow you to determine what each block
contains. This will be important at a later step. Also, all constant and gain blocks
should contain a nominal value that will be close to the true value. If the true values
are not known, leave the constant or gain as the default value.

Save the Simulink model under a filename that does not contain spaces and
does not begin with a number. For future reference, the term filename will be used to
denote your Simulink filename. Before continuing, ask your lab Teaching Assistant
review your Simulink model.

Close any LabVIEW programs that may be open, and build (compile) the
Simulink model by selecting Tools >> Real-Time Workshop >> Build Model, or press
Ctrl+B. Building the Simulink model creates a dynamic link library (DLL) file. The
Simulation Interface Toolkit includes a plug-in for Real-Time Workshop that converts
the .mdl file into C code and then compiles the C code into a model DLL. The model
DLL contains all aspects of the Simulink model but no longer requires Simulink to run.

Allow the Simulink model to build. LabVIEW will open and close in the process.
The build is complete when the MATLAB command prompt displays:

### Successful completion of Real-Time Workshop build


procedure for model: filename.

*NOTE: Do not close MATLAB throughout the process detailed in this tutorial
because the SIT server only runs when MATLAB is running.

The next step involves creating a LabVIEW interface to run the Simulink
diagram. Open LabVIEW and create a New (blank) VI. The LabVIEW execution target
should be LabVIEW for Windows. On the Front panel, add a numeric or dial control for
every data value you wish to alter from Simulink. In this example, it is desired to change
the signal amplification value and see the effects. A numerical or graphical indicator for
every data value you wish to view must be added. There should be at least one numerical
or graphical indicator for each NI Sink block in Simulink. The front panel for this
example is shown in Figure 6. Be sure to label all items on the front panel with a unique
and descriptive label.

Once the Front panel is completed, select Tools >> SIT Connection Manager. A
dialog box will open. Select “Load Model” and verify that the Select Host dialog box has
a machine name as “localhost” on port 6011 and click OK. In the next dialog box, select
the Simulink model in your project folder as filename.mdl and click OK. Link the
LabVIEW controls to Simulink gain or constant values, and link LabVIEW indicators to
Simulink NI Sink block names. See Figure 7. For this example, click the LabVIEW
control for the knob on the left and expand the gain listing on the right. Click on “gain”
and then click “Add to connections”. Repeat this process for the indicator by connecting

5
MEEN 364
J. Mlcak
Last Update: 8/27/2006
the Waveform chart to the NI_SINK. Note that the icons beside the controls and
indicators differ. Icons of different types cannot be linked. Once all connections have
been made, select OK to create all connections and return to the Front panel. Pressing
OK causes LabVIEW to build a block diagram that makes all necessary connections
between the Front panel elements and the Simulink model DLL.

Figure 6. LabVIEW Front panel

Figure 7. SIT Connection Manager

6
MEEN 364
J. Mlcak
Last Update: 8/27/2006

For this example, it is desired to log (save) the data in an array, or spreadsheet
form. This array can be opened in any spreadsheet program or MATLAB as long as it is
saved as a comma-delimited or tab-delimited file. Double click the Waveform Chart
indicator on the Front panel. This takes you to the Waveform Chart’s location in the
Block diagram. Right click on the Block diagram and select Functions >> File I/O >>
Write LVM and drag the Write LVM block to a location under the Waveform Chart
terminal in the Block diagram. A dialog box as shown in Figure 8 will appear. Select a
location and file name where the data will be logged and save it as a “.txt” file. Select
all options so that they match those shown in Figure 8. Click OK to close the Configure
Write LVM File dialog box. A Write LVM block must be added for each data set you
wish to log.

*NOTE: The filename in the Write LVM block must be changed each time you want
to save another data set.

Figure 8. Configure Write LabVIEW Measurement (LVM) File dialog box

As shown in Figure 9, use the wiring tool to connect the Signals port of the Write
LVM block to the wire that enters the indicator’s terminal block. Repeat the process
above for all data values that must be logged. Save the LabVIEW VI in the project
folder. Use a unique file name such as RUN_ME.vi to separate this VI from the many
others that were generated in the build process.

7
MEEN 364
J. Mlcak
Last Update: 8/27/2006

Figure 9. LabVIEW Block diagram showing location of Write LVM block.

Next, the analog input and output ports must be selected. Select File >> Open.
Open the file entitled filename_daq_driver.vi that is located in the filename_nidll_rtw
folder. The number of input and output ports should exactly equal the number of input
and output blocks in Simulink, respectively. For this example, the analog input channel
is Analog Input Channel 0 of the terminal block and the analog input channel is Analog
Output Channel 0 of the terminal block. This means that a 0 should be entered in the AI
channels box, and a 0 should be entered in the AO channels box. Figure 10 shows what
the VI should look like. Save the filename_daq_driver.vi and close it.

8
MEEN 364
J. Mlcak
Last Update: 8/27/2006

Figure 10. filename_daq_driver Front panel.

Turn on the PXI box and allow it to boot. Next, run the VI that contains the user
interface (in this example, RUN_ME.vi). The dialog box as shown in Figure 11 will
appear. For Machine Name/IP, enter the IP address corresponding to your PXI box. The
IP address may or may not be the same as shown in Figure 11. The port should be 6011.
Check the “RT Target?” box. In the Path to Model VI box, click the open button and
select filename_main_daq.vi in the filename_nidll_rtw folder. In the Path to Model DLL
box, click the open button and select the filename.dll file in the filename_nidll_rtw folder.
Click OK in the Select Host dialog box to run the VI.

Figure 11. Select Host dialog box

9
MEEN 364
J. Mlcak
Last Update: 8/27/2006
The Front panel of the main VI is shown in Figure 12. Select the “Run SIT”
button on the Front panel to run the SIT/Simulink diagram in LabVIEW, and select the
“Stop SIT” button on the Front panel to stop the SIT/Simulink diagram in LabVIEW.

*NOTE: Do not use the stop button labeled “Stop VI” or the stop button on the top
toolbar. These stop buttons do not properly close the SIT/Simulink connection with
LabVIEW.

Run SIT Stop SIT

Figure 12. LabVIEW Front panel after SIT connections are established

To convert the logged data to a variable in the MATLAB workspace, make sure
the “.txt” file is in the MATLAB working directory. At the MATLAB command prompt
or a M-file type:

load Trial_1.txt

where Trial_1.txt is the filename given in the Write LVM block. This will create an array
labeled Trial_1 in the MATLAB workspace. The first column of the array is time, and
the second column is the data set that was logged while running SIT.

The data can be opened in Excel by first opening Excel, then going to
File>>Open. Under the “Files of Type” drop-down menu, select Text Files. Then select
the “.txt” file containing the data you want. Click the next button twice, and then click
the finish button. The data can then be manipulated using Excel. Like MATLAB, the
first column is time, and the second column is the data set that was logged while running
SIT.

10
MEEN 364
J. Mlcak
Last Update: 8/27/2006
3. Summary

1. Create a folder for your lab projects.


2. Open MATLAB and change the working directory to your lab project folder.
3. Create Simulink Diagram:
a. Label all blocks with a unique and descriptive name.
b. Include an In block for each analog input data acquisition terminal.
c. Include an Out block for each analog output operation.
d. Insert an NI Sink where data values will be viewed in LabVIEW.
e. Give constants and gains nominal values if appropriate.
f. Include at least one “In” and “Out” block even if the blocks are not used.
If the In or Out block is not used, do not connect it to anything.
4. Set the sampling frequency, fixed calculation step size, and real-time workshop:
a. Go to Simulation>>Simulation Parameters>>Solver tab:
i. Set a Stop time (can leave at default of 10 seconds).
ii. Under Solver Options, select the type as Fixed-step with the ode5
(Dormand-Prince) solver.
iii. Under Fixed step size, type in the sampling time in seconds. For
example, if sampling at 100 Hz, type 0.01 seconds.
b. Select the Workspace I/O tab:
i. Uncheck all boxes in the Save to workspace area.
ii. Uncheck “Limit data points to last” dialog box in the Save options
area.
c. Select the Real-Time Workshop tab:
i. Select browse.
ii. Select nidll.tlc (LabVIEW DLL Target) >> OK.
d. Click Apply and OK to close the Simulation Parameters dialog box.
e. From the drop-down menu on the toolbar, select External.
f. Save the Simulink model. Recall that no spaces can appear in a Simulink
file name. The first character in the filename must not be a number.
g. Ask your lab Teaching Assistant to review your Simulink model before
continuing.
5. Close all LabVIEW programs that may be open, and build the Simulink model by
selecting Tools >> Real-Time Workshop >> Build Model, or press Ctrl+B.
6. Allow the Simulink model to build. LabVIEW will open and close in the process.
The build is complete when the MATLAB command prompt displays:
### Successful completion of Real-Time Workshop build
procedure for model: filename.
7. Do NOT close MATLAB.
8. Open LabVIEW and create a New (blank) VI.
9. Select the Front panel
a. Add a numerical or dial control for every data value you wish to change
(ex. gains and constants).
b. Add a numerical or graphical indicator for every data value you wish to
output from your simulink model. At least one indicator must be present
for each NI Sink used in the Simulink model.

11
MEEN 364
J. Mlcak
Last Update: 8/27/2006
c. Label each control and indicator with a unique and descriptive name.
10. Select Tools >> SIT Connection Manager:
a. Select Load Model and verify that the Select Host dialog box has a
machine name as “localhost” on port 6011. Click OK.
b. Select the Simulink model and click OK.
c. Link LabVIEW controls and indicators with Simulink blocks. Any block
that must be varied in Simulink must be paired with a control, and every
NI Sink in Simulink must be paired with a LabVIEW indicator.
i. Match a LabVIEW control with a Simulink gain or constant value
and click “Add to Connections”
ii. Match a LabVIEW indicator with NI Sinks from Simulink and
click “Add to Connections”
iii. Repeat for all desired connections between the LabVIEW and
Simulink models.
d. Click OK.
11. If acquired will not be saved (logged), skip this step. For each data point that will
be logged:
a. Double click the indicator on the front panel. This will take you to the
indicator’s location in the Block diagram.
b. Select Functions >> Output >> Write LVM and drag the Write LVM
block to a location under the indicator’s terminal block in the Block
diagram.
c. In the dialog box, select a file name where the data will be logged and
save it as a “.txt” file.
d. Select options as shown in Figure 8.
e. Click OK to close the dialog box.
f. Use the wiring tool to connect the Signals port of the Write LVM block to
the wire that enters the indicator’s terminal block.
g. Repeat steps a-f for all values that require logging.
12. Save the LabVIEW VI in the project folder. A unique file name such as
RUN_ME.vi will help separate this VI from the many others that were generated
in the build process.
13. In LabVIEW, go to File>>Open:
a. In your project folder, double-click the filename_nidll_rtw folder
b. Open the filename_daq_driver.vi
c. Under “AI Channels” type in the analog input channels that any cables are
connected to on the terminal block.
d. Under the “AO Channels” type in the analog output channels that any
cables are connected to on the terminal block.
e. Save the filename_daq_driver.vi and close it.
14. If the PXI box is not already on, turn it on and allow it to boot.
15. Run the VI created that has the user interface (RUN_ME.vi):
a. In the dialog box type the following:
i. Machine Name/IP: Type in the IP address listed on the PXI box
ii. Port: 6011
b. Check the “RT Target?” box

12
MEEN 364
J. Mlcak
Last Update: 8/27/2006
c. Under “Path to Model VI”, click the open folder icon and select
filename_main_daq.vi in the filename_nidll_rtw folder.
d. Under “Path to Model DLL”, click the open folder icon and select
filename.dll.
e. Click OK in the “Select Host” dialog box to run the VI.
f. Select the “Run SIT” button on the Front panel to run the SIT/Simulink
diagram in LabVIEW.
g. Select the “Stop SIT” button on the Front panel to stop the SIT/Simulink
diagram in LabVIEW.
h. Do NOT use the stop button that says “Stop VI” or the stop button on the
top toolbar because these do not properly close the SIT/Simulink
connection with LabVIEW.

For additional information, refer to:

Simulation Interface Toolkit User Guide, National Instruments, 2002-2003.


Available at http://www.ni.com

13

Вам также может понравиться