0% found this document useful (0 votes)
95 views44 pages

Digi Usecases API Doc v20 en

Uploaded by

Thắng Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views44 pages

Digi Usecases API Doc v20 en

Uploaded by

Thắng Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Digitalization with TIA Portal:

Virtual Commissioning with


SIMATIC and Simulink

Use case 1:
Connecting Simulink models
to SIMATIC PLCSIM Advanced
via API

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 1


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 Setting up and starting virtual controller ............................................... 7
2.2 Enabling simulation support in TIA Portal ............................................ 8
2.3 TIA Portal hardware configuration ....................................................... 9
2.4 Code generation with SIMATIC Target 1500S ................................... 12
2.4.1 Settings for code generation .............................................................. 12
2.4.2 Coding with SIMATIC Target 1500S .................................................. 18
2.4.3 Description of the generated function blocks ..................................... 22
2.5 Create Step 7 program ...................................................................... 22
2.6 Insert S-function in Simulink model .................................................... 28
2.7 Commissioning the example application ............................................ 31
2.7.1 Download project ............................................................................... 31
2.7.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
3.4 Speeding up/slowing down the controller's virtual time ...................... 38
4 Useful information .......................................................................................... 40
4.1 Block parameters of the S-function "PLCSIM Advanced" .................. 40
4.2 S-function "PLCSIM Advanced" ......................................................... 41
4.3 Synchronization ................................................................................. 42
5 Appendix.......................................................................................................... 43
5.1 Service and support ........................................................................... 43
5.2 Industry Mall ...................................................................................... 44
5.3 Links and literature............................................................................. 44
5.4 Change documentation ...................................................................... 44

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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 provides you with the S-function "PLCSIM Advanced" for
Simulink for the purpose of communication and data exchange with a virtual
controller via the PLCSIM Advanced API. This makes it possible to validate and
virtually commission a PID control system, running 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 (this document).
• Use case 2: Connecting Simulink models to a SIMATIC S7-1500 software
controller via OPC UA.
© 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 1


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

1.2 Principle of operation


This application example provides you with a fully programmed S-function
"PLCSIM Advanced". The function makes it possible to write and read tags from a
PLCSIM Advanced virtual controller.
The function in Simulink is 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. Here, the
exchange always takes place at the controller's cycle control point.
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

S-Function

SIMATIC WinCC Runtime

API 20 ms

PID

Global

SO file

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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: Software components
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
S7-PLCSIM Advanced 6ES7823-1FE02-0YA5
© Siemens AG 2020 All rights reserved

V3.0 Upd1 Manual:


https://support.industry.siemens.com/cs/ww/en/view/109773484

This application example consists of the following components:


Table 1-2: Components of the application example
Components Contents
109749187_DIGI_Usecases_MAIN_DOC_V20_en.pdf Main document.
109749187_DIGI_Usecases_API_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 1


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

2 Engineering
2.1 Setting up and starting virtual controller
1. Start S7-PLCSIM Advanced V3.0 Upd1. The PLCSIM Advanced icon will
appear in the info region of the Windows taskbar. Left-click the icon to open the
graphical interface of PLCSIM Advanced, the Control Panel.

2. Start an instance of the virtual controller with the Control Panel.


1. Select the PLCSIM Virtual Eth. adapter.
2. Enter an instance name, for example "MFP".
3. Enter the IP address 192.168.3.1 and subnet mask 255.255.255.0.
4. Click on "Start".
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

3. Check whether the "Siemens PLCSIM Virtual Ethernet Adapter" is enabled.


Also check whether "Siemens PLCSIM Virtual Switch" is enabled and "Internet
Protocol Version 4 (TCP/IPv4)" is set to "Obtain an IP address automatically".
© Siemens AG 2020 All rights reserved

4. 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 Siemens PLCSSIM Virtual Ethernet
Adapter.TCPIP.1.

Unrestricted

2.2 Enabling simulation support in TIA Portal


Perform the following steps to enable simulation support with PLCSIM Advanced.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

1. Create a new TIA Portal project.


2. Open your project's "Properties".

