Farbsensor funktionalisiert

This commit is contained in:
flhipp 2023-03-08 17:50:37 +01:00
parent 2511adc3c6
commit 57096f642d
2 changed files with 26 additions and 5 deletions

View file

@ -33,6 +33,14 @@ class IQRobot:
wait_for_seconds(seconds) wait_for_seconds(seconds)
self.movementMotors.stop() 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): def getColorIntensity(self):
# Ermittele Farbintensität über den Farbsensor # Ermittele Farbintensität über den Farbsensor
(red, green, blue, colorIntensity) = self.colorSensor.get_rgb_intensity() (red, green, blue, colorIntensity) = self.colorSensor.get_rgb_intensity()
@ -40,6 +48,9 @@ class IQRobot:
def main(self): def main(self):
self
'''
self.moveStick(30) self.moveStick(30)
if self.colorSensor.get_reflected_light() > 1: if self.colorSensor.get_reflected_light() > 1:
self.show('ANGRY') self.show('ANGRY')
@ -49,13 +60,10 @@ class IQRobot:
colorIntensity = self.getColorIntensity() colorIntensity = self.getColorIntensity()
print("Farbintensität: " + str(colorIntensity)) print("Farbintensität: " + str(colorIntensity))
'''
def moveStick(self,degrees) : def moveStick(self,degrees) :
#Bewege sanft und langsam die Schleuderstange am hinteren Rumpf des Geräts #Bewege sanft und langsam die Schleuderstange am hinteren Rumpf des Geräts
self.stickMotor.run_for_degrees(degrees) self.stickMotor.run_for_degrees(degrees)
print("successfully loaded the IQ Lego teams code :)") print("successfully loaded the IQ Lego teams code :)")

15
main.py
View file

@ -12,6 +12,7 @@ if battery.voltage() < 8000: #set threshold for battery level
print("Spannung der Batterie zu niedrig: " + str(battery.voltage()) + " \n" print("Spannung der Batterie zu niedrig: " + str(battery.voltage()) + " \n"
+ "--------------------------------------- \n " + "--------------------------------------- \n "
+ "#### UNBEDINGT ROBOTER AUFLADEN !!! #### \n" + "#### UNBEDINGT ROBOTER AUFLADEN !!! #### \n"
+ "+++"
+ "---------------------------------------- \n") + "---------------------------------------- \n")
else: else:
print("Spannung der Batterie " + str(battery.voltage()) + "\n") 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) 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: # Führe Funktionen aus unser Robot Klasse aus:
iqRobot.main() #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')