This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
151 lines (137 loc) · 4.25 KB
/
main.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
from _mqtt_pub import *
from _face import *
from _gps import *
import _pin
from _pin import *
from RPi.GPIO import HIGH
import time
import random as rnd
from numpy import ndarray
# pin = my abstraction over RPi.GPIO
# blink(on_duration_ms, total_cycle_ms)
# set() = out(HIGH)
# clear() = out(LOW)
# get() = in(), no pull-up/pulldown resistor
green = outpin(35)
red = outpin(36)
buzzer = outpin(37)
relay = outpin(38, init=HIGH) # relays are active-low
alcohol = inpin(11)
spy = facemon()
# "do_sentry":
# make the driver intermittently pick up the alcohol IID device and breathe into it
# alcohol sampled when face detected
try:
# intermittent BrAC (breath-alc-conc) checker
def do_sentry():
print("in sentry mode...")
# global spy, red, green, buzzer, relay, alcohol
green.set()
red.clear()
buzzer.clear()
# time.sleep(rnd.randint(120, 1800))
time.sleep(10)
red.blink(20, 1600)
buzzer.blink(100, 1000)
start = time.time()
face_found = False
while True:
face_found = spy.has_face()
if (time.time() - start >= 10) or face_found:
break
if face_found:
print("FOUND FAce")
buzzer.clear()
if not spy.match_face():
print("New face")
green.clear()
buzzer.blink(400, 800)
red.blink(200, 400)
viol_msg = "Unverified driver!\n"
try:
pos: fix = fix(12.916517, 79.132500)
viol_msg += f"Location (lat, long): {pos.lat:#8.4g}, {pos.lng:#8.4g}\n"
except NoFixException:
viol_msg += f"Cannot get fix on location\n"
push_data(viol_msg, "avoiding")
print("Registering face...")
green.blink(200, 400)
spy.reset_face()
spy.init_face()
else:
print("MATCHED!!!!")
spy.store_face()
if alcohol.get():
green.clear()
red.set()
buzzer.blink(400, 800)
red.blink(50, 100)
relay.set()
try:
speed: float = 0 # in km/h
if (speed > 5):
viol_msg = f"Drunken driver\nLocation (lat, long): {pos.lat:#8.4g}, {pos.lng:#8.4g}\n"
push_data(viol_msg, "violating")
else:
main()
except NoFixException:
viol_msg = f"Drunken driver\nCannot get fix on location\n"
push_data(viol_msg, "violating")
time.sleep(5)
else:
green.set()
red.clear()
buzzer.clear()
relay.clear()
else:
green.clear()
buzzer.blink(400, 800)
red.blink(200, 400)
try:
pos: fix = fix(12.916517, 79.132500)
viol_msg = f"Measurement avoided\nLocation (lat, long): {pos.lat:#8.4g}, {pos.lng:#8.4g}\n"
except NoFixException:
viol_msg = f"Measurement avoided\nCannot get fix on location\n"
push_data(viol_msg, "avoiding")
time.sleep(5)
buzzer.clear()
do_sentry()
def wait_on_all_clear():
global spy
spy.wait_face()
if alcohol.get():
green.clear()
relay.set()
red.set()
time.sleep(5)
wait_on_all_clear()
print("non-drunk driver, proceed")
print("remembering face...")
green.clear()
green.blink(200, 400)
spy.init_face()
sleep(1/2)
green.set()
red.clear()
relay.clear()
return
def main():
green.blink(20, 1600)
buzzer.clear()
red.set()
relay.set()
# time.sleep(10)
print("waiting...")
wait_on_all_clear()
print("going into sentry mode...")
do_sentry()
if __name__ == "__main__":
main()
except KeyboardInterrupt:
print("Caught ^C, quitting!\n")
del green
del red
del buzzer
del relay
del alcohol
del spy