Your implementation will have many similarities to the earlier maze lab, in which you did obstacle sensing and avoidance. The program you write should make the robot wander through its world. If it detects an obstacle, it should adjust its direction and continue to wander. It should stop only when it is turned off (or the stop button is pressed).
As shown in the figure below, you will need four wires and two sets of pins. One set of pins should be a group of four, with one pin removed (i.e., the standard pin configuration for all sensors). Solder three of the wires to the three-pin unit. Solder the remaining wire to a one-pin unit.
Next you will need to solder the wires to the Devantech SRF04 Ultrasonic Range Finder. The figure above shows where each wire is to be attached. Note that the figure shows the back of the SRF04 Board. It is quite tricky to solder the wires to the board. You will need enough solder to keep the wires in place; but you don't want big blobs of solder that run into each other. The goal is to keep the wires completely separate from one another.
As illustrated in the diagram, you should plug the sonar unit into port 7 (the single wire) and port 9 (the triple wire).
Before you can get any sonar readings, you will need to call the function sonar_init(), as illustrated in main() in the srf04-3.c file. Then, to get a reading, simply call dist_measure(). dist_measure() works as follows. First it calls sonar_sample(). This function returns an integer, which is the amount of time it takes for the sonar ping to hit an object and bounce back. The function reports this in special timing units. (2,000,000 counts are made per second.) You will need to convert this reading to distance in feet. Remember that sound travels 1.1 feet per millisecond. That is, it takes 0.89 milliseconds to travel one foot. So the distance to the target (in feet) is the time it takes for the round trip (in milliseconds) divided by 1.78.
Note that in dist_measure() I check whether the value returned by sonar_sample() is negative. This indicates that the "chirp" sent out by the sonar device didn't return within a reasonable amount of time and so there is no valid reading to be obtained. In this case, you might want to assume that the closest obstacle is far away.
Next, the robot should turn a bit to the right and take a sonar reading; it should then turn a bit to the left and do the same. It can compare these values to determine whether to turn to the right or to the left.
Note that the robot can get stuck in a corner, just as in the earlier maze lab. Fortunately, the strategy for detecting a corner can be adapted and used here.
Finally, you will find that even with the sonar unit, your robot will occasionally bump into walls. If it does so, the robot should back up and adjust, just as in the earlier lab. When this occurs, however, you should also increase the distance threshold for the sonar. For example, say you set your distance threshold to 0.75. If the sonar reading says you are closer than 0.75 feet to an object, you need to turn. If you've bumped into something, it may be because the threshold is too low. Therefore, you should increase it slightly with each bump.
You should turn in your program. Turn it in both electronically and on paper. Don't forget to put your name on everything submitted. It is particularly helpful if you put your name (or initials) in the name of the file you submit electronically.
This lab must be completed by Monday, November 3 at 10:00 PM.