Sample program 3 (write LookUp table)

Download TwinCAT 3 project: program link

Program description

Transmission of LookUp table interpolation values for mapping of an equation f(x) = x3 via CoE into the box module.

Variable declaration sample program 3

PROGRAM MAIN
VAR
   //LookUp-Table (LUT) generated by: MBE * x³
   aLUT:ARRAY[0..99] OF DINT :=
   [
      -7812500,-7812500,-7493593,-6894382,
      -7174765,-6051169,-6855859,-5279674,-6536953,-4576709,
      -6218125,-3939087,-5899218,-3363620,-5580390,-2847120,
      -5261484,-2386402,-4942578,-1978275,-4623750,-1619555,
      -4304843,-1307052,-3985937,-1037580,-3667109,-807951,
      -3348203,-614978,-3029375,-455472,-2710468,-326248,
      -2391562,-224117,-2072734,-145892,-1753828,-88385,
      -1434921,-48409,-1116093,-22776,-797187,-8300,
      -478281,-1792,-159453,-66,159453,66,
      478281,1792,797187,8300,1116093,22776,
      1434921,48409,1753828,88385,2072734,145892,
      2391562,224117,2710468,326248,3029375,455472,
      3348203,614978,3667109,807951,3985937,1037580,
      4304843,1307052,4623750,1619555,4942578,1978275,
      5261484,2386402,5580390,2847120,5899218,3363620,
      6218125,3939087,6536953,4576709,6855859,5279674,
      7174765,6051169,7493593,6894382,7812500,7812500
   ];
   // For CoE 0x8000 and 0x8005 - write values:
   // ===============================================
   wCoEIndexScaler :WORD := 16#8005; // CoE Index
   wState          :BYTE := 0; // Write status
   fb_coe_writeEx  :FB_EcCoESdoWriteEx; // Function Block for writing in CoE
   userNetId       :T_AmsNetId := '172.128.1.1.5.1'; // Have to be entered
   userSlaveAddr   :UINT := 1003; // Have to be entered
   bWriteLUT2CoE   :BOOL:=FALSE; // Sign for start writing
   bError          :BOOL:=FALSE; // Sign for any error
END_VAR

Remarks:

Sample program for transferring the LookUp table:

Execution part:

// Example program 3:
// ###### Write LookUp table into CoE object 0x8005: #######
IF bWriteLUT2CoE THEN
CASE wState OF
   0:
      fb_coe_writeEx(bExecute := FALSE);// Prepare CoE-Access
      wState := wState + 1;// Next state
   1:
      // Write 100 X/Y LookUp-Table entries
      fb_coe_writeEx(
         sNetId:= userNetId,
         nSlaveAddr:= userSlaveAddr,
         nSubIndex:= 1,
         nIndex:= wCoEIndexScaler,
         pSrcBuf:= ADR(aLUT),
         cbBufLen:= SIZEOF(aLUT),
         bCompleteAccess:= TRUE,
         bExecute:= TRUE
         );
      wState := wState + 1; // Next state
   2:
      // Proceed with writing to CoE
      fb_coe_writeEx();
      IF NOT fb_coe_writeEx.bBusy THEN
         wState := 0;// Done
         bWriteLUT2CoE := FALSE;
         bError := fb_coe_writeEx.bError; // See nErrId if TRUE
      END_IF
END_CASE
END_IF

A simple variable query, e.g., via button linked with bEnable, can be used to initiate the transfer. The variable declaration must contain

VAR_INPUT
   bEnable   AT%I*   :BOOL;
END_VAR

and the following program lines:

IF bEnable AND NOT startWrite THEN
   bWriteLUT2CoE := TRUE;
END_IF