Prerequisite Knowledge
A basic understanding of how to control the Turtle's movements as well as using data structures and other fundamentals in java.

Sonar sensors are often used in robots for obstacle avoidance, navigation and map building.
Much of the early work was based on a device developed by Polaroid for camera range finding.
Sonar sensors work by
emitting a short burst of ultrasonic sound (often 40 khz). Then, if any signals are reflected and the sensor can detect these reflected signals, the sensor can compute the object distance by using the time that it took for the signal to be reflected.
One of the difficulties with sonar, is that if the front face of the object in front of the sensor is too far from perpendicular to the sensor, then the signals will not be reflected back towards the sensor, and thus, the object will not be detected. This is why most modern-day machines and robots that still use sonar have multiple sensors surrounding the machine. This way, they have the ability to sense obstacles from multiple angles.
Although Lego does not currently produce a sonar sensor, there are other options out there. This lab has been tested with a sonar sensor from MindSensors. The 40khz sonar sensor gives feedback in inches, and is very accurate, however it requires a relatively high voltage level from the RCX brick batteries. To the right is a picture of the 40khz sonar sensor from MindSensors, along with an electric connect and 6 inch connecting wire.
A basic understanding of how to control the Turtle's movements as well as using data structures and other fundamentals in java.
Standard Turtle robot with 2 rotation sensors and a sonar sensor.
Gridded area for testing. See printable example.
From the previous two labs, you should now have the ability to guide your robot from any cell in an occupancy grid to any goal cell in the least number of steps, assuming that the robot is not blocked off. All you need to change now, is how the robot detects obstacles. Rather than have perfect world knowledge, you're going to change your GridWalker so that it uses the sonar to detect obstacles in the grid and build them accordingly in the representation within the robot. In addition to this, when you build obstacles in your representative world, you're going to need to update the wavefront propagation values, which might change the path that the robot needs to take to get to the goal midway through its traversal. You're also going to need a modified RotationNavigator, since LejOS's RotationNavigator is only setup to read from the two rotation sensors. You can get this later from here.
Now that you can read values from our sonar sensor, it's time to start thinking about how you're going to apply this data to the occupancy grid. One thing that you want to be careful of, is if the sonar sensor reads a wrong value. Therefore, you do not want to immediately build an obstacle in the cell 16 inches in front of the robot if the sonar sensor reads 16 inches. Perhaps you want to another 2D array that stores the probability of an obstacle in each cell. So when the robot reads in a value from the sonar sensor,
it will increase the probability of an obstacle in that cell, and if that probability passes some threshold, then it will build an obstacle in the array that stores the wavefront propagation values. Also, what about the cells between the robot and the cell that the sonar sensor supposedly sensed an obstacle in. You would probably want to decrease the probability of an obstacle being in those cells in the 2D array of obstacle probability values. This would help to correct any mistakes the robot might have recieved from the sonar readings.
So the major changes that you first need to make, are to create another 2D array that can store the obstacle probability values, along with create a method that, given some sonar reading, will modify the obstacle probability values in the proper direction, and, if necessary, build obstacles in the wavefront values array.
Now that you can update our representative world properly, based on the sonar readings, you're going to have to take another look at your algorithm for working your way through the occupancy grid. The reason that you need to change this, is because you no longer have perfect world knowledge, as you did when you decided where all of the obstacles were located before compiling and downloading the program. This new algorithm is going to need to be something similar to:
Modify your code so that if the next cell that you need to move to is directly in front of the robot (does not require the robot to turn), then the robot will not pulse its sonar sensor, but will rather just move ahead. This way, you don't read in redundant sonar sensor readings because the sonar sensor should be able to read any obstacle that is directly in front of it.
In this lab, you learned the following: