**Fuzzy Logic Control in Autonomous Robotics:**

**Investigating the Motorola MC68HC12 on a Line
Following Robot**

David Olsen

Department of Electrical
and Computer Engineering

Faculty Advisor:
Dr. Marian S. Stachowicz

**Abstract**

Autonomous robot systems
require complex control systems. Fuzzy Logic,
a mathematical system developed by Professor Lotfi Zadeh, helps to reduce the complexity of modeling nonlinear
problems. In the 1990s, Motorola
developed the MC68HC12 microcontroller with native Fuzzy Logic
instructions. This research determined
the effectiveness of the MC68HC12’s Fuzzy Logic instructions for robotic
control. This research involved
designing a robotic platform using the MC68HC12 and testing binary logic
control systems against Fuzzy Logic control systems. The research analyzed the two systems using
four criteria: (1) the size of memory
required to develop the control system, (2) the ease of writing the control
software, (3) how well the control system managed the functions of the robot,
and (4) the overall processing power of the system. The results showed that Fuzzy Logic uses less
memory than binary logic and is much easier to design, although more difficult
to program initially. Fuzzy Logic can
control more functions of the robot and has greater processing
capabilities. The power, ease of use,
and small size of Fuzzy Logic instructions make Fuzzy Logic a practical
solution to autonomous robotic control systems.

**Keywords: Fuzzy Logic, robotics,
control systems, HC12**

**1. Introduction**

The expansion of robotics and microcontrollers into
the facets of everyday life increases the need to develop efficient control
systems. A non-traditional approach to
control system design is the use of Fuzzy Logic.

Fuzzy Logic
extends from the traditional crisp boundaries of Aristotelian logic (true or
false) to include the concept of partial truth – having truth-values between
‘completely true’ and ‘completely false.’
Dr. Lotfi Zadeh of ^{ oC} to
100^{o}C. A linguistic value,
COLD may be used to represent temperatures from 0^{o}C
to 10^{ oC}
while other linguistic values represent other ranges. Fuzzy Logic makes it possible to solve
complex, ill-defined problems where there is a large degree of expert knowledge
or the solution is easy to describe linguistically [3].

Applications
of Fuzzy Logic are appearing in many industries. Fuzzy Logic enables designers to model
complex systems more quickly and effectively than traditional approaches. Consumer appliances, automobile engines,
transmissions, and industrial systems are all using Fuzzy Logic techniques.

Motorola’s
HC68HC12 (HC12) microcontroller incorporates several Fuzzy Logic primitives
directly in its instruction set. The
instruction set contains the Fuzzy Logic operations of trapezoidal membership,
rule evaluation, and weighted average defuzzification. The microcontroller also includes other
instructions that are helpful in Fuzzy Logic applications such as MIN / MAX
instructions and table lookups [4].
Motorola’s HC12 allows the development of low-level applications that
can utilize the unique features of Fuzzy Logic.

The
goal of this project is to design and build an autonomous line following robot
based on Fuzzy Logic techniques. The
robot uses the Motorola HC68HC12 microcontroller. This project involves investigating the HC12’s
Fuzzy Logic instruction set and analyzing its ability to control an autonomous
robot. The robot in this project serves
as a test bed for several pieces of software.
These software programs include initial test scripts, several
control system based on traditional logic, and several control systems based on
fuzzy systems. The best classical logic
and fuzzy control systems are compared in various
tests. These tests involved the robot
following a black line on the floor.

**2.
Fuzzy Logic on the HC12**

As
stated in the introduction, Motorola designed the HC12 with advanced
capabilities to handle Fuzzy Logic calculations. The HC12 contains four instructions that are
specific to Fuzzy Logic. These
instructions are:

- MEM Evaluates the
trapezoidal membership functions
- REV/REVW Performs unweighted/weighted
MIN-MAX rule evaluation
- WAV Performs weighted
average defuzzification

The following section describes the basics
of Fuzzy Logic on the Motorola HC12.
This section will only serve as an introduction to fuzzy programming on
the HC12 and is not a replacement for the manufacture’s documentation. This section assumes that the reader has some
knowledge of Fuzzy Logic.

**2.1**** fuzzy logic basics**

The
design of a Fuzzy Logic interface for the HC12 consists of two parts. First, the user must design a knowledge base
that contains the membership functions and the rule set. The second part is the inference kernel that
takes the system inputs and produces the system outputs based on the knowledge
base [5]. Figure 1 shows the basic
structure of the Fuzzy Logic system.

