Computer Science 014 |
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.
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 expansion board looks like this:
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.
We will also be using a Sonar device: the Devantach SRF04.
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 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.
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:
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?
You will need to demonstrate that your robot successfully completes both tasks described above. Also turn in printouts of your programs.