Experiment 3
Building a Real Time System with ControlDesk
Objective: Getting familiar with dSPACE ControlDesk and Matlab/Simulink by building a real-time
system model.
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 the computer can compile the initial code.
Basically a real-time application can be created by means of two methods:
Using MATLAB/Simulink for building the model and automatically generate the C code and
download it into the DSP memory.
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 realtime model from that. Basically once the Simulink model had been completed and wanted to be 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. Then the object file can be
downloaded on DS1104 board and automatically starts the hardware execution.
However, there are some important settings required prior “transporting” the model into the real-time
world. The examples perform in this experiment will guide students on building a real time system with
ControlDesk.
Experiment:
A. Creating a model in Simulink
The first example introduces the analog channels input-output communication with external devices. For
this example a Signal Generator is used to generate different waveforms as inputs to signal processing
algorithm. The result of this process will be directed to an analog output channel, in order to be monitored
with the lab oscilloscope.
Suppose the response of a second-order system at different types of input signals, with variable
amplitudes, need to be analyized. The second-order system is defined by the following parameters:
𝜔𝑛2
𝑠 2 + 2𝜔𝑛 𝜉𝑠 + 𝜔𝑛2
where damping, 𝜉 = 0.7, natural frequency, 𝜔𝑛 = 2𝜋𝑓 (rad/sec), and 𝑓 = 32 Hz.
Follow the steps below to build the Simulink model.
NOTE: If prompted when opening Matlab, select RTI1104 as the Real-Time Interface.
Procedure:
1. Create a folder called “Expt01”.
2. Start MATLAB and set the Path Browser to the working folder (Expt01).
Linear Controls and Drives Laboratory
Rev. 8/29/19
Page 1 of 6
3.
4.
5.
6.
7.
8.
NOTE: If prompted, select RTI1104 as the Real-Time Interface.
Type Simulink at the prompt line and create a new model from File menu.
Choose from the Simulink Continuous library the Transfer Fcn block and drag it into a
new simulation model.
Set the parameters of the Transfer Fcn block.
Drag a Signal Generator block from Sources library, a Slider Gain from Math library and
a Scope from Sinks library and connect all blocks.
Now, set the simulation parameters by pressing Ctrl-E as:
Set the solver options to fixed step
Specify a fixed step size. Let it be “0.001” i.e 1ms.
Stop-time as “2” seconds.
Set the Signal Generator output to Square-wave and frequency as “2 Hz”.
Use a Mux block to compare the input and output on the same scope. The completed system can
seen in Fig. 1.
Run the simulation by pushing the triangular play button and adjust the display of the Scope
with the auto-scale option.
Fig. 1. Simulation model of a second order system.
B.
Building the Real Time Simulation Model
The model developed for simulation is now to be connected to the external devices (Signal generator and
Oscilloscope). Since these devices are physically generating/accepting signals going to or coming from
the DSP board, users need to stream these signals via the analog Input/Output channels, located on the CP
1104 I/O board as shown in Fig. 2.
Make sure that the Signal Generator and the Oscilloscope are connected via shielded BNC cables to
ADCH5 and DACHl respectively. The Signal Generator output is set such that its signal amplitude is
approximately 1V at a frequency of 10 Hz.
Important: Before connecting the Signal Generator to ADCH5, Check to make sure the amplification or
scaling factor on the oscilloscope is 1X. Now connect it to the oscilloscope and make sure it is a square
wave of amplitude 1 V (peak-to-peak is 2 V) and capture the waveform from the scope for the report.
Scaling of Input and Output Signals - Information Only
Communication with the input/output channels is performed via two dSPACE blocks found in the
dSPACE RTI1104 library under the sub-library DS1104 MASTER PPC, named DS1104ADC_C5 and
DS1104DAC_C1. They will replace the Signal Generator block and Scope block respectively. The
analog input channel is down-scaled by the hardware with a ratio of 1:10. This means that 10V at the
input will be read as 1 V in the model. The analog output channel is also down scaled in the hardware
with the same ratio. Thus, a 1 V signal generated within the model will have an amplitude of 10V at the
connector. Thus, two Gain blocks, will be required to correctly read and write the values from and to the
analog channels.
Linear Controls and Drives Laboratory
Rev. 8/29/19
Page 2 of 6
Communication Link
PC
CP 1104 I/O Board
ADCH5
DACH1
Signal
Generator
Oscilloscope
ControlDesk
MATLAB /
SIMULINK
DS1104 R&D Controller Card
Fig. 2. CP 1104 I/O board diagram.
Procedure:
1.
2.
3.
4.
Drag the DS1104ADC_C5 and DS1104DAC_C1 blocks into the Simulink model and replace the
Signal generator and Scope blocks and build the system as shown in Fig. 3. Place the two gains of 10
and 0.1 on the input and output signals respectively. Save the model with a new name, to preserve
the simulation model. Label all the gains as shown in Fig. 3.
Remember that the simulation was performed for only 2 seconds. In real-time, however, the system
needs to run continuously. Therefore, press CTRL-E for simulation parameters.
Set the Stop-Time as ‘inf’.
In Simulation, go to Model Configuration Parameters and select All
Parameters tab. Search for Block Reduction and then uncheck it.
Return to Simulink, go to Code, then C/C++ Code and then Build Model.
Fig. 3. Equivalent real-time model of simulation system.
Once the Build command is given, a list of messages will be displayed in Matlab Command window.
These messages correspond to the different steps that the RTI Software perform in order to transform the
Simulink mdl file into DSP code. When the code generation successful, the Matlab will prompt:
“Successful Completion of Real-Time Workshop build procedure for model:
xxxx.mdl”.
Information Only
First there is a compilation stage, in which the Simulink file is transformed into a C file, and 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 file that was
Linear Controls and Drives Laboratory
Rev. 8/29/19
Page 3 of 6
successfully built can be seen from the MATLAB window screen. The “obj” file will be automatically
loaded in the DSP memory and its execution started.
Note that the directory in which the model was built is the same directory for creating the Simulink
model. In this directory, there are 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. With
the simulation running in the DSP board, in a digital, real-time format, users need to visualize, modify,
and analyze the variables. For this, dSPACE comes with its own Graphical User Interface, called
ControlDesk.
C. Creating a new experiment file with Control Desk
ControlDesk is software that allows users to look at the variables, display their behavior and modify the
simulation parameters by interacting directly with the DSP board.
Procedure:
1.
2.
3.
4.
5.
6.
7.
Go to Start then dSPACEControlDesk4.1 folder and click on dSPACEControlDesk4.1
Go to View then View Sets, and then select Standard. (It might already have the standard
view).
Important Information: Users will mainly be dealing with the “Project” tab and the
“Variable” tab. The Variable Browser and the Parameter Editor provides access to
the variables of an application. These variables are stored in the model file with extension .sdf.
Start a new experiment. Click File then New and then Project + Experiment. In the pop-up
window write the name of the project, and very important, set the path where the simulation files are
stored.
Note: When creating a new experiment, don’t forget to set correct working directory.
Click Next, and type the Experiment Name (example Experiment_01).
Click Next and make sure the “ds1104” platform is selected.
Click Next, then load the file containing the variables of the simulation. Click the Import… button,
select the “sdf” file and click open.
Click Finish.
Important Information:
The Variable Manager Tab appears at the bottom of the screen. The window contains the structure
of the simulation model. At the highest level, users can see the simulation control variables.
For the purpose of our simulation, the variables of interest are contained under the Model Root group.
This group contains the variables belonging to the top-level of the Simulink model. Variables from
subsystems go into further groups in deeper hierarchical levels. The variables available have a prefix to
distinguish the different variable types. The name following the prefix is the block name, except for
labeled signals where the label itself is used.
Also notice that the experiment that users created is in a tree branch under the created project in the
Project window on the left of the screen. Users can add new experiments at any time by RightClicking the Project and Clicking New Experiment… in the context menu that pops up.
D. Display Controls and Scopes with Instrumentation Management Tools
Linear Controls and Drives Laboratory
Rev. 8/29/19
Page 4 of 6
In order to see the behavior of each variable and modify the parameters in real-time, start a new layout
screen to add instruments such as buttons, knobs, slider-gains, plotter.
Procedure:
1.
2.
3.
4.
5.
6.
7.
Open an existing layout or create a new one from the Project window.
To open an existing layout, double click in the Project Window under {Experiment
Name}/Experiment Layouts.
Users can also insert a new Layout by clicking Insert/Layout on the menu bar.
The layout window opens in the ControlDesk main work area (center of the ControlDesk
window). The Instrument Selector is hidden on the right of the ControlDesk screen for
easy access.
The controls displayed in the Instrument Selector handle only the variables that can be
modified online, i.e. the P:-type variables.
Select the plotter icon from the Instrument Selector and draw a larger rectangle in the
layout window.
To format the scope right click Y-axis and go to Y-Axis/Properties. The Y-Axes would be
grouped under “Axis 0”. When the signals are displayed on the same y-axis, the graph settings will
be identical for both.
At the X-label, users can write “Time” and at Y-label write “Input and output signals”.
Users can delete any signal by selecting it and press Delete key. Users can even delete or modify
signals or changing the color of the signals, by right clicking Y-axis and going in to the YAxis/Properties.
All gains variables (called P:variable) can be monitored on the layout using numerical input or
sliders. They can be changed if needed. Monitor the InputGain and OutputGain on the layout.
Make sure their values are all set to 1 as in the model.
Drag the “out” signals for the InputGain and OutputGain onto the plotter. When dragging the
second signal, make sure that the mouse button is release above the first one, on the vertical axis.
Otherwise, a new vertical axis will be drawn, and users will have less available space to visualize the
waveforms. Now both signals are assigned to the plotter and will be displayed with different colors.
The label on the vertical axis will show only the last signal dragged to the scope.
Running the Experiment
There are two operations that have to be done to run/stop the experiment. First, the DSP execution has to
be started. Second, the animation and data acquisition/printing needs to be initiated. When stopping an
experiment, the operations have to be done in the reverse order.
Procedure:
1.
2.
3.
To start and stop the DSP users can use the icons in the Measurement Data Management
toolbar.
Start the DSP by pressing the blue triangle button. If the obj file is loaded in the DSP
memory then it will start running. The Stop, blue rectangle shaped button with an M will become
active. This button can be used to stop the measurement. Also note that the state changes to online in
the status bar.
Users will see both the signal generator and the system output signals on the plotter. Is the output
signal following the input signal as in the simulation? Take a snapshot of the ControlDesk results
plot for the report. Compare it to the simulation … any similarities or differences?
Linear Controls and Drives Laboratory
Rev. 8/29/19
Page 5 of 6
4.
5.
Obtained on the oscilloscope, the output signal from DAC channel 1. How does this signal compares
to the signal from the signal generator captured earlier? Any reason?
Change the input from a square-wave to a triangular wave at the same frequency from the signal
generator. Modify the Simulink model by adding another DAC channel to output both the original
signal from the signal generator and the output of the system from the dSPACE. Display both input
and output signal on the oscilloscope superimposed on each other and capture the waveform. How
does the output follow the input. How does it compare to the simulation with a triangular wave as the
input?
Saving and Loading:
1.
2.
Click Save Project + Experiment to save the entire project together with all loaded files
including the variable set for the downloaded application.
For loading an experiment, simply click File/Open/Project+Experiment and, if saved as
previously instructed, the layout and the variables will appear on the screen.
Lab. Report:
Simulation results of the 2nd order system obtained in section A.
ControlDesk results of input reference and output on the same plot.
All plots captured on the oscilloscope.
Compare the simulation and real-time results and comment on that.
NOTE: Please save Simulink/Matlab files in version 2016a if you have a higher version.
Linear Controls and Drives Laboratory
Rev. 8/29/19
Page 6 of 6
Purchase answer to see full
attachment