mirror of
				https://github.com/kevinveenbirkenbach/erinaco.git
				synced 2025-11-04 04:08:04 +00:00 
			
		
		
		
	Autopilotenklasse optimiert
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,3 +1,4 @@
 | 
			
		||||
__pycache__/autopilot.cpython-36.pyc
 | 
			
		||||
__pycache__/vero.cpython-36.pyc
 | 
			
		||||
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
 | 
			
		||||
from core import Core as CORE
 | 
			
		||||
from threading import Thread as THREAD
 | 
			
		||||
from time import sleep 
 | 
			
		||||
class Autopilot(CORE,THREAD):
 | 
			
		||||
class Autopilot(CORE):
 | 
			
		||||
	def __init__(self):
 | 
			
		||||
		CORE.__init__(self);
 | 
			
		||||
		THREAD.__init__(self);
 | 
			
		||||
		self.moveStatus=0; 
 | 
			
		||||
		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):
 | 
			
		||||
		switcher = {
 | 
			
		||||
			1: lambda: self.testForward(),
 | 
			
		||||
			3: lambda: self.testLeft(),
 | 
			
		||||
			4: lambda: self.testRight(),
 | 
			
		||||
		}
 | 
			
		||||
		func = switcher.get(self.moveStatus, lambda:0)
 | 
			
		||||
		func = switcher.get(self.moveStatus, lambda:"Error")
 | 
			
		||||
		return func();		
 | 
			
		||||
	def testLeft(self):
 | 
			
		||||
		ultraschallLinks=self.ultraschallLinks.getValue();
 | 
			
		||||
		if(ultraschallLinks<=self.ultraschalminimum or self.infarotLinks.getValue()):
 | 
			
		||||
		if(self.ultraschallLinksValue<=self.ultraschalminimum or self.infarotLinks.getValue()):
 | 
			
		||||
			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):
 | 
			
		||||
		ultraschallRechts=self.ultraschallRechts.getValue();
 | 
			
		||||
		if(ultraschallRechts<=self.ultraschalminimum or self.infarotRechts.getValue()):
 | 
			
		||||
		if(self.infarotMitteRechtsValue<=self.ultraschalminimum or self.infarotRechtsValue):
 | 
			
		||||
			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):
 | 
			
		||||
		ultraschallMitte=self.ultraschallMitte.getValue();
 | 
			
		||||
		if(ultraschallMitte<=self.ultraschalminimum 
 | 
			
		||||
			#or self.ultraschallRechts.getValue()<=self.ultraschalminimum 
 | 
			
		||||
			#or self.ultraschallLinks.getValue()<=self.ultraschalminimum 
 | 
			
		||||
			or self.infarotMitteLinks.getValue() 
 | 
			
		||||
			or self.infarotMitteRechts.getValue()):
 | 
			
		||||
		if(self.ultraschallMitteValue<=self.ultraschalminimum 
 | 
			
		||||
			or self.ultraschallRechtsValue<=self.ultraschalminimum 
 | 
			
		||||
			or self.ultraschallLinksValue<=self.ultraschalminimum 
 | 
			
		||||
			or self.infarotMitteLinksValue 
 | 
			
		||||
			or self.infarotMitteRechtsValue):
 | 
			
		||||
			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):
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
		self.moveStatus=3;
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
		CORE.turnLeft(self);
 | 
			
		||||
	def turnRight(self):
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
		self.moveStatus=4;	
 | 
			
		||||
		self.moveStatus=4;
 | 
			
		||||
		self.statusTest();	
 | 
			
		||||
		CORE.turnRight(self);
 | 
			
		||||
	def forward(self):
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
		self.moveStatus=1;		
 | 
			
		||||
		
 | 
			
		||||
		self.moveStatus=1;
 | 
			
		||||
		self.statusTest();		
 | 
			
		||||
		CORE.forward(self);
 | 
			
		||||
	def backward(self):
 | 
			
		||||
		self.moveStatus=2;		
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
		self.moveStatus=2;
 | 
			
		||||
		CORE.backward(self);
 | 
			
		||||
	def stop(self):
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
	def stop(self):		
 | 
			
		||||
		self.moveStatus=0;
 | 
			
		||||
		self.statusTest();
 | 
			
		||||
		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);
 | 
			
		||||
		MOTION.__init__(self);
 | 
			
		||||
		VERO.__init__(self);
 | 
			
		||||
	def routine(self): #Routine welche vor jedem Arbeitsschritt ausgefuehrt werden soll
 | 
			
		||||
		self.setSensorValues();
 | 
			
		||||
	def __del__(self):
 | 
			
		||||
		GPIO.cleanup();
 | 
			
		||||
 
 | 
			
		||||
@@ -21,31 +21,34 @@ def help():
 | 
			
		||||
	print("Manuell:         e")
 | 
			
		||||
