-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic.py
95 lines (78 loc) · 1.83 KB
/
ultrasonic.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
import RPi.GPIO as GPIO
import time
import threading
TRIG = 26
ECHO = 24
_running = False
_last_dis = None
_start = False
_thread = None
_count = 0
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(TRIG, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ECHO, GPIO.IN)
def distance():
if _last_dis:
return _last_dis
return refresh_distance()
def refresh_distance():
global _running, _last_dis, _count
if _running:
if _last_dis:
return _last_dis
else:
return -1
else:
_running = True
tmmp = time.time()
while GPIO.input(ECHO):
if time.time() - tmmp > 0.02:
_running = False
return -1
GPIO.output(TRIG, GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(TRIG, GPIO.LOW)
tmmp = time.time()
while not GPIO.input(ECHO):
if time.time() - tmmp > 0.02:
_running = False
return -1
t1 = time.time()
while GPIO.input(ECHO):
if time.time() - t1 > 0.02:
_running = False
return -1
t2 = time.time()
dis = (t2 - t1) * 17000
_last_dis = dis
_count += 1
_running = False
return dis
def loop():
while _start:
refresh_distance()
time.sleep(0.005)
def start():
run()
def run():
global _start, _thread
if not _start:
_start = True
if _thread is None:
_thread = threading.Thread(target=loop)
_thread.start()
def stop():
global _start, _thread
_start = False
_thread = None
if __name__ == '__main__':
# run()
# # time.sleep(60)
# while True:
# print(distance())
# if input() == '0':
# break
# stop()
# print(_count)
test()