You are on page 1of 63

TTTech Off Highway Division

TTControl GmbH

Control Library User Manual

Product version 1.1 of 30-Jul-2011


Manual edition 1.2 of 30-Jul-2011
Document number D-TTCSW-M-20-001
TTControl GmbH Tel.: +39 0472 268011
Via Julius Durst 66 Fax: +39 0472 268014
39042 Bressanone Web: http://www.ttcontrol.com
Italy E-mail: office@ttcontrol.com

TTTech Computertechnik AG Tel.: +43 1 585 34 34–0


Schoenbrunner Strasse 7 Fax: +43 1 585 34 34–90
A–1040 Vienna Web: http://www.tttech.com
Austria E-mail: support@tttech.com

Copyright Document number D-TTCSW-M-20-001


The data in this document may not be altered or amended without special notification from
TTControl GmbH. TTControl GmbH undertakes no further obligation in relation to this
document. The software described in it can only be used if the customer is in possession
of a general license agreement or single license.
Using and copying is only allowed in concurrence with the specifications stipulated in the
contract. Under no circumstances may any part of this document be copied, reproduced,
transmitted, stored in a retrieval system, or translated into another language without writ-
ten permission of TTControl GmbH.
The names and designations used in this document are trademarks or brands belonging to
the respective owners.
Copyright  2010 TTControl GmbH. All rights reserved.
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Bistable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1 cl d latch dint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 cl d latch real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 cl jk flip flop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Bit and Byte Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1 cl get bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 cl set bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 cl make word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 cl make dword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.5 cl split word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.6 cl split dword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.7 cl pack bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.8 cl unpack bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.1 cl ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 cl floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3 cl round . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 cl sign dint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.5 cl sign real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.6 cl dead zone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.7 cl gradient limiter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.8 cl scaling dint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.9 cl scaling real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.10 cl 3 pos analog input . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.11 cl range check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.12 cl binary search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.13 cl crc32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.1 cl pt1 filter dint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.2 cl pt1 filter real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.3 cl mov average dint 10 . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Control Library User Manual
ii Contents

5.4 cl mov average real 10 . . . . . . . . . . . . . . . . . . . . . . . . . . 32


5.5 cl mov average dint 50 . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.6 cl mov average real 50 . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6 Signal Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1 cl blink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2 cl hysteresis dint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3 cl hysteresis real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.4 cl debounce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.5 cl dither . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.6 cl pid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.7 cl start stop ramp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.8 cl toggle rising edge . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.9 cl toggle rising edge2 . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.10 cl toggle falling edge . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
6.11 cl toggle falling edge2 . . . . . . . . . . . . . . . . . . . . . . . . . . 51
6.12 cl lookup table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
6.13 cl characteristic curve . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7 Safe Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8 Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.1 cl version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9 Icons and Symbols Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
1 Introduction
The Control Library is a comprehensive set of functions, templates and examples de-
veloped for all CoDeSys®-enabled TTControl ECUs including the TTC 200 family, the
TTC 50 family as well as the Vision family.
This user manual describes the function provided with the Control Library in detail.
Control Library User Manual
2 2 Bistable Functions

2 Bistable Functions

2.1 cl d latch dint


This function block implements a simple memory element known as D-Latch. Whenever
the clock-input enable is TRUE the D-Latch passes the input directly through to the output
and stores it in an internal memory. If the clock-input is FALSE, the D-Latch outputs the
value stored in the memory.

CoDeSys block

Input ports

• input of type DINT


Input value.
• enable of type BOOL
Enable input. If clock is TRUE the input is latched through to the output and stored
internally. If clock is FALSE the value from the previous cycle is returned.

Output ports

This function returns the variable output of type DINT containing the result of the latch
element.

Example

VAR
res : DINT;

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
2.2 cl d latch real 3

fb_latch : cl_d_latch_dint;
END_VAR

(* The following statement returns the value 10 *)


fb_latch(input := 10, enable := TRUE, output => res);

(* The following statement returns the stored value 10,


because the enable flag is set to FALSE *)
fb_latch(input := 5, enable := FALSE, output => res);

(* The following statement returns the value 5 *)


fb_latch(input := 5, enable := TRUE, output => res);

2.2 cl d latch real

This function block implements a simple memory element known as D-Latch. Whenever
the clock-input enable is TRUE the D-Latch passes the input directly through to the output
and stores it in an internal memory. If the clock-input enable is FALSE, the D-Latch
outputs the value stored in the memory.

CoDeSys block

Input ports

• input of type REAL


Input value.
• enable of type BOOL
Enable input. If clock is TRUE the input is latched through to the output and stored
internally. If clock is FALSE the value from the previous cycle is returned.

Output ports

This function returns the variable output of type REAL containing the result of the latch
element.

Example

VAR

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
4 2 Bistable Functions

res : REAL;
fb_latch : cl_d_latch_real;
END_VAR

(* The following statement returns the value 10 *)


fb_latch(input := 10, enable := TRUE, output => res);

(* The following statement returns the stored value 10,


because the enable flag is set to FALSE *)
fb_latch(input := 4.5, enable := FALSE, output => res);

(* The following statement returns the value 4.5 *)


fb_latch(input := 4.5, enable := TRUE, output => res);

2.3 cl jk flip flop


This function block implements a JK Flip Flop. The output is determined by the state
of the inputs J and K at a rising edge of the clock signal. If no rising edge is detected
at the clock input, the value of the previous cycle is output. The characteristic equation
is Q = J · NOT (QPrev ) + NOT (K) · Q prev where Q prev denotes the output of the function
block in the previous cycle. The truth table for this function block is as follows:
J K Q
FALSE FALSE Q prev (unchanged)
FALSE TRUE FALSE (reset)
TRUE FALSE TRUE (set)
TRUE TRUE NOT (Q prev ) (toggle)

CoDeSys block

Input ports

• clock of type BOOL


Clock input. On a state transition of clock from FALSE to TRUE the j and k in-
put parameters are evaluated according to the truth table depicted in the previous
paragraph.
• j of type BOOL
J-input of flip flop. See truth table for effect on the flip flop output state.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
2.3 cl jk flip flop 5

