Sei sulla pagina 1di 20

CE-801

Report

KUMAIL JAVAID
1907723
Table of Contents
Table of Contents .......................................................................................................................................... 1
Introduction: ................................................................................................................................................. 2
Literature Review: ......................................................................................................................................... 2
Tasks and their solutions: ............................................................................................................................. 4
Implement a PID Controller that implements a Right Edge Following Behaviour .................................... 4
Implement a Right Edge Following Behaviour and an Obstacle Avoidance Behaviour using Fuzzy Logic
Control: ..................................................................................................................................................... 6
Integrate the aforementioned Fuzzy Logic Right Edge Following and Obstacle Avoidance Behaviours.. 8
Observations: ................................................................................................................................................ 9
Analysis: ...................................................................................................................................................... 10
Result and Conclusions: .............................................................................................................................. 10
References: ................................................................................................................................................. 11
Appendix: .................................................................................................................................................... 13
Introduction:
ARIA robot with SONAR sensors was to be controlled so that it can perform right edge
following. The robot was to be programmed using some controller to take input from sensors
and set the output motor speeds accordingly. For completion of this task, there were quite a
several methods proposed which were then implemented, and this report was prepared. Below
is a summary of the works and researches which have been done in this particular field of PID
and Fuzzy Logic Controller.

Literature Review:
An omnidirectional robot was introduced with a compass and DC motor using STM32F103, PID
was implemented on the feedback got from the compass, and it was sent to DC motor. [1] But
its sometimes really hard to choose the parameters, which is the reason a self-adaptive system
was developed using a backpropagation neural network. [2] For the inverted pendulum
problem, it was also controlled by a self-adaptive PID controller using fuzzy logic. [3][4]
[5] is about a park scene and a visual path tracking system was introduced, having a PID
controller using only one webcam. The error of distance and error of angle is calculated using
images, and then both errors are combined by fuzzification. It is used to detect lanes and makes
the robot able to run through them robustly and autonomously.
Tuning PID controller is the most crucial part. Therefore, an Ant Colony Algorithm was used in
supervised PID controller and compared with the offline PID controller. It was concluded that
the system is efficient if both simultaneously are used rather than applied alone. [6] Few people
have tried even to replace the PID controller, which was IOPID (Integer Order Proportional
Integral Derivative) by implementing FOPID – Fractional Operated PID. It is observed that it is
more efficient than the other one. [7] Performance of conventional and fuzzy-based PID was
compared by controlling an induction motor drive. Whole simulation and modelling were done
in MATLAB. [8] It was also checked on Brushless DC motors (BLDC) and tested with the same
result as the previous one. [9] In the same way, Hydraulic elevators were controlled using both
of the controllers, and it was concluded that fuzzy based PID controller was better in terms of
step response curves, track feature, error curves, and gave a speedy response. [10]
In another process, the PID controller was added with delay control.[11] Main aim of this
research is to shift from analogue PID to discrete or digital PID with FPGA implementation. It
was observed that updated architecture caused the computation to reduce by 50 percent. [12]
PID control was also used UPFC Unified Power Flow Controller, and it is optimized by Zeiglar-
Nichols tuning method. That was done to control the flow of current in the field of power
electronics. The simulation showed that if UPFC is installed externally, the system becomes
quite stable. [13]
[14] suggests that the 3-DOF-EI-PID controller with emotion function should be used and the
tuning technique by using an immune algorithm. For liquid level system, output was non-linear,
so it was converted into linear by Taylor series, and then it was fed into conventional PID and
fuzzy-based PID, the result showed that fuzzy based PID was better. [15] PID controllers
parameters were tuned by Intelligent Iterative Learning Algorithm to control a flexible beam
structure. [16] This hybrid type of controller which consists of conventional as well as fuzzy logic
PID controller and then it is used for pressure control in many industries. [17] Fuzzy logic is
being used in places where there is some kind of control. Another application is FATE (Fuzzy
logic Automatic Transmission Expert System) in which gears are being changed using fuzzy logic.
[18]
Some work is also being done in improving the fuzzy inference and named as FINEST (Fuzzy
Inference Environment Software with Tuning) which would enhance its working. [19] TSK fuzzy
system works on non-linear systems and systems with lesser rules, but now it can be used on a
singleton fuzzy model too. [20] Instead of local linear models, BLM (Boundary Linear Models)
were used for better accuracy and results were compared with conventional models. [21] Some
research is also being done on the type of fuzzy models, previously it was type-0. Then, type-1
and type-2 were developed, and now work is being done on getting another type to get more
complex problems solved. [22] Now there are other fuzzy models which are dynamic, and they
are solved by two methods i.e. local design method and global design method. [23] A fuzzy
visual system is developed based on three different fuzzy classifiers, which, when combined,
allows the robot to detect the presence of doors in the images captured by its camera.
However, the tuning procedure is complex as two different performance indexes are involved
in the optimization process and its a multi-objective problem. Hence, in order to automatically
put the fuzzy system tuning into effect, different single and multi-objective evolutionary
algorithms are considered to optimize the two criteria. [24] Type-2 fuzzy controller is suggested
to control an inverted pendulum system with disturbance, random noise and parameter
variations using Takagi-Sugeno fuzzy and simulation showed better efficacy. [25] Fuzzy control
can also deal with the stability problems and control design for time-delay dependent non-
linear systems. First, it is converted into a nominal linear plant, and the concept of the matching
condition is used. [26]
Development of MRFACS (Model Inference Fuzzy adaptive Control System) is also notable in
this regard as it can be used in complex situations. [27] Another approach is to divide one
system into a number of sub-spaces which could be solved independently by creating individual
rule base and then merged into one. [28] Type-2 fuzzy systems and sets are better than Type-1
in terms of handling the uncertainties of systems. These systems can further be enhanced by
fuzzy interpolation to make complex problems simpler and to have a strong rule base. [29]
Another method can be used for fuzzy system implementation and representation. As fuzzy
logic is theoretically a control system theory, we can induce matrices and state-space
representation to represent fuzzy models which will make it efficient. [30]
FACTS (Flexible AC Transmission Systems) have been used nowadays in power systems
oscillations damping, which used a fuzzy logic system. As the fuzzy system is not much accurate
and can not handle non-linear situations very smoothly, research is being done for increasing its
efficiency. [31] [32]Another approach which is being tested nowadays is to converting the rule-
base to the linguistic rule base. If-then statements are being used, and it has been observed
that results got better with that attempt. [33] Another task which was done was to design a
robust static output-feedback controller for the non-linear network-based controller by using
the T-S fuzzy model. MATLAB LMI toolbox was used for this purpose and results were improved.
[34] Tagagi and Sunego proposed a method which was analyzed by different researchers and it
was concluded that the results were independent of inference and shape of the model, and
thus, it simplifies the procedure to a great extent. [35] Research is also being done on DNA-GA
algorithm for biological DNA and genetic information. Arbitrary singleton sets containing
continuous fuzzy sets are there. In short, fuzzy sets and fuzzy logic is being implemented in
almost every field now. [40] Fuzzy logic is an emerging field and therefore, each step it includes
has been discussed by the researcher. A most important one is defuzzification, i.e. converting
crisp values into mathematical values. [41]

Tasks and their solutions:

Implement a PID Controller that implements a Right Edge Following Behaviour – this
required the ARIA robot’s right side to be at a certain distance to the wall.
For this purpose, a PID controller was designed and implemented. PID controller is used in
control operations in different applications and industries to regulate different variable values
such as pressure, temperature and distance etc. To keep value to a specific value, we use PID.
Here, in this case, we want to keep a fix distance between the wall and the robot, so it is the
first method worth trying to achieve this task. It is highly popular and applicable in different
places that it is the first choice which comes to mind. Its ease of implementation is also one of
the reasons which make it so popular. Previously, a controller was high in demand in industries,
and PID was the only controller back then because there was not much research in other fields
like Fuzzy logic controller and artificial neural network. Therefore, PID was implemented in the
start.
It had one input which was the error (the difference between the desired distance which was
provided and the current distance got from the sensors) and output which was the speed of
right and left motors. The equation used for calculating the output speed using the input error
is given below:
Output = 𝑘𝑝 ∗ 𝑒 + 𝑘𝑖 ∗ 𝑒𝑖 + 𝑘𝑑 ∗ 𝑒𝑑
where
𝑒=𝑑𝑒𝑠𝑖𝑟𝑒𝑑 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 − 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒
ei=ei + e
ed=e-eprev

