Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
160
Добавлен:
11.10.2020
Размер:
2.81 Mб
Скачать

Appendix D: - The CoDeSys Libaries

reset to the initial value DT#1970-01-01-00:00:00. Please note that the time in PDT is only set by a rising edge.

10.17 The Util.lib library

This library contains an additional collection of various blocks which can be used for BCD conversion, bit/byte functions, mathematical auxiliary functions, as controller, signal generators, function manipulators and for analogue value processing.

As some of the functions and function blocks contain REAL variables, an accessory library named UTIL_NO_REAL exists in which these POUs are excluded.

10.17.1BCD Conversion

A byte in the BCD format contains integers between 0 and 99. Four bits are used for each decimal place. The ten decimal place is stored in the bits 4-7. Thus the BCD format is similar to the hexadecimal presentation, with the simple difference that only values between 0 and 99 can be stored in a BCD byte, whereas a hexadecimal byte reaches from 0 to FF.

An example: The integer 51 should be converted to BCD format. 5 in binary is 0101, 1 in binary is 0001, which makes the BCD byte 01010001, which corresponds to the value $51=81.

BCD_TO_INT

This function converts a byte in BCD format into an INT value:

The input value of the function is type BYTE and the output is type INT.

Where a byte should be converted which is not in the BCD format the output is -1.

Examples in ST:

i:=BCD_TO_INT(73); (* Result is 49 *) k:=BCD_TO_INT(151); (* Result is 97 *)

l:=BCD_TO_INT(15); (* Output -1, because it is not in BCD format *)

INT_TO_BCD_

This function converts an INTEGER value into a byte in BCD format: The input value of the function is type INT, the output is type BYTE.

The number 255 will be outputted where an INTEGER value should be converted which cannot be converted into a BCD byte.

Examples in ST:

i:=INT_TO_BCD(49); (* Result is 73 *) k:=BCD_TO_INT(97); (* Result is 151 *) l:=BCD_TO_INT(100); (* Error! Output: 255 *)

10.17.2 Bit-/Byte Functions

EXTRACT

Inputs to this function are a DWORD X, as well as a BYTE N. The output is a BOOL value, which contains the content of the Nth bit of the input X, whereby the function begins to count from the zero bit.

Examples in ST:

FLAG:=EXTRACT(X:=81, N:=4); (* Result : TRUE, because 81 is binary 1010001, so the

4th bit is 1 *)

10-50

CoDeSys V2.3

Appendix D: - The CoDeSys Libaries

FLAG:=EXTRACT(X:=33, N:=0); (* Result : TRUE, because 33 is binary 100001, so the bit '0' is 1 *)

PACK

This function is capable of delivering back eight input bits B0, B1, ..., B7 from type BOOL as a BYTE. The function block UNPACK is closely related to this function.

PUTBIT

The input to this function consists of a DWORD X, a BYTE N and a BOOLean value B. PUTBIT sets the Nth bit from X on the value B, whereby it starts counting from the zero bit.

Example in ST:

A:=38;

(* binary 100110 *)

B:=PUTBIT(A,4,TRUE); (* Result : 54 = 2#110110 *)

C:=PUTBIT(A,1,FALSE); (* Result : 36 = 2#100100 *)

UNPACK

UNPACK converts the input B from type BYTE into 8 output variables B0,...,B7 of the type BOOL, and this is the opposite to PACK.

Example in FBD: Output:

10.17.3 Mathematic Auxiliary Functions

DERIVATIVE

This function block approximately determines the local derivation.

The function value is delivered as a REAL variable by using IN. TM contains the time which has passed in msec in a DWORD and the input of RESET of the type BOOL allows the function block to start anew through the delivery of the value TRUE.

The output OUT is of the type REAL.

In order to obtain the best possible result, DERIVATIVE approximates using the last four values, in order to hold errors which are produced by inaccuracies in the input parameters as low as possible.

Block in FBD:

CoDeSys V2.3

10-51

Appendix D: - The CoDeSys Libaries

INTEGRAL

This function block approximately determines the integral of the function.

In an analogue fashion to DERIVATIVE, the function value is delivered as a REAL variable by using IN. TM contains the time which has passed in msec in a DWORD and the input of RESET of the type BOOL allows the function block to start anew with the value TRUE.

The output OUT is of the type REAL.

The integral is approximated by two step functions. The average of these is delivered as the approximated integral.

Block in FBD: Example: Integration of a linear function:

STATISTICS_INT

This function block calculates some standard statistical values:

The input IN is of the type INT. All values are initialised anew when the BOOLean input RESET is TRUE.

The output MN contains the minimum, MX of the maximum value from IN. AVG describes the average, that is the expected value of IN. All three outputs are of the type INT.

Block in FBD:

STATISTICS_REAL

This function block corresponds to STATISTICS_INT, except that the input IN is of the type REAL like the outputs MN, MX, AVG.

VARIANCE

VARIANCE calculates the variance of the entered values.

The input IN is of the type REAL, RESET is of the type BOOL and the output OUT is again of the type REAL.

This block calculates the variance of the inputted values. VARIANCE can be reset with RESET=TRUE.

The standard deviation can easily be calculated as the square root of the VARIANCE.

10-52

CoDeSys V2.3

Appendix D: - The CoDeSys Libaries

10.17.4 Controllers

PD

The PD controller function block:

ACTUAL (actual value) and DESIRED (desired or nominal value) as well as KP, the proportionality coefficient, are all input values of the type REAL. TV is of the type DWORD and contains the derivative action time in msec. Y_OFFSET, Y_MIN and Y_MAX are of type REAL and are used for the transformation of the manipulated variable within a prescribed range. MANUAL, of type BOOL, switches to manual operation. RESET is of the type BOOL and serves to reset the controller.

Y = KP (+ TV δ∆/δt) + Y_OFFSET whereby =SET_POINT-ACTUAL

Y is also limited to the allowed range between Y_MIN and Y_MAX. If Y exceeds this range, LIMITS_ACTVE, a BOOLean output variable, becomes TRUE. If no limitation of the manipulated variable is desired, Y_MIN and Y_MAX are set to 0.

If MANUAL is TRUE, then the regulator is suspended, that is Y is not altered (by the controller), until MANUAL becomes FALSE, whereby the controller is re-initialized.

A P-controller is easily generated by setting TV to a fixed value of 0.

PID

The PID controller function block:

Unlike the PD controller, this function block contains a further DWORD input TN for the readjusting time in msec.

The output, the manipulated variable (Y) is again of type REAL, and contains, unlike the PD controller, an additional integral part:

Y = KP (+ 1/TN ?∆(t)dt + TV δ∆/δt) + Y_OFFSET

The PID controller can be easily converted to a PI controller by setting TV=0.

CoDeSys V2.3

10-53

Соседние файлы в папке 759-333