-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor.py
More file actions
160 lines (118 loc) · 4.79 KB
/
Sensor.py
File metadata and controls
160 lines (118 loc) · 4.79 KB
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
#Group 2 -- GPIO code
#Causey, Dalferes, Vaurigaud
import pineworkslabs.RPi as GPIO
from time import sleep
import time
import datetime
GPIO.setmode(GPIO.LE_POTATO_LOOKUP)
#Constants (some unused)
DEBUG = False
SETTLE_TIME = 0.75/7.5 #must be low to prevent slow down from real time measurment
#may have to change this and other values to make program match up with
#walking speed
TRIGGER_TIME = 0.0001
SPEED_OF_TIME = 343 #m/s
DEFAULT_PINS = [1,2]
DISTANCE_DOOR = 2.5 #ft
#Since two untrasonic sensors are being used, there's benefit in a Sensor class
class Sensor():
'''added self.TRIG and self.ECHO and getters/setters'''
def __init__(self, name:str, pins:list):
self.name = name
#self.configure_pins(pins); not needed
self.TRIG = pins[0]
self.ECHO = pins[1]
self.gap = 0.0 #to be used by sensor_tripped for range checking (being within door frame)
@property
def name(self):
return self._name
@name.setter
def name(self,value):
self._name = value
"TRIG/ECHO getters/setters"
@property
def TRIG(self):
return self._TRIG
@TRIG.setter
def TRIG(self, value):
self._TRIG = value
@property
def ECHO(self):
return self._ECHO
@ECHO.setter
def ECHO(self, value):
self._ECHO = value
@property
def gap(self):
return self._gap
@gap.setter
def gap(self, value):
#shouldn't be negative, but range-checking not needed
self._gap = value
def calibrate(self):
"""Regulates the distances and returns a correction factor to use for calculations"""
#Notes for August: make is where we put in the measured distance from the sensor to the door,
#then the sensor goes off and gets that value. You get: correction factor.
#If the correction factor is lower, than the sensor wont go off unless
#sensor_distance_reading > known_distance
#by greater than correction factor
#vice versa
"self.gap is modified"
timeavg = 0
denom = 0
count = 0
GPIO.setup(self.TRIG, GPIO.OUT)
GPIO.setup(self.ECHO, GPIO.IN)
while count <= 5:
time_start = time.time()
GPIO.output(self.TRIG, GPIO.LOW)
sleep(TRIGGER_TIME)
GPIO.output(self.TRIG, GPIO.HIGH)
if GPIO.input(self.ECHO) == GPIO.HIGH:
time_end = time.time()
timeavg += (time_end - time_start)
denom += 1
count +=1
if (timeavg == 0) or (denom == 0):
#recursion allows function to rerun if self.ECHO was
#never HIGH
#if sensor broken recursion error will be returned
self.calibrate()
return
timeavg = timeavg/denom
self.gap = timeavg
def sensor_tripped(self, my_list):
"""If there's movement of a person between a calculated distance of the doorways,
the sensor records this distance for correction factor later. """
GPIO.output(self.TRIG, GPIO.LOW)
sleep(SETTLE_TIME)
#time_start - time_end = comparable time stamp
time_start = time.time()
GPIO.output(self.TRIG, GPIO.HIGH)
sleep(TRIGGER_TIME)
GPIO.output(self.TRIG, GPIO.LOW)
#wait for ECHO pin to be high
if (GPIO.input(self.ECHO) == 0):
trip_time = time.time()
#rounded_time = round(trip_time, 5) not needed
if (trip_time - time_start) <= (self.gap - .0003): #lower contol limit
my_list.append(trip_time)
return my_list
def calculations(self, list):
"""Takes in the list of recorded times as a argument and calculates a average time that
the sensor was tripped and returns that value"""
list_sum = sum(list)
average = list_sum/len(list)
#implement some way to limit the decimals
return average
def in_or_out(self, other:'Sensor', people, list1, list2):
"""Compares two sensors by taking their calculations and returning a positive or negative value"""
#To label the sensors, self is considered closer to outside the room and other is closer to the inside.
#if the time is smaller, got tripped first, someone went in, otherwise, someone left the room
if self.calculations(list1) <= other.calculations(list2):
people += 1
elif other.calculations(list1) >= other.calculations(list2):
people -= 1
else:
raise ValueError("Wrong data type possibly.")
return people