diff --git a/iqrobot.py b/iqrobot.py index b830cd8..dc11612 100644 --- a/iqrobot.py +++ b/iqrobot.py @@ -26,6 +26,7 @@ class IQRobot: FRONT_MOTOR_RIGHT_PORT = "B" FRONT_MOTOR_LEFT_PORT = "A" + self.farbSensor: ColorSensor = ColorSensor("C") self.bothFrontMotors: MotorPair = MotorPair(FRONT_MOTOR_LEFT_PORT, FRONT_MOTOR_RIGHT_PORT) self.linker_motor_vorne: Motor = Motor(FRONT_MOTOR_LEFT_PORT) self.linker_motor_vorne.set_stall_detection(stop_when_stalled=True) @@ -45,7 +46,10 @@ class IQRobot: self.bewegungsSensor: MotionSensor = MotionSensor() - self.abstandsSensor: DistanceSensor = DistanceSensor("D") + try: + self.abstandsSensor: DistanceSensor = DistanceSensor("D") + except: + self.abstandsSensor: DistanceSensor = DistanceSensor("C") def show(self, image: str): ''' @@ -98,7 +102,7 @@ class IQRobot: self.antrieb.stop() print ("Final Ziel: {}, Aktuell: {}".format(ziel, aktuell)) - def fahre_gerade_aus(self, cm, speed=20): + def fahre_gerade_aus(self, cm, speed=20, intensity_stop=None, abstand_stop=None, abstand_greater=True): """ Funktion um den Roboter geradeaus fahren zu lassen @@ -122,8 +126,8 @@ class IQRobot: self.bewegungsSensor.reset_yaw_angle() # Mit irgendeiner Geschwindigkeit g>0 starten. Wert ist irrelevant - self.antrieb.start_tank(10, 10) - self.antrieb.set_default_speed(10) + self.antrieb.start_tank(richtung*10, richtung*10) + self.antrieb.set_default_speed(richtung*10) linker_speed=speed # Geschwindigkeit linker Motor rechter_speed=speed # Geschwindigkeit rechter Motor @@ -134,7 +138,21 @@ class IQRobot: sum_versatz = 0 # Summe des Versatzes über Zeit # wiederhole solange die gefahrene Strecke noch nicht erreicht ist - while sum_cm < cm * richtung: + while sum_cm < cm * richtung: + if intensity_stop: + red, green, blue, intensity = self.farbSensor.get_rgb_intensity() + print("Farbintensität: " + str(intensity)) + if intensity == intensity_stop: + break + if abstand_stop: + abstand = self.abstandsSensor.get_distance_cm() + print("Abstand: " + str(abstand)) + if abstand_greater: + if abstand >= abstand_stop: + break + else: + if abstand <= abstand_stop: + break wait_for_seconds(0.05) # Sonst wird das zu oft ausgeführt sum_cm = self.strecke_gefahren() * richtung # Gefahrene Strecke, ggf. eben negativ machen versatz = self.bewegungsSensor.get_yaw_angle() # Um wie viel sind wir falsch? @@ -193,7 +211,7 @@ class IQRobot: """ self.bothFrontMotors.move_tank(-cm*3.3,"cm", -speed, speed) # Heber bewegen - def schaufel(self,prozent, speed=20): + def schaufel(self,prozent, speed=100): """ Lässt die Schaufel fahren diff --git a/main.py b/main.py index 723c52b..025ae10 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -# LEGO type:standard slot:5 autostart +# LEGO type:standard slot:5 import os, sys @@ -88,32 +88,61 @@ def hologram_alt(): iqRobot.fahre_gerade_aus(cm=-75,speed=50) def druckmaschine(): - iqRobot.fahre_gerade_aus(19,30) + iqRobot.fahre_gerade_aus(19,70) iqRobot.drehe(-45) - iqRobot.fahre_gerade_aus(20,30) - iqRobot.fahre_gerade_aus(-15,30) + iqRobot.fahre_gerade_aus(25,50) + iqRobot.fahre_gerade_aus(-15,40) def hologram(): iqRobot.drehe(45) - iqRobot.fahre_gerade_aus(37.5,30) + iqRobot.fahre_gerade_aus(37.5,60) + iqRobot.fahre_bis_abstand(28,30,True) + print(iqRobot.abstandsSensor.get_distance_cm()) iqRobot.drehe(45) - iqRobot.fahre_gerade_aus(15,30) + iqRobot.antrieb.move_tank(15,left_speed=30,right_speed=30) + #iqRobot.fahre_gerade_aus(15,30) iqRobot.fahre_gerade_aus(-15,30) def augmented_reality(): - iqRobot.drehe(-135) - iqRobot.fahre_gerade_aus(42,30) - iqRobot.drehe(90) + iqRobot.drehe(45) + # cm should be greater than real distance as we stop at color intensity + iqRobot.fahre_gerade_aus(cm=-50,speed=50, abstand_stop=86) + iqRobot.drehe(-90) iqRobot.fahre_gerade_aus(12,30) - iqRobot.schaufel(-100) - iqRobot.fahre_gerade_aus(-3,30) + iqRobot.schaufel(-2200) + iqRobot.fahre_gerade_aus(-4,30) iqRobot.drehe(90) iqRobot.fahre_gerade_aus(20,30) - iqRobot.drehe(-90) - iqRobot.fahre_gerade_aus(5,20) + #iqRobot.drehe(-90) + #iqRobot.fahre_gerade_aus(5,20) + +def backHome(): + iqRobot.fahre_gerade_aus(-5, 30) + iqRobot.drehe(138) + iqRobot.fahre_gerade_aus_alt(80, 100) + #iqRobot.fahre_gerade_aus(16, 20) #iqRobot.drehe(38) #iqRobot.fahre_gerade_aus(33,25) -iqRobot.schaufel(1600, speed=100 ) -iqRobot.schaufel(-1600, speed=100 ) \ No newline at end of file +#iqRobot.schaufel(1600, speed=100 ) +#iqRobot.schaufel(-1600, speed=100 ) + +iqRobot.schaufel(3000) +druckmaschine() +hologram() +augmented_reality() +#backHome() +#huenchenaufgabe() + +#iqRobot.farbSensor.light_up(1,1,1) + +#wait_for_seconds(2) + +#while 0 == 0: + #print(iqRobot.abstandsSensor.get_distance_cm()) + #print(iqRobot.farbSensor.get_reflected_light()) + #print(str(iqRobot.farbSensor.get_blue())+" "+str(iqRobot.farbSensor.get_green())+" "+str(iqRobot.farbSensor.get_red())) + #wait_for_seconds(0.5) + #print(str(iqRobot.abstandsSensor.get_distance_cm())) +