# Implementing A Fuzzy Logic Controller Computer Science Essay

**Published:** **Last Edited:**

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

This assignment focuses on designing and implementing a fuzzy logic controller to move the Khepera robot in any environment for 1minute by avoiding collision with obstacles and by following the wall on its left side. Efforts were taken to maximise the distance travelled while preserving the above conditions. The assignment was implemented using MathWorks' fuzzy logic toolbox and kiks. Two different arenas were provided to test the performance of the fuzzy logic control on Khepera robot.

Design involves construction, implementation of Fuzzy Inference System (FIS) and to tune the same based on the performance of the robot in the environment. Parameters that determine the performance of the robot include distance covered, how good it is in avoiding collisions and ability to keep track of the left wall. Decisions were made in choosing the type of the system, number of inputs and outputs, universe of discourse for each input and output, number of fuzzy sets and range for each input and output, membership function for each fuzzy set, fuzzy rules, method to correlate rule consequent with rule antecedent, defuzzification method and the baud rate.

The developed design is a mamdani type FIS with 3 inputs from weighted scaling combination of 6 infra-red proximity sensors and 2 outputs which determines the speed and turn rate of the robot. The robot covered a distance of 2080 cm in arena_1 (figure 5) and 1846 cms in arena_2 (Figure 6) in a minute by following its left wall and without colliding with obstacles.

## Aim

The aim of the assignment is to design a fuzzy control system that make the Khepera robot wander in any given environment by following wall on its left while avoiding collision with the obstacles in its path and covering a maximum distance in one minute.

## Work Stages

The stages of works is shown in figure 1

Figure 1

## 2.1 Collection of reference material and preparation

User manual of Khepera robot provided the information regarding the type of sensors used in robot, sensor configuration in the robot, baud rate requirements to connect personal computer with robot and stepper motor configuration

User guide of Kiks software provided information about the parameters to be initialised and the facilities of the software

Artificial Intelligence [1](Chapter 4) book aided in designing the FIS

## 2.2 Planning and Design

Planning includes the estimation of work and difficulties to overcome. It also includes the planning of time frame to complete the assignment. Design process includes decisions on type of FIS, number of inputs and outputs required for the system, fuzzy set for each input and output, number of membership function and type of membership function for each input and output, fuzzy rules, method for correlating consequent to antecedent and defuzzification method. Only initial decision where made on the parameters in designing but surely requires the process of tuning and modifications.

## 2.3 Implementation of the Design

The design was implemented on Matlab using kiks simulation software and fuzzy logic toolbox.

## 2.4 Testing

Performance of the robot on the environment was monitored using Kiks software

## 2.5 Tuning

Refinements on parameters of designing the FIS were made and the refined system was made to undergo the process of testing again.

The last two stages repeats number of times until satisfactory performance is achieved.

## 3. DESIGN DECISIONS

## 3.1 TYPE OF FIS SYSTEM

Two types of FIS system are available Mamdani FIS and Sugeno FIS. Among these Mamdani FIS was choosen to implement the fuzzy logic controller for khepera mobile robot to wander in its environment. Mamdani FIS incorporates capturing human expert knowledge which was required to implement this system. Mamdani FIS burdens the controller for computation.(Pg 114) [1] Computational burden by Mamdani FIS is tolerable in this case.

## 3.2 INPUTS AND OUTPUTS

Assignment briefing sheet suggests considering combination of sensor inputs and 2 outputs to control speed and turning rate of the robot.

## 3.2.1 Inputs

## a) Sensor combination

Eight infra-red sensors are positioned on the robot to determine the distance from the walls of the environment. Out of these 3 sensors on the left, 3 sensors on the right and 2 sensors at the rear side of the robot are placed. 10-bit analog to digital converter converts the distance in millimetre to a value between 0 and 1024.

Weighted scaling was used to combine the sensor values. Weights were determined empirically and the values are mentioned below. Table 1 shows the sensor combination and weights for each sensor. Figure 2 show the position of sensor on robot and the combination along with weights pictorially.

Input Variable

Sensor Combination

Comment

Left

((2*sensor1) + (sensor 2)) / 150

Obstacle distance on left

Front

((10*sensor3) + (sensor4)) / 550

Obstacle distance in front

Right

((10*sensor5) + (sensor6)) / 550

Obstacle distance on right

Table 1

Figure 2

Values 0 to 1024 are scaled to a value 0 to 21. Large value on fuzzy sets causes the fuzzy system to perform roughly (Pg124) [1]. Scaling was done to make the fuzzy system to perform smoothly. Weighted scaling caused the robot stay sufficiently away from the corners of the obstacle. [Appendix 1]. Universe of discourse for left, front and right input variables are 0 to 21.

