diff --git a/iqrobot.py b/iqrobot.py index 0ff3148..1595e5b 100644 --- a/iqrobot.py +++ b/iqrobot.py @@ -1,9 +1,9 @@ -# LEGO type:standard slot:7 autostart +# LEGO type:standard slot:6 autostart -from spike import PrimeHub, Motor, MotorPair, ColorSensor, MotionSensor +from spike import PrimeHub, Motor, MotorPair, ColorSensor from spike.control import wait_for_seconds -HELLO = "HELLO IQ 2" +HELLO = "HELLO IQ" ''' Wir nutzen "Duck typing", dh wir schreiben hinter jede Variabel mit ':' die Klasse, zB `leftMotor: Motor` @@ -16,10 +16,7 @@ class IQRobot: 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() + self.colorSensor: ColorSensor = ColorSensor(colorSensorPort) def show(self, image: str): @@ -41,75 +38,10 @@ class IQRobot: (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 schreibeL(self, schreibe=True, zurueck=False): - if zurueck: - step = 5 - faktor = -1 - else: - step = 1 - faktor = 1 - print("Schreibe L") - 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 schreibeE(self): - print("Schreibe E") - - def schreibeG(self): - print("Schreibe G") - - - def schreibeO(self): - print("Schreibe O") - - def schreibeLego(self): - self.schreibeL() - self.schreibeL(schreibe=False, zurueck=True) - self.schreibeE() - self.schreibeG() - self.schreibeO() - print("successfully loaded the IQ Lego teams code :)") diff --git a/main.py b/main.py index 2179ecb..c6ba5c2 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -# LEGO type:standard slot:4 autostart +# LEGO type:standard slot:5 autostart import os, sys @@ -33,10 +33,9 @@ def importFile(slotid=0, precompiled=False, module_name='importFile'): program = f.read() print(program) try: - os.remove("/"+module_name+".py") - os.remove("/"+module_name+".mpy") + os.remove("/"+module_name+suffix) except: - print("Couldn't remove old module") + pass with open("/"+module_name+suffix,"w+") as f: print("trying to write import program") f.write(program) @@ -51,7 +50,7 @@ def importFile(slotid=0, precompiled=False, module_name='importFile'): # Importiere Code aus der Datei "iqrobot.py" # Dateiname und Modulname sollten gleich sein, dann kann man Code Completion nutzen -importFile(slotid=7, precompiled=True, module_name="iqrobot") +importFile(slotid=6, precompiled=True, module_name="iqrobot") import iqrobot as iq print(iq.HELLO) @@ -72,8 +71,8 @@ dh auch an die Funktionen im importierten Code übergeben werde ''' # Definiere an welchen Ports die Sensoren angeschlossen sind -COLOR_SENSOR_PORT = 'E' #not implemented yet -LEFT_MOTOR_PORT = 'F' +COLOR_SENSOR_PORT = 'E' +LEFT_MOTOR_PORT = 'A' RIGHT_MOTOR_PORT = 'B' # Initialisieren des Hubs, der Aktoren und Sensoren @@ -84,7 +83,10 @@ iqRobot: iq.IQRobot = iq.IQRobot(hub, LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT, COLOR_S # Führe Funktionen aus unser Robot Klasse aus: iqRobot.show('HAPPY') -iqRobot.schreibeLego() +iqRobot.driveForward_for_sec(2.0) +colorIntensity = iqRobot.getColorIntensity() +print("Farbintensität: " + str(colorIntensity)) +