def autopilot():
 | 
			
		||||
	try:
 | 
			
		||||
		core=AUTOPILOT()
 | 
			
		||||
		#core.start();
 | 
			
		||||
		while True:			
 | 
			
		||||
			try:	
 | 
			
		||||
				core.printValues();			
 | 
			
		||||
				if core.moveStatus!=1:			
 | 
			
		||||
					core.forward();
 | 
			
		||||
				else:
 | 
			
		||||
					core.statusTest();
 | 
			
		||||
			except ForwardMoveException:
 | 
			
		||||
				if randint(0,1):			
 | 
			
		||||
					core.backward();
 | 
			
		||||
				else:
 | 
			
		||||
					if randint(0,1):
 | 
			
		||||
						core.turnLeft();				
 | 
			
		||||
		autopilot=AUTOPILOT()
 | 
			
		||||
		while True:						
 | 
			
		||||
			try:			
 | 
			
		||||
				try:	
 | 
			
		||||
					autopilot.setSensorValues();
 | 
			
		||||
					autopilot.printValues();
 | 
			
		||||
					print("Richtung: {0}".format(autopilot.moveStatus));			
 | 
			
		||||
					if autopilot.moveStatus!=1:			
 | 
			
		||||
						autopilot.forward();
 | 
			
		||||
					else:
 | 
			
		||||
						core.turnRight();
 | 
			
		||||
			except LeftMoveException:
 | 
			
		||||
				core.turnLeft();
 | 
			
		||||
			except RightMoveException:
 | 
			
		||||
				core.turnRight();
 | 
			
		||||
			sleep(1);
 | 
			
		||||
						autopilot.statusTest();
 | 
			
		||||
				except ForwardMoveException:				
 | 
			
		||||
					if randint(0,1):			
 | 
			
		||||
						autopilot.backward();
 | 
			
		||||
					else:
 | 
			
		||||
						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:
 | 
			
		||||
		print("Verlasse Autopilot...")
 | 
			
		||||
		#core=CORE();
 | 
			
		||||
def doIt(order):
 | 
			
		||||
	switcher = {
 | 
			
		||||
        	'w': lambda: core.forward(),
 | 
			
		||||
@@ -62,11 +65,10 @@ def doIt(order):
 | 
			
		||||
print("Herzlich Willkommen im manuellen Controll-Interface!\n") 
 | 
			
		||||
try:	
 | 
			
		||||
	while True:
 | 
			
		||||
		core.setSensorValues()
 | 
			
		||||
		input=getch.getch();
 | 
			
		||||
		print("Erinaco>>{0}".format(input))
 | 
			
		||||
		doIt(input);
 | 
			
		||||
except MoveException:
 | 
			
		||||
	print("Move Exception...")
 | 
			
		||||
except KeyboardInterrupt:
 | 
			
		||||
	print("Verlasse Erinaco...")
 | 
			
		||||
	core.__del__();
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								vero.py
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								vero.py
									
									
									
									
									
								
							@@ -5,6 +5,7 @@
 | 
			
		||||
"""
 | 
			
		||||
from sensoren.boolsensor import Boolsensor as BOOLSENSOR 
 | 
			
		||||
from sensoren.ultraschall import Ultraschall as ULTRASCHALL
 | 
			
		||||
import time
 | 
			
		||||
#from sensoren.dht11 import Dht11 as DHT11
 | 
			
		||||
class Vero(object):
 | 
			
		||||
	def __init__(self):
 | 
			
		||||
@@ -18,18 +19,29 @@ class Vero(object):
 | 
			
		||||
		self.infarotRechts=BOOLSENSOR(21);
 | 
			
		||||
		self.infarotMitteLinks=BOOLSENSOR(16);
 | 
			
		||||
		self.infarotMitteRechts=BOOLSENSOR(20);
 | 
			
		||||
	def printValues(self):
 | 
			
		||||
		self.timestampValue=0;
 | 
			
		||||
	def printValues(self): 		
 | 
			
		||||
		#self.dht.setValues();
 | 
			
		||||
		print("PIR:			{0}".format(self.pir.getValue()));
 | 
			
		||||
		print("PIR:			{0}".format(self.pirValue));
 | 
			
		||||
		#print("Temperatur:		{0}".format(self.dht.getTemperatur()));
 | 
			
		||||
		#print("Luftfeuchtigkeit:	{0}".format(self.dht.getLuftfeuchtigkeit()));
 | 
			
		||||
		print("Ultraschall-Links:	{0}cm".format(self.ultraschallLinks.getValue()));
 | 
			
		||||
		print("Ultraschall-Mitte:	{0}cm".format(self.ultraschallMitte.getValue()));
 | 
			
		||||
		print("Ultraschall-Rechts:	{0}cm".format(self.ultraschallRechts.getValue()));
 | 
			
		||||
		print("Infarot Links:		{0}".format(self.infarotLinks.getValue()));
 | 
			
		||||
		print("Infarot Mitte-Links:	{0}".format(self.infarotMitteLinks.getValue()));
 | 
			
		||||
		print("Infarot Mitte-Rechts:	{0}".format(self.infarotMitteRechts.getValue()));
 | 
			
		||||
		print("Infarot Rechts:		{0}".format(self.infarotRechts.getValue()));
 | 
			
		||||
		print("Ultraschall-Links:	{0}cm".format(self.ultraschallLinksValue));
 | 
			
		||||
		print("Ultraschall-Mitte:	{0}cm".format(self.ultraschallMitteValue));
 | 
			
		||||
		print("Ultraschall-Rechts:	{0}cm".format(self.ultraschallRechtsValue));
 | 
			
		||||
		print("Infarot Links:		{0}".format(self.infarotLinksValue));
 | 
			
		||||
		print("Infarot Mitte-Links:	{0}".format(self.infarotMitteLinksValue));
 | 
			
		||||
		print("Infarot Mitte-Rechts:	{0}".format(self.infarotMitteRechtsValue));
 | 
			
		||||
		print("Infarot Rechts:		{0}".format(self.infarotRechtsValue));
 | 
			
		||||
	def saveToDB(self): #Speichert die Werte in der Datenbank
 | 
			
		||||
		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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user