forked from karauri14/safetyplus-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turnListener.py
78 lines (60 loc) · 1.5 KB
/
turnListener.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
import obd
import RPi.GPIO as GPIO
import time
import threading
#speed log
MAX_LENGTH = 100
MARGIN = 5
INF = 1000
#use GPIO pin
L_PIN = 20
R_PIN = 21
#demo only
SLOW_PIN = 16
SPEED = 60
#turn listener
'''
con = obd.OBD()
'''
speedRecode = []
def speedLog():
#demo only
for i in range(0, MAX_LENGTH):
if (GPIO.input(SLOW_PIN) == GPIO.LOW):
speedRecode.append(SPEED - MARGIN)
else :
speedRecode.append(SPEED)
time.sleep(0.01)
'''
for i in range(0, MAX_LENGTH):
speedRecode.append(con.query(obd.commands.SPEED))
time.sleep(0.01)
'''
def isSlow():
speedThread = threading.Thread(target = speedLog, name="speedThread")
speedThread.setDaemon(True)
if speedThread.is_alive() == False:
#demo only
firstSpeed = SPEED
'''
firstSpeed = con.query(obd.commands.SPEED)
'''
speedThread.start()
for i in speedRecode:
if (i <= (firstSpeed - MARGIN)):
return True
return False
def listener(state):
if (GPIO.input(L_PIN) == GPIO.LOW):
if state == 'left,':
return ('left,')
if isSlow():
return ('left,')
if (GPIO.input(R_PIN) == GPIO.LOW):
if state == 'right,':
return ('right,')
if isSlow():
return ('right,')
if (state == 'right,' or state == 'left,'):
speedRecode[:] = []
return (',')