Figure 1:
Block diagram of a fuzzy logic system [5].

**2.2****
fuzzification strategy (MEM)**

Fuzzification
is the process by which system inputs are evaluated to
determine the degree at which they belong to a particular fuzzy set, on a scale
from 00 to FF in hexadecimal [5]. The
MEM instruction evaluates trapezoidal membership functions. These functions define the fuzzy set, the
foundation of Fuzzy Logic. A fuzzy set
is a set without a crisp, clearly defined boundary. It can contain elements with only a partial
degree of membership. For example, the
membership function for COLD could equal FF for temperatures below 7^{o}C
and slope down to 00 toward 10^{o}C.
The MEM instruction compares the system input against the membership function
to determine the degree of truth of a fuzzy input.

The
y-axis in Figure 2 represents the degree of truth from completely
false (00) to completely true (FF).
The x-axis represents the range of input values for the particular
system input. The MEM function works by
finding the y-value, given a system input (x-value) and the membership
function. MEM returns the percentage of
truth (y-axis value) for the particular fuzzy set. The result is a set of fuzzy values that
describe characteristics of input variables in the system.

Figure 2: Trapezoidal membership
function. The x-axis represents the
input range and the y-axis represents the degree of truth [5].

To
define a trapezoidal membership function for the HC12, you need four values:
(1) the start of the trapezoid, (2) the first slope, (3) second slope, and (4)
the end point of the trapezoid. Figure 2 labels these points and shows the
memory representation. The user should
define the trapezoidal in memory as follows:

LABEL_MF DC.B $40,$D0,$08,$04

The
program should use a descriptive label because the programmer will need this
label during fuzzification. For example,
COLD_MF could represent a membership function of the fuzzy set COLD where the
postfix _MF reminds the programmer that this variable represents a membership
function. A trapezoidal definition is needed for each membership function.

**2.3**** rule
definition and evaluation (REV/REVW)**

Rule evaluation is how Fuzzy Logic performs
calculations. The fuzzy values produced
by the MEM function are passed through the rule list
to find the fuzzy output. The two types
of rules that the HC12 allows are weighted (REVW), where each rule can have a
different weights; and un-weighted, (REV) were all rules have equal weight. An example of a rule list:

If
temperature is COLD and wind is HIGH, then heat is on HIGH.

If
temperature is WARM and wind is LOW, then heat is on LOW.

If
temperature is HOT and wind is LOW, then heat is OFF.

After
the fuzzy inputs are evaluated with REV/REVW, the
system’s fuzzy outputs indicate the degree to which an output should have a
specific value. These outputs must then
undergo defuzzification before their values are useful. Creating the rule list is actually very
simple. The antecedents (left side of the
rule) are the fuzzy inputs created by the MEM instruction (e.g., a temperature
reading evaluated with the COLD, WARM and HOT membership functions). The consequents (right side of the rule) are
the fuzzy outputs of the system. During
REV, each antecedent is joined using the fuzzy *and *operator (MIN). This
minimum value is compared to the current fuzzy output
of each consequent using the fuzzy *or*
operator (MAX), and the maximum of these two values is stored in each
consequent (fuzzy output). In other words,
the overall “truth” of a rule is stored in the fuzzy outputs and if a
subsequent rule is “truer,” then the fuzzy outputs are
updated to reflect this new value.

The
rule list is stored in memory as a list of pointers to fuzzy inputs
(antecedents), a reserved separator value, a list of pointers to fuzzy outputs
(consequents), and then another separator.
Each rule follows this pattern and the rule list is
terminated by an end rule reserved value.

RULE_LIST DC.B P_TisCold, P_WisHigh, SEPARATOR, P_HeatHigh, SEPARATOR

DC.B P_TisWarm, P_WisLow, SEPARATOR, P_HeatLow,
SEPARATOR

**2.4****
defuzzification strategy (WAV)**

The
final step in the Fuzzy Logic calculation is defuzzification, when the raw
fuzzy outputs are evaluated to create a composite
system output. Unlike the input, the
fuzzy output membership function is not trapezoidal but a singleton. This singleton indicates one system output
value for each fuzzy output. The output
membership singletons are arraigned in memory in the
same order as their corresponding fuzzy outputs. WAV calculates a sum of products of each
fuzzy output value times its singleton value and a sum of all of the fuzzy
output values. The first sum is divided by the second using EDIV to produce an overall
value that is the defuzzified output of the
system. Defuzzification creates a
weighted average system output based on the truth of the fuzzy outputs.

