# Robotics/Real World Sensors

This section covers the topics on sensor imperfection and how these components behave in the real world.
There is no such thing as a "distance sensor". period. Those components commonly called "distance sensor" or similar names measure something and extract distance information out of that. This extraction works pretty good in particular circumstances, but are worthless in many others. The key to successfully measuring e.g. a distance, is knowing, exactly, what your sensors measures and how external factors influence the measurement. This doesn't mean you just need to know how accurate the sensor is, it means you need to know what part of physics is used, and what the laws of physics say about that.
I'll cover some of the most commonly used sensors and what laws of physics apply to those. I'm not going very deep into explaining physics, there are better sources for that (a wiki physics book for example), just enough to give you the idea of what problems you may expect and where to look for a solution.

## Light Based Sensors
### Reflection: short range sensors

This type of sensor detects objects at a range up to a few centimeter. These sensors are quite simple. They consist of a light source, usually an IR diode which signal can be modulated and a light detector, which can be as simple as a light sensitive diode or transistor with an amplification circuit or a more complex IC complete with filters and TTL level outputs.
These sensors work by detecting the reflection of the emitted light. The range at which an object is detected depends on a number of properties of the object:
* reflectivity/color: how well does the object reflect IR-light? Every object has a color. A green object means that it reflects light with wavelengths that we interpret as the color green. This can be a pretty large range. IR is also a color. Like any other color, some objects reflect IR, and other objects absorb IR.
* surface smoothness: A very smooth surface (like a mirror) reflects more light than a rough surface. (For example, a photograph of a black billiards ball usually shows a white spot caused by w:specular reflection).
* Angle: The more the surface is turned away from the sensor the more light is going to be reflected away from the sensor.
* Lightsources: Other lightsources like light bulbs or the sun emit IR light as well. Especially the sun can prevent an IR based sensor to operate.

### Reflection: medium range sensors

Medium range sensors are a bit more complicated than short range sensors. These sensors consist of an IR emitting diode which signal is modulated, the receiver has a lens to focus the reflected light onto a light sensitive strip. Moving the sensor back and forward towards an object moves the reflection beam along the light sensitive strip. The resistance of the strip depends on where the light hits the strip.
Its range has the same limiting factors as short range sensors.

### Reflection: Long range sensors

Long range sensors use the time a laser pulse takes to travel from the emitter to the object and back. There are several methods of measuring this time of flight, but most involve correlating the transmitted and received light pulse. By comparing the phase of these two pieces of data, a very accurate time value can be extracted. These sensors can operate over a wide range, typically between a few centimeters up to several kilometers.
Its range is also limited in the same way as the previous IR sensors. Another thing than can limit is haze, smoke and other particles on the air.

### Camera

Cameras used in robotics are commonly built around a image Sensor. These cameras are sensitive to IR-light and usually have a IR-filter in front of the lens. Cheap webcams may not contain such a filter, which makes them very sensitive to sunlight.

### Stereo vision

These sensors consist of (at least) 2 cameras mounted some fixed distance from each other.
This is rarely used because solving the correspondence problem is difficult.
See also Robotics: Sensors: Computer Vision.

## Sound Based Sensors
### What is sound?

Sound is in essence vibrations and pressure differences in the air. These vibrations are split into 3 groups by their frequency. The first group, called infrasone has a frequency below 20 Hz. The second group, called ultrasonic, has a frequency above 20 kHz and an upperbound of 2 MHz in air or 30 MHz in water. The last group is what is commonly called sound. This groups range lays between 20 Hz and 20 kHz and can be heard. Although only newborn babies can really hear all the way up to 20 kHz. The older you get the less frequencies you can hear.
Most sensors use ultrasonic sound, usually around 40kHz. Such a signal can't be heard, while it's still easy to use (generate, detect, ...).

### Doppler effect

If both the source and the receiver are motionless relative to each other, the receiver will hear the same frequency as the source emitted. However if one or both of them move relative to the other, the receiver will detect a different frequency. This change in frequency is called the Doppler Effect. Most people know this effect from the sirens of passing police cars or ambulances. When they pass you'll hear one sound as they approach and a somewhat different sound as they move away.
Calculating what frequency the receiver will hear is quite easy:
$f_{r}=f{\frac {c+v_{r}}{c-v_{s}}}$
With:
    $f_{r}$ = The frequency the receiver hears
    $f$ = the frequency the source emits
    $c$ = the speed of sound
    $v_{r}$ = the speed of the receiver
    $v_{s}$ = the speed of the source

### Speed of sound

The speed of sound depends on the medium it traverse through and its temperature. For air this is approximately 330m/s at 0°C. Of course most of the time the temperature is a bit higher than this. Calculating the actual speed is fairly easy:
$c=c_{0}{\sqrt {\frac {T}{T_{0}}}}$
with:
    $c$ = the actual speed at the current temperature.
    $c_{0}$ = the speed of sound at 0°C: 330m/s.
    $T$ = the current temperature in Kelvin.
    $T_{0}$ = 273,15 K (this is 0°C in Kelvin)

