Example 20 Fuzzy Control Lecture L10.2.

Presentation on theme: "Example 20 Fuzzy Control Lecture L10.2."— Presentation transcript:

Example 20 Fuzzy Control Lecture L10.2

Fuzzy Control Design of a Fuzzy Controller Fuzzification of inputs
MEM and fill_weights() Processing the Rules REV and fire_rules(…) Output Defuzzification WAV and calc_output(…)

Fuzzy Rules IF A AND B THEN L * *
Fuzzy Control Inputs Map to Fuzzy Sets get_inputs(); Fuzzy Rules IF A AND B THEN L * * fire_rules(); Defuzzification find_output(); Output

Plexiglas tube Ping-pong ball Problem: Maintain ping-pong ball floating at a specified height Ultrasonic transducer Muffin fan

Input membership functions for floating ping-pong ball

Output membership functions for floating ping-pong ball

Fuzzy K-map for floating ping-pong ball

Fuzzy Control Design of a Fuzzy Controller Fuzzification of inputs
MEM and fill_weights() Processing the Rules REV and fire_rules(…) Output Defuzzification WAV and calc_output(…)

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

Data structure used by the HCS12 MEM instruction
point_2 point_1

unsigned char memb_pos[20];
static int maxsize = 20; void main(void) { // input membership functions const unsigned char ball_position[] = { 0, 0, 30, 60, // neg_far 40, 60, 80,110, // neg_close 90,120,135,165, // zero_pos 150,175,185,220, // pos_close 200,220,255, // pos_far }; unsigned char memb_pos[20]; get_slopes(ball_position,memb_pos,maxsize);

unsigned char memb_speed[20];
const unsigned char ball_speed[] = { 0, 0, 20, 60, // neg_fast 35, 60, 80,110, // neg_slow 80,130,130,180, // zero_speed 155,175,185,215, // pos_slow 195,220,255, // pos_fast }; unsigned char memb_speed[20]; get_slopes(ball_speed,memb_speed,maxsize);

void get_slopes(const unsigned char ball[],unsigned char memb[],
int maxsize) { int j; unsigned char diff; for(j = 0; j < maxsize; j=j+4){ memb[j] = ball[j]; // point_1 memb[j+1] = ball[j+3]; // point_2 diff = (ball[j+1] - ball[j]); if(diff == 0) memb[j+2] = 0; else memb[j+2] = 255/diff; // slope_1 diff = (ball[j+3] - ball[j+2]); memb[j+3] = 0; memb[j+3] = 255/diff; // slope_2 }

// The fuzzy controller
while(1) { // x1 = get_position(); // (user defined) x1 = 100; // test case -- remove fill_weights(ptr_weight_pos,ptr_memb_pos,num_memb_pos,x1); // x2 = get_speed(); // (user defined) x2 = 150; // test case -- remove fill_weights(ptr_weight_speed,ptr_memb_speed,num_memb_speed,x2);

Fuzzy Control Design of a Fuzzy Controller Fuzzification of inputs
MEM and fill_weights() Processing the Rules REV and fire_rules(…) Output Defuzzification WAV and calc_output(…)

Pseudocode for fire_rules()

The HCS12 REV Instruction
if x1 is neg_far and x2 is neg_fast then y is pos_high

Fuzzy K-map inout_array[] indicies

unsigned char inout_array[] = {
0, // weight_pos[] 0 neg_far 0, // neg_close 0, // zero_pos 0, // pos_close 0, // pos_far 0, // weight_speed[] 5 neg_fast 0, // neg_slow 0, // zero_speed 0, // pos_slow 0, // pos_fast 0, // out[] neg_high 0, // neg_low 0, // zero_motor 0, // pos_low 0, // pos_high };

unsigned char rules[] = {
0,5,0xFE,14,0xFE, // if x1 is neg_far && x2 is neg_fast then y is pos_high 0,6,0xFE,14,0xFE, 0,7,0xFE,14,0xFE, 0,8,0xFE,13,0xFE, 0,9,0xFE,12,0xFE, 1,5,0xFE,14,0xFE, // if x1 is neg_close && x2 is neg_fast then y is pos_high 1,6,0xFE,14,0xFE, 1,7,0xFE,13,0xFE, 1,8,0xFE,12,0xFE, 1,9,0xFE,11,0xFE, 2,5,0xFE,14,0xFE, // if x1 is zero_pos && x2 is neg_fast then y is pos_high 2,6,0xFE,13,0xFE, 2,7,0xFE,12,0xFE, 2,8,0xFE,11,0xFE, 2,9,0xFE,10,0xFE, 3,5,0xFE,13,0xFE, // if x1 is pos_close && x2 is neg_fast then y is pos_low 3,6,0xFE,12,0xFE, 3,7,0xFE,11,0xFE, 3,8,0xFE,10,0xFE, 3,9,0xFE,10,0xFE, 4,5,0xFE,12,0xFE, // if x1 is pos_far && x2 is neg_fast then y is zero_motor 4,6,0xFE,11,0xFE, 4,7,0xFE,10,0xFE, 4,8,0xFE,10,0xFE, 4,9,0xFE,10,0xFF };

unsigned char* ptr_memb_pos;
unsigned char* ptr_memb_speed; unsigned char* ptr_weight_pos; unsigned char* ptr_weight_speed; unsigned char* ptr_inout_array; unsigned char* ptr_out; unsigned char* ptr_rules; unsigned char x1; // input x1 unsigned char x2; // input x2 unsigned char y; // output y ptr_memb_pos = memb_pos; ptr_memb_speed = memb_speed; ptr_weight_pos = inout_array; ptr_weight_speed = inout_array+num_memb_pos; ptr_inout_array = inout_array; ptr_out = inout_array+num_memb_pos+num_memb_speed; ptr_rules = rules;

// The fuzzy controller
while(1) { // x1 = get_position(); // (user defined) x1 = 100; // test case -- remove fill_weights(ptr_weight_pos,ptr_memb_pos,num_memb_pos,x1); // x2 = get_speed(); // (user defined) x2 = 150; // test case -- remove fill_weights(ptr_weight_speed,ptr_memb_speed,num_memb_speed,x2); fire_rules(ptr_inout_array,ptr_rules,ptr_out,num_memb_motor);

Fuzzy Control Design of a Fuzzy Controller Fuzzification of inputs
MEM and fill_weights() Processing the Rules REV and fire_rules(…) Output Defuzzification WAV and calc_output(…)

Pseudocode for find_output(…)
HCS12 WAV instruction computes numer and denom

// output membership functions unsigned char cent[] = {
5,65,128,175,22 }; unsigned char* ptr_cent; unsigned char y; // output y ptr_cent = cent;

// The fuzzy controller
while(1) { // x1 = get_position(); // (user defined) x1 = 100; // test case -- remove fill_weights(ptr_weight_pos,ptr_memb_pos,num_memb_pos,x1); // x2 = get_speed(); // (user defined) x2 = 150; // test case -- remove fill_weights(ptr_weight_speed,ptr_memb_speed,num_memb_speed,x2); fire_rules(ptr_inout_array,ptr_rules,ptr_out,num_memb_motor); y = calc_output(ptr_out,ptr_cent,num_memb_motor); // adjust_motor(y): // (user defined) }