• Tidak ada hasil yang ditemukan

Creating a library

Dalam dokumen Designing Autonomous Mobile Robots (Halaman 58-66)

A good library of mathematical and geometric functions is an essential part of the foundation of a robot control program. This library should be written with speed and reentrance in mind. This means that library functions must keep all variables locally, and units of measure and conventions must be consistent between functions.

Decide the type of variables you will use, and be consistent. If angles are assumed to be positive for the clockwise direction in one routine, then all routines must assume this. If the system is to accept both English and metric units of measure, decide which system it will actually use, and make the conversions to the other system at the operator interface.

If you are programming in a high-level language, then the choices will range from integers and long integers, to singles and doubles. Remember that nothing is free. If you decide to use singles instead of integers, you may slow the calculations dramati- cally. On the other hand, if your target system has a math coprocessor, this might not be the case.

Mathematical functions

Generally you will need only the basic mathematic functions for the techniques described later. These include signed addition, subtraction, multiplication, division, squares, and square roots. Note that whatever variable types you choose, interim calculations may overrun the type limits. Routines must therefore be able to handle this internally without returning errors.

Geometric and vector functions

In the coming chapters we will discuss various processes in terms of adding vectors to each other and of converting vectors to Cartesian coordinates and vice-versa. For clarity and brevity, the mathematical details of how this is done will not be included in the discussions. The library must support this capability. For many indoor applica- tions, a two-dimensional frame of reference is completely adequate, but for outdoor systems the library must include the Z axis in all vector functions. At a minimum, the library must allow support for the following very basic functions:

Add one or more vectors together and return a single vector.

Add one or more vectors to a coordinate and return the resulting coordinate.

Calculate the vector between two coordinates.

Calculate the included angle between vectors with the same base.

Calculate the angular sum between vectors with the same base.

If integers are to be used, then all functions should take rounding into consideration to achieve maximum accuracy. If this is not done, a phenomenon known as digital integration can occur, which can cause significant error in long-term results.

Once a basic library is produced, it can be supplemented as required. Before I code anything in a routine, I find it useful to include a paragraph or so of comments defining what the routine is to do, what variables it uses, what units they are in, and anything else that a user might need to know about it. These comments are useful both in structuring the routine and in later maintaining and using it.

Create a configuration definition

If you want your program to be maintainable, then do not code constants into any part of it! This means that parameters such as distance between the wheels of the robot, or the position and orientation of each sensor should be variables that are preset in operation. When something in the configuration changes, it will be a simple matter to correct all the calculations that use this measurement by simply changing the single variable.

Never code anything specific to a configuration into your library.

Test your library at every boundary

One of the best places for software bugs to hide is near boundaries. Test all your routines with maximum and minimum input values in all combinations of signs and magnitudes. It is even useful to write a simple automatic test program which incre- ments the inputs linearly and graphs the output of each function. Mathematical glitches in these functions will be very difficult to diagnose once you start your system running.

Thinking More Clearly Through Fuzzy Logic

4

C H A P T E R

Of all the software concepts touched upon in this book, fuzzy logic is one of the most valuable and most widely misunderstood. These misunderstandings are at least in part a consequence of the fact that the term fuzzy is not transparently descriptive. The basic concept of fuzzy logic is so simple that it amounts to little more than common sense. If you understand the mathematics (geometry) behind a straight line, you can master fuzzy logic.

The purpose of fuzzy logic is to provide the best possible “guess” at the value of something that cannot be measured directly, but which can be inferred from a combination of inputs.

For this reason, a system that accomplishes this is sometimes referred to as an “inference engine.”

To understand the place of fuzzy logic, we will first consider Boolean logic as it has been used in sensor based systems. In Boolean logic, all inputs, outputs, and calcula- tions deal with two states; ‘true’ and ‘false’. These states may be expressed as ‘1’ and

‘0’, or –1 (FFFFh) and ‘0’, but they always represent the two Boolean states. If inputs are not Boolean to begin with, then they are compared to a threshold to convert them to Boolean states before being manipulated by the Boolean logic.

A simple example of this process can be seen in a “dual technology” motion detector used in the security industry. In the 1970’s and early 1980’s, several motion detector technologies vied for market supremacy. These included light-beam, ultrasonic, pas- sive infrared (PIR), and microwave based systems. The benchmark for effectiveness was measured by how sensitive the system could be set without experiencing an unacceptable rate of false alarms. The PIR and microwave technologies eventually emerged as dominant, but both could be false alarmed under some circumstances.

