diff --git a/.gitignore b/.gitignore
index ee64372..325dc55 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
-.idea/
+.idea*
*.pyc
diff --git a/.idea/für Robotototo.iml b/.idea/für Robotototo.iml
deleted file mode 100644
index 2be6dbc..0000000
--- a/.idea/für Robotototo.iml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index cb2632a..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index aae3f5e..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index ec35bbc..0000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,458 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Buildout
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1501667906814
-
-
- 1501667906814
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Manuals etc/TODO.txt b/Manuals etc/TODO.txt
index 81ecd50..3d92a51 100644
--- a/Manuals etc/TODO.txt
+++ b/Manuals etc/TODO.txt
@@ -1,7 +1,7 @@
--Motoren--
3V an VCC, 5V an V+
-5V aus externer Quelle, nicht PI -> zu große Schwankungen durch Servomotor
-Schrittmotoren für Räder, Servomotoren für Zusatz
+5V aus externer Quelle, nicht PI -> zu große Schwankungen durch Servomotor
+Schrittmotoren für Räder, Servomotoren für Zusatz
--Gyro--
Messung von Lage und Beschleunigung
@@ -13,7 +13,7 @@ zur Erstellung von 3d Raumkarte (Experimentell)
kann an Schrittmotor angeschlossen werden -> zur Drehung
--Lautsprecher--
-Benötigt für Soundwiedergabe
+Benötigt für Soundwiedergabe
--Controller--
-Input von Gerät zu Gerät verschieden
\ No newline at end of file
+Input von Gerät zu Gerät verschieden
\ No newline at end of file
diff --git a/lib/controllib.py b/lib/controllib.py
index a3398df..781b53b 100644
--- a/lib/controllib.py
+++ b/lib/controllib.py
@@ -1,13 +1,15 @@
-import pygame, os
+import os
+import pygame
+
class Joystick():
def __init__(self, navigator, light, configuration, camera):
- #joystick
+ # joystick
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
- #sound
+ # sound
pygame.mixer.init()
sound = pygame.mixer.Sound('./sounds/gasgasgas.wav')
sound.set_volume(0.3)
@@ -24,27 +26,27 @@ class Joystick():
c = self.configuration
nav = self.navigator
dict = {}
- #axes
+ # axes
for i in range(self.joystick.get_numaxes()):
axis = self.joystick.get_axis(i)
- dict['Axis {}'.format(i)]=axis
+ dict['Axis {}'.format(i)] = axis
- if i==c['GAS'] and axis>0.1:
+ if i == c['GAS'] and axis > 0.1:
nav.forward()
- elif i==c['GAS'] and axis<0.1:
+ elif i == c['GAS'] and axis < 0.1:
nav.stop()
- #buttons
+ # buttons
for i in range(self.joystick.get_numbuttons()):
button = self.joystick.get_button(i)
dict['Button {}'.format(i)] = button
- if i==c['LIGHT'] and button==1 and button not in self.pressed:
+ if i == c['LIGHT'] and button == 1 and button not in self.pressed:
self.light.switch()
self.pressed.append(i)
- elif i==c['LIGHT'] and button==0 and button in self.pressed:
+ elif i == c['LIGHT'] and button == 0 and button in self.pressed:
self.pressed.remove(i)
- elif i==c['MUSIC'] and button==1 and button not in self.pressed:
+ elif i == c['MUSIC'] and button == 1 and button not in self.pressed:
if self.splaying:
self.sound.stop()
self.splaying = False
@@ -52,35 +54,35 @@ class Joystick():
self.sound.play()
self.splaying = True
self.pressed.append(i)
- elif i==c['MUSIC'] and button==0 and button in self.pressed:
+ elif i == c['MUSIC'] and button == 0 and button in self.pressed:
self.pressed.remove(i)
- elif i==c['VOLIN'] and button==1 and button not in self.pressed:
- self.sound.set_volume(self.sound.get_volume()+0.1)
+ elif i == c['VOLIN'] and button == 1 and button not in self.pressed:
+ self.sound.set_volume(self.sound.get_volume() + 0.1)
self.pressed.append(i)
- elif i==c['VOLIN'] and button==0 and button in self.pressed:
+ elif i == c['VOLIN'] and button == 0 and button in self.pressed:
self.sound.set_volume(self.sound.get_volume() - 0.1)
self.pressed.remove(i)
- elif i==c['VOLDE'] and button==1 and button not in self.pressed:
+ elif i == c['VOLDE'] and button == 1 and button not in self.pressed:
self.pressed.append(i)
- elif i==c['VOLDE'] and button==0 and button in self.pressed:
+ elif i == c['VOLDE'] and button == 0 and button in self.pressed:
self.pressed.remove(i)
- elif i==c['REC'] and button==1 and button not in self.pressed:
+ elif i == c['REC'] and button == 1 and button not in self.pressed:
self._save_camimg()
self.pressed.append(i)
- elif i==c['REC'] and button==0 and button in self.pressed:
+ elif i == c['REC'] and button == 0 and button in self.pressed:
self.pressed.remove(i)
- #hats
+ # hats
for i in range(self.joystick.get_numhats()):
hat = self.joystick.get_hat(i)
dict['Hat {}'.format(i)] = hat
- if hat==(-1,0):
+ if hat == (-1, 0):
nav.left()
- elif hat==(1,0):
+ elif hat == (1, 0):
nav.right()
else:
nav.straight()
@@ -88,12 +90,11 @@ class Joystick():
dict['Volume'] = self.sound.get_volume()
return dict
-
def _save_camimg(self):
img = self.camera.image
if os.path.isfile('image.jpg'):
- count=0
+ count = 0
while os.path.isfile('./images/img{}.jpg'.format(count)):
- count+=1
+ count += 1
os.rename('image.jpg', 'images/img{}.jpg'.format(count))
- pygame.image.save(img, 'image.jpg')
\ No newline at end of file
+ pygame.image.save(img, 'image.jpg')
diff --git a/lib/graphiclib.py b/lib/graphiclib.py
index e056a3b..6c5ebb5 100644
--- a/lib/graphiclib.py
+++ b/lib/graphiclib.py
@@ -1,24 +1,25 @@
-import pygame, pygame.camera
+import pygame
+import pygame.camera
from pygame import *
-def render_text_line(image, color, font, text, pos = (0,0)):
+
+def render_text_line(image, color, font, text, pos=(0, 0)):
render_text = font.render(text, 1, color)
image.blit(render_text, pos)
+
class Screen:
"""The Screen for the Terminal"""
- def __init__(self, size = (100,100), title = "Screen"):
+ def __init__(self, size=(100, 100), title="Screen"):
pygame.display.init()
self.size = size
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption(title)
-
def refresh(self, rectangles=None):
pygame.display.update(rectangles)
-
def toggle_fullscreen(self):
if self.fullscreen:
pygame.display.set_mode(self.size)
@@ -38,30 +39,30 @@ class List(pygame.sprite.Sprite):
pygame.font.init()
self.size = size
self.image = pygame.Surface(self.size)
- self.image.fill((0,0,0))
+ self.image.fill((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.topleft = position
- self.font = pygame.font.SysFont('Arial',25)
+ self.font = pygame.font.SysFont('Arial', 25)
self.dict = {}
self.updated = True
self.txtsize = self.font.size('__||__')
-
def set_dict(self, dict):
self.dict = dict
self.updated = True
- def update(self, *args):
+ def update(self):
if self.updated:
height = 0
for key in self.dict.keys():
line = '{}: {}'.format(key, self.dict[key])
- render_text_line(self.image, (255, 255, 255), self.font, line, (0,height))
- height+=self.txtsize[1]
+ render_text_line(self.image, (255, 255, 255), self.font, line, (0, height))
+ height += self.txtsize[1]
self.updated = False
+
class PiCamera(pygame.sprite.Sprite):
"""The Picamera as pygame cam"""
@@ -77,5 +78,5 @@ class PiCamera(pygame.sprite.Sprite):
self.rect = self.image.get_rect()
self.rect.topleft = position
- def update(self, *args):
- self.camera.get_image(self.image)
\ No newline at end of file
+ def update(self):
+ self.camera.get_image(self.image)
diff --git a/lib/gyro.py b/lib/gyro.py
index 61ed378..460b6aa 100644
--- a/lib/gyro.py
+++ b/lib/gyro.py
@@ -10,15 +10,18 @@ bus = smbus.SMBus(1)
address = 0x68
bus.write_byte_data(address, power_mgmt_1, 0)
+
def read_byte(reg):
return bus.read_byte_data(address, reg)
+
def read_word(reg):
h = bus.read_byte_data(address, reg)
l = bus.read_byte_data(address, reg + 1)
value = (h << 8) + l
return value
+
def read_word_2c(reg):
val = read_word(reg)
if (val >= 0x8000):
@@ -26,31 +29,35 @@ def read_word_2c(reg):
else:
return val
+
def dist(a, b):
return math.sqrt((a * a) + (b * b))
+
def get_y_rotation(x, y, z):
radians = math.atan2(x, dist(y, z))
return -math.degrees(radians)
+
def get_x_rotation(x, y, z):
radians = math.atan2(y, dist(x, z))
return math.degrees(radians)
-def getAllOut():
- gyroskop_xout = read_word_2c(0x43)
- gyroskop_yout = read_word_2c(0x45)
- gyroskop_zout = read_word_2c(0x47)
-
- beschleunigung_xout = read_word_2c(0x3b)
- beschleunigung_yout = read_word_2c(0x3d)
- beschleunigung_zout = read_word_2c(0x3f)
-
- beschleunigung_xout_skaliert = round(beschleunigung_xout / 16384.0,3)
- beschleunigung_yout_skaliert = round(beschleunigung_yout / 16384.0,3)
- beschleunigung_zout_skaliert = round(beschleunigung_zout / 16384.0,3)
- x_rotation = round(get_x_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert),0)
- y_rotation = round(get_y_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert),0)
-
- return {'acc':[beschleunigung_xout,beschleunigung_yout,beschleunigung_zout], 'acc_sca':[beschleunigung_xout_skaliert,beschleunigung_yout_skaliert,beschleunigung_zout_skaliert],'rot':[x_rotation,y_rotation]}
\ No newline at end of file
+def getAllOut():
+ acceleration_xout = read_word_2c(0x3b)
+ acceleration_yout = read_word_2c(0x3d)
+ acceleration_zout = read_word_2c(0x3f)
+
+ acceleration_xout_scaled = round(acceleration_xout / 16384.0, 3)
+ acceleration_yout_scaled = round(acceleration_yout / 16384.0, 3)
+ acceleration_zout_scaled = round(acceleration_zout / 16384.0, 3)
+
+ x_rotation = round(
+ get_x_rotation(acceleration_xout_scaled, acceleration_yout_scaled, acceleration_zout_scaled), 0)
+ y_rotation = round(
+ get_y_rotation(acceleration_xout_scaled, acceleration_yout_scaled, acceleration_zout_scaled), 0)
+
+ return {'acc': [acceleration_xout, acceleration_yout, acceleration_zout],
+ 'acc_sca': [acceleration_xout_scaled, acceleration_yout_scaled, acceleration_zout_scaled],
+ 'rot': [x_rotation, y_rotation]}
diff --git a/lib/hardwarelib.py b/lib/hardwarelib.py
index 513b06f..557ab56 100644
--- a/lib/hardwarelib.py
+++ b/lib/hardwarelib.py
@@ -2,6 +2,7 @@ from subprocess import call, check_output
from lib import ultrasonic
import RPi.GPIO as GPIO
+
class Navigator():
"""Forward Motor with relais, Steering with servo"""
@@ -11,29 +12,24 @@ class Navigator():
GPIO.setup(self.mrelpin, GPIO.OUT)
self.stop()
-
def left(self):
if self.steer != 'left':
call(['python', './lib/servolib.py', 'left'])
self.steer = 'left'
-
def right(self):
if self.steer != 'right':
call(['python', './lib/servolib.py', 'right'])
self.steer = 'right'
-
def straight(self):
if self.steer:
call(['python', './lib/servolib.py'])
self.steer = None
-
def forward(self):
GPIO.output(self.mrelpin, False)
-
def stop(self):
GPIO.output(self.mrelpin, True)
@@ -47,16 +43,13 @@ class Light:
GPIO.output(self.pin, True)
self.shine = False
-
def switch(self):
GPIO.output(self.pin, not self.shine)
self.shine = not self.shine
-
def switch_on(self):
GPIO.output(self.pin, False)
-
def switch_of(self):
GPIO.output(self.pin, True)
@@ -65,15 +58,13 @@ class Ultrasonic:
"""A ultrasonic sensor"""
def __init__(self, trigger, echo):
- self.sensor =ultrasonic.Sensor()
+ self.sensor = ultrasonic.Sensor()
self.sensor.init(trigger, echo)
-
def get_distance(self):
distance = self.sensor.echo()
return distance
-
def __del__(self):
self.sensor.clean()
@@ -82,12 +73,11 @@ class Temperature:
"""A temperature sensor"""
def get_Temperature(self):
- 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('|')
return temp
-
def get_Humidity(self):
- 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('|')
- return hum
\ No newline at end of file
+ return hum
diff --git a/lib/motors.py b/lib/motors.py
index 5b1c6bd..c2dead7 100644
--- a/lib/motors.py
+++ b/lib/motors.py
@@ -36,7 +36,6 @@ class Motor(object):
GPIO.output(self.P3, 0)
GPIO.output(self.P4, 0)
-
def move_to(self, angle):
"""Take the shortest route to a particular angle (degrees)."""
# Make sure there is a 1:1 mapping between angle and stepper angle
@@ -45,7 +44,7 @@ class Motor(object):
steps = (steps % self.steps_per_rev)
if steps > self.steps_per_rev / 2:
steps -= self.steps_per_rev
- print ("moving " + repr(steps) + " steps")
+ print("moving " + repr(steps) + " steps")
self._move_acw(-steps / 8)
else:
print("moving " + repr(steps) + " steps")
@@ -53,7 +52,6 @@ class Motor(object):
# self.step_angle = target_step_angle #in case you want to keep track of the position
self.step_angle = 0
-
def _move_acw(self, big_steps):
self.clean_pins_up()
for i in range(big_steps):
@@ -75,7 +73,6 @@ class Motor(object):
sleep(self._T)
self.clean_pins_up()
-
def _move_cw(self, big_steps):
GPIO.output(self.P1, 0)
GPIO.output(self.P2, 0)
@@ -100,18 +97,19 @@ class Motor(object):
sleep(self._T)
self.clean_pins_up()
+
if __name__ == "__main__":
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
m_l = Motor([6, 13, 19, 26])
- #m_r = Motor([10, 9, 11, 25])
+ # m_r = Motor([10, 9, 11, 25])
m_l.rpm = float(sys.argv[1])
- #m_r.rpm = float(sys.argv[1])
- print ("Pause in seconds: " + repr(m_l._T))
+ # m_r.rpm = float(sys.argv[1])
+ print("Pause in seconds: " + repr(m_l._T))
i = 1
while i < 5:
start_new_thread(m_l.move_to, (int(sys.argv[2]),))
- #start_new_thread(m_r.move_to, (int(sys.argv[3]),))
+ # start_new_thread(m_r.move_to, (int(sys.argv[3]),))
sleep(2)
i = i + 1
- GPIO.cleanup()
\ No newline at end of file
+ GPIO.cleanup()
diff --git a/lib/servolib.py b/lib/servolib.py
index d6dfead..dfa1095 100644
--- a/lib/servolib.py
+++ b/lib/servolib.py
@@ -4,20 +4,24 @@ import sys
pwm = ServoLib.PCA9685()
pwm.set_pwm_freq(60)
+
def left():
- pwm.set_pwm(0,0,261)
+ pwm.set_pwm(0, 0, 261)
+
def right():
- pwm.set_pwm(0,0,479)
+ pwm.set_pwm(0, 0, 479)
+
def reset():
- pwm.set_pwm(0,0,370)
+ pwm.set_pwm(0, 0, 370)
+
if __name__ == '__main__':
- args=sys.argv
+ args = sys.argv
if 'left' in args:
left()
elif 'right' in args:
right()
else:
- reset()
\ No newline at end of file
+ reset()
diff --git a/lib/thermolib.py b/lib/thermolib.py
index 1e85e3d..a1aa8a5 100644
--- a/lib/thermolib.py
+++ b/lib/thermolib.py
@@ -1,8 +1,11 @@
import Adafruit_DHT
+
def main():
humidity, temperature = Adafruit_DHT.read_retry(11, 14)
- print '{}|{}'.format(temperature, humidity)
+ print
+ '{}|{}'.format(temperature, humidity)
+
if __name__ == '__main__':
- main()
\ No newline at end of file
+ main()
diff --git a/lib/ultrasonic.py b/lib/ultrasonic.py
index c73a4f1..c8836dc 100644
--- a/lib/ultrasonic.py
+++ b/lib/ultrasonic.py
@@ -3,14 +3,15 @@ import time
GPIO.setmode(GPIO.BOARD)
+
class Sensor(object):
- def init(self,TRIG,ECHO):
- self.TRIGGER=TRIG
- self.ECHO=ECHO
- self.lastValues=[1,1,1,1,1,1,1,1,1,1]
- GPIO.setup(self.TRIGGER,GPIO.OUT)
- GPIO.setup(self.ECHO,GPIO.IN)
- GPIO.output(self.TRIGGER,False)
+ def init(self, TRIG, ECHO):
+ self.TRIGGER = TRIG
+ self.ECHO = ECHO
+ self.lastValues = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
+ GPIO.setup(self.TRIGGER, GPIO.OUT)
+ GPIO.setup(self.ECHO, GPIO.IN)
+ GPIO.output(self.TRIGGER, False)
print('Waiting for Sensor to settle')
time.sleep(2)
@@ -18,30 +19,28 @@ class Sensor(object):
GPIO.output(self.TRIGGER, True)
time.sleep(0.00001)
GPIO.output(self.TRIGGER, False)
- abs_start=time.time()
- pulse_start=time.time()
+ abs_start = time.time()
- while GPIO.input(self.ECHO)==0 and (time.time()-abs_start)<0.02:
- #print(time.time()-abs_start)
+ while GPIO.input(self.ECHO) == 0 and (time.time() - abs_start) < 0.02:
+ # print(time.time()-abs_start)
pass
- pulse_start=time.time()
- pulse_end=time.time()
+ pulse_start = time.time()
- while GPIO.input(self.ECHO)==1 and (time.time()-abs_start)<0.02:
- #print(time.time()-abs_start)
+ while GPIO.input(self.ECHO) == 1 and (time.time() - abs_start) < 0.02:
+ # print(time.time()-abs_start)
pass
- pulse_end=time.time()
+ pulse_end = time.time()
- pulse_duration=pulse_end-pulse_start
+ pulse_duration = pulse_end - pulse_start
- distance=pulse_duration*17150
- distance=round(distance,2)
+ distance = pulse_duration * 17150
+ distance = round(distance, 2)
self.lastValues.append(distance)
self.lastValues = self.lastValues[1:]
- distance=round((sum(self.lastValues))/(len(self.lastValues)),2)
- #print(self.lastValues)
- #print("Distance: {}".format(distance))
+ distance = round((sum(self.lastValues)) / (len(self.lastValues)), 2)
+ # print(self.lastValues)
+ # print("Distance: {}".format(distance))
return distance
def clean(self):
- GPIO.cleanup()
\ No newline at end of file
+ GPIO.cleanup()
diff --git a/main.py b/main.py
index 144d49f..fbce7bf 100644
--- a/main.py
+++ b/main.py
@@ -1,28 +1,30 @@
-import pygame, time
+import pygame
+import time
from lib import graphiclib, hardwarelib, controllib
configuration = {
- 'GAS':5,
- 'STOP':2,
- 'MUSIC':1,
- 'LIGHT':0,
- 'VOLIN':5,
- 'VOLDE':4,
- 'REC':3
+ 'GAS': 5,
+ 'STOP': 2,
+ 'MUSIC': 1,
+ 'LIGHT': 0,
+ 'VOLIN': 5,
+ 'VOLDE': 4,
+ 'REC': 3
}
+
def main():
navigator = hardwarelib.Navigator(16)
light = hardwarelib.Light(15)
- ultrasonic = hardwarelib.Ultrasonic(11,7)
+ ultrasonic = hardwarelib.Ultrasonic(11, 7)
temperature = hardwarelib.Temperature()
camera = graphiclib.PiCamera((500, 0), (500, 1000))
jstick = controllib.Joystick(navigator, light, configuration, camera)
- #pygame stuff
- screen = graphiclib.Screen(size=(1000,1000))
+ # pygame stuff
+ screen = graphiclib.Screen(size=(1000, 1000))
all_sprites = pygame.sprite.RenderUpdates()
- list = graphiclib.List((0,0),(500,1000))
+ list = graphiclib.List((0, 0), (500, 1000))
all_sprites.add(list)
all_sprites.add(camera)
clock = pygame.time.Clock()
@@ -38,9 +40,9 @@ def main():
list.set_dict(dict)
all_sprites.update()
update_rects = all_sprites.draw(screen.screen)
- screen.refresh(rectangles= update_rects)
+ screen.refresh(rectangles=update_rects)
pygame.quit()
if __name__ == '__main__':
- main()
\ No newline at end of file
+ main()
diff --git a/main_old.py b/main_old.py
deleted file mode 100644
index d8a34db..0000000
--- a/main_old.py
+++ /dev/null
@@ -1,366 +0,0 @@
-import pygame, picamera #,gyro
-from lib import ultrasonic
-from datetime import datetime
-from subprocess import call
-import RPi.GPIO as GPIO
-
-
-
-# Define some colors
-BLACK = ( 0, 0, 0)
-WHITE = ( 255, 255, 255)
-# --ultrasonic--
-sensor= ultrasonic.Sensor()
-sensor.init(11,7)
-# -- Nightmode
-# WHITE, BLACK=BLACK,WHITE
-# Pins BCM
-motor = 16
-lightpin=15
-#
-# --config--
-use_camera=False
-use_gyro_preview=False
-#--
-forpress = False
-light = False
-lpress = False
-play = False
-ppress = False
-voluppress = False
-voldopress = False
-recpress=False
-recording=False
-left=False
-right=False
-lastContact=datetime.now()
-if use_camera:
- cam = picamera.PiCamera()
-
-top_view = pygame.image.load('top_view.png')
-top_view= pygame.transform.scale(top_view, (192, 108))
-
-# Axis
-GAS = 5
-STOP = 2
-
-# Buttons
-MUSIC = 1
-LIGHT = 0
-VOLUP = 5
-VOLDO = 4
-RECSTART = 3
-
-# setup the relais
-GPIO.setup(motor,GPIO.OUT)
-GPIO.setup(lightpin,GPIO.OUT)
-GPIO.output(motor,True)
-GPIO.output(lightpin, True)
-# ____________________________
-# -- driving functions
-def drive_forw():
- GPIO.output(motor,False)
- return
-
-def drive_backw():
- print(drive_backw)
- return
-
-def reset_dire():
- GPIO.output(motor,True)
- return
-
-def drive_left():
- if not left:
- call(['python', './lib/servolib.py','left'])
- return
-
-def drive_right():
- if not right:
- call(['python', './lib/servolib.py','right'])
- return
-
-def reset_turn():
- if left or right:
- call(['python','./lib/servolib.py'])
- return
-# ____________________________
-# -- special functions
-def turn_light(shine):
- if shine:
- GPIO.output(lightpin, False)
- else:
- GPIO.output(lightpin, True)
- return
-
-def turn_rec(*record):
- #if record:
- #cam.start_recording('record.h264')
- #return
- #else:
- #cam.stop_recording()
- #return
- if use_camera:
- try:
- cam.capture('image.jpg', resize=(192 * 2, 108 * 2))
- cam.capture("./Images/image{}.jpg".format(str(datetime.now()).replace(':','_')))
- except Exception as error:
- print(error)
-
-def imgRefresh():
- #turn_rec(True)
- image = None
- try:
- image = pygame.image.load('image.jpg')
- image = pygame.transform.scale(image, (192*2, 108*2))
- except:
- pass
- screen.blit(image, (500, 10))
- #gyro_output = gyro.getAllOut()
- #rotation = gyro_output['rot']
- if use_gyro_preview:
- top_view = pygame.image.load('top_view.png')
- # The scaling of the top_view Spider
- scal_x = abs(int(192 * ((abs(rotation[0]) / 90) - 1)))
- scal_y = abs(int(108 * ((abs(rotation[1]) / 90) - 1)) - 1)
- top_view = pygame.transform.scale(top_view, (scal_x, scal_y))
- screen.blit(top_view, (550 - (scal_x / 2), 300 - (scal_y / 2)))
-
-def printDriveData():
- textPrint.print(screen,"")
- textPrint.print(screen,"Sound Information")
- textPrint.print(screen, " Sound: {}".format(play))
- textPrint.print(screen, " Volume: {}".format(sound.get_volume()))
- textPrint.print(screen, "Light: {}".format(light))
- textPrint.print(screen, "Last Controller Input: {}".format((datetime.now()-lastContact).seconds))
- #gyro_output=gyro.getAllOut()
- #rotation=gyro_output['rot']
- #textPrint.print(screen, "Rotation: X:{}; Y:{}".format(rotation[0],rotation[1]))
- #acceleration = gyro_output['acc_sca']
- #textPrint.print(screen, "Acceleration: X:{}; Y:{}; Z:{}".format(acceleration[0], acceleration[1],acceleration[2]))
- distance=sensor.echo()
- textPrint.print(screen, "Distance: {} cm".format(distance))
- #if distance<40:
- # turn_rec()
-
-def doSubroutine():
- if abs((datetime.now()-lastContact).seconds)==30:
- print('Contact Lost')
- imgRefresh()
-
-# ____________________________
-class TextPrint:
- def __init__(self):
- self.reset()
- self.font = pygame.font.Font(None, 20)
-
- def print(self, screen, textString):
- textBitmap = self.font.render(textString, True, BLACK)
- screen.blit(textBitmap, [self.x, self.y])
- self.y += self.line_height
-
- def reset(self):
- self.x = 10
- self.y = 10
- self.line_height = 15
-
- def indent(self):
- self.x += 10
-
- def unindent(self):
- self.x -= 10
-
-pygame.init()
-
-# ________________________
-# -- music playback
-pygame.mixer.init()
-sound=pygame.mixer.Sound('./sounds/gasgasgas.wav')
-sound.set_volume(0.3)
-# ________________________
-
-# Set the width and height of the screen [width,height]
-size = [1000, 700]
-screen = pygame.display.set_mode(size)
-
-pygame.display.set_caption("SPYDER STEUERZENTRALE")
-
-# Loop until the user clicks the close button.
-done = False
-
-# Used to manage how fast the screen updates
-clock = pygame.time.Clock()
-
-# Initialize the joysticks
-pygame.joystick.init()
-
-# Get ready to print
-textPrint = TextPrint()
-turn_rec(True)
-image=pygame.image.load('image.jpg')
-image=pygame.transform.scale(image,(192*2,108*2))
-count=0
-
-# -------- Main Program Loop -----------
-while done==False:
- # EVENT PROCESSING STEP
- for event in pygame.event.get(): # User did something
- if event.type == pygame.QUIT: # If user clicked close
- done=True # Flag that we are done so we exit this loop
-
- # Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTIO
-
-
- # DRAWING STEP
- # First, clear the screen to white. Don't put other drawing commands
- # above this, or they will be erased with this command.
- screen.fill(WHITE)
- textPrint.reset()
-
- # Get count of joysticks
- joystick_count = pygame.joystick.get_count()
-
- textPrint.print(screen, "Number of joysticks: {}".format(joystick_count) )
- textPrint.indent()
-
- # For each joystick:
- for i in range(joystick_count):
- joystick = pygame.joystick.Joystick(i)
- joystick.init()
-
- textPrint.print(screen, "Joystick {}".format(i) )
- textPrint.indent()
-
- # Get the name from the OS for the controller/joystick
- #name = joystick.get_name()
- #textPrint.print(screen, "Joystick name: {}".format(name) )
-
- # Usually axis run in pairs, up/down for one, and left/right for
- # the other.
- axes = joystick.get_numaxes()
- textPrint.print(screen, "Number of axes: {}".format(axes) )
- textPrint.indent()
-
- for i in range( axes ):
- axis = joystick.get_axis( i )
- textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
- if i==STOP and axis>0.1:
- print('STOP')
- drive_backw()
- lastContact=datetime.now()
- elif i==GAS and axis>0.1:
- print('GAS')
- if not forpress:
- drive_forw()
- forpress=True
- lastContact=datetime.now()
- elif i==GAS and axis<0.1:
- if forpress:
- reset_dire()
- print('RESETDRIVE')
- forpress=False
-
- textPrint.unindent()
-
- buttons = joystick.get_numbuttons()
- textPrint.print(screen, "Number of buttons: {}".format(buttons) )
- textPrint.indent()
-
- for i in range( buttons ):
- button = joystick.get_button( i )
- textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )
- if button==1:
- lastContact=datetime.now()
- #if i==7 and button==1:
- # print('GAS')
- # drive_forw()
- #elif i==6 and button==1:
- # print('STOP')
- # drive_backw()
- if i==LIGHT and button==1 and not lpress:
- light=not light
- lpress=True
- turn_light(light)
- print('Light: {}'.format(light))
- elif i==LIGHT and button==0:
- lpress=False
- elif i==MUSIC and button==1 and not play and not ppress:
- ppress=True
- sound.play()
- play=True
- print('Sound: {}'.format(play))
- elif i==MUSIC and button==1 and play and not ppress:
- ppress=True
- sound.stop()
- play=False
- print('Sound: {}'.format(play))
- elif i==MUSIC and button==0 and ppress:
- ppress=False
- elif i==VOLUP and button==1 and not voluppress:
- sound.set_volume(sound.get_volume()+0.1)
- print('Volume: {}'.format(sound.get_volume()))
- voluppress=True
- elif i==VOLUP and button==0:
- voluppress=False
- elif i== VOLDO and button==0:
- voldopress=False
- elif i==VOLDO and button==1 and not voldopress:
- sound.set_volume(sound.get_volume()-0.1)
- print('Volume: {}'.format(sound.get_volume()))
- voldopress=True
- elif i==RECSTART and button==1 and not recpress:
- recording=not recording
- turn_rec(recording)
- print('Recording: {}'.format(recording))
- recpress=True
- elif i==RECSTART and button==0:
- recpress=False
-
- textPrint.unindent()
-
- # Hat switch. All or nothing for direction, not like joysticks.
- # Value comes back in an array.
- hats = joystick.get_numhats()
- textPrint.print(screen, "Number of hats: {}".format(hats) )
- textPrint.indent()
-
- for i in range( hats ):
- hat = joystick.get_hat( i )
- textPrint.print(screen, "Hat {} value: {}".format(i, str(hat)) )
- if hat==(-1,0):
- print('LEFT')
- drive_left()
- left=True
- right=False
- lastContact=datetime.now()
- elif hat==(1,0):
- print('RIGHT')
- drive_right()
- right=True
- left=False
- lastContact=datetime.now()
- else:
- reset_turn()
- left=False
- right=False
- textPrint.unindent()
-
- textPrint.unindent()
-
- printDriveData()
- doSubroutine()
-
-
- # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
-
- # Go ahead and update the screen with what we've drawn.
- pygame.display.flip()
-
- # Limit to 20 frames per second
- clock.tick(20)
-
-# Close the window and quit.
-# If you forget this line, the program will 'hang'
-# on exit if running from IDLE.
-sensor.clean()
-pygame.quit ()
diff --git a/stream.sh b/stream.sh
index a918f48..66a2cd6 100644
--- a/stream.sh
+++ b/stream.sh
@@ -1 +1,2 @@
+#!/usr/bin/env bash
raspivid -o - -t 0 -hf -w 640 -h 360 -fps 25 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554}' :demux=h264
\ No newline at end of file
diff --git a/lib/gyro_test.py b/testscripts/gyro_test.py
similarity index 98%
rename from lib/gyro_test.py
rename to testscripts/gyro_test.py
index 22c5f66..769d607 100644
--- a/lib/gyro_test.py
+++ b/testscripts/gyro_test.py
@@ -1,5 +1,6 @@
from lib import gyro
import time
+
while True:
print(gyro.getAllOut()['rot'])
time.sleep(1)
diff --git a/lib/motor.py b/testscripts/motor_test.py
similarity index 78%
rename from lib/motor.py
rename to testscripts/motor_test.py
index edb2e67..1e10205 100644
--- a/lib/motor.py
+++ b/testscripts/motor_test.py
@@ -1,131 +1,139 @@
+import time
+
import RPi.GPIO as GPIO
-import sys, time
+
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
-def full_step(phase,pins):
+
+def full_step(phase, pins):
pin1, pin2, pin3, pin4 = pins
- if phase==0:
+ if phase == 0:
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
- if phase==1:
+ if phase == 1:
GPIO.output(pin1, 1)
GPIO.output(pin2, 1)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
- if phase==2:
+ if phase == 2:
GPIO.output(pin1, 0)
GPIO.output(pin2, 1)
GPIO.output(pin3, 1)
GPIO.output(pin4, 0)
- if phase==3:
+ if phase == 3:
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 1)
GPIO.output(pin4, 1)
- if phase==4:
+ if phase == 4:
GPIO.output(pin1, 1)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 1)
-def half_step(phase,pins):
+
+def half_step(phase, pins):
pin1, pin2, pin3, pin4 = pins
- if phase==0:
+ if phase == 0:
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
- if phase==1:
+ if phase == 1:
GPIO.output(pin1, 1)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
- if phase==2:
+ if phase == 2:
GPIO.output(pin1, 1)
GPIO.output(pin2, 1)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
- if phase==3:
+ if phase == 3:
GPIO.output(pin1, 0)
GPIO.output(pin2, 1)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
- if phase==4:
+ if phase == 4:
GPIO.output(pin1, 0)
GPIO.output(pin2, 1)
GPIO.output(pin3, 1)
GPIO.output(pin4, 0)
- if phase==5:
+ if phase == 5:
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 1)
GPIO.output(pin4, 0)
- if phase==6:
+ if phase == 6:
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 1)
GPIO.output(pin4, 1)
- if phase==7:
+ if phase == 7:
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 1)
- if phase==8:
+ if phase == 8:
GPIO.output(pin1, 1)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 1)
+
def full_phase(pins):
- #order=[1,2,3,4]
- order=[4,3,2,1]
+ # order=[1,2,3,4]
+ order = [4, 3, 2, 1]
print("Moving in {}-order".format(order))
for i in order:
full_step(i, pins)
time.sleep(0.005)
+
def half_phase(pins):
- for i in range(0,8):
- half_step(i,pins)
+ for i in range(0, 8):
+ half_step(i, pins)
time.sleep(0.0025)
+
def forward(pins):
half_phase(pins)
+
def backward(pins):
(pins[0], pins[1], pins[2], pins[3]) = (pins[1], pins[0], pins[2], pins[3])
half_phase(pins)
-if __name__=='__main__':
- pins=[6, 13, 19, 26]
+
+if __name__ == '__main__':
+ pins = [6, 13, 19, 26]
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
while True:
try:
- start=time.time()
- while(time.time()