Digi Usecases Opc Doc v20 en

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Digitalization with TIA Portal:

Virtual Commissioning with


SIMATIC and Simulink

Use case 2:
Connecting Simulink models
to SIMATIC PLCSIM Advanced
via OPC UA

SIMATIC S7-PLCSIM Advanced


Siemens
Simulink Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/109749187 Support
Legal information

Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG ("Siemens"). They are
non-binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the
respective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application
examples is your responsibility. Sharing the application examples with third parties or copying the
application examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not
result in property damage or injury to persons.

Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without
limitation, liability for the usability, availability, completeness and freedom from defects of the
application examples as well as for related information, configuration and performance data and
any damage caused thereby. This shall not apply in cases of mandatory liability, for example
under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of
life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent
non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for
damages arising from a breach of material contractual obligations shall however be limited to the
© Siemens 2020 All rights reserved

foreseeable damage typical of the type of agreement, unless liability arises from intent or gross
negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions
do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens
against existing or future claims of third parties in this connection except where Siemens is
mandatorily liable.
By using the application examples you acknowledge that Siemens cannot be held liable for any
damage beyond the liability provisions described.

Other information
Siemens reserves the right to make changes to the application examples at any time without
notice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have
precedence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.

Security information
Siemens provides products and solutions with Industrial Security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an
enterprise network or the Internet if and to the extent such a connection is necessary and only
when appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer
supported, and failure to apply the latest updates may increase customer’s exposure to cyber
threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 2
Table of contents

Table of contents
Legal information ........................................................................................................ 2
1 Introduction ....................................................................................................... 4
1.1 Overview .............................................................................................. 4
1.2 Principle of operation ........................................................................... 5
1.3 Components used ................................................................................ 6
2 Engineering ....................................................................................................... 7
2.1 Hardware configuration in TIA Portal ................................................... 7
2.2 Code generation with SIMATIC Target 1500S ................................... 12
2.2.1 Settings for code generation .............................................................. 12
2.2.2 Coding with SIMATIC Target 1500S .................................................. 18
2.2.3 Description of the generated function blocks ..................................... 22
2.3 Simulation model in Simulink ............................................................. 22
2.3.1 MATLAB function ............................................................................... 22
2.3.2 Inserting MATLAB function ................................................................ 24
2.4 Create Step 7 program ...................................................................... 25
2.5 Commissioning the example application ............................................ 31
2.5.1 Download project ............................................................................... 32
2.5.2 Start Simulink simulation.................................................................... 33
3 Operation ......................................................................................................... 34
3.1 Operating the PID controller .............................................................. 34
© Siemens AG 2020 All rights reserved

3.2 Monitor control result ......................................................................... 35


3.3 Using the user website....................................................................... 36
4 Alternative solution......................................................................................... 38
5 MATLAB function code .................................................................................. 39
6 Appendix.......................................................................................................... 40
6.1 Service and support ........................................................................... 40
6.2 Industry Mall ...................................................................................... 41
6.3 Links and literature............................................................................. 41
6.4 Change documentation ...................................................................... 41

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 3
1 Introduction

1 Introduction
1.1 Overview
The Simulink software from MathWorks is frequently used in automation and
controls engineering to simulate processes and create algorithms. The requirement
is to simulate the model, the algorithm or the function in a few steps either in a
virtual environment with PLCSIM Advanced or, using hardware, via a software
controller.
This application example shows you how to create a MATLAB function for
communicating with a software controller via OPC UA. Integrating the function into
the Simulink model makes it possible to validate and virtually commission a PID
control system that runs on a virtual controller, in the context of a simulated
process in Simulink.
This document describes the necessary steps for configuring, commissioning and
operating the application example.
The complete application example consists of the following documents:
• Main document: Overview of the two use cases and the Simulink model.
• Use case 1: Connecting Simulink models to SIMATIC PLCSIM Advanced via
API.
• Use case 2: Connecting Simulink models to a SIMATIC S7-1500 software
controller via OPC UA (this document).
© Siemens AG 2020 All rights reserved

