SlideShare a Scribd company logo
28/03/2019
PI UK @ Coventry & London 1
“MATLAB code in PLCs, an industrial
control case.
“I-Device " & " Oversampling" features
in PROFINET.
Philippe Saey (lecturer)
Dimitri De Schuyter
Frederic Depuydt
Mathieu Troch
Jos De Brabanter
PI UK “The smart way to Industry 4.0 with
PROFINET based technologies”
Coventry, 26/03/2019 & London, 28/03/2019
Outline
• Introduction – Goals
• Code generation from MATLAB
o Why?
o How to generate code from MATLAB => PLC
• Work flow – Importing the SCL file – 32 vs. 64 bit calculations
o Industrial use case in ArcelorMittal Gent
o Testing the prediction algorithms
• Some inspiration for applications:
o Early warning & long term analysis: FFT
o Putting it in a PROFINET I-Device
o Capturing data using oversampling in PROFINET
2
The document reflects the author's views. The INTERREG V 2 Seas Program Authorities are not liable for any use that may be made of the information contained therein.
1
2
28/03/2019
PI UK @ Coventry & London 2
• MATLAB & Simulink code in PLCs and DCS: a hot topic
the last years …
3
Introduction - Goals
• MATLAB & Simulink are the “lingua franca” of engineering
and control system design …
• With numerous toolboxes and blocksets for systems
modelling, control system design, signal and image
processing, virtual commissioning, neural networks, data
analytics, machine learning, etc., but …
4
Why use code generation from MATLAB?
3
4
28/03/2019
PI UK @ Coventry & London 3
But how do we get the algorithms designed by these guys …
5
Why use code generation from MATLAB?
But how do we get the algorithms designed by these guys …
6
Why use code generation from MATLAB?
… to these guys ???
5
6
28/03/2019
PI UK @ Coventry & London 4
• Many studies, algorithms, models, controller designs stay
in the “study & reporting phase” and are not converted into
actual industrial applications 
• Why not?
o Coding needs to be for industrial hardware (PLC, DCS)
(Reliability, industrial networks, safety, redundancy, etc.)
o Manual programming of complex algorithms is not often done
(error-prone, time consuming, often too complicated, not easy to
maintain over time, execution time of algorithms could be an issue, etc.)
7
Why use code generation from MATLAB?
• Using code generation from MATLAB and integrating it into
the PLC/DCS code is a solution
o Straight from the design/simulation phase: quick, not error-prone,
changes in the design are easy to implement
o If a (simplified) plant model is available, the algorithms can be tested
off-line, but on the actual target hardware
=> HIL (Hardware In the Loop), virtual commissioning, …
o The code can also easily run alongside existing (proven) code in the
real system, to compare the control outputs of the new and old
algorithms
=> Fall back to old solutions, careful steps during implementation …
8
Why use code generation from MATLAB?
7
8
28/03/2019
PI UK @ Coventry & London 5
Outline
• Introduction – Goals
• Code generation from MATLAB?
o Why?
o How to generate code from MATLAB => PLC
• Work flow – Importing the SCL file – 32 vs. 64 bit
calculations
o Industrial use case in ArcelorMittal Gent
o Testing the prediction algorithms
• Some inspiration for applications:
o Early warning & Long term analysis: FFT
o Putting it in an I-Device
o Capturing data using oversampling in PROFINET
• Further work
9
1. Define and design a Simulink model from which you want to generate code.
2. Identify the model components for which you want to generate code for
importing to a PLC.
3. Place the components in a Subsystem block.
4. Identify your target PLC IDE (1)
5. Select a solver.
6. Configure the Subsystem block to be atomic.
7. Check that the model is compatible with the Simulink PLC Coder software.
8. Simulate your model.
9. Configure model parameters to generate code for your PLC IDE.
10.Examine the generated code.
11.Import code to your PLC IDE.
(1) Integrated Development Environment, e.g. Siemens Step7 or TIA Portal or PC Worx or …
(https://nl.mathworks.com/help/plccoder/basic-workflow.html)
10
Work flow for code generation from MATLAB => PLC
9
10
28/03/2019
PI UK @ Coventry & London 6
11
Work flow for code generation from MATLAB => PLC
• Generates structured text (ST), in our case a SCL file
• Must be imported in TIA Portal or STEP 7
12
Importing the SCL code
• SIMATIC Manager STEP 7: external source file / create
block numbers automatically / manually create instance
data block linked to that FB
11
12
28/03/2019
PI UK @ Coventry & London 7
13
Importing the SCL code
• TIA Portal: external source file / “generate blocks from
source”
A manual for a workshop is almost ready …
14
32 vs. 64 bit floating point calculations
• Not every target PLC supports 64 bit calculations …
• Difference in representation between MATLAB and TIA Portal
• MATLAB: IEEE 754 FP, showing numbers with 16 digits in e+x format
• TIA Portal and the PLC use IEEE 754 for calculations, but … display in
another way …
• FP >e-5 is shown as comma value, FP < e-5 is shown in IEEE 754 but with
15 digits. (Error when manually copying process data into TIA Portal …)
13
14
28/03/2019
PI UK @ Coventry & London 8
A basic test …
• A basic test to check:
• If it actually works …
• Is the timing correct if we run it at the appropriate sampe time?
• If we can enter parameters in real-time from outside the generated
code?
• Does it perform matrix calculations (…)
15
First order step response with external parameter setting, sample time of
50 ms, and a matrix computation to produce double step response
Outline
• Introduction – Goals
• Code generation from MATLAB
o Why?
o How to generate code from MATLAB => PLC
• Work flow – Importing the SCL file – 32 vs. 64 bit calculations
o Industrial use case in ArcelorMittal Gent
o Testing the prediction algorithms
• Some inspiration for applications:
o Early warning & long term analysis: FFT
o Putting it in a PROFINET I-Device
o Capturing data using oversampling in PROFINET
16
15
16
28/03/2019
PI UK @ Coventry & London 9
Industrial use case @ ArcelorMittal Gent
• Location: continuous slab caster 2 in the Steel Shop
• About 6 months ago: “can you convert these MATLAB
algorithms to PLC code?”
17
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2 – Side view
18
Draaitoren
Turret
Staalgietpan
Steel ladle
Verdeler
Tundish
Gietvorm
Mold
Gietboog
Slab caster
machine
(vertical
straigthening
machine)
Cut-to-length
machine
segments
17
18
28/03/2019
PI UK @ Coventry & London 10
19
Gietboog
Slab caster
machine
(vertical
straigthening
machine)
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2
Draaitoren
Turret
Verdeler
Tundish
Gietvorm
Mold
Staalgietpan
Steel ladle
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2
20
Draaitoren
Turret
Verdeler
Tundish
Gietvorm
Mold
Staalgietpan
Steel ladle
19
20
28/03/2019
PI UK @ Coventry & London 11
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2
21
Verdeler
Tundish
Gietvorm
Mold
Staalgietpan
Steel ladle
Verdeler / Tundish:
- Split the steel flow
- Buffers when
changing steel ladle
- Extraction of Al2O3
- Keep the exit
speed/flow as
constant as possible
Flux powder
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
22
We want to control the mold level:
- Actuator is the “stopper”
- The level in the tundish (verdeler) is changing (it buffers,
especially when the steel ladle (staalgietpan) is switched
- …
- But what happens in the mold itself?
“Periodic Disturbance Suppression in a Steel Plant with Unstable Internal Feedback and
Delay”, C. Furtmueller, L. del Re, H. Bramerdorfer, K. Moerwald; Institute for Design and
Control of Mechatronical Systems Johannes Kepler University & ISC1-Technologie
Stranggießtechnik VOEST-ALPINE, 2005, Austria
21
22
28/03/2019
PI UK @ Coventry & London 12
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
23
Some of the phenomenons in the
mold:
- Sputters
- Sticking (“aanberen”) and
falling off at the bottom of the
stopper (our actuator)
- Oscillating the mold to prevent
sticking of the liquid steel
- Standing waves (about 1 Hz)
- Bulging (“buiken”,
“renflement”)
- … we don’t want fluctations in
the mold level (“mold level
hunching”): too much flux
powder is taken away => steel
quality decreases because of
non-metallic inclosures.
24
The good news is that it is already controlled …
Using a state observer (Kalman filter) and a state variable feedback
controller. (Manually programmed into the PLC !)
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
23
24
28/03/2019
PI UK @ Coventry & London 13
25
The other news is that at high casting speeds, the influence of the
bulging on the mold level is too large, sometimes creating:
- quality issues (… recycling the slab in the steel shop)
- or worse: break throughs of the liquid steel …
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
26
The other news is that at high casting speeds, the influence of the
bulging on the mold level is too large, sometimes creating:
- quality issues (… recycling the slab in the steel shop)
- or worse: break throughs of the liquid steel …
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
0 1000 2000 3000 4000 5000 6000
-10
-5
0
5
10
15
20
tijd (seconden)
GVP-fluctuatie(mm)
25
26
28/03/2019
PI UK @ Coventry & London 14
27
Bulging …
- Is always present, but
important at high casting
speeds
- Unsteady bulging causes
changes in the GVP
Fb: buikfrequentie (Hz)
λ: steeklengte (m)
v: gietsnelheid (m/s)
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
𝑓𝑏 =
𝑣
𝜆
28
• Unsteady bulging causes changes in the mold level => plant scheme
• Usually, the feedback controller is set slow, with a bandwidth lower than the
mold level variations
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
27
28
28/03/2019
PI UK @ Coventry & London 15
29
Most pronounced frequencies come from the
first segments of the straightening machine
1
2
3
0.255
0.260
0.270
m
m
m



=
=
=
@ 1,7 m/min we get f1
= 0,111 Hz, f2 = 0,109
Hz, f3 = 0,105 Hz.
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
30
Let’s zoom in to see:
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
0 1000 2000 3000 4000 5000 6000
-10
-5
0
5
10
15
20
tijd (seconden)
GVP-fluctuatie(mm)
3600 3700 3800 3900 4000 4100 4200 4300 4400
-10
-5
0
5
10
15
Tijd (s)
GVP-fluctuatie(mm)
v = 170.0384 cm/min ; # samples = 7201
29
30
28/03/2019
PI UK @ Coventry & London 16
31
Let’s look at the FFT in real-time:
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2 0.22 0.24
0
10
20
30
40
50
60
70
80
Frequentie (Hz)
MagnitudeGVP(dB)
32
• We would like to create a feedforward signal to suppress mold level
fluctuations originating from the bulging
• There is considerable delay in the control loop => prediction of the mold level
is required.
• … that prediction algorithm has been designed in … MATLAB and Simulink!
AMG Steel shop – KG2 (Continugieterij 2)
Continuous slab caster 2: what do we want to control?
31
32
28/03/2019
PI UK @ Coventry & London 17
Outline
• Introduction – Goals
• Code generation from MATLAB
o Why?
o How to generate code from MATLAB => PLC
• Work flow – Importing the SCL file – 32 vs. 64 bit calculations
o Industrial use case in ArcelorMittal Gent
o Testing the prediction algorithms
• Some inspiration for applications:
o Early warning & long term analysis: FFT
o Putting it in a PROFINET I-Device
o Capturing data using oversampling in PROFINET
33
• AMG was considering 2 algorithms to try to reduce the
fluctuations in the mold level
o 1st one is used on another ArcelorMittal site (“external”
algorithm):
• Iterative calculation thas uses current time, frequency and
measurement, and the previously calculated variables. This algorithm
requires calculations with complex numbers.
o 2nd one was developed in a joint VUB – KU Leuven MSc
thesis project (2008-2009):
• It uses a window of (at least) 230 samples to predict the mold level the
next 2 seconds, allowing timely feedforward. This algorithm contains
matrix calculations; inputs are the searched frequencies and the current
measurements.
• AMG’s question, and our application: can we rapidly
implement these two algorithms using code generation from
MATLAB? What is the calculation time in the CPU? Which
resources are required?
34
Testing the prediction algorithms
33
34
28/03/2019
PI UK @ Coventry & London 18
• Initial tests are performed with MATLAB R2017b and TIA Portal
v14, target PLC is a S7-1516.
o 1st algorithm only requires some calculations with complex
numbers
o 2nd algorithm requires e.g. an “array circular shift”
• Is converted correctly, an extra for loop is created in the PLC code
(figures, for 100 points)
35
Testing the predicition algorithms
Part of the 2nd algorithm, to be coded by PLC coder:
36
Testing the prediction algorithms
35
36
28/03/2019
PI UK @ Coventry & London 19
• The actual controller is in a S7-416-2 CPU, 120 ns for a floating
point operation (2004 hardware onwards).
• Initial measurements in the lab are on S7-1516F (64 ns),
afterwards also on S7-319.
• Available dataset from KG2 is 30 minutes of casting, 18.001
samples, sample time = 0,1 s.
• Data is put on SD-card in the PLC, every 100 ms we read new
values (emulation of real process), and calculate.
• With a window of 250, we obtained an average of about 15 ms,
maximum of 32 ms. (Times 2 on a 416 …)
37
Testing the prediction algorithms
• With a window of 250, we obtained an average of about 15 ms,
maximum of 32 ms. (Times 2 on a 416 …)
38
Testing the prediction algorithms
37
38
28/03/2019
PI UK @ Coventry & London 20
• Without on-line monitor, and a window of 250, we obtained a
more constant execution time:
39
Testing the prediction algorithms
• Calculation time, S7-1516F, prediction with 2 frequencies:
o 1st algorithm: about 10 µs
o 2nd algorithm: 14 ms (max. 16 ms)
• Calculation time, S7-1516F, prediction with 4 frequencies:
o 2nd algorithm: 31 ms (max. = 32 ms)
• Calculation time, S7-1518, prediction with 4 frequencies:
o 2nd algorithm: 1,5 ms
40
Testing the predection algorithms
39
40
28/03/2019
PI UK @ Coventry & London 21
• Feedforward signal correctly calculated?
• MATLAB calculation compared to PLC calculation:
41
Testing the prediction algorithms
• Example of a prediction of 20 samples ahead (Ts = 100 ms, 2
seconds prediction)
• Red line (prediction) is scaled
42
Testing the prediction algorithms
41
42
28/03/2019
PI UK @ Coventry & London 22
Outline
• Introduction – Goals
• Code generation from MATLAB
o Why?
o How to generate code from MATLAB => PLC
• Work flow – Importing the SCL file – 32 vs. 64 bit calculations
o Industrial use case in ArcelorMittal Gent
o Testing the prediction algorithms
• Some inspiration for applications:
o Early warning & long term analysis: FFT
o Putting it in a PROFINET I-Device
o Capturing data using oversampling in PROFINET
43
• Two extensions are proposed:
o Early warning detection of bulging: FFT analysis (Fast Fourier
Transform1) on an array 512 points (51.2 seconds), with detection
of spectral lines exceeding a treshold => “bulging Y/N”
o An FFT with a high spectral resolution
44
Early warning & long term analysis
1 A FFT is an algorithm that calculates the discrete Fourier transform (DFT) of a sequence of numbers (an array).
43
44
28/03/2019
PI UK @ Coventry & London 23
• Two extensions are proposed:
o Early warning detection of bulging
o An FFT with a high spectral resolution:
• (Remember the frequencies depend on the distance between rolls!)
• FFT on a larger array, and/or slower sampling => high spectral resolution =>
individual frequencies and their amplitude
• Long term logging of spectral lines => analysis of causes (… big data)
45
Early warning & long term analysis
• Two extensions are proposed:
o Early warning detection of bulging
o An FFT with a high spectral resolution:
46
Early warning & long term analysis
45
46
28/03/2019
PI UK @ Coventry & London 24
• Actual controller in KG2 casting line is S7-416-2 (64 bit calculation time
is x 2 the one of the S7-1516)
• Prediction algorithm (and proposed extensions) probably take too
much calculation time to implement it in the PLC that is running the
mold level controller
47
Putting it in an I-Device
Master PLC
(IO-controller)
PLC
I-device
PROFINET
IO-device 1 IO-device 1
=> Run it in a separate PLC as “I-Device”
• “Intelligent IO-Device”
• Allows for communication between IO Controllers (“masters”) and CPs,
without using PN/PN couplers
48
Putting it in an I-Device: what?
109478798_config_idevice_standard_DOCU_V1d0_en.pdf
47
48
28/03/2019
PI UK @ Coventry & London 25
49
Putting it in an I-Device
• Calculation times on S7-1518 CPU
Prediction algorithm + “FFT” (FFT= removing DC, filtering,
windowing, FFT, …)
50
Putting it in an I-Device: calculation time
0,197 0,412 0,847
1,775
3,752
7,847
18,107
1,685 1,946 2,345
3,284
5,239
9,344
19,608
0
5
10
15
20
25
256 512 1024 2048 4096 8192 16384
Time[ms]
Window size [# samples]
Runtime FFT
Mean FFT mean total
49
50
28/03/2019
PI UK @ Coventry & London 26
• Calculation times on S7-1518 CPU
51
Putting it in an I-Device: memory
215080 311336 512080 888888
1719424
3362868
0
5000000
10000000
15000000
20000000
512 1024 2048 4096 8192 16384
usedbytes
Window size [# samples]
Memory usage
0,00%
10,00%
20,00%
30,00%
40,00%
50,00%
60,00%
70,00%
80,00%
90,00%
100,00%
512 1024 2048 4096 8192 16384
used/totaldatawork-memory
Window size [# samples]
Percentage memory usage
• Communication bytes I-Device to IO-controller:
52
Variable Amount of bytes needed
freq_peaks 10 x 8 bytes
amp_peaks 10 x 8 bytes
prediction 8 bytes
Total: 168 bytes
Max bytes possible
< 244 bytes in PROFIBUS
OK for PROFINET
Putting it in an I-Device: PROFINET
51
52
28/03/2019
PI UK @ Coventry & London 27
• Communication bytes I-Device to IO-controller:
53
Putting it in an I-Device: PROFINET
• Transfer areas of the CPU 1518 I-Device
• Packet from main to calculation CPU with the provider and consumer
statuses
• IO-controller communication, including communication to touch screen.
54
Putting it in an I-Device: PROFINET
53
54
28/03/2019
PI UK @ Coventry & London 28
Outline
• Introduction – Goals
• Code generation from MATLAB?
o Why?
o How to generate code from MATLAB => PLC
• Work flow – Importing the SCL file – 32 vs. 64 bit calculations
o Industrial use case in ArcelorMittal Gent
o Testing the prediction algorithms
• Some inspiration for applications:
o Early warning & Long term analysis: FFT
o Putting it in an I-Device
o Capturing data using oversampling in PROFINET
• Further work
55
Capturing data – oversampling in PROFINET
• We now know how to do a FFT in a PLC …
• We even know how to use a “calculation PLC” as I-Device
• Maybe we could design a low-cost “predictive analytics
device”:
o we sample analog signals
o process with some filtering and FFT
o and decide if we need preventive maintenance, etc.
o All of that in our “normal” PLC, with fairly standard IO-devices!
56
• We could even call it a CPS (Cyber Physical System):
o a mechanism (physical thing or system)
o controlled by computer-based algorithms
o tightly integrated with the Internet
55
56
28/03/2019
PI UK @ Coventry & London 29
Capturing data – oversampling in PROFINET
• But how fast can we sample data with fairly standard I/O, in a
deterministic way ?
o Take a look at your vendor’s fastest local analog cards
o Think about PROFINET’s shortest update cycle
o Is this deterministic sampling?
o How are you going to process this?
• PROFINET has an “oversampling” mechanism
57
“Networked sensing architecture using oversampling techniques in PROFINET IRT devices and isochronous mode processing: proof-of-principle and signal
reconstruction at IO Controller side”; IEEE conference on Emerging Technologies and Factory Automation, ETFA 2017, Limassol (Cyprus). Philippe Saey,
Frederic Depuydt, Mathieu Troch, Stijn Noppe, Annemarie Kokosy, Jos Knockaert. PID5069551.
58
Let’s take a look:
Using PROFINET IRT @500 µs cycle, and an
ET200SP HF IO Device that oversamples 10 x,
a sample time of 50 µs is achieved.
The oversamples are sent one after another in
the next PN IRT cycle
The “Main CPU” typically has a 2-3
ms overall cycle time. Mastering a
data stream of several analog
signals @ 20 kHz requires too much
CPU load, and timely processing is
not guaranteed.
Isochronous mode processing
using OB6x is used for the
timely processing issue. CPU
load rises even more (table).
Capturing data – oversampling in PROFINET
57
58
28/03/2019
PI UK @ Coventry & London 30
59
Let’s take a look:
Using PROFINET IRT @500 µs cycle, and an
ET200SP HF IO Device that oversamples 10 x,
a sample time of 50 µs is achieved.
The oversamples are sent one after another in
the next PN IRT cycle
The “Main CPU” typically has a 2-3
ms overall cycle time. Mastering a
data stream of several analog
signals @ 20 kHz requires too much
CPU load, and timely processing is
not guaranteed.
Isochronous mode processing
using OB6x is used for the
timely processing issue. CPU
load rises even more (table).
Proposed solution: add a low-cost
“streaming CPU” that is sync master
and unloads the main CPU.
On its 2nd port, the S7-1512SP acts
as I-Device in a PN RT network.
Capturing data – oversampling in PROFINET
60
Experimental analysis and signal reconstruction
Timing reconstruction in MATLAB
Measurement set-up
Capturing data – oversampling in PROFINET
59
60
28/03/2019
PI UK @ Coventry & London 31
61
Signal timing and propagation
Data timing in networks and CPUs:
with streaming CPU, the useful data
is available in the main CPU 3 x 500
µs later.
Capturing data – oversampling in PROFINET
62
Signal timing and propagation
Data timing in networks and CPUs:
with streaming CPU, the useful data
is available in the main CPU 3 x 500
µs later.
Signal propagation of analog input (yellow),
reconstructed data from PROFINET messages (red
is PROFINET data to S7-1512, blue is PROFINET
data from S7-1512), and analog output (green).
Capturing data – oversampling in PROFINET
61
62
28/03/2019
PI UK @ Coventry & London 32
Conclusions
• Compiling complex algorithms from MATLAB/Simulink to a
number of PLC/DCS is possible
• We discussed the design of an industrial application in
ArcelorMittal Gent, introducing also:
o Signal processing using FFT (+ DC removal, filtering, windowing, etc.)
o PROFINET I-Device
• We introduced PROFINET’s oversampling mechanism
• We invite you to visit our stand … it’s not all virtual ;-)
• Any questions?
63
63

More Related Content

Industrial control cases with MATLAB code in PLCs, using PROFINET's "oversampling" & "I-Device" features - Philippe Saey, KU Leuven (Catholic University of Leuven, Belgium)

  • 1. 28/03/2019 PI UK @ Coventry & London 1 “MATLAB code in PLCs, an industrial control case. “I-Device " & " Oversampling" features in PROFINET. Philippe Saey (lecturer) Dimitri De Schuyter Frederic Depuydt Mathieu Troch Jos De Brabanter PI UK “The smart way to Industry 4.0 with PROFINET based technologies” Coventry, 26/03/2019 & London, 28/03/2019 Outline • Introduction – Goals • Code generation from MATLAB o Why? o How to generate code from MATLAB => PLC • Work flow – Importing the SCL file – 32 vs. 64 bit calculations o Industrial use case in ArcelorMittal Gent o Testing the prediction algorithms • Some inspiration for applications: o Early warning & long term analysis: FFT o Putting it in a PROFINET I-Device o Capturing data using oversampling in PROFINET 2 The document reflects the author's views. The INTERREG V 2 Seas Program Authorities are not liable for any use that may be made of the information contained therein. 1 2
  • 2. 28/03/2019 PI UK @ Coventry & London 2 • MATLAB & Simulink code in PLCs and DCS: a hot topic the last years … 3 Introduction - Goals • MATLAB & Simulink are the “lingua franca” of engineering and control system design … • With numerous toolboxes and blocksets for systems modelling, control system design, signal and image processing, virtual commissioning, neural networks, data analytics, machine learning, etc., but … 4 Why use code generation from MATLAB? 3 4
  • 3. 28/03/2019 PI UK @ Coventry & London 3 But how do we get the algorithms designed by these guys … 5 Why use code generation from MATLAB? But how do we get the algorithms designed by these guys … 6 Why use code generation from MATLAB? … to these guys ??? 5 6
  • 4. 28/03/2019 PI UK @ Coventry & London 4 • Many studies, algorithms, models, controller designs stay in the “study & reporting phase” and are not converted into actual industrial applications  • Why not? o Coding needs to be for industrial hardware (PLC, DCS) (Reliability, industrial networks, safety, redundancy, etc.) o Manual programming of complex algorithms is not often done (error-prone, time consuming, often too complicated, not easy to maintain over time, execution time of algorithms could be an issue, etc.) 7 Why use code generation from MATLAB? • Using code generation from MATLAB and integrating it into the PLC/DCS code is a solution o Straight from the design/simulation phase: quick, not error-prone, changes in the design are easy to implement o If a (simplified) plant model is available, the algorithms can be tested off-line, but on the actual target hardware => HIL (Hardware In the Loop), virtual commissioning, … o The code can also easily run alongside existing (proven) code in the real system, to compare the control outputs of the new and old algorithms => Fall back to old solutions, careful steps during implementation … 8 Why use code generation from MATLAB? 7 8
  • 5. 28/03/2019 PI UK @ Coventry & London 5 Outline • Introduction – Goals • Code generation from MATLAB? o Why? o How to generate code from MATLAB => PLC • Work flow – Importing the SCL file – 32 vs. 64 bit calculations o Industrial use case in ArcelorMittal Gent o Testing the prediction algorithms • Some inspiration for applications: o Early warning & Long term analysis: FFT o Putting it in an I-Device o Capturing data using oversampling in PROFINET • Further work 9 1. Define and design a Simulink model from which you want to generate code. 2. Identify the model components for which you want to generate code for importing to a PLC. 3. Place the components in a Subsystem block. 4. Identify your target PLC IDE (1) 5. Select a solver. 6. Configure the Subsystem block to be atomic. 7. Check that the model is compatible with the Simulink PLC Coder software. 8. Simulate your model. 9. Configure model parameters to generate code for your PLC IDE. 10.Examine the generated code. 11.Import code to your PLC IDE. (1) Integrated Development Environment, e.g. Siemens Step7 or TIA Portal or PC Worx or … (https://nl.mathworks.com/help/plccoder/basic-workflow.html) 10 Work flow for code generation from MATLAB => PLC 9 10
  • 6. 28/03/2019 PI UK @ Coventry & London 6 11 Work flow for code generation from MATLAB => PLC • Generates structured text (ST), in our case a SCL file • Must be imported in TIA Portal or STEP 7 12 Importing the SCL code • SIMATIC Manager STEP 7: external source file / create block numbers automatically / manually create instance data block linked to that FB 11 12
  • 7. 28/03/2019 PI UK @ Coventry & London 7 13 Importing the SCL code • TIA Portal: external source file / “generate blocks from source” A manual for a workshop is almost ready … 14 32 vs. 64 bit floating point calculations • Not every target PLC supports 64 bit calculations … • Difference in representation between MATLAB and TIA Portal • MATLAB: IEEE 754 FP, showing numbers with 16 digits in e+x format • TIA Portal and the PLC use IEEE 754 for calculations, but … display in another way … • FP >e-5 is shown as comma value, FP < e-5 is shown in IEEE 754 but with 15 digits. (Error when manually copying process data into TIA Portal …) 13 14
  • 8. 28/03/2019 PI UK @ Coventry & London 8 A basic test … • A basic test to check: • If it actually works … • Is the timing correct if we run it at the appropriate sampe time? • If we can enter parameters in real-time from outside the generated code? • Does it perform matrix calculations (…) 15 First order step response with external parameter setting, sample time of 50 ms, and a matrix computation to produce double step response Outline • Introduction – Goals • Code generation from MATLAB o Why? o How to generate code from MATLAB => PLC • Work flow – Importing the SCL file – 32 vs. 64 bit calculations o Industrial use case in ArcelorMittal Gent o Testing the prediction algorithms • Some inspiration for applications: o Early warning & long term analysis: FFT o Putting it in a PROFINET I-Device o Capturing data using oversampling in PROFINET 16 15 16
  • 9. 28/03/2019 PI UK @ Coventry & London 9 Industrial use case @ ArcelorMittal Gent • Location: continuous slab caster 2 in the Steel Shop • About 6 months ago: “can you convert these MATLAB algorithms to PLC code?” 17 AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2 – Side view 18 Draaitoren Turret Staalgietpan Steel ladle Verdeler Tundish Gietvorm Mold Gietboog Slab caster machine (vertical straigthening machine) Cut-to-length machine segments 17 18
  • 10. 28/03/2019 PI UK @ Coventry & London 10 19 Gietboog Slab caster machine (vertical straigthening machine) AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2 Draaitoren Turret Verdeler Tundish Gietvorm Mold Staalgietpan Steel ladle AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2 20 Draaitoren Turret Verdeler Tundish Gietvorm Mold Staalgietpan Steel ladle 19 20
  • 11. 28/03/2019 PI UK @ Coventry & London 11 AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2 21 Verdeler Tundish Gietvorm Mold Staalgietpan Steel ladle Verdeler / Tundish: - Split the steel flow - Buffers when changing steel ladle - Extraction of Al2O3 - Keep the exit speed/flow as constant as possible Flux powder AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 22 We want to control the mold level: - Actuator is the “stopper” - The level in the tundish (verdeler) is changing (it buffers, especially when the steel ladle (staalgietpan) is switched - … - But what happens in the mold itself? “Periodic Disturbance Suppression in a Steel Plant with Unstable Internal Feedback and Delay”, C. Furtmueller, L. del Re, H. Bramerdorfer, K. Moerwald; Institute for Design and Control of Mechatronical Systems Johannes Kepler University & ISC1-Technologie Stranggießtechnik VOEST-ALPINE, 2005, Austria 21 22
  • 12. 28/03/2019 PI UK @ Coventry & London 12 AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 23 Some of the phenomenons in the mold: - Sputters - Sticking (“aanberen”) and falling off at the bottom of the stopper (our actuator) - Oscillating the mold to prevent sticking of the liquid steel - Standing waves (about 1 Hz) - Bulging (“buiken”, “renflement”) - … we don’t want fluctations in the mold level (“mold level hunching”): too much flux powder is taken away => steel quality decreases because of non-metallic inclosures. 24 The good news is that it is already controlled … Using a state observer (Kalman filter) and a state variable feedback controller. (Manually programmed into the PLC !) AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 23 24
  • 13. 28/03/2019 PI UK @ Coventry & London 13 25 The other news is that at high casting speeds, the influence of the bulging on the mold level is too large, sometimes creating: - quality issues (… recycling the slab in the steel shop) - or worse: break throughs of the liquid steel … AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 26 The other news is that at high casting speeds, the influence of the bulging on the mold level is too large, sometimes creating: - quality issues (… recycling the slab in the steel shop) - or worse: break throughs of the liquid steel … AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 0 1000 2000 3000 4000 5000 6000 -10 -5 0 5 10 15 20 tijd (seconden) GVP-fluctuatie(mm) 25 26
  • 14. 28/03/2019 PI UK @ Coventry & London 14 27 Bulging … - Is always present, but important at high casting speeds - Unsteady bulging causes changes in the GVP Fb: buikfrequentie (Hz) λ: steeklengte (m) v: gietsnelheid (m/s) AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 𝑓𝑏 = 𝑣 𝜆 28 • Unsteady bulging causes changes in the mold level => plant scheme • Usually, the feedback controller is set slow, with a bandwidth lower than the mold level variations AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 27 28
  • 15. 28/03/2019 PI UK @ Coventry & London 15 29 Most pronounced frequencies come from the first segments of the straightening machine 1 2 3 0.255 0.260 0.270 m m m    = = = @ 1,7 m/min we get f1 = 0,111 Hz, f2 = 0,109 Hz, f3 = 0,105 Hz. AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 30 Let’s zoom in to see: AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 0 1000 2000 3000 4000 5000 6000 -10 -5 0 5 10 15 20 tijd (seconden) GVP-fluctuatie(mm) 3600 3700 3800 3900 4000 4100 4200 4300 4400 -10 -5 0 5 10 15 Tijd (s) GVP-fluctuatie(mm) v = 170.0384 cm/min ; # samples = 7201 29 30
  • 16. 28/03/2019 PI UK @ Coventry & London 16 31 Let’s look at the FFT in real-time: AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2 0.22 0.24 0 10 20 30 40 50 60 70 80 Frequentie (Hz) MagnitudeGVP(dB) 32 • We would like to create a feedforward signal to suppress mold level fluctuations originating from the bulging • There is considerable delay in the control loop => prediction of the mold level is required. • … that prediction algorithm has been designed in … MATLAB and Simulink! AMG Steel shop – KG2 (Continugieterij 2) Continuous slab caster 2: what do we want to control? 31 32
  • 17. 28/03/2019 PI UK @ Coventry & London 17 Outline • Introduction – Goals • Code generation from MATLAB o Why? o How to generate code from MATLAB => PLC • Work flow – Importing the SCL file – 32 vs. 64 bit calculations o Industrial use case in ArcelorMittal Gent o Testing the prediction algorithms • Some inspiration for applications: o Early warning & long term analysis: FFT o Putting it in a PROFINET I-Device o Capturing data using oversampling in PROFINET 33 • AMG was considering 2 algorithms to try to reduce the fluctuations in the mold level o 1st one is used on another ArcelorMittal site (“external” algorithm): • Iterative calculation thas uses current time, frequency and measurement, and the previously calculated variables. This algorithm requires calculations with complex numbers. o 2nd one was developed in a joint VUB – KU Leuven MSc thesis project (2008-2009): • It uses a window of (at least) 230 samples to predict the mold level the next 2 seconds, allowing timely feedforward. This algorithm contains matrix calculations; inputs are the searched frequencies and the current measurements. • AMG’s question, and our application: can we rapidly implement these two algorithms using code generation from MATLAB? What is the calculation time in the CPU? Which resources are required? 34 Testing the prediction algorithms 33 34
  • 18. 28/03/2019 PI UK @ Coventry & London 18 • Initial tests are performed with MATLAB R2017b and TIA Portal v14, target PLC is a S7-1516. o 1st algorithm only requires some calculations with complex numbers o 2nd algorithm requires e.g. an “array circular shift” • Is converted correctly, an extra for loop is created in the PLC code (figures, for 100 points) 35 Testing the predicition algorithms Part of the 2nd algorithm, to be coded by PLC coder: 36 Testing the prediction algorithms 35 36
  • 19. 28/03/2019 PI UK @ Coventry & London 19 • The actual controller is in a S7-416-2 CPU, 120 ns for a floating point operation (2004 hardware onwards). • Initial measurements in the lab are on S7-1516F (64 ns), afterwards also on S7-319. • Available dataset from KG2 is 30 minutes of casting, 18.001 samples, sample time = 0,1 s. • Data is put on SD-card in the PLC, every 100 ms we read new values (emulation of real process), and calculate. • With a window of 250, we obtained an average of about 15 ms, maximum of 32 ms. (Times 2 on a 416 …) 37 Testing the prediction algorithms • With a window of 250, we obtained an average of about 15 ms, maximum of 32 ms. (Times 2 on a 416 …) 38 Testing the prediction algorithms 37 38
  • 20. 28/03/2019 PI UK @ Coventry & London 20 • Without on-line monitor, and a window of 250, we obtained a more constant execution time: 39 Testing the prediction algorithms • Calculation time, S7-1516F, prediction with 2 frequencies: o 1st algorithm: about 10 µs o 2nd algorithm: 14 ms (max. 16 ms) • Calculation time, S7-1516F, prediction with 4 frequencies: o 2nd algorithm: 31 ms (max. = 32 ms) • Calculation time, S7-1518, prediction with 4 frequencies: o 2nd algorithm: 1,5 ms 40 Testing the predection algorithms 39 40
  • 21. 28/03/2019 PI UK @ Coventry & London 21 • Feedforward signal correctly calculated? • MATLAB calculation compared to PLC calculation: 41 Testing the prediction algorithms • Example of a prediction of 20 samples ahead (Ts = 100 ms, 2 seconds prediction) • Red line (prediction) is scaled 42 Testing the prediction algorithms 41 42
  • 22. 28/03/2019 PI UK @ Coventry & London 22 Outline • Introduction – Goals • Code generation from MATLAB o Why? o How to generate code from MATLAB => PLC • Work flow – Importing the SCL file – 32 vs. 64 bit calculations o Industrial use case in ArcelorMittal Gent o Testing the prediction algorithms • Some inspiration for applications: o Early warning & long term analysis: FFT o Putting it in a PROFINET I-Device o Capturing data using oversampling in PROFINET 43 • Two extensions are proposed: o Early warning detection of bulging: FFT analysis (Fast Fourier Transform1) on an array 512 points (51.2 seconds), with detection of spectral lines exceeding a treshold => “bulging Y/N” o An FFT with a high spectral resolution 44 Early warning & long term analysis 1 A FFT is an algorithm that calculates the discrete Fourier transform (DFT) of a sequence of numbers (an array). 43 44
  • 23. 28/03/2019 PI UK @ Coventry & London 23 • Two extensions are proposed: o Early warning detection of bulging o An FFT with a high spectral resolution: • (Remember the frequencies depend on the distance between rolls!) • FFT on a larger array, and/or slower sampling => high spectral resolution => individual frequencies and their amplitude • Long term logging of spectral lines => analysis of causes (… big data) 45 Early warning & long term analysis • Two extensions are proposed: o Early warning detection of bulging o An FFT with a high spectral resolution: 46 Early warning & long term analysis 45 46
  • 24. 28/03/2019 PI UK @ Coventry & London 24 • Actual controller in KG2 casting line is S7-416-2 (64 bit calculation time is x 2 the one of the S7-1516) • Prediction algorithm (and proposed extensions) probably take too much calculation time to implement it in the PLC that is running the mold level controller 47 Putting it in an I-Device Master PLC (IO-controller) PLC I-device PROFINET IO-device 1 IO-device 1 => Run it in a separate PLC as “I-Device” • “Intelligent IO-Device” • Allows for communication between IO Controllers (“masters”) and CPs, without using PN/PN couplers 48 Putting it in an I-Device: what? 109478798_config_idevice_standard_DOCU_V1d0_en.pdf 47 48
  • 25. 28/03/2019 PI UK @ Coventry & London 25 49 Putting it in an I-Device • Calculation times on S7-1518 CPU Prediction algorithm + “FFT” (FFT= removing DC, filtering, windowing, FFT, …) 50 Putting it in an I-Device: calculation time 0,197 0,412 0,847 1,775 3,752 7,847 18,107 1,685 1,946 2,345 3,284 5,239 9,344 19,608 0 5 10 15 20 25 256 512 1024 2048 4096 8192 16384 Time[ms] Window size [# samples] Runtime FFT Mean FFT mean total 49 50
  • 26. 28/03/2019 PI UK @ Coventry & London 26 • Calculation times on S7-1518 CPU 51 Putting it in an I-Device: memory 215080 311336 512080 888888 1719424 3362868 0 5000000 10000000 15000000 20000000 512 1024 2048 4096 8192 16384 usedbytes Window size [# samples] Memory usage 0,00% 10,00% 20,00% 30,00% 40,00% 50,00% 60,00% 70,00% 80,00% 90,00% 100,00% 512 1024 2048 4096 8192 16384 used/totaldatawork-memory Window size [# samples] Percentage memory usage • Communication bytes I-Device to IO-controller: 52 Variable Amount of bytes needed freq_peaks 10 x 8 bytes amp_peaks 10 x 8 bytes prediction 8 bytes Total: 168 bytes Max bytes possible < 244 bytes in PROFIBUS OK for PROFINET Putting it in an I-Device: PROFINET 51 52
  • 27. 28/03/2019 PI UK @ Coventry & London 27 • Communication bytes I-Device to IO-controller: 53 Putting it in an I-Device: PROFINET • Transfer areas of the CPU 1518 I-Device • Packet from main to calculation CPU with the provider and consumer statuses • IO-controller communication, including communication to touch screen. 54 Putting it in an I-Device: PROFINET 53 54
  • 28. 28/03/2019 PI UK @ Coventry & London 28 Outline • Introduction – Goals • Code generation from MATLAB? o Why? o How to generate code from MATLAB => PLC • Work flow – Importing the SCL file – 32 vs. 64 bit calculations o Industrial use case in ArcelorMittal Gent o Testing the prediction algorithms • Some inspiration for applications: o Early warning & Long term analysis: FFT o Putting it in an I-Device o Capturing data using oversampling in PROFINET • Further work 55 Capturing data – oversampling in PROFINET • We now know how to do a FFT in a PLC … • We even know how to use a “calculation PLC” as I-Device • Maybe we could design a low-cost “predictive analytics device”: o we sample analog signals o process with some filtering and FFT o and decide if we need preventive maintenance, etc. o All of that in our “normal” PLC, with fairly standard IO-devices! 56 • We could even call it a CPS (Cyber Physical System): o a mechanism (physical thing or system) o controlled by computer-based algorithms o tightly integrated with the Internet 55 56
  • 29. 28/03/2019 PI UK @ Coventry & London 29 Capturing data – oversampling in PROFINET • But how fast can we sample data with fairly standard I/O, in a deterministic way ? o Take a look at your vendor’s fastest local analog cards o Think about PROFINET’s shortest update cycle o Is this deterministic sampling? o How are you going to process this? • PROFINET has an “oversampling” mechanism 57 “Networked sensing architecture using oversampling techniques in PROFINET IRT devices and isochronous mode processing: proof-of-principle and signal reconstruction at IO Controller side”; IEEE conference on Emerging Technologies and Factory Automation, ETFA 2017, Limassol (Cyprus). Philippe Saey, Frederic Depuydt, Mathieu Troch, Stijn Noppe, Annemarie Kokosy, Jos Knockaert. PID5069551. 58 Let’s take a look: Using PROFINET IRT @500 µs cycle, and an ET200SP HF IO Device that oversamples 10 x, a sample time of 50 µs is achieved. The oversamples are sent one after another in the next PN IRT cycle The “Main CPU” typically has a 2-3 ms overall cycle time. Mastering a data stream of several analog signals @ 20 kHz requires too much CPU load, and timely processing is not guaranteed. Isochronous mode processing using OB6x is used for the timely processing issue. CPU load rises even more (table). Capturing data – oversampling in PROFINET 57 58
  • 30. 28/03/2019 PI UK @ Coventry & London 30 59 Let’s take a look: Using PROFINET IRT @500 µs cycle, and an ET200SP HF IO Device that oversamples 10 x, a sample time of 50 µs is achieved. The oversamples are sent one after another in the next PN IRT cycle The “Main CPU” typically has a 2-3 ms overall cycle time. Mastering a data stream of several analog signals @ 20 kHz requires too much CPU load, and timely processing is not guaranteed. Isochronous mode processing using OB6x is used for the timely processing issue. CPU load rises even more (table). Proposed solution: add a low-cost “streaming CPU” that is sync master and unloads the main CPU. On its 2nd port, the S7-1512SP acts as I-Device in a PN RT network. Capturing data – oversampling in PROFINET 60 Experimental analysis and signal reconstruction Timing reconstruction in MATLAB Measurement set-up Capturing data – oversampling in PROFINET 59 60
  • 31. 28/03/2019 PI UK @ Coventry & London 31 61 Signal timing and propagation Data timing in networks and CPUs: with streaming CPU, the useful data is available in the main CPU 3 x 500 µs later. Capturing data – oversampling in PROFINET 62 Signal timing and propagation Data timing in networks and CPUs: with streaming CPU, the useful data is available in the main CPU 3 x 500 µs later. Signal propagation of analog input (yellow), reconstructed data from PROFINET messages (red is PROFINET data to S7-1512, blue is PROFINET data from S7-1512), and analog output (green). Capturing data – oversampling in PROFINET 61 62
  • 32. 28/03/2019 PI UK @ Coventry & London 32 Conclusions • Compiling complex algorithms from MATLAB/Simulink to a number of PLC/DCS is possible • We discussed the design of an industrial application in ArcelorMittal Gent, introducing also: o Signal processing using FFT (+ DC removal, filtering, windowing, etc.) o PROFINET I-Device • We introduced PROFINET’s oversampling mechanism • We invite you to visit our stand … it’s not all virtual ;-) • Any questions? 63 63