Exp1 dSPACE
Exp1 dSPACE
Exp1 dSPACE
Basic Steps: In order to perform an experiment, the following basic steps must be
performed:
1. Model system in Simulink,
The system can be built in Simulink using any of the available libraries and may or may
not include blocks form the dSpace library. The dSpace library includes blocks that
provide graphical representation of physical connections to the dSpace board. Using the
blocks in this library allows a physical system to be controlled by dSpace.
2. Download system to dSpace board (DS 1104)
Once the model is built in Simulink, it must be downloaded to the dSpace board. This can
be accomplished by using the Real-Time Workshop in Matlab.
3. Build a Control Desk Layout for the system
Once the model has been downloaded to the dSpace board, a layout must be built in
Control Desk to control the system. This interface can be used to start and stop the
experiment, change
parameters of the system in real time, display values of parameters in real time, and log
data from the experiment.
4. Run Experiment
After successfully loading the model onto the board and building a layout, the experiment
can be run.
5. Export data to Matlab for analysis
Control Desk can log data from the experiment and save it as file that can be exported
directly to Matlab. This allows the data to be manipulated by any tool available in the
entire Matlab program.
Processor
Memory
Timer
A/D Converter
D A Converter
Slave DSP
2.1
8 channel, 16 bit,
10 V, 10 us (settling time)
TMS32OF240, 20 MHz
Introduction
One of the best features of the dSPACE package is the ease of building real-time
applications. The time between converting the design into digital instructions for the DSP
and effectively running the application depends only on how fast your computer can
compile the initial code.
Basically a real-time application can be created by means of two methods:
1. Using MATLAB/Simulink for building the model and automatically generate the
C code and download it into the DSP memory.
2. Hand-coding in C and compile the model into DSP code.
The fastest way of developing a real-time code is developing the model in Simulink and
preparing a real-time model from that. Basically once you have completed the Simulink
model which you want to run in real-time, the only command required is RTW Build
under Tools menu in SIMULINK.
Once the command is executed, dSPACE software creates the object (*.obj) file,
downloads it on DS1104 board and automatically starts the hardware execution.
However, there are some important settings you have to make before transporting your
model into the real-time world. Lets start with a simple example.
8. Choose from the Simulink Continuous library the Transfer Function block and
drag it into a new simulation model.
The second order system, with the parameters specified, can be described in transfer
function with the following relation:
n2
G (s) 2
s 2n s n2
(2.1)
G ( s)
3947.84
s 87.962 s 3947.84
2
(2.2)
1. Set the parameters of the Transfer Fcn block as shown in Fig. 2.1
2. Next, drag a Signal Generator block from Sources library, a Slider Gain from
Math library and a Scope from Sinks library. Connect all blocks as in Fig. 2.2.
3. Now, set the simulation parameters by pressing Ctrl-E as:
Run the simulation by pushing the triangular play button and adjust the display of
the Scope with the autoscale option. You will obtain a plot similar to the one shown in
Fig 2.3.
Figure 2.3: Simulation Results of 2nd Order System for Square Wave Input
This simulation was really fast. The generator waveform can be changed by doubleclicking the signal generator block. Now, since we have the idea how the system work,
we will implement the system in real-time and observe the results.
connector. Thus, two Gain blocks, from the Math Library, will be required to correctly
read and write the values from and to the analog channels.
Drag the DS1104ADC C5 and DS1104DAC C1 blocks into the Simulink model
and replace the Signal generator and Scope blocks. Place the two gains of 10 and 0.1 on
the input and output signals respectively. The model should become similar to the one
depicted in Fig. 2.4. You might need to save the model with another name, to preserve the
simulation model.
Set the Stop-Time as inf . The simulation parameters should look like shown in
Fig. 2.5
In Simulation Parameters, go to Advanced and make Block Reduction OFF.
Next, choose the Real-Time Workshop, Build Model from the Tools menu.
Once the above command is given, you will observe a list of messages displayed in
Matlab Command window. These messages correspond to the different steps that the RTI
Software perform in order to transform the Simulink code of the example1.mdl file into
DSP code.
First there is a compilation stage, in which the Simulink file is transformed into a C file,
then comes the link stage where all the variables and subroutines are correlated with the
DSP environment, and finally the code is transformed into an object file and downloaded
into the DSP memory. The MATLAB window screen will look like shown in Fig. 2.6.
You can see that the file was successfully built. The result is example1.obj which was
already loaded in the DSP memory and its execution started.
Please note that the directory in which the model was built is the same you choose for
creating the Simulink model. If you look now in this directory you will find several files,
generated during the build command. Due to the large number of files generated, it is
advisable that each project to be located in a separate sub-directory.
Now, our simulation is running in the DSP board, in a digital, real-time format. We can
predict that this is much faster than what we saw on the Scope screen, in Simulink. More
important, now, seems to be the interaction with the system. We need to visualize, modify
and analyze the variables. For this, dSPACE comes with its own Graphical User
Interface, called CONTROL DESK. So, lets learn how to use the Control Desk to
interact with the real-time simulation.
Start ControlDesk and select only the toolbars checked as shown in Fig. 2.7. The
Tool Window is displayed at the bottom of Control Desk screen as shown in Fig.
2.7. The Tabs display the tool currently used. In the figure only three of the
working tools are available: Log Viewer, Interpreter and File Selector.
As we shall further describe, there is one tool, very important, to which we shall give a
special attention. This tool is called Variable Browser and the Parameter Editor. It
provides access to the variables of an application. These variables are stored in a file
called example1.sdf.
Next, load the file containing the variables of the simulation. Click File/Open
Variable
File and select example1.sdf.
The Variable Manager Tab appears at the bottom of the screen (see Fig. 2.9). The
window contains the structure of the simulation model. At the highest level we see the
simulation control variables. Their function is described in Table1.
Variable Type
labeled signals
block outputs
inputs of signal sinks
block parameters
The name following the prefix is the block name, except for labeled signals where the
label itself is used. Here are all the variables corresponding to the Model Root for our
example are detailed in Table. 2.2.
The controls displayed in the Virtual Instruments toolbar let us handle only the variables
that can be modified on-line, i.e. the P:-type variables. Our example has 5 such variables.
Four of them were listed in Table. 2.2 and there is one more under the Slider Gain
hierarchical level, which controls the amplitude of the Gain block.
1. Select the SLIDER button from the right toolbar.
2. The cursor changes into a square target. Click and hold the mouse while dragging
a rectangular shape in the Layout1 window (see Fig. 2.11).
Figure 2.11: Selecting Slider Bar from Visual Instruments and drawing
When the mouse is released, you will obtain a slider control, which let you continuously
change any P:variable between the limits selected.
As long as we decided to use amplifications between 0 and 5, double-click the Slider
control, select the Slider Tab, and set the Range Min and Range Max as shown in Fig.
2.12.
Now the limits where the cursor can be adjusted are 0 to 5. But the slider is still
bordered with a red line. This means that it hasnt been assigned a variable to control yet.
In the Variable Manager window, at the bottom of the screen, select the Slider Gain.
Click the P:Slider Gain.Gain variable and drag it to the rectangle drawn in the Layout
window.
The new Slider control will display the handled variable and will no longer be bordered
with a red line as shown in Fig. 2.13.
Now, we can add some visualization equipment. In our example, only two signals are
worth monitoring: the system input, coming from the real signal generator connected to
ADC#1 and the output of the second-order system. Both should be displayed on the same
scope. The output signal will also be monitored with the Lab Oscilloscope connected to
the DAC#1 channel, on the dSPACE Interface Box. Remember that the real values
Figure 2.13: Slider Control with Variable handled and its control limits
of the input signals will be obtained after the In-Gain block, while the real values of the
output signals will be obtained before the Out-Gain block.
Click on the Data Acquisition tab, in the Instrument toolbar at the right.
Select the Plotter icon and draw a larger rectangle in the Layout window.
7. One more step, before you actually can start the simulation: setting the
visualization parameters. The process parameters can be set in the Capture
Settings Window under View/Controlbars menu.
8. Open the Capture Settings Window and set the Length of the simulation as 2,
while leaving unchanged the Downsampling number. Your capture settings should
look like as shown in Fig. 2.16. For more complex systems this number has to be
increased when the Length is larger than 20 times the sampling time.
Now, we are ready to start the simulation.
be initiated. When stopping an experiment, the operations have to be done in the reverse
order.
To start and stop the DSP you can use the icons in the Platform Management toolbar (see
Fig. 2.17.
Edit mode toolbar contains edit, test or animation icons. Refer Fig. 2.17 for edit, test and
animation tool icons.
Edit mode is used to edit or modify your layout, start and stop your real-time simulation.
Once you start the real-time simulation, click the animation icon, this will let you modify
parameters in real-time and observe the waveform. Unless real-time simulation starts,
you cannot go into this mode. Clicking on Test icon lets you go into the Test mode. In this
mode, you can observe the snap-shot of your real-time simulation.
Click File/Add All Opened Files. This implies that the experiment will
remember the *.sdf file, containing the variables to handle, will know the
path for all other files and will open the connections between the layout
and the variables.
Important note: Remember that the object file compiled has to be in the DSP memory. It
is not implicitly loaded with the experiment !
Lab Report
The lab report should contain a brief, along with the details what have been done in the
lab and details asked below.
1.
2.
3.
4.