schreibe L mit Backstein Robot

This commit is contained in:
unknown 2023-05-24 19:17:57 +02:00
parent e07670468c
commit 6b094d3f9c
2 changed files with 49 additions and 19 deletions

View file

@ -1,9 +1,9 @@
# LEGO type:standard slot:6 autostart
# LEGO type:standard slot:7 autostart
from spike import PrimeHub, Motor, MotorPair, ColorSensor, MotionSensor
from spike.control import wait_for_seconds
HELLO = "HELLO IQ"
HELLO = "HELLO IQ 2"
'''
Wir nutzen "Duck typing", dh wir schreiben hinter jede Variabel mit ':' die Klasse, zB `leftMotor: Motor`
@ -17,8 +17,8 @@ class IQRobot:
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("D")
#self.frontMotorLeft: Motor = Motor("C")
self.frontMotorRight: Motor = Motor("E")
self.motionSensor: MotionSensor = MotionSensor()
@ -50,16 +50,44 @@ class IQRobot:
self.movementMotors.stop()
def schreibeL(self):
print("Schreibe L")
self.frontMotorRight.run_for_rotations(-0.1)
self.movementMotors.set_default_speed(20)
self.movementMotors.move(5)
#self.movementMotors.move(7, steering=100)
self.drehe(-90)
self.movementMotors.move(2)
self.frontMotorRight.run_for_rotations(0.1)
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
@ -77,6 +105,7 @@ class IQRobot:
def schreibeLego(self):
self.schreibeL()
self.schreibeL(schreibe=False, zurueck=True)
self.schreibeE()
self.schreibeG()
self.schreibeO()

13
main.py
View file

@ -1,4 +1,4 @@
# LEGO type:standard slot:5
# LEGO type:standard slot:4 autostart
import os, sys
@ -33,9 +33,10 @@ def importFile(slotid=0, precompiled=False, module_name='importFile'):
program = f.read()
print(program)
try:
os.remove("/"+module_name+suffix)
os.remove("/"+module_name+".py")
os.remove("/"+module_name+".mpy")
except:
pass
print("Couldn't remove old module")
with open("/"+module_name+suffix,"w+") as f:
print("trying to write import program")
f.write(program)
@ -50,7 +51,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=6, precompiled=True, module_name="iqrobot")
importFile(slotid=7, precompiled=True, module_name="iqrobot")
import iqrobot as iq
print(iq.HELLO)
@ -72,8 +73,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 = 'E'
RIGHT_MOTOR_PORT = 'F'
LEFT_MOTOR_PORT = 'F'
RIGHT_MOTOR_PORT = 'B'
# Initialisieren des Hubs, der Aktoren und Sensoren
hub = PrimeHub()