• k of type BOOL
K-input of flip flop. See truth table for effect on the flip flop output state.

Output ports

• q of type BOOL
Output of JK Flip Flop. Output is defined by the truth table depicted in the previous
paragraph.
• qn of type BOOL
Negated output. Corresponds to NOT (Q).

Example

VAR
fb_jk_flip_flop : cl_jk_flip_flop;
q : BOOL;
END_VAR

(* q toggles at every rising edge *)


fb_jk_flip_flop(clock := TRUE, j := TRUE, k := TRUE, q => q);
fb_jk_flip_flop(clock := FALSE, j := TRUE, k := TRUE, q => q);

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
6 3 Bit and Byte Manipulation

3 Bit and Byte Manipulation

3.1 cl get bit


This function extracts one bit out of a WORD variable and converts it to a BOOLean value.

CoDeSys block

Input ports

• input of type WORD


Input value from which one bit shall be extracted.
• bit of type BYTE
A value between 0 (LSB) and 15 (MSB) specifying which bit shall be returned.

Output ports

This function returns a value of type BOOL, which is TRUE if the selected bit is set to 1 and
returns FALSE if the selected bit is 0.

Example

VAR
res : BOOL;
END_VAR

(* The following statement returns TRUE *)


res := cl_get_bit(16#0003, 1);

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
3.2 cl set bit 7

(* The following statement returns FALSE *)


res := cl_get_bit(16#0003, 2);

This function is particularly useful in case the bit-position to be extracted is not a con-
stant (as in the example above), but a variable. For constants the following, easier-to-use,
CoDeSys syntax is recommended:

VAR
res : BOOL;
value : WORD := 16#0003;
END_VAR

(* The following statement returns TRUE *)


res := value.1;

(* The following statement returns FALSE *)


res := value.2;

3.2 cl set bit


This function sets one bit in a WORD variable to TRUE or FALSE and returns the modified
value.

CoDeSys block

Input ports

• input of type WORD


Input variable which should be modified.
• bit of type BYTE
Bit position that shall be set. Has to be between 0 (LSB) and 15 (MSB).
• value of type BOOL
Value to which the selected bit shall be set. Can be TRUE (1) or FALSE (0).

Output ports

This function returns a value of type WORD, containing the modified input variable.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
8 3 Bit and Byte Manipulation

Example

VAR
value1 : WORD := 16#0000;
value2 : WORD := 16#FFFF;
END_VAR

(* The following statement sets value1 to 16#0004 *)


value1 := cl_set_bit(value1, 2, TRUE);

(* The following statement sets value2 to 16#FFFB *)


value2 := cl_set_bit(value2, 2, FALSE);

This function is particularly useful in case the bit-position to be set is not a constant (as in
the example above), but a variable. For constants the following, easier-to-use, CoDeSys
syntax is recommended:

VAR
value1 : WORD := 16#0000;
value2 : WORD := 16#FFFF;
END_VAR

(* The following statement sets value1 to 16#0004 *)


value1.2 := TRUE;

(* The following statement sets value2 to 16#FFFB *)


value2.2 := FALSE;

3.3 cl make word


This function takes two BYTE values as input and packs them into a WORD variable.

CoDeSys block

Input ports

• byet0 of type BYTE


Least significant byte that should be combined to a WORD value.
• byte1 of type BYTE
Most significant byte that should be combined to a WORD value.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
3.4 cl make dword 9

Output ports

This function returns a value of type WORD with byte0 as its least significant byte and
byte1 as its most significant byte.

Example

VAR
my_word : WORD;
END_VAR

(* The following statement sets my_word to 16#0102 *)


my_word := cl_make_word(16#02, 16#01);

3.4 cl make dword

This function takes four BYTE values as input and packs them into a DWORD variable.

CoDeSys block

Input ports

• byet0 of type BYTE


Least significant byte that should be combined to a DWORD value.
• byet1 of type BYTE
Byte 1 that should be combined to a DWORD value.
• byet2 of type BYTE
Byte 2 that should be combined to a DWORD value.
• byte3 of type BYTE
Most significant byte that should be combined to a DWORD value.

Output ports

This function returns a value of type DWORD, byte0 as its least significant byte, then byte
1 and byte 2, and byte3 as its most significant byte.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
10 3 Bit and Byte Manipulation

Example

VAR
my_dword : DWORD;
END_VAR

(* The following statement sets my_word to 16#01020304 *)


my_word := cl_make_word(16#04, 16#03, 16#02, 16#01);

3.5 cl split word

This program takes one WORD value as input and splits it into two BYTE variables.

CoDeSys block

Input ports

• input of type WORD


Variable that should be split into its individual bytes.

Output ports

• byte0 of type BYTE


Least significant byte of input WORD.
• byte1 of type BYTE
Most significant byte of input WORD.

Example

VAR
byte0 : BYTE;
byte1 : BYTE;
END_VAR

(* The following statement assigns the value 16#01 to


byte1 and 16#02 to byte0 *)
cl_split_word(input := 16#0102, byte0 => byte0, byte1 => byte1);

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
3.6 cl split dword 11

3.6 cl split dword

This program takes one DWORD value as input and splits it into four BYTE variables.

CoDeSys block

Input ports

• input of type DWORD


Variable that should be split into its individual bytes.

Output ports

• byte0 of type BYTE


Least significant byte of input DWORD.
• byte1 of type BYTE
Second byte of input DWORD.
• byte2 of type BYTE
Third byte of input DWORD.
• byte3 of type BYTE
Most significant byte of input DWORD.

Example

VAR
byte0 : BYTE;
byte1 : BYTE;
byte2 : BYTE;
byte3 : BYTE;
END_VAR

(* The following statement assigns the value 16#01 to byte3


16#02 to byte2, 16#03 to byte 1 and
16#04 to byte0 *)
cl_split_dword(input := 16#01020304,
byte0 => byte0,
byte1 => byte1,

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
12 3 Bit and Byte Manipulation

byte2 => byte2,


byte3 => byte3);

3.7 cl pack bits

This function takes eight BOOLean variables and packs them into one BYTE variable.

CoDeSys block

Input ports

• bit0 . . . bit7 of type BOOL


Input values which are packed into one BYTE. bit0 specifies the LSB, bit7 the
MSB of the BYTE.

Output ports

This function returns a value of type BYTE, containing the packed input values.

Example

VAR
res : BYTE;
END_VAR

(* The following statement returns 16#07 *)


res := cl_pack_bits(TRUE, TRUE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE);

3.8 cl unpack bits

This POU splits a BYTE into its single bits of type BOOL.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
3.8 cl unpack bits 13

CoDeSys block

Input ports

• input of type BOOL


Input value to be unpacked.

Output ports

This function returns eight BOOLean variables, bit0 . . . bit7. The output variable bitx is
TRUE if the bit at position x is 1, else it is FALSE.

Example

VAR
bit0 : BOOL;
bit1 : BOOL;
bit2 : BOOL;
END_VAR

(* In the following statement the variables


bit0 and bit1 return TRUE and bit2 returns FALSE *)
cl_unpack_bits(input:= 16#03, bit0 => bit0, bit1 => bit1, bit2 => bit2);

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
14 4 Mathematical Functions

4 Mathematical Functions

4.1 cl ceil

This function rounds the input value up to the closest integer value. For both positive and
negative REAL input values the rounding direction is towards the next larger integer value.

CoDeSys block

Input ports

• input of type REAL


Input value to be rounded.

Output ports

This function returns a variable of type DINT containing the rounded value.

Example

VAR
res : DINT;
END_VAR

(* In the following statement the value


4 is returned *)
res := ceil(3.9);

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.2 cl floor 15

(* In the following statement the value


-3 is returned *)
res := ceil(-3.5);

4.2 cl floor

This function rounds the input value down to the closest integer value. For both positive
and negative REAL input values the rounding direction is towards the next smaller integer
value.

CoDeSys block

Input ports

• input of type REAL


Input value to be rounded.

Output ports

This function returns a variable of type DINT containing the rounded value.

Example

VAR
res : DINT;
END_VAR

(* In the following statement the value


3 is returned *)
res := floor(3.9);

(* In the following statement the value


-4 is returned *)
res := floor(-3.5);

4.3 cl round

This function rounds the input value towards the closest value with n decimals.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
16 4 Mathematical Functions

CoDeSys block

Input ports

• input of type REAL


Input value to be rounded.
• n of type INT
Number of decimals. If 0, this function behaves like a standard round function.
Maximum allowed value is 5.

Output ports

This function returns a variable of type REAL containing the input value rounded to the
number of specified decimals.

Example

VAR
res : REAL;
END_VAR

(* In the following statement the value


3 is returned *)
res := round(3.499999, 0);

(* In the following statement the value


-4 is returned *)
res := round(-3.5, 0);

(* In the following statement the value


1.235 is returned *)
res := round(1.234567, 3);

4.4 cl sign dint

This function applies the sign function to the specified input value of type DINT.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.5 cl sign real 17

CoDeSys block

Input ports

• input of type DINT


Input value on which the sign function is applied.

Output ports

This function returns a variable of type DINT. The following value is returned:

• if input = 0 then output = 0


• if input < 0 then output = -1
• if input > 0 then output = 1

Example

VAR
res : DINT;
END_VAR

(* The following statement returns -1 *)


res := cl_sign_dint(-3);

(* The following statement returns +1 *)


res := cl_sign_dint(3);

(* The following statement returns 0 *)


res := cl_sign_dint(0);

4.5 cl sign real


This function applies the sign function to the specified input value of type REAL.

CoDeSys block

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
18 4 Mathematical Functions

Input ports

• input of type REAL


Input value on which the sign function is applied.

Output ports

This function returns a variable of type REAL. The following value is returned:

• if input = 0 then output = 0


• if input < 0 then output = -1
• if input > 0 then output = 1

Example

VAR
res : REAL;
END_VAR

(* The following statement returns -1 *)


res := cl_sign_real(-3.5);

(* The following statement returns +1 *)


res := cl_sign_real(0.001);

(* The following statement returns 0 *)


res := cl_sign_real(0);

4.6 cl dead zone

This function implements a dead-zone. The function returns 0 as long as the input value is
within a certain, predefined range, else it returns the input value minus the specified offset
(start for input values ≤ start and end for input values ≥ end).

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.6 cl dead zone 19

CoDeSys block

Input ports

• input of type REAL


Input value to which the dead zone shall be applied.
• start of type REAL
Lower boundary for dead zone. The dead zone is applied for values start ≤ in ≤
end.
• end of type REAL
Upper boundary for dead zone. The dead zone is applied for values start ≤ in ≤
end.

Output ports

This function returns a variable of type REAL, which is the input value with applied dead
zone.

Example

VAR

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
20 4 Mathematical Functions

res : REAL;
END_VAR

(* In the following statement the value


7 is returned (12 - 5) *)
res := dead_zone(12, 0, 5);

(* In the following statement the value


0 is returned *)
res := dead_zone(3, 0, 5);

4.7 cl gradient limiter


This utility function limits the gradient (i.e. slope, “steepness”) of an input signal. This
function is typically used to avoid abrupt signal changes or to implement simple linear
ramps.

The basic formula that this function applies to the input signal is:

IF input < (old_input - down_gradient) THEN


cl_gradient_limiter := old_input - down_gradient;
ELSIF input > (old_input + up_gradient) THEN
Gradient_Limiter := old_input + up_gradient;
ELSE
cl_gradient_limiter := input;
END_IF

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.8 cl scaling dint 21

CoDeSys block

Input ports

• input of type DINT


Input value to which the gradient limiting will be applied to.
• down gradient of type DINT
This variable defines the maximum value that the output signal will be decreased
within one single function execution. The function will limit the gradient by this
value in case input < (old value − down gradient).
• up radient of type DINT
This variable defines the maximum value that the output signal will be increased
within one single function execution. The function will limit the gradient by this
value in case input > (old value + up gradient).

Output ports

This function returns the gradient-limited output value of type DINT.

4.8 cl scaling dint

This utility function maps a value input from an input range [inmin , inmax ] to a value output
of an output range [outmin , outmax ]. A typical application for this function is the scaling of
a value to percent.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
22 4 Mathematical Functions

CoDeSys block

Input ports

• input of type DINT


Input value that should be scaled from from range [in min, in max] to range
[out min, out max].
• in min of type DINT
Lower boundary of input range.
• in max of type DINT
Upper boundary of input range.
• out min of type DINT
Lower boundary of output range.
• out max of type DINT
Upper boundary of output range.
• saturation of type BOOL
If set to TRUE the output will be limited to [out min, out max], if set to FALSE the
output of out-of-boundary inputs will be calculated through linear extrapolation.

Output ports

This function returns the scaled output value of type DINT.

Example

VAR
res : DINT;
END_VAR

(* In the following statement the value


700 is returned *)
res := cl_scaling_dint(7, 0, 10, 0, 1000, FALSE);

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.9 cl scaling real 23

4.9 cl scaling real

This utility function maps a value input from an input range [inmin , inmax ] to a value output
of an output range [outmin , outmax ]. A typical application for this function is the scaling of
a value to percent. It is the same function as cl scaling dint, but operates on REAL
values instead of DINT.

CoDeSys block

Input ports

• input of type REAL


Input value that should be scaled from from range [in min, in max] to range
[out min, out max].
• in min of type REAL
Lower boundary of input range.
• in max of type REAL
Upper boundary of input range.
• out min of type REAL
Lower boundary of output range.
• out max of type REAL
Upper boundary of output range.
• saturation of type BOOL
If set to TRUE the output will be limited to [out min, out max], if set to FALSE the
output of out-of-boundary inputs will be calculated through linear extrapolation.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
24 4 Mathematical Functions

Output ports

This function returns the scaled output value of type REAL.

Example

VAR
res : REAL;
END_VAR

(* In the following statement the value


0.75 is returned *)
res := cl_scaling_real(7.5, 0, 10, 0, 1, FALSE);

4.10 cl 3 pos analog input

This function maps an input signal range [in min . . . in mid . . . in max] to an output signal
range [out min . . . out mid . . . out max]. Furthermore a deadband can be applied around
in mid. A typical use for this function is the scaling of sensor inputs that have three defined
positions, where the middle position represents the “neutral” or “off” state. An example
for this kind of input device is an analog joystick. The following graph shows an example
of the mapping from the input range to the output range with a specified deadband.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.11 cl range check 25

CoDeSys block

Input ports

• input of type DINT


Input value to be scaled.
• in min of type DINT
Smallest possible input value.
• in mid of type DINT
Input value in “neutral” position, e. g. joystick center position.
• in max of type DINT
Largest possible input value.
• out min of type DINT
Minimum value for output range.
• out mid of type DINT
Output value for “neutral” or “center” position.
• out max of type DINT
Maximum value for output range.
• deadband of type UINT
If input is in between [in mid − deadband, in mid + deadband] then out mid is
returned.

Output ports

This function returns the scaled value of type DINT.

4.11 cl range check

This utility function checks whether an input value is within a predefined range. Unlike the
standard function LIMIT, this function block allows also to specify a debouncing period,
i.e. the value is flagged to be out of range only after violating the specified boundaries for
the time specified in the debouncing parameter.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
26 4 Mathematical Functions

CoDeSys block

Input ports

• input of type DINT


Input value that is range-checked.
• limit min of type DINT
Lower boundary of allowed range.
• limit max of type DINT
Upper boundary of allowed range.
• debounce of type TIME
The input value is flagged to be out of range only after violating the range for the
time specified with this parameter.

Output ports

This function block returns the variable output of BOOL. output is TRUE if a range vio-
lation was detected (after debouncing), else the value is FALSE.

Example

VAR
range_check_fb : cl_range_check;
res : BOOL;
END_VAR

(* res turns TRUE after 5 seconds *)


range_check_fb(input := 5,
limit_min := 10,
limit_max := 20,
debounce := t#5s,
output => res);

4.12 cl binary search

This function implements a binary search algorithm.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
4.13 cl crc32 27

CoDeSys block

Input ports

• data of type DWORD


Address for a variable of type ARRAY OF REAL, containing a list of ascendingly
sorted values in which an element should be searched for.
• nr elements of type WORD
Number of elements in the data list.
• value of type REAL
The value that should be searched in the data list.
• pleft of type POINTER TO WORD
This variable returns the index of the searched element, if the element was found,
else it returns the index of the next-smaller element in the data list.
• pright of type POINTER TO WORD
This variable returns the index of the searched element, if the element was found,
else it returns the index of the next-larger element in the data list.

Output ports

This function returns a variable of type BOOL. The return value is TRUE if the element was
found, else FALSE is returned.

4.13 cl crc32
This function implements the calculation of a CRC (Cyclic Redundancy Check) sum. The
CRC polynomial used is a reversed CRC-32-IEEE 802.3, i.e. 16#EDB88320 with a final
XOR operation with the value 16#FFFFFFFF.

CoDeSys block

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
28 4 Mathematical Functions

Input ports

• crc of type DWORD


Initial value of CRC. Typically 0.
• buf of type POINTER TO BYTE
Address to the data for which the CRC shall be calculated.
• length of type DWORD
Length in bytes for the data for which the CRC shall be calculated

Output ports

This function returns a variable of type DWORD containing the calculated CRC.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
5 Filters 29

5 Filters

5.1 cl pt1 filter dint


This utility function implements a very simple filter. It is typically used to reduce noise in
incoming sensor signals. From the signal processing point-of-view this filter is a infinite
impulse response (IIR) filter with a first order feedback, following the following basic
formula:
x[n] + α · y[n − 1]
y[n] =
1+α
where y[n] is the new output, x[n] is the current input and y[n − 1] is the previous output
of this function.
In order to initialize the filter to a certain value, set input to the desired value and set
filter range to 0 for one cycle.

CoDeSys block

Input ports

• input of type DINT


Input value that shall be filtered.
• alpha of type DINT
Filter constant. The higher this value the stronger the input is filtered. A value of 0
results in no filtering of the input.
• filter range of type DINT
If the difference between the new input value and the previous value is larger than
filter range, the filter is not applied. This allows the filtering of low-amplitude
noise but keeps the filter reactive to larger signal changes.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
30 5 Filters

Output ports

This function returns the filtered output value of type DINT.


The following diagram shows an example for a filtered signal with Alpha = 100 and
FilterRange = 50.

5.2 cl pt1 filter real

This utility function implements a very simple filter. It is typically used to reduce noise in
incoming sensor signals. From the signal processing point-of-view this filter is a infinite
impulse response (IIR) filter with a first order feedback, following the following basic
formula:

x[n] + α · y[n − 1]
y[n] =
1+α

where y[n] is the new output, x[n] is the current input and y[n − 1] is the previous output
of this function. It is the same as cl pt1 filter dint, but works with REAL values.
In order to initialize the filter to a certain value, set input to the desired value and set
filter range to 0 for one cycle.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
5.3 cl mov average dint 10 31

CoDeSys block

Input ports

• input of type REAL


Input value that shall be filtered.
• alpha of type DINT
Filter constant. The higher this value the stronger the input is filtered. A value of 0
results in no filtering of the input.
• filter range of type REAL
If the difference between the new input value and the previous value is larger than
filter range, the filter is not applied. This allows the filtering of low-amplitude
noise but keeps the filter reactive to larger signal changes.

Output ports

This function returns the filtered output value of type REAL.

5.3 cl mov average dint 10

This POU implements a moving average filter over 10 DINT values. It is typically used to
reduce noise in incoming sensor signals.

CoDeSys block

Input ports

• input of type DINT


Input value that shall be filtered.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
32 5 Filters

Output ports

This function returns the filtered output value of type DINT.


The following diagram shows an example for a signal filtered with this POU.

5.4 cl mov average real 10

This POU implements a moving average filter over 10 REAL values. It is typi-
cally used to reduce noise in incoming sensor signals. This POU is equivalent to
cl mov average dint 10, but works with REAL values.

CoDeSys block

Input ports

• input of type REAL


Input value that shall be filtered.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
5.5 cl mov average dint 50 33

Output ports

This function returns the filtered output value of type REAL.

5.5 cl mov average dint 50

This POU implements a moving average filter over up to 50 DINT values. It


is typically used to reduce noise in incoming sensor signals. Compared with
cl mov average dint 10, this block provides stronger filtering and more flexibility.
With the additional parameter nr elements it is possible to specify over how many el-
ements the average shall be computed. Please note that the POU has high memory re-
quirements as it needs to reserve space for 50 DINT variables. If many values need to be
filtered, it might be more efficient to use cl pt1 filter dint instead.

CoDeSys block

Input ports

• input of type DINT


Input value that shall be filtered.
• nr elements of type BYTE
Number of elements over which the average is calculated. Allowed range is
[0 . . . 50].

Output ports

This function returns the filtered output value of type DINT.

5.6 cl mov average real 50

This POU implements a moving average filter over up to 50 REAL values. It is typ-
ically used to reduce noise in incoming sensor signals. This POU is equivalent to
cl mov average real 50, but works with REAL values.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
34 5 Filters

CoDeSys block

Input ports

• input of type REAL


Input value that shall be filtered.
• nr elements of type BYTE
Number of elements over which the average is calculated. Allowed range is
[0 . . . 50].

Output ports

This function returns the filtered output value of type REAL.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6 Signal Manipulation 35

6 Signal Manipulation

6.1 cl blink
This function block outputs a configurable square wave signal. Typical use for this func-
tion is the implementation of a blinking light like the turn signals or other devices that
need to be enabled / disabled periodically.

CoDeSys block

Input ports

• enable of type BOOL


Enables the function block.
• high time of type TIME
Time for which the output is set to TRUE
• low time of type TIME
Time for which the output is set to FALSE
• hold of type BOOL
If this input is set to TRUE the output holds its current value after disabling the block,
if hold is set to FALSE then the output will switch to FALSE as soon as the function
block is disabled.
• timer continue of type BOOL
If this input is set to FALSE the timers will start from 0 after re-enabling the function
block, if this variable is set to TRUE, the timer will resume its operation with the
timings it had before being disabled.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
36 6 Signal Manipulation

Output ports

This function returns an output of type BOOL.

The following diagram shows different examples of this function block, with high time
set to t#1s and low time set to t#0.5s. Different combinations of the hold and
timer continue values are shown.

6.2 cl hysteresis dint

This function implements a hysteresis for DINT numbers allowing the user to define two
distinct points for enabling and disabling the output value.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.2 cl hysteresis dint 37

CoDeSys block

Input ports

• input of type DINT


Input value to which the hysteresis is applied. If input ≤ switch off, then output
switches to FALSE, if input ≥ switch on, then output switches to TRUE, else
output outputs the value from the previous cycle.
• switch on of type DINT
Switch-on point for the hysteresis.
• switch off of type DINT
Switch-off point for the hysteresis.

Output ports

This function returns a variable of type BOOL containing the result of the hysteresis func-
tion.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
38 6 Signal Manipulation

Example

VAR
res : BOOL;
fb_hysteresis : cl_hysteresis_dint;
END_VAR

(* In the following statement the value


FALSE is returned *)
fb_hysteresis(input := 1, switch_on := 100, switch_off := 10, output => res);

(* In the following statement the value


TRUE is returned *)
fb_hysteresis(input := 120, switch_on := 100, switch_off := 10, output => res);

(* In the following statement the value


TRUE is returned *)
fb_hysteresis(input := 11, switch_on := 100, switch_off := 10, output => res);

(* In the following statement the value


FALSE is returned *)
fb_hysteresis(input :=9, switch_on := 100, switch_off := 10, output => res);

6.3 cl hysteresis real

This function implements a hysteresis for REAL numbers allowing the user to define two
distinct points for enabling and disabling the output value. It is the same function as
cl hysteresis dint, but operates on REAL values instead of DINT.

CoDeSys block

Input ports

• input of type REAL


Input value to which the hysteresis is applied. If input ≤ switch off, then output
switches to FALSE, if input ≥ switch on, then output switches to TRUE, else
output outputs the value from the previous cycle.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.4 cl debounce 39

• switch on of type REAL


Switch-on point for the hysteresis.
• switch off of type REAL
Switch-off point for the hysteresis.

Output ports

This function returns a variable of type BOOL containing the result of the hysteresis func-
tion.

Example

VAR
res : BOOL;
fb_hysteresis : cl_hysteresis_real;
END_VAR

(* In the following statement the value


FALSE is returned *)
fb_hysteresis(input := 1, switch_on := 100, switch_off := 10, output => res);

(* In the following statement the value


TRUE is returned *)
fb_hysteresis(input := 120, switch_on := 100, switch_off := 10, output => res);

(* In the following statement the value


TRUE is returned *)
fb_hysteresis(input := 11, switch_on := 100, switch_off := 10, output => res);

(* In the following statement the value


FALSE is returned *)
fb_hysteresis(input :=9, switch_on := 100, switch_off := 10, output => res);

6.4 cl debounce

This utility function implements a debouncing function. Digital input switches or sensors
very often do not provide a “clean” signal transition between the state open and close and
vice versa. This is due to the fact that a switch contact may “bounce” several times before
coming to its resting position. In order to get a reliable signal, this kind of digital input
signal is typically debounced, i. e. after a state transition is detected the new state is used
only after a certain period of time (the debouncing period) passed.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
40 6 Signal Manipulation

CoDeSys block

Input ports

• input of type BOOL


Input value that shall be debounced.
• debounce of type TIME
Debouncing time. Only if the state of the input signal remains constant for at least
the time specified in this variable, the output of this function is updated to the new
state.
• enable countdown of type BOOL
If this variable is TRUE, the state transition from FALSE to TRUE as well as transition
from TRUE to FALSE will be debounced. If the variable is FALSE, only the state
transition from FALSE to TRUE will be debounced and the output will switch directly
to FALSE as soon as the input changes to FALSE.

Output ports

This function returns the debounced output value of type BOOL.


The following diagram shows an example for a debounced signal with debounce = t#3s
and enable countdown = TRUE.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.5 cl dither 41

6.5 cl dither
This utility function can be used to add a dither to a PWM output signal. Dither is typi-
cally applied to PWM outputs controlling the solenoid of a valve in order to reduce static
friction and therefore increase the responsiveness and minimize the stiction induced hys-
terisis of the valve itself. The function allows to specify the amplitude and frequency of
the dither.

CoDeSys block

Input ports

• duty cycle of type UINT


Duty cycle for PWM output. A value between 0 and 65535.
• dither promille of type UINT
This value defines the “amplitude” of the dither in permille. For example a value
of 50 means that on one cycle 5% will be added to the input signal and on the next
those 5% will be subtracted of the input signal. The resulting output is limited by
duty cycle min and duty cycle max.
• duty cycle min of type UINT
Minimum duty cycle. A value between 0 and 65535. This is the lower boundary for
the output signal.
• duty cycle max of type UINT
Maximum duty cycle. A value between 0 and 65535. This is the upper boundary
for the output signal.
• dither period of type TIME
This input specifies the dither period.

Output ports

This function returns a value of type UINT containing the duty cycle with the applied
dither.

Example

VAR

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
42 6 Signal Manipulation

fb_dither : cl_dither;
END_VAR

(* limit set duty cycle to min and max -> e.g. 5% - 95% to
detect short circuits etc. *)

fb_dither(duty_cycle := 32767, (* 50%: 655535 * 0.50 *)


dither_permille := 50, (* 5% *)
duty_cycle_min := 3276, (* 5%: 655535 * 0.05 *)
duty_cycle_max:= 6258, (* 95%: 655535 * 0.95 *)
dither_period := t#10ms, (* 100 Hz *)
output => output);

The example shown above would generate the following PWM signal assuming that the
PWM output stage is configured with a frequency of 200 Hz.

6.6 cl pid

This function implements a PID (Proportional-Integral-Derivative) controller with anti-


wind-up and feed-forward functionality. A typical use for this controller is the control
of the current of the PWM outputs with current measurement. Using the provided set-
point and feedback, the controller calculates a new setpoint lying between min val and

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.6 cl pid 43

max val, trying to minimize the error between the setpoint and feedback value. The fol-
lowing formula is used for the PID:
f w gain p gain d gain i gain
104 ∑
out put = · set point + · error + · ∆(error) + · (error)
103 103 104
Please note that fw gain and p gain are scaled by a factor of 1 000 and d gain as well
as i gain by a factor of 10 000 to improve the resolution on small gains.

CoDeSys block

Input ports

• setpoint of type DINT


Desired setpoint. The PID will try to minimize the error between this setpoint and
the actual value. For PWM current control this is typically the desired current in
[mA].
• feedback of type DINT
Current value. Is fed back into the controller. For PWM current control this is typi-
cally the actual current in [mA].
• fw gain of type DINT
Gain constant for feed-forward control. A value of 0 disables the feed-forward con-
trol, a value of 1000 leads to a 1:1 loop through of the setpoint.
• p gain of type DINT
Proportional gain constant. A value of 0 disables the proportional term in the PID.
• i gain of type DINT
Integral gain constant. A value of 0 disables the integral term in the PID.
• d gain of type UDINT
Derivative gain constant. A value of 0 disables the derivative term in the PID.
• output min of type DINT
Lower boundary for output value. The output will saturate on this value.
• output max of type DINT
Upper boundary for output value. The output will saturate on this value.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
44 6 Signal Manipulation

• reset hold of type BYTE


This input can be used to reset or hold the integral term.
0. . . Integral term is automatically calculated by the PID controller
1. . . Resets the integral part to the value specified in reset val
2. . . Holds the integral part. The PID controller is not allowed to modify the value.
• reset val of type UINT
The integral part is assigned this value, if reset hold is set to 2.

Output ports

Primary output of this function block is the calculated setpoint which should be applied
to the system being controlled. In case of a PWM current control this is the duty cycle for
the PWM output stage.

• output of type DINT


Calculated setpoint.
• control error of type DINT
Returns the current error between setpoint and feedback.
• integrator of type DINT
Returns the current value of the integral term.

Example

VAR
pid_control : cl_pid;
END_VAR

(* Example using the default values for gains and the


current-feedback received from the PWM stages *)

pid_control(setpoint := 850, (* in mA *)
feedback := current_feedback, (* in mA *)
output => pwm_duty_cycle); (* 0..65535 *)

The following table shows the effect on the control loop response obtained by increasing
one of the gains.

Parameter Rise Time Overshoot Settling Time Steady-state Error


Kp Decrease Increase Small Change Decrease
Ki Decrease Increase Increase Eliminate
Kd Small Increase Decrease Decrease None

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.7 cl start stop ramp 45

For PIDs used for current control in combination with the PWM outputs with current mea-
surement typical values of the gains for controlling standard valve solenoids are (software
cycle time of 10 ms and PID output in range [0, 65535] are assumed):

• fw gain between 40 000 and 70 000


• i gain between 20 000 and 40 000
• p gain and d gain very small or 0.

The optimal settings depend on several factors including supply voltage, valve type, soft-
ware cycle time, acceptable overshoot and much more and may therefore deviate substan-
tially from the abovementioned gains.
Please note that the PID works over the full DINT range, but no internal over-/underflow
checks are performed by this function block.

6.7 cl start stop ramp

With this function two ramps can be specified, which can be used to define the ramp-up
and ramp-down of a signal. Typically this function is used to smoothly switch on and off
actuators (like proportional valves), by limiting the gradient (“steepness”) of the signal
and by predefining the “shape” of the signal. The following graph shows an example
signal, where a start and stop ramp have been applied.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
46 6 Signal Manipulation

CoDeSys block

Input ports

• input of type UINT


Input signal to which the ramp function shall be applied.
• start ramp of type ARRAY[0..5] OF UINT
This variable contains the definition of the start ramp. The user has to provide six
values in ascending order that define the output after 0 ms, start time / 1/5 ms,
start time / 2/5 ms, start time / 3/5 ms . . . and start time ms. The function
will then interpolate the output between the provided values (see the figure below).
The function uses the start ramp whenever the difference between the new input
value and the previously provided input value is positive.
• start time of type UINT
Defines the time in [ms] that is needed to go from the value defined in
start time[0] to the value defined in start time[5].
• stop ramp of type ARRAY[0..5] OF UINT
This variable contains the definition of the stop ramp. The user has to provide six

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.7 cl start stop ramp 47

values in descending order that define the output after 0 ms, stop time / 1/5 ms,
stop time / 2/5 ms, stop time / 3/5 ms . . . and stop time ms. The function will
then interpolate the output between the provided values (see the figure below). The
function uses the stop ramp whenever the difference between the new input value
and the previously provided input value is negative.
• stop time of type UINT
Defines the time in [ms] that is needed to go from the value defined in
stop time[0] to the value defined in stop time[5].

Please note that the first value of the start ramp has to be the same as the last value of
the stop ramp (i. e. the minimum value). Same restriction applies to the last value for the
start ramp and first value of the stop ramp (i. e. the maximum value). Typically the first
value for the ramps is 0, but other minimum values can be provided as well in case a
fixed offset is desired. Finally, the values provided in the start ramp need to be monoton-
ically increasing wheras the values provided in the stop ramp need to be monotonically
decreasing.

Output ports

This function block returns the variable output of type UINT containing the signal value
with the applied ramp function.
The following figure shows the definition of a start ramp using the above specified inputs.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
48 6 Signal Manipulation

Example

The following code shows a simple example with a linear start and stop ramp.

VAR
start_ramp : ARRAY[0..5] OF UINT := 0, 200, 400, 600, 800, 1000;
stop_ramp : ARRAY[0..5] OF UINT := 1000, 800, 600, 400, 200, 0;

fb_ramp : cl_start_stop_ramp;
input_signal : UINT;
output_signal : UINT;
END_VAR

fb_ramp(input := input_signal,
start_ramp := start_ramp,
start_time := t#2500ms,
stop_ramp := stop_ramp,
stop_time := t#1000ms,
output => output_signal);

6.8 cl toggle rising edge

This utility function toggles its BOOLean output on every rising edge on its input. A
typical use case for this function (in combination with a debouncing block) is the en-
abling / disabling of devices with a push button.

CoDeSys block

Input ports

• input of type BOOL


Whenever a rising edge is detected on this input variable, the output toggles its
state.

Output ports

This function returns a value of type BOOL which toggles its value at every rising edge on
the input of the function block.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.9 cl toggle rising edge2 49

Example

VAR
fb_toggle : cl_toggle_rising_edge;
output : BOOL;
END_VAR

(* Output remains FALSE *)


fb_toggle(input := FALSE, output => output);

(* This call sets output to TRUE *)


fb_toggle(input := TRUE, output => output);

(* Output remains TRUE *)


fb_toggle(input := FALSE, output => output);

(* This call sets output to FALSE *)


fb_toggle(input := TRUE, output => output);

6.9 cl toggle rising edge2

This utility function toggles its BOOLean output on every rising edge on its in-
put. A typical use case for this function (in combination with a debouncing
block) is the enabling / disabling of devices with a push button. Only difference to
cl toggle rising edge is the possibility to initialize the output variable.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
50 6 Signal Manipulation

CoDeSys block

Input ports

• input of type BOOL


Whenever a rising edge is detected on this input variable, the output toggles its
state.

In-Out ports

• toggle value of type BOOL


This function returns a value of type BOOL which toggles its value at every rising
edge on the input of the function block. This value can be initialized explicitely.

6.10 cl toggle falling edge

This utility function toggles its BOOLean output on every falling edge on its input. A
typical use case for this function (in combination with a debouncing block) is the en-
abling / disabling of devices with a push button.

CoDeSys block

Input ports

• input of type BOOL


Whenever a falling edge is detected on this input variable, the output toggles its
state.

Output ports

This function returns a value of type BOOL which toggles its value at every falling edge on
the input of the function block.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.11 cl toggle falling edge2 51

Example

VAR
fb_toggle : cl_toggle_falling_edge;
output : BOOL;
END_VAR

(* Output remains FALSE *)


fb_toggle(input := TRUE, output => output);

(* This call sets output to TRUE *)


fb_toggle(input := FALSE, output => output);

(* Output remains TRUE *)


fb_toggle(input := TRUE, output => output);

(* This call sets output to TRUE *)


fb_toggle(input := FALSE, output => output);

6.11 cl toggle falling edge2

This utility function toggles its BOOLean output on every falling edge on its in-
put. A typical use case for this function (in combination with a debouncing
block) is the enabling / disabling of devices with a push button. Only difference to
cl toggle rising edge is the possibility to initialize the output variable.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
52 6 Signal Manipulation

CoDeSys block

Input ports

• input of type BOOL


Whenever a falling edge is detected on this input variable, the output toggles its
state.

In-Out ports

• toggle value of type BOOL


This function returns a value of type BOOL which toggles its value at every falling
edge on the input of the function block. This value can be initialized explicitely

6.12 cl lookup table

This function implements a lookup-table with interpolation between values. A typical


example for the use of this function is a temperature sensor, where a limited, predefined
list of resistance-to-temperature mappings is given and the read sensor value should be
interpolated between the available mappings.

CoDeSys block

Input ports

• x data of type DWORD


Address for a variable of type ARRAY OF REAL, containing a list of ascendingly
sorted values, representing the x axis of the look-up table.
• y data of type DWORD
Address for a variable of type ARRAY OF REAL, containing a list of ascendingly
sorted values, representing the y axis of the look-up table.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
6.13 cl characteristic curve 53

• nr elements of type WORD


Number of elements in the provided arrays.
• x of type REAL
Input value on the x-axis for look-up table.

Output ports

This function returns a variable of type REAL. The variable is the y-value corresponding
to the input value on the x-axis.

6.13 cl characteristic curve

This function implements an optimized and easier-to-use lookup table for the mapping of
input sensors to outputs. A typical example for the use of this function is the mapping of
a linear potentiometric jostick input to a non-linear solenoid output current. The number
of data points is limited to 5 and the accepted data type is DINT.

CoDeSys block

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
54 6 Signal Manipulation

Input ports

• input of type DINT


Input value that needs to be mapped to an output value.
• in of type ARRAY[0..4] OF DINT
Array containing a list of ascendingly sorted values, representing the x axis of the
look-up table.
• ou of type ARRAY[0..4] OF DINT
Array containing a list of ascendingly sorted values, representing the y axis of the
look-up table.

Output ports

This function returns a variable of type DINT. The variable is the output value correspond-
ing to the specified input value.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
7 Safe Casts 55

7 Safe Casts
This class of POUs provide functionality for the downcasting of variables with overflow
and underflow detection. If the value of the input data type is larger / smaller than allowed
by the target data type, the output will be set to the maximum / minimum allowed value
and the detected overflow / underflow will be signaled through a dedicated error output.
Please note that all these POUs are defined as PROGRAM in order to be able to return both
the down-casted output value as well as the error flag.
All POUs follow the following naming scheme:
cl safecast <input datatype> to <output datatype>, e. g.:

• cl safecast dint to int


• cl safecast word to byte

The following section shows the exemplary interface description for the POU
cl safecast dint to int.

CoDeSys block

Input ports

• input of source data type. In this case DINT


Input value that shall be casted.

Output ports

The POU returns the value casted to the target data type. For this example the target data
type is INT.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
56 7 Safe Casts

• output of target data type. In this case INT


The function block returns the value casted to the target data type.
• minor of type DWORD
This variable returns the error code. The following values are defined:
– CL NO ERROR (0) . . . No error occured.
– CL OVERFLOW (1) . . . Overflow was detected and output value was set to max-
imum allowed value.
– CL UNDERFLOW (2) . . . Underflow was detected and output value was set to
minimum allowed value.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
8 Other Functions 57

8 Other Functions

8.1 cl version

This function returns the current version of the Control Library.

CoDeSys block

Input ports

None.

Output ports

This program returns a version in the following format:


major.minor.patchlevel.

• major of type BYTE


Major version of library.
• minor of type BYTE
Minor version of library.
• major of type WORD
Patchlevel version of library.

Copyright  2010 TTControl GmbH. All rights reserved. Document number D-TTCSW-M-20-001
Control Library User Manual
58 9 Icons and Symbols Library

9 Icons and Symbols Library


The installation directory includes a folder named Icons and Symbols which contains a
collection of bitmaps that can be integrated in the HMI of a target visualization or GUI
of a CoDeSys configuration screen. The graphics library contains standardized symbols
specified in ISO 2575:2004, including for example the icons used for coolant temperature,
engine failure, high beam or winch. Futhermore it contains a collection of symbols for
crane applications specified in ISO 7296-2:1996 and an icon set for navigation in menues
and complex visualizations. Most of the symbols are available in several foreground col-
ors (white, black, red, yellow. . . ), background colors and resolutions. Additionally, high-
resolution bitmaps are provided for user customizations.

Document number D-TTCSW-M-20-001 Copyright  2010 TTControl GmbH. All rights reserved.
Disclaimer
While every precaution has been taken in the preparation of this document, the publishers
assume no responsibility for any remaining errors or omissions, or for damages resulting
from the use of the information herein.

You might also like