fuzzy control

60
Fuzzy Control Design of Embedded Systems Using 68HC12/11 Microcontrollers Chapter 14 Richard E. Haskell, Prentice Hall, 2000

Upload: deo

Post on 19-Mar-2016

89 views

Category:

Documents


0 download

DESCRIPTION

Fuzzy Control. Design of Embedded Systems Using 68HC12/11 Microcontrollers Chapter 14 Richard E. Haskell, Prentice Hall, 2000. Fuzzy Control. Fuzzy Sets Design of a Fuzzy Controller Fuzzification of inputs: get_inputs() Fuzzy Inference Processing the Rules: find_rules() - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Fuzzy Control

Fuzzy Control

Design of Embedded Systems Using 68HC12/11 Microcontrollers

Chapter 14 Richard E. Haskell, Prentice Hall, 2000

Page 2: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 3: Fuzzy Control

" So far as the laws of mathematics refer to reality, they are not certain,

And so far as they are certain, they do not refer to reality."

Albert EinsteinGeometrie und Erfahrung

Fuzzy Logic

Page 4: Fuzzy Control

Fuzzy Sets

The sentence on the other sideof the line is false

The sentence on the other sideof the line is false

Is this sentence true or false?

Page 5: Fuzzy Control

The Barber Paradox

Sign on a Barber Shop:

“I shave all who do not shave themselves.”

Who shaves the barber?

Page 6: Fuzzy Control

Agea1 a2

Young1

0

F igure 1 4 .2 M em be rsh ip func tion fo r the fuz zy se t Y oung

Page 7: Fuzzy Control

Table 14.1 Fuzzy Logic OperationsLogic Operation Fuzzy Logic Operation

NOT A 1 - AA AND B MIN(A,B)A OR B MAX(A,B)

Page 8: Fuzzy Control

Table 14.1 Fuzzy Logic OperationsLogic Operation Fuzzy Logic Operation

NOT A 1 - AA AND B MIN(A,B)A OR B MAX(A,B)

Agea1 a2

Young1

0

NOT Young

Figure 14.3 NO T Young = 1 - Young

Page 9: Fuzzy Control

Age

Old1

0

NOT Old

F igu re 1 4 .4 M e m be rsh ip fun c tio ns fo r th e fu zzy se ts O ld an d N O T O ld

Page 10: Fuzzy Control

NOT OldNOT Young

Age

1

0

Age

1

0

Middle Age = NOT Young AND Not Old

(a)

(b)

Figure 14.5 Deriv ing the membership function for Middle Age

Page 11: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 12: Fuzzy Control

NM NS Z PS PM

128 174 22082360

1

Universe of discourse2550

Figure 14.6 Example of fuzzy membership functions

Page 13: Fuzzy Control

INPUTS

OUTPUT

Map to Fuzzy Sets

FUZZY RULES

If A AND B then L

• • •

Defuzzification

get_inputs();

fire_rules();

find_output();

A Fuzzy Controller

Page 14: Fuzzy Control

do_forever{get_inputs();fire_rules();find_output();}

Figure 14.8 Overall algorithm of a fuzzy controller

Page 15: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 16: Fuzzy Control

get_inputs()for i = 1, num_inputs

{get_x(i);fill_weight(xi, Mi);}

Figure 14.9 Pseudocode for get_inputs()

Page 17: Fuzzy Control

u1 u2 u4

1wt

u3

slope_1 slope_2

Figure 14.10 A membership function is defined in terms of u1, u2, u3, and u4

The 68HC12 MEM Instruction

Page 18: Fuzzy Control

NM NS Z PS PM

128 174 22082360

1

Universe of discourse2550

Figure 14.6 Example of fuzzy membership functions

Page 19: Fuzzy Control

The 68HC12 MEM Instruction

A xiweight(j)

X --> point_1 Y -->point_2 Y + 1 -->slope_1slope_2

X + 4 -->

Figure 14.11 Data structure used by the 68HC12 MEM instruction

Page 20: Fuzzy Control

pfa 5 # mem fncs^wt pointer to weight array

X --> 0 point_172 point_2

slope_1slope_2

48 point_1116 point_2

slope_1slope_2

