This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proximity.py
187 lines (141 loc) · 5.62 KB
/
proximity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""Provide proximity measurements.
"""
import sys
from time import time, sleep
from threading import Thread
from multiprocessing import Process
from edi2c import ads1x15
class Proximity:
DEFAULT_CHANNELS = (2, 3)
DEFAULT_VOLTAGE = 5.0
DEFAULT_PGA = 6144
DEFAULT_SPS = 860
VALID_DISTANCE = (20, 770)
adc = None
channels = None
voltage = None
pga = DEFAULT_PGA
sps = DEFAULT_SPS
_last_warning = 0
def __init__(self, channels=DEFAULT_CHANNELS, voltage=DEFAULT_VOLTAGE, sps=DEFAULT_SPS):
self.adc = ads1x15.ADS1X15(ic=ads1x15.IC_ADS1115)
self.channels = channels
self.voltage = float(voltage)
self.sps = sps
if len(channels) > 2:
raise ValueError, "Proximity: only two channels are supported"
def is_valid_distance(self, distance):
return distance >= Proximity.VALID_DISTANCE[0] and distance <= Proximity.VALID_DISTANCE[1]
def _maybe_warn(self, message):
now = time()
elapsed = now - self._last_warning
if elapsed < 5:
return False
self._last_warning = now
#print >> sys.stderr, message
return True
def read_once(self):
"""Take a single reading from the channels.
"""
result = []
# pga should be 6144? verify the math here
for channel in self.channels:
value = self.adc.read_single_ended(channel, pga=self.pga, sps=self.sps) * 1024 / (1000 * self.voltage)
result.append(int(round(value)))
return result
def read(self, filter_length=10, rejection_threshold_cm=30):
"""Take median filtered readings.
"""
distances = ([], [])
while filter_length > 0:
reading = self.read_once()
distances[0].append(reading[0])
distances[1].append(reading[1])
filter_length -= 1
medians = [0, 0]
mean = 0
for i, readings in enumerate(distances):
# Median filter
readings.sort()
median = readings[len(readings)/2]
mean += median
medians[i] = median
for i, readings in enumerate(distances):
# Bad data rejection
bad = sum(0 if self.is_valid_distance(x) else 1 for x in readings)
if bad > 0:
self._maybe_warn("Proximity: ADC channel %d returned bad data" % self.channels[i])
# Automatically return other value
return float(medians[(i + 1) % 2])
mean /= float(len(distances))
# TODO: use alternative median filtering? IEEE 05720809 when combining?
# if we're beyond the rejection threshold and there are two or less sensors,
# choose the reading from the sensor reporting a closer object (smaller value)
for i, median in enumerate(medians):
if abs(mean - median) >= rejection_threshold_cm:
return float(medians[(i + 1) % 2])
return mean
class Proxemic(Proximity):
INTIMATE = 0
PERSONAL = 1
SOCIAL = 2
PUBLIC = 3
RANGE = [45, 120, 280, 720]
last_space_distance = 0
last_space = -1
def __init__(self, *args, **kwargs):
Proximity.__init__(self, *args, **kwargs)
def get_space_distance(self, filter_length=10, rejection_threshold_cm=30):
distance = self.read(filter_length, rejection_threshold_cm)
for i, limit in enumerate(Proxemic.RANGE):
if distance <= limit:
return i, distance
return len(Proxemic.RANGE) - 1, distance
def _monitor_space_thread(self, callback, distance_callback, interrupt_callback):
current_distance = -1
current_space = None
current_space_time = 0
try:
while True:
sleep(0)
space, distance = self.get_space_distance()
now = time()
if distance != current_distance:
current_distance = distance
if not distance_callback(current_distance):
return
if space != current_space:
if now - current_space_time > 1.33 and abs(distance - self.last_space_distance) > 25:
current_space = space
current_space_time = now
self.last_space_distance = distance
if not callback(current_space):
return
except KeyboardInterrupt:
if callable(interrupt_callback):
interrupt_callback()
def monitor_space(self, callback, distance_callback, interrupt_callback=None):
monitor_thread = Thread(target=self._monitor_space_thread, args=(callback, distance_callback, interrupt_callback))
monitor_thread.setDaemon(True)
monitor_thread.start()
def main(args):
channels = map(int, args) or Proximity.DEFAULT_CHANNELS
pr = Proxemic(channels)
current_color = None
current_space = None
current_space_time = 0
last_space_distance = 0
while True:
space, distance = pr.get_space_distance(10, 30)
now = time()
if space != current_space:
if now - current_space_time > 1.33 and abs(distance - last_space_distance) > 25:
current_space = space
current_space_time = now
last_space_distance = distance
print int(round(distance)), "\t", current_space, " "*70, "\r",
sys.stdout.flush()
if __name__ == '__main__':
args = sys.argv[1:]
main(args)
sys.exit(0)