Datasheet

Tactile Navigation with Whiskers • Chapter 5
Robotics with the BOE Shield-Bot 165
How Escaping Corners Works
This sketch is a modified version of RoamingWithWhiskers, so we’ll just look at the new code
for detecting and escaping corners.
First, three global byte variables are added:
wLeftOld, wRightOld, and counter.
The
wLeftOld and wRightOld variables store the whisker states from a previous whisker
contact so that they can be compared with the states of the current contact. Then
counter is
used to track the number of consecutive, alternate contacts.
byte wLeftOld; // Previous loop whisker values
byte wRightOld;
byte counter; // For counting alternate corners
These variables are initialized in the setup function. The counter variable can start with
zero, but one of the “old” variables has to be set to 1. Since the routine for detecting corners
always looks for an alternating pattern, and compares it to the previous alternating pattern,
there has to be an initial alternate pattern to start with. So,
wLeftOld and wRightOld are
assigned initial values in the
setup function before the loop function starts checking and
modifying their values.
wLeftOld = 0; // Initialize previous whisker
wRightOld = 1; // states
counter = 0; // Initialize counter to 0
The first thing the code below // Corner Escape has to do is check if one or the other
whisker is pressed. A simple way to do this is to use the not-equal operator (!= ) in an
if
statement. In English,
if(wLeft != wRight) means “if the wLeft variable is not equal to
the
wRight variable…”
// Corner Escape
if(wLeft != wRight) // One whisker pressed?
If they are not equal it means one whisker is pressed, and the sketch has to check whether
it’s the opposite pattern as the previous whisker contact. To do that, a nested
if statement
checks if the current
wLeft value is different from the previous one and if the
current
wRight value is different from the previous one. That’s if((wLeft != wLeftOld)
&& (wRight != wRightOld))
. If both conditions are true, it’s time to add 1 to the counter
variable that tracks alternate whisker contacts. It’s also time to remember the current
whisker pattern by setting
wLeftOld equal to the current wLeft and wRightOld equal to the
current
wRight.