104 point_1152 point_2

slope_1slope_2

140 point_1208 point_2

slope_1slope_2

184 point_1255 point_2

slope_1slope_2

Figure 14.12 Data structure for storing membership function parameters

Page 21: Fuzzy Control

* Fill Weight Arrays* fill.weights ( xi pfa -- )FILLWT

LDY 2,X+ Y = pfaLDD 2,X+ B = xiPSHX save XTFR Y,X X = pfaLDY 2,X Y -> input weight tablePSHB save xiLDD 4,X+ B = #mem functs; X->1st memb fncPULA A = xi

FW1 MEM fuzzy membership gradeDBNE B,FW1PULX restore XRTS

Figure 14.13 68HC12 code for the WHYP word fill.weights ( xi addr -- )

Page 22: Fuzzy Control

fuzzy.input ball_position 0 0 36 72 >m \ neg_far 48 82 100 116 >m \ neg_close 104 128 128 152 >m \ zero_p 140 156 174 208 >m \ pos_close 184 220 255 255 >m \ pos_far

end.input

Figure 14.14 Format for entering membership functions

Page 23: Fuzzy Control

NM NS Z PS PM

128 174 22082360

1

Universe of discourse2550

Figure 14.6 Example of fuzzy membership functions

Page 24: Fuzzy Control

Listing 14.1 Fuzzy Control in WHYP -- Defining Membership Functions\ Fuzzy Logic Control Using WHYP12 File: FUZZY12.WHP\ *********************************************\ Fuzzy Control\ *********************************************

\ ********** Defining word for input **********: fuzzy.input ( +++ )

CREATE HERE 0 , 0 , DOES> ( pfa -- ) ;

: get.slopes ( u1 u2 u3 u4 -- )3 PICK C, \ store point_1DUP C, \ store point_22SWAP \ u3 u4 u1 u2SWAP - ?DUPIF 255 SWAP / C, \ store slope_1ELSE 0 C, \ vertical slopeTHEN \ u3 u4SWAP - ?DUPIF 255 SWAP / C, \ store slope_2ELSE 0 C, \ vertical slopeTHEN ;

Page 25: Fuzzy Control

: 4DROP ( n1 n2 n3 n4 -- )2DROP 2DROP ;

: >m ( pfa u1 u2 u3 u4 -- pfa )DUP 255 >IF 4DROP CR ." ERROR: u4 > 255"ELSE \ pfa u1 u2 u3 u4 2DUP > IF 4DROP CR ." ERROR: u3 > u4" ELSE \ pfa u1 u2 u3 u4 2OVER > IF 4DROP CR ." ERROR: u1 > u2" ELSE \ pfa u1 u2 u3 u4 2 PICK 2 PICK > IF 4DROP CR ." ERROR: u2 > u3" ELSE \ pfa u1 u2 u3 u4

get.slopes THEN THEN THENTHEN1 OVER +! ; \ inc #member fncs

: end.input ( pfa -- ) VHERE OVER 2+ ! @ VALLOT ;

Page 26: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 27: Fuzzy Control

Fuzzy Inference

if x1 is A1 and x2 is B1 then y is L1 rule 1

if x1 is A2 and x2 is B2 then y is L2 rule 2

Given the fact that

x1 is A' and x2 is B' fact

the problem is to find the conclusion

y is L' conclusion

Page 28: Fuzzy Control

m

m

m

m

m

m

m

x1 x2

x1 x2

y

y

A1

A2

B1

B2

L1

L2

yy0

a b

w1w2

w2*L1

w1*L2

sum

w1

w2

rule 1

rule 2

L'

Figure 14.15 Fuzzy inference

Page 29: Fuzzy Control

m

yy0

L'

m

yy0

L'

m

yy0

L'

Maximum Sum Singletons

F ig u re 1 4 .1 6 C o m p a r in g th e M A X ru le a n d th e S U M ru le

Page 30: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 31: Fuzzy Control

fire_rules()clear Out array;for j = 1, num_rules { min_wt = 1; for i = 1, num_inputs { wt = weighti[Aij] if wt < min_wt min_wt = wt; } Out[Lj] = MAX(Out[Lj], min_wt); }