### Diffraction
## Ultrasonic Distance sensors

These sensors are pretty simple. In theory that is. In practice these sensors can be a real pain in the pinky. In this section I'll cover some of the troubles you may run into when trying to get them to work.
Ultrasonic distance sensors consist of 3 major parts: A transmitter, a receiver and a timer. To measure a distance the timer triggers the transmitter which emits a series of pulses, then the timer waits until the receiver detects the reflection of the pulses and stops the timer. The time measured is then divided by 2 and multiplied with the speed of sound. The result is the distance between the sensor and the object in front of it. The transmitter send out a stream of pulses on a carrier frequency. The maximum frequency humans can hear is about 20 kHz. A frequency higher than that is picked to avoid annoying humans with the constant beep -- 40 kHz is a common value.
The receiver triggers when it receives a signal with that particular frequency. This is not necessary the signal the transmitter sent. If more than one ultrasonic sensor with the same carrier frequency are used, they can detect each others signals.
Sound doesn't move in a straight line, but rather as a 3D expanding wave. When the wave reaches an object part of it bounces back and moves again as a 3D expanding wave in the opposite direction. Such a wave can easily bounce multiple times before disappearing. So it is very possible that you receive pulses that have travel a much larger trajectory than just to and back from the object in front of the sensor. While some part of this problem can be solved by letting the sensor wait some time before starting another measurement, other situation can produce incorrect measurements which are fairly tough to correct. For example moving through a doorway can fail because the sensors emitted pulses bounce from the walls back to the sensor and so giving a measurement that indicates an object in front of the sensor. One way of correcting this is using another sensor, for example a IR distance sensor to see if there really is an object. However such solution pose another problem: which sensor to believe? 3 sensors allow you to go with the majority, but then things become quite complicated in constructing and interfacing such systems, not to mention what it does to the power consumption.

### Distance Formula

The formula for calculating distance from a sonar pulse looks like:
${\text{Distance}}=343\,\mathrm {m/s} \times {\frac {\text{Elapsed Time}}{2}}$
343 m/s is the speed of sound, and we need to divide the time by 2 because the sound travels out and back.

### Availability & Range

Sonar sensors are widely available and relatively inexpensive, ranging from $15 to $40 depending on the desired range. On average the maximum range of a midlevel sonar sensor will be between 4 and 6 meters. Unlike infrared or laser sensors, sonar sensors also have a minimum sensing distance as well. This is due to the fact that the distance measurements are based on the speed of sound, and over very short ranges the sound travels out and back more quickly than the circuitry can respond. This minimum distance will vary by sensors, but is typically around 2 to 5 centimeters. Also unlike infrared sensors, sonar sensors don’t have a perfect “cone” of vision. Because sound propagates as a 3D pressure wave, the sensor actually has a range that resembles a sinc function wrapped around a curve.

### Potential Problems

Sonar sensors work very well for collision avoidance in large areas, but they do have some limitations. Because sound propagates out as a 3D pressure wave and echoes, your robot may see things that are not really in its way. For instance, near angled walls the sound waves may bounce several times before returning to the sonar receiver. This makes it difficult for the robot to know which echo is actually the correct distance to the nearest obstacle.
Similar to this is the problem of having multiple sonar sensors operating the in the same area. If the frequencies of nearby sonar sensors are too similar they may cause false readings since the sensors have no method besides frequency to distinguish pulses it sent out from those other sensors send out.
Another common problem is the difference in absorbency and reflection of different materials. If you shoot a sonar pulse at a cloth covered wall (i.e. cubicle), it is likely that the cloth will absorb a significant amount of the acoustic energy and that the robot will not see the wall at all. On the opposite end of the spectrum, a floor with very high acoustic reflection may register as an obstacle when it is really a clear plane.

## Magnetism Based Sensors
### Compass sensors

These sensors are used to measure the orientation of the robot relative to the magnetic north. It is important to remember that the magnetic north is not exactly the same as the geographical north. They differ several degrees.
The magnetic field of Earth is quite weak. This makes that these sensors will not operate well along other magnetic fields. E.g. speakers would mess up the reading. If you use these sensors it is best to mount them as far away from your motors as possible. While you can't shield them without making them useless, paying attentions to where you mount them can make a considerable difference in reliability.

## Other distance sensors

* (How do those "stud sensors" detect the lumber in the walls behind the sheetrock?)

## Further reading

* the sensor wiki http://sensorwiki.org/
* ultrasonics
* Robot Sensors
* "Migrating from x86 to PowerPC, Part 9: Sensors, sensors, sensors!" recommends using a optocoupler for isolation for all sensors.