Your first robot

You've now learned enough to make a simple robot. Its purpose is to always be looking at the closest object. OK, it's not the most exciting robot, but it's a great demo of how you can combine what you've learned about the PWM and ADC subsystems.

For this, you will need:

  • Breadboard
  • 1x Sharp IR proximity sensor
  • 1x micro servo motor
  • 3x 1 kΩ resistor
  • 1x 0.1 µF capacitor
  • Jumper wires

Wire up the circuit on your breadboard, as shown in the figure:

Your first robot

The Sharp IR proximity sensor contains an infrared LED, an infrared detector, and some circuitry to drive them. The detector measures the amount of infrared light that is reflected back from whatever object is in front of it, which will vary depending on how close the object is, and a voltage is generated on the output which corresponds to the distance. It requires a 5 V supply to operate, and the output voltage can exceed 3 V. Therefore, a voltage divider is used to not exceed the ADC's 1.8 V maximum. The sensor has a high impedance output from its on board amplifier, so we can get away without a voltage follower.

Wire up the circuit, attach the proximity sensor to the servo motor, and the servo motor to a flat surface, as shown in the following image:

Your first robot

When the program starts up, it will scan across the full range of the motor and record the measured distances along the way. It will then rotate the motor back to the position where the least distance was measured. It will then stay there until the object it's looking at moves, then it will start the process over. Let's have a look at the following code:

from bbio import *
from bbio.libraries.Servo import Servo

range_pin = AIN0
servo_pin = PWM1A

servo = Servo(min_ms=0.81, max_ms=1.99)

def setup():
    servo.attach(servo_pin)

def loop():
    max_v_in = 0
    angle = 0
    for i in range(0, 180):
        servo.write(i)
        delay(1)
        v_in = analogRead(range_pin)
        if v_in > max_v_in:
            max_v_in = v_in
            angle = i
    servo.write(angle)
    delay(500)
    while (abs(max_v_in - analogRead(range_pin)) < 200):
        delay(100)

run(setup, loop)

The 1 millisecond delay in the scan is there to ensure the motor has had time to reach the set location before measuring the distance. The greater the sensor's output voltage, the closer the object that it's looking at it. Whenever we see a higher voltage than we have previously during a scan, we save the measured voltage and angle. That way, once the scan has finished, we know that the angle saved was the angle at which the closest object was detected. The 500 ms delay once again ensures that the motor has reached that angle, then the while loop holds the motor there until the voltage measured has changed by at least 200 mV. If we were to simply wait until the voltage changed at all, then any amount of noise on the input (which is inevitable) would cause it to rescan. The 100 ms delay in the while loop keeps the program from hogging the CPU while it's waiting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216