Figure 14.17 Pseudocode for fire_rules()

Page 32: Fuzzy Control

m

m

m

m

m

m

m

x1 x2

x1 x2

y

y

A1

A2

B1

B2

L1

L2

yy0

a b

w1w2

w2*L1

w1*L2

sum

w1

w2

rule 1

rule 2

L'

Figure 14.15 Fuzzy inference

Page 33: Fuzzy Control

Y--> neg_far 0 weight(1) in.out.array pfa #rules rules neg_close 1 - - Out0

zero_p 2 - - X--> 0 x1 is neg_far pos_close 3 - - 5 x2 is neg_fast

pos_far 4 - - FEneg_fast 5 weight(2) 15 y is pos_high

neg_slow 6 - - FEzero_s 7 - - 0 x1 is neg_far

pos_slow 8 - - 6 x2 is neg_slowpos_fast 9 - - FE

10 5 Out0 15 y is pos_highneg_high 11 0 Out FEneg_low 12 0zero_m 13 0

pos_low 14 0 11pos_high 15 0 A = $FF FF end of rules

The 68HC12 REV Instruction

Figure 14.18 Setup required for REV instruction

Page 34: Fuzzy Control

* Fire all rules* firerules ( in.out.array rules -- )FIRERULES

LDD 2,X+ D -> rulesLDY 2,X+ Y -> in.out.arrayPSHXTFR D,X X -> rulesINXINX X -> Out0 addrPSHY save YLDY 2,X+ Y -> Out0, X -> 1st ruleLDAB 0,Y B = #out.memb.fncs

FR0 CLR 1,+Y clear Out arrayDBNE B, FR0PULY Y -> in.out.arrayLDAA #$FFREV rule evaluationPULXRTS

Figure 14.19 68HC12 code for WHYP word firerules ( in.out.array rules-- )

Page 35: Fuzzy Control

make.rules rules\ ball_position ball_speed motor_power

neg_far C, neg_fast C: pos_high C; neg_far C, neg_slow C: pos_high C; ----- ----- ----- ----- pos_far C, pos_slow C: neg_high C; pos_far C, pos_fast C: neg_high C.

end.rules

Figure 14.20 Format for defining fuzzy rules

Page 36: Fuzzy Control

Listing 14.2 Fuzzy Control in WHYP -- Making rules\ Fuzzy Logic Control Using WHYP12 File: FUZZY12.WHP

\ ********** Defining word for making rules **********: make.rules ( +++ )

CREATE HERE 0 , \ leave room for #rules

0 , \ leave room for ^Out0DOES> ( pfa -- ) ;

HEX

: C: ( pfa ix -- pfa )C, FE C, ;

: C; ( pfa ix -- pfa )C, FE C,1 OVER +! ; \ inc #rules

: C. ( pfa ix -- pfa )C, FF C,1 OVER +! ; \ inc #rules

DECIMAL

: end.rules ( pfa -- )DROP ; \ drop addr of #rules

Page 37: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 38: Fuzzy Control

m

m

m

m

m

m

m

x1 x2

x1 x2

y

y

A1

A2

B1

B2

L1

L2

yy0

a b

w1w2

w2*L1

w1*L2

sum

w1

w2

rule 1

rule 2

L'

Figure 14.15 Fuzzy inference

Page 39: Fuzzy Control

14.2.4 Centroid Defuzzification

The last step in the fuzzy controller shown in Figure 14.7 is defuzzification. Thisinvolves finding the centroid of the net output fuzzy set L' shown in Figures 14.15 and14.16. Although we have used the MIN-MAX rule in the previous section we will beginby deriving the centroid equation for the sum rule shown in Figure 14.16. This willilluminate the assumptions made in deriving the defuzzification equation that we willactually use in the fuzzy controller.

Let Li(y) be the original output membership function associated with rule i where yis the output universe of discourse (see Figure 14.15.). After applying rule i thismembership function will be reduced to the value

mi(y) = wiLi(y) (14.1)

where wi is the minimum weight found by applying rule i. The sum of these reducedoutput membership functions over all rules is then given by

M(y) = i=1

Nmi(y) (14.2)

where N is the number of rules.

Page 40: Fuzzy Control