Figure 1-1: Overview of use cases


SIMATIC PLCSIM
Advanced Open Controller
+ +
Simulink Simulink Simulink

PID Controller

API OPC UA

Process model
Use case 1 Use case 2

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 4
1 Introduction

1.2 Principle of operation


Using the OPC Toolbox add-on for MATLAB, it is possible to integrate functions
into Simulink which facilitate data exchange with an OPC UA server.
This application example provides you with a fully programmed MATLAB function
"Read_OPC_Func". The function makes it possible to write and read tags from a
controller via OPC UA.
The function is integrated into Simulink via the block "Interpreted MATLAB Fcn"
and placed in a control loop rather than a controller.
The Simulink model of a PID controller is coded with SIMATIC Target 1500S and
made to run on an S7-1500 software controller. This is so that it can then be tested
in the context of a simulation model.
Tags are exchanged via a global data block in the virtual controller. In this context,
the exchange takes place in the cycle of the simulation model.
The closed-loop controller's results are visualized, and the controller itself is
controlled via a simulated HMI screen in the WinCC runtime.

Note The configuration of the HMI is not a part of this application example.

Figure 1-2
© Siemens AG 2020 All rights reserved

M-Function

S7-1500 SIMATIC WinCC runtime


Software controller

OPC UA PID 20 ms

OpcInterface

SO file

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 5
1 Introduction

1.3 Components used


This application example was created with these hardware and software
components:
Table 1-1: Hardware and software
Components Item number / Notes / Link
(R2019b) MathWorks online documentation:
MATLAB V9.7 http://mathworks.com/help/
MATLAB Coder V4.3
Simulink V10.0
Simulink Coder V9.2
OPC Toolbox V4.0.8
STEP 7 Professional 6ES7822-1..06-..
V16 Manual:
https://support.industry.siemens.com/cs/ww/en/view/109773506
SIMATIC S7-1500 ODK 6ES7806- 2CD03-0YA0
1500S V2.5 SP1 Manual:
https://support.industry.siemens.com/cs/ww/en/ps/13914/man
SIMATIC Target 1500S 6ES7823-1BE03-0YA5
for Simulink V4.0 Manual:
https://support.industry.siemens.com/cs/ww/en/ps/24443/man
SIMATIC ET 200SP 6ES7677-2DB42-0GB0
© Siemens AG 2020 All rights reserved

Open Controller Manual, SIMATIC S7-1500 CPU 150xS


CPU 1515SP PC2 https://support.industry.siemens.com/cs/ww/en/view/109740725
Manual: SIMATIC ET 200SP Open Controller CPU 1515SP PC
https://support.industry.siemens.com/cs/ww/en/view/109758551

This application example consists of the following components:


Table 1-2: Components of the application example

109749187_DIGI_Usecases_MAIN_DOC_V20_en.pdf Main document.


109749187_DIGI_Usecases_OPC_DOC_V20_en.pdf This document.
109749187_DIGI_Usecases_TIAV16_PROJ_V20.zip TIA Portal project for use
cases 1 & 2.
109749187_DIGI_Usecases_Simulink_PROJ_V20.zip Simulink models for use
cases 1 & 2.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 6
2 Engineering

2 Engineering
2.1 Hardware configuration in TIA Portal
Perform the following steps to set up the hardware configuration for your PLC.
1. In the project, add a CPU 1515SP PC2. For example, with a
CPU 1505 SP V2.5 with a CPU 1505SP F V2.7. Name the PLC "UseCase2".
2. Double-click to open the CPU's "Device configuration".
© Siemens AG 2020 All rights reserved

3. In the Inspector window under "Properties > General > PROFINET interface
[X2] > Ethernet addresses", set the IP address for your CPU.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 7
2 Engineering

4. Double-click to open the software PLC's "Device configuration".

5. In the Inspector window under "Properties > General > PROFINET interface
[X1] > Ethernet addresses", set the IP address for your CPU.
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 8
2 Engineering

