GuyCarver-MicroPython/lib/IRDistance.py

33 wiersze
761 B
Python
Czysty Zwykły widok Historia

2017-11-04 21:43:37 +00:00
from pyb import Pin, ADC
2018-02-18 14:11:30 +00:00
class irdistance(object):
2017-11-04 21:43:37 +00:00
""" Driver for Sharp Gp2y0a IR distance sensor. The distance
range is around 3 to 40 inches. """
def __init__( self, pin ) :
"""pin may be name or pin object. It must be able to handle ADC input."""
if type(pin) == str:
p = Pin(pin)
elif type(pin) == Pin:
p = pin
else:
raise Exception("pin must be pin name or pyb.Pin able to support ADC")
self._adc = ADC(p)
@property
def distance( self ) : return self._adc.read()
@property
def inches( self ) :
2018-02-28 23:03:42 +00:00
#distance / 204.8? Why?
2017-11-04 21:43:37 +00:00
volts = self.distance * 0.0048828125
2018-02-28 23:03:42 +00:00
#inches = v^-1.02.
return 65.0 * pow(volts, -1.02)
2017-11-04 21:43:37 +00:00
@property
def centimeters( self ) : return self.inches * 2.54