The crisp output value y0 is then given by the centroid of M(y) from the equation

y0 = yM(y)dy

M(y)dy (14.3)

Note that the centroid of membership function Li(y) is given by

ci = yLi(y)dy

Li(y)dy (14.4)

ButIi = Li(y)dy (14.5)

is just the area of membership function Li(y). Substituting (14.5) into (14.4) we can write

yLi(y)dy = ciIi (14.6)

Page 41: Fuzzy Control

Using Eqs. (14.1) and (14.2) we can write the numerator of (14.3) as

yM(y)dy = y

i=1

NwiLi(y) dy

= i=1

NywiLi(y) dy

= i=1

N wiciIi (14.7)

where (14.6) was used in the last step.

Page 42: Fuzzy Control

Similarly, using (14.1) and (14.2) the denominator of (14.3) can be written as

M(y)dy =

i=1

NwiLi(y) dy

= i=1

NwiLi(y) dy

= i=1

N wiIi (14.8)

where (14.5) was used in the last step. Substituting (14.7) and (14.8) into (14.3) we canwrite the crisp output of the fuzzy controller as

y0 =

i=1

N wiciIi

i=1

N wiIi

(14.9)

Eq. (14.9) says that we can compute the output centroid from the centroids, ci, of theindividual output membership functions.

Page 43: Fuzzy Control

Note in Eq. (14.9) the summation is over all N rules. But the number of outputmembership functions, Q, will, in general, be less than the number of rules, N. This meansthat in the sums in Eq. (14.9) there will be many terms that will have the same values of ciand Ii. For example, suppose that rules 2, 3, and 4 in the sum all have the outputmembership function Lk as the consequent. This means that in the sum

w2c2I2 + w3c3I3 + w4c4I4

the values ci and Ii are the same values ck and Ik because they are just the centroid andarea of the kth output membership function. These three terms would then contribute thevalue

(w2 + w3 + w4)ckIk = WkckIk

to the sum, where

Wk = (w2 + w3 + w4)

is the sum of all weights from rules whose consequent is output membership function Lk.

Page 44: Fuzzy Control

This means that the equation for the output value, y0, given by (14.9) can be rewritten as

y0 =

k=1

Q WkckIk

k=1

Q WkIk

(14.10)

If the area of all output membership functions, Ik are equal, then Eq. (14.10) reduces to

y0 =

k=1

Q Wkck

k=1

Q Wk

(14.11)

Eqs. (14.10) and (14.11) show that the output crisp value of a fuzzy controller can becomputed by summing over only the number of output membership functions rather thanover all fuzzy rules. Also, if we use Eq. (14.11) to compute the output crisp value, thenwe need to specify only the centroids, ck, of the output fuzzy membership functions. Thisis equivalent to assuming singleton fuzzy sets for the output.

We will always use singleton fuzzy sets for the output represented by thecentroids, ck. We will also use the MIN-MAX inference rule described in the previoussection. It should be clear from Figure 14.16 that in this case the centroid y0 will still begiven by Eq. (14.11) where Wk is now the output array, Out(k), shown in Figure 14.18and computed by the word firerules given in Figure 14.19.

Page 45: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 46: Fuzzy Control

find_output()numer = 0;denom = 0;for k = 1, Q if Out[k] != 0 { numer = numer + Out[k]*c[k]; denom = denom + Out[k]; }y0 = numer/denom;

Figure 14.21 Pseudocode for find_output()

Page 47: Fuzzy Control

The 68HC12 WAV InstructionThe values of numer and denom in Figure 14.21 can easily be calculated using the

68HC12 WAV instruction. If index register X points to c[k], index register Y points toOut[k], and accumulator B contains the number of output membership functions, Q, thenthe 68HC12 WAV instruction will compute a 24-bit value for numer and store the resultin Y:D and compute a 16-bit value for denom and store this result in X. Therefore, if theWAV instruction is followed by the instruction EDIV (see Section 3.32) then the centroidvalue y0 will be left in Y.

The WHYP word calc.output ( rules cent_addr -- output_value ) will expectthe address of rules (see Figure 14.18) and the address of the centroid array, c[k], on thedata stack and will return the crisp output value, y0. The 68HC12 code for the wordcalc.output is shown in Figure 14.22.