## b) Fuzzy sets & Memberships functions

The range of values for each fuzzy set was determined empirically. Initially performance testing started with approximate range of values and after many trials the below values showed satisfactory performance. Table 2 tabulates the input variables, universe of discourse, fuzzy sets, membership function and range of values for membership function. Appendix 2 displays the fuzzy sets of input variable left, right and front.

Input variables (Universe of discourse)

Linguistic variable 1, Membership function, Range of values

Linguistic variable 2, Membership function, Range of values

Linguistic variable 3, Membership function, Range of values

Left (0 - 21)

Away, Trapezoidal, [-1 -1 1.4 10]

Moderate, Triangular, [2 9 16]

Close, Trapezoidal, [10 17.5 22 22]

Front (0 -21)

Away, Trapezoidal, [-1 -1 0.9 10]

Moderate, Triangular, [0 10 20]

Close, Trapezoidal, [10 16 21 21]

Right (0 - 21)

Away, Trapezoidal, [-1 -1 0 10]

Moderate, Triangular, [0 9.5 20]

Close, Trapezoidal, [10 17.6 21 21]

Table 2

## 3.2.2 Outputs

Two outputs speed and turn rate from FIS is converted to wheel speeds by a function which was provided for assignment.

## Speed

Output variable speed with 0 to 200 as universe of discourse and with three fuzzy sets of linguistic variable slow, medium and fast was tested. These parameters caused the robot to cover smaller distance and caused jerky motion. And hence number of fuzzy sets was increased to five with linguistic variables crawl, slow, medium, fast and rapid which provided a smother movement of robot. But the universe of discourse for speed variable differed in different environments [figure 5 and figure 6]. For example Khepera robot was able to move at a universe of discourse of 0 to 750 in arena_1 and with 0 to 700 in arena_2. Table3(a) tabulates the universe of discourse, fuzzy sets, membership function and range of values for each membership function for arena 1 and appendix 2 shows the plot of the fuzzy sets of the output variable.

Output variable (Universe of discourse)

Linguistic variable 1, Membership function (MF), Range of values for the MF

Linguistic variable 2, Membership function (MF), Range of values for the MF

Linguistic variable 3, Membership function (MF), Range of values for the MF

Linguistic variable 4, Membership function (MF), Range of values for the MF

Linguistic variable 5, Membership function (MF), Range of values for the MF

Speed

(0-750)

Crawl, Trapezoidal, [-6 -6 0 150]

Slow, Triangular, [18.8 162 298]

Moderate, Triangular, [150 375 600]

Speed, Triangular, [450 594.2 729]

Rapid, Trapezoidal,

[600 750 755.7 755.7]

Table 3 (a)

Table 3 (b) tabulates the universe of discourse, fuzzy sets, membership function and range of values for each membership function for arena 2 and appendix 2 shows the plot of the fuzzy sets of the output variable.

Output variable (Universe of discourse)

Linguistic variable 1, Membership function (MF), Range of values for the MF

Linguistic variable 2, Membership function (MF), Range of values for the MF

Linguistic variable 3, Membership function (MF), Range of values for the MF

Linguistic variable 4, Membership function (MF), Range of values for the MF

Linguistic variable 5, Membership function (MF), Range of values for the MF

Speed (0-700)

Crawl, Trapezoidal, [-5.6 -5.6 0 140]

Slow, Triangular, [17.56 151 278.1]

Moderate, Triangular, [140 350 560]

Speed, Triangular, [420 554.7 680.4]

Rapid, Trapezoidal, [560 700 705.6 705.6]

Table 3(b)

## Turn rate

In Universe of discourse of turn rate variable negative values turned the robot in right direction and positive values turned the robot to its left. Output variable turn rate with -3 to 3as universe of discourse and with three fuzzy sets of linguistic variable left, straight and right was tested. These parameters caused the robot to collide frequently. And hence number of fuzzy sets was increased to 7 with linguistic variables mmore right, more right, right, straight, left, more right, mmright which avoided collision with obstacles. And the universe of discourse was also increased to -10 to 10 which caused the robot to cover more distance [Appendix 3]. Table 4 tabulates the final designs universe of discourse, fuzzy sets, membership function and range of values for each membership function and appendix 2 shows the plot of the turn rate variable.

Output variable (Universe of discourse)

Linguistic variable 1, Membership function (MF), Range of values for the MF

Linguistic variable 2, Membership function (MF), Range of values for the MF