**2.5 fuzzy inference kernel**

The
inference kernel contains all of the instructions that make up the fuzzy
system. The kernel utilizes the
knowledge base to create a system output from given system inputs [5]. All of the fuzzy instructions require proper
initialization of the accumulators, index registers, and fuzzy values. These details are beyond the scope of this
paper and can be found in the manufacture’s
documentation. The sample program
listing, Appendix A, also includes comments on developing the fuzzy kernel.

Readers
wanting more information are encouraged to refer to the “Fuzzy Logic Support”
chapter in the *Motorola 68HC12 CPU12
Reference Manual* for a detailed explanation of the Fuzzy Logic instruction
set.

**3. Control Software**

To
test if Fuzzy Logic is a superior solution to the problems of autonomous robotic
control, this project involved creating two control systems. The first system uses traditional logic to
control the system. This system does not
use any of the microcontrollers embedded Fuzzy Logic functions. The second robot relies solely on the HC12’s
Fuzzy Logic instructions. This second
system uses Fuzzy Logic for all of its control processing. Both control systems only used the HC12’s
onboard RAM area (512 bytes) for program storage.

**3.1**** ****traditional control system**

The
traditional control system does not use an of the Fuzzy Logic instructions on
the HC12. It relies on testing each
input as true or false and then uses an if-else programming structure to
determine the correct system output. Because
of the limited size of RAM available for programming, the binary robot relies
on an external chip to convert the analog signals from the sensors to a binary,
on or off, form. The control structure is simplified to reduce the necessary amount of memory
needed for storage. The traditional control
system uses the following rules:

- Center sensor on Go
Straight
- Left sensor on Turn Right
- Right sensor on Turn
Left
- No sensors on Stop

The motor control of this system is limited
to one speed for straight, left, and right.
Because the system relies on an external chip to convert the analog
sensor values, it is unable to detect when the sensor is partially on the
line. While this simplifies programming,
it reduces the effective resolution of the sensors.

**3.2**** fuzzy control system**

The
small amount of RAM also limits the fuzzy control system. However, the fuzzy instructions are able to
handle far more control system computations than the traditional system in a
similar amount of memory. First, the
fuzzy system uses the analog values from the sensors. This gives the fuzzy robot the benefit of
being able to tell exactly when the sensor is leaving the line and to be more
robust to changes in the environment.
Second, this system has three levels of speed for each direction. This gives the fuzzy control system improved
turning ability.

The
fuzzy control system takes the analog value from the sensors and assigns a
level of “truth” to a fuzzy value. This
control system used two fuzzy membership functions for each sensor: ON and OFF,
to create a total of six fuzzy input values.

Fuzzy
control systems are based on “rules.” The control system uses these rules to
evaluate the fuzzy values and create a fuzzy output. Table 1 shows the rules based upon the truth
of the fuzzy input values and the corresponding fuzzy outputs used in this
control system. In the table, there are ON
and OFF fuzzy values for each of the three sensors. The system combines the “truth” of the three
fuzzy inputs for each rule using the fuzzy MIN operator. This minimum value is stored in the fuzzy
outputs unless the outputs already have a greater value stored from a previous
rule. The system finds the output that
best fit the fuzzy inputs.

Table 1. Fuzzy Rule List. This
table shows the rule list that evaluates the output speed and direction given the
fuzzy input variables. This rule list is evaluated using the REV instruction on the MC68HC12.

Fuzzy Inputs |
Fuzzy Outputs |
|||

Left |
Center |
Right |
Speed |
Direction |

Off |
Off |
Off |
Stop |
Line Finder |

On |
Off |
Off |
Slow |
Hard Left |

On |
On |
Off |
Medium |
Left |

On |
On |
On |
Slow |
Line Finder |

Off |
On |
Off |
Fast |
Straight |

Off |
On |
On |
Medium |
Right |

Off |
Off |
On |
Slow |
Hard Right |

On |
Off |
On |
Stop |
Line Finder |

**4. Hardware Implementation**

This project uses a
custom robot designed to test the various control systems. This robot has a line sensor and two
motors. The robot can also handle IR
distance sensors, but the distance sensors are not uses in the testing portion
of this project.