Page 48: Fuzzy Control

Y--> neg_far 0 weight(1) in.out.array pfa #rules rules neg_close 1 - - Out0

zero_p 2 - - X--> 0 x1 is neg_far pos_close 3 - - 5 x2 is neg_fast

pos_far 4 - - FEneg_fast 5 weight(2) 15 y is pos_high

neg_slow 6 - - FEzero_s 7 - - 0 x1 is neg_far

pos_slow 8 - - 6 x2 is neg_slowpos_fast 9 - - FE

10 5 Out0 15 y is pos_highneg_high 11 0 Out FEneg_low 12 0zero_m 13 0

pos_low 14 0 11pos_high 15 0 A = $FF FF end of rules

The 68HC12 REV Instruction

Figure 14.18 Setup required for REV instruction

Page 49: Fuzzy Control

* Find Output value* calc.output ( rules cent_addr -- output_value )FINDOUT

LDD 2,X+ D -> centLDY 2,X+ Y -> rulesPSHXTFR D,X X -> cent arrayLDY 2,Y Y -> Out0LDAB 0,Y B = #out.memb.fncsINY Y -> Out arrayWAVEDIV Y = quotientTFR Y,D D = quotient A = 0, B = outputPULXSTD 2,-XRTS

Figure 14.22 68HC12 code for calc.output ( rules cent_addr -- output_value )

Page 50: Fuzzy Control

Fuzzy Control

• Fuzzy Sets• Design of a Fuzzy Controller

– Fuzzification of inputs: get_inputs()– Fuzzy Inference– Processing the Rules: find_rules()– Centroid Defuzzification– Output Defuzzification: find_output()– A Fuzzy Control Example --

• Floating Ping-Pong Ball

Page 51: Fuzzy Control
Page 52: Fuzzy Control

ball_position neg_far neg_close zero_p pos_close pos_far

pos_fast zero_m neg_low neg_high neg_high neg_high

pos_slow pos_low zero_m neg_low neg_high neg_high

ball_speed zero_s pos_high pos_low zero_m neg_low neg_high

neg_slow pos_high pos_high pos_low zero_m neg_low

neg_fast pos_high pos_high pos_high pos_low zero_m

Figure 14.23 Fuzzy K-map for floating ping-pong ball

Page 53: Fuzzy Control

Listing 14.3 Fuzzy Control Example -- Ping-Pong Ball\ Fuzzy Control Example File: PINGPONG.WHP\ Floating ping-pong ball

LOAD FUZZY12.WHP\ ******************************************************\ Define inputs\ ******************************************************

\ Ball position0 CONSTANT neg_far1 CONSTANT neg_close2 CONSTANT zero_p3 CONSTANT pos_close4 CONSTANT pos_far

\ Ball speed5 CONSTANT neg_fast6 CONSTANT neg_slow7 CONSTANT zero_s8 CONSTANT pos_slow9 CONSTANT pos_fast

Page 54: Fuzzy Control

\ Define input membership functions

fuzzy.input ball_position 0 0 30 50 >m \ neg_far 40 60 80 110 >m \ neg_close 100 120 135 165 >m \ zero_p 155 175 185 210 >m \ pos_close 200 220 255 255 >m \ pos_far

end.input

fuzzy.input ball_speed 0 0 20 60 >m \ neg_fast 50 70 90 120 >m \ neg_slow 100 125 135 155 >m \ zero_s 140 170 180 200 >m \ pos_slow 190 210 255 255 >m \ pos_fast

end.input

Page 55: Fuzzy Control

\ ******************************************************\ Define output

\ ******************************************************5 CONSTANT #out.member.fncs \ No. of output membership functionsCREATE cent \ output centroid array

5 C, 65 C, 128 C, 175 C, 200 C,\ |neg_high|neg_low|zero_m|pos_low|pos_high|

10 CONSTANT Out0\ Power percent change to motor

11 CONSTANT neg_high12 CONSTANT neg_low13 CONSTANT zero_m

14 CONSTANT pos_low15 CONSTANT pos_high

