Digi Usecases Opc Doc v20 en
Digi Usecases Opc Doc v20 en
Digi Usecases Opc Doc v20 en
Use case 2:
Connecting Simulink models
to SIMATIC PLCSIM Advanced
via OPC UA
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 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
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
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
M-Function
OPC UA PID 20 ms
OpcInterface
SO file
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
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
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
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
– Select "PN/IE".
– Select the Ethernet interface of your PG/PC.
– Select "Direct at Slot '2 X2'".
–
© Siemens AG 2020 All rights reserved
Unrestricted
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.
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
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.
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
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.
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
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
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
3. Open the DB "OpcInterface" and add the following tags for the data exchange
with the Simulink model:
– fanForce [Real]
– currentAngle [LReal]
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
4. Navigate to "Filebrowser".
5. Click the folder "ODK1500S".
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".
3. Download:
© Siemens AG 2020 All rights reserved
Unrestricted
Unrestricted
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
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
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.
Matlab
20 ms
PID
© Siemens AG 2020 All rights reserved
OPC UA
OpcInterface communication
(asynchronous)
SO file
Unrestricted
5 MATLAB function code
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
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
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