This repository has been archived on 2025-06-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compLIB/compLib/IRSensor.py
Konstantin Lampalzer 8f2944da71 Documentation
2021-09-12 13:42:55 +02:00

58 lines
No EOL
1.7 KiB
Python

from compLib.LogstashLogging import Logging
from compLib.MetricsLogging import MetricsLogging
from compLib.Spi import Spi, Register
SENSOR_COUNT = 5
class IRSensor(object):
"""Access the different IR Sensors of the robot
"""
@staticmethod
def read(sensor: int) -> int:
"""Read one infrared sensor
:param sensor: Which sensor to read. Between 1 and 5
:raises: IndexError
:return: Sensor value. 10 bit accuracy
:rtype: int
"""
if sensor <= 0 or sensor > SENSOR_COUNT:
raise IndexError("Invalid sensor specified!")
result = 0
if sensor == 1:
result = Spi.read(Register.IR_1_H, 2)
elif sensor == 2:
esult = Spi.read(Register.IR_2_H, 2)
elif sensor == 3:
esult = Spi.read(Register.IR_3_H, 2)
elif sensor == 4:
esult = Spi.read(Register.IR_4_H, 2)
elif sensor == 5:
esult = Spi.read(Register.IR_5_H, 2)
MetricsLogging.put("Infrared", result, sensor)
return result
@staticmethod
def set(sensor: int, on: bool):
"""Turn on / off a IR emitter
:param sensor: Which sensor to read. Between 1 and 5
:raises: IndexError
"""
if sensor <= 0 or sensor > SENSOR_COUNT:
raise IndexError("Invalid sensor specified!")
if sensor == 1:
Spi.write(Register.IR_1_LED, 1, on)
elif sensor == 2:
Spi.write(Register.IR_2_LED, 1, on)
elif sensor == 3:
Spi.write(Register.IR_3_LED, 1, on)
elif sensor == 4:
Spi.write(Register.IR_4_LED, 1, on)
elif sensor == 5:
Spi.write(Register.IR_5_LED, 1, on)