erinaco/autopilot.py

92 lines
2.3 KiB
Python
Raw Normal View History

2017-04-16 01:32:58 +02:00
"""
Autopilot-Klasse schmeisst Exceptions und stoppt Erinaco bei Fehlern
@author kf
@since 2017-04-15
Status-Codes
0: Stopp
1: Forward
2: Backward
3: Left
4: Right
"""
#Bewegungs-Exeptions:
class MoveException(Exception):
pass
class RightMoveException(MoveException):
pass
class LeftMoveException(MoveException):
pass
class ForwardMoveException(MoveException):
pass
#Hauptklasse
from core import Core as CORE
from time import sleep
2017-04-16 23:44:12 +02:00
class Autopilot(CORE):
2017-04-16 01:32:58 +02:00
def __init__(self):
CORE.__init__(self);
self.moveStatus=0;
self.ultraschalminimum=10;
def statusTest(self):
switcher = {
1: lambda: self.testForward(),
3: lambda: self.testLeft(),
4: lambda: self.testRight(),
}
2017-04-16 23:44:12 +02:00
func = switcher.get(self.moveStatus, lambda:"Error")
2017-04-16 01:32:58 +02:00
return func();
def testLeft(self):
2017-04-16 23:44:12 +02:00
if(self.ultraschallLinksValue<=self.ultraschalminimum or self.infarotLinks.getValue()):
2017-04-16 01:32:58 +02:00
self.stop();
2017-04-16 23:44:12 +02:00
raise LeftMoveException("Abstand Ultraschall-Links betraegt {0}cm und Infarot-Links hat Wert {1}"
.format(
self.ultraschallLinksValue,
self.infarotLinksValue
)
);
2017-04-16 01:32:58 +02:00
def testRight(self):
2017-04-16 23:44:12 +02:00
if(self.infarotMitteRechtsValue<=self.ultraschalminimum or self.infarotRechtsValue):
2017-04-16 01:32:58 +02:00
self.stop();
2017-04-16 23:44:12 +02:00
raise RightMoveException("Abstand Ultraschall-Rechts betraegt {0}cm und Infarot-Rechts hat Wert {1}".
format(
self.infarotMitteRechtsValue,
self.infarotRechtsValue
)
);
2017-04-16 01:32:58 +02:00
def testForward(self):
2017-04-16 23:44:12 +02:00
if(self.ultraschallMitteValue<=self.ultraschalminimum
or self.ultraschallRechtsValue<=self.ultraschalminimum
or self.ultraschallLinksValue<=self.ultraschalminimum
or self.infarotMitteLinksValue
or self.infarotMitteRechtsValue):
2017-04-16 01:32:58 +02:00
self.stop();
2017-04-16 23:44:12 +02:00
raise ForwardMoveException("Abstand Ultraschallmitte betraegt {0}cm. Infarot-Mitte-Links: {1}. Infarot-Mitte-Rechts:{2}".
format(
self.ultraschallMitteValue,
self.infarotMitteLinksValue,
self.infarotMitteRechtsValue)
);
2017-04-16 01:32:58 +02:00
def turnLeft(self):
self.moveStatus=3;
2017-04-16 23:44:12 +02:00
self.statusTest();
2017-04-16 01:32:58 +02:00
CORE.turnLeft(self);
def turnRight(self):
2017-04-16 23:44:12 +02:00
self.moveStatus=4;
self.statusTest();
2017-04-16 01:32:58 +02:00
CORE.turnRight(self);
def forward(self):
2017-04-16 23:44:12 +02:00
self.moveStatus=1;
self.statusTest();
2017-04-16 01:32:58 +02:00
CORE.forward(self);
def backward(self):
2017-04-16 23:44:12 +02:00
self.moveStatus=2;
2017-04-16 01:32:58 +02:00
self.statusTest();
CORE.backward(self);
2017-04-16 23:44:12 +02:00
def stop(self):
2017-04-16 01:32:58 +02:00
self.moveStatus=0;
2017-04-16 23:44:12 +02:00
self.statusTest();
2017-04-16 01:32:58 +02:00
CORE.stop(self);