6. Under "OPC UA", enable the OPC UA server for your CPU. Under "Server
addresses:", you will find the IP address and the port number where the server
can be reached.

7. Under "Runtime licenses > OPC UA", set the type of license you have
acquired, based on the type of license you need.
8. Enable the web server for the PC system. Acknowledge the security dialog.
© Siemens AG 2020 All rights reserved

9. Click on the "CPU 1505SP F" and enable "Activate web server on this module".
Acknowledge the security dialog.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 9
2 Engineering

10. Navigate to "Web server > User management" and check all checkboxes under
"Access level" to grant full access. Create a password if necessary.
© Siemens AG 2020 All rights reserved

Note
In order to load the SO file to the web server or delete it, the parameters "read
files" and "write/delete" files must be enabled. Any further parameters are
optional.
11. Set the PG/PC interface.
1. To do this, open your Windows Control Panel.
2. Set the appearance to "Small icons".
3. Select "Set PG/PC Interface".
4. For S7ONLINE (STEP 7), select the Ethernet interface of your PG/PC with
TCPIP.1.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 10
2 Engineering

12. Load the hardware configuration to the ET 200SP Open Controller.


– Select "Usecase2" in the project tree.
– Click the "Download to device" button.

– Select "PN/IE".
– Select the Ethernet interface of your PG/PC.
– Select "Direct at Slot '2 X2'".

© Siemens AG 2020 All rights reserved

Click "Start search".


– Click "Load".

Unrestricted

13. Confirm all subsequent dialogs until the download is complete.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 11
2 Engineering

Note When you load the hardware configuration of the SIMATIC ET 200SP Open
Controller for the first time, you must use the Windows interface (here IP:
192.168.2.1).
Make sure that the IP address of the Windows interface matches the
parameterization in TIA Portal.

2.2 Code generation with SIMATIC Target 1500S


The following chapter describes how to prepare the simulated model of the PID
controller with Simulink for coding with Target 1500S, then how to carry it out.

2.2.1 Settings for code generation

Make the following code generation settings for your model.


1. Open your model "Pendulum_Controlled.slx".
2. In the menu bar, click on "Code > Settings > C/C++ Code generation
settings…".
3. Navigate to "Code Generation".
1. Click the "Browse" button to open the target selection window.
2. Select "Target_1500S_V4_0_grt.tlc".
© Siemens AG 2020 All rights reserved

3. Confirm the setting with "Apply".

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 12
2 Engineering

4. Navigate to "Solver".
1. Leave "Start time" at "0".
2. Change "Stop time" to "inf". This ensures that the External Mode will be
used without a definite end.
3. Leave "Solver" on the default "auto (Automatic solver selection)".

5. Navigate to "Code Generation > Optimization".


© Siemens AG 2020 All rights reserved

– Set the parameter "Default parameter behavior" to "Tunable".


After code generation, this setting gives you access with STEP 7 (TIA Portal)
to all internal parameters of the model.

Unrestricted
6. Click on "Target 1500S Options > Target 1500s Options".

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 13
2 Engineering

7. Navigate to "TIA Portal integration".


1. Select your TIA project under "TIA Portal project".
2. For "Device", set the software PLC.
3. For "PROFINET interface", select the [X1] interface. The IP address is
modified automatically.
4. Make sure that "Import the generated SCL file to STEP 7" is checked.
5. Confirm with "Apply"
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 14
2 Engineering

8. Navigate to "Accessibility".
1. Check the box for "Enable parameter access with Step 7".
2. Check the box for "Enable Step 7 access to internal model signals".
3. Make sure that "Generate model diagrams for PLC Web server" is
checked.
4. Confirm with "Apply"
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 15
2 Engineering

9. Navigate to "Post-build options" and make sure that the box by "Transfer the
generated SO file to the CPU" is checked.
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 16
2 Engineering

10. Navigate to "ODK options".


1. For "ODK 1500S version", select V2.5.
2. Check the box for "Copy all referenced files to ODK project".
3. Confirm with "Apply"
© Siemens AG 2020 All rights reserved

