Length and Speed Measurement Using High-Speed Counters (HSC)
Length and Speed Measurement Using High-Speed Counters (HSC)
Length and Speed Measurement Using High-Speed Counters (HSC)
Measurement
using High-Speed
Counters (HSC)
Siemens
SIMATIC S7-1200 from firmware V4.2 Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/109754525 Support
Legal information
Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG (“Siemens”). They are non-
binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the
respective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application
examples is your responsibility. Sharing the application examples with third parties or copying the
application examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not
result in property damage or injury to persons.
Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without
limitation, liability for the usability, availability, completeness and freedom from defects of the
application examples as well as for related information, configuration and performance data and
any damage caused thereby. This shall not apply in cases of mandatory liability, for example
under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of
life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent
non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for
damages arising from a breach of material contractual obligations shall however be limited to the
Siemens AG 2018 All rights reserved
foreseeable damage typical of the type of agreement, unless liability arises from intent or gross
negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions
do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens
against existing or future claims of third parties in this connection except where Siemens is
mandatorily liable.
By using the application examples you acknowledge that Siemens cannot be held liable for any
damage beyond the liability provisions described.
Other information
Siemens reserves the right to make changes to the application examples at any time without
notice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have
precedence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.
Security information
Siemens provides products and solutions with industrial security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an
enterprise network or the Internet if and to the extent such a connection is necessary and only
when appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer
supported, and failure to apply the latest updates may increase customer’s exposure to cyber
threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.
Table of contents
Legal information ..................................................................................................... 2
1 Introduction .................................................................................................... 4
1.1 Overview ........................................................................................... 4
1.2 Mode of operation.............................................................................. 5
1.3 Components used ............................................................................. 5
2 Engineering .................................................................................................... 7
2.1 Hardware configuration ...................................................................... 7
2.1.1 Resistance calculation ....................................................................... 8
2.2 Configuration ..................................................................................... 8
2.2.1 Configuring pulse generators ............................................................. 8
2.2.2 Configuring a high-speed counter .................................................... 10
2.2.3 Configuring a digital input ................................................................ 12
2.3 Interface description ........................................................................ 13
2.3.1 Retrieve hardware ID ....................................................................... 14
2.4 Operation ........................................................................................ 16
2.4.1 Watch table ..................................................................................... 17
2.4.2 HMI ................................................................................................. 17
2.5 Errorconsideration ........................................................................... 19
2.5.1 Inclined plane .................................................................................. 19
2.5.2 Eddy current brake .......................................................................... 19
2.5.3 Acceleration by tensioning device .................................................... 20
Siemens AG 2018 All rights reserved
1 Introduction
1.1 Overview
The SIMATIC S7-1200 offers integrated functions such as pulse generators and
high-speed counters.
In most cases, high-speed counters are used in conjunction with a rotary encoder
to measure motor speed or the distance or length of workpieces on a conveyor
belt.
Using the example of the "pencil slingshot", the length and speed measurement is
shown strikingly by the high-speed counters, by the simplest means, such as the
integrated PWM generator of the S7-1200 and light barriers. You can find a video
about this Application Example at:
https://www.youtube.com/watch?v=eB-k6HW58xA
Figure 1-1
Pencil
Siemens AG 2018 All rights reserved
Tensioning device
Board
Aluminum rail
with
light barriers
HSC
PWM
S7-1200 HMI KTP BASIC
Column
Table 1-2
Component File name Note
Documentation 109754525_S71200_HSC_CalcVeloLength_DOC_V10_de.pdf
STEP 7 project 109754525_S71200_HSC_CalcVeloLength_TiaV15_PROJ_V10.zip
2 Engineering
2.1 Hardware configuration
Figure 2-1
1,8 kΩ 1,8 kΩ
211-1AE40-0XB0
Q0.0: Clock
560Ω 1W
Note Depending on the pulse receiver and cable, additional load resistance (at least
10% of the rated current) may improve the quality of the pulse signals and
resistance to interference.
Note Since the duty cycle of the PWM signal is 50%, half the dissipation of the load
Siemens AG 2018 All rights reserved
2.2 Configuration
2.2.1 Configuring pulse generators
The maximum frequency of the digital inputs (Ea.0 to Ea.5) of the S7-1200 is
100 kHz. This corresponds to a period duration (cycle time) of 10 μs.
To configure a pulse generator (PWM) with a cycle time of 10 μs and a duty cycle
of 50%, proceed as follows:
1. Select an S7-1200 CPU in the device or network view.
2. In the Inspector window, under "Properties> General> Pulse generators
(PTO/PWM)", click on "PTO1/PWM1".
3. Activate the pulse generator in the parameter group "General" with the
corresponding check box. In "Project information", you can enter a name and a
comment for the pulse generator.
4. Define the pulse options of the pulse generator in the parameter group
"Parameter assignment" as follows:
– "Signal type": "PWM"
– "Time base": "Microseconds"
– "Pulse duration format": "Hundredths"
– "Cycle time": 10 µs
– "Initial pulse duration": 50 "hundredths"
– Activate the "Allow runtime modification of the cycle time" option to output
the cycle time via the output addresses (Figure 2-5).
Siemens AG 2018 All rights reserved
Note The smaller the cycle time, the more accurately the speed can be determined.
5. In the parameter group "Hardware outputs", for the "Pulse output", enter the
hardware output "% Q0.0":
Figure 2-4: Hardware outputs
6. In the "I/O addresses" area, you set the parameters of the output addresses.
Figure 2-5: Parameters for output addresses.
The output addresses allow you to read (or set) the following parameters ->
see Figure 2-17:
QW1008: Pulse duration
QD1010: Cycle time
4. Define the function of the counter in the "Function" parameter group as follows:
– "Type of counting": "Count"
– "Operating phase": "Single phase"
– "Counting direction is specified by": "User program (internal direction
control)"
– "Initial counting direction": "Count up"
5. Activate the check box "Use external input to capture current count" in the
parameter group "Capture input".
In the "Start condition for recording the input" drop-down list, select the "Falling
edge" option.
Figure 2-8: Activating the capture input
6. Activate the check box "Use external gate input" in the parameter group "Gate
Siemens AG 2018 All rights reserved
input".
Since we wish to measure the time at which the light barrier signal is
interrupted, from the drop-down list "Signal level 11of the hardware gate",
select the option "Active low".
Figure 2-9: Activating the hardware gate
7. Enter the following hardware inputs in the "Hardware inputs" parameter group:
– "Clock generator input": "%I0.0"
– "Gate input":"%I0.1"
– "Capture input":"%I0.2"
8. In the "I/O addresses" area, you can set the parameters of the input
addresses.
Figure 2-11: Parameters for input addresses.
Siemens AG 2018 All rights reserved
Note The S7-1200 saves the current value of the HSC as DINT in the input address
specified under "Start address". You can call up the value, for example, in the
watch table via ED1000 (see Chapter 2.4.1) or you can use the count value of
the system data type in conjunction with the instruction "CTRL_HSC_EXT" (see
Table 2-2).
To safely detect the pulses of the inputs to the clock generator and the gate input,
you must set the filter time of the digital inputs at a lower value than the duration of
the input signal.
F = 100 KHz -> T = 10 µs
Input filter < T/2 = 5 µs
The filter time is set as follows:
1. Select an S7-1200 CPU in the device or network view.
2. In the Inspector window, under "Properties > General > DI 6/DQ 4 > Digital
inputs", click on "Channel0".
3. Set the "Input filter" to e.g. "3.2 microsec".
4. Repeat the setting of the "Input filter" of "3.2 microsec" for Channels 1 and 2.
Note With smaller filter times, double signals may occur due to the simple light barriers
(without Schmitt trigger). Ensure EMC-compliant cable routing. For high-speed
counters, it is essential that the input filters are set correctly!
CalcVeloLength
Real distance
The pulse generator and high-speed counter hardware identifiers can be found by
displaying all PLC variables. In the "System constants" tab, you will find the name
of the configured pulse generator (see Figure 2-2) or high-speed counter (see
Figure 2-6) with the prefix "Local~". You can transfer the absolute value or the
symbolic name to the respective interface parameter in Table 2-1.
Figure 2-14
Siemens AG 2018 All rights reserved
Figure 2-15
CTRL_
PWM
Calc CTRL_
MAIN
Velo HSC_
[OB 1] Length EXT
R_
TRIG
Inst
Calc
Velo
Length
System Data
User program
blocks blocks
Siemens AG 2018 All rights reserved
2.4 Operation
The Application Example is operated automatically via light barrier detection. For
explanation, the following trace capture is used:
Figure 2-16
10457
4655
10457
Siemens AG 2018 All rights reserved
1,582
0,165
The light barriers are light-switching. If there is a negative edge at the "hwGate"
gate input, the pulses are counted by means of a high-speed counter at
"InstCalcVeloLength.statHscCount.CurrentCount". When the capture light barrier
"hwCapture" is reached, the current counter value is stored in
"InstCalcVeloLength.statHscCount.CaptureCount" (here 4655).
When the light barrier "hwGate” is exited, the count value "CurrentCount" is
stopped (here 10457). If the object also leaves the second light barrier
"hwCapture", the count is copied into the variable
"InstCalcVeloLength.statLastGateCount" and the speed and the length of the
object are calculated:
Data.partVelocity = distance / (captureCount" * pwmCycleTime)
Data.partVelocity = 73.66 mm / (4655 * 10 µs) ≈ 1.582 m/s
Data.partLength = partVelocity * gateCount * pwmCycleTime
Data.partLength = 1.582 m/s * 10457 * 10 µs ≈ 0.165 m/s
The "WatchTable" shows you all required data of the Application Example.
Figure 2-17
Siemens AG 2018 All rights reserved
2.4.2 HMI
You can display the measured values using the SIMATIC HMI KTP 700 Basic PN
or its simulation in the TIA Portal.
Note For simulation in the TIA Portal, you must set the PC/PC interface (Set PC/PC
Interface (32-bit)) in the Windows Control Panel as follows:
Access point of the application: "S7ONLINE (STEP 7)"
Interface Parameter Assignment Used: <Your network card>
(Parameter assignment of your NDIS-CP with TCP/IP protocol
(RFC-1006))
(Parameter assignment of your NDIS-CP with TCP/IP protocol
(RFC-1006))
Figure 2-18
6
5
Siemens AG 2018 All rights reserved
In addition to the calculated length and velocity of the pencil, the number of pulses
over a period of 10 μs is displayed.
The following variables are displayed:
1. Measured number of pulses during the darkening of the "Gate" light barrier
over a period of 10 μs (see PWM signal configuration -> Chapter 2.2.1)
2. Pencil length
3. Pencil speed
4. Measured number of pulses for the path ("distance" parameter -> Chapter 2.3)
from the "Gate" light barrier to the "Capture" light barrier over a period of 10 μs
(see PWM signal configuration -> Chapter 2.2.1) to calculate the speed
5. Cycle time of the calling OB1 "Main"
6. Button for extending the cycle time of the OB1 "Main" (as proof that the
configured events of the high-speed counter function independently of the
cycle time of the calling organization block)
2.5 Errorconsideration
2.5.1 Inclined plane
The pencil is accelerated evenly on the inclined plane. Therefore, the measured
speed when the pencil tip reaches the capture light barrier (t1) is lower than when
the gate light barrier is exited (t2). Thus, the measured length of the pencil is less
than the actual length (l = v * t).
Figure 2-19
v[m/s]
v=a*t
v2
v1
Siemens AG 2018 All rights reserved
t1 t2 t[s]
An eddy current brake is suitable for maintaining a constant speed of the pencil on
the inclined plane. For this purpose, a small magnet is stuck to the pencil with
adhesive tape. The magnet must fit in the guide of the aluminum rail, but does not
touch it. The polar alignment (north-south) of the magnet is parallel to the guide in
the direction of movement.
Figure 2-20
Magnet
Pencil
Aluminum rail
If the magnet is moved along the aluminum profile, a magnetic field is generated in
the electrically conductive aluminum in accordance with Lenz's law, which
counteracts that of the magnet. This results in an approximately constant speed of
the pencil (\9\).
The tensioning device consists of a cross brace with two screws at the ends. A
cord with two rubber sealing rings is tightened across the two screws. In this "bow",
the slotted round wood peg is pulled like an "arrow" and converts the potential
tension energy of the rubbers into kinetic energy upon release. This achieves a
much higher speed of the pencil. The proportionate gravitational acceleration which
also acts here has only a smaller influence on the speed measurement of the
pencil due to the higher speed.
Siemens AG 2018 All rights reserved
3 Useful information
3.1 Details on functionality
3.1.1 Dimensions
Figure 3-1
350 350
Cord
200
35 35
Angle
Cross struts
Sealing rings
Siemens AG 2018 All rights reserved
M6 screws
1500
Rail
Figure 3-2
“Gate” 4
light barrier 5
10
29 * 2.54 mm
= 73.66 mm 20
Siemens AG 2018 All rights reserved
30
“Capture” 33
light barrier 34
40
3.2 Basics
3.2.1 High-speed counters
The high-speed counters are internal hardware counters and thus much faster than
software counters. The speed is limited by the inputs used.
Inputs
The SIMATIC S7-1200 offers the following inputs for the acquisition of signals via
six configurable high-speed counters:
Table 3-1
Max. Inputs Screen
counting
frequency
1 MHz Differential inputs (RS422/RS485) of the CPU
1217C (Eb.2 to Eb.5)
* Selected variant
Gate
The gate input enables the high-speed counter. Depending on the configuration,
the gate input can block or enable counting. Pulses arriving at the count entrance
are counted only when the gate is open.
Figure 3-3
Note This use case is presented in detail in the Application Examples for High-Speed
Counters (HSC) of the S7-1200 (\4\).
Capture
The capture input "Capture" saves the current counter value. Thus, while the gate
Siemens AG 2018 All rights reserved
Other / inactiv e
v = s / tCapture
tCapture = nCapture * TPulse
v = s / (nCapture * TPulse)
l = v * tGate
tGate = nGate * TPulse
l = v * nGate * TPulse
l = s * nGate / nCapture
Note The prerequisite for this is a constant speed of movement and the length of the
object must be at least equal to the distance between the light barriers!
The SIMATIC S7-1200 has four configurable pulse generators that can output the
following:
Pulse trains (fixed duty cycle, varying period) or
PWM signals (fixed period, varying duty cycle)
The speed is limited by the outputs used.
Outputs
The SIMATIC S7-1200 offers the following DC outputs for pulse generation:
Table 3-2
Max. pulse Inputs Screen
rate
1 MHz Differential outputs (RS422/RS485) of the CPU
1217C (Aa.0 to Aa.3)
Siemens AG 2018 All rights reserved
4 Appendix
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire
service and support know-how and portfolio.
The Industry Online Support is the central address for information about our
products, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
https://support.industry.siemens.com
Technical Support
The Technical Support of Siemens Industry provides you fast and competent
support regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts. Please send queries
to Technical Support via Web form:
www.siemens.com/industry/supportrequest
We support you with our globally available training courses for industry with
practical experience, innovative learning methods and a concept that’s tailored to
the customer’s specific needs.
For more information on our offered trainings and courses, as well as their
locations and dates, refer to our web page:
www.siemens.com/sitrain
Service offer
Our range of services includes the following:
Plant data services
Spare parts services
Repair services
On-site and maintenance services
Retrofitting and modernization services
Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
https://support.industry.siemens.com/cs/sc