forked from lionyhw/Octopus_MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdistance.py
More file actions
51 lines (38 loc) · 1.07 KB
/
distance.py
File metadata and controls
51 lines (38 loc) · 1.07 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
from microbit import *
from time import sleep_us
from machine import time_pulse_us
class DISTANCE(object):
"""基本描述
HC_SR04超声波支持库,可以选择返回厘米和英尺
Args:
pin_trig (pin): Trig信号引脚
pin_echo (pin): Echo信号引脚
Returns:
distance: 距离
"""
def __init__(self, pin_d):
self.__pin_e = pin_d
self.__pin_t = pin_d
def get_distance(self, unit=0):
"""基本描述
读取距离值
Args:
unit (number): 检测距离单位 0 厘米 1 英尺
Returns:
distance: 距离
"""
self.__pin_e.read_digital()
self.__pin_t.write_digital(1)
sleep_us(10)
self.__pin_t.write_digital(0)
ts = time_pulse_us(self.__pin_e, 1, 25000)
distance = ts * 9 / 6 / 58
if unit == 0:
return distance
elif unit == 1:
return distance / 254
if __name__ == "__main__":
dis = DISTANCE(pin8)
while 1:
print(dis.get_distance())
sleep(500)