Building a fuzzy-logic controller doesn't require an AI degree-just common sense
My last column (see
reference) outlined the
motivation for developing a "smart" IIR implementation, a mechanism where an
instrument, even in the presence of noise, presents quiet results and short response
times. This month I'll detail my foray into fuzzy logic by explaining where to use it and
how to develop a dynamic IIR filter that adapts to input data, thus keeping the instrument
always within published specifications.
Back to school
The first problem encountered while learning this new technology was finding
comprehensible and informative books or articles on the subject. National news magazines
published articles about the Japanese manufacturing adaptive washing machines, vacuum
cleaners and jiggle-free camcorders. Although these puff pieces were enough to pique my
interest, they shed little light on how that technology works or, more importantly, how to
implement it. Technical libraries, on the other hand, have books on the topic, but
unfortunately most of them consist of in- comprehensible albeit spirited arguments among
mathematicians over issues that have little to do with practical hands-on development
practices. Recently some popular press books appeared touting the fuzzy-logic revolution,
but again they contained more fluff and hype than substance.
I typically find most technical information on developing with new technologies in
manufacturers' literature and technical magazines, and this case was no exception. Keep in
mind, though, that magazine articles usually don't reveal all the secrets needed to
complete a design, so you'll need to glean bits of the puzzle from many different sources.
For example, not only did I survey the electronic- and software-engineering periodicals, I
also checked out other fields such as medicine, agriculture and transportation and soon
compiled my own "textbook" on the subject.
Upon gathering this critical mass of knowledge, I had to pick a project to try it out
on. My firm's always working on several projects in parallel. At that time we had two
applications that emerged as candidates to serve as the testbed for this new technology.
One was the high-precision temperature monitor discussed in my last column (reference),
and the other was an instrument for measuring gas concentrations.
The common thread running between the two projects is that both required the same kind
of filtering. The gas-concentration instrument requires extreme sensitivity to detect
femtoamp currents while keeping the noise level better than one part in a thousand. In
addition, the response time in obtaining a reading measuring 90% of the final value had to
be < 20 sec. To make matters worse, as gas concentration grew, the sensor's already low
signal level dropped further. As with the temperature monitor, we tried various
conventional techniques but couldn't boost the S/N ratio more than 10%-definitely not
enough to launch a major new product. In the end, I implemented the gas analysis system
first because it was a more difficult problem and so would be a better test of the
Fuzzy to the rescue
In considering how to approach the design, I noticed many articles dealt with fuzzy
logic in applications using a PID (proportional-integral-derivative) loop. If my problem
was stated in PID terms then maybe I could use fuzzy logic to make the gas analyzer
marketable. Let me interject that I'm not an expert in this technology, but just a working
engineer trying to apply the tools and techniques at my disposal to solve a problem.
Regardless of whether the following experiences are typical of fuzzy-logic projects, they
speak highly of the intuitive and straightforward nature of this new analytical procedure
in that the technique worked and was easy to implement.
With any new skill the question of how to begin always exists. In my case, none of the
articles read gave step-by-step instructions, so I began by expressing the problem in
simplistic terms. The filter had to reduce noise to an acceptable level without ruining
the system's response time. From that simple statement came the two control inputs for the
fuzzy logic-the measurement's noise and slope. These two simple variables provided the
operating conditions of my smart IIR filter.
Next, I had to determine the rules. Again, the literature contained many
(contradictory) ways to derive and express rules. Seeking a simple straightforward
technique, I approached rule creation as if I were in a factory turning a dial to control
the process. For example, if the signal level wasn't changing and the signal was getting
noisier, you'd turn a dial that increased filtering. Likewise, if the signal was too quiet
because of too much filtering, turn the dial in the other direction. In both cases you'd
continue to adjust the dial to keep the signal noise in an acceptable region-to paraphrase
Goldilocks: not too noisy, not too quiet, but just right.
Likewise, the same technique applies to the time-response issue with the same dial. If
the signal begins to change while monitoring the noise, you can reduce the filtering to
allow more noise while speeding up the filter's response time. Once the readings level
off, increase the filtering again to keep noise in the acceptable region. The point is
that a change in signal level isn't noise, and once you separate these two components, a
smart IIR filter becomes possible.
To express the conceptual control dial as rules, first decide how many states each
input variable can have. Many fuzzy-logic implementations define five states for each
input variable, resulting in a maximum of 25 rules (52). Although ways apparently exist to
reduce this number, for this quick foray into the technique I decided to keep things
simple and limit the fuzzy engine to three states/variable for a total of nine rules.
Hence, the signal was either Too Quiet, Too Noisy or Just Right, and the measurement slope
was either Rising, Falling or Zero. With these definitions, I could now express my rules.
For example, if the signal is Just Right and the slope is Zero, don't change the IIR
filter. Alternately, if the signal is Too Noisy and the slope is Zero, apply more
filtering. These rules are just as if an operator were turning dials on a machine.
The beautiful thing about fuzzy logic is how you can apply everyday language to a
problem-a little noisy, a lot of dirt, rising fast. Everyone's seen the triangles and
trapezoids for expressing membership sets. The beauty of these charts is how measurements
naturally map into them. For example, I asked the client to identify an acceptable noise
level. In the past the client's answer might've been translated into a voltage or maybe
even A/D counts. In fuzzy logic, merely scale one of these charts using the client's
nomenclature. Don't worry about nonlinear relationships between A/D counts and measurement
results; fuzzy logic wants you to map its antecedents and consequents in familiar terms.
Mapping the criteria from everyday language to the 8-bit world was a simple matter of
scaling. The only problem was how to independently calculate the input signal's slope and
noise level without the two interfering with each other. I tried many techniques, but the
only one that worked consistently was to perform a least-squares fit on the previous set
of measurements. The fit gives a value for slope and a standard deviation that gives a
measure of the noise. Put these two numbers through the fuzzy engine and derive a crisp
value that sets the IIR a term. By continuously feeding the sensor output into the
least-squares algorithm, piping the results into the fuzzy engine and then changing the
IIR a value to the crisp output value, I built a dynamic filter.
The real test of this approach is the real world. With a sensor on the gas analyzer,
the 90% response time is now < 20 sec compared to 4 min when working with an IIR that
has a fixed a term. The noise on a stable signal is consistent at both ends of the dynamic
range. The whole project was astonishingly simple to implement.
Roll your own
As previously stated, for a number of reasons I wrote my own fuzzy-logic engine. The
hardest part of the project was learning how to derive non-coupled noise and slope values.
My original cut at the dynamic filter took roughly a day to write. The fuzzy engine itself
was only about 100 lines of C code. It was crude but proved its worth. The refined code is
now more "professional" (expandable, adaptable and fully commented) and runs
approximately 700 lines. My approach revealed the mysteries of implementing fuzzy
technology, thus making me better able to recognize future opportunities where it might
apply. For most low-speed applications normally encountered, fuzzy logic doesn't extract a
high price from the processor. The software isn't the problem; the issue is identifying
how and where to use fuzzy logic.
For anyone who's sweated trying to derive the proper constants, anti-windup terms and
other intricacies needed to tune a PID loop, fuzzy logic is a snap. The funny thing about
the fuzzy logic was that it was always trying to work, and all I was doing during my
tweaking was showing it how to do a better job. Never once did I feel, as I sometimes do
with PIDs, that I was precariously balancing the system, such that one slip in the
standard configuration would cause it all to come tumbling down. I felt in control and
that the fuzzy logic was mimicking how I would've turned the dials. PE&IN
Rosenthal, S, "The future of high precision is fuzzy,"
PE&IN, July 1993, pgs 64-65.