The Sharp GP2D12 sensors can be used to determine the distance of the robot from the center of a hallway or from a wall as well as the angle relative to the walls. Four D12s are used for this purpose.
Two D12 sensors are placed on each side of the robot pointing straight out to the side.
Since the D12 can't read distances of less than 4 inches, the sensors are located as close to the centerline of the robot as possible so that if the robot is very close to the wall, the sensors will still be about 4 inches from the wall.
The distances read by the sensors are compensated so that they read the distance from the centerline of the robot to the wall. For example, DF, the distance read by the right front sensor might read 4 inches when the robot is touching the wall. Since the robot has a 5 inch radius, another inch is added to the sensor reading so that DF equals 5 inches. The same is done for the rear measurement, DR.
Now, the distance from the robot to the wall can be determined by taking the average of the two readings: (DF + DR)/2. This method will work quite well even if the robot is at an angle to the wall (as show in the next figure). Those of you who are geometrically inclined may note that this calculation is not quite right when the robot is at an angle. The distance should be multiplied by the cosine of the angle. This is true, but for relatively small angles as may be expected while traveling down a corridor (maybe 15 degrees or less), the non-compensated calculation is only a few percent off and will have little effect on how the robot is steered.
The angle of the robot relative to the wall can also be determined by these two sensor readings. Using small angle approximations (put in definition), the angle is very close to (DF - DR) / X where X is the distance between the mounting locations of the two sensors. This calculation will give the angle in radians(define) with a very usable degree of accuracy.
Snuffy has a similar set of D12 sensors on the left side. They can be used to do the same calculations. If both side walls are visible to the sensors, the calculations from the two sides can be averaged. If only one side is visible (as in passing a hall or doorway), that side will give readings. And, if only one wall is in sensor range (as in moving around within a room), either side can give calculations to do wall following.
Two IR sensors are used for detecting distance forward. A D12 range sensor is placed on the right side of the robot. It is on the right side because it is used while doing right wall following while trying to get out of an arbitrary room, and it must see the next wall coming up. When in room 4, the small interior room, the upcoming wall is very short and can't be seen by a sensor on the left side.
The D15 sensor does not provide range, but just gives a logic signal when something comes within its range. This sensor was added on the left side to detect an upcoming wall on the left side if the robot happens to be approaching the wall at such an angle that the right side D12 sensor still sees a large distance. I used a D15 only because I was running out of analog inputs to my microcontroller.
One thing to consider, I didn't find the forward looking D12 to be very accurate when approaching a wall at significant speed (like 20 inches per second or so). I used a couple different sensors in different locations and found that at distances of more than 9 inches, the reading was a lot closer than it really was. The sensor read fine when standing still and looking at a forward wall. Fortunately, it became pretty accurate in time to slow down before hitting the wall.