From cbcd4cf93784d33c79606eb830d41ebce48a1f2d Mon Sep 17 00:00:00 2001 From: Julius Riegel Date: Sat, 13 Jan 2018 19:49:51 +0100 Subject: [PATCH] Added Ultrasonic Sensor Ultrasonic sensor implemented and now in use. --- .idea/workspace.xml | 79 +++++++++++++++++++++++---------------------- install.sh | 4 +-- lib/hardwarelib.py | 9 ++++-- main.py | 2 ++ 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d7fb9dd..885147c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,9 @@ - - + + + @@ -482,14 +483,6 @@ - - - - - - - - @@ -512,41 +505,49 @@ - + - - + + - + - + + + + + + + + + - - - - + + - + - - + + - + - + - - - + + + + + diff --git a/install.sh b/install.sh index af6a0dd..531fe66 100644 --- a/install.sh +++ b/install.sh @@ -3,8 +3,8 @@ sudo apt update sudo apt upgrade sudo apt dist-upgrade sudo pip3 install pygame -sudo apt install python-smbus -sudo apt install i2c-tools +sudo apt-get install -y python-smbus +sudo apt-get install -y i2c-tools sudo pip install adafruit-pca9685 sudo apt install vsftpd sudo apt install vlc diff --git a/lib/hardwarelib.py b/lib/hardwarelib.py index 51abee8..f836b20 100644 --- a/lib/hardwarelib.py +++ b/lib/hardwarelib.py @@ -1,6 +1,7 @@ from subprocess import call, check_output from lib import ultrasonic import RPi.GPIO as GPIO +import time class Navigator(): @@ -61,10 +62,14 @@ class Ultrasonic: def __init__(self, trigger, echo): self.sensor = ultrasonic.Sensor() self.sensor.init(trigger, echo) + self.time = 0 + self.distance = 0 def get_distance(self): - distance = self.sensor.echo() - return distance + if (time.time()-self.time)>1: + self.distance = self.sensor.echo() + self.time = time.time() + return self.distance def __del__(self): self.sensor.clean() diff --git a/main.py b/main.py index 5a3196f..6c38622 100644 --- a/main.py +++ b/main.py @@ -29,6 +29,7 @@ def main(): running = True while running: + clock.tick(25) for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -36,6 +37,7 @@ def main(): running = False dict = jstick.handle() + dict['Distance'] = ultrasonic.get_distance() list.set_dict(dict) all_sprites.update() update_rects = all_sprites.draw(screen.screen)