\ ball_position @ 2- VALLOT \ input 1 weight array\ ball_speed @ VALLOT \ input 1 weight array\ #out.member.fncs 1+ VALLOT \ controller output weights

: in.out.array ( -- addr ) ball_position 2+ @ ;

This is WRONG

Page 56: Fuzzy Control

Y--> neg_far 0 weight(1) in.out.array pfa #rules rules neg_close 1 - - Out0

zero_p 2 - - X--> 0 x1 is neg_far pos_close 3 - - 5 x2 is neg_fast

pos_far 4 - - FEneg_fast 5 weight(2) 15 y is pos_high

neg_slow 6 - - FEzero_s 7 - - 0 x1 is neg_far

pos_slow 8 - - 6 x2 is neg_slowpos_fast 9 - - FE

10 5 Out0 15 y is pos_highneg_high 11 0 Out FEneg_low 12 0zero_m 13 0

pos_low 14 0 11pos_high 15 0 A = $FF FF end of rules

The 68HC12 REV Instruction

Figure 14.18 Setup required for REV instruction

Page 57: Fuzzy Control

\ ******************************************************\ Define output\ ******************************************************5 CONSTANT #out.member.fncs \ No. of output membership fncs.

#out.member.fncs 1+ VALLOT \ controller output weights

CREATE cent \ output centroid array 5 C, 65 C, 128 C, 175 C, 200 C,\ |neg_high|neg_low|zero_m|pos_low|pos_high|

10 CONSTANT Out0\ Power percent change to motor11 CONSTANT neg_high12 CONSTANT neg_low13 CONSTANT zero_m14 CONSTANT pos_low15 CONSTANT pos_high

: in.out.array ( -- addr )ball_position 2+ @ ; \ address of weight1 array

Page 58: Fuzzy Control

pfa 5 # mem fncs^wt pointer to weight array

X --> 0 point_172 point_2

slope_1slope_2

48 point_1116 point_2

slope_1slope_2

104 point_1152 point_2

slope_1slope_2

140 point_1208 point_2

slope_1slope_2

184 point_1255 point_2

slope_1slope_2

Figure 14.12 Data structure for storing membership function parameters

Page 59: Fuzzy Control

\ ******************************************************\ Define rules\ ******************************************************

make.rules rules\ ball_position ball_speed motor_power

neg_far C, neg_fast C: pos_high C; neg_far C, neg_slow C: pos_high C; neg_far C, zero_s C: pos_high C; neg_far C, pos_slow C: pos_low C; neg_far C, pos_fast C: zero_m C; neg_close C, neg_fast C: pos_high C; neg_close C, neg_slow C: pos_high C; neg_close C, zero_s C: pos_low C; neg_close C, pos_slow C: zero_m C; neg_close C, pos_fast C: neg_low C; zero_p C, neg_fast C: pos_high C; zero_p C, neg_slow C: pos_low C; zero_p C, zero_s C: zero_m C; zero_p C, pos_slow C: neg_low C; zero_p C, pos_fast C: neg_high C; pos_close C, neg_fast C: pos_low C; pos_close C, neg_slow C: zero_m C; pos_close C, zero_s C: neg_low C; pos_close C, pos_slow C: neg_high C; pos_close C, pos_fast C: neg_high C; pos_far C, neg_fast C: zero_m C; pos_far C, neg_slow C: neg_low C; pos_far C, zero_s C: neg_high C; pos_far C, pos_slow C: neg_high C; pos_far C, pos_fast C: neg_high C.

end.rules

Page 60: Fuzzy Control

\ ******************************************************\ Implement fuzzy controller\ ******************************************************

: init.Out.array ( -- )#out.member.fncs \ #in.out.array Out0 + \ # Out0DUP rules 2+ ! \ store Out0 @rules+2C! ; \ store # @Out0

\ Compute output power given input pos and speed: fuzzy ( pos speed -- power )

ball_speed fill.weightsball_position fill.weightsin.out.array rules firerulesrules cent calc.output ;

: fuzzy_control ( -- )init.Out.arrayBEGIN

\ get.position \ pos (user defined) \ get.speed \ pos speed (user defined)

fuzzy \ power \ adjust.motor \ (user defined)

AGAIN ;