# Introduction to Robotics/Sensors/Lab/Students

Sensors: Lecture (For Students) (For Teachers) — Lab (For Students) (For Teachers) — Assignment (For Students) (For Teachers) — Quiz (For Students) (For Teachers)

## Ultrasonic Sensors

Before starting, connect the Ultrasonic sensor to the BoeBot. You may mount the sensor either directly to the chassis or you may attempt to connect it to the motor mount. If you need help, ask the instructor. Connect the Ultrasonic Sensor to Port 15.

### Speed of Sound

For an ultrasonic sensor to work, the sound must bounce back from the target. This means that the sound must travel twice the distance from the sensor to the object. For an object 10 meters away, the sound must travel 10 meters to the object, and 10 meters back to the sensor, for a total of 20 meters.
Questions:
* How long does it take for sound to travel 10 centimeters (to an object 5 centimeters away and back)?
* Do the same value for 20, 30, 40, and 50 centimeters. Plot all the

### Measured Values

Place the BoeBot so that the ultrasonic sensor is facing a wall or other solid surface. Type in the following code:
Run the program, while the BoeBot is still connected to the computer. The Debug information should appear in a new window. The program is an infinite loop that will continue to run until the BoeBot is turned off or reprogrammed.
Do the following:
* Move the BoeBot so that the ultrasonic sensor is exactly 5 centimeters from the wall or surface. Record the time value.
* Do the same thing for a variety of values: 10 centimeters, 15, 20, etc.
* Plot the results on a graph of distance/time. The "Time" variable contains the number of clock ticks, not seconds. To get seconds, multiply "Time" by 2μseconds. What is the shape of the graph?
* Compare your graphs for calculated and measured values. Remember that the distances traveled by the ultrasonic wave are doubled. Are the graphs the same?
Questions:
* Can you think of an approximate mathematical relationship between the Time variable and the distance traveled?

### Exercises

Perform the following exercises using the time values you measured above, and your subroutines and constant definitions from your previous labs.
* Write a program to make the BoeBot move forward in a straight line. If the BoeBot comes within 5 centimeters of a wall or other object, do a 180° turn and go back the other direction.
* Write a program to make the BoeBot “search” for other BoeBots. Rotate your BoeBot around in the circle until it finds an object within 20 centimeters. When the BoeBot finds that object, move towards it.

## Ultrasonic Sensors with Motor Mount

If you have not already done so, connect your ultrasonic sensor to the supplied motor mount. This will allow the sensor to swivel around, without having to turn the entire BoeBot. Plug this new motor into port 14.

### Calibration

Do the following first:
* Properly calibrate the new motor
* Create subroutines to turn the ultrasonic sensor exactly 90° to the left and right.
When using the ultrasonic sensor and the motor mount, be careful not to turn the sensor more then 360° because the wire will wrap around the motor. Some motors may only turn 180°. In this case, you do not need to worry about going too far in one direction.

### Exercises

Using the new motor mount and subroutines, do the following exercises:
* The BoeBot is afraid of other BoeBots. When another BoeBot gets close, your BoeBot should run as fast as it can in the opposite direction. Hint: keep track of what direction the ultrasonic sensor is pointed in using a variable, and use IF / THEN statements to check that variable to determine which way to turn and run.
* Write a program to have a BoeBot sneak up on a victim and pounce. When the BoeBot detects another BoeBot, it should creep forward slowly till it is within 6 inches. When it is 10 centimeters away, the BoeBot should go to full speed and attack!
* Write subroutines to turn the motor mount 45° to the left and right.
* Write a program to play follow the leader. The rules are: The BoeBot should stay between 10 and 20 centimeters of the leader. When the leader turns, the BoeBot should also turn.