Skip to content

Commit c0ba686

Browse files
committed
增加乐动模块IRObstacle 驱动
1 parent 80e4e3e commit c0ba686

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

port/boards/mpython/modules/mpython.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,3 +1391,26 @@ def numberMap(inputNum, bMin, bMax, cMin, cMax):
13911391
outputNum = 0
13921392
outputNum = ((cMax - cMin) / (bMax - bMin)) * (inputNum - bMin) + cMin
13931393
return outputNum
1394+
1395+
1396+
class IRObstacle():
1397+
def __init__(self, pin):
1398+
'''初始化参数,引脚'''
1399+
self.pin = MPythonPin(pin, PinMode.ANALOG)
1400+
self.threshold = 1500 #默认阈值
1401+
1402+
def detect(self):
1403+
'''是否探测到,布尔类型True/False'''
1404+
tmp = self.pin.read_analog()
1405+
if(tmp<=self.threshold):
1406+
return True
1407+
else:
1408+
return False
1409+
1410+
def get_raw_val(self):
1411+
'''获取红外探测传感器裸数据,模拟值'''
1412+
return self.pin.read_analog()
1413+
1414+
def set_threshold(self, threshold):
1415+
'''设置红外探测传感器阈值,模拟值'''
1416+
self.threshold = threshold

0 commit comments

Comments
 (0)