You are on page 1of 23

EE-361 Feedback Control Systems

Introduction to Simulink and Data Acquisition


Experiment # 2

Date: / / 2020 Section:

Lab Report 2 Requirements


These are the questions/exercises from the handout that have to be answered
in the lab report:

- [1.1] Question: Why did the error occur in the transfer function of
Example 3?
- [1.1.1] Exercise-1: Provide the result of the scope from the block dia-
gram shown in Figure-3.
- [1.2] Exercise-2: Provide the scope plot from Exercise-2, using the xla-
bel, ylabel, title, axis, and legend commands.
- [1.3.2] Exercise-3: Show the resulting waveform in oscilloscope to in-
structor. Take a snapshot as well for your report.
- [1.3.4] Exercise-4: Show the resulting LED to the instructor. Save the
plot from scope.
- [1.3.5] Exercise-5: Give the plot of the Simulink scope.
- [1.3.5] Exercise-6: Show the ouput with each sample rate mentioned in
exercise.
- [1.3.6] Exercise-7: Run the simulation using different sampling times
and see the effect on the Simulink scope. Also, save all these response for
your report.
- [1.3.7] Exercise-8: Show the resulting waveform in oscilloscope to in-
structor. Take a snapshot as well for your report.

1
Instructions
Follow the instructions stated below:
1. All the exercises in this lab handout are to be evaluated by instructors.
After getting a result, ask the instructor to evaluate them.
2. Please save all your graphs by taking screen shots in a word file with the
exercise numbers mentioned.

3. After the lab, you (all members of a group) have to upload the above
mentioned word file into the drop box on LMS with the name of that
file as LabX-Y-N.pdf (X is the experiment number, Y is the day e.g.
Tuesday(Morning), Tuesday(Evening), Thursday and N is the name of
the student).

1 Introduction
In this lab session an introduction to Simulink is given to the students, in which
they learn how to use Simulink, how to program in Simulink, and how to acquire
data using the Simulink Support Package for Arduino Hardware.

1.1 Simulink
Simulink is a software package included in MATLAB for modeling, simulating,
and analyzing dynamical systems. It supports linear and nonlinear systems,
modeled in continuous time, sampled time, or a hybrid of the two. For mod-
eling, Simulink provides a graphical user interface (GUI) for building models
as block diagrams, using click-and-drag mouse operations. Simulink includes a
comprehensive block library of sinks, sources, linear and nonlinear components,
and connectors. You can also customize and create your own blocks.

To open Simulink, there are two options:

1. Click on the simulink button in the menu bar.

2. Type ”simulink” in the command window.

The following window will appear as shown in Figure-1.


To create a new model, go to:

File → New → Model

Search for the desired block in the library browser, and then drag that block
and place it in the model file. Any block can be resized easily by dragging its
corners. To place the model of any physical system, drag the Transfer Function

2
Figure 1: Starting Window of Simulink

3
Figure 2: Window to Enter Transfer Function

Block from the following path.

Simulink → Continuous → Transfer Fcn

To enter the coefficients of the Transfer Function, double click on the Trans-
fer Function block. A window will appear where you can enter these coefficients
as shown in Figure-2.

Following are some examples to understand how to use the Transfer Func-
tion block:

Example-1

s2 + 3s + 1
G(s) = (1)
s2 + 8s + 10
Numerator Coefficient = [1 3 1]
Denominator Coefficient = [1 8 10]

4
Figure 3: Basic Block Diagram

Example-2
s + 10
G(s) = (2)
s3 + 3s + 1
Numerator Coefficient = [1 10]
Denominator Coefficient = [1 0 3 1]

Example-3
s2 + 10s + 10
G(s) = (3)
s
Numerator Coefficient = [1 10 10]
Denominator Coefficient = [1 0]
On entering this, an error box will appear.

Question: Why does this error occur?

1.1.1 Building a Block Diagram in Simulink


It is very easy to build a block diagram in the Simulink. First open a new
model file. Place the required blocks in the blank model file and connect them
as required. Build the block diagram as shown in Figure-3 and run it.
Note: To set up the step function, double click on its block and set the values
according to those found in Figure-4. Also double click on the scope function
block and check the ”save to workspace” option as illustrated in Figure-5. Once
the simulation runs, the scope data can be saved by running the following com-
mand from the command line in the MATLAB editor:
 simplot(ScopeData)

5
Figure 4: Step Function Window

Figure 5: Scope Function Window

6
Figure 6: Transferring Data from Simulink to Workspace

Exercise-1: Copy the result of the block diagram shown in Figure-3 and
save it to your word file.

The start and stop buttons for Simulink are shown in Figure-3. The simu-
lation time can be changed through the text box shown in Figure-3.

