Merge pull request #7 from Trivernis/develop

More Cleanup
pull/11/head
Trivernis 6 years ago committed by GitHub
commit 8c518929c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,23 +21,10 @@ class Joystick:
self.pressed = [] self.pressed = []
self.splaying = False self.splaying = False
def handle(self): def _handle_buttons(self, axis_dict, c):
c = self.configuration
nav = self.navigator
dict = {}
# axes
for i in range(self.joystick.get_numaxes()):
axis = self.joystick.get_axis(i)
dict['Axis {}'.format(i)] = axis
if i == c['GAS'] and axis > 0.1:
nav.forward()
elif i == c['GAS'] and axis < 0.1:
nav.stop()
# buttons
for i in range(self.joystick.get_numbuttons()): for i in range(self.joystick.get_numbuttons()):
button = self.joystick.get_button(i) button = self.joystick.get_button(i)
dict['Button {}'.format(i)] = button axis_dict['Button {}'.format(i)] = button
if i == c['LIGHT'] and button == 1 and i not in self.pressed: if i == c['LIGHT'] and button == 1 and i not in self.pressed:
self.light.switch() self.light.switch()
@ -73,11 +60,26 @@ class Joystick:
self.pressed.append(i) self.pressed.append(i)
elif i == c['REC'] and button == 0 and i in self.pressed: elif i == c['REC'] and button == 0 and i in self.pressed:
self.pressed.remove(i) self.pressed.remove(i)
return axis_dict
def handle(self):
c = self.configuration
nav = self.navigator
axis_dict = {}
# axes
for i in range(self.joystick.get_numaxes()):
axis = self.joystick.get_axis(i)
axis_dict['Axis {}'.format(i)] = axis
if i == c['GAS'] and axis > 0.1:
nav.forward()
elif i == c['GAS'] and axis < 0.1:
nav.stop()
axis_dict = self._handle_buttons(axis_dict, c)
# hats # hats
for i in range(self.joystick.get_numhats()): for i in range(self.joystick.get_numhats()):
hat = self.joystick.get_hat(i) hat = self.joystick.get_hat(i)
dict['Hat {}'.format(i)] = hat axis_dict['Hat {}'.format(i)] = hat
if hat == (-1, 0): if hat == (-1, 0):
nav.left() nav.left()
@ -86,8 +88,8 @@ class Joystick:
else: else:
nav.straight() nav.straight()
dict['Volume'] = self.sound.get_volume() axis_dict['Volume'] = self.sound.get_volume()
return dict return axis_dict
def _save_camimg(self): def _save_camimg(self):
self.camera.new_frame() self.camera.new_frame()

@ -2,12 +2,11 @@ import picamera
import picamera.array import picamera.array
import pygame import pygame
import pygame.camera import pygame.camera
from pygame import *
def render_text_line(image, color, font, text, pos=(0, 0)): def render_text_line(rimage, rcolor, rfont, text, pos=(0, 0)):
render_text = font.render(text, 1, color) render_text = rfont.render(text, 1, rcolor)
image.blit(render_text, pos) rimage.blit(render_text, pos)
class Screen: class Screen:
@ -20,7 +19,8 @@ class Screen:
self.screen = pygame.display.set_mode(self.size) self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption(title) pygame.display.set_caption(title)
def refresh(self, rectangles=None): @staticmethod
def refresh(rectangles=None):
pygame.display.update(rectangles) pygame.display.update(rectangles)
def toggle_fullscreen(self): def toggle_fullscreen(self):
@ -30,7 +30,7 @@ class Screen:
else: else:
displayinfo = pygame.display.Info() displayinfo = pygame.display.Info()
fullsize = (displayinfo.current_w, displayinfo.current_h) fullsize = (displayinfo.current_w, displayinfo.current_h)
pygame.display.set_mode(fullsize, FULLSCREEN | DOUBLEBUF) pygame.display.set_mode(fullsize, pygame.FULLSCREEN | pygame.DOUBLEBUF)
class List(pygame.sprite.Sprite): class List(pygame.sprite.Sprite):
@ -45,12 +45,12 @@ class List(pygame.sprite.Sprite):
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.topleft = position self.rect.topleft = position
self.font = pygame.font.SysFont('Arial', 25) self.font = pygame.font.SysFont('Arial', 25)
self.dict = {} self.display_dict = {}
self.updated = True self.updated = True
self.txtsize = self.font.size('__||__') self.txtsize = self.font.size('__||__')
def set_dict(self, dict): def set_dict(self, dispdict):
self.dict = dict self.display_dict = dispdict
self.updated = True self.updated = True
def update(self): def update(self):
@ -58,8 +58,8 @@ class List(pygame.sprite.Sprite):
height = 0 height = 0
self.image.fill((0, 0, 0)) self.image.fill((0, 0, 0))
for key in self.dict.keys(): for dictkey in self.display_dict.keys():
line = '{}: {}'.format(key, self.dict[key]) line = '{}: {}'.format(dictkey, self.display_dict[dictkey])
render_text_line(self.image, (255, 255, 255), self.font, line, (0, height)) render_text_line(self.image, (255, 255, 255), self.font, line, (0, height))
height += self.txtsize[1] height += self.txtsize[1]

@ -17,8 +17,8 @@ def read_byte(reg):
def read_word(reg): def read_word(reg):
h = bus.read_byte_data(address, reg) h = bus.read_byte_data(address, reg)
l = bus.read_byte_data(address, reg + 1) r_data = bus.read_byte_data(address, reg + 1)
value = (h << 8) + l value = (h << 8) + r_data
return value return value
@ -44,7 +44,7 @@ def get_x_rotation(x, y, z):
return math.degrees(radians) return math.degrees(radians)
def getAllOut(): def get_all_gyro_data():
acceleration_xout = read_word_2c(0x3b) acceleration_xout = read_word_2c(0x3b)
acceleration_yout = read_word_2c(0x3d) acceleration_yout = read_word_2c(0x3d)
acceleration_zout = read_word_2c(0x3f) acceleration_zout = read_word_2c(0x3f)

@ -36,7 +36,7 @@ class Navigator:
class Light: class Light:
"""Light switched with a relais""" """Light switched with a relays"""
def __init__(self, lightpin): def __init__(self, lightpin):
self.pin = lightpin self.pin = lightpin
@ -72,18 +72,23 @@ class Ultrasonic:
return self.distance return self.distance
def __del__(self): def __del__(self):
self.sensor.clean() self.sensor.cleanup()
class Temperature: class Temperature:
"""A temperature sensor""" """A temperature sensor"""
def get_Temperature(self): def __init__(self):
pass
@staticmethod
def get_temperature():
outp = check_output(['python', '-u', './lib/thermolib.py']).decode('ISO-8859-1') outp = check_output(['python', '-u', './lib/thermolib.py']).decode('ISO-8859-1')
temp, hum = outp.split('|') temp, hum = outp.split('|')
return temp return temp
def get_Humidity(self): @staticmethod
def get_humidity():
outp = check_output(['python', '-u', './lib/thermolib.py']).decode('ISO-8859-1') outp = check_output(['python', '-u', './lib/thermolib.py']).decode('ISO-8859-1')
temp, hum = outp.split('|') temp, hum = outp.split('|')
return hum return hum

@ -17,7 +17,7 @@ class Motor(object):
GPIO.setup(p, GPIO.OUT) GPIO.setup(p, GPIO.OUT)
GPIO.output(p, 0) GPIO.output(p, 0)
def __exit__(self, type, value, traceback): def __exit__(self, type_of_motor, value, traceback):
self.clean_pins_up() self.clean_pins_up()
def _set_rpm(self, rpm): def _set_rpm(self, rpm):
@ -54,7 +54,7 @@ class Motor(object):
def _move_acw(self, big_steps): def _move_acw(self, big_steps):
self.clean_pins_up() self.clean_pins_up()
for i in range(big_steps): for _ in range(big_steps):
GPIO.output(self.P1, 0) GPIO.output(self.P1, 0)
sleep(self._T) sleep(self._T)
GPIO.output(self.P3, 1) GPIO.output(self.P3, 1)
@ -78,7 +78,7 @@ class Motor(object):
GPIO.output(self.P2, 0) GPIO.output(self.P2, 0)
GPIO.output(self.P3, 0) GPIO.output(self.P3, 0)
GPIO.output(self.P4, 0) GPIO.output(self.P4, 0)
for i in range(big_steps): for _ in range(big_steps):
GPIO.output(self.P3, 0) GPIO.output(self.P3, 0)
sleep(self._T) sleep(self._T)
GPIO.output(self.P1, 1) GPIO.output(self.P1, 1)

