Autopilotenklasse optimiert

This commit is contained in:
Kevin Frantz 2017-04-16 21:44:12 +00:00
parent 473ec178a6
commit 6f4364665b
6 changed files with 87 additions and 70 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
__pycache__/autopilot.cpython-36.pyc __pycache__/autopilot.cpython-36.pyc
__pycache__/vero.cpython-36.pyc __pycache__/vero.cpython-36.pyc
sensoren/__pycache__/ sensoren/__pycache__/
__pycache__/core.cpython-36.pyc

Binary file not shown.

View File

@ -24,69 +24,69 @@ class ForwardMoveException(MoveException):
#Hauptklasse #Hauptklasse
from core import Core as CORE from core import Core as CORE
from threading import Thread as THREAD
from time import sleep from time import sleep
class Autopilot(CORE,THREAD): class Autopilot(CORE):
def __init__(self): def __init__(self):
CORE.__init__(self); CORE.__init__(self);
THREAD.__init__(self);
self.moveStatus=0; self.moveStatus=0;
self.ultraschalminimum=10; self.ultraschalminimum=10;
self.threadRun=True;
#self.start();
def run(self):
x=1
while self.threadRun:
self.statusTest();
print("Autopilot-Test #{0}".format(x));
#sleep(0.2);
x+=1
def statusTest(self): def statusTest(self):
switcher = { switcher = {
1: lambda: self.testForward(), 1: lambda: self.testForward(),
3: lambda: self.testLeft(), 3: lambda: self.testLeft(),
4: lambda: self.testRight(), 4: lambda: self.testRight(),
} }
func = switcher.get(self.moveStatus, lambda:0) func = switcher.get(self.moveStatus, lambda:"Error")
return func(); return func();
def testLeft(self): def testLeft(self):
ultraschallLinks=self.ultraschallLinks.getValue(); if(self.ultraschallLinksValue<=self.ultraschalminimum or self.infarotLinks.getValue()):
if(ultraschallLinks<=self.ultraschalminimum or self.infarotLinks.getValue()):
self.stop(); self.stop();
raise LeftMoveException("Abstand Ultraschall-Links betraegt {0}cm und Infarot-Links hat Wert {1}".format(ultraschallLinks,self.infarotLinks.getValue())); raise LeftMoveException("Abstand Ultraschall-Links betraegt {0}cm und Infarot-Links hat Wert {1}"
.format(
self.ultraschallLinksValue,
self.infarotLinksValue
)
);
def testRight(self): def testRight(self):
ultraschallRechts=self.ultraschallRechts.getValue(); if(self.infarotMitteRechtsValue<=self.ultraschalminimum or self.infarotRechtsValue):
if(ultraschallRechts<=self.ultraschalminimum or self.infarotRechts.getValue()):
self.stop(); self.stop();
raise RightMoveException("Abstand Ultraschall-Rechts betraegt {0}cm und Infarot-Rechts hat Wert {1}".format(ultraschallRechts,self.infarotRechts.getValue())); raise RightMoveException("Abstand Ultraschall-Rechts betraegt {0}cm und Infarot-Rechts hat Wert {1}".
format(
self.infarotMitteRechtsValue,
self.infarotRechtsValue
)
);
def testForward(self): def testForward(self):
ultraschallMitte=self.ultraschallMitte.getValue(); if(self.ultraschallMitteValue<=self.ultraschalminimum
if(ultraschallMitte<=self.ultraschalminimum or self.ultraschallRechtsValue<=self.ultraschalminimum
#or self.ultraschallRechts.getValue()<=self.ultraschalminimum or self.ultraschallLinksValue<=self.ultraschalminimum
#or self.ultraschallLinks.getValue()<=self.ultraschalminimum or self.infarotMitteLinksValue
or self.infarotMitteLinks.getValue() or self.infarotMitteRechtsValue):
or self.infarotMitteRechts.getValue()):
self.stop(); self.stop();
raise ForwardMoveException("Abstand Ultraschallmitte betraegt {0}cm. Infarot-Mitte-Links: {1}. Infarot-Mitte-Rechts:{2}".format(ultraschallMitte,self.infarotMitteLinks.getValue(),self.infarotMitteRechts.getValue())); raise ForwardMoveException("Abstand Ultraschallmitte betraegt {0}cm. Infarot-Mitte-Links: {1}. Infarot-Mitte-Rechts:{2}".
format(
self.ultraschallMitteValue,
self.infarotMitteLinksValue,
self.infarotMitteRechtsValue)
);
def turnLeft(self): def turnLeft(self):
self.statusTest();
self.moveStatus=3; self.moveStatus=3;
self.statusTest();
CORE.turnLeft(self); CORE.turnLeft(self);
def turnRight(self): def turnRight(self):
self.statusTest();
self.moveStatus=4; self.moveStatus=4;
self.statusTest();
CORE.turnRight(self); CORE.turnRight(self);
def forward(self): def forward(self):
self.statusTest();
self.moveStatus=1; self.moveStatus=1;
self.statusTest();
CORE.forward(self); CORE.forward(self);
def backward(self): def backward(self):
self.statusTest();
self.moveStatus=2; self.moveStatus=2;
self.statusTest();
CORE.backward(self); CORE.backward(self);
def stop(self): def stop(self):
self.statusTest();
self.moveStatus=0; self.moveStatus=0;
self.statusTest();
CORE.stop(self); CORE.stop(self);
def __del__(self):
self.threadRun=False;