ei is the accumulation of all the errors, while the differential error is just a comparison to
previous and current error. And then the output speeds were given as:
Rightspeed=250-Output
Leftspeed=250+Output

PID basically has a loop control structure where it constantly checks the error and changes the
output accordingly. It is being done in a loop so whenever variable value moves away from the
desired value, it calculates the error and focuses on moving error to zero and actual value near
to desired value.
Initially, assumed values of kp, ki, and kd were chosen 0.5,0.6,0.7 respectively, but when tried
on the robot and going through each value of error, they were determined accordingly.
Changing the value of the parameter would affect the performance of the robot. Increasing the
kd resulted in increasing the step size while increasing while ki managed the accumulation of
errors. So the values of parameters were changed accordingly and decreased to stabilize the
system. Parameter selection of kp, kd, ki is the most difficult task to calculate as there was no
algorithm which could be used to develop this. However, now researchers have been working
on this so that these parameters could be changed simultaneously as per the need.
Sensor 6 was not exactly at the right; therefore, to measure the horizontal distance between
sensor and wall, it was calculated through Pythagoras theorem and hence multiplied by 0.872.
In this way, all the values were put into the equations and speed of both motors were
calculated with the calculation of errors simultaneously. Velocity and error keep on changing
unless the robot is completely parallel to the wall.
A check was imposed on ei that if it increases by 100, then the error will reset because ei is the
accumulative error, so it gets bigger and bigger with time. It was necessary to reset this for the
smoothness of working of the robot.
Implement a Right Edge Following Behaviour and an Obstacle Avoidance Behaviour
using Fuzzy Logic Control:

First of all, the number of inputs and outputs were determined. For the first half part of the task
which is Right Edge Following, two sensors (6 and 7) were used, and their ranges were divided
into three categories, i.e. Low, Medium and High. For more accuracy, it could have been divided
into more than three categories, but to maintain simplicity, three were enough. Moreover, two
outputs were drawn out of it which are left and right motor speeds.
Same was done for the second part in which three sensors (number 5 was also used) and
ranges were categorized. Outputs were the same.
Next step was fuzzification which means to convert sensor values into membership values
which are between 0 and 1. Trapezoidal graphs were made for this, as shown in figures below.

Fig.1
Fig.2

Rule-base was created for both which are given in the tables below:

Sensor#6 Sensor#7 Left Speed Right Speed


Low Low Low Medium
Low Medium Low Medium
Low High Low Medium
Medium Low Medium Low
Medium Medium Medium Medium
Medium High Low Medium
High Low Low Medium
High Medium Medium Low
High High Medium Low
Table 1

Sensor#5 Sensor#6 Sensor#7 Left Speed Right


Speed
Low Low Low Low High
Low Low Medium High Low
Low Low High Medium Low
Low Medium Low Low High
Low Medium Medium High Medium
Low Medium High Medium Low
Low High Low Medium Medium
Low High Medium Medium Low
Low High High Medium Low
Medium Low Low Low High
Medium Low Medium Low High
Medium Low High High Low
Medium Medium Low Low High
Medium Medium Medium Low Medium
Medium Medium High High Medium
Medium High Low Low High
Medium High Medium Medium Medium
Medium High High Low Medium
High Low Low Low High
High Low Medium Low High
High Low High Low High
High Medium Low Low High
High Medium Medium Low High
High Medium High High Medium
High High Low Low High
High High Medium Medium Low
High High High Medium High
Table 2

Check firing strengths and multiply it to the centre values of output fuzzy set to get the
defuzzified values. Those values are speeds of motors – left and right.

Integrate the aforementioned Fuzzy Logic Right Edge Following and Obstacle Avoidance
Behaviours.