3. Enable simulation support with PLCSIM Advanced under the "Protection" tab.
1. Enable "Support simulation during block compilation".
2. Click "OK" to confirm the change.
© Siemens AG 2020 All rights reserved

2.3 TIA Portal hardware configuration


Perform the following steps to set up the hardware configuration for your PLC.
1. In the project, add a CPU 1518 MFP with V2.8. Name the PLC "UseCase1".
2. Double-click to open the CPU's "Device configuration".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

3. In the Inspector window under "Properties > General > PROFINET interface
[X1] > Ethernet addresses", set the IP address (e.g. 192.168.3.1) for your
CPU.

4. Enable the web server. Acknowledge the security dialog.

5. 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.
Unrestricted

Unrestricted
Virtual Commissioning with SIMATIC and Simulink - Use case 1
Entry-ID: 109749187, V2.0, 09/2020 10
2 Engineering

6. Download the hardware configuration to the virtual controller. Select


"Usecase1" in the project tree. Click the "Download to device" button.

7. Select "PN/IE".
– Select the Ethernet interface "Siemens PLCSIM Virtual Ethernet Adapter".
– Select "Direct at Slot '1 X1'".
– Click "Start search".
– Click "Load".
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

8. Confirm the "Assign IP address" dialog with "Yes" and allow the Siemens
PLCSIM Virtual Ethernet Adapter to assign an IP address.

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

2.4 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.4.1 Settings for code generation


© Siemens AG 2020 All rights reserved

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".
3. Confirm the setting with "Apply".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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 1


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 "UseCase1".
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 1


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 1


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 1


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 1


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

2.4.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.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

4. Once the code generation is done, you can click the "View..." link in the status
bar of the Simulink window.

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.
© Siemens AG 2020 All rights reserved

Unrestricted

6. The directory of the Simulink model,


"…\Pendulum_Target_1500S_V1_0_grt_Output\outputs", contains the
generated files.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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 1


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 1


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

2.4.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.5 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 1


Entry-ID: 109749187, V2.0, 09/2020 22
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
Target 1500S.
© Siemens AG 2020 All rights reserved

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 1


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

OBs and global DBs


