Skip to content

Commit d289866

Browse files
committed
Add support for Sharp 2Y0A21 sensor
1 parent 8823712 commit d289866

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

robotpy_ext/common_drivers/distance_sensors.py

+34
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,40 @@ def getDistance(self):
3636
return max(min(d, 145.0), 22.5)
3737

3838

39+
class SharpIR2Y0A21:
40+
"""
41+
Sharp GP2Y0A21YK0F is an analog IR sensor capable of measuring
42+
distances from 10cm to 80cm. Output distance is measured in
43+
centimeters.
44+
45+
Distance is calculated using the following equation derived from
46+
the graph provided in the datasheet::
47+
48+
26.449*x ^ -1.226
49+
50+
.. warning:: FRC Teams: the case on these sensors is conductive and
51+
grounded, and should not be mounted on a metallic
52+
surface!
53+
"""
54+
55+
def __init__(self, port):
56+
""":param port: Analog port number"""
57+
self.distance = wpilib.AnalogInput(port)
58+
59+
def getDistance(self):
60+
"""
61+
:returns: distance in centimeters. The output is constrained to
62+
be between 10 and 80
63+
"""
64+
65+
# Don't allow zero/negative values
66+
v = max(self.distance.getVoltage(), 0.00001)
67+
d = 26.449 * math.pow(v, -1.226)
68+
69+
# Constrain output
70+
return max(min(d, 80.0), 10.0)
71+
72+
3973
class SharpIRGP2Y0A41SK0F:
4074
"""
4175
Sharp GP2Y0A41SK0F is an analog IR sensor capable of measuring

0 commit comments

Comments
 (0)