69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
# LEGO type:standard slot:6 autostart
|
|
|
|
from spike import PrimeHub, Motor, MotorPair, ColorSensor
|
|
from spike.control import wait_for_seconds
|
|
|
|
HELLO = "HELLO IQ"
|
|
VERSION = "V1"
|
|
|
|
'''
|
|
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, stickMotorPort: 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.stickMotor: Motor = Motor(stickMotorPort)
|
|
|
|
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 driveBackward_for_sec(self, seconds: float):
|
|
# Fahre die übergebene Anzahl seconds gerade aus
|
|
self.movementMotors.set_default_speed(-100)()
|
|
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 main(self):
|
|
|
|
'''
|
|
self.moveStick(30)
|
|
if self.colorSensor.get_reflected_light() > 1:
|
|
self.show('ANGRY')
|
|
else:
|
|
self.show('SAD')
|
|
|
|
|
|
colorIntensity = self.getColorIntensity()
|
|
print("Farbintensität: " + str(colorIntensity))
|
|
'''
|
|
|
|
def moveStick(self,degrees) :
|
|
#Bewege sanft und langsam die Schleuderstange am hinteren Rumpf des Geräts
|
|
self.stickMotor.run_for_degrees(degrees)
|
|
|
|
print("Loading library IQRobot in version " + VERSION)
|