Follow the steps outlined below to create the global project tags and tags for
communicating with the Simulink model.
1. Add the following blocks to your S7 program:
– Cyclic Interrupt OB: "Cyclic20ms" (with an interrupt time of 20 ms)
– Startup OB: "Startup"
– Global DB: "Global"
Note
The cycle of the OB "Cyclic20ms" must match the cycle Ts of the Simulink
model (here, Ts=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

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.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

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


shown in the figure below.

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


the figure below. Here, the instance name must be "gDB_SWV_Interface".
© Siemens AG 2020 All rights reserved

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.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

Unrestricted
2 Engineering

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


below.

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.
© Siemens AG 2020 All rights reserved

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".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

Upload SO file to web server


1. Open a browser of your choice (Internet Explorer, Firefox, etc.).
2. Open the website http://192.168.3.1. The homepage of the CPU web server
will open.
3. Click "Enter".
4. Navigate to "Filebrowser".
5. Click the folder "ODK1500S".
© Siemens AG 2020 All rights reserved

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. SelectUnrestricted
the file and click "Open".
9. Click the "Upload file" button.
10. Close the browser.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

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.6 Insert S-function in Simulink model


Requirement
• You have started an instance of the virtual controller with PLCSIM Advanced.
• You have fully programmed the S7 program and loaded it to the virtual
controller.
• You have stored the included folder "S-Function" from the compressed folder
"Simulink_Usecase1" in a known directory on your computer, and added the
file with all child filed to the search path in MATLAB.

Perform the following steps to use the S-function in the Simulink model.
1. Open the Simulink model "Pendulum_Controlled" from the compressed folder
"Simulink_Main".
Remove the PID controller and the function that are executed by the S7
program.
© Siemens AG 2020 All rights reserved

2. Switch to the MATLAB interface and navigate in the directory to where you
stored the extracted "S-function" folder.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

3. Double-click to open the library object "PLCSimAdvancedBlockLibrary.slx". A


new window will open.
© Siemens AG 2020 All rights reserved

4. Drag and drop the S-function block "PLCSIM Advanced" into the Simulink
model with the control path.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

5. Double-click to open the block parameters of the S-function "PLCSIM


Advanced".

6. Enter the instance name of the started virtual controller of PLCSIM Advanced
in apostrophes ('MFP'). Also specify the input
('Global.pendulumController.currentAngle') and the output
('Global.pendulumController.fanforce).
© Siemens AG 2020 All rights reserved

Apply the change with "Apply".

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

7. The inputs and outputs of the S-function are derived from the PLC tags and
generated automatically. Connected the "PLCSIM Advanced" block with the
control path.

2.7 Commissioning the example application


Perform the steps below to commission the simulation with the supplied TIA Portal
© Siemens AG 2020 All rights reserved

project and the supplied Simulink model. The S-function with the parameterized
instance name "MFP" has already been added to the Simulink model.

2.7.1 Download project

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


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

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

3. Download:
1. Set the interface to the controller.
2. Start the search.
3. Select the found virtual controller from the list.
4. Download the project.

1
© Siemens AG 2020 All rights reserved

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

The virtual controller is put in "RUN" mode.

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

2.7.2 Start Simulink simulation

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

Note
The "S-function" folder must be added to the MATLAB search path.
"startup1.m" can be run to do this.

2. Start the simulation of the Simulink model.


1. Set the simulation duration to "inf" (unlimited).
2. Click the "Run" icon.
© Siemens AG 2020 All rights reserved

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

3 Operation
3.1 Operating the PID controller
Start HMI
1. In the project tree, select the HMI "HMI_PLCSIM_Adv".
2. Click "Start simulation".
© Siemens AG 2020 All rights reserved

3. Select "Start Application".

Unrestricted

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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 1


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 1


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 1


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

3.4 Speeding up/slowing down the controller's virtual time


For test purposes, you can speed up or slow down the virtual time of the controller
by using a scaling factor.
• Fast mode: A scaling factor of greater than 1 speeds up the virtual clock.
Example: Scaling factor 2.0 → The virtual time runs twice as fast.
• Slow motion: A scaling factor of less than 1 slows down the virtual clock.
Example: Scaling factor 0.5 → The progression of virtual time slows to 50%.

Note Fast mode and slow motion do not change the execution speed of the CPU
machine code. For example, the speed at which all operations of an OB1 cycle
are performed does not change. The execution speed depends on the processor
of the PC that the virtual controller is running on. If you modify the scaling factor,
more or fewer cycle control points are reached in a fixed amount of virtual time.

Make sure to enable the option "Enable time synchronization" in the block
parameters of the S-function "PLCSIM Advanced" so that the Simulink model
synchronizes with the modified simulation speed.

Perform the following steps to speed up or slow down the virtual time of the
controller.
1. Open the PLCSIM Advanced graphical interface.
© Siemens AG 2020 All rights reserved

2. Use the slider to set the scaling factor.

3. Enable the scaling factor for the instance of the virtual controller.

4. Specify a setpoint value for the PID controller and monitor the effect of the
change in virtual time.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

The following figure shows possible effects of the accelerated or slowed virtual
time.
Figure 3-4

Scaling factor WinCC Runtime Simulink

1.0

Virtual time is almost The PseudoRealtime is almost


identical to real time. identical to the real time.

0.5
© Siemens AG 2020 All rights reserved

Virtual time runs 50% The PseudoRealtime runs


slower. The control is slower 50% slower, synchronized to
on the real time base. the virtual time of the
controller.

2.0

Virtual time runs twice as The PseudoRealtime runs twice


fast. The regulation is faster as fast, synchronized to the
on the real time basis. virtual time of the controller.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


Entry-ID: 109749187, V2.0, 09/2020 39
4 Useful information

4 Useful information
4.1 Block parameters of the S-function "PLCSIM
Advanced"
Figure 4-1: Block parameters, S-function, "PLCSIM-Advanced"

3
© Siemens AG 2020 All rights reserved

Table 4-1: Description of block parameters


Item Description Note
1. Instance name of the virtual controller The instance name must be written
between two apostrophes.
2. Scaling factor for the virtual time of the Scale factor preset. Relevant for
virtual controller creating the instance of a virtual
controller from Simulink.
3. Number of inputs If "-1" is entered, the number of inputs is
derived automatically from the number
of PLC input tags.
4. Number of outputs If "-1" is entered, the number of outputs
is derived automatically from the
number of PLC input tags.
5. Tags of the inputs The tag must be written between two
apostrophes.
6. Tags of the outputs The tag must be written between two
Unrestricted apostrophes.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


Entry-ID: 109749187, V2.0, 09/2020 40
4 Useful information

4.2 S-function "PLCSIM Advanced"


The source code for the S-function "PLCSIM Advanced" is programmed in the
high-level programming language C++ and contains instructions for data exchange
with an instance of the virtual controller via the PLCSIM Advanced API. An MEX
file is generated from it so that the code can be run in MATLAB. The MEX file can
be integrated into Simulink with an S-function.

Automatic generation of an instance of the virtual controller


Mechanisms for generating a new instance of the virtual controller are programmed
in the code of the S-function "PLCSIM Advanced", in case they do not yet exist.
When applying the modified block parameters of the S-function, a new instance of
the virtual controller is automatically generated if the name entered for the instance
does not yet exist in the directory of the Virtual SIMATIC Memory Card1 of PLCSIM
Advanced.
In this case the instance is started immediately if PLCSIM Advanced is already
started. Otherwise, only the SIMATIC Memory Card with the instance name will be
created in the directory of the Virtual SIMATIC Memory Card, and the instance
must be manually started via the Control Panel of PLCSIM Advanced.

Note The S-function "PLCSIM Advanced" automatically derives the number of inputs
and outputs for the block from the PLC tags of the virtual controller.
For this reason it is recommended to first start the virtual controller and load it
© Siemens AG 2020 All rights reserved

with a project, and only then to insert the S-function in Simulink.

Inputs/outputs
The S-function "PLCSIM Advanced" automatically derives the number of contacts
for the inputs and outputs of the blocks from the PLC tags of the virtual controller if
"-1" is set for the number of inputs and outputs in the block parameters of the
S-function.

Figure 4-2

There are the following two cases of when the contacts can be generated.
• The S-function is inserted into the Simulink model before a virtual controller
has been started and loaded with TIA Portal project. After the instance name of
the virtual controller is entered in the block parameters of the S-function and
the changes are applied, an instance of the virtual controller will be
automatically generated when PLCSIM Advanced is started. The contacts are
only generated when the TIA Portal project is loaded to the virtual controller
and the Simulink simulation has been started. After the simulation is stopped,
the S-function ca be connected with the Simulink model.
• The instance of the virtual controller has been manually generated and already
loaded with the TIA Portal project. The S-function is then inserted into the
Simulink model. After the instance name of the virtual controller is entered in
the block parameters of the S-function and the changes are applied, the
contacts are automatically generated and the block can be connected.

1
The Virtual SIMATIC Memory Card saves the user program, the hardware configuration and
the retentive data for the virtual controller.

Virtual Commissioning with SIMATIC and Simulink - Use case 1


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

Tag exchange
Tags are exchanged at the cycle control point of the virtual controller. If the cycle
control point has not yet been reached, the S-function waits for the cycle end of the
virtual controller. The S-function reads the output tags and writes the input tags
from the list of PLC tags.

4.3 Synchronization
The S-function facilitates a synchronization of the Simulink model with the virtual
time of the virtual controller. To do this, the option "Enable time synchronization"
must be enabled in the block parameters of the S-function.
In such case, tag exchange occurs not only at the cycle control point, but also
when the pseudo-real-time of the Simulink model and the virtual time of the virtual
controller are nearly the same. In order to follow the modified virtual time of the
virtual controller, Simulink will slow down or speed up the pseudo-real-time.
© Siemens AG 2020 All rights reserved

Virtual Commissioning with SIMATIC and Simulink - Use case 1


Entry-ID: 109749187, V2.0, 09/2020 42
5 Appendix

5 Appendix
5.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 1


Entry-ID: 109749187, V2.0, 09/2020 43
5 Appendix

5.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

5.3 Links and literature


Table 5-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

5.4 Change documentation


Table 5-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 1


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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy