From 57096f642d6b37f4cdcd06dc6101be37a9ef686c Mon Sep 17 00:00:00 2001 From: flhipp Date: Wed, 8 Mar 2023 17:50:37 +0100 Subject: [PATCH] Farbsensor funktionalisiert --- iqrobot.py | 16 ++++++++++++---- main.py | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/iqrobot.py b/iqrobot.py index 55cda1a..c56c882 100644 --- a/iqrobot.py +++ b/iqrobot.py @@ -33,6 +33,14 @@ class IQRobot: 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() @@ -40,6 +48,9 @@ class IQRobot: def main(self): + + self + ''' self.moveStick(30) if self.colorSensor.get_reflected_light() > 1: self.show('ANGRY') @@ -49,13 +60,10 @@ class IQRobot: 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("successfully loaded the IQ Lego teams code :)") - - - - diff --git a/main.py b/main.py index fa4fcec..6f5ef1c 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,7 @@ if battery.voltage() < 8000: #set threshold for battery level print("Spannung der Batterie zu niedrig: " + str(battery.voltage()) + " \n" + "--------------------------------------- \n " + "#### UNBEDINGT ROBOTER AUFLADEN !!! #### \n" + + "+++" + "---------------------------------------- \n") else: print("Spannung der Batterie " + str(battery.voltage()) + "\n") @@ -83,4 +84,16 @@ hub = PrimeHub() iqRobot: iq.IQRobot = iq.IQRobot(hub, LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT, COLOR_SENSOR_PORT, STICK_MOTOR_PORT) # Führe Funktionen aus unser Robot Klasse aus: -iqRobot.main() \ No newline at end of file +#iqRobot.main() + +while(True): + print('DIE FARBE IST ', iqRobot.colorSensor.get_color()) + if (iqRobot.colorSensor.get_color() == "cyan"): + iqRobot.driveForward_for_sec(1) + print('Blau du Affe') + elif(iqRobot.colorSensor.get_red() == "red"): + iqRobot.driveForward_for_sec(1) + print('Rot du Affe') + else: + print('Keine Farbe') +