Page 296 - The Definitive Guide to Building Java Robots
P. 296
Preston_5564C07.fm Page 277 Monday, September 26, 2005 5:38 AM
CHAPTER 7 ■ NAVIGATION 277
/**
* @return Returns the edges.
*/
public ArrayList getEdges() {
return edges;
}
/**
* @param edges The edges to set.
*/
public void setEdges(ArrayList edges) {
this.edges = edges;
}
}
Code Objective
The code objective here is to give the robot the ability to navigate indoors, and most importantly
to the fridge.
Code Discussion
The IndoorNavigation class extends ObstacleNavigation because I don’t want my robot hitting
anything on its way to the refrigerator. It has two fields of type Room and Region where the
region will be the current region and the room will be the room passed through the constructor.
The move() method in this algorithm takes a single String parameter end, which will be the
name of the end NavPoint. The method itself consists of three parts: first, to get the current
region and start point through localization; second, to move from its current region to its end
region via the shortest path determined by the Dijkstra Algorithm; third, to move to the desired
end point in the end region.
Getting the start position is done via the getBestRegion() method. This method gets four
coordinate axes from the getFourCoordinates() method in Localization. These four coordinates
are measured against all the regions’ sizes and characteristics to produce a vote. The region
with the largest vote will then be the best region, and from there the start position will be
obtained from the best readings of the four axes: N, E, S, and W. See Example 7-19.
Example 7-19. IndoorNavigation.java
package com.scottpreston.javarobot.chapter7;
import java.util.ArrayList;
import com.scottpreston.javarobot.chapter2.JSerialPort;
import com.scottpreston.javarobot.chapter2.WebSerialClient;
public class IndoorNavigation extends ObstacleNavigation {
private Room currentRoom;
private Region currentRegion;