Combined those two by subsumption by putting and if the condition that if the front sensor is
blocked by any obstacle, then the rule base of obstacle avoidance will be used otherwise the
rule of right edge following will be implemented. Another rule was context blending. It means
that two fuzzy logic systems are combined through implementing another fuzzy logic on these.
In previous cases, two algorithms were formed in which they were working individually. One
was for right edge following, and other was for obstacle avoidance. In the right edge following,
it was calculating how much the robot is away from the wall and how much it needs to come
closer to the robot. On the other hand, for obstacle avoidance, it would sense how much the
obstacle is nearer to the robot and how much it needs to move in another direction and which
direction.
Now, the same theory was implemented between these two. In context blending, it was done
that an algorithm was there which would calculate how much right edge following is needed for
the particular sensor readings and how much obstacle avoidance is required for particular
sensor readings. Actually what is being done is performing fuzzy logic control over two other
fuzzy logic controllers of right edge following and obstacle avoidance.

Observations:

PID controller show poor control performances for an integrating process and a large time
delay process. Further, it can not incorporate ramp-type setpoint change and slow disturbance.
Parameter selection is another famous problem, and without that, the PID controller is of no
use. It solely depends on those. However, good thing it has is its implementation is that it is
quite simple and easy. Less computational power Is required and can be used in every system
and every control application.
Fuzzy Logic has the advantage that it describes systems in terms of a combination of numeric
and linguistic approach is taken, which makes this easy to implement. This is easier than both
other approaches, i.e. completely mathematics approach and completely linguistic approach as
it is itself a mixture and very often, system knowledge is available in such a combination.
The fuzzy logic actually is able to measure the certainty and uncertainty of membership of
element of the set. Just like a human mind works, it analogously makes a decision during the
mental and physical activities. For most of the case, the solution of certain case is found on the
principle of rules that were defined by the fuzzy logic controller.
Fuzzy algorithms are not very sensitive to changing environments or forgotten rules, and hence,
they are robust.
The reasoning process is often quite simple if compared to computationally precise systems, so
it can be said that computing power is saved. This is a very interesting feature, especially in
real-time systems, and it does not require GPUs instead of CPUs.
Fuzzy methods usually have a shorter development time if compared than conventional
methods as they are basically the If-Else statements which are coded.
As everything comes with a price, price of fuzzy logic is accuracy which is the hard task to
achieve. You have to try hard, and by hit and trial method, you have to define the initial ranges
for membership functions. Then another task is the rule-base which is hard to define. You have
to define rules for this system. Otherwise, it can not work, which is unlike the neural network
where you do not define any rules and implementation is quite easier than fuzzy logic.
Analysis:
Improvements can be made as said in the literature review; researchers have been working in
this sector and trying to improve the working of these algorithms. PID parameters would
automatically be taken, which are ideal, and we do not have to work hard on that. In fuzzy logic,
supervised learning is being used to increase the accuracy of the controller. Different type of
fuzzy logic controllers is being designed for different purposes, specifically to get better results.

Result and Conclusions:


When right edge following would run, in command window following will be shown:

Fig.3
If this runs, the command window is like below:

Fig. 4
Its path can be seen in the figure below:

Fig.5
It was concluded that although the task was simple and simple techniques were used, but still,
there was room for improvement and usage of the updated and latest algorithm should have
been used for better smoothness.

References:
[1] Phuong Truong, Le & Khai, Khai. (2019). “Self-developed Three Wheels Omni-Directional for Autonomous
Mobile Robots”. 206-209.

[2] Liu, Jiayu & Pan, Wei & Qu, RuoPeng & Xu, Meng. (2018). “Research on the Application of PID Control with
Neural Network and Parameter Adjustment Method of PID Controller.” 72-76.

[3] Zeng, Xiangkun. (2019). “Principle and Application of Self-Adaptive Fuzzy PID Control.” 797-802.

[4] Manikandan, R. & Arulprakash, A. & Murugan, Arulmozhiyal. (2015). “Design of equivalent fuzzy PID controller
from the conventional PID controller.” 356-362.

