Computer Science 014
LEGO Robot Engineering

Williams College
Winter 2007


Lab 6: GOTO and Smoother Maze Running
Due: 10:00 AM, Tuesday, January 16, 2007


Next, we will add some new toys to the robots and use those to program some new behaviors. Our new devices are servo motors and sonar range finders.

Servo Motors

The motors we have used so far are controlled by the Handy Board by sending a signal telling the motor how fast to spin and in which direction. With our servo motors, the Handy Board sends a signal that specifies the position to which the motor's axle should spin. Logically, you send a signal to the servo specifying an angle and the servo twists its axle until it is positioned at the desired angle.

The range of motion of servo motors is typically limited to about 180 degrees. Internal gearing allow the motor to deliver plenty of torque. While you may have needed to gear down the output from the Lego motors to get more torque at a slower speed, you may find it necessary to do the opposite for the servos. In particular, if you want a range of motion of 360 degrees rather than 180 degrees, you will need to implement a 1 to 2 gearing.

The servos we will be using are not standard Lego parts, so they will not have the ability to snap nicely onto your robot. You will need to be creative and use tape and glue and screws to get your servo mounted securely.

The servo's connection is also different than those we have seen before: a three-pin female connector. To connect to the Handy Boards, we will have to install expansion boards that will provide a convenient way to install and interact with the servos (and later, the sonar).

The expansion board looks like this:

The servo connectors are in the bottom left. To install an expansion board on your Handy Board, first (carefully) remove the LCD monitor. Place the expansion board so that its pins match the sockets on the expansion board and gently press on the expansion board so that the pins are firmly installed in the sockets. You can then reinsert the LCD board into the socket on top of the expansion board. More detailed instructions may be found here.

You may now connect your servo to any of the six servo outputs on the lower left corner of the expansion board. The dark brown or black wire must be connected to the pin on the outside edge of the board.

To enable servo support, you first must call the function:

init_expbd_servos(1);

If you will not be using any servos for any length of time, you should disable them to save power, using the function call:

init_expbd_servos(0);

When the servos are enabled, you can control their position by setting values of the int variables servo0, servo1, servo2, servo3, servo4, and servo5. That's it!

The values you need to assign to these variables don't measure angles on a convenient scale like degrees or radians. Instead, the numbers correspond to the length of the pulse that the Handy Board will send to the servo to specify its position. Useful values are typically in the range of 600 to 3400, with a value of 2000 putting the servo near the center of its range of motion. You will need to experiment with your servo to find the best values. You may also wish to write functions that allow you to specify positions in degrees, given the behavior of your servo, any gearing you do, and the position of the servo on your robot.

Sonar Range Sensors

We will also be using a Sonar device: the Devantach SRF04.

This device sends out pulses of ultrasonic sound, measures the elapsed time between the pulse and the detection of the first echo, and reports the time value to the Handy Board. Dividing the value returned by the speed of sound enables the Handy Board to approximate the distance to the nearest object in front of the sonar range sensor. You should mount an SRF04 on a platform that can be rotated to specific orientations using a servo. This will allow you to scan for objects using the sonar without having to reorient your robot to take readings in different directions.

The SRF04 is very easy to use. The Interactive C function sonar() will return the distance value measured in millimeters (or a negative number, if no echo is returned). The tricky part for you as a programmer trying to make use of this information is that the echo may not come from an object directly in front of the SRF04. As the sound moves through the air, it spreads in a cone shape about 60 degrees wide. See the image on this page to see a graphical representation of the beam pattern.

The other tricky part about using the SRF04 is connecting it to your Handy Board. The desired wiring is shown below:

You may choose an SRF04 that it already soldered or solder your own. Your connections will need to be strong enough to withstand some movement as your sonar is moved around atop your platform, being rotated by the servo. Note that three of the wires are connected to pins on port 0 of the expansion board's digital outputs, and one is connected to a pin in port 7 on the Handy Board's digital inputs (on the original Handy Board, not the expansion board).

Preparing Your Robot

You will need to design and construct a support on which you can mount your servo motor and SRF04 sonar device. This may require some minor (or perhaps major) reconstruction of your robot. You will need to experiment with positions for the sonar device that will allow it to detect objects without interference from the parts of the robot itself.

Task 1: GOTO

Your first programming task is to write a program that uses the sonar to move your new and improved robot to a specified location relative to a corner:

Your robot will be placed near to a corner (at least, much closer to one corner than to any others) and should position itself at a point 3 feet from one wall and 2 feet from the other. Use sonar to determine your robot's initial position then try to move to the correct location. Once there, use the sonar to get a new update and make any necessary corrections. Repeat until your robot reaches the correct position.

Task 2: Smoother Maze Running

Next, it's back into the maze. But 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).

When started, the robot should move forward. As it moves, it should check the sonar reading (straight ahead) and stop if it is within some small distance of an obstacle (a wall). Next, the robot should take sonar readings a bit to the left and a bit to the right. It can compare these values to determine whether to turn to the right or to the left. It should then make an appropriate turn and move forward again until it encounters another obstacle.

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. If you've bumped into something, it may be because your distance threshold is too low. Therefore, you should increase it slightly with each bump so you are less likely to bump into the next wall.

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 the maze. How does it handle corners? Does it get stuck? How does this program compare to the one that used only touch sensors?

Demonstration

You will need to demonstrate that your robot successfully completes both tasks described above. Also turn in printouts of your programs.