View File

@ -11,5 +11,7 @@ class Core(MOTION,VERO):
GPIO.setmode(GPIO.BCM); GPIO.setmode(GPIO.BCM);
MOTION.__init__(self); MOTION.__init__(self);
VERO.__init__(self); VERO.__init__(self);
def routine(self): #Routine welche vor jedem Arbeitsschritt ausgefuehrt werden soll
self.setSensorValues();
def __del__(self): def __del__(self):
GPIO.cleanup(); GPIO.cleanup();

View File

@ -21,31 +21,34 @@ def help():
print("Manuell: e") print("Manuell: e")
def autopilot(): def autopilot():
try: try:
core=AUTOPILOT() autopilot=AUTOPILOT()
#core.start();
while True: while True:
try: try:
core.printValues(); try:
if core.moveStatus!=1: autopilot.setSensorValues();
core.forward(); autopilot.printValues();
else: print("Richtung: {0}".format(autopilot.moveStatus));
core.statusTest(); if autopilot.moveStatus!=1:
except ForwardMoveException: autopilot.forward();
if randint(0,1):
core.backward();
else:
if randint(0,1):
core.turnLeft();
else: else:
core.turnRight(); autopilot.statusTest();
except LeftMoveException: except ForwardMoveException:
core.turnLeft(); if randint(0,1):
except RightMoveException: autopilot.backward();
core.turnRight(); else:
sleep(1); if randint(0,1):
autopilot.turnLeft();
else:
autopilot.turnRight();
except LeftMoveException:
core.turnLeft();
except RightMoveException:
core.turnRight();
except MoveException:
autopilot.stop();
sleep(0.5);
except KeyboardInterrupt: except KeyboardInterrupt:
print("Verlasse Autopilot...") print("Verlasse Autopilot...")
#core=CORE();
def doIt(order): def doIt(order):
switcher = { switcher = {
'w': lambda: core.forward(), 'w': lambda: core.forward(),
@ -62,11 +65,10 @@ def doIt(order):
print("Herzlich Willkommen im manuellen Controll-Interface!\n") print("Herzlich Willkommen im manuellen Controll-Interface!\n")
try: try:
while True: while True:
core.setSensorValues()
input=getch.getch(); input=getch.getch();
print("Erinaco>>{0}".format(input)) print("Erinaco>>{0}".format(input))
doIt(input); doIt(input);
except MoveException:
print("Move Exception...")
except KeyboardInterrupt: except KeyboardInterrupt:
print("Verlasse Erinaco...") print("Verlasse Erinaco...")
core.__del__(); core.__del__();

30
vero.py
View File

@ -5,6 +5,7 @@
""" """
from sensoren.boolsensor import Boolsensor as BOOLSENSOR from sensoren.boolsensor import Boolsensor as BOOLSENSOR
from sensoren.ultraschall import Ultraschall as ULTRASCHALL from sensoren.ultraschall import Ultraschall as ULTRASCHALL
import time
#from sensoren.dht11 import Dht11 as DHT11 #from sensoren.dht11 import Dht11 as DHT11
class Vero(object): class Vero(object):
def __init__(self): def __init__(self):
@ -18,18 +19,29 @@ class Vero(object):
self.infarotRechts=BOOLSENSOR(21); self.infarotRechts=BOOLSENSOR(21);
self.infarotMitteLinks=BOOLSENSOR(16); self.infarotMitteLinks=BOOLSENSOR(16);
self.infarotMitteRechts=BOOLSENSOR(20); self.infarotMitteRechts=BOOLSENSOR(20);
self.timestampValue=0;
def printValues(self): def printValues(self):
#self.dht.setValues(); #self.dht.setValues();
print("PIR: {0}".format(self.pir.getValue())); print("PIR: {0}".format(self.pirValue));
#print("Temperatur: {0}".format(self.dht.getTemperatur())); #print("Temperatur: {0}".format(self.dht.getTemperatur()));
#print("Luftfeuchtigkeit: {0}".format(self.dht.getLuftfeuchtigkeit())); #print("Luftfeuchtigkeit: {0}".format(self.dht.getLuftfeuchtigkeit()));
print("Ultraschall-Links: {0}cm".format(self.ultraschallLinks.getValue())); print("Ultraschall-Links: {0}cm".format(self.ultraschallLinksValue));
print("Ultraschall-Mitte: {0}cm".format(self.ultraschallMitte.getValue())); print("Ultraschall-Mitte: {0}cm".format(self.ultraschallMitteValue));
print("Ultraschall-Rechts: {0}cm".format(self.ultraschallRechts.getValue())); print("Ultraschall-Rechts: {0}cm".format(self.ultraschallRechtsValue));
print("Infarot Links: {0}".format(self.infarotLinks.getValue())); print("Infarot Links: {0}".format(self.infarotLinksValue));
print("Infarot Mitte-Links: {0}".format(self.infarotMitteLinks.getValue())); print("Infarot Mitte-Links: {0}".format(self.infarotMitteLinksValue));
print("Infarot Mitte-Rechts: {0}".format(self.infarotMitteRechts.getValue())); print("Infarot Mitte-Rechts: {0}".format(self.infarotMitteRechtsValue));
print("Infarot Rechts: {0}".format(self.infarotRechts.getValue())); print("Infarot Rechts: {0}".format(self.infarotRechtsValue));
def saveToDB(self): #Speichert die Werte in der Datenbank def saveToDB(self): #Speichert die Werte in der Datenbank
pass pass
def setSensorValues(self):
if(time.time() >= self.timestampValue+1): #Werte maximal jede Sekunde abspeichern
self.pirValue = self.pir.getValue();
self.infarotMitteLinksValue = self.infarotMitteLinks.getValue();
self.infarotMitteRechtsValue = self.infarotMitteRechts.getValue();
self.infarotLinksValue = self.infarotLinks.getValue();
self.infarotRechtsValue = self.infarotRechts.getValue();
self.ultraschallLinksValue = self.ultraschallLinks.getValue();
self.ultraschallMitteValue = self.ultraschallMitte.getValue();
self.ultraschallRechtsValue = self.ultraschallRechts.getValue();
self.timestampValue = time.time();