73 lines
1.7 KiB
Python
73 lines
1.7 KiB
Python
import RPi.GPIO as GPIO
|
|
|
|
from compLib.ADC import ADC
|
|
|
|
TOP_LEFT_CHANNEL = 0
|
|
TOP_RIGHT_CHANNEL = 1
|
|
|
|
TOP_IR_MIN_VOLTAGE = 0.0
|
|
TOP_IR_MAX_VOLTAGE = 3.3
|
|
|
|
BOTTOM_LEFT_PIN = 14
|
|
BOTTOM_MIDDLE_PIN = 15
|
|
BOTTOM_RIGHT_PIN = 23
|
|
|
|
adc = ADC()
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
GPIO.setup(BOTTOM_LEFT_PIN, GPIO.IN)
|
|
GPIO.setup(BOTTOM_MIDDLE_PIN, GPIO.IN)
|
|
GPIO.setup(BOTTOM_RIGHT_PIN, GPIO.IN)
|
|
|
|
|
|
class IRSensor(object):
|
|
"""Access the different IR Sensors at top / bottom of the robot
|
|
"""
|
|
|
|
@staticmethod
|
|
def top_left_percent() -> int:
|
|
"""Get top left infrared sensor percentage
|
|
|
|
:return: Percentage between 0 and 100
|
|
:rtype: int
|
|
"""
|
|
voltage = adc.read(TOP_LEFT_CHANNEL)
|
|
return int((voltage - TOP_IR_MIN_VOLTAGE) / TOP_IR_MAX_VOLTAGE * 100)
|
|
|
|
@staticmethod
|
|
def top_right_percent() -> int:
|
|
"""Get top right infrared sensor percentage
|
|
|
|
:return: Percentage between 0 and 100
|
|
:rtype: int
|
|
"""
|
|
voltage = adc.read(TOP_RIGHT_CHANNEL)
|
|
return int((voltage - TOP_IR_MIN_VOLTAGE) / TOP_IR_MAX_VOLTAGE * 100)
|
|
|
|
@staticmethod
|
|
def bottom_left() -> bool:
|
|
"""Get bottom left infrared sensor status
|
|
|
|
:return: True, if sensor detects IR signals
|
|
:rtype: bool
|
|
"""
|
|
return GPIO.input(BOTTOM_LEFT_PIN)
|
|
|
|
@staticmethod
|
|
def bottom_middle() -> bool:
|
|
"""Get bottom middle infrared sensor status
|
|
|
|
:return: True, if sensor detects IR signals
|
|
:rtype: bool
|
|
"""
|
|
return GPIO.input(BOTTOM_MIDDLE_PIN)
|
|
|
|
@staticmethod
|
|
def bottom_right() -> bool:
|
|
"""Get bottom right infrared sensor status
|
|
|
|
:return: True, if sensor detects IR signals
|
|
:rtype: bool
|
|
"""
|
|
return GPIO.input(BOTTOM_RIGHT_PIN)
|