Você está na página 1de 6

Moving from FCL

To Fuzz-C
If youre using the C language, the facilities
of FCL (and more) are available through
Byte Crafts Fuzz-C.
Kirk Zurell, Byte Craft Limited

IEC 61131 (previously named IEC 1131) is a standard that formalizes the programming
of Programmable Logic Controllers. It details a block-structured language called ST
that is suitable for processing into a control program.
Section IEC 61131-7 is concerned with Fuzzy Logic and its use in PLCs. It specifies ST
language constructs for describing linguistic and consequence variables and fuzzy rules.
This language is called FCL (Fuzzy Control Language).
Fuzz-C is Byte Craft Limiteds fuzzy logic preprocessor for C. It accepts fuzzy logic
statements embedded within a C program, and generates pure C code suitable for any
ISO-compliant C compiler.
IEC 61131-7 ST code and Fuzz-C source programs are similar in many respects. Moving
fuzzy logic code from a PLC to a custom microprocessor system programmed in C is
straightforward.

Coding Fuzzy Programs


The Fuzz-C source program roughly corresponds to a FCL Fuzzy Function Block. Within
a Fuzz-C source program, input and output variables and fuzzy rules are declared, as well
as a common fuzzy set. This table compares FCL structures and Fuzz-C statements:

Byte Craft Limited


A2-490 Dutton Drive
Waterloo, Ontario, Canada
N2L 6H7
sales@bytecraft.com
www.bytecraft.com

Conventional Membership Functions

FCL

Fuzz-C

FUNCTION_BLOCK function_block_name

(C module)

VAR_INPUT
variable_name: data_type;

END_VAR

LINGUISTIC variable_name TYPE data_type