[5] Zhu, Linjiong & Wang, Wenfu & Yang, Weijie & Pan, Zhijie & Chen, An. (2018). “Visual Path Tracking Control for
Park Scene.” 195-201.

[6] Singh, Samarth & Mitra, R.. (2014). “Comparative analysis of the robustness of optimally offline tuned PID
controller and Fuzzy supervised PID controller.” 2014 Recent Advances in Engineering and Computational Sciences,
RAECS 2014. 1-6.

[7] Sundaravadivu, K. & Arun, B. & Saravanan, K.. (2011). “Design of Fractional Order PID Controller for liquid level
control of the spherical tank. Proceedings” - 2011 IEEE International Conference on Control System, Computing
and Engineering, ICCSCE 2011.

[8] Pati, Swagat & Panda, Abinash & Mohanty, Sattapriya. (2015). “A comparative performance study of scalar
controlled induction motor using a PID controller and fuzzy PID controller.” 2014 International Conference on
Circuits, Power and Computing Technologies, ICCPCT 2014. 904-909.
[9] Murugan, Arulmozhiyal & Rajendran, Kandiban. (2012). “Design of Fuzzy PID controller for Brushless DC
motor.” International Conference on Computer Communication and Informatics. 1-7.

[10 ] Xu, Xian & Wang, Qingling. “Speed control of hydraulic elevator by using PID controller and self-tuning fuzzy
PID controller.” (2017).

[11] Balasevicius, Leonas & Kunickaite, Asta & Janusevicius, Vytis. “Discrete-time PID Controller Design in
Programmable Logical Controllers.” (2007).

[12] Xu, Xian & Wang, Qingling. “Speed control of hydraulic elevator by using PID controller and self-tuning fuzzy
PID controller.” 812-817. (2017).
[13] Samal, Sudhansu & Panda, P.. “Damping of power system oscillations by using unified Power Flow Controller
with POD and PID controllers.” (2014).
[14] Kim, Dong. (2012). “Design and tuning approach of 3-DOF emotion intelligent PID (3-DOF-PID) controller.”
Proceedings - UKSim-AMSS 6th European Modelling Symposium, EMS 2012.

[15] Prusty, Sankata & Pati, Umesh C. & Mahapatra, Kamalakanta. (2014). “Implementation of the fuzzy-PID
controller to the liquid level system using LabVIEW.” International Conference on Control, Instrumentation, Energy
and Communication, CIEC 2014.

[17] Anitha, T. & Govindasamy, Gopu & Nagarajapandian, M. & Devan, P.. “Hybrid Fuzzy PID Controller for Pressure
Process Control Application.” (2019).

[18] Bastian, A. & Tano, S. & Oyama, T. & Arnould, T.. (1995). FATE: fuzzy logic automatic transmission expert
system. 5 - 6 vol.5

[19] Oyama, T. & Tano, S. & Miyoshi, T. & Kato, Y. & Arnould, T. & Bastian, A. & Umano, M.. (1995). “FINEST: fuzzy,
inference environment software with tuning.” 3 - 4 vol.5.

[20] Chae, Yangbum & Oh, Kabsuk & Lee, Wonchang & Kang, Geuntaek. “Transformation of TSK fuzzy system into a
fuzzy system with singleton consequents and its application.” 2. 969 - 973 vol.2. (1999).

[21] Ponniah, Mohanlal & Kaimal, Ramachandra.. “Fuzzy modelling and optimal control of nonlinear second-order
systems.” 611 - 616 vol.2. (2004)

[22] Castillo, Oscar & Melin, Patricia. “An Approach for Optimization of Intuitionistic and Type-2 Fuzzy Systems in
Pattern Recognition Applications.” (2019).

[23] Chen, Chieh-Li & Chen, Pey-Chung & Chen, Cha'o-Kuang. “Analysis and design of fuzzy control system. Fuzzy
Sets and Systems.” (2001).

[25] Al-Mahturi, Ayad & Santoso, Fendy & Garratt, Matt & Anavatti, S.G.. “An Intelligent Control of an Inverted
Pendulum Based on Adaptive Interval Type-2 Fuzzy Inference System.” (2019).