Note
The parameters "Heap size for dynamic memory allocation in kByte" and "Max
block size for dynamic memory allocation in Byte" only need to be adjusted if
there are problems with the code generation.
For further information on this topic, please consult the Target 1500S manual
https://support.industry.siemens.com/cs/ww/en/ps/24443/man
and the SIMATIC S7-1500 ODK 1500S manual
https://support.industry.siemens.com/cs/ww/en/ps/13914/man.
11. Close the dialog by clicking "OK".
Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 17
2 Engineering

2.2.2 Coding with SIMATIC Target 1500S

1. Open your own model, or the supplied model "Pendulum_Controlled.slx".


2. Right-click to select the subsystem "PID_Controller" and click "Code > C/C++
Code > Build This Subsystem" in the selection window.
3. The "Build code for Subsystem:PID_Controller" window will open. Confirm this
with "Build".
© Siemens AG 2020 All rights reserved

– Simulink cannot be operated while the code is generating.


– SIMATIC Target 1500S compiles the Simulink model
"Pendulum_Controlled" with its input and output tags to "C/C++" code.
– This source code is then used to generate the SO file and SCL source with
the ODK compiler of the SIMATIC ODK 1500S.
4. Once the code generation is done, you can click the "View..." link in the status
bar of the Simulink window.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 18
2 Engineering

5. The "Diagnostic Viewer" opens.


– Click the "outputs" link. You will be taken directly to the save directory of
the SO file and the SCL source.

6. The directory of the Simulink model,


© Siemens AG 2020 All rights reserved

"…\Pendulum_Target_1500S_V1_0_grt_Output\outputs", contains the


generated files.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 19
2 Engineering

7. In the TIA Portal project, check whether the SCL files have been automatically
imported. Otherwise, manually add the SCL file that was generated from
Matlab. Here, follow the steps for "Importing SCL source" in Creating Step 7
program.
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 20
2 Engineering

