Uploaded by Lộc Võ Quang

TM140TRE.00-ENG Automation Target for Simulink V4100

advertisement
TM140
B&R Automation Studio Target for
Simulink
®
Requirements
Training modules
TM210 – Working with Automation Studio
Software
Automation Studio 3 (Version 8.0.80 and higher)
Automation Studio Target for Simulink® (V3.0 and higher)
MATLAB®(R2008a and higher)
Simulink® (R2008a and higher)
Real -Time Workshop® (R2008 and higher) or Simulink Coder™ (R2011 and
higher)
Hardware
None
Trademarks
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, Simulink Coder, Simscape,
SimMechanics, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks
and MathWorks, the L-shaped membrane logo, Embedded MATLAB, and PolySpace are trademarks
of MathWorks, Inc.
Other product or brand names are trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.
2
TM140 - B&R Automation Studio Target for Simulink®
Table of contents
Table of contents
1 Introduction........................................................................................................................................... 4
1.1 Objectives................................................................................................................................6
1.2 Definition..................................................................................................................................7
1.3 Installation............................................................................................................................... 9
1.4 Advanced software requirements......................................................................................... 10
2 B&R Automation Studio Toolbox....................................................................................................... 11
2.1 B&R Config block..................................................................................................................12
2.2 B&R Input / Output blocks.................................................................................................... 13
2.3 B&R Parameter block........................................................................................................... 17
2.4 B&R Extended Input / Output blocks.................................................................................... 19
2.5 B&R Structure Input / Output blocks.................................................................................... 25
2.6 B&R Bus Input / Output blocks.............................................................................................29
2.7 B&R Workspace Variable block............................................................................................31
3 Configuration Settings........................................................................................................................ 33
4 Working with B&R Automation Studio Target for Simulink................................................................ 38
4.1 Basic example.......................................................................................................................38
4.2 Function block generation.....................................................................................................49
4.3 Structure interface blocks..................................................................................................... 55
4.4 Bus interface blocks..............................................................................................................60
4.5 Automatic transfer................................................................................................................. 64
4.6 External Mode....................................................................................................................... 66
5 Examples............................................................................................................................................ 69
5.1 PID controller........................................................................................................................ 69
5.2 Temperature model...............................................................................................................70
5.3 Hydraulics applications......................................................................................................... 73
5.4 Inverted Pendulum on a Cart Model.................................................................................... 74
5.5 Simscape applications.......................................................................................................... 83
5.6 Data logger............................................................................................................................87
5.7 Library FB PID...................................................................................................................... 93
5.8 Inverted Pendulum with Model Referencing.........................................................................94
6 Automation Studio target tools.........................................................................................................100
6.1 Trace Viewer....................................................................................................................... 100
7 Appendix...........................................................................................................................................105
7.1 Real-Time Workshop Embedded Coder or Simulink Coder Embedded Coder...................105
7.2 Simulink block support........................................................................................................ 105
7.3 Suggestions.........................................................................................................................105
7.4 Additional links.................................................................................................................... 106
TM140 - B&R Automation Studio Target for Simulink®
3
Introduction
1
Introduction
For years the MATLAB® program package from MathWorks (http://www.mathworks.com) has served
as a powerful tool in solving technical, mathematical and economic problems and has been used extensively in the industrial world. MATLAB® is a numerical computing environment and programming language. The biggest strength of the program lies in its handling of large matrices, as its name MATrix
LABoratory suggests. MATLAB®can be expanded using various add-on packages, as Simulink® for
instance. This program package allows graphic creation of simulation models, used to adjust complex
technical processes under realistic conditions.
Figure 1: MATLAB® and Simulink® by MathWorks, Inc.
Automatic implementation of Simulink models in C/C++ Code, specially optimized for use in B&R target
systems, offers the developer new possibilities for designing sophisticated simulation models and control
structures that would otherwise be impossible or very time-intensive to implement.
The biggest advantage of Automatic Code Generation affects those developers who already use MATLAB® and Simulink® for simulation and solutions design and to developers who used to tediously rework
implemented structures in a language supported by Automation Studio in the past. In the procedures
listed below, the Automatic Code Generation tool provided by B&R represents an innovation with endless possibilities that help to productively reform the development of control systems.
The basic principle is simple: The module created in Simulink® is automatically translated using Simulink
Coder® or Embedded Coder® (optional) into the optimal language for the B&R target system, guaranteeing maximum performance of the generated source code. Seamless integration into an Automation
Studio project makes the development process perfect.
4
TM140 - B&R Automation Studio Target for Simulink®
Introduction
Figure 2: Workflows of the Automatic Code Generation
The elimination of extensive reengineering in Automation Studio allows simple transfer of complex and
sophisticated Simulink models to the PLC (Hardware-in-the-Loop). Closed-loop controllers can also be
easily tested and optimized on the target system without requiring the user to adjust large amounts of
code and run the risk of creating coding errors (On-Target Rapid Prototyping).
On-Target Rapid prototyping
Automatic Code Generation makes it possible to quickly and easily transform sophisticated Simulink
based control systems into source code and integrate them into an Automation Studio project.
Many potentially successful ideas have been immediately rejected due to the large amount of time required for conversion into executable machine code and the risk of developing a dead end solution. The
‘Rapid Prototyping’ concept brings an end to this. Using Simulink® and the Automatic Code Generation
tool provided by B&R, any system, no matter how complex, can be intuitively built, compiled and tested
in a short amount of time. This practically eliminates implementation errors as the Automatic Code Generation tool has been well-proven over several years in critical fields like aviation or automotive industry.
In the case of ‘On-Target Rapid Prototyping’ the prototyping process is directly performed on the industrial target system which makes test results even more realistic and significant.
Hardware-in-the-Loop
Every modification of the closed-loop controller bears the risk of damaging the controlled system during
commissioning. ‘Hardware-in-the-Loop’ is the key word that stands for simple transfer of sophisticated
system models developed in Simulink® to a B&R target system. The prepared PLC assumes the role
of the actual system for the duration of the first function test. This allows easy and safe testing of new
controller concepts without risking the damage of costly machine parts. In some cases the controller and
system simulation can even run on the same target system.
Although there are numerous applications for using B&R’s Automatic Code Generation, they have one
thing in common: the possibility to generate source code for B&R target systems at the push of a single
button.
TM140 - B&R Automation Studio Target for Simulink®
5
Introduction
1.1
Objectives
After completing the installation described in section 1.3 "Installation", simple access to professional
application can be learned with the help of an example worked out in section 3 "Configuration Settings".
More detailed examples are located in section 5 "Examples". In section 4 "Working with B&R Automation
Studio Target for Simulink" there is a short introduction to MATLAB and Simulink functions as well as
an overview of more detailed links. A description of all B&R Automation Studio blocks for Simulink is
located in section 2 "B&R Automation Studio Toolbox".
Figure 3: Objectives
After successful completion of the training module, the user should be able to prepare existing Simulink
models for the Automatic Code Generation using B&R Automation Studio Target for Simulink.
An additional part of this training module deals with the integration of automatically generated tasks in
existing Automation Studio projects as well as recognition of numerous options for error diagnosis.
An introduction to the products of MathWorks is not included in the course of this module, but can be
found in the documentation accompanying the respective products.
6
TM140 - B&R Automation Studio Target for Simulink®
Introduction
1.2
Definition
1.2.1 Rapid Prototyping
As mentioned above, ‘Rapid Prototyping’ offers numerous possibilities for easy and flexible implementation of sophisticated control systems solutions. Innovative ideas that in the past would have been rejected because of time and resource restraints can now be smoothly developed using Simulink and transferred to the PLC using B&R Automation Studio Target for Simulink. Tedious manual reimplementation
of source code, which always bears the risk of coding errors, is a thing of the past.
Figure 4: Rapid Prototyping
The procedure is quite simple – the task created in Simulink and transferred to the controller via B&R
Automation Studio Target for Simulink is ready for application in a matter of a few steps.
1.2.2 Hardware-in-the-Loop
In order to avoid damaging the real machine system when applying newly developed algorithms, it is
recommended to implement critical system parts into an emulation system.
For this purpose, a second B&R target system is used. The emulation task runs on this system, which
mimics the behaviour of the real plant as accurately as possible. New developments are thus tested
on the emulation target system without putting the system operator at risk of experiencing damage to
hardware components.
TM140 - B&R Automation Studio Target for Simulink®
7
Introduction
Figure 5: Hardwar-in-the-Loop on two seperate B&R target systems
As there is enough free processing power available on the controller in most of the cases, both tasks
can be run on the same B&R controller, thanks to the task structure of B&R Automation Studio.
Even if the behaviour of the physical inputs and outputs must not be neglected, it is essential to use two
separate, hard-wired B&R target systems.
Figure 6: Hardware-in-the-Loop on single B&R target system Preparations
8
TM140 - B&R Automation Studio Target for Simulink®
Introduction
1.3
Installation
IMPORTANT
For installation of B&R Automation Studio Target for Simulink on a Windows Vista or Windows
7 system, MATLAB has to be performed as administrator (Right-click → ‘Run as administrator’).
IMPORTANT
Before installing B&R Automation Studio Target for Simulink it is necessary to configure a C
compiler for MATLAB. To start the compiler setup enter “mex -setup” in the MATLAB command
window. Please see www.mathworks.de/support/compilers/ for compatible compilers.
The components required for using B&R Automation Studio Target for Simulink can be installed using
the enclosed setup script ‘install.p’.
Figure 7: B&R Automation Studio Target for Simulink setup
The B&R Automation Studio Target for Simulink components will be installed into a directory of your
choice (e.g. C:\B&R Automation Studio Target for Simulink – the directory ‘B&R Automation Studio Target
for Simulink’ will be added automatically) and registered in MATLAB. After the installation please restart
MATLAB in order to guarantee smooth functionality.
IMPORTANT
Especially on Windows 7 and Windows Vista systems please do NOT install B&R Automation
Studio Target for Simulink components into the Windows program folder (e.g. ‘C:\Program Files
\ B&R Automation Studio Target).
For removing B&R Automation Studio Target for Simulink from your system please use the enclosed
uninstall script ‘uninstall.p’.
TM140 - B&R Automation Studio Target for Simulink®
9
Introduction
Figure 8: B&R Automation Studio Target for Simulink uninstall
1.4
Advanced software requirements
For use of Automatic Code Generation with B&R Automation Studio Target for Simulink, the following
software components are required:
•
•
•
Automation Studio 3 (Version 3.0.71 and higher)1
Automation Studio Target for Simulink® (Version 3.0 and higher)
MathWorks Release 2008a and higher2
■
MATLAB®(Version 7.6 and higher)
■
Simulink® (Version 7.1 and higher)
■
Real-Time Workshop® (Version 7.1 and higher) or Simulink Coder™ (Version 8.0 and
higher)
For optimal code efficiency, the use of Real-Time Workshop® Embedded Coder™ or Simulink Coder™
Embedded Coder™, is suggested for automatic code generation.
•
Real-Time Workshop® Embedded Coder™ (Version 5.1 and higher) or Simulink Coder™ Embedded Coder™ (Version 6.0 and higher)
Furthermore, in case of use of Stateflow objects the following products are also required for code generation:
•
•
Stateflow® (Version 7.1 or higher)
Stateflow® Coder™ (Version 7.1 or higher)
Most toolboxes provided by MathWorks are also fully compatible with B&R Automation Studio Target
for Simulink.
1
2
Resommended: 3.0.80 or higher
Recommended: Release 2012b or higher
10
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2
B&R Automation Studio Toolbox
The ‘B&R Automation Studio Toolbox’ is available after the installation of Automation Studio Target for
Simulink. It contains eleven B&R specific interface and configuration blocks that are described in the
following sections.
•
•
•
•
•
•
•
•
•
•
•
B&R Config block
B&R Input block
B&R Output block
B&R Parameter block
B&R Extended Input block
B&R Extended Output block
B&R Structure Input block
B&R Structure Output block
B&R Bus Input block
B&R Bus Output block
B&R Workspace Variable block
Figure 9: B&R Automation Studio Toolbox
TM140 - B&R Automation Studio Target for Simulink®
11
B&R Automation Studio Toolbox
2.1
B&R Config block
The ‘B&R Config block’ is used to switch between three modes of operation, ‘Simulation’, ‘Code Generation (ERT based)’ and ‘Code Generation (GRT based)’. Once the block is inserted to an existing
Simulink model the current configuration set is renamed to ‘Simulation’ and the ‘Code Generation (ERT
based)’ and ‘Code Generation (GRT based)’ configuration sets are added.
IMPORTANT
Only one instance of the B&R Config block can be added to a Simulink model. The B&R Config
block has to be located in the root level of your Simulink model.
Figure 10: B&R Config block
Selecting the configuration set ‘Code Generation (ERT based)’ automatically invokes the system target
file ‘bur_ert.tlc’ for the current Simulink model. If ‘Code Generation (GRT based)’ is selected the system
target file ‘bur_grt.tlc’ will be activated. Alternatively the corresponding system target file can also be
selected manually by experienced users.
INFORMATION
The configuration set ‘Code Generation (Embedded Real Time Target based)’ will only be available if Real-Time Workshop® Embedded Coder™ or Simulink Coder™ Embedded Coder™ is
installed on the current system.
12
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2.2
B&R Input / Output blocks
The ‘B&R Input block’ serves as the interface between the automatically generated task or function
block based on the Simulink model and the other parts of the project. For each ‘B&R Input block’ an IEC
variable is created on the target system.
Figure 11: B&R Input Block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
TM140 - B&R Automation Studio Target for Simulink®
13
B&R Automation Studio Toolbox
Variable Data Type: The data type of the created variable can be selected from all available numerical
data types in Automation Studio and Simulink:
Automation Studio
Simulink
Value range
BOOL
boolean
FALSE, TRUE
DINT
int32
-2.147.483.648 … 2.147.483.647
INT
int16
-32768 … 32767
LREAL (default)
double
-1.7E+308 … 1.7E+308
REAL
single
-3.4E+38 … 3.4E+38
SINT
int8
-128 … 127
UDINT
uint32
0 … 4.294.967.295
UINT
uint16
0 … 65535
USINT
uint8
0 … 255
IMPORTANT
While declaring global variables manually in Automation Studio, the user must make sure that
the data type of the variable in the project matches the selected data type in the dialog field.
Initial Value: The start value of the variable is defined in this entry field. The variable created on the
B&R target will be initialized with this value.
INFORMATION
All elements of an array are initialized with the same value.
Array Dimension: If the value of this field exceeds zero, an array is created instead of a scalar variable.
Memory: It can be chosen if the variable is RETAIN or standard.
Parameter (only for function block- generation): If this feature is active the variable is treated as an
internal variable and not as input.
The ‘B&R Output block’ serves as the interface between the automatically generated task or function
block based on the Simulink model and the other parts of the project. For each ‘B&R Output block’ a
variable is created on the target system.
14
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
Figure 12: B&R Output block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
TM140 - B&R Automation Studio Target for Simulink®
15
B&R Automation Studio Toolbox
Variable Data Type: The data type of the created variable can be selected from all data types available
in Automation Studio and Simulink:
Automation Studio
Simulink
Value range
BOOL
boolean
FALSE, TRUE
DINT
int32
-2.147.483.648 … 2.147.483.647
INT
int16
-32768 … 32767
LREAL (default)
double
-1.7E+308 … 1.7E+308
REAL
single
-3.4E+38 … 3.4E+38
SINT
int8
-128 … 127
UDINT
uint32
0 … 4.294.967.295
UINT
uint16
0 … 65535
USINT
uint8
0 … 255
When manually declaring global variables in Automation Studio, the user must make sure that
the data type of the variable in the project matches the selected data type in the dialog field.
Initial Value: The start value of the variable is defined in this entry field. The variable created on the
B&R target will be initialized with this value.
INFORMATION
All elements of an array are initialized with the same value.
Array Dimension: If the value of this field exceeds zero, an array is created instead of a scalar variable.
Memory: It can be chosen if the variable is RETAIN or standard.
16
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2.3
B&R Parameter block
The ‘B&R Parameter block’ is used to make internal parameters of individual blocks accessible during
operation on the target system. For each ‘B&R Input block’ a variable is created on the target system.
Figure 13: B&R Parameter block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
TM140 - B&R Automation Studio Target for Simulink®
17
B&R Automation Studio Toolbox
Variable Data Type: The data type of the created variable can be selected from all data types available
in Automation Studio and Simulink:
Automation Studio
Simulink
Value range
BOOL
boolean
FALSE, TRUE
DINT
int32
-2.147.483.648 … 2.147.483.647
INT
int16
-32768 … 32767
LREAL (default)
double
-1.7E+308 … 1.7E+308
REAL
single
-3.4E+38 … 3.4E+38
SINT
int8
-128 … 127
UDINT
uint32
0 … 4.294.967.295
UINT
uint16
0 … 65535
USINT
uint8
0 … 255
IMPORTANT
When manually declaring global variables in Automation Studio, the user must make sure that
the data type of the variable in the project matches the selected data type in the dialog field.
Initial Value: The start value of the variable is defined in this entry field. The variable created on the
B&R target will be initialized with this value.
INFORMATION
All elements of an array are initialized with the same value.
Array Dimension: If the value of this field exceeds zero, an array is created instead of a scalar variable.
Memory: It can be chosen if the variable is RETAIN or standard.
18
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2.4
B&R Extended Input / Output blocks
The ‘B&R Extended Input block’ serves as the interface between the automatically generated task or
function block based on the Simulink model and the other parts of the project. For each ‘B&R Extended
Input block’ a variable is created on the target system. The ‘Extended B&R blocks’ provide an easy–touse means to convert hardware inputs or outputs (usually INT) to floating point values (REAL or LREAL)
for powerful calculations in the control algorithm and vice versa. The conversion and casting is done
automatically by this block.
Figure 14: B&R Extended Input block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio variable.
TM140 - B&R Automation Studio Target for Simulink®
19
B&R Automation Studio Toolbox
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
Array Dimension: If the value of this field exceeds zero, an array is created instead of a scalar variable.
Automation Studio Data Type: The data type of the created variable can be selected from all data
types available in Automation Studio and Simulink:
Automation Studio
Simulink
Value range
BOOL
boolean
FALSE, TRUE
DINT
int32
-2.147.483.648 … 2.147.483.647
INT
int16
-32768 … 32767
LREAL (default)
double
-1.7E+308 … 1.7E+308
REAL
single
-3.4E+38 … 3.4E+38
SINT
int8
-128 … 127
UDINT
uint32
0 … 4.294.967.295
UINT
uint16
0 … 65535
USINT
uint8
0 … 255
IMPORTANT
When manually declaring global variables in Automation Studio, the user must make sure that
the data type of the variable in the project matches the selected data type in the dialog field.
Automation Studio Maximum Value: Maximum value for Automation Studio input corresponding to
the maximum Simulink value used for calculations.
Automation Studio Initial Value: The start value of the variable is defined here. The variable created
on the B&R target will be initialized with this value.
INFORMATION
All elements of an array are initialized with the same value.
Simulink Data Type: The data type of the variable used for calculations can be selected from all data
types available in Automation Studio and Simulink.
20
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
The conversion operation from Simulink calculation value to Automation Studio value is:
Simulink Minimum Value: Minimum Simulink value corresponding to the minimum value for Automation
Studio input. The calculated value is limited automatically.
Simulink Maximum Value: Maximum Simulink value corresponding to the maximum value for Automation Studio input. The calculated value is limited automatically.
Simulink Simulation Value: During Simulink simulations the output of the block will be set to the value
defined here.
Memory: It can be chosen if the variable is RETAIN or standard.
Parameter (only for function block generation): If this feature is active the variable is treated as an
internal variable and not as input.
The ‘B&R Extended Output block’ serves as the interface between the automatically generated task or
function block based on the Simulink model and the other parts of the project. For each ‘B&R Extended
Output block’ a variable is created on the target system. The ‘Extended B&R blocks’ provide an easy-touse means to convert hardware inputs or outputs (usually INT) to floating point values (REAL or LREAL)
for powerful calculations in the control algorithm and vice versa. The conversion and casting is done
automatically by the library block.
TM140 - B&R Automation Studio Target for Simulink®
21
B&R Automation Studio Toolbox
Figure 15: B&R Extended Output block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
22
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however. (see section 4.1.8 "Variables: Global and local variable files")
Array Dimension:If the value of this field exceeds zero, an array is created instead of a scalar variable.
Automation Studio Data Type: The data type of the created variable can be selected from all data
types available in Automation Studio and Simulink:
Automation Studio
Simulink
Value range
BOOL
boolean
FALSE, TRUE
DINT
int32
-2.147.483.648 … 2.147.483.647
INT
int16
-32768 … 32767
LREAL (default)
double
-1.7E+308 … 1.7E+308
REAL
single
-3.4E+38 … 3.4E+38
SINT
int8
-128 … 127
UDINT
uint32
0 … 4.294.967.295
UINT
uint16
0 … 65535
USINT
uint8
0 … 255
Simulink Minimum Value: Minimum Simulink value corresponding to the minimum value for Automation
Studio output.
Simulink Maximum Value: Maximum Simulink value corresponding to the maximum value for Automation Studio input. The calculated value is limited automatically.
Simulink Simulation Value: During Simulink simulations the output of the block will be set to the value
defined here.
Automation Studio Data Type: The data type of the variable used for calculations can be selected from
all data types available in Automation Studio and Simulink.
The conversion operation from Simulink calculation value to Automation Studio value is:
IMPORTANT
When manually declaring global variables in Automation Studio, the user must make sure that
the data type of the variable in the project matches the selected data type in the dialog field.
Automation Studio Minimum Value: Minimum value for Automation Studio input corresponding to the
minimum Simulink value used for calculations. The calculated value is limited automatically.
TM140 - B&R Automation Studio Target for Simulink®
23
B&R Automation Studio Toolbox
Automation Studio Maximum Value: Maximum value for Automation Studio input corresponding to
the maximum Simulink value used for calculations. The calculated value is limited automatically.
Automation Studio Initial Value: The start value of the variable is defined here. The variable created
on the B&R target will be initialized with this value.
INFORMATION
All elements of an array are initialized with the same value.
Memory: It can be chosen if the variable is RETAIN or standard.
24
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2.5
B&R Structure Input / Output blocks
The ‘B&R Structure Input block’ enables the use of structure elements defined in Automation Studio for
Automatic Code Generation. Structures that are defined in the .typ files assigned to the current Simulink
model can be used as an interface for the automatically generated task or function block.
Figure 16: B&R Structure Input block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
TM140 - B&R Automation Studio Target for Simulink®
25
B&R Automation Studio Toolbox
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
Type File: Lists all available .typ files for the current model (see section 4.3 "Structure interface blocks").
After selecting a type file the associated structure names defined in the file are displayed in ‘Structure
Name’.
Structure Name: Lists the available structures in the currently selected type file. After selecting a structure name the associated structure elements are displayed in ‘Structure Element’.
Variable Data Type: Displays the data type of the currently selected structure element. The data type is
defined in the type file (see section 4.3 "Structure interface blocks") and cannot be modified in this mask.
Array Dimension: Displays the array dimension of the currently selected structure element. The array
dimension is defined in the type file (see section 4.3 "Structure interface blocks") and cannot be modified
in this mask.
The ‘B&R Structure Output block’ enables the use of structure elements defined in Automation Studio
for Automatic Code Generation. Structures that are defined in the type files assigned to the current
Simulink model can be used as an interface for the automatically generated task or function block.
26
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
Figure 17: B&R Structure Output block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
TM140 - B&R Automation Studio Target for Simulink®
27
B&R Automation Studio Toolbox
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
Type File: Lists all available .typ files for the current model (see section 4.3 "Structure interface blocks").
After selecting a type file the associated structure names defined in the file are displayed in ‘Structure
Name’.
Structure Name: Lists the available structures in the currently selected type file. After selecting a structure name the associated structure elements are displayed in ‘Structure Element’.
Variable Data Type: Displays the data type of the currently selected structure element. The data type is
defined in the type file (see section 4.3 "Structure interface blocks") and cannot be modified in this mask.
Array Dimension: Displays the array dimension of the currently selected structure element. The array
dimension is defined in the type file (see section 4.3 "Structure interface blocks") and cannot be modified
in this mask.
28
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2.6
B&R Bus Input / Output blocks
The ‘B&R’ Bus Input block’ serves as the interface between the automatically generated task or function
block based on the Simulink model and the other parts of the project. For each ‘B&R Bus Input block’
a variable is created on the target system.
Figure 18: B&R Bus Input block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio Variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
Variable Bus Name: Name of the Simulink bus that is used as data type.
Array Dimension: If the value of this field exceeds zero, an array is created instead of a scalar variable.
Memory: It can be chosen if the variable is RETAIN, standard or a pointer. For inputs which are reference
to variables (pointers) the Automation Studio variable data type is a UDINT which indicates the address
of the variable.
TM140 - B&R Automation Studio Target for Simulink®
29
B&R Automation Studio Toolbox
The ‘B&R Bus Output block’ serves as the interface between the automatically generated task or
function block based on the Simulink model and the other parts of the project. For each ‘B&R Bus Output
block’ a variable is created on the target system.
Figure 19: B&R Bus Output block
Variable Name: Specifies the Automation Studio variable name on the target system.
Variable Description: Description of the Automation Studio Variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
Variable Bus Name: Name of the Simulink bus that is used as data type.
Array Dimension: If the value of this field exceeds zero, an array is created instead of a scalar variable.
Memory: It can be chosen if the variable is RETAIN, standard or a pointer. For inputs which are reference
to variables (pointers) the Automation Studio variable data type is a UDINT which indicates the address
of the variable.
30
TM140 - B&R Automation Studio Target for Simulink®
B&R Automation Studio Toolbox
2.7
B&R Workspace Variable block
The ‘B&R’ Workspace Variable block’ serves as the interface between the automatically generated
task or function block based on the Simulink model and the other parts of the project. It provides the use
of variables from the MATLAB workspace in generated tasks or function blocks in Automation Studio.
Figure 20: B&R Workspace Variable block
Variable: Lists variables from MATLAB workspace. Supported data types of variables:
•
•
•
•
Double
Double 1D-array
Double 2D-array
Structures (numerical data types only)
IMPORTANT
External Mode cannot be used in combination with structures generated by the “B&R Workspace Variable Block”.
Variable Description: Description of the Automation Studio variable.
Variable Scope: Specifies the scope (‘GLOBAL’ or ‘LOCAL’) of the variable created on the target system.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file”
feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is intended
as support for the user, however (see section 4.1.8 "Variables: Global and local variable files").
TM140 - B&R Automation Studio Target for Simulink®
31
B&R Automation Studio Toolbox
Variable Data Type: The data type of the created variable can be selected from all data types available
in Automation Studio and Simulink:
Automation Studio
Simulink
Value range
BOOL
boolean
FALSE, TRUE
DINT
int32
-2.147.483.648 … 2.147.483.647
INT
int16
-32768 … 32767
LREAL (default)
double
-1.7E+308 … 1.7E+308
REAL
single
-3.4E+38 … 3.4E+38
SINT
int8
-128 … 127
UDINT
uint32
0 … 4.294.967.295
UINT
uint16
0 … 65535
USINT
uint8
0 … 255
IMPORTANT
The initial values of the variable in the MATLAB workspace must correspond to the assigned
data type.
INFORMATION
Local variables are created automatically. Global variables have to be declared manually in
Automation Studio or can be generated automatically by setting the “create global*.var file” feature in the B&R Advanced Settings. The automatically generated file ‘global.txt’ is only intended
as support for the user (see section 4.1.8 "Variables: Global and local variable files").
INFORMATION
All elements are initialized with current value of the chosen variable in MATLAB workspace.
Memory: It can be chosen if the variable is RETAIN, constant or standard.
32
TM140 - B&R Automation Studio Target for Simulink®
Configuration Settings
3
Configuration Settings
For the configuration of the interface between Simulink and Automation Studio the following project
settings options are available.
Figure 21: Fundamental sample time
Fundamental sample time: The fundamental sample time of the Simulink model must be equal to the
selected task class cycle of the PLC. The sample time is entered in seconds.
IMPORTANT
If the fundamental sample time specified in Simulink does not match the duration of the cyclic
task class for the automatically generated task in Automation Studio, the task will be suspended
and an entry in the PLC’s logger will be created.
TM140 - B&R Automation Studio Target for Simulink®
33
Configuration Settings
Figure 22: B&R Basic Settings
Automation Studio project path: Base directory (absolute or relative path) of the Automation Studio
project containing the automatically generated task .
Automation Studio task or library name: Name of the automatically generated task or library.
Function block name: Name of the automatically generated function block (if nothing is entered the
name is the same as the library name).
Create zip-file: If the generated source code should not be integrated into an existing Automation Studio
on the developer's computer, there is also the possibility to export the generated source code into a zipfile and easily import the zipped task or function block into Automation Studio (File →Import...).
Zip-file path: Destination directory (absolute or relative path) for the automatically generated zip-file.
Add task to hardware configuration: Automatically add the generated task to the current hardware
configuration of the Automation Studio project.
Automation Studio configuration name: Name of the current Automation Studio configuration.
Automation Studio PLC name: Name of the current Automation Studio PLC.
Task class: Number of task class where the task should be assigned.
Change task class timing settings: Task class timing settings, of selected task class, are adapted
automatically.
Cycle time: Task class cycle time in seconds.
System-tick: System-tick in seconds.
34
TM140 - B&R Automation Studio Target for Simulink®
Configuration Settings
INFORMATION
For more information regarding task-class timing settings, see the section “Real-time Operating
System” in the Automation Studio help.
Create function block: Create an Automation Studio compatible function block instead of a task (see
section 4.2 "Function block generation")
IMPORTANT
The creation of function blocks using B&R Automation Studio Target for Simulink is intended for
discrete models only. For continuous time models, please use the Simulink® Model Discretizer
or contact the B&R support.
Figure 23: B&R Advanced Settings
Automation Studio package name: Name of the target Automation Studio project package (optional).
INFORMATION
Only one level of Automation Studio packages is supported by the Automatic Code Generation.
Control Packages are not supported.
Add .typ files: Include additional Automation Studio .typ files for support of structure variables (optional).
Add source files: Include additional source and header files (*.c, *.h. *.cpp, *.hpp) to the generated
task (optional).
Add include directories: Include additional include directories (optional).
TM140 - B&R Automation Studio Target for Simulink®
35
Configuration Settings
INFORMATION
If the generated task is moved to a different development system, please make sure to adapt
all include directories.
Additional compiler switches: Define additional compiler switches for the Automation Studio compilation (optional).
Version: Declare a version number for the generated task (optional).
Encryption password: Enter an encryption password for source files (*.c, *.cpp) in Automation Studio
(optional).
Enable continuous time support: Allow continuous time blocks in your Simulink model (not recommended for production use).
Enable expert mode: Enable all options to be set manually (only recommended for experienced users).
Enable logging: Create a log file of all warnings and errors during code generation (Simulink model
name + ‘.log’).
Enable automatic transfer: Automatically compile and transfer the generated task or function block to
the target system (see section 4.5 "Automatic transfer").
INFORMATION
Before using the ‘Automatic transfer’ feature make sure that the target Automation Studio
project can be compiled and transferred without any errors or warnings in Automation Studio
and that the connection to the target system is established.
Enable Simscape support: Enable support for Simulink models that use blocks from the Simscape
toolboxes like SimMechanics, etc..
Enable External Mode: Enable the External Mode feature described in section 4.6 "External Mode".
External Mode buffer memory size: Buffer size for use of External Mode (see section 4.6 "External
Mode"). Default value: 1000000
External Mode IP address: IP address for External Mode (see section 4.6 "External Mode"). Default
value: 127.0.0.1
External Mode Port: Port for External Mode (see section 4.6 "External Mode"). Default value: 17725
Enable model specific post processing: After finishing the code generation, a specific *.m file could
be called for a post processing routine.
Post processing file name: Name of the model specific post processing file. The file has to be in a
MATLAB known path.
Create global *.var file: Automatically generates model specific global .var and global .typ file.
Heap size for C++: Heap size required for C++ code generation. Default value: 6553500
36
TM140 - B&R Automation Studio Target for Simulink®
Configuration Settings
Figure 24: B&R Information
Getting started: Open this documentation in your pdf viewer.
Info: Show version information.
B&R Homepage: Link to B&R Homepage (http://www.br-automation.com/).
TM140 - B&R Automation Studio Target for Simulink®
37
Working with B&R Automation Studio Target for Simulink
4
Working with B&R Automation Studio Target for Simulink
4.1
Basic example
The following example clearly explains the use of the blocks introduced above and gives an introduction
about the first steps in connection with B&R Automation Studio Target for Simulink.
Example
The following introductory example illustrates, in simple steps, how an existing Simulink model
is prepared for Automatic Code Generation with B&R Automation Studio Target for Simulink.
•
•
•
•
Basic Simulink model
Interface and parameter blocks
Project and target configuration
Debugging
An introduction to Automation Studio, MATLAB and Simulink is not included in this training
module and is a prerequisite for working with the following excerpts.
4.1.1 The model: a simple algebraic system
The algebraic system displayed in Figure 25 serves as a basic structure for the following implementation
example. The two inputs a and b are added, multiplied by a constant factor k and copied to variable c.
Because basic knowledge of the use of MATLAB and Simulink is a prerequisite, the implementation of
the basic model will not be discussed here.
Figure 25: Basic Simulink model
38
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
4.1.2 Configuration settings: B&R Config block
Inserting the B&R Config block completes the first step towards Automatic Code Generation. By choosing
the ‘Code Generation (ERT based)’ or ‘Code Generation (GRT based)’ configuration set, all relevant
basic settings are prepared automatically.
Figure 26: Adding the B&R Config block
TM140 - B&R Automation Studio Target for Simulink®
39
Working with B&R Automation Studio Target for Simulink
4.1.3 Interfaces: B&R Input and Output block
In order to make the process variables accessible in Automation Studio and to allow communication
with other processes in the system application the corresponding external interfaces must be defined.
In the course of the Automatic Code Generation a variable is created in the target system for every B&R
Input and Output block.
Figure 27: Adding B&R Input blocks
As the interface blocks should be accessible for other Automation Studio tasks in our example we decide
to use global variables. Furthermore the process variables created on the target do not have scalar
values but represent arrays of three float values (0..2) in our example.
Figure 28: Adding a B&R Ouput block
40
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
In our example, variables a, b and c will have the following settings:
a:
Variable Name:
Variable Desciption:
Variable Scope:
Variable Data Type:
Initial Value:
Array Index:
a
first input
LOCAL
LREAL
1
2
b:
Variable Name:
Variable Desciption:
Variable Scope:
Variable Data Type:
Initial Value:
Array Index:
b
second input
LOCAL
LEAL
2
2
c:
Variable Name:
Variable Desciption:
Variable Scope:
Variable Data Type:
Initial Value:
Array Index:
c
first output
LOCAL
LREAL
0
2
4.1.4 Parameter configuration: B&R Parameter block
To make factor k accessible during operation, a B&R Parameter block must be inserted.
Figure 29: Adding B&R a Parameter block
TM140 - B&R Automation Studio Target for Simulink®
41
Working with B&R Automation Studio Target for Simulink
As described for the B&R Input and Output block, the settings Variable Description, Variable
Scope, Variable Data Type, Initial Value and Array Index also apply for the B&R Parameter
block:
k:
Variable Name:
Variable Description:
Variable Scope:
Variable Data Type:
Initial Value:
Array Index:
k
parameter
LOCAL
LREAL
10
2
4.1.5 Model settings: Automation Studio project path and sample time
To allow automatic integration of code produced from the model in an existing Automation Studio project,
the appropriate project settings have to be made. Firstly, enter the path to the corresponding Automation
Studio project followed by the Automation Studio task name and package name (optional).
INFORMATION
Only one single level of Automation Studio packages is supported by the Automatic Code Generation. Control Packages are not supported.
If the generated Automation Studio task should also be added to a certain hardware configuration, the
option ‘Add task to hardware configuration’ has to be selected and the Automation Studio configuration
name and the Automation Studio PLC name have to be entered correctly. This can be done manually or
by using the ‘Select hardware configuration’ button. It automatically searches for valid hardware configurations in the chosen Automation Studio project path. A popup appears and provides a list of available
hardware configurations. The selected hardware configuration will be filled into the Automation Studio
configuration name and he Automation Studio PLC name automatically.
In order to be able to handle time continuous3 Simulink models the option ‘Enable continuous time support’ must be activated. As in this basic model there are no time continuous Simulink blocks this option
is left disabled.
Enabling the expert mode allows the user to modify various additional settings and should only be activated by users who are familiar with Real-Time Workshop Embedded Coder.
The log file option can be used to record warnings and errors during code generation.
3
should not be used for generation of production code
42
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 30: B&R Automation Studio settings
INFORMATION
The configuration name and PLC name used, are available in the Configuration View of the
current Automation Studio project.
TM140 - B&R Automation Studio Target for Simulink®
43
Working with B&R Automation Studio Target for Simulink
Figure 31: Setting the sample time
IMPORTANT
If the fundamental sample time specified in Simulink does not match the duration of the cyclic
task class for the automatically generated task in Automation Studio, the task will be suspended.
4.1.6 Preparation: B&R Automation Studio libraries
In order to be able to run the automatically generated source code on the B&R target two B&R Automation
Studio libraries are required in the project: ‘brsystem’ and ‘sys_lib’. If these libraries are not yet part
of the project they need to be added.
Figure 32: Required B&R Automation Studio libraries
At this point all preparations are complete and Automatic Code Generation can be started.
44
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
4.1.7 Integration: Automatic code generation and project download
Once the above preparations have been successfully completed, you can start the Automatic Code
Generation by using the menu item Tools → Real-Time Workshop → Build Model… (Ctrl+B) or using
the corresponding button on the toolbar.
A message will appear in the MATLAB command window indicating that the code generation was successful. Then the automatically created source code can be compiled in Automation Studio and transferred to the target system.
Figure 33: Automatically integrated program code from the example program
TM140 - B&R Automation Studio Target for Simulink®
45
Working with B&R Automation Studio Target for Simulink
4.1.8 Variables: Global and local variable files
As mentioned before local variables are automatically registered in the corresponding variable file ‘local.var’. Global variable have to be registered manually. The automatically generated file ‘global.txt’ can
be used in order to declare global variables needed by the automatically generated task. The file contains all needed declarations and can be copied to the source directory of the Automation Studio project
and renamed to ‘global.var’ if no other global variables exist in the project.
Figure 34: Adding declaration for global variables
IMPORTANT
When manually declaring global variables in Automation Studio, the user must make sure that
the data type of the variable in the project matches the selected data type in the dialog field.
By opening the variable files for global and local process variables the correct declaration can be
checked.
Figure 35: Declaration of gloabl variables
Figure 36: Declaration of local variables
46
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
4.1.9 Physical view: Hardware assignment
The assignment of the generated task to your hardware configuration can be done automatically by
selecting the option ‘Add task to hardware configuration’.
Figure 37: Adding task to hardware configuration
INFORMATION
The generated task is added to the first cyclic task class on the target system by default.
If you choose to manually add the generated task to a cyclic task class of the hardware, it is important
to add the required ‘Additional build options’ and the required ‘Additional include directories’. All
necessary options can be found in the automatically generated text file ‘task_properties.txt’.
TM140 - B&R Automation Studio Target for Simulink®
47
Working with B&R Automation Studio Target for Simulink
Figure 38: Task property settings
INFORMATION
If the generated task is moved to a different development system, please make sure to adapt
all include directories.
48
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
4.1.10Debugging: B&R Automation Studio watch
The result of the Automatic Code Generation can be easily verified by opening the B&R Automation
Studio Watch window after the download to the target.
Figure 39: Automation Studio Watch window
4.2
Function block generation
Example
The following example shows the automatic generation of Automation Studio function blocks
with Automation Studio Target for Simulink. The automatically generated Automation Studio
library containing the function block is integrated into an existing project and can be used in any
programming language. Error handling can be directly implemented in Simulink (e.g. based on
Embedded MATLAB functions).
The example contains an algorithm that integrates the sum of the two inputs a and b and then divides
the result by d. In order to avoid division by zero an Embedded MATLAB function is included. In addition
an error number is generated as soon as a division by zero would occur.
TM140 - B&R Automation Studio Target for Simulink®
49
Working with B&R Automation Studio Target for Simulink
Figure 40: Basic example for function block generation
To enable function block generation instead of task generation, the ‘Create function block’ setting has
to be activated in the ‘B&R Basic Settings’ section of the model configuration parameters (Figure 41 ).
Figure 41: Enable 'Create function block' setting
50
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
IMPORTANT
The SIMULINK model name and the chosen library name for function block generation must
not be identical.
The automatic code generation process then automatically generates an Automation Studio library containing the corresponding function block.
Figure 42: Automatically generated Automation Studio library
Instances of the automatically generated function block can be used in any Automation Studio task.
TM140 - B&R Automation Studio Target for Simulink®
51
Working with B&R Automation Studio Target for Simulink
Figure 43: Relationship between the Simulink model configuration and the automatically generated Automation
Studio library
The function block can be called by the ‘ssMethodType’, which switch structure is located in the file
‘simulink_model_name.c’ (in this example ‘function_block_sample.c’).
Figure 44: Excerpt of the 'simulink_model_name.c' file
The ‘ssMethodType’ option is automatically added to the function block structure. It determines the current function call step.
•
•
•
52
SS_INITIALIZE … initialize function
SS_OUTPUT… cyclic update and output function
SS_TERMINATE … exit function
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 45: Excerpt of the 'simulink_model_name.c' file
TM140 - B&R Automation Studio Target for Simulink®
53
Working with B&R Automation Studio Target for Simulink
4.2.1 Reusable Code
IMPORTANT
The option to use reusable code is supported in MATLAB R2012b and higher.
The option reusable code of the configuration parameters adjustments is needed if the automatically
generated Automation Studio function block used multiple times in one or several Automation Studio
tasks. Therefore the possibility ‘Reusable Code’ for ‘Code interface packing’ has to be chosen. The option
‘Pass root-level I/O as’ provides the options ‘Structure reference’ and ‘Part of the model data structure’.
If multiple instances of function blocks which are automatically generated are used in one or more Automation Studio tasks then it becomes necessary to create a reusable code for the function block. This
requirement is fulfilled by choosing the option “Reusable function” in “Interface: Code Interface Packaging”. The option ‘Pass root-level I/O as’ consists of the following options:
Structure reference:
Part of the model data
structure:
Each structure is assigned to every function call in the generated library
function block code.
Just the instance of the function block called in the Automation Studio
task is assigned to every function call in the generated library function
block code.
Figure 46: Settings for Reusable Code in Code Generation → Interface
54
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
4.3
Structure interface blocks
Example
The following example shows the use of the structure interface blocks. To use structure variables for automatic code generation a corresponding Automation Studio .typ file has to be created. The type file can then be imported into Simulink and be used for the B&R Structure Input
and B&R Structure Output blocks.
In the first step a type file has to be exported from Automation Studio.
IMPORTANT
Type files should not be modified or deleted after the B&R Structure blocks have been inserted
in order to avoid inconsistencies.
Figure 47: Automation Studio type file
IMPORTANT
Large type files can significantly slow down the performance of the B&R Structure interface
blocks. It is recommended to split up large .typ files into several smaller files.
IMPORTANT
Reference variables are not supported!
TM140 - B&R Automation Studio Target for Simulink®
55
Working with B&R Automation Studio Target for Simulink
Figure 48: Basic sample model with structure interfaces
In Simulink the type file is imported using the ‘B&R Advanced Settings’ tab.
Figure 49: Add Automation Studio type files
56
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 50: Import Automation Studio type files
As soon as at least one valid type file has been added to the model, structure elements can be selected
in the B&R Structure interface mask.
TM140 - B&R Automation Studio Target for Simulink®
57
Working with B&R Automation Studio Target for Simulink
Figure 51: B&R Structure interface mask
The generated source code contains the interface to the corresponding structures selected in the block
mask.
58
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 52: Structure interface in Automation Studio
The type file itself is not automatically copied to the Automation Studio project by default. However, the
‘Add source files’ tab can be used to copy the file to the project without user interaction.
Figure 53: Automatically copy type files to the Automation Studio project
TM140 - B&R Automation Studio Target for Simulink®
59
Working with B&R Automation Studio Target for Simulink
4.4
Bus interface blocks
Example
The following example shows the use of the Simulink bus interface blocks. To use Simulink bus
objects for automatic code generation the bus objects have to be created with the Simulink Bus
Editor. The Automation Studio type file is then automatically generated or updated.
In the first step a bus object has to be created (enter “buseditor” in the MATLAB prompt).
Important
Simulink types like fixed point (fixdt) and enumeration (enum) are not supported as data types.
Figure 54: Simulink Bus Editor
60
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 55: Basic sample model with bus interfaces
In the block mask the required Simulink bus element has to be entered into the field “Variable Bus Name”.
Figure 56: B&R Bus Input mask
To get the required elements of the bus object a Bus Selector block has to be used and configured.
TM140 - B&R Automation Studio Target for Simulink®
61
Working with B&R Automation Studio Target for Simulink
Figure 57: Simulink bus selector block
To create a signal for the Bus Output block a Bus Creator block has to be used and configured with
the right bus object. To ensure that the right signals are connected to the Bus Creator the input signals
should be named and connected in the right order.
Figure 58: Configuration of a Bus Creator
62
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
IMPORTANT
Only when bus signals to the Bus Creator ports are named will they appear in the list of “Signals
in the bus” in the Bus Creator mask.
In the block mask the required Simulink bus element has to be entered in the field “Variable Bus Name”.
Figure 59: B&R Bus Output mask
TM140 - B&R Automation Studio Target for Simulink®
63
Working with B&R Automation Studio Target for Simulink
4.5
Automatic transfer
With the ‘Enable automatic transfer’ option being enabled the generated program is included into the
Automation Studio project, the entire project is compiled and then transferred to the target system automatically.
IMPORTANT
For connection to target hardware, it is necessary to tick the option “Use in active config” in
the Automation Studio → Online → Settings.
INFORMATION
In order to be able to use the automatic transfer feature the automatically generated program
has to be assigned to hardware configuration on the target system (see section 3 "Configuration
Settings").
Figure 60: Automatic transfer setting
64
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 61: Compilation and download to the target system in MATLAB
4.5.1 Change Automation Studio path for Automation Studio Target for Simulink
For the first use of Automation Studio Target for Simulink or if the version of Automation Studio is changed
it is necessary to change the target path. This can be achieved by the following commands in MATLAB
Command window.
To get Automation Studio path:
1
bur_getaspath
To reset Automation Studio path:
1
2
bur_getaspath(‘reset’)
Enter
To set new Automation Studio path
1
2
bur_getaspath(‘reset’)
Enter new path (e.g.: C:\BrAutomation\AS40 )
TM140 - B&R Automation Studio Target for Simulink®
65
Working with B&R Automation Studio Target for Simulink
4.6
External Mode
IMPORTANT
For use of the External Mode feature the Automation Studio libraries ‘AsArLog’ and ‘AsTCP’
need to be part of the project.
The External Mode feature allows the developer to connect to the target and debug automatically generated programs directly from Simulink. Therefore values on the target system can be directly displayed in
Simulink (e.g. using a ‘Scope’ or a ‘Display’ block) and parameters on the target system can be modified
from Simulink (e.g. ‘Gain’ or ‘Constant’ blocks).
INFORMATION
When enabling the External Mode option, additional source code will be generated and run
on the target system. Therefore using External Mode is not recommended for generating production code.
For further information see the corresponding sections in the MathWorks Real-Time Workshop
documentation or Embedded Coder documentation.
To be able to use the External Mode feature the corresponding option has to be checked on the settings
page. The buffer memory size, IP-address and port number can also be set in the ‘B&R Advanced
Settings’ section.
Figure 62: External Mode settings
To change block parameters on the target system during execution the ‘Inline parameters’ option in the
Optimization → Signals and Parameters has to be enabled.
66
TM140 - B&R Automation Studio Target for Simulink®
Working with B&R Automation Studio Target for Simulink
Figure 63: Inline parameters option
After downloading the generated program, the values and parameters can directly be accessed from
Simulink, as soon as a connection has been established between Simulink and the target (for detailed
information see External Mode section in the MathWorks Real-Time Workshop documentation or Embedded Coder documentation).
Once the generated task is running on the target system, select the 'Simulation Mode: External' as
shown in the red box below. Alternately choose this option from the menu 'Simulation: Mode: External'.
Then click on the icon “Connect to Target” as shown in the green box below.
Figure 64: External Mode connection in Simulink
Alternately, the External mode Control Panel offers the user the possibility to start the External mode
using the 'Connect' button.
TM140 - B&R Automation Studio Target for Simulink®
67
Working with B&R Automation Studio Target for Simulink
Figure 65: External Mode - Opening the External Mode Control Panel
If values of variables used in the model are changed, the checkbox ‘Batch down’ has to be selected.
By using the button ‘Download’ the new values are downloaded to the target system.
Figure 66: External Mode Control Panel
68
TM140 - B&R Automation Studio Target for Simulink®
Examples
5
Examples
The following examples provide a small overview of the extensive possibilities for using B&R Automation
Studio Target for Simulink in the field of automation technology. In the first example the fast and easy
implementation of a simple discrete-time PID controller is demonstrated. In the second part a Hardware-in-the-Loop application representing a simulation model of a temperature system is realized
using B&R Automation Studio Target for Simulink. In the last example a second Hardware-in-the-Loop
system modelling a hydraulic valve and cylinder is shown.
5.1
PID controller
Example
Using Simulink it is easy to implement a simple PID controller. After the control deviation has
been calculated from the set and actual values, the equations listed below are used to calculate
the manipulated variable directly on the controller's output. All that is needed to install the controller on the target system is to add the B&R blocks described before and start the Automatic
Code Generation.
Figure 67: PID controller
The control concept for the PID controller is
with:
Since the controller code is executed on the target system in equidistant time cycles, it is recommended
to ensure that all integrator and differentiator blocks are also discrete-time.
TM140 - B&R Automation Studio Target for Simulink®
69
Examples
5.2
Temperature model
In order to properly test the controller created in section 5.1 "PID controller" without having to have a
real system at hand, a simplified model of a temperature system can be created and transferred to the
target system using B&R Automation Studio Target for Simulink in only a few steps.
Example
The system is based on the following mathematical model:
The simulation model is enhanced with a white noise block representing the measurement
process as well as the quantization to tenths of a degree by the sensor. All system parameters,
like the ambient temperature for instance , are accessible as local parameters.
Figure 68: Temperature System
Since the simulation model is a continuous-time model, support for continuous-time systems has to be
enabled.
70
TM140 - B&R Automation Studio Target for Simulink®
Examples
Figure 69: Settings for continuous-time Simulink models
In order to be able to run the continuous-time system on the target system with fixed equidistant steps,
a fixed step solver (e.g. ode1 - Euler) must be selected.
Figure 70: Fixed step solver
TM140 - B&R Automation Studio Target for Simulink®
71
Examples
Or, alternatively, the system can be converted to an adequate discrete-time system. This can be done either manually using transformations that are described in detail in the corresponding literature, or by using the ‘Model Discretizer’ provided with the Simulink toolboxes Control System Toolbox and Simulink®
Control Design. It can be found under Tools → Control Design → Model Discretizer (see section 7.1
"Real-Time Workshop Embedded Coder or Simulink Coder Embedded Coder").
Figure 71: Model Discretizer
72
TM140 - B&R Automation Studio Target for Simulink®
Examples
5.3
Hydraulics applications
In Figure 72 a Simulink model of a linear hydraulic actuator is depicted. It consists of the hydraulic
servo valve and the hydraulic cylinder. The servo valve is modelled via a nonlinear curve, describing the
dependence of its opening with respect to the voltage, and its non-linear hydraulic resistance, i.e. the
relationship between pressure drop across the valve and volume flow. The hydraulic cylinder is described
via four differential equations, two for the pressure build-up in the two cylinder chambers and two for
the mechanical movement. For the implementation of the respective equations Embedded MATLAB
Function blocks are used. The model includes friction and leakage of cylinder and valve. In addition, the
cylinder has two end positions with modelled damping. The differential equations are discretized, thus
for simulation and code generation a discrete solver can be used. The inputs to the model are the valve
voltage and an external load force acting on the cylinder. Further, the model has many parameters, e.g.
for the geometric dimensions of the cylinder, the leakage and friction coefficients and the nominal values
of the valve, which are accessible via parameter blocks. The model outputs are the states of the system,
i.e. the two chamber pressures, the cylinder position and speed. The sensors are modelled in so far, as
the physical values of the signals are scaled to the corresponding sensor outputs.
The Simulink model represents a typical valve controlled hydraulic drive application, and is used for
hardware-in-the-loop tests of hydraulic controllers and hydraulic control trainings.
Figure 72: Application of a hydraulic drive including servo valve and cylinder
TM140 - B&R Automation Studio Target for Simulink®
73
Examples
5.4
Inverted Pendulum on a Cart Model
In this section the model of an inverted pendulum is considered and linear as well as non-linear closedloop controllers are designed with the help of MATLAB. The design is verified using simulation in Simulink
and based on these simulations the real-time code is generated for hardware-in-the-loop testing and
rapid prototyping.
Figure 73: Application sketch of the inverted pendulum
Figure 73 shows a sketch of an inverted-pendulum-on-a-cart model. It consists of a cart of mass M which
is driven by a motor generating an input force F acting on the cart. A pendulum of length l can rotate
freely about the point P of the cart. At the tip of the pendulum a mass m is mounted. The position of the
cart is denoted by x, the displacement angle of the pendulum by θ (with respect to the vertical position).
Typical benchmark control problems for this model are:
•
•
•
the closed loop control of the vertical (unstable) position of the pendulum,
the swing up of the pendulum,
the positioning of the cart with the attached pendulum.
The equations of motion have the form
with
the non-linear system can be described as a system of explicit differential equations
74
TM140 - B&R Automation Studio Target for Simulink®
Examples
Please refer to the Simulink file ‘pend_mod_nlin.mdl’ for a simulation of the non-linear model without
controller.
5.4.1 Linear controller design for the linearized pendulum system
In order to design a controller for the unstable (upper) equilibrium position of the pendulum the linearized
equations of motion are of interest. The linearization at the equilibrium point
results in a model of the form
with
Because this is the linearization about the unstable equilibrium position (pendulum up), the Eigenvalues
of the linear model are unstable. As the controller is intended to run in discrete time, the model is discretized using the MATLAB command ‘c2d’ to derive the system matrices of a discrete time system of
the form
For this discrete time linear model a linear state regulator of the form
is designed.
Using the MATLAB command ‘acker’ the Formula of Ackermann is applied to directly assign the desired
Eigenvalues to the closed loop system.
With the Linear Quadratic Regulator design (MATLAB command ‘lqr’) an objective function is minimized
to derive the optimal controller in the sense of the provided weights.
TM140 - B&R Automation Studio Target for Simulink®
75
Examples
The MATLAB script ‘init_pend_ctrllin.m’ does the mentioned derivations and initializes the respective
simulation parameters. The Simulink model ‘pend_ctrl_lin.mdl’ shows a simulation of controller and linearized model. Figure 74 shows the simulation model and Figure 75 a step response of the controlled
pendulum on a cart. The initial condition of the pendulum is set to θ = 0.01rad and at t = 4s a reference
step of 0.1m is applied.
Figure 74: Simulation model of pendulum and controller
76
TM140 - B&R Automation Studio Target for Simulink®
Examples
Figure 75: Step response of pendulum and controller
Remark: For some applications the controller is directly calculated for the continuous-time linear model
although it runs in discrete time. However, this can lead to undesired behaviour of the controlled system,
especially when the sample time is increased.
5.4.2 The inverted-pendulum-on-a-cart model library
The library ‘pend_lib.mdl’ (Figure 76) represents a collection of different modeling examples for the linear
as well as for the non-linear inverted pendulum model, to visualize how the same modeling goal can be
reached, using Simulink with different modeling approaches.
In addition, the model ‘inverted_pendulum_IOs does not provide a physical model, but the inputs and
outputs of the controller.
The configurable subsystem ‘Inverted Pendulum (Simulation / IOs)’ incorporates all different modeling
blocks and provides the user with the possibility to select on demand which model this subsystem represents. In the following sections it will be shown, how with the help of this configurable subsystem the
same simulation model which is used for simulating the controller with model in Simulink can easily be
used for code-generation.
Similar facts hold for the section ‘Inputs’ and ‘Controller’. Again a configurable subsystem incorporates
two different forms of inputs and controllers, respectively, which can be selected within the simulation
model on demand.
TM140 - B&R Automation Studio Target for Simulink®
77
Examples
Figure 76: Inverted Pendulum library showing different modeling features and configurable subsystems
Figure 77 shows the Simulink model ‘pend_ctrl_lin.mdl’ using the configurable blocks ‘Inverted Pendulum
(Simulation / IOs)’ and ‘Reference Input’.
Figure 77: Simulink model with configureable subsystems ‘Reference Input’ and ‘Inverted Pendulum’
78
TM140 - B&R Automation Studio Target for Simulink®
Examples
5.4.3 Code generation of the state feedback controller
To prepare the model for code generation of the tested controller, in the ‘B&R Config’ block the corresponding ‘Code Generation’ choice has to be made.
Figure 78: ‘B&R Config’ dialog
As the Simulink model ‘pend_ctrl_lin.mdl’ uses the configurable blocks ‘Inverted Pendulum (Simulation/IOs)’ and ‘Reference Input’, with the control ‘Block Choice’ in the context menu these two blocks
can be configured to represent the desired choice. With the choice ‘BR reference input’ for the block
‘Reference Input’ and ‘inverted pendulum IOs’ for the block ‘Inverted Pendulum’ the entire simulation is
configured in such a way, that just the code of the controller is left and the interface to the other system
parts (like reference input and plan) is given with the respective ‘B&R Input’ and ‘B&R Output’ blocks.
5.4.4 Hardware-in-the-Loop Simulation
For hardware-in-the-loop simulations of the controller against a simulation model running also on a target
system (see Section 1.2.2 "Hardware-in-the-Loop") it is necessary to generate real-time code from the
simulation model. The respective setup is prepared in the model ‘pend_mod_nlin.mdl’ in which the inputs
and outputs of the (exact, non-linear) simulation model are connected to ‘B&R Inputs’ and ‘B&R Outputs’
respectively.
By setting in the ‘B&R Config’ block the option to ‘Code generation’ and adding the desired values in
the ‘Real Time Workshop → Options’ dialog this model can be generated and represents a task in an
Automation Studio project.
The sample Automation Studio project ‘PenPrj’ contains the task ‘pend_mod’, which represents the simulation model of the pendulum and the task ‘pend_ctr’ which contains the controller in the two packages
‘Controller’ and ‘Model’ (see Figure 79). The global variables are used for interfacing these two tasks
when running on the target system.
TM140 - B&R Automation Studio Target for Simulink®
79
Examples
Figure 79: Automation Studio project with controller and model tasks
Via the Automation Studio diagnostic tools like ‘Watch’ and ‘Trace’, the correct operation of the controller
which controls the upper (unstable) equilibrium position of the pendulum can be verified. Figure 80 shows
a step response of the setup to a reference step of 0.1m.
Figure 80: Step response in Automation Studio Trace
5.4.5 Code generation of function blocks
Another feature of B&R Automation Studio Target for Simulink is the generation of function blocks for Automation Studio (see section 4.2 "Function block generation"). In the example which follows it is shown,
how a function block can be designed and how the generation of function block error codes can be
included in the simulation model.
Figure 81 shows the model ‚pend_ctrl_fun.mdl’ which contains the Embedded MATLAB function ‘State
feedback controller with error handling’. Besides the interface variables ‘ref’, ‘u’ and ‘x’ this block has
several additional inputs used for the parameterization of the corresponding limits. If one of these limits
is exceeded, the block responds either with a warning (control is continued) or an error (controller output
u is set to zero), respectively. This behavior can, of course, be simulated in Simulink.
80
TM140 - B&R Automation Studio Target for Simulink®
Examples
By switching the ‘B&R Config’ parameters to ‘Code Generation’, selecting the ‘B&R reference input’
and the ‘inverted_pendulum_IOs’ option in the two configurable subsystems and adapting the desired
options in the ‘Real Time Workshop → Options’ dialog a function block can be generated having the
interface depicted in Automation Studio project with the generated function block.
Figure 81: Simulink model ‘pend_ctrl_fun.mdl’
Figure 82: Automation Studio project with the generated function block
By using an instance of the function block ‘pctr_fun’ in the task ‘ctrl_tsk’, connecting inputs and outputs
of the instance to the plant signals and to the reference signal, again plant and controller can be realized
in a ‘hardware-in-the-loop’ setup.
TM140 - B&R Automation Studio Target for Simulink®
81
Examples
Figure 83: Structured Text task using the automatically generated function
block
Figure 84: Software configuration including the controller task ‘ctrl_tsk’ and the modeling task ‘pend_mod’ for
‘hardware-in-the-loop’ testing
5.4.6 Swing Up control of the pendulum
The model ‘pend_ctrl_swingup.mdl’ shows one possibility for swing-up control of the pendulum. The
MATLAB Stateflow Toolbox is used to switch between a swing-up control algorithm and the stabilizing
linear controller for the upper (unstable) equilibrium position of the pendulum.
5.4.7 Using the MATLAB OPC Toolbox for Online-MATLAB/Simulink debugging
Please contact B&R for examples on this topic.
82
TM140 - B&R Automation Studio Target for Simulink®
Examples
5.5
Simscape applications
Figure 85 shows the “Simple Mechanical System” example provided by MATLAB for the Simscape ™
adapted with B&R library blocks. More details about the model itself can be found under:
http://www.mathworks.com/help/physmod/simscape/examples/simple-mechanical-system.html
Figure 85: Simulink model ‘demo_simscape.mdl’
The B&R Automation Studio Target for Simulink offers the possibility to generate C4 or C++ code from
these models.
Figure 86: Config Set for ‘demo_simscape.mdl’
On the B&R Advanced Settings Tab under Configuration Parameters, the option, “Enable Simscape
support” provides a means for easy code generation and inclusion into Automation Studio.
TM140 - B&R Automation Studio Target for Simulink®
83
Examples
Figure 87: Enable Simscape option under B&R Advanced Settings
Figure 88: Solver setting for ‘demo_simscape.mdl’
When this option is selected, the “Enable continuous time support” is also activated. The default solver
selected when this option is selected is “ode14x”, because typically the models containing Simscape
components are stiff.
84
TM140 - B&R Automation Studio Target for Simulink®
Examples
The source files which are affiliated to the Simscape toolbox are precompiled and available as an archive
file which is automatically added to the generated code. The corresponding header files are available
under SSC_includes package, which is also downloaded with the generated code to Automation Studio.
Figure 89: Logical view of task generated from model ‘demo_simscape.mdl’
The package “SSC_includes” is added to “Additional include directories” under the section “Compiler”
of task properties.
TM140 - B&R Automation Studio Target for Simulink®
85
Examples
Figure 90: Additional include for model 'demo_simscape.mdl'
These settings are assigned if the task is automatically added to the Hardware Configuration. For manual
assignment, please refer to the “simscape_properties.txt” provided within the demo folder for this demo
model.
Please contact B&R for examples on this topic.
86
TM140 - B&R Automation Studio Target for Simulink®
Examples
5.6
Data logger
This model shows a simple plant (PT1, e.g. Raising temperature of a heater) with two additional logging
features:
1.
Creation of B&R PLC- Logger Entries
Corresponding blocks are available to specify if the logger entry is an error, warning or information. These blocks are separated into different subsystems depending on their message level. Every time one of the LoggerEntry blocks gets triggered the specified entry is created in the
PLC's Logger. In this demo model the triggers of all LoggerEntry blocks are bound to the output
value of the plant.
2.
Writing Data into a log-file located on the PLC
The Block B&R Datalogger illustrates how to create a file on the PLC and write data into it. The
data which is connected to the data port is written into the specified file as long as the trigger input is set to 1. In this model the output value is written into the file whenever it has a positive value.
Figure 91: Sample 8 Datalogger
IMPORTANT
Before code generation, it is needed to add the two additional libraries ‘TMTFileLog’ and
‘TMTLgEntry’ from the Demo folder (‘Automation Studio Libraries’ folder) to the Automation
Studio project (see section 5.6.1 "Add TMTFileLog and TMTLGEntry to Automation Studio
project").
TM140 - B&R Automation Studio Target for Simulink®
87
Examples
5.6.1 Add TMTFileLog and TMTLGEntry to Automation Studio project
Figure 92: Add a library to an Automation Studio project -Step 1
Figure 93: Add a library to an Automation Studio project - Step 2
88
TM140 - B&R Automation Studio Target for Simulink®
Examples
Figure 94: TMTFileLog and TMTLgEntry are added to the libraries folder
Furthermore the file libarsystem.a has to be added to the library TMTFileLog. The file is available in
the Demo folder in the folder ‘Automation Studio Libraries’. Right click on the library itself and select
“Add Object”.
TM140 - B&R Automation Studio Target for Simulink®
89
Examples
Figure 95: Adding the file libarsystem.a to the library TMTFileLog
90
TM140 - B&R Automation Studio Target for Simulink®
Examples
Figure 96: Choose the option existing file
Figure 97: Added file libarsystem.a
As a final step, the string “arsystem” has to be added to the property ‘Library archive files’ of the
TMTFileLog library. Change to the Physical View and open the software configuration.
TM140 - B&R Automation Studio Target for Simulink®
91
Examples
Figure 98: Open the properties of the library TMTFileLog
Figure 99: Add the string 'arsystem' to the library archive files
92
TM140 - B&R Automation Studio Target for Simulink®
Examples
5.7
Library FB PID
This model shows how to implement a library function block, which adheres to the library development
guidelines of B&R. In this function block a simple PID controller is realized.
Figure 100: Sample 9 Library FUB
Before code generation, the option ‘Create function block’ and ‘Create B&R library compliant function
block’ in the B&R configuration parameters have to be chosen.
Figure 101: Configuration parameter settings
TM140 - B&R Automation Studio Target for Simulink®
93
Examples
5.8
Inverted Pendulum with Model Referencing
In this section, the use of model referencing is shown. The idea of model referencing is to include one or
more models into another model as a block by using Model blocks. During simulation or code generation,
the Model blocks get replaced by the precompiled real models. Simulink offers the two Model blocks
‘Model’ and ‘Model Variants’ for the feature model referencing. The difference between the two blocks
is that with ‘Model Variants’ the user can set various references to other models. Afterwards the user
can switch between the various referenced models. The block ‘Model’ can just set one model reference.
For more information see the MATLAB help.
Figure 102: The two Model blocks ‚Model‘ and ‚Model Variants‘ for using model referencing in Simulink
This example is based on the Inverted Pendulum example in section 5.4 "Inverted Pendulum on a Cart
Model", where linear as well as non-linear closed-loop controllers are designed with the help of Simulink.
IMPORTANT
To use model referencing with code generation / simulation all models must have the same
configuration settings. Therefore a referenced configuration setting can be created. If a referenced configuration is used, only the top-level model needs a B&R Config Block. Otherwise
every model needs its own B&R Config Block.
Figure 103 shows the structure of this example, which illustrates a three level structure of model referencing. The linear and non-linear closed loop controllers are located in individual models, which are then
added to the top-level model as a reference. Within the closed loop models the user can choose between
different realisations of the inverted pendulum. Furthermore, the linear closed-loop controller offers two
different models for the state feedback controller.
94
TM140 - B&R Automation Studio Target for Simulink®
Examples
Figure 103: Hierarchy of the example Inverted Pendulum with model variant references
5.8.1 How to create a referenced configuration?
Step 1:
Add a B&R Config Block to the top-level model and choose a configuration for the model.
Step 2:
Open the Model Explorer (View->Model Explorer).
Figure 104: Model Explorer Window
TM140 - B&R Automation Studio Target for Simulink®
95
Examples
Step 3:
Choose the top-level model in the Model Hierarchy. Right click on the active configuration
and choose ‘Export’. Save the configuration as a *.mat file.
Figure 105: Export of a configuration setting.
step 4:
Choose the top-level model in the Model Hierarchy. Right click on the active configuration
and choose “Convert to Configuration Reference”. Enter the name of the *.mat file name into edit field of the newly opened window.
Figure 106: Convert a model configuration to a referenced model
configuration.
Step 5:
96
Choose the top-level model in the Model Hierarchy, right click and choose “Propagate
to Referenced Models”. A new window opens in which the button Propagate has to be
pressed. Afterwards every new setting in the top-level model configuration will be propagated to all referenced models automatically.
TM140 - B&R Automation Studio Target for Simulink®
Examples
Figure 107: Propagate the referenced model configuration to all active submodels.
After the first click of the build button the following MATLAB warning message may occur. Close
the message, save all submodels and the top-level model and press the build button again.
TM140 - B&R Automation Studio Target for Simulink®
97
Examples
5.8.1.1
Important model configuration settings for this example
1. Code Configuration- Interface option
Figure 108: Set of the configuration parameters in the Code Configuration – Interface option.
2. Model Referencing
Figure 109: Set of the configuration parameters in the Model Referencing option.
3. Diagnostics
Figure 110: Set of the configuration parameters Diagnostics.
98
TM140 - B&R Automation Studio Target for Simulink®
Examples
5.8.2 Hierarchy of the top-level model and the submodels
To see the hierarchy and the dependency of the referenced models and libraries the Dependency Viewer
of MATLAB can be used. The viewer can be found in the menu bar under Analysis → Model Dependencies → Model Dependency Viewer → Models & Libraries / Models Only. The Dependency Viewer just
shows dependencies of the current chosen models.
Figure 111: Open the Model Dependency Viewer.
Figure 112: MATLAB created overview of the models and libraries
dependencies.
Figure 113: MATLAB created overview of the model dependencies.
TM140 - B&R Automation Studio Target for Simulink®
99
Automation Studio target tools
6
Automation Studio target tools
6.1
Trace Viewer
The TraceViewer is an additional tool of the B&R Automation Studio Target for Simulink. It provides a fast
and easy way to display data in MATLAB which is recorded by the diagnostic tool “Trace” in Automation
Studio. It provides feedback-functions from generated code and Automations Studio to MATLAB to establish better workflow. TraceViewer is available as a MATLAB function for MATLAB versions before
2012a and as an “App” for MATLAB versions 2012b and newer.
6.1.1 Installation
The installation file of the Trace Viewer is located in the installed AS Target for Simulink folder under
the folder ‘burtools’.
Figure 114: Folderstructure of the AS Target for Simulink installation
To install the Trace Viewer this path has to be opened in the MATLAB Version and the ‘TraceViewer.mlappinstall’ has to be started.
100 TM140 - B&R Automation Studio Target for Simulink®
Automation Studio target tools
Figure 115: Run the installation of the Trace Viewer
The Trace Viewer is now integrated into the MATLAB Apps.
Figure 116: The Trace Viewer is an element of the MATLAB Apps
6.1.2 Design
The graphical user interface of TraceViewer is divided into following main areas:
1. Menu
The menu contains the main and common functionalities which are needed to open, add and close data
and usual commands which are also available in the standard figure provided by MATLAB.
2. Chart area
This area is used to display diagrams of the currently chosen data. Its format can be changed in the
configuration block.
3. Config & Data area
This area is positioned on the left side of the GUI and contains the Chart area configuration and the
currently loaded data
4. Status bar
The status bar gives information about the current status, the last actions or error occurrences.
TM140 - B&R Automation Studio Target for Simulink® 101
Automation Studio target tools
Figure 117: User interface of the Trace Viewer
6.1.3 Features
With the TraceViewer, data formatted as a .csv or .txt (MATLAB formatted) files can be displayed graphically. These files can be exported with the Automation Studio tool Trace.
6.1.3.1
Menu
The menu of TraceViewer consists of all commonly used functions. Options include all standard options
provided in a standard figure of MATLAB. The available options are: (from left to right)
Element
Open file
Function
•
•
Opens a dialog to select and open one or more supported files. The data
is read and displayed in the diagram.
All currently loaded data is discarded.
Add file
•
Provides the same functionality like the open button retaining all current
data and adding new data.
Import data from
workspace
•
Opens a dialog to import data from the workspace of MATLAB.
Clear
•
•
Deletes all currently loaded data.
The plot configuration is not affected.
Load Trace Viewer
Configuration
•
Opens a dialog to load a TraceViewer plot configuration which was previously saved with TraceViewer to a .mat file.
102 TM140 - B&R Automation Studio Target for Simulink®
Automation Studio target tools
Element
6.1.3.2
Function
Save Trace Viewer
Configuration
•
Opens a dialog to save the current plot configuration of TraceViewer into
a .mat file. A plot-configuration consists of the amount and orientation of
the plots, title of diagram, names, min, max and scale of the axes.
Zoom In
•
•
Standard MATLAB function.
Provides zoom-in functionality in diagrams.
Zoom Out
•
•
Standard MATLAB function.
Provides zoom-out functionality in diagrams.
Move
•
•
Standard MATLAB function.
Provides movement of data within the plots.
Data Cursor
•
•
Standard MATLAB function.
Adds one datatip for every dataset displayed on all diagrams.
Legend on/off
•
•
Standard MATLAB function.
Toggle Legend (on/off).
Grid on/off
•
•
Standard MATLAB function.
Toggle grid (on/off).
Link X
•
Links the x-axis of all diagrams displayed on TraceViewer.
Link Y
•
Links the y-axis of all diagrams displayed on TraceViewer.
Help
•
Navigation to B&R Homepage for further information.
Configuration
In the configuration area, the number and alignment of the diagrams can be adjusted by two increment-decrement buttons. The preview displays the new configuration. With the button “adjust”, the preview configuration gets transferred to the chart-area. An adjustment of the chart-area forces it to redraw
all diagrams and displayed data on it.
Figure 118: Trace Viewer configurations options
TM140 - B&R Automation Studio Target for Simulink® 103
Automation Studio target tools
6.1.3.3
Options for data
All loaded data is displayed below the configuration. Newly added data will always be displayed on the
first top-left diagram. A checkbox indicates whether a dataset is drawn in the actual configuration or not.
The diagram where a dataset is displayed can be chosen by the popup next to its name. The color of
the name corresponds to the color of the drawn line on a diagram.
A right-click on the name of a dataset opens a context menu with the following functions:
Element
6.1.3.4
Function
Change Name
•
Opens a dialog to change the name of the dataset.
Change Color
•
Opens a standard MATLAB dialog for changing the color of the dataset.
Line Style
•
Changes the style of a line.
Thickness
•
Changes the thickness of the drawn data inside 1 to 9.
Export
•
Exports current dataset to MATLAB workspace (signal formatted).
Export all
•
•
Exports all datasets to MATLAB workspace (signal formatted).
Selection of datasets possible.
Chart-area
The chart-area comprises of all diagrams to display data. The amount of diagrams and its orientations
can be adjusted in the configuration area (see 6.1.3.2 "Configuration"). Every diagram has its own context
menu which can be accessed by right-clicking on them (Note: A right-click on one of the lines will open
the context menu of the line instead of the diagram). The context menu provides following functionalities:
Element
Function
Set plot title
•
Change title of diagram.
Set x-axis label
•
Change label of x-axis.
Set y-axis label
•
Change label of y axis.
Set x-axis limits
•
Change limits of x-axis.
Set y-axis limits
•
Change limits of y-axis.
Set axis-limits to
auto
•
Change axis limits to auto (default).
Export to workspace
•
•
Export one or all datasets which are displayed on this diagram.
Selection of datasets possible.
Export to file
•
•
Opens a Dialog to save the selected diagram to an image.
Type and location can be chosen.
Export all to file
•
•
Opens a dialog to save all diagrams to one output file (image).
Type and location can be chosen.
104 TM140 - B&R Automation Studio Target for Simulink®
Appendix
7
Appendix
7.1
Real-Time Workshop Embedded Coder or Simulink Coder Embedded Coder
Real-Time Workshop Embedded Coder or Simulink Coder Embedded Coder is not mandatory for the use
of B&R Automation Studio Target for Simulink. However, it is recommended to generate more efficient
and optimized source code for your target system.
For detailed information regarding Real-Time Workshop Embedded Coder or Simulink Coder Embedded Coder please visit http://www.mathworks.com/products/simulink-coder or contact MathWorks directly.
7.2
Simulink block support
Nearly all standard Simulink blocks are supported by the Automatic Code Generation. You can get an
overview in MATLAB using the command showblockdatatypetable. However, it is not recommended to
use continuous-time blocks for industrial applications. These blocks should be replaced by corresponding
discrete-time blocks. One option is to use the Model Discretizer mentioned in section 5.2 "Temperature
model".
More detailed information regarding the company MathWorks, Inc. can be found at http://www.mathworks.com/products.
B&R cannot guarantee problem-free implementation of blocks other than the standard Simulink blocks.
In this case it is recommended to contact the MathWorks support to inquire whether a particular block
is supported by the products Real-Time Workshop and Real-Time Workshop Embedded Coder.
Technical support: http://www.mathworks.com/contact_TS.html
7.3
Suggestions
7.3.1 Recommended blocks
•
•
•
•
Simulink standard blocks
Time-Discrete blocks
Embedded MATLAB functions
Inlined s-functions
7.3.2 Supported but not recommended for production code
•
•
•
Time continuous blocks
Large look-up tables
Non-inlined s-functions
7.3.3 Not supported blocks
•
•
Level-2 m-files, s-functions
MATLAB functions
TM140 - B&R Automation Studio Target for Simulink® 105
Appendix
7.3.4 Further suggestions
The B&R Parameter block only works for tuneable block parameters. For non-tuneable blocks the parameter value will be directly used for the generated source code. If you want to make sure that the block
parameter is accessible during runtime, it is strongly recommended to use B&R Input blocks instead of
B&R Parameter blocks.
7.4
Additional links
The following links will take you to MathWorks’ corporate website. B&R can therefore not make any
guarantees regarding the site's content. Any questions should be directed to the MathWorks support.
Contact information: MathWorks
For questions regarding MathWorks products, you can find the appropriate contact information here:
http://www.mathworks.de/company/aboutus/contact_us/
To contact the technical support department for MathWorks (for customers with a valid maintenance
contract), it is recommended to use a ‘MathWorks Account’ – free registration at http://www.mathworks.com/accesslogin/createProfile.do – since this is the only way to submit an online query regarding
the current processing status: http://www.mathworks.com/accesslogin/createProfile.do
Using Simulink with B&R Automation Studio
MathWorks web portal summarizing latest news and application reports on ‘Automation Studio Target
for Simulink’ and other interface products between Simulink and Automation Studio http://www.mathworks.com/br
Industrial automation and industrial machines
Industry-specific portal with access to the most important sources of information regarding implementation of MATLAB & Simulink, including user reports, book program and event calendar. http://www.mathworks.com/industries/iam/
Tech notes / How-to guides
Tips & tricks for MATLAB & Simulink. http://www.mathworks.com/support/tech-notes/list_all.html
MATLAB Central
Public exchange platform for MATLAB & Simulink users, including exchange for files and links as well
as access to the public MATLAB http://www.mathworks.com/matlabcentral/
Product documentation
Online access to the complete HTML & PDF documentation for the current MATLAB release. Note: Access to individual product documentations, such as ‘Real-TimeWorkshop Embedded Coder’, requires a
‘MathWorks Account’ – free registration at http://www.mathworks.com/accesslogin/createProfile.do
106 TM140 - B&R Automation Studio Target for Simulink®
Appendix
MATLAB tutorial
Online tutorial for introduction to MATLAB. http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
Simulink tutorial
Online tutorial for introduction to Simulink. http://www.mathworks.com/academia/student_center/tutorials/index.html?link=body#
MathWorks Newsletter
Access to MathWorks’ newsletter ‘News&Notes’ as well as other publications. http://www.mathworks.com/company/newsletters/?s_cid=HP_NL
Recorded Webinars
Presentation of the latest MathWorks solutions in approx. 1 hour long online presentations (in English and German). http://www.mathworks.com/company/events/archived_webinars.html?s_cid=HP_E_RW
TM140 - B&R Automation Studio Target for Simulink® 107
Training modules
Training modules
TM210 – Working with Automation Studio
TM213 – Automation Runtime
TM223 – Automation Studio Diagnostics
TM230 – Structured Software Development
TM240 – Ladder Diagram (LD)
TM241 – Function Block Diagram (FBD)
TM242 – Sequential Function Chart (SFC)
TM246 – Structured Text (ST)
TM250 – Memory Management and Data Storage
TM400 – Introduction to Motion Control
TM410 – Working with Integrated Motion Control
TM440 – Motion Control: Basic Functions
TM441 – Motion Control: Multi-axis Functions
TM450 – ACOPOS Control Concept and Adjustment
TM460 – Initial Commissioning of Motors
TM500 – Introduction to Integrated Safety
TM510 – Working with SafeDESIGNER
TM540 – Integrated Safe Motion Control
TM600 – Introduction to Visualization
TM610 – Working with Integrated Visualization
TM630 – Visualization Programming Guide
TM640 – Alarm System, Trends and Diagnostics
TM670 – Advanced Visual Components
TM800 – APROL System Concept
TM811 – APROL Runtime System
TM812 – APROL Operator Management
TM813 – APROL XML Queries and Audit Trail
TM830 – APROL Project Engineering
TM890 – The Basics of LINUX
TM920 – Diagnostics and service
TM923 – Diagnostics and Service with Automation Studio
TM950 – POWERLINK Configuration and Diagnostics
TM1010 – B&R CNC System (ARNC0)
TM1110 – Integrated Motion Control (Axis Groups)
TM1111 – Integrated Motion Control (Path Controlled Movements)
TM261 – Closed Loop Control with LOOPCONR
TM280 – Condition Monitoring for Vibration Measurement
TM480 – The Basics of Hydraulics
TM481 – Valve-based Hydraulic Drives
TM482 – Hydraulic Servo Pump Drives
108 TM140 - B&R Automation Studio Target for Simulink®
Training modules
TM140 - B&R Automation Studio Target for Simulink® 109
Training modules
110 TM140 - B&R Automation Studio Target for Simulink®
Training modules
TM140 - B&R Automation Studio Target for Simulink® 111
www.br-automation.com
TM140TRE.00-ENG / V1.3.0.1
©2015/06/19 by B&R. All rights reserved.
All registered trademarks are the property of their respective owners.
We reserve the right to make technical changes.
Download