Fix #114 - ultrasonic sensors

Implements support for the HC-SR04 ultrasonic sensor as an input device
class named DistanceSensor
This commit is contained in:
Dave Jones
2016-02-09 14:08:27 +00:00
parent 810bb67a6d
commit 83fb6ae8b4
8 changed files with 287 additions and 4 deletions

View File

@@ -48,6 +48,8 @@ so you can still do::
.. autoexception:: GPIOBadQueueLen
.. autoexception:: GPIOBadSampleWait
.. autoexception:: InputDeviceError
.. autoexception:: OutputDeviceError

View File

@@ -20,8 +20,8 @@ Button
:members: wait_for_press, wait_for_release, pin, is_pressed, pull_up, when_pressed, when_released
Motion Sensor (PIR)
===================
Motion Sensor (D-SUN PIR)
=========================
.. autoclass:: MotionSensor(pin, queue_len=1, sample_rate=10, threshold=0.5, partial=False)
:members: wait_for_motion, wait_for_no_motion, pin, motion_detected, when_motion, when_no_motion
@@ -33,6 +33,14 @@ Light Sensor (LDR)
.. autoclass:: LightSensor(pin, queue_len=5, charge_time_limit=0.01, threshold=0.1, partial=False)
:members: wait_for_light, wait_for_dark, pin, light_detected, when_light, when_dark
Distance Sensor (HC-SR04)
=========================
.. autoclass:: DistanceSensor(echo, trigger, queue_len=30, max_distance=1, threshold_distance=0.3, partial=False)
:members: wait_for_in_range, wait_for_out_of_range, trigger, echo, when_in_range, when_out_of_range, max_distance, distance, threshold_distance
Analog to Digital Converters (ADC)
==================================

View File

@@ -443,6 +443,36 @@ level::
pause()
Distance sensor
===============
.. IMAGE TBD
Have a :class:`DistanceSensor` detect the distance to the nearest object::
from gpiozero import DistanceSensor
from time import sleep
sensor = DistanceSensor(23, 24)
while True:
print('Distance to nearest object is', sensor.distance, 'm')
sleep(1)
Run a function when something gets near the sensor::
from gpiozero import DistanceSensor, LED
from signal import pause
sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2)
led = LED(16)
sensor.when_in_range = led.on
sensor.when_out_of_range = led.off
pause()
Motors
======
@@ -480,6 +510,19 @@ Make a :class:`Robot` drive around in (roughly) a square::
robot.right()
sleep(1)
Make a robot with a distance sensor that runs away when things get within
20cm of it::
from gpiozero import Robot, DistanceSensor
from signal import pause
sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2)
robot = Robot(left=(4, 14), right=(17, 18))
sensor.when_in_range = robot.backward
sensor.when_out_of_range = robot.stop
pause()
Button controlled robot
=======================