The list of the Simulink blocks used most often is given at the end of this
lab manual. This list will help you in future labs as well, so make sure to go
through it.

1.2 Data Transfer from Simulink to Workspace


We usually do not have much control over data processing. By transferring the
data from Simulink to workspace, however, we are able to do many desirable
manipulations (processing) over it and its formatting is very easy. To transfer
the data from the Simulink scope to workspace, the Simulink library browser
provides a block named ”To Workspace” as shown in Figure-6. You can find
the block, ”To Workspace” from the following path:

Simulink → Sinks → To Workspace

Set the properties of the block, ”To Workspace” as shown in Figure-7. Af-
ter setting the properties, rerun the simulation. This will generate the ”Data”
and ”tout” variables in the workspace as shown in Figure-8. The ”Data” object
will contain information for the input and output signals.

To plot these variables, we have to separate the information from this object
into two different variables. Run the following commands from the command

7
Figure 7: Properties of ’To Workspace’ Block

Figure 8: Display of Variables in the Workspace

8
Figure 9: System’s Response from Command Line

line to separate the variables:

 output = Data(:,2);
 input = Data(:,1);

Now we can plot the input and output signals in separate windows as well as in
the same window. Run the following command to plot the input and output in
the same window with different colors and line widths:

 plot(tout,input,’r’,’lineWidth’,2);
 hold
 plot(tout,output,’b-.’,’lineWidth’,2);
 legend(’Input’,’Output’)
 axis([0 10 0 5.1])

This will display the output as shown in Figure-9. Save this to your word file.

Exercise-2: Explore the following commands, use them in the last response,
and save the results.
xlabel
ylabel
title
axis
legend

9
Figure 10: Getting started with Arduino Hardware using Simulink

1.3 Data Acquisition in MATLAB


There are multiple options to acquire data in MATLAB from external sensors.
One of them is to use the Data Acquisition Toolbox of MATLAB. Recently, low
cost microcontroller boards like Arduino have been equipped with toolboxes to
communicate with Simulink directly. One such package is Simulink Support
P ackage f or Arduino Hardware.

1.3.1 Getting Started with Arduino Mega 2560 Hardware


This example shows how to use Simulink Support Package for Arduino Hard-
ware to run a Simulink model on Arduino Mega 2560 board. Simulink Support
Package for Arduino Hardware enables you to create and run Simulink models
on Arduino Mega 2560 board. The target includes a library of Simulink blocks
for configuring and accessing Arduino sensors, actuators and communication
interfaces. Additionally, the target enables you to monitor and tune algorithms
running on Arduino Mega 2560 board from the same Simulink models from
which you developed the algorithms.

In this example you will learn how to create and run a simple Simulink model
on Arduino Mega 2560 board.

1.3.2 Required Hardware


To run this example you will need the following hardware: Arduino Mega 2560
board, USB cable, LED, 220 Ohm resistor and Breadboard.

10
Task 1: Connect an LED to an Arduino Output Pin
In this task, you will connect an LED to an Arduino output pin so you can see
changes in the logical state of the pin (See Figure-11).

1. Attach one end of the 220 Ohm resistor to output pin 9 on the Arduino
Mega 2560 board. Use the recommended breadboard and the breadboard
wires.

2. Attach the long leg (positive) of the LED to the resistor. Attach the short
leg (negative) to the ground pin on the Arduino Mega 2560 board.

Task 2: Review Arduino Block Library

Simulink Support Package for Arduino Hardware provides an easy way to cre-
ate algorithms that use Arduino sensors and actuators by using the blocks that
can be added to your Simulink model. The blocks are used to configure the
associated sensors and actuators, as well as to read and write data to them.

1. Enter simulink at the MATLAB prompt. This opens the Simulink Library
Browser.
2. In the Simulink Library Browser, navigate to Simulink Support Package
for Arduino Hardware → Common.
3. Double-click the Digital Output block. Review the block mask, which
contains a description of the block and parameters for configuring the
associated Arduino digital output pin.

Task 3: Create a Model for Arduino Mega 2560 Hardware

In this task, you will create a simple Simulink model that changes the state of
the Arduino digital output pin.

1. In MATLAB, select Home → New → Simulink Model.


2. Drag the Pulse Generator block from the Simulink Sources library to your
model.

3. Double-click the Pulse Generator block. Set the Pulse type to parameter
to Sample based and set the sample time parameter to 0.1 second.
4. Drag the Digital Output block to the model. Use the default block set-
tings.
5. Connect the Pulse Generator block to the Digital Output block.

11
Figure 11: Hardware Connections

12
Figure 12: Arduino Common Blocks