@ -5,13 +5,11 @@ GPIO.setmode(GPIO.BOARD)
class Sensor(object): class Sensor(object):
def __init__(self): def __init__(self, trigger, echo):
self.TRIGGER = TRIG self.TRIGGER = trigger
self.ECHO = ECHO self.ECHO = echo
self.lastValues = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] self.lastValue = 0
self.lastValues = self.lastValues[1:] self.lastValuesCount = 1
def init(self, TRIG, ECHO):
GPIO.setup(self.TRIGGER, GPIO.OUT) GPIO.setup(self.TRIGGER, GPIO.OUT)
GPIO.setup(self.ECHO, GPIO.IN) GPIO.setup(self.ECHO, GPIO.IN)
GPIO.output(self.TRIGGER, False) GPIO.output(self.TRIGGER, False)
@ -25,12 +23,10 @@ class Sensor(object):
abs_start = time.time() abs_start = time.time()
while GPIO.input(self.ECHO) == 0 and (time.time() - abs_start) < 0.02: while GPIO.input(self.ECHO) == 0 and (time.time() - abs_start) < 0.02:
# print(time.time()-abs_start)
pass pass
pulse_start = time.time() pulse_start = time.time()
while GPIO.input(self.ECHO) == 1 and (time.time() - abs_start) < 0.02: while GPIO.input(self.ECHO) == 1 and (time.time() - abs_start) < 0.02:
# print(time.time()-abs_start)
pass pass
pulse_end = time.time() pulse_end = time.time()
@ -38,11 +34,11 @@ class Sensor(object):
distance = pulse_duration * 17150 distance = pulse_duration * 17150
distance = round(distance, 2) distance = round(distance, 2)
self.lastValues.append(distance) lastc = self.lastValuesCount
distance = round((sum(self.lastValues)) / (len(self.lastValues)), 2) self.lastValue = (1/lastc) * distance + ((lastc-1)/lastc) * self.lastValue
# print(self.lastValues) distance = self.lastValue
# print("Distance: {}".format(distance))
return distance return distance
def clean(self): @staticmethod
def cleanup():
GPIO.cleanup() GPIO.cleanup()

@ -24,8 +24,8 @@ def main():
# pygame stuff # pygame stuff
screen = graphiclib.Screen(size=(1000, 1000)) screen = graphiclib.Screen(size=(1000, 1000))
all_sprites = pygame.sprite.RenderUpdates() all_sprites = pygame.sprite.RenderUpdates()
list = graphiclib.List((0, 0), (500, 1000)) glist = graphiclib.List((0, 0), (500, 1000))
all_sprites.add(list) all_sprites.add(glist)
all_sprites.add(camera) all_sprites.add(camera)
clock = pygame.time.Clock() clock = pygame.time.Clock()
running = True running = True
@ -38,9 +38,9 @@ def main():
print('quit event') print('quit event')
running = False running = False
dict = jstick.handle() gdict = jstick.handle()
dict['Distance'] = ultrasonic.get_distance() gdict['Distance'] = ultrasonic.get_distance()
list.set_dict(dict) glist.set_dict(gdict)
all_sprites.update() all_sprites.update()
update_rects = all_sprites.draw(screen.screen) update_rects = all_sprites.draw(screen.screen)
screen.refresh(rectangles=update_rects) screen.refresh(rectangles=update_rects)

@ -4,4 +4,4 @@ sensor = ultrasonic.Sensor()
sensor.init(11, 7) sensor.init(11, 7)
print('Beginn der Messung') print('Beginn der Messung')
print(sensor.echo()) print(sensor.echo())
sensor.clean() sensor.cleanup()

Loading…
Cancel
Save