Case Study 1
Case Study 1
00
Printed in Great Britain. # 2005 TEMPUS Publications.
178
Implementing Fuzzy Logic for Machine Intelligence 179
Table 1. Sensor interfacing configuration switch has three pins, pin 3 being ground, pin 2 is
Vcc and pin 1 is the output. When the switch is
Sensor Set Enable Pin Output
open, the output is ground, when closed the output
Left PD2 PE7 is Vcc. The bump switches are connected to a 10-
Centre PA3 PE6 pin connector that is hard-wired on the microcon-
Right PD3 PE5 troller board. The analogue output for the bump
switches is received on the PE3 line of the micro-
controller [3]. When one or more of the bump
OBSTACLE SENSORS AND THEIR switches are triggered, a binary value is returned
INTERFACING AND SOFTWARE DRIVERS to the microcontroller (via PE3). The return values
are shown in Table 2.
Ultrasonic sensors For this application, only the two front bump
For obstacle detection operation, an ultrasonic switches were being used; the output line for the
ranging system was chosen. The sensor system rear bump switch was hard-wired to ground, there-
used was a Velleman Parking Radar kit available fore only analogue outputs of 0±3 could be
from Dick Smith Electronics stores. The output of received from the bump switches.
the sensor kit is either high or low; no ranging data
is provided from the kit [7]. The ultrasonic detec-
tion module works on a very simple principle. It FUZZY LOGIC FOR OBSTACLE
transmits a 40 kHz signal from an ultrasonic AVOIDANCE
transducer and, with the receiving transducer, the
echo from the transmitter is monitored at a Why fuzzy logic?
sampling frequency of 26 Hz. When an echo is For the obstacle avoidance algorithm, a fuzzy
received, the resultant output from the receiver is logic system was chosen. There are two main
then conditioned before being used to drive a piezo reasons why a fuzzy logic system was chosen: the
buzzer. The resultant output to the piezo buzzer is first is due to the ultrasonic sensors that are used,
an active low signal on the negative pin (i.e. when and the second is because of processing require-
an obstacle is detected, the signal is low). For the ments. Ultrasonic sensors in a distance ranging
application of obstacle avoidance, the piezo buzzer system such as the one used in this application are
was removed and the negative pin was connected notorious for providing imprecise data, and using
to the Rug-Warrior's microcontroller. a fuzzy logic system aids in ensuring that this lack
Three sets of ultrasonic sensors (and so three of precision has minimal effects on the overall
Velleman Parking Radar kits) were required, one functionality of the program. One of the main
to detect objects on the left, one set for the centre considerations for the implementation of this
and one set for the right. This meant that three application was the processing speed of the micro-
digital inputs and three digital outputs were controller and thus the reaction time of the robot.
required. The digital inputs were available in the With this in mind, it was necessary to minimise the
form of PE5, PE6 and PE7 (Port E, lines 5±7). number of instructions and calculations required.
These lines were used to receive the output (or echo Many obstacle avoidance techniques that do not
line) from the sensors [2, 3]. In order to get the use a fuzzy logic system work by dividing the area
required three digital outputs, the two infrared surrounding the robot into a grid pattern and then
emitters and the piezo buzzer were removed from calculating the probability of an obstacle being
the microcontroller. This left the digital output present in each square of the grid based on the
lines PD2, PD3 and PA3 available for the sensor sensor data. These methods require a vast number
enable lines. of calculations and so significantly increase the
response time of the robot. Fuzzy logic requires
Bump Switches much fewer calculations and so is more suited for
The Rug-Warrior Pro comes standard with this application. For this application of fuzzy logic
three bump switches; one on the front left, one for machine intelligence, there were three basic
on the front right and one at the rear. Each bump steps to developing the program:
Rear (Bit 2) Left (Bit 1) Right (Bit 0) Returned Value Analogue Value
0 0 0 000 0
0 0 1 001 1
0 1 0 010 2
0 1 1 011 3
1 0 0 100 4
1 0 1 101 5
1 1 0 110 6
1 1 1 111 7
180 M. D. Hurley et al.
. converting the obstacle detection algorithm to object and return again, this time-of-flight must be
get distance-ranging data; halved.
. developing a fuzzy logic algorithm based on the ToF Number of Clock Cycles x Clock Period
ranging data; and No. of cycles x 0.5 us
. combining the ranging system with the fuzzy Distance (ToF / 2) x Speed of Sound
logic algorithm and the bump switches to get (ToF / 2) x 342 m/s
an overall obstacle avoidance program.
Both these calculations can be combined, giving
Distance ranging the result as: Distance ToF x 0.0000855, where
The distance ranging algorithm is very simple the distance is in metres.
and is conducted separately for each set of sensors. If no echo is detected, the result is that the
The enable bit is set to allow the signal to be set by distance is set to the maximum, which for this
the transmitter. For the left set of sensors, this is application is one metre.
PD3, and Port D is located at address 0x1008 and PE7 peek (0x100A) & 0b10000000;
0b00001000 indicated bit 3 at that address [4]. That if (PE7 0)
is, bit_set (0x1008, 0b00001000). { left_time peekword (0x100E)
The internal clock on the microcontroller is then - left_start;
checked and the transmit time is stored as a left_dist (float) left_time * 0.0000855;
variable. For the left set, it is left_start. The }
internal clock, also called the E-clock, is located else
at address 0x100E. Thus, left_start peekword left_dist 1.0;
(0x100E). Once the object distance has been calculated, the
The output or echo bit is then checked. If an enable bit for the transmit signal is cleared, turning
echo has been received, the E-clock is again off the transmit signal. The operation is then
checked and the signal's time-of-flight is calcu- returned to the main program returning the obsta-
lated. This time-of-flight is simply the time the cle distance. That is:
echo is received minus the transmit time. The
result is the number of machine cycles that have bit_clear (0x1008, 0b00001000);
elapsed. Once the time-of-flight has been calcu- return (left_dist);
lated, this is then converted into a distance by
taking into account the speed of sound and the Membership functions
clock frequency. However, as the time-of-flight (or For a fuzzy logic operation, two main parts are
ToF) is the time taken for the signal to travel to the required. The first is the membership functions
and, the other part that a fuzzy logic operation membership functions. They are used to convert
requires is the rule base, which defines the required the ranging data into near and far components. For
outputs for any given combination of inputs [5, 6]. distances less than 0.25 m (25 cm), near 1 and
The membership functions are used for converting far 0. For distances greater than 0.75 m (75 cm),
the discreet data from the inputs, in this case the near 0 and far 1. For distances greater than
ranging data, into a fuzzy value between zero and 0.25 m and less than 0.75 m, the result is a combina-
one, or converting the fuzzy output values into tion of near and far [6].
discrete values for output. For this application In order to convert the ranging data into fuzzy
there were three sets of membership functions values, these membership functions need to be
required, one for the inputs (distance) and two for converted into calculations that can be implemen-
the outputs (i.e., translational and rotational speed). ted using Interactive C. Below is the code that is
The ranging data (inputs) use the distance used to do this.
float NEAR (float dist_near) required. The fuzzy value for each parameter is
{ multiplied by the speed at the maximum point of
if (dist_near < 0.25) the membership function (refer to Fig. 3), then the
return (1.0); three are added together. That is, the fuzzy value
else if (dist_near > 0.75) for slow is multiplied by 0.0, the medium fuzzy by
return (0.0); 25.0 and the fast fuzzy value by 50.0. By adding
else these values together, the required speed can be
return ( (±2.0 * dist_near) 1.5); found.
}
float DEFUZZIFY_SPEED (void)
float FAR (float dist_far) {
{ float speed;
if (dist_far < 0.25) speed (max_SLOW * 0.0)
return (0.0); (max_MED * 25.0)
else if (dist_far > 0.75) (max_FAST * 50.0);
return (1.0); return (speed);
else }
return ( (2.0 * dist_far)Ð0.5);
} Calculating the rotational speed output follows
the same principle as the translational speed;
There are two required outputs for the fuzzy however, the rotational speed has five parts: turn
logic system, the translational speed and the rota- right big (TRB), turn right small (TRS), turn zero
tional speed. These two outputs are the values that (TZ), turn left small (TLS), and turn left big
are used for the robot's drive function. The trans- (TLB). The outputs for rotational speed can be
lational speed is divided into three parts: slow, in the range of ÿ50 to 50, where ÿ50 is TLB and 50
medium and fast, as shown in Fig. 3, with the is TRB (refer to Fig. 4).
maximum speed being 50 for this application. This Converting the rotational speed calculations to
is reflected as a percentage of the robot's maximum code for Interactive C uses the following function:
speed. The top speed is not used in this application,
because slowing the robot down gives a better float DEFUZZIFY_SA (void)
response as it allows more time for the robot to {
react as the environment changes. For the output float SA;
variable, a fuzzy value for each parameter (i.e. SA (max_TLB*-
slow, medium and fast) is received. These fuzzy 50.0) (max_TLS*25.0) (max_TZ*0.0)
values are combined to give the required output. (max_TRS*25.0) (max_TRB*50.0);
In order to convert the fuzzy values into a return (SA);
discrete speed, only one simple calculation is }
Fuzzy rule base The fuzzy value for the output is the smallest of the
For a fuzzy logic system, the rule base defines input values, so SLOW and TRB equal the result
the required outputs for any given combination of from the minimum calculation. By conducting this
inputs. The variables for each input or output are for each rule, fuzzy values for all the output
defined by the membership functions. For the parameters can be derived. Looking at the rule
distance inputs, they are either near or far; for base (Table 3), it is obvious that there is going to
translation speed output, they are slow, medium or be more than one value for each parameter. In this
fast and for the rotational speed output they are application there are either two or three values for
turn right big (TRB) turn right small (TRS), turn each. This means it is necessary to decide what the
zero (TZ), turn left small (TLS) or turn left big final output is going to be. Using a maximum
(TLB) [6]. For example, if an object is near to the calculation does this according to the fuzzy infer-
left, centre and right sensors, then rule A applies ence [5]. The final value is the maximum value
(refer to Table 3). This would make the translation from the values calculated above. For example,
speed slow and the rotation turn right big (or rules A, C and F will all give a value for the output
TRB). In converting the rule base, the rules are SLOW; the final value for this output is the largest
expressed as a simple If±Then rule; for example, in of these three values. This is given in the Inter-
the case of rule A, this is converted into an If±Then active C code procedure below:
rule as:
float MAX (float max1, float max2, float max3)
IF (left Near) & (centre Near) {
& (right Near) THEN (speed slow) float max_val;
& (rotation TRB) max_val max1;
if (max2 > max_val)
Fuzzy logic operation max_val max2;
For calculating the fuzzy values for the outputs, if (max3 > max_val)
it is necessary to combine the input fuzzy values max_val max3;
with the rule base. The fuzzy value for each rule return (max_val);
output is the smallest fuzzy value from the rule }
inputs. For example, take rule A: Once this operation is complete, fuzzy values
IF (left Near) & (centre Near) will have been found for each of the output
& (right Near) THEN (speed slow) parameters, which are subsequently defuzzified to
& (rotation TRB) get discrete values that may be used to drive the
robot.
To find the fuzzy values for the output, it is
necessary to find the smallest input value. Taking Overall machine intelligence
the near component for the three inputs (left, The overall functionality of the program is a
centre and right), a calculation is done to find combination of the bump switches, the ultrasonic
the minimum value. The Interactive C code is as distance ranging and the fuzzy logic operation. To
follows: begin, the status of the bump switches are checked.
float MIN (float min1, float min2, float min3) If one is closed, the robot will reverse and turn
{ away from the object by 90; if both are closed, the
float min_val; robot will reverse and turn 180. When neither of
min_val min1; the bump switches is closed (the normal case), the
if (min2 < min_val) program receives ranging data from the sensors
min_val min2; then uses this to conduct the fuzzy logic opera-
if (min3 < min_val) tions. The outputs from the fuzzy logic operation
min_val min3; are a translational speed and a rotational speed,
return (min_val); and these are used to drive the robot using default
} the library function: drive (trans_vel, rot_vel).
Figure 5 shows the flowchart of the fuzzy logic
In this case, the outputs are SLOW and TRB. obstacle avoidance algorithm, and Fig. 6 shows the
184 M. D. Hurley et al.
REFERENCES
1. I. Baturone et al., Microelectronic Design of Fuzzy Logic Based Systems, CRC Press, Boca Raton
(2000).
2. J. L. Jones, A. M. Flynn and B. A. Seiger, Mobile Robots: Inspiration to Implementation (2nd
edition), A. K. Peters, Massachusetts (1999).
3. J. L. Jones, Rug-Warrior Pro Assembly Guide, A. K. Peters, Massachusetts.
4. A. Wright, R. Sargent and C. Witty, Interactive C Users Guide (0.9 ed.), Newton Research Labs
(1997).
5. L. X. Wang, A Course in Fuzzy Systems and Control, Prentice-Hall International, New Jersey (1997).
6. W. L. Xu, S. K. Tso and Y. H. Fung, Fuzzy reactive control of a mobile robot incorporating a real/
virtual target switching strategy, Robotics and Automation Systems, 23 (1998), pp. 171±186.
7. Velleman Components NV, Velleman Parking Radar Manual (http://www.velleman-kit.com/).
8. Motorola, M68HC11E Family Data Sheet (http://www.motorola.com/semiconductors/).
M. D. Hurley received a B.E. in Mechatronics with honours from Massey University, New
Zealand, in May 2004. He now serves as an automation engineer in the New Zealand army.
186 M. D. Hurley et al.
Glen Bright graduated from the University of Natal, South Africa, with a Ph.D. in
Mechatronics and Robotics in 1993. He currently leads the Mechatronics and Robotics
Research Group (MR2G), which has active members in New Zealand, South Africa and
North America. He is an active member of IEEE, ISPE, IASTED and a newly appointed
CIRP member. His research interests include Internet Manufacturing, reconfigurable
machines for Agile Manufacturing, Automated Guided Vehicle technologies and wireless
communication for materials handling and automated machines.