Sample program 6 (interlacing of measured values)

Program description / function

Note on this chapter: The use of EL3751/ELM3xxx terminals also applies accordingly to EPP35xx.

In some use cases a particularly fine temporal resolution of the signal is desired, e.g. so that many measuring points are available for an FFT. Two ways to do this are shown below:

The second way is described in this sample: Use of two EL3751 EtherCAT Terminals, each with a maximum sampling rate of 10 kSps (and thus a conversion time of 100 µs in this case, cf. Continuative documentation for I/O components with analog in and outputs, chapter “Temporal aspects of analog/digital or digital/analog conversion”). Due to their parallel connection, both terminals are fed the same signal simultaneously and are configured by Distributed Clocks in such a way that they sample not at the same time, but offset by half the conversion time (in this case: 50 µs). If the two measured data streams are now combined alternately in the controller, i.e. "interlaced", the result is a net measured data stream of 20 ksps.

Sample program 6 (interlacing of measured values) 1:
Process of interlacing the input data

The following configuration is used for this purpose:

Sample program 6 (interlacing of measured values) 2:
Configuration and setup for sample program 6: Doubling of the sample rate with 2 x EL3751

The sample is also available with corresponding adaptations for other EL3xxx/ELM3xxx terminals or box modules. There may then be different oversampling factors, shift times, etc. The optionally existing task with 50 µs in sample 6a may then also not be usable.

So that the input values can be successively combined to form a total value, a corresponding shift time is necessary for each channel/terminal; in this sample 50 µs for the second terminal. This is set in the "Advanced settings" for Distributed Clocks ("DC" tab) for the second terminal:

Sample program 6 (interlacing of measured values) 3:
Setting the DC shift time for terminal 2

Some notes and restrictions

Sample program

This setting, like the base time and the task cycle time, is already configured in the sample program:

Download TwinCAT 3 project / sample program 6a:

In the following section, the simplest form of input value interlacing in Structured Text is initially shown with oversampling = 1 for each input value: each of two elements of a field variable receives a value from a terminal. The variable can be used for further processing and is shown here in the TwinCAT ScopeView. In the EL3751 the programming instructions are assigned to a 100 µs task:

Variable declaration sample program 6a

   nSamples_1         AT%I*      :DINT; // EL3751 input with no added shift time
   nSamples_2         AT%I*      :DINT; // EL3751 input with -50 µs added shift time
   aCollectedResult              :ARRAY[0..1] OF DINT;

Execution part:

// Example program 6a:
// 100 µs task
// ============================================================
aCollectedResult[0] := nSamples_1;   // Put 1st Value of sequence into array
// Pattern:
aCollectedResult[1] := nSamples_2;   // Put n-th Value of sequence into array (2nd here)
// Pattern: .
// ============================================================
// Result pattern: 12121212... (--> see scope view dots)

For an input signal with sine 5 kHz and 2.5 V amplitude, for example, the TwinCAT ScopeView provides the following results:

Sample program 6 (interlacing of measured values) 4:
Oversampling 20 ksps with 2 x EL3751 with input signals (below) and result signal (top)

The upper diagram shows the total signal and the two input signals (nSample_1, nSample_2), with a time shift of 50 µs relative to each other, within 18 s in compressed form. The total input signal (nCollectedResult) indicates the interlacing of the two input signals.

The following diagram (enhanced through highlighting) shows how the input signals (nSample_1, nSample_2) contribute to the structure of the total input signal:

Sample program 6 (interlacing of measured values) 5:
Oversampling 20 ksps with 2 x EL3751 shows input value 1 and input value 2 alternately for a result value

Under certain conditions, both inputs can be combined into a single variable in a correspondingly fast task. For this purpose the sample program contains an additional task with 50 µs cycle time, which is required for representing the input signals in the SopeView and contains a variable (nCollected) to which both inputs are assigned alternately:

// 50 µs task
// ============================================================
// Junction of the two inputs
nCollected := SEL(nToggle, MAIN.nSamples_1_, MAIN.nSamples_2_);
nToggle := NOT nToggle;

The input variables required for the ScopeView are read in this task from the 100 µs task, so that the individual values can be represented at 50 µs intervals.

Variant with 2 x oversampling 10 = oversampling 20

If, for example, an oversampling factor of 10 is used for both input terminals, a field variable is used for the total measured value. A simple loop can be used for interlacing the input values, which reads the values sequentially into a field variable for the resulting result variable:

Variable declaration sample program 6b

   aSamples_1         AT%I*      :ARRAY[0..9] OF DINT; // EL3751 input with no added shift time
   aSamples_2         AT%I*      :ARRAY[0..9] OF DINT; // EL3751 input with -50 µs added shift time
   aCollectedResult              :ARRAY[0..19] OF DINT;
// ===================================================
   nPos                          :BYTE;

Execution part:

// Example program 6b:
// 1 ms task
// ============================================================
FOR nPos := 0 TO 9 DO
   // Put 1st Value of sequence into array:
   aCollectedResult[2*nPos] := aSamples_1[nPos];
   // Put n-th value of sequence into array (2nd here):
   aCollectedResult[2*nPos+1] := aSamples_2[nPos];

Download TwinCAT 3 project / sample program 6b:

Sample program 6b returns the same result, except that the total input signal is only available in the form of a field variable with 20 elements.