[26] Han, Hugang. “Theoretical Analysis for a Design of Delay-Dependent Fuzzy Control System with Input
Saturation.” 2190 - 2194. (2006).

[27]Hsu, Chin-Chih & Yamada, S. & Fujikawa, H. & Shida, K.. “MRFACS with nonlinear consequents by fuzzy
identification of system for time-delay system.” 283 - 288 vol.1. (1995).

[28]Lin, Yinghua & III, G.A. & Coggeshall, Stephen. (1997). “Using fuzzy partitions to create fuzzy systems from
input-output data and set the initial weights in a fuzzy neural network.” Fuzzy Systems, IEEE Transactions on. 5.
614 - 621.
[29]Li, Jie & Yang, Longzhi & Fu, Xin & Chao, Fei & Qu, Yanpeng. “Interval Type-2 TSK+ Fuzzy Inference System.” 1-
8. (2018).

[30]Zhinong, Miao & You, Xia & Zhen, Huijun. “A New Method for Fuzzy System Representation and
Implementation.” 142-146. (2008).

[31]Khanmohammadi, S. & Ghaderi, Omid. “Simultaneous Coordinated Tuning of Fuzzy PSS and Fuzzy FACTS
Device stabilizer for Damping Power System Oscillations in Multi-Machine Power System.” 1 - 6. (2007).

[32]Azevedo, Haroldo & Martins, R.M. & Oliveira, J.C. & Moraes, A.J. & Guimares, G.C. & Abreu, J.P.G.. “A fuzzy
logic-based stabilizer for an electric power system.” 1561-1566 vol.3. (1995).

[33] Ishibuchi, H.. “Fuzzy reasoning method in fuzzy rule-based systems with general and specific rules for function
approximation.” 1. 198 - 203 vol.1. (1999).

[34] Chen, Song-Shyong. “Networked Control Systems Design via Fuzzy Logic Method. IEEE International
Conference on Fuzzy Systems.” 1238-1243. (2009).

[35] Qiao, Wu & Mizumoto, M.. “Frame of fuzzy systems with the functional rule.” 2153-2158 vol.4. (1995).

[36] Ren, Lihong & Ding, Yongsheng & Shao, Shihuang. “DNA genetic algorithms for the design of fuzzy systems.”
1005 - 1008 vol.2. (2000).

[37] Ghalia, M.B.. “Aspects of the effects of the defuzzification stage on the stability and the performance of fuzzy
model-based control systems.” 475 - 478 vol.1. (1996).

Appendix:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <conio.h>
#include <Aria.h>
using namespace std;
//to calculate the minimum of 3 values
float MIN(float a, float b, float c) {

float MIN = 10000;

if (float a = MIN)
MIN = a;
if (b < MIN)
MIN = b;
if (c< MIN)
MIN = c;

return MIN;
}
//inputing ranges for low, medium and high
int range_1 = 0;
int range_2 = 300;
int range_3 = 500;
int range_4 = 700;
int range_5 = 800;
float sum_of_inputs_membership = 0;
//fuzzification
// taking input value and calculating membership functions
float mem_low(double x)
{
if (x <= range_1 || x >= range_3) {
return 0;
}
else if ((x <= range_3) && (x >= range_2)) {

float member = (range_3 - x) / (range_3 - range_2);


return member;
}
else
return 1;
}

float mem_medium(double x)
{
if (x <= range_2 || x >= range_5) {
return 0;
}
else if ((x >= range_1) && (x <= range_2)) {

float member = (x - range_1) / (range_2 - range_1);


return member;
}
else if ((x >= range_4) && (x <= range_5)) {

float member = (range_5 - x) / (range_5 - range_4);


return member;
}
else
return 1;
}

float mem_high(double x)
{
if (x <= range_4) {
return 0;
}
else if ((x >= range_4) && (x <= range_5)) {

float member = (x - range_4) / (range_5 - range_4);


return member;
}
else
return 1;
}

int main(int argc, char **argv)