Linguistic variable 3, Membership function (MF), Range of values for the MF

Linguistic variable 4, Membership function (MF), Range of values for the MF

Linguistic variable 5, Membership function (MF), Range of values for the MF

Linguistic variable 6, Membership function (MF), Range of values for the MF

Linguistic variable 7, Membership function (MF), Range of values for the MF

Turn rate (-10 to 10)

MMoreRight, Trapezoidal, [-12.89

-11.94

-7.8

-6.428]

MoreRight, Triangular, [-8.57

-5.714

-2.858]

Right, Triangular, [-5.71

-2.85

0]

Straight, Triangular, [-1

0

1]

Left, Triangular, [0

2.85

5.714]

More Left, Triangular, [2.85

5.714

8.572]

MMoreLEft, Trapezoidal, [7.17

8.74

12.54

15.46]

Table 4

Although there are many membership functions are available they cause computational complexity and hence linear fit membership functions were used (Pg 94) [1].

## 3.3 FUZZY RULES AND EVALUATION

Fuzzy Associative Memory (FAM) figure 3 shows the final rules used to design the fuzzy controller for khepera robot. Three inputs variables with three fuzzy sets each caused 27 rules to be framed. The number of fuzzy sets in input variables determines the number of rules and number of fuzzy sets in output variables doesn't affect the number of rules in the FIS. As the number of rules increases the computation complexity increases and hence three fuzzy sets for each input variables where used to achieve satisfactory performance with acceptable complexity. Rules could be combined to achieve a the same performance before combination. Min function was used to implement AND operation and max function was used to implement OR operation. Rules are combined to form 17 rules as shown in the figure 3

Figure 3

## 3.4 METHOD OF CORRELATING CONSEQUENT WITH ANTECEDENT

Two methods to correlate consequent with antecedent are clipping and scaling. In this design clipping was chosen to perform the task because clipping generates output which can be easily defuzzified and with less complex and faster mathematics even though it losses some information [Pg 110] [1].

## 3.5 DEFUZZIFICATION

To generate crisp number from fuzzy input defuzzification is performed. Most popular technique for defuzzification is centroid technique (Pg 111) [1] and hence it was adopted for use in this controller design.

## 4 FINAL SYSTEM

Figure 4

Final fuzzy logic controller which will make Khepera robot to wander about its environment with avoiding obstacles and maintaining wall on its left is a 3 inputs, 2 outputs and 17 rules mamdani FIS system which implements AND by a min function, OR by a max function and defuzzification using centroid technique. Final FIS is show in the figure 4

## 5 RESULTS

Results suggest Khepera robot wandered around the environment avoiding collision with obstacle and has maintained its wall on its left.

Figure 5

Distance covered in arena 1 is 2080.5 cm in 1 min at 0 to 750

Figure 6

Distance covered in arena 2 is 1846.6 cm in 1 min at 0 to 700

## 6 Problems Faced

During the trials robot took different paths in every lap. This was solved by increasing the baud rate from 9600 to 19200 [Appendix 4].

In a particular condition i.e when right is close, front is away and left is away in order to locate the left wall the initial condition was to turn mmore right at a speed of crawl which caused a stagnant condition in few points on the environment (figure 7(a)). And hence the rule was changed to make left with medium speed but it introduced a problem of spinning (figure 7 (b)) before locating the left wall.

Figure 7 (a) Figure 7 (b)

The final FIS is not capable of handling a situation where corner exists in an environment with left wall is medium, right wall is medium and front is far. The robot comes to a stagnant position as shown in the figure 8

Figure 8

When the sensor values showed no nearest wall in either side the FIS was not designed to take a decision. Hence a Boolean logic was introduced to find a wall. When wall is not near in either side Boolean logic decides to make the robot go straight at fixed speed.

## 7 Suitability of Fuzzy Control for this application

Khepera robot was tested in the two environments provided and also in some different custom made environments [appendix 5]. In all these environments with very few issues, as mentioned in problems faced, Khepera is able to perform satisfactorily. This demonstrates the robustness of the system which in turn validates the suitability of fuzzy control for this application.

## 8 Conclusion

Results exhibit that the Khepera robot can wander a fairly longer distance while maintaining the wall on its left and by avoiding collisions with obstacles. Further tuning of fuzzy set enables the robot to wander in difficult environment [appendix 5]

MATLAB CODE

## % -------------------------------------------------

% (c) 2000-2004 Theodor Storm <theodor@tstorm.se>

% http://www.tstorm.se

% Modified by David Lee 2nd February 2007

