Sample program 5 (write filter coefficients)

Download TwinCAT 3 project: program link

Program description

Transmission of exemplary filter coefficients via CoE access into the box module.

Sample program 5 (write filter coefficients) 1:

General settings

  • The function block "FB_EcCoESdoWrite" requires the "Tc2_EtherCAT" library
  • <AmsNetID> must show the local device EtherCAT NetID in inverted commas (e.g. '168.57.1.1.5.1')
  • <DeviceEtherCATAddress> must show the local device EtherCAT address of the EPP3504‑0023/ ERP3504‑0022 box module (e.g., 1007)

Variable declaration sample program 5

PROGRAM MAIN
// Variable declaration example program 5
VAR CONSTANT
NumOfFilterCoeff                 :BYTE:=40;
END_VAR
VAR
// Function block of library "Tc2_EtherCAT" for CoE Object access:
fb_coe_write                     :FB_EcCoESdoWrite;
userNetId                        :T_AmsNetId := '???';
userSlaveAddr                    :UINT := ???;
// Writing PLC state for coefficients transfer (Set to 0 for start)
wState                           :BYTE:=255;
index                            :BYTE:=1; // Start index for coefficients transfer
wCoEIndexUserFilterCoeffizents   :WORD:=16#8001;
aFilterCoeffs:ARRAY[0..NumOfFilterCoeff] OF LREAL :=
   [
   // Example filter coefficients FIR band pass: 3600..3900 Hz
   // Usage: "User defined FIR Filter" (32)
      0.03663651655662163,
      0.04299467480848277,
      -0.007880289104928245,
      0.0664029021294729,
      -0.0729038234874446,
      -0.00005849791174519834,
      0.05628409460964408,
      -0.0525134329294473,
      0.026329003448584205,
      0.00027114381194760643,
      -0.03677629552114248,
      0.06743018479714939,
      -0.0560894442193289,
      0.0009722394088121363,
      0.05676876756757213,
      -0.07775650809213645,
      0.05330627422911416,
      0.0009941073749156226,
      -0.055674804078696793,
      0.07874009379691002,
      -0.055674804078696793,
      0.0009941073749156226,
      0.05330627422911416,
      -0.07775650809213645,
      0.05676876756757213,
      0.0009722394088121363,
      -0.0560894442193289,
      0.06743018479714939,
      -0.03677629552114248,
      0.00027114381194760643,
      0.026329003448584205,
      -0.0525134329294473,
      0.05628409460964408,
      -0.00005849791174519834,
      -0.0729038234874446,
      0.0664029021294729,
      -0.007880289104928245,
      0.04299467480848277,
      0.03663651655662163,
      0
   ];
nValue :DINT; // Temporary variable
END_VAR

Execution part:

// Example program 5:
// writes filter coefficients of
// "User defined FIR Filter" (32)
// incl. example coefficients for band pass
// Note: writing possible, if CoE Object
// PAI Settings Ch.1 (0x8000:16) has value 32 or 33 set, only!
// (32 = User defined FIR Filter / 33 = User defined IIR Filter)
// ===============================================================
CASE wState OF
   0:
      fb_coe_write(bExecute := FALSE);// Prepare CoE access
      wState := wState + 1;// Go to next state
   1:
      //nValue := REAL_TO_DINT(DINT_TO_REAL(aFilterCoeffs[index]) *16384);
      nValue := LREAL_TO_DINT(aFilterCoeffs[index] * 1073741824); // Bit-shift factor: 2^30
      // Write filter coefficients (max. 40 entries)
      fb_coe_write(
         sNetId:= userNetId,
         nSlaveAddr:= userSlaveAddr,
         nSubIndex:= index,
         nIndex:= wCoEIndexUserFilterCoeffizents,
         pSrcBuf:= ADR(nValue),
         cbBufLen:= SIZEOF(nValue),
         bExecute:= TRUE,
         tTimeout:= T#1S
      );
      wState := wState + 1; // Go to next state
   2:
      // Execute writing to CoE
      fb_coe_write();
      IF fb_coe_write.bError THEN
         wState := 100; // Error case
      ELSE
         IF NOT fb_coe_write.bBusy THEN
         index := index + 1;
            IF index <= (NumOfFilterCoeff) THEN
               fb_coe_write(bExecute := FALSE);// Prepare the next CoE access
               wState := 1;// Write next value
            ELSE
               wState := 255;// Done
            END_IF
         END_IF
      END_IF
   100:
      ; // Error handling
   255:
      ; // Go on..
END_CASE