CSCI 108 Lab 7


This week's lab will take you back to the maze. This time, with the help of sonar, your goal will be to get the robot through the maze without bumping into the walls.

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).

Preparing the robot

For this lab you need to mount an ultrasonic range finder to the front of your vehicle. First, however, you will need to wire the device so that it can be plugged into the Handy Board.

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).

Getting sonar readings

You will find it helpful to use srf04-3.c for this assignment (introduced in class.) The file can be found in the CS 108 folder on the cortland file server. It contains all the code necessary to initialize the sonar unit and get distance readings from it. In particular, you should copy the functions sonar_init(), sonar_sample(), and dist_measure().

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.

Writing the program

Write an Interactive C program that demonstrates the following behavior. When started, the robot should move forward. As it moves, it should check the sonar reading and stop if it is within some small distance of an obstacle.

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.


Load your program to your robot, and try it out. First let the robot wander in a fairly open space with just a few obstacles. Then try it out in a space that has more obstacles. Finally, see what it does when put in this week's maze. How does it handle corners? Does it get stuck? How does this program compare to the one that used only touch sensors?


You will need to demonstrate to me that your robot successfully navigates away from obstacles. In particular, you will demonstrate it in the maze.

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.

Before leaving the lab

Before you leave the lab, please be sure to do the following: