def __init__(self, position, gpio_trigger, gpio_echo): GPIO.setmode(GPIO.BCM) self.__position = position self.__front_distance = int(0) self.__gpio_trigger = gpio_trigger self.__gpio_echo = gpio_echo self.__distance = int(0) self.__run = True GPIO.setup(gpio_trigger, GPIO.OUT) GPIO.setup(gpio_echo, GPIO.IN) GPIO.output(gpio_trigger, GPIO.LOW) Thread(target=self.measure).start()
def _get_distance(self): if settings.USE_EMULATOR: return random.randint(settings.DISTANCE_LOW, settings.DISTANCE_HIGH) GPIO.output(self.__gpio_trigger, True) time.sleep(0.00001) GPIO.output(self.__gpio_trigger, False) start = time.time() """Les deux boucles hyper bizarre ici ont un sens, en fait quand on envoi le signal, input est HIGH Donc dès que le signal part le start time se fixe car on sort de la boucle Puis dès que le signal revient, l'input devient LOW, et donc le stop time se fixe dès que le rebound est detecte par ECHO""" while GPIO.input(self.__gpio_echo) == 0: start = time.time() while GPIO.input(self.__gpio_echo) == 1: stop = time.time() # Calculate pulse length elapsed = stop - start # Distance pulse travelled in that time is time # multiplied by the speed of sound (cm/s) # That was the distance there and back so halve the value distance = elapsed * 34000 / 2 return distance
import threading import settings from kivy.app import App from packages.robot import robot from emulator.RPi.GPIO import GPIO from emulator.interface.application import EmulatorApp if __name__ == "__main__": # todo parse arguments a = robot.Robot(True) app = EmulatorApp(a, True) try: app.run() finally: a.kill() app.stop() GPIO.cleanup()
from emulator.RPi.GPIO import GPIO """ Emulator that was used to emulate GPIO of the pi for dev purposes only """ if __name__ == "__main__": GPIO.setmode(GPIO.BCM)
# -*- coding:utf-8 -*- import threading import settings from packages.robot.robot import Robot from packages.logger.logger import LOG if settings.USE_EMULATOR: from emulator.RPi.GPIO import GPIO else: import RPi.GPIO as GPIO if __name__ == "__main__": # todo parse arguments GPIO.setmode(GPIO.BCM) a = Robot(True) try: a.run() except Exception as e: LOG("An unexcpeted error occurs") LOG(e) finally: a.kill() GPIO.cleanup()