**4.1**** base and motors**

The
test robot consists of a plywood base and uses differential steering. The motors are two servos, modified to allow
continuous revolution. Servos are useful
for small robots because the servo has an integrated motor controller and it
provides adequate torque for a small robot [6]. Ball casters support the front and back of the
robot. This allows the robot to turn on
its center. A rechargeable battery pack
powers the motors, line sensor, and the microcontroller.

**4.2**** line sensor**

The
custom line sensor is a collection of three IR phototransistor, transmitter
pairs that sense when the robot in centered on the line, to the left, and to
the right. These sensors feed analog
data into the HC12’s A/D ports for processing.
The analog values represent the IR reflectivity of the surface below the
line sensor at three points. Because the
sensor contains IR transmitters, sensor works without ambient lighting.

**5. Testing**

The two robot control systems were
tested on a variety of tracks.
These tracks were created by placing black
electrical tape on a wooden, carpeted, and cement floor. The tracks included a straight line, a
45-degree angle turn, a 90-degree angle turn, and a 12-inch radius oval shaped
ring. The lighting level was kept constant throughout the trials. The testing measurements include a
qualitative description of the robots ability to stay on the line without
“wiggle” or overcorrection.

**6. Results**

For each of the test tracks, both robots maintained
control and followed the line. The
traditional control system did have more “wiggle” as it moved down the straight
section. This robot was unable to center
itself on the line and instead zigzagged down the line. The fuzzy robot system was able to quickly
find and hold the true course of the line.
The fuzzy robot system also made fewer corrections during the turns in
the oval test track. The traditional
robot had to correct its course many more times during the oval turns. The fuzzy robot system had a much smoother
course throughout the track. Neither
robot was unable to make any of the turns in the test.

In addition to comparing at the performance
of the two different control systems, this project also looks at the memory
requirements, the ease of writing the control software, and the processing
power of the system. For the two systems
tested, the Fuzzy Logic system used more memory. However, it also processed significantly more
information and gave results with finer resolution. Achieving this same level of processing using
the traditional system would have made the traditional system’s memory
requirements much larger.

The fuzzy system is initially more difficult
to program because the syntax of the instructions and the format of the
knowledge base in memory is unclear.
However, it is much easier to modify the fuzzy control system to changes
in the environment or system outputs because only the membership functions need
to change. For the traditional system,
the entire logic of the control system may need to be
modified. It is more intuitive to
program in Fuzzy Logic because of the ease of creating a linguistic solution to
the control problem. This intuitive
nature makes Fuzzy Logic easier to program than traditional logic for complex
control problems.

**7. Conclusion**

Fuzzy
Logic represents a tremendous advancement in autonomous robotic control
systems. The Motorola MC68HC12’s
instruction set will make designing fuzzy solutions on microcontrollers much
easier than in the past. Fuzzy Logic is
well suited for complex control problems like autonomous control, but it may be
too difficult for simple control systems.
The HC12’s fuzzy instructions simplify the design cycle for control
system designers and provide an alternative to traditional control
methodologies. The fuzzy system developed by Motorola is a very powerful set of
instructions that will have a dramatic impact on control systems in many
industries.

**8.
References**