Task 4: Configure and Run the Model on Arduino Mega 2560 Hard-
ware

In this task, you will configure and run your model on the Arduino Mega 2560
board.

1. Connect the Arduino Mega 2560 board to your computer with a USB
cable.
2. In your Simulink model, click Tools → Run on Target Hardware → Prepare
To Run... .
3. When the Configuration Parameters page opens up, set the Target hard-
ware parameter to Arduino Mega 2560. Do not change any other settings.
4. Click OK.
5. In your Simulink model, click the Deploy To Hardware button on the
toolbar. The model will now be deployed to the Arduino Mega 2560
hardware.
6. Look at the LED attached to pin 9. The LED should blink one time every
second.
7. Save your model.

Exercise-3: Show the resulting waveform in oscilloscope to instructor. Take a


snapshot as well for your report.

13
Bonus Task

1. Create and run a model that turns the LED on if a signal is applied to a
digital input pin.
2. Create and run a model that repeatedly brightens and dims an LED.
Hint: use the PWM block.

1.3.3 Communicating with Arduino Mega 2560 Hardware


This example shows how to tune the parameters and monitor the signals of an
algorithm running on Arduino Mega 2560 board.

Simulink Support Package for Arduino Hardware enables you to monitor and
tune algorithms running on Arduino Mega 2560 board from the same Simulink
models from which you developed the algorithms.

In this example you will learn how to tune and monitor the algorithm in real
time as it is executing. When you are developing algorithms, it is often nec-
essary to determine appropriate values of critical algorithm parameters in an
iterative fashion. For example, a surveillance algorithm that measures motion
energy in a room may use a threshold to determine an intruder in the presence
of ambient noise. If the threshold value is set too low, the algorithm may er-
roneously interpret any movement as an intruder. If the threshold value is set
too high, the algorithm may not be able to detect any movement at all. In such
cases, the right threshold value may be obtained by trying different values until
the desired algorithm performance is reached. This iterative process is called
parameter tuning.

Simulink’s External mode feature enables you to accelerate the process of pa-
rameter tuning by letting you change certain parameter values while the model
is running on target hardware, without stopping the model. When you change
parameter values from within Simulink, the modified parameter values are com-
municated to the target hardware immediately. The effects of the parameters
tuning activity may be monitored by viewing algorithm signals on scopes or
displays in Simulink.

This example introduces the Simulink External mode feature by showing you
how to:

- Set up communication between Simulink and Arduino Mega 2560 board.

- Use a Simulink model to tune the parameters of an algorithm that is


running on Arduino Mega 2560 board.

14
- Use Simulink scopes to monitor the state of an algorithm running on
Arduino Mega 2560 board.

1.3.4 Required Hardware


To run this example you will need the following hardware: Arduino Mega 2560
board, USB cable, LED220 Ohm resistor, Breadboard.

Task 1: Connect an LED to the Arduino Output Pin

In this task, you will connect an LED to an Arduino digital output pin so you
can see changes in the logical state of the pin.

1. Attach one end of the 220 Ohm resistor to digital output pin 9 on the
Arduino Mega 2560 board. Use the recommended breadboard and the
breadboard wires.

2. Attach the long leg (positive) of the LED to the resistor. Attach the short
leg (negative) to the ground pin on the Arduino Mega 2560 board.

Task 2: Simulate the Model

1. To simulate the model, create a model as shown in Figure- 14. For Motion
Energy block, go to:
Simulink → Sources → Repeating Sequence Square
and set the properties as Figure-15:

15
Figure 13: Connect an LED to the Arduino Output Pin

Figure 14: Motion Energy block properties

16
Figure 15: Motion Energy block properties

17
For Vector of output values, download dataset data lab2 from LMS
and load it in workspace. This dataset will represent the output of motion
sensor for our case now.
2. Observe that the model plays the motion energy recorded in a room and
compares it with a threshold to detect intrusion.
3. In the model, change the Simulation mode on the toolbar to Normal. This
tells Simulink to run the model on the host computer. See Task 3 below
to run the model on the Arduino target hardware.
4. In the model, click the Run button in the Simulink toolbar.
5. Click the Scope block. Observe that the algorithm detects multiple intru-
sions.
6. Click Stop button in the Simulink model.
Task 3: Run the Model in External Mode
1. Connect a USB cable from your computer to the Arduino Mega 2560
board.
2. In the model, change the Simulation mode on the toolbar to External.
3. In the model, click the Run button on the toolbar.
Task 4: Communicate with the Model

