Add IRWrapper and change IRSensor logic

This commit is contained in:
Konstantin Lampalzer 2021-03-28 16:39:54 +02:00
parent 3101e56252
commit e94d735e24
No known key found for this signature in database
GPG key ID: 9A60A522835A2AD9
5 changed files with 235 additions and 17 deletions

View file

@ -3,6 +3,8 @@ import pigpio
from compLib.ADC import ADC
from compLib.LogstashLogging import Logging
import spidev
import threading
import time
TOP_LEFT_CHANNEL = 0
TOP_RIGHT_CHANNEL = 1
@ -14,6 +16,10 @@ BOTTOM_LEFT_PIN = 14
BOTTOM_MIDDLE_PIN = 15
BOTTOM_RIGHT_PIN = 23
BOTTOM_LEFT_CHANNEL = 2
BOTTOM_MIDDLE_CHANNEL = 1
BOTTOM_RIGHT_CHANNEL = 0
adc = ADC()
spi = spidev.SpiDev()
spi.open(0, 0)
@ -28,18 +34,46 @@ GPIO.set_mode(BOTTOM_MIDDLE_PIN, pigpio.INPUT)
GPIO.set_mode(BOTTOM_RIGHT_PIN, pigpio.INPUT)
states = {
str(BOTTOM_LEFT_PIN): GPIO.read(BOTTOM_LEFT_PIN),
str(BOTTOM_MIDDLE_PIN): GPIO.read(BOTTOM_MIDDLE_PIN),
str(BOTTOM_RIGHT_PIN): GPIO.read(BOTTOM_RIGHT_PIN)}
str(BOTTOM_LEFT_PIN): GPIO.read(BOTTOM_LEFT_PIN),
str(BOTTOM_MIDDLE_PIN): GPIO.read(BOTTOM_MIDDLE_PIN),
str(BOTTOM_RIGHT_PIN): GPIO.read(BOTTOM_RIGHT_PIN)}
analog_states = {
str(BOTTOM_LEFT_CHANNEL): 0,
str(BOTTOM_MIDDLE_CHANNEL): 0,
str(BOTTOM_RIGHT_CHANNEL): 0}
def gpio_callback(gpio, level, tick):
states[str(gpio)] = level
# print(gpio, level, tick)
GPIO.callback(BOTTOM_LEFT_PIN, pigpio.EITHER_EDGE, gpio_callback)
GPIO.callback(BOTTOM_MIDDLE_PIN, pigpio.EITHER_EDGE, gpio_callback)
GPIO.callback(BOTTOM_RIGHT_PIN, pigpio.EITHER_EDGE, gpio_callback)
def read_analog_channel(channel):
spi.writebytes([channel << 3, 0])
time.sleep(0.0033) # 300HZ
bytes = spi.readbytes(2)
return (bytes[0] * 256 + bytes[1]) >> 2
def sensor_thread():
while True:
analog_states[str(BOTTOM_LEFT_CHANNEL)] = read_analog_channel(BOTTOM_LEFT_CHANNEL)
analog_states[str(BOTTOM_MIDDLE_CHANNEL)] = read_analog_channel(BOTTOM_MIDDLE_CHANNEL)
analog_states[str(BOTTOM_RIGHT_CHANNEL)] = read_analog_channel(BOTTOM_RIGHT_CHANNEL)
analog_thread = threading.Thread(target=sensor_thread, daemon=True)
analog_thread.start()
time.sleep(0.25)
class IRSensor(object):
"""Access the different IR Sensors at top / bottom of the robot
"""
@ -101,10 +135,7 @@ class IRSensor(object):
:return: 10-bit brightness value
:rtype: int
"""
channel = 2
spi.writebytes([channel << 3, 0])
bytes = spi.readbytes(2)
return (bytes[0] * 256 + bytes[1]) >> 2
return analog_states[str(BOTTOM_LEFT_CHANNEL)]
@staticmethod
def bottom_middle_analog() -> int:
@ -113,10 +144,7 @@ class IRSensor(object):
:return: 10-bit brightness value
:rtype: int
"""
channel = 1
spi.writebytes([channel << 3, 0])
bytes = spi.readbytes(2)
return (bytes[0] * 256 + bytes[1]) >> 2
return analog_states[str(BOTTOM_MIDDLE_CHANNEL)]
@staticmethod
def bottom_right_analog() -> int:
@ -125,7 +153,5 @@ class IRSensor(object):
:return: 10-bit brightness value
:rtype: int
"""
channel = 0
spi.writebytes([channel << 3, 0])
bytes = spi.readbytes(2)
return (bytes[0] * 256 + bytes[1]) >> 2
return analog_states[str(BOTTOM_RIGHT_CHANNEL)]