From a544133e927985a55a903d5ab66e651962f6c9e2 Mon Sep 17 00:00:00 2001 From: Konstantin Lampalzer Date: Sat, 23 Jan 2021 11:55:56 +0000 Subject: [PATCH] Change IR Sensor, fix motor direction --- compLib/IRSensor.py | 33 +++++++++++++++++++++++++-------- compLib/Motor.py | 6 +++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/compLib/IRSensor.py b/compLib/IRSensor.py index 0477c7d..b6fe55d 100644 --- a/compLib/IRSensor.py +++ b/compLib/IRSensor.py @@ -1,4 +1,4 @@ -import RPi.GPIO as GPIO +import pigpio from compLib.ADC import ADC from compLib.LogstashLogging import Logging @@ -15,12 +15,26 @@ BOTTOM_RIGHT_PIN = 23 adc = ADC() -GPIO.setmode(GPIO.BCM) +# 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) +GPIO = pigpio.pi() +GPIO.set_mode(BOTTOM_LEFT_PIN, pigpio.INPUT) +GPIO.set_mode(BOTTOM_MIDDLE_PIN, pigpio.INPUT) +GPIO.set_mode(BOTTOM_RIGHT_PIN, pigpio.INPUT) + +states = { + str(BOTTOM_LEFT_PIN): 0, + str(BOTTOM_MIDDLE_PIN): 0, + str(BOTTOM_RIGHT_PIN): 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) class IRSensor(object): """Access the different IR Sensors at top / bottom of the robot @@ -53,7 +67,8 @@ class IRSensor(object): :return: True, if sensor detects IR signals :rtype: bool """ - return GPIO.input(BOTTOM_LEFT_PIN) + return states[str(BOTTOM_LEFT_PIN)] + # return GPIO.read(BOTTOM_LEFT_PIN) @staticmethod def bottom_middle() -> bool: @@ -62,7 +77,8 @@ class IRSensor(object): :return: True, if sensor detects IR signals :rtype: bool """ - return GPIO.input(BOTTOM_MIDDLE_PIN) + return states[str(BOTTOM_MIDDLE_PIN)] + # return GPIO.read(BOTTOM_MIDDLE_PIN) @staticmethod def bottom_right() -> bool: @@ -71,4 +87,5 @@ class IRSensor(object): :return: True, if sensor detects IR signals :rtype: bool """ - return GPIO.input(BOTTOM_RIGHT_PIN) + return states[str(BOTTOM_RIGHT_PIN)] + # return GPIO.read(BOTTOM_RIGHT_PIN) diff --git a/compLib/Motor.py b/compLib/Motor.py index 73fd1b3..8c2556e 100644 --- a/compLib/Motor.py +++ b/compLib/Motor.py @@ -39,11 +39,11 @@ class Motor(object): adjusted_speed = int(min(max(0, percent), 100) * MOTOR_PERCENTAGE_MULT) if forward: - pwm.setMotorPwm(port * 2, 0) - pwm.setMotorPwm(port * 2 + 1, adjusted_speed) - else: pwm.setMotorPwm(port * 2, adjusted_speed) pwm.setMotorPwm(port * 2 + 1, 0) + else: + pwm.setMotorPwm(port * 2, 0) + pwm.setMotorPwm(port * 2 + 1, adjusted_speed) @staticmethod def all_off():