{
Aria::init();
ArRobot robot;
ArArgumentParser argParser(&argc, argv);
argParser.loadDefaultArguments();
ArRobotConnector robotConnector(&argParser, &robot);
if (robotConnector.connectRobot()) std::cout << "Robot connected!" << std::endl;
robot.runAsync(false);
robot.lock();
robot.enableMotors();
robot.unlock();
while (true) {
ArSensorReading *sonarSensor[8];
//getting readings from all sensors
int sonarRange[8];
for (int i = 0; i < 8; i++)
{
sonarSensor[i] = robot.getSonarReading(i);
sonarRange[i] = sonarSensor[i]->getRange();
cout << i<< " " << "sensor reading:"<<" " << sonarRange[i] << endl;
}
robot.setVel2(50, 50);

ArUtil::sleep(100);
//checking if something is front of the robot

if (sonarRange[2] < 400 || sonarRange[5] < 400 || sonarRange[3] < 350 || sonarRang
e[4] < 350)
{
cout << "Obstacle Avoidance" << endl;
int range_1 = 0;
int range_2 = 200;
int range_3 = 400;
int range_4 = 600;
int range_5 = 5000;
float first_sensor = sonarRange[2];
float second_sensor = MIN(sonarRange[3], sonarRange[4], 9999);
float third_sensor = sonarRange[5];
float inputarray[27];

cout << mem_low(first_sensor) << " " << mem_medium(first_sensor) << " " << mem_
high(first_sensor)<< " "<< endl;

cout << mem_low(second_sensor) << " " << mem_medium(second_sensor) << " " <<
mem_high(second_sensor) << " " << endl;

cout << mem_low(third_sensor) << " " << mem_medium(third_sensor) << " " << me
m_high(third_sensor) << " " << endl;
//defuzzification matrices

float leftmotorspeed[] = { 120,320,200,120,320,200,200,200,200,120,120,320,120,12


0,320,120,200,120,120,120,120,120,120,320,120,200,200 };

float rightmotorspeed[] = { 320,120,120,320,200,120,200,120,120,320,320,120,320,2


00,200,320,200,200,320,320,320,320,320,200,320,120,320 };
//creating fuzzified matrices

float firstsensorarray[] = { mem_low(first_sensor) ,mem_low(first_sensor) ,mem_low(


first_sensor),mem_low(first_sensor) ,mem_low(first_sensor) ,mem_low(first_sensor),mem_l
ow(first_sensor) ,mem_low(first_sensor) ,mem_low(first_sensor),mem_medium(first_sensor
),mem_medium(first_sensor),mem_medium(first_sensor),mem_medium(first_sensor),mem_
medium(first_sensor),mem_medium(first_sensor),mem_medium(first_sensor),mem_mediu
m(first_sensor),mem_medium(first_sensor),mem_high(first_sensor),mem_high(first_sensor),
mem_high(first_sensor),mem_high(first_sensor),mem_high(first_sensor),mem_high(first_se
nsor),mem_high(first_sensor),mem_high(first_sensor),mem_high(first_sensor) };

float secsensorarray[] = { mem_low(second_sensor),mem_low(second_sensor),mem_


low(second_sensor),mem_medium(second_sensor),mem_medium(second_sensor),mem_m
edium(second_sensor),mem_high(second_sensor),mem_high(second_sensor),mem_high(sec
ond_sensor),mem_low(second_sensor),mem_low(second_sensor),mem_low(second_sensor)
,mem_medium(second_sensor),mem_medium(second_sensor),mem_medium(second_senso
r),mem_high(second_sensor),mem_high(second_sensor),mem_high(second_sensor),mem_l
ow(second_sensor),mem_low(second_sensor),mem_low(second_sensor),mem_medium(sec
ond_sensor),mem_medium(second_sensor),mem_medium(second_sensor),mem_high(seco
nd_sensor),mem_high(second_sensor),mem_high(second_sensor) };

float thirdsensorarray[] = { mem_low(third_sensor),mem_medium(third_sensor),me


m_high(third_sensor),mem_low(third_sensor),mem_medium(third_sensor),mem_high(third
_sensor) ,mem_low(third_sensor),mem_medium(third_sensor),mem_high(third_sensor) ,me
m_low(third_sensor),mem_medium(third_sensor),mem_high(third_sensor) ,mem_low(third
_sensor),mem_medium(third_sensor),mem_high(third_sensor) ,mem_low(third_sensor),me
m_medium(third_sensor),mem_high(third_sensor) ,mem_low(third_sensor),mem_medium(t
hird_sensor),mem_high(third_sensor) ,mem_low(third_sensor),mem_medium(third_sensor),
mem_high(third_sensor) ,mem_low(third_sensor),mem_medium(third_sensor),mem_high(t
hird_sensor) };

//checking for which row would fire (firing strengths)


for (int o = 0; o < 27; o++) {

if (firstsensorarray[o] == 0 || secsensorarray[o] == 0 || thirdsensorarray[o] == 0)


inputarray[o] = 0;
else

inputarray[o] = MIN(firstsensorarray[o], secsensorarray[o], thirdsensorarray[o]);


cout << inputarray[o] << " ";

sum_of_inputs_membership = sum_of_inputs_membership + inputarray[o];


}
float sumofleft = 0;
float sumofright = 0;
//defuzzification
for (int a = 0; a < 27; a++) {
float newelementleft = inputarray[a] * leftmotorspeed[a];
float newelementright = inputarray[a] * rightmotorspeed[a];
sumofleft = sumofleft + newelementleft;
sumofright = sumofright + newelementright;
}
float left_speed = (sumofleft / (sum_of_inputs_membership));
float right_speed = (sumofright / (sum_of_inputs_membership));
cout << "final left speed:" << left_speed << endl;
cout << "final right speed:" << right_speed << endl;
robot.setVel2(left_speed, right_speed);
}
else {
cout << "Right Edge Following" << endl;
float first_sensor = sonarRange[6];
float second_sensor = sonarRange[7];
float inputarray[9];

cout << mem_low(first_sensor) << " " << mem_medium(first_sensor) << " " << mem_
high(first_sensor) << " " << endl;

cout << mem_low(second_sensor) << " " << mem_medium(second_sensor) << " " <<
mem_high(second_sensor) << " " << endl;
float leftmotorspeed[] = { 80,80,80,200,200,80,80,200,200 };
float rightmotorspeed[] = { 200,200,200,80,200,200,200,80,80 };
float sum_of_membership = 0;

float sensor1membershipvaluearray[] = { mem_low(first_sensor) ,mem_low(first_sens


or) ,mem_low(first_sensor),mem_medium(first_sensor),mem_medium(first_sensor),mem_m
edium(first_sensor),mem_high(first_sensor),mem_high(first_sensor),mem_high(first_sensor)
};

float sensor2membershipvaluearray[] = { mem_low(second_sensor),mem_medium(se


cond_sensor),mem_high(second_sensor),mem_low(second_sensor),mem_medium(second_s
ensor),mem_high(second_sensor),mem_low(second_sensor),mem_medium(second_sensor),
mem_high(second_sensor) };
for (int o = 0; o < 9; o++) {

if (sensor1membershipvaluearray[o] == 0 || sensor2membershipvaluearray[o] == 0)
inputarray[o] = 0;
else

inputarray[o] = MIN(sensor1membershipvaluearray[o], sensor2membershipvaluearra


y[o],9999);
cout << inputarray[o] << " ";
sum_of_membership = sum_of_membership + inputarray[o];

}
float sumofleft = 0;
float sumofright = 0;
for (int a = 0; a < 9; a++) {
float newelementleft = inputarray[a] * leftmotorspeed[a];
float newelementright = inputarray[a] * rightmotorspeed[a];
sumofleft = sumofleft + newelementleft;
sumofright = sumofright + newelementright;
}

float left_speed = (sumofleft / (sum_of_membership));


float right_speed = (sumofright / (sum_of_membership));
cout << "final left speed:" << left_speed << endl;
cout << "final right speed:" << right_speed << endl;
robot.setVel2(left_speed, right_speed);
}

ArUtil::sleep(500);

}
return 0;
}

Potrebbero piacerti anche