Compare commits
No commits in common. "6b094d3f9cf6557492af54fb39715b5fce309cc0" and "31510f9e2134d697e28cc60ece551f4b0cb6dc0e" have entirely different histories.
6b094d3f9c
...
31510f9e21
2 changed files with 14 additions and 80 deletions
76
iqrobot.py
76
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
|
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`
|
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.leftMotor: Motor = Motor(leftMotorPort)
|
||||||
self.rightMotor: Motor = Motor(rightMotorPort)
|
self.rightMotor: Motor = Motor(rightMotorPort)
|
||||||
self.movementMotors: MotorPair = MotorPair(leftMotorPort, rightMotorPort)
|
self.movementMotors: MotorPair = MotorPair(leftMotorPort, rightMotorPort)
|
||||||
#self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
|
self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
|
||||||
#self.frontMotorLeft: Motor = Motor("C")
|
|
||||||
self.frontMotorRight: Motor = Motor("E")
|
|
||||||
self.motionSensor: MotionSensor = MotionSensor()
|
|
||||||
|
|
||||||
|
|
||||||
def show(self, image: str):
|
def show(self, image: str):
|
||||||
|
@ -41,75 +38,10 @@ class IQRobot:
|
||||||
(red, green, blue, colorIntensity) = self.colorSensor.get_rgb_intensity()
|
(red, green, blue, colorIntensity) = self.colorSensor.get_rgb_intensity()
|
||||||
return colorIntensity
|
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 :)")
|
print("successfully loaded the IQ Lego teams code :)")
|
||||||
|
|
||||||
|
|
||||||
|
|
18
main.py
18
main.py
|
@ -1,4 +1,4 @@
|
||||||
# LEGO type:standard slot:4 autostart
|
# LEGO type:standard slot:5 autostart
|
||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
|
|
||||||
|
@ -33,10 +33,9 @@ def importFile(slotid=0, precompiled=False, module_name='importFile'):
|
||||||
program = f.read()
|
program = f.read()
|
||||||
print(program)
|
print(program)
|
||||||
try:
|
try:
|
||||||
os.remove("/"+module_name+".py")
|
os.remove("/"+module_name+suffix)
|
||||||
os.remove("/"+module_name+".mpy")
|
|
||||||
except:
|
except:
|
||||||
print("Couldn't remove old module")
|
pass
|
||||||
with open("/"+module_name+suffix,"w+") as f:
|
with open("/"+module_name+suffix,"w+") as f:
|
||||||
print("trying to write import program")
|
print("trying to write import program")
|
||||||
f.write(program)
|
f.write(program)
|
||||||
|
@ -51,7 +50,7 @@ def importFile(slotid=0, precompiled=False, module_name='importFile'):
|
||||||
|
|
||||||
# Importiere Code aus der Datei "iqrobot.py"
|
# Importiere Code aus der Datei "iqrobot.py"
|
||||||
# Dateiname und Modulname sollten gleich sein, dann kann man Code Completion nutzen
|
# 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
|
import iqrobot as iq
|
||||||
print(iq.HELLO)
|
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
|
# Definiere an welchen Ports die Sensoren angeschlossen sind
|
||||||
COLOR_SENSOR_PORT = 'E' #not implemented yet
|
COLOR_SENSOR_PORT = 'E'
|
||||||
LEFT_MOTOR_PORT = 'F'
|
LEFT_MOTOR_PORT = 'A'
|
||||||
RIGHT_MOTOR_PORT = 'B'
|
RIGHT_MOTOR_PORT = 'B'
|
||||||
|
|
||||||
# Initialisieren des Hubs, der Aktoren und Sensoren
|
# 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:
|
# Führe Funktionen aus unser Robot Klasse aus:
|
||||||
iqRobot.show('HAPPY')
|
iqRobot.show('HAPPY')
|
||||||
iqRobot.schreibeLego()
|
iqRobot.driveForward_for_sec(2.0)
|
||||||
|
colorIntensity = iqRobot.getColorIntensity()
|
||||||
|
print("Farbintensität: " + str(colorIntensity))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue