disconnecting the keyboard from the tablet.
A Compass Example
The goal of our second project is for the robot to use
the tablet’s compass to first face a specified direction.
Once there, the compass readings should be used to
maintain the robot’s heading as it moves forward until a
nearby object is detected. The program in Figure 8
shows how easily this can be accomplished.
This program performs two distinct actions. First, the
robot must rotate to orient itself in the desired direction
before moving forward. Then, as it moves forward, it
should constantly turn slightly left or right based on a
comparison of its current heading to the compass
reading. Since our RB- 9 robot has numerous other
sensors, we will allow the robot to move forward until it
detects an object blocking its path. Let’s examine how
the program works.
The main portion of the program is easy to follow
because the function AngleDifference does all the hard
work. For that reason, let’s examine that function first. It
is passed through three parameters. The first is the
robot’s current orientation angle,
followed by the desired destination
angle. The third parameter is the
name of the variable where the actual
difference between the two angles is
stored.
Calculating the difference is more
complicated than just subtracting the
two angles — especially when the
angles are on opposite sides of due
north. If, for example, the robot is
facing 350° with a desired destination
of 5°, the difference of the two
numbers will be 345 instead of the
desired answer of 15. The function
AngleDifference solves this problem
by always returning the correct
difference with its sign indicating
which way to turn.
After initialization, the main
portion of the program in Figure 8
starts by establishing a destination
angle. The AngleDifference function is
then called to see how much the
robot needs to rotate in order to face
the destination.
In our case, the robot can be
ordered to move to the desired
orientation with a single r Turn
command. This is true because our RB-Once the robot is pointing in the right direction, a
while-loop moves the robot forward until an object is
detected with the RB- 9’s front-facing ranging sensor.
Inside the loop, the tablet’s compass is constantly read
and used to turn the robot back on course.
The TurnStyle parameter in the rCommand tells the
RROS to use gentle turns (one wheel running slower than
the other) as opposed to the default rotational behavior
(where the two wheels run in opposite directions). Note:
The RROS always uses the rotational style for turns with
an argument greater than 1°.
Now that we have shown how to obtain data from
the tablet sensors and use it to influence a robot’s
behavior, we are ready to explore a more complicated
project.
Next month, our robot will use GPS readings and the
compass routines developed this month to navigate to a
general location where the tablet’s integrated camera will
be used to find an object of a specific color. SV
72 SERVO 01.2014
Figure 7.