[1] Reuss, Robert F, et al.
“Fuzzy Logic Control in a Line Following Robot.” http://www.cs.unr.edu/~simon/fuzzy/fuzzylogic.htm
[

[2] Kandel, Abraham, et al.
*Fuzzy Control
Systems*.

[3] Stachowicz, M.S. and Beall, L.,
"Fuzzy Logic Package for Mathematica®",
Wolfram Research, Inc., 2000.

[4] M. S. Stachowicz and C. Carroll, "Intelligent Systems on
Motorola's Microcontroller: A Team
Design Workshop," Proceedings of the 2000 International Conference on
Engineering Education,

[5] Motorola
Semiconductor, *CPU12 Reference Manual*. Motorola Inc., 1999.

[6] McComb, Gordon. *The Robot Builder’s Bonanza*.

**Appendix A. Example Fuzzy Assembly Listing**

The follow code example lists the overall structure
of an assembly program for the HC12 that utilizes Fuzzy Logic. This simplified example takes two system
inputs, temperature and wind speed; and produces one system output, heat. Input variables, stored in $0800 and $0801, have been scaled from 00_{16} to ff_{16}. The range of the heat output is from 00_{16}
to ff_{16}. Refer to section 2
for details on the Fuzzy Logic operators.

ORG $0900 ;Memory location
for Fuzzy Knowledge Base

;### Fuzzy Constants ###

MARKER EQU $FE ;Used to
separate rule sections

ENDRO EQU $FF ;Used to
finish the rule list

;###
Relative Pointers ###

P_COLD EQU $00 ;Pointers
to the relative locations of the fuzzy

P_WARM EQU $01 ;Input
and Output variables

P_HOT EQU $02

P_CALM EQU $03

P_WINDY EQU $04

P_NoHt EQU $05

P_LowHt EQU $06

P_MedHt EQU $07

P_HiHt EQU $08

;### Membership Functions ###

COLD_MF DC $00,$76,$00,$05 ;Temperature
Membership functions

WARM_MF DC $56,$AC,$07,$07 ;Point 1,
Point 2, Slope 1, Slope 2

HOT_MF DC $8C,$FF,$07,$00

CALM_MF DC $00,$60,$00,$07 ;Wind
Membership functions

WINDY_MF
DC $50,$FF,$07,$00

;### Output Singletons ###

No_Heat_FS DC $00 ;
The Fuzzy system will take the weighted average

Low_Heat_FS DC $56 ;
of these values based on the truths of the

Med_Heat_FS DC $AC ;
corresponding Fuzzy output variables

High_Heat_FS DC $FF

;###
Fuzzy Variables ###

COLD_FI DS $01 ;These instructions reserve bytes for the Fuzzy

WARM_FI DS $01 ; input and output
variables.

HOT_FI DS $01 ;(NOTE: This
variable ordering is what determines the

CALM_FI DS $01 ; relative locations stored above and can
be the

WINDY_FI DS $01 ; same ordering
as the membership functions.)

NoHeat_FO DS $01

LowHeat_FO DS $01

MedHeat_FO DS $01

HHeat_FO DS $01

;###
RULE Definitions ###

RULE_START

DC P_COLD,P_WINDY,MARKER,P_HiHt,MARKER ;This section defines the rule list

DC P_COLD,P_CALM,MARKER,P_MedHt,MARKER ;The rules are defined as relative

DC P_WARM,P_WINDY,MARKER,P_MedHt,MARKER ;pointers to the locations of

DC P_WARM,P_CALM,MARKER,P_LowHt,MARKER ;Fuzzy input and output variables.

DC P_HOT,P_WINDY,MARKER,P_LowHt,MARKER ;MARKER and ENDRO are constants

DC P_HOT,P_CALM,MARKER,P_NoHt,ENDRO ;$FE, $FF respectively

org $0803 ;
Memory starting location for Inference Kernel

;###
Fuzzification for temperature data ###

LDX #COLD_MF ; First of three membership function for
temperature

LDY #COLD_FI ; Location of first fuzzy input variable for
temperature

LDAA $0800 ; Location of temperature data in
system

LDAB #$03 ; Number of membership functions for
temperature

TEMPFUZZ: MEM ; This loop will process each of the three
membership functions

DBNE B,TEMPFUZZ ; and create
the three fuzzy input variables

;###
Fuzzification for wind data ###

LDX #CALM_MF ; First of two membership function for wind

LDY #CALM_FI ; Location of fuzzy wind (These last two
lines are optional because

; Accumulator X and Y
already have the correct values)

LDAA $0801 ; Location of wind data in system

LDAB #$02 ; Number of membership functions for wind

WINDFUZZ: MEM ; This loop will process each of the two
membership functions

DBNE B,WINDFUZZ ; and create
the two fuzzy input variables

;###
RULE EVALUATION ###

RULE: LDAB #$04 ; First you must clear out the fuzzy
output variables

CLEAROUT: CLR 1,Y+

DBNE B,CLEAROUT

LDY #COLD_FI ; Point at start of Fuzzy Input variables

LDX #RULE_START ; Point at start of rule list

LDAA #$FF ; Must load AA with $FF

REV

;###
DEFUZZIFICATION ###

LDX #No_Heat_FS ;
Point to singleton positions

LDY #NoHeat_FO ; Point to
Fuzzy Output locations

LDAB #$04 ; Number of Fuzzy Outputs

WAV ;
Calculate sums for weighted average

EDIV ;
Final divide step

TFR Y,D ; Move the
results to A:B

STAB $0802 ; Store system outputs