8. Check whether the SO file was automatically saved in the web server under
"Filebrowser > ODK1500S". Otherwise, perform this upload manually ("Load
SO file to web server" in "Creating Step 7 program").
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 21
2 Engineering

2.2.3 Description of the generated function blocks

The following table describes all blocks that have been generated from the SCL
source.

Table 2-1: Block explanation


Generated block Functional description
FB Block clears the SO file from the CPU's memory.
PID_Controller_Unload
FB Block loads the SO file from the web server into
PID_Controller_Load the CPU's memory

FB Block calls up the SO file. The interfaces


PID_ControllerOneStep correspond to the original Simulink model.

FB Block is called for reading or writing via the user-


PID_ControllerWebReadWrite defined website.
(optional)
FB Block is called to read or write manipulated model
PID_ControllerReadWriteParameters parameters.
(optional)
© Siemens AG 2020 All rights reserved

2.3 Simulation model in Simulink


The following chapter describes how to create a user-defined MATLAB function
"Read_OPC_Func" for data exchange with an OPC server in MATLAB and
integrate it into the Simulink model via the block "Interpreted MATLAB Function".

2.3.1 MATLAB function

Perform the following steps to create a MATLAB function.


1. Navigate in MATLAB to the save location of your Simulink model and add a
new function there.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 22
2 Engineering

2. Name the function "Read_OPC_Func" and double-click the function to open


the function editor.

3. Enter the following code in the function editor. You can find a master copy in
the appendix under chapter 5.
Adjust the following parameters to match your application:
1. Specify the IP address here and the port of the OPC server.
2. Enter the name of the interface DB here via which the data exchange
between the PLC and the model takes place.
3. Enter the name of the tag here whose value the function is supposed to
read from the interface DB.
4. Enter the name of the tag here whose values the function is supposed to
write in the interface DB.
© Siemens AG 2020 All rights reserved

2 3

Unrestricted

4. Save the created function.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 23
2 Engineering

2.3.2 Inserting MATLAB function

1. Open your Simulink model with the control loop and remove the PID controller
and the highlighted functions, which are executed by the virtual controller.

2. Open the Simulink Library Browser.


© Siemens AG 2020 All rights reserved

3. Drag and drop the block "Interpreted MATLAB function" from the directory
"Simulink > User-Defined Functions" and into the Simulink model; then connect
it with the model.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 24
2 Engineering

4. Double-click to open the block properties of the block "Interpreted MATLAB


function".

5. For the function name, enter the name of your function, here
"Read_OPC_Func", and set other parameters as shown in the figure below.
Apply the change with "Apply" and confirm with "OK".
© Siemens AG 2020 All rights reserved

2.4 Create Step 7 program


Importing SCL source
In case the automatic import of the SCL source did not work, perform the following
steps to import the SCL source, which was generated by the Target 1500S.
1. In the "Project tree", navigate to "External source files".
2. Click "Add new external file".
3. Select the generated SCL source "PID_Controller_ODK.scl" and import the file
in to the S7 program.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 25
2 Engineering

4. Generate:
1. Right-click the file "PID_Controller_ODK.scl".
2. Click "Generate blocks from source" and confirm the dialog.

TIA Portal will use the SCL source to generate the blocks created by
© Siemens AG 2020 All rights reserved

Target 1500S.
5. You will find the generated blocks under "Program blocks".

6. You will find the generated PLC data types under "PLC data types".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 26
2 Engineering

OBs and global DBs


Perform the steps below in order to create the OBs and global DBs.
1. Add the following blocks to your S7 program:
– Cyclic Interrupt OB: "Cyclic20ms" (with a cyclic interrupt time of 20 ms)
– Startup OB: "Startup"
– Global DB: "Global"
– Global DB: "OpcInterface"
Note
The cycle of the OB "Cyclic20ms" must match the cycle Ts of the Simulink
model (here 0.02, or 20 ms).
2. Open the DB "Global" and add the following tags. The tags are bundled in a
structure.
– pendulumController [Struct]
• angleSetpoint [Int]
• currentAngle [LReal]
• fanforce [LReal]
• operatingMode [Int] (starting value = 3)
– internalsignals [PID_ControllerInternalModelSignals]
– modelParams [PID_ControllerParams]
© Siemens AG 2020 All rights reserved

3. Open the DB "OpcInterface" and add the following tags for the data exchange
with the Simulink model:
– fanForce [Real]
– currentAngle [LReal]

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 27
2 Engineering

OB "Cyclic20ms"
Carry out the following steps to program the "Cyclic20ms" OB.
1. Open the cyclic interrupt OB "Cyclic20ms".
2. Call the block "PID_ControllerOneStep" and interconnect as shown in the
figure below.

3. Call the block "PID_ControllerReadWriteParameters" and interconnect it as


shown in the figure below.
© Siemens AG 2020 All rights reserved

4. Call the block "PID_ControllerWebReadWrite" and interconnect it as shown in


the figure below. Here, the instance name must be "gDB_SWV_Interface".

5. Assign the OPCUA tags "fanForce" and "currentAngle" their values.


Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 28
2 Engineering

OB "Startup"
Perform the following steps to program the "Startup" OB.
1. Call the block "PID_Controller_Unload" and interconnect it as shown in the
figure below.

2. Call the block "PID_Controller_Load" and interconnect it as shown in the figure


below.
© Siemens AG 2020 All rights reserved

Web server user pages


Perform the steps below in order to add the user page to the web server.
1. Select the directory of the website generated from the Matlab model:
"…\Pendulum_Target_1500S_V1_0_grt_Output\website".
2. Make "home.html" the start HTML page.
3. Generate the blocks.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 29
2 Engineering

4. Check in the OB "Cyclic20ms" whether the selected web DB number (here


333) is populated when the FB "PID_ControllerWebReadWrite" is called, and
that the instance is named "gDB_SWV_Interface".

Upload SO file to web server


1. Open a browser of your choice (Internet Explorer, Firefox, etc.).
2. Open the website http://192.168.2.1:81.
The homepage of the CPU web server will open.
3. Click "Enter".
© Siemens AG 2020 All rights reserved

4. Navigate to "Filebrowser".
5. Click the folder "ODK1500S".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 30
2 Engineering

6. Click the "Browse" button.

7. Navigate to the "PID_Controller_ODK.so" file, which was created by Target


1500S. The file is located in the "outputs" folder of the generated Target 1500S
code.
8. Select the file and click "Open".
9. Click the "Upload file" button.
10. Close the browser.
© Siemens AG 2020 All rights reserved

Note Once you have uploaded the SO file to the web server, you must restart the
CPU.
The SO file will only be loaded to the CPU's memory once the block
"PID_Controller_Load" is called in the OB "Startup".

2.5 Commissioning the example application


Perform the steps below to commission the simulation with the supplied TIA Portal
project and the supplied Simulink model. The OPC function has already been
added in the Simulink model.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 31
2 Engineering

2.5.1 Download project

1. Open the example project or your TIA Portal project.


2. In the project tree, select the controller "UseCase2" and select "Online >
Extended download to device" in the TIA Portal menu bar.

3. Download:
© Siemens AG 2020 All rights reserved

1. Set the interface to the controller.


2. Start the search.
3. Select the found virtual controller from the list.
4. Download the project.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 32
2 Engineering

4. Follow subsequent TIA Portal messages and start the controller.

2.5.2 Start Simulink simulation

1. Open the supplied Simulink model "Pendulum_OC2_OpcUA.slx".


© Siemens AG 2020 All rights reserved

2. Start the simulation of the Simulink model.


1. Set the simulation duration to "inf" (unlimited).
2. Click the "Run" icon.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 33
3 Operation

3 Operation
3.1 Operating the PID controller
Start HMI
1. Connect via remote desktop to the Windows environment of the open
controller.
2. Start the HMI runtime via the WinCC Runtime Loader.
© Siemens AG 2020 All rights reserved

3. Select "Start Application".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 34
3 Operation

Specify setpoint
Click in the input box "Setpoint" and enter a setpoint value for the pendulum's
deflection angle. Confirm your entry with Enter.
Figure 3-1: Specify setpoint

3.2 Monitor control result


You have two ways of monitoring the control result.
First, the trace in the simulation of the HMI represents the history of the setpoint,
the actual value (deflection angle) and the manipulated variable (force) of the PID
controller.
The figure shows curves for a setpoint angle of 45 °.
© Siemens AG 2020 All rights reserved

Figure 3-2: HMI simulation

Second, you can also monitor the control result via a display window in Simulink.
OpenUnrestricted
the display window by double-clicking the "Scope" function in the Simulink
model.

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 35
3 Operation

Figure 3-3: "Scope" function

3.3 Using the user website


The browser-based user site is another option for monitoring and controlling the
PID controller.
1. Open the user site in the web server.
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 36
3 Operation

2. The values at the input and output, as well as inside the PID_Controller, can be
viewed in the user site. For the values inside, click on the subsystem.
© Siemens AG 2020 All rights reserved

3. The parameters of the PID_Controller can be adjusted under


ModelWorkspaceParams.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 37
4 Alternative solution

4 Alternative solution
Alternatively, you can download your program and the hardware configuration to a
virtual CPU and thus test the function of the PID controller in conjunction with the
simulation model.
To do this, download the CPU "UseCase2PLCSimAdv" that has already been
prepared in the TIA project to a virtual CPU on PLCSIM Advanced. Save the SO
file "PID_:Controller_ODK.so" to the virtual CPU either via the web server or via the
"Virtual SIMATIC Memory Card". Then start both the simulation of the HMI
"UseCase2HMIPLCSimAdv" and the simulation model
"Pendulum_OC2_OpcUA.slx", and test the interaction.

Note You must set the PG/PC interface in the Windows Control Panel to the Siemens
PLCSIM Virtual Ethernet Adapter.

Figure 4-1: Overview of the alternative solution

Matlab
20 ms

PID
© Siemens AG 2020 All rights reserved

OPC UA
OpcInterface communication
(asynchronous)

SO file

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 38

Unrestricted
5 MATLAB function code

5 MATLAB function code


function [x] = Read_OPC_Func(y)
% variables
persistent init_Server;
persistent init_Nodes;
persistent uaClient;
persistent Var_Node_In;
persistent Var_Node_Out;
persistent testVal;
% initialize variables
if (isempty(init_Server))
testVal = 0;
init_Server = 0;
init_Nodes = 0;
end
% OPC UA server (PLC) address and connecting client (Simulink) to
the server
if init_Server == 0
init_Server = 1;
uaClient = opcua('192.168.1.1',4840);
© Siemens AG 2020 All rights reserved

connect(uaClient);
end
% define variable nodes in the server
if uaClient.isConnected == 1 && init_Nodes == 0
init_Nodes = 1;
% read out the variables of the OPC UA server
Var_Node_In =
opcuanode(3,"""OpcInterface"".""fanForce""",uaClient);
Var_Node_Out=
opcuanode(3,"""OpcInterface"".""currentAngle""",uaClient);
end
% read and write variables of the server
if uaClient.isConnected == 1 && init_Nodes == 1
% read "fanForce" value from server and store in "val"
[val, ~, ~] = readValue(uaClient, Var_Node_In);
% assign input y of the function to "currentAngle" variable
writeValue(uaClient, Var_Node_Out, y);
% assign "val" to variable "testVal"
testVal = val;
end
% assign "fanForce" ("testVal") value to the output x of the
function
x = double(testVal);
end

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 39
6 Appendix

6 Appendix
6.1 Service and support
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire
service and support know-how and portfolio.
The Industry Online Support is the central address for information about our
products, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
support.industry.siemens.com

Technical Support
The Technical Support of Siemens Industry provides you fast and competent
support regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts.
Please send queries to Technical Support via Web form:
support.industry.siemens.com/cs/my/src
© Siemens AG 2020 All rights reserved

SITRAIN – Training for Industry


We support you with our globally available training courses for industry with
practical experience, innovative learning methods and a concept that’s tailored to
the customer’s specific needs.
For more information on our offered trainings and courses, as well as their
locations and dates, refer to our web page:
siemens.com/sitrain

Service offer
Our range of services includes the following:
• Plant data services
• Spare parts services
• Repair services
• On-site and maintenance services
• Retrofitting and modernization services
• Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
support.industry.siemens.com/cs/sc

Industry Online Support app


You will receive optimum support wherever you are with the "Siemens Industry
Online Support" app. The app is available for iOS and Android:
support.industry.siemens.com/cs/ww/en/sc/2067

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 40
6 Appendix

6.2 Industry Mall

The Siemens Industry Mall is the platform on which the entire siemens Industry
product portfolio is accessible. From the selection of products to the order and the
delivery tracking, the Industry Mall enables the complete purchasing processing –
directly and independently of time and location:
mall.industry.siemens.com

6.3 Links and literature


Table 6-1
No. Topic
\1\ Siemens Industry Online Support
https://support.industry.siemens.com
© Siemens AG 2020 All rights reserved

\2\ Link to this entry page of this application example


https://support.industry.siemens.com/cs/ww/en/view/109749187
\3\ Manual: SIMATIC S7-PLCSIM Advanced
https://support.industry.siemens.com/cs/ww/en/view/109773484
\4\ MathWorks online documentation:
http://mathworks.com/help/
\5\ Manual: STEP 7 Professional V16
https://support.industry.siemens.com/cs/ww/en/view/109773506

6.4 Change documentation


Table 6-2
Version Date Modifications
V1.0 12/2017 First edition
V2.0 09/2020 Revised use cases 1 & 2

Virtual Commissioning with SIMATIC and Simulink - Use case 2


Entry-ID: 109749187, V2.0, 09/2020 41

You might also like