{
MEMBER member_name { point, point, point };

FUZZIFY variable_name
TERM term_name := (pair), (pair)
END_FUZZIFY
VAR_OUTPUT Output
variable_name: data_type;

END_VAR

CONSEQUENCE variable_name TYPE data_type


DEFUZZ method
{
MEMBER member_name { singleton_point };
}

DEFUZZIFY variable_name
TERM term_name := crisp;
METHOD method;
END_DEFUZZIFY
RULEBLOCK block_name
(operator definition; see below)
(activation method; see below)
(accumulation method; see below)
RULE number: IF condition THEN
conclusion
[WITH weight];

END_RULEBLOCK

FUZZY block_name
{
IF condition THEN conclusion;

VAR
variable_name: data_type;
END_VAR

(typical C variable declarations)


data_type identifier;

END_FUNCTION_BLOCK

(C module)

The following sections give details about the differences between FCL and Fuzz-C
notation.

Conventional Membership Functions


In 61131-7, variable TERMs are described with a list of Cartesian points, pairing a crisp
input figure with a degree of membership value between 0 and 1.
TERM hot := (20, 0), (30, 1), (40, 0);

Fuzz-C employs a convention to abbreviate the description of membership functions.


This simplifies the definition of membership functions and makes understanding them
much easier. Fuzz-C MEMBER declarations require only a series of crisp values. The
appropriate degree-of-membership values are imputed:
2

Moving from FCL to Fuzz-C

Fuzzy Operators: Aggregation

Values

Envelope

Example

Diagram
Fuzzy 1

Threshold

"Threshold
member"

Fuzzy 0

MEMBER { 30 }

0x20 - - - 0x 30 - - - 0x40
"Range
member"

Fuzzy 1

Range

Fuzzy 0

MEMBER { 25, 35 }

0x20 - - - 0x 30 - - - 0x40
"Triangl e
member"

Fuzzy 1

Triangle

Fuzzy 0

MEMBER { 20, 30, 40 }

0x20 - - - 0x 30 - - - 0x40
"Trapezoid
member"

Fuzzy 1

Trapezoid

MEMBER { 20, 25, 30, 40 }

Fuzzy 0
0x20 - - - 0x 30 - - - 0x40

2+

Table

"Tabl e
member"

Fuzzy 1

MEMBER { (10,0),
(20, 0.25),
(30, 0.77),
(40, 0), }

Fuzzy 0
0x20 - - - 0x 30 - - - 0x40

Table 1: Conventional membership function notation

A singleton envelope can be specified using a range envelope, with the two crisp values
set to be the same. Most fuzzy sets are triangular or trapezoidal, having three or four
important points, so this notation reduces code size.
Fuzzy sets more complex than these can be specified using a pair notation very similar to
FCL.

Fuzzy Operators: Aggregation


FCL specifies three ways to define the fuzzy AND and OR operators:
Description

Key.

AND

Key.

OR

Zadeh Operators

MIN

min(x, y)

MAX

max(x, y)

Probabilistic Product/Sum

PROD

x y

ASUM

x + y x y

Bounded
Intersection/Union

BDIF

max(0, x + y - 1)

BSUM

min(1, x + y)

These are used in the Aggregation phase of fuzzy inference.


Byte Craft Limited

Fuzzy Rules: Activation and Accumulation

In Fuzz-C, fuzzy AND and OR are defined as macros in a standard header file (fuzz-c.h):

By default, the Zadeh operators are compiled in. We have found the
overwhelming majority of fuzzy logic programs use the Zadeh operators.

The Bounded Intersection/Union routines and Probabilistic Product and Sum


methods can be implemented using C macros. Instead of using constants 0 and 1,
the macros F_ZERO and F_ONE can be used; these correspond with the underlying
degree of membership dynamic range.

Fuzzy Rules: Activation and Accumulation


In Fuzz-C, rules are not labeled with rule numbers. All rules are evaluated at once, and
there is no facility to change program flow or make other reference to individual rules.
The Activation and Accumulation methods for fuzzy inference are explicitly declared in
the RULEBLOCK in FCL. In contrast, Fuzz-C uses calculations for similar purposes from the
defuzzification templates. This is an example of the accumulation portion of a DEFUZZ
template, named fm to cause it to be expanded once per CONSEQUENCE member invoked.

fm
// Code to be executed each time a consequence function is called
/* fm section */
fc_@CONSNAME += @CONSVOL;
fa_@CONSNAME += (@CONSVOL * (@CONSPOINT));
;;

Weighting (the FCL WITH clause) is accomplished differently as well. Where in FCL
explicit weights are assigned to the action of different rules and activate them, in
Fuzz-C weighting can be accomplished more naturally through the crisp values given in
CONSEQUENCE members. Adding a rule multiple times to give it extra weight is a quick but
unattractive alternative and suggests a design problem.
Even when there are only two possible outcomes, it is useful to declare many different
consequence members. Doing so allows the fuzzy rules to be more intuitive and makes
the effect of combining several rules more pronounced. Weighting the output
CONSEQUENCE members in effect weights the rules, but within the concepts expressed
through linguistic variables (and not as arbitrary values).
In the following PID controller process, the manipulated variable (the output) is likely a
single port bit, to be taken from the MSB of ManVar. With the CONSEQUENCE range centered
on 0, this is a direct copying operation, but in the fuzzy system the rules are weighted by
large and small positive and negative values. With a large result, it is far more likely that
the bit will be switched; this is useful behaviour for large errors.
CONSEQUENCE ManVar
{
MEMBER LNegative
MEMBER SNegative
MEMBER SPositive
MEMBER LPositive
}

TYPE int MIN -20 MAX 20 DEFUZZ cg


{
{
{
{

-18 }
-6 }
6 }
18 }

FUZZY pid
{

Moving from FCL to Fuzz-C

Defuzzification Methods
/* large moves for large proportional errors */
IF Error IS LNegative THEN ManVar IS LPositive
IF Error IS LPositive THEN ManVar IS LNegative
/* small moves for changes in error */
IF Error IS normal AND DeltaError IS Positive
THEN ManVar IS SNegative
IF Error IS normal AND DeltaError IS Negative
THEN ManVar IS SPositive
/* small moves for large sums of accumulated error */
IF Error IS close AND SumError IS LPos
THEN ManVar IS SNegative
IF Error IS close AND SumError IS LNeg
THEN ManVar IS SPositive
}

Defuzzification Methods
Defuzzification methods are pre-defined in the FCL standard. In contrast, Fuzz-C uses a
user-defined defuzzification template that allows any type of defuzzification. Several prewritten templates ship with Fuzz-C.
Defuzzification Technique

FCL METHOD Keyword

Fuzz-C DEFUZZ name

Centre of Gravity

COG

CG

Centre of Gravity for


Singletons

COGS

CG

Centre of Area

COA

Maximum

MAX

Maximum Average

MAXAVE

Left Most Maximum

LM

MAXLEFT

Right Most Maximum

RM

MAXRIGHT

Middle of the Maximum

MIDMAX

Fuzz-C performs an intermediate centre of gravity calculation on CONSEQUENCE (output)


members having more than two crisp points. This simplifies consequence calculations on
resource-limited hardware and does not materially affect the outcome.
A default crisp output value can be declared with the DEFAULT statement. In Fuzz-C, a
similar DEFAULT setting can define a default output (crisp) value or invoke error handling
routines or other side-effects.

Additional Features
Fuzz-C includes some features not reflected in FCL:

Byte Craft Limited

Summary

Hedge functions. Functions like NEARLY or ROUGHLY can add extra detail to a fuzzy
condition, effectively adding an additional transformation to the membership
function.

Membership functions based on fuzzy conditions. This is useful when one


member describes a useful fuzzy set and other members can be derived from that
set.
Fuzz-C can accept membership functions such as the following:
LINGUISTIC hours TYPE char MIN 0 MAX 240
{
MEMBER day
{ 55 , 65 , 175 , 185 }
MEMBER night
{FUZZY { hours IS NOT day }}
MEMBER morning {50, 60 , 80 , 90 }
MEMBER evening { 160 ,170 ,190 , 200 }
MEMBER nightsetback {
FUZZY { hours IS night AND hours IS NOT evening }}
}

Ad-hoc fuzzy comparisons. These are essentially anonymous membership


functions:
IF F_EQ(variable, crisp, delta) THEN

The function compares the variable to a triangular membership function, centered


at crisp and declining to delta on both sides. The function returns full equality
(DOM of F_ONE) when temperature and crisp are identical, and declining DOM
until variable exceeds delta.

Summary
Translating FCL fuzzy logic code to Fuzz-C source is straightforward; there is an almost
one-to-one correspondence between the two for the most commonly used fuzzy logic
constructions.

References
IEC: International Electrotechnical Commission, http://www.iec.ch/index.html.
IEC 61131-7: International Electrotechnical Commission (IEC). Technical committee no.
65: Industrial process measurement and control. Sub-committee 65b: Devices. IEC 61131
Programmable ControllersPart 7: Fuzzy control programming.
Fuzz-C: Byte Craft Limited, http://www.bytecraft.com/Fuzz-C.
Byte Craft Limited
A2-490 Dutton Drive
Waterloo, Ontario, Canada
N2L 6H7
sales@bytecraft.com
www.bytecraft.com

Moving from FCL to Fuzz-C

Você também pode gostar