## % -------------------------------------------------

function avoid(port,baud,time)

% avoid(port,baud,time)

% port = serial port to communicate with (port<0 ==> simulated robot, port>=0 ==> real robot

% baud = baud rate

% time = time to run behaviour

if nargin<3 time=60; end;

if nargin<2 baud=19200; end;

if nargin<1 port=-1; end;

ref=kiks_kopen([port,baud,1]);

kSetEncoders(ref,0,0);

reflex = 0;

t0=clock;

loops=0;

% Read the FIS from disk

fis = readfis('VivekFIS8C_rulecombination');

move_path=0;

## %++++++++++++++VIVEK - BINARY LOGIC++++++++++++++++++

reflex = kProximity(ref);

left = sum(reflex(1:2))/100

front = sum(reflex(3:4))/100

right = sum(reflex(5:6))/100

## %When all the sensor values are less than or equal to 6 indicating %no nearest wall the speed is assigned a value of 200 and the turn %rate is made 0 the loop is repeated until a wall is detected.

if(left<=6&&front<=6&&right<=6)

speed=200;

turn=0;

move_path=1;

end;

while (kiks_ktime(port)<time)

loops=loops+1;

if(move_path==1)

reflex = kProximity(ref);

left = sum(reflex(1:2))/100

front = sum(reflex(3:4))/100

right = sum(reflex(5:6))/100

if(left<=6&&front<=6&&right<=6)

speed=200;

turn=0;

else

move_path=0;

end;

else

## %++++++++++++++VIVEK - END BINARY LOGIC++++++++++++++++++

% Read the Khepera's range sensors and group

% them as necessary

reflex = kProximity(ref);

## %+++++++++++++++VIVEK - WEIGHTED SCALING SENSOR COMBINATION+++++++

left = ((2*reflex(1))+reflex(2))/150;

front = ((10*reflex(3))+reflex(4))/550;

right = ((10*reflex(5))+reflex(6))/550;

## %++++++++++++++++ VIVEK - END ++++++++++++++++++++++++++++++++++++

% Use the FIS to compute the speed and turn rate

FIS_outputs = evalfis([left front right],fis);

% Assign the speed and turn rate to outputs generated by the FIS

turn = FIS_outputs(2);

speed = FIS_outputs(1);

end;

% use the function to calculate the wheel speeds

setWheelSpeeds(ref,speed,turn);

end;

%kGetEncoders(ref)

t=etime(clock,t0);

disp(sprintf('%.1f loops in %.1f seconds = %.1f loops/second (%.0f%%)\n',loops,t,loops/t,(time/t)*100));

kSetSpeed(ref,0,0);

kiks_kclose(ref);

## % *************************************************************

% The following function implements the kinemtatics of the

% robot, converting turn speed and forward speed into speeds

% for the left and right wheels.

% DO NOT MAKE ANY CHANGES TO THIS FUNCTION

function setWheelSpeeds(ref,velocity,rotation)

wheelRadius = 6; % mm

turnRadius = 24; % mm

% Prevent the robot "drifiting to the left"

% Seems to be a bug in KiKs?

if abs(rotation)<0.01

rotation = 0;

end

wl = (1/wheelRadius)*(velocity - turnRadius*rotation);

wr = (1/wheelRadius)*(velocity + turnRadius*rotation);

unitsPerRadPerSec = 0.75;

kSetSpeed(ref,wl*unitsPerRadPerSec,wr*unitsPerRadPerSec);

## APPENDIX 1

Figure A1 (a) Figure A1(b)

In the figure A1(a) robot caused a hit on the wall. Which was resolved by adding weight to the sensors as in figure A1(b).

## APPENDIX 2

Figure A2 (a) Membership functions for Variable Left

Figure A2 (b) Membership functions for Variable Forward

Figure A2 (c) Membership functions for Variable Right

Figure A2 (d) Membership functions for Variable Speed

Figure A2 (e) Membership functions for Variable Turn rate

Figure A2 (a) Figure A2(b)

Figure A2 (c) Figure A2(d)

Figure A2 (e)

## APPENDIX 3

Figure A3

The figure A3 was simulated with -3 to 3 as the universe of discourse of turn rate and with 3 fuzzy sets for turn rate variable which caused a hit with the walls.

## APPENDIX 4

Figure A4 (a) path at 9600 baud rate Figure A4 (b) path at 19200 baud rate

## APPENDIX 5

Figure 5A

Simulation in a difficult environment at a 0 to 400 as universe of discourse for speed. Robot performed satisfactorily except very few places which is shown in the figure 5A marking a circle.