Datasheet

Light-Sensitive Navigation with Phototransistors • Chapter 6
Robotics with the BOE Shield-Bot 197
Activity 3: Light Measurements for Roaming
We now have circuits that can work under a variety of lighting conditions. Now we need
some code that can adapt as well. An example of sketch code that cannot
adapt to change
would be:
if(tLeft > 2500)… // Not good for navigation.
Maybe that statement would work well for turning away from shadows in one room, but
take it to another with brighter lights, and it might never detect a shadow. Or, take it to a
darker room, and it might think it’s seeing shadows all the time. For navigation, what
matters is not an actual number reporting the light level over each sensor. What matters is
the difference in how much light the two sensors detect, so the robot can turn toward the
sensor seeing brighter light (or away from it, depending on what you want.)
The solution is simple. Just divide the right sensor measurement into the sum of both. Your
result will always be in the 0 to 1 range. This technique is an example of a normalized
differential measurement Here’s what it looks like as an equation:
tLefttRight
tRight
shadealdifferentinormalized
+
=
For example, a normalized differential measurement of 0.25 would mean “the light is 1/2 as
bright over the right sensor as it is over the left.” The actual values for
tRight and tLeft
might be small in a bright room or large in a dark room, but the answer will still be 0.25 if
the light is 1/2 as bright over the right sensor. A measurement of 0.5 would mean that
the
tRight and tLeft values are equal. They could both be large, or both be small, but if the
result is 0.5, it means the sensors are detecting the same level of brightness.
Here’s another trick: subtract 0.5 from the normalized differential shade measurement. That
way, the results range from 0.5 to +0.5 instead of 0 to 1, and a measurement of 0 means
equal brightness. The result is a zero-justified normalized differential shade measurement.
5.0
+
=
tLefttRight
tRight
shadealdifferentinormalizedjustifiedzero
But why do it? The value range 0.5 to +0.5 is great for navigation sketches because the
positive and negative values can be used to scale the wheels speeds. Here is how the zero-
justified normalized differential shade equation appears in the next sketch:
float ndShade; // Normalized differential shade
ndShade = tRight / (tLeft + tRight) - 0.5; // Calculate & subtract 0.5