144 lines
4.8 KiB
Python
144 lines
4.8 KiB
Python
# LEGO type:standard slot:7 autostart
|
|
|
|
from spike import PrimeHub, Motor, MotorPair, ColorSensor, MotionSensor
|
|
from spike.control import wait_for_seconds
|
|
|
|
HELLO = "HELLO IQ 2"
|
|
|
|
'''
|
|
Wir nutzen "Duck typing", dh wir schreiben hinter jede Variabel mit ':' die Klasse, zB `leftMotor: Motor`
|
|
damit man dann später auch wieder Code Completion hat bei Nutzung der Variablen
|
|
'''
|
|
class IQRobot:
|
|
|
|
def __init__(self, hub: PrimeHub, leftMotorPort: str, rightMotorPort: str, colorSensorPort: str):
|
|
self.hub: PrimeHub = hub
|
|
self.leftMotor: Motor = Motor(leftMotorPort)
|
|
self.rightMotor: Motor = Motor(rightMotorPort)
|
|
self.movementMotors: MotorPair = MotorPair(leftMotorPort, rightMotorPort)
|
|
#self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
|
|
#self.frontMotorLeft: Motor = Motor("C")
|
|
self.frontMotorRight: Motor = Motor("E")
|
|
self.motionSensor: MotionSensor = MotionSensor()
|
|
|
|
|
|
def show(self, image: str):
|
|
'''
|
|
Zeige Bild auf LED Matrix des Spikes
|
|
image: Bildname wie zB 'HAPPY'
|
|
'''
|
|
self.hub.light_matrix.show_image(image)
|
|
|
|
|
|
def driveForward_for_sec(self, seconds: float):
|
|
# Fahre die übergebene Anzahl seconds gerade aus
|
|
self.movementMotors.start()
|
|
wait_for_seconds(seconds)
|
|
self.movementMotors.stop()
|
|
|
|
def getColorIntensity(self):
|
|
# Ermittele Farbintensität über den Farbsensor
|
|
(red, green, blue, colorIntensity) = self.colorSensor.get_rgb_intensity()
|
|
return colorIntensity
|
|
|
|
def drehe(self, grad=90):
|
|
self.motionSensor.reset_yaw_angle()
|
|
steering = 100 if grad > 0 else -100
|
|
self.movementMotors.start(steering=steering, speed=10)
|
|
while abs(self.motionSensor.get_yaw_angle()) < abs(grad):
|
|
pass
|
|
self.movementMotors.stop()
|
|
|
|
|
|
def drehe_backstein_robot(self, grad=90):
|
|
radius=9.5
|
|
stift_versatz=2.2
|
|
self.movementMotors.move(-radius - stift_versatz)
|
|
self.drehe(grad)
|
|
self.movementMotors.move(radius - stift_versatz)
|
|
|
|
def buchstabe_zu_segmenten(self, buchstabe):
|
|
# Segmente um Buchstaben zu schreiben
|
|
# 5_
|
|
# 6 |__|4
|
|
# 1 |7_|3
|
|
# 2
|
|
#
|
|
buchstabe_zu_segmenten = {"L": [1,1,0,0,0,1,0], "E": [1,1,0,0,1,1,1], "G": [1,1,1,0,1,1,0], "O": [1,1,1,1,1,1,0]}
|
|
return buchstabe_zu_segmenten[buchstabe]
|
|
|
|
|
|
def bewege_stift(self, richtung):
|
|
self.frontMotorRight.run_for_rotations(richtung*0.4)
|
|
|
|
def schreibe_buchstabe(self, buchstabe):
|
|
print("Schreibe " + buchstabe)
|
|
segmente = self.buchstabe_zu_segmenten(buchstabe)
|
|
grad_drehung=-90
|
|
self.movementMotors.move(2)
|
|
self.drehe(grad_drehung)
|
|
for segment, segment_nummer in enumerate(segmente):
|
|
print("Segment: " + str(segment) + " , Segment Nummer: " + str(segment_nummer))
|
|
if segment==1:
|
|
self.bewege_stift(-1)
|
|
self.movementMotors.move(5)
|
|
if segment==1:
|
|
self.bewege_stift(1)
|
|
if (segment_nummer+1) != 3:
|
|
self.drehe_backstein_robot(grad_drehung)
|
|
|
|
|
|
def schreibeL(self, schreibe=True, zurueck=False):
|
|
if zurueck:
|
|
step = 5
|
|
faktor = -1
|
|
else:
|
|
step = 1
|
|
faktor = 1
|
|
print("Schreibe L")
|
|
#self.frontMotorRight.run_for_rotations(-0.4)
|
|
radius=9.5
|
|
stift_versatz=2.2
|
|
if schreibe:
|
|
self.frontMotorRight.run_for_rotations(0.4)
|
|
self.movementMotors.set_default_speed(10)
|
|
|
|
while (True):
|
|
if step == 0:
|
|
break
|
|
if step == 1:
|
|
self.movementMotors.move(faktor * 5)
|
|
if schreibe:
|
|
self.frontMotorRight.run_for_rotations(-0.4)
|
|
if step == 2:
|
|
self.movementMotors.move(faktor * (-radius - stift_versatz))
|
|
if step == 3:
|
|
self.drehe(faktor * -90)
|
|
if step == 4:
|
|
self.movementMotors.move(faktor*(radius - stift_versatz))
|
|
if schreibe:
|
|
self.frontMotorRight.run_for_rotations(0.4)
|
|
if step == 5:
|
|
self.movementMotors.move(faktor * 2)
|
|
if schreibe:
|
|
self.frontMotorRight.run_for_rotations(-0.4)
|
|
if step == 6:
|
|
break
|
|
step += faktor
|
|
# Fahre 5 cm rückwerts
|
|
# dann drehe nach rechts 90°
|
|
# und fahre 2cm fohrwärts
|
|
#stift hoch
|
|
|
|
|
|
def schreibeLego(self):
|
|
#self.schreibeL()
|
|
#self.schreibeL(schreibe=False, zurueck=True)
|
|
self.schreibe_buchstabe("L")
|
|
self.schreibe_buchstabe("E")
|
|
self.schreibe_buchstabe("G")
|
|
self.schreibe_buchstabe("O")
|
|
|
|
print("successfully loaded the IQ Lego teams code :)")
|
|
|
|
|