A microwave motion detector emits a UHF radio signal which reflects off objects in the environment and is then received back at the detector where it is mixed with the outgoing signal. If nothing moves in the environment, the two frequencies are the same and no sum or difference “beat” signal is produced. However, if some of the returning signal was reflected from a moving object, then a frequency (beat note) is produced that is proportional in frequency to the velocity of the target. This signal is then amplified, rectified, and compared to a threshold. If it is greater in amplitude than an adjustable threshold, then a relay closes to signal an alarm.

A PIR alarm, on the other hand, is purely passive. Black body radiation (including body heat) in the 8 to 12 micron wave lengths is passed through a Freznel lens that has multiple facets. The images from these lenses are focused onto two elements of a detector that are connected in opposite polarity. As the heat source moves, the dan- cing images on the detector cause a pulsating signal. This signal is then amplified, rectified, and compared to a threshold just as is done in microwave systems.

Microwave systems can be falsely triggered by stray signals from other systems, and such systems could actually detect large moving vehicles outside of the building that did not represent true intruders. Passive infrared sensors can be falsely triggered by heat from forced air heaters, as well as by blowing curtains and certain other signal sources. Initially, manufacturers attempted to reduce false alarms with signal-process- ing techniques such as time delays. Despite these marginal improvements, the market wanted a system whose false alarms were much less frequent than either system could achieve.

As the market grew, eventually the cost of these systems became low enough that it was possible to incorporate both technologies into a single alarm without the price becoming prohibitive. The result is shown in Figure 4.1. Note that the symbol shown as an “Amp” is both an amplifier and detector, which puts out a fluctuating DC signal proportional to the disturbance magnitude.

Since the disturbances that cause PIR technology to false alarm are not related to those that cause microwave technology to false alarm, the false alarm rate of a com- bined system is much lower than for either technology alone. The Boolean equation for an alarm is stated as:

Alarm = (MW > MW Threshold) AND (PIR > PIR Threshold) Or:

Alarm = (MW > MW Threshold) · (PIR > PIR Threshold)

Notice that the comparators convert the analog signals to binary (on/off) signals. In- terestingly, most early systems actually contained two relays. The Boolean AND function was implemented by wiring the contacts of these relays in series. Why this was not done with simple logic is puzzling, as the extra relay is much more expensive than a couple of transistors or even diodes. Even so, the dual technology alarm was an immediate success and has remained a mainstay of the industry.

Figure 4.1. A dual technology motion detector

M W Amp.

Comp.

+V Coil

+V

Amp.

Comp.

+V Coil

+V

Microwave Horn

Differential PIR Detector

MW Threshold

PIR Threshold

+ - - +

The dual technology detector is a classical example of Boolean logic in a signal pro- cessing application, but it had a logical weakness. To understand this weakness, let’s arbitrarily call a signal 100% when it is precisely at the threshold level. Now con- sider a case when the microwave signal is 99% and the PIR signal is 200%. This would not generate an alarm, yet it would be more likely to represent a true detec- tion than the case when an alarm would be generated by both signals being 101%.

There is a better way to combine these signals.

M W Amp.

Amp.

Comp.

+V Coil

+V

Microwave Horn

Differential PIR Detector

Alarm Threshold

+ - - +

+ +

Amp.

MW Gain

PIR Gain

The circuit of Figure 4.2 demonstrates how we might sum the two sensor signals of the motion detector before comparing the result to a threshold. Instead of convert- ing the signals to Boolean values before ANDing them, we sum their analog levels and then compare the result to a threshold. Two gain potentiometers have been pro- vided so that we can balance the two signals to appropriately represent their relative sensitivities.

If the final threshold is set higher than either sensor signal can achieve by itself, then both signals must be present to at least some degree to cause an alarm. The advantage to this approach is that a much wider range of valid alarm input combina- tions is possible. What we have done is to create an electrical equivalent to a fuzzy logic solution.

Figure 4.2. Dual technology motion detector using fuzzy logic equivalence

Now if we were to read the voltage out of the summing amplifier with an A/D (ana- log to digital) converter instead of feeding it to the hardware comparator, then we could consider this signal to be proportional to the intrusion threat. We could compare this threat level to software thresholds and perform all alarm processing in software.

Better yet, if we moved back to reading the outputs of the two signal amplifiers, and perform the gain balancing, summing, and thresholding in software, then we have created a true fuzzy logic intrusion detector. What could be simpler?

Dalam dokumen Designing Autonomous Mobile Robots (Halaman 58-66)