Navigation Theory
The method just described is called dead reckoning (sometimes called orienteering), a method of navigation used by all animals, older sailing vessels, geologists, forest rangers, and hikers. The only pieces of information needed for dead reckoning are direction and distance. Direction is usually obtained using a magnetic compass and distance (odometry) can be found in a number of ways, such as keeping count of paces and knowing the distance of each pace. A geologist entering a thick forest essentially has no visual references to follow because the trees block off almost everything on the horizon. Essentially it is the same situation as navigating an ocean, with few landmarks to help identify position. A geologist typically wants to find an outcrop of rock in the forest, usually identified by an aerial map of the location. Because trees themselves are not landmarks, the geologist must plot the distance as a straight line from the starting point (usually a road) to the destination (the outcrop). Once the path is plotted, the geologist takes a direction measurement directly toward the goal and starts pacing off the distance. The method is as good as the person pacing off the distances and scoping the direction, but generally it only brings the geologist to within 100 feet of the targetnot very accurate!
There are some advantages and disadvantages to using this type of navigation for a robot. The advantages for a robot are obvious: A robot's ability to pace distances is vastly more accurate than that of a human, at least on a smooth floor. With wheels and accurate timers, a robot (like any machine) will outperform a human. It is still estimating by timing and, although it is better than a human, this can lead to low accuracy. The accuracy of calculating and storing coordinate points is also better than that of humans, although a pad of paper often helps humans with better memory recall. There is a downside to robots, however: Most robots are unable to self-correct their course by analyzing the situation. They also have no ability to visually recognize a target or landmark. Finally, the RIS kit does not contain a compass to help with navigation (see Chapter 10). These shortcomings are addressed here through firsthand demonstrations.
Trigonometry
The goal with navigation is to accurately know the current location, and the easiest way to represent this is with a coordinate system. For our navigation purposes we will employ simple x, y coordinates to indicate a position in a two-dimensional space. Let's examine how these coordinates can be calculated. A rectangular coordinate system consists of an x and y axis. Both of these axes start at zero and include positive and negative numbers. The x and y axis divide the system into four quadrants (Figure 73). Any point in a two-dimensional area can be plotted in this space using x and y.
Figure 73 A rectangular coordinate system.
The angle can be measured in two ways. The first, most common form is degrees. A complete rotation is 360 degrees. Engineers, scientists, and mathematicians prefer using radians because the units are not arbitrarily arrived at. A complete rotation in radians is given the value 2þ or about 6.28 radians. You can use whichever system you are more comfortable with, and there are methods in the Math class for conversion between the two (see the leJOS API docs). In the coordinate system, zero degrees (or zero radians) always runs along the x axis, and positive rotation occurs counterclockwise (Figure 74). Thus, when the robot is pointing up along the y axis, it is at 90 degrees.
Figure 74 Rotation.
Trigonometry is simply a branch of mathematics dealing with the relationships of the sides of triangles with angles. For our navigational purposes we use only the very simplest form of trigonometry: right angle trigonometry. Every time a robot moves a distance, the x and y coordinates also change. For example, if the robot rotates 60 degrees to the left and travels 20 cm, as it is moving both the x and y values will increase (Figure 75). As you can see, this movement creates a triangle with a right angle. In trigonometry, when there is a right-angled triangle with all angles known, and the length of one side is known, it is possible to calculate the lengths of all sides.
Figure 75 Right angle trigonometry.
The side of the triangle opposite the right angle is called the hypotenuse, the side opposite the angle in the calculation is the opposite, and the remaining side is the adjacent (Figure 76). As stated before, our robot will always keep track of the angle, and it will pace off the distance traveled (the hypotenuse). To calculate the new location we will need to calculate x (the adjacent) and y (the opposite).
Figure 76 The three sides of a right-angle triangle.
In high school math you probably learned tangent, cosine, and sine (tan, cos, and sin). These functions are used to solve for the lengths of sides on a triangle. Many people use the mnemonic SOH, CAH, TOA to remember the following equations:
sin(angle) = opposite/hypotenuse cos(angle) = adjacent/hypotenuse tan(angle) = opposite/adjacent
We only need to know the opposite and adjacent, so only the first two equations are useful to us. Let's replace the technical terms with variables and rearrange the equations to make things simpler. The distance traveled by the robot, the hypotenuse, will be replaced by distance:
x = cos(angle) \ distance y = sin(angle) \ distance
We can now use these equations to figure out the x and y coordinates after a robot has moved a distance across the floor. Let's imagine the robot has started at 0,0 and rotates positive 70 degrees (counterclockwise), then moves 25 cm (Figure 75). In order to find new coordinates simply plug our values into the preceding equations to get:
x = cos(70) \ 25 y = sin(70) \ 25
If you are using a calculator, make sure it is in degrees (DEG mode) and not radians (RAD mode). The resulting x and y coordinates are 8.55 and 23.49.
In Java code, it is very easy to figure out how these will look. Just use the calculus methods from the Math class, as follows:
double x = Math.cos(Math.toRadians(70)) * 25; double y = Math.sin(Math.toRadians(70)) * 25;
NOTE
All methods in the java.lang.Math class use radians for angles. There are two methods available to convert back and forth between degrees to radians: Math.toRadians() and Math.toDegrees().