At this point, your model is running on Arduino Mega 2560 board. As the
model runs on hardware, it communicates with Simulink model in External
mode using serial connection over USB.
1. Notice that the LED attached to pin 9 is glowing almost constantly. This
means that the selected threshold is too low and that the algorithm mis-
interprets even a minor motion energy change as an intrusion. You need
to find a more optimal value of the threshold.
2. Double-click the Threshold block in the model, increase its value, and click
OK or Apply. This changes the threshold value in the model running on
the board.
3. Check whether the glowing pattern of the LED has changed. The LED
should light up every 10 and 11.5 seconds in a correctly tuned algorithm.
4. If there is no change in LED light pattern, repeat the Steps 2 and 3 until
you find the right value of the threshold.
5. Click Stop button in the Simulink model.
Exercise-4: Show the resulting LED to the instructor. Save the plot from
scope.

18
Figure 16: Model for Analog Input

1.3.5 Analog Input


To check the analog input of the Arduino Mega 2560, set up a new simulink
model as shown in Figure-16. Configure the function generator with following
settings:

Sine Wave
5 Hz
5 V (peak to peak)
Sample Time = 0.001 sec
Simulation Time (in Simulink) = 5 sec

Note: Analog Output block gives the measurement as a 10-bit value that ranges
from 0 to 1023. Use proper gain for the model considering the maximum value
of the analog signal you are measuring. In our case, be sure not to use more
than 5V.

Note: Make sure that you are running the model in External Mode now as
shown in Figure- 17

Next, connect the output of the function generator to Analog Input Pin 4 of
Arduino. The channel numbers are properly defined on each Arduino, so please
connect the function generator with the it properly. Connect the ground of the
function generator to GND of the Arduino. Then connect the oscilloscope to
Analog Input Pin 4 of Arduino. Now run the simulation in External Mode.

Exercise-5: Give the plot of the Simulink scope.

Exercise-6: Repeat the same procedure with the following sampling rates
(Change the sampling rate by a double click on the Analog Input block).

Sampling Rate = 500


Sampling Rate = 100
Sampling Rate = 50

19
Figure 17: External Mode Simulation

1.3.6 Digital Input


To use the digital input block, open a new model file. Place the digital input
block from the Simulink Support P ackage f or Arduino Hardware and con-
nect the model as given in Figure-18. Set following properties:

Simulation Time (in Simulink) = 10 sec


Sample time = 0.01 sec

Now connect the function generator to pin 8 with the following settings:

Output Type = TTL


Frequency = 10 Hz

Note: Take care of Nyquist-Shannon sampling theorem while deciding for sam-
pling time.

Now run the simulation and check the response on the Simulink scope.

Exercise-7: Run the simulation using different sampling times and see the ef-
fect on the Simulink scope. Also, save all these response for your report.

20
Figure 18: Model for Digital Input

1.3.7 Analog Output


There is no block in Simulink Support P ackage f or Arduino Hardware for
Analog Output. But there are other options which can be used for this pur-
pose. One of them is to use PWM Block. The model for this task is shown in
Figure-19. The parameter values for the blocks are as:

Input: Sine Wave


Frequency: 100 rad/sec
Amplitude: 127
Bias: 127
Phase: 0
Sample Time = 0.001 sec
Simulation Time (in Simulink) = 10 sec

As this signal is being applied to PWM block, you need an RC low pass filter
at the output to convert it into smooth wave. Choose suitable values of R and
C and see the effect for different combinations.

Exercise-8: Show the resulting waveform in oscilloscope to instructor. Take a


snapshot as well for your report.

21
Figure 19: Model for Analog Output

2 Important Blocks
Constant
simulink → Sources (or Commonly Used Blocks) → Constant

Integrator
Simulink → Continuous (or Commonly used Blocks) → Integrator

Transfer Function
Simulink → Continuous → Transfer Fcn

State Space
Simulink → Continuous → State-Space

Saturation
Simulink → Discontinuities → Saturation

Zero-Order Hold
Simulink → Discrete → Zero-Order Hold

Subtract
Simulink → Math operations → Subtract

Sum
Simulink → Math operations → Sum

Subsystem
Simulink → Ports and Subsystems → Subsystem

Mux
Simulink → Signal Routing → Mux

Scope

22
Simulink → Sinks → Scope

To File
Simulink → Sinks → To File

To Workspace
Simulink → Sinks → To Workspace

Pulse Generator
Simulink → Sources → Pulse Generator

Ramp
Simulink → Sources → Ramp

Signal Generator
Simulink → Sources → Signal Generator

Step
Simulink → Sources → Step

MATLAB Function
Simulink → User-defined Functions → MATLAB Functions

PID
Simulink →Continuous→ PID Controller

Simulink Design Optimization


MATLAB command line → srolib

23

You might also like