Digi Usecases API Doc v20 en
Digi Usecases API Doc v20 en
Use case 1:
Connecting Simulink models
to SIMATIC PLCSIM Advanced
via API
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.
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
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
PID Controller
API OPC UA
Process model
Use case 1 Use case 2
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
API 20 ms
PID
Global
SO file
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.
Unrestricted
Unrestricted
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
Unrestricted
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.
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
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
8. Confirm the "Assign IP address" dialog with "Yes" and allow the Siemens
PLCSIM Virtual Ethernet Adapter to assign an IP address.
Unrestricted
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)".
Unrestricted
6. Click on "Target 1500S Options > Target 1500s Options".
Unrestricted
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
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
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
Unrestricted
4. Once the code generation is done, you can click the "View..." link in the status
bar of the Simulink window.
Unrestricted
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
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
The following table describes all blocks that have been generated from the SCL
source.
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
6. You will find the generated PLC data types under "PLC data types".
Unrestricted
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.
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
Unrestricted
2 Engineering
Unrestricted
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".
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.
4. Drag and drop the S-function block "PLCSIM Advanced" into the Simulink
model with the control path.
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
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.
project and the supplied Simulink model. The S-function with the parameterized
instance name "MFP" has already been added to the Simulink model.
Unrestricted
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
Unrestricted
Note
The "S-function" folder must be added to the MATLAB search path.
"startup1.m" can be run to do this.
Unrestricted
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
Unrestricted
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
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.
Unrestricted
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
Unrestricted
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
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.
The following figure shows possible effects of the accelerated or slowed virtual
time.
Figure 3-4
1.0
0.5
© Siemens AG 2020 All rights reserved
2.0
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
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
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.
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
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
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
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