mirror of
https://github.com/kevinveenbirkenbach/erinaco.git
synced 2024-11-23 14:31:04 +01:00
Autopilotenklasse optimiert
This commit is contained in:
parent
473ec178a6
commit
6f4364665b
1
.gitignore
vendored
1
.gitignore
vendored
@ -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.
72
autopilot.py
72
autopilot.py
@ -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.moveStatus=2;
|
||||||
self.statusTest();
|
self.statusTest();
|
||||||
self.moveStatus=2;
|
|
||||||
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;
|
|
||||||
|
2
core.py
2
core.py
@ -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();
|
||||||
|
@ -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:
|
try:
|
||||||
core.printValues();
|
autopilot.setSensorValues();
|
||||||
if core.moveStatus!=1:
|
autopilot.printValues();
|
||||||
core.forward();
|
print("Richtung: {0}".format(autopilot.moveStatus));
|
||||||
else:
|
if autopilot.moveStatus!=1:
|
||||||
core.statusTest();
|
autopilot.forward();
|
||||||
except ForwardMoveException:
|
|
||||||
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__();
|
||||||
|
32
vero.py
32
vero.py
@ -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);
|
||||||
def printValues(self):
|
self.timestampValue=0;
|
||||||
|
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();
|
||||||
|
Loading…
Reference in New Issue
Block a user