2023-02-14 16:43:41 +00:00
|
|
|
# LEGO type:standard slot:6 autostart
|
|
|
|
|
2023-09-27 19:00:25 +00:00
|
|
|
import math
|
|
|
|
|
|
|
|
from spike import PrimeHub, Motor, MotorPair, ColorSensor, MotionSensor
|
2023-02-14 16:43:41 +00:00
|
|
|
from spike.control import wait_for_seconds
|
|
|
|
|
|
|
|
HELLO = "HELLO IQ"
|
2023-10-05 06:56:46 +00:00
|
|
|
BRICKIES_BOT = "brickies"
|
|
|
|
BRICKIES_BOT_2 = "brickies_2"
|
|
|
|
BACKSTEIN_BOT = "backstein"
|
2023-02-14 16:43:41 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
Wir nutzen "Duck typing", dh wir schreiben hinter jede Variabel mit ':' die Klasse, zB `leftMotor: Motor`
|
|
|
|
damit man dann später auch wieder Code Completion hat bei Nutzung der Variablen
|
|
|
|
'''
|
|
|
|
class IQRobot:
|
|
|
|
|
2023-10-05 06:56:46 +00:00
|
|
|
def __init__(self, hub: PrimeHub, typ: str):
|
|
|
|
|
2023-02-14 16:43:41 +00:00
|
|
|
self.hub: PrimeHub = hub
|
2023-09-27 19:00:25 +00:00
|
|
|
self.typ=typ
|
2023-10-05 06:56:46 +00:00
|
|
|
|
|
|
|
if self.typ==BACKSTEIN_BOT:
|
|
|
|
# Radantrieb
|
2023-09-27 19:00:25 +00:00
|
|
|
LEFT_MOTOR_PORT = 'F'
|
|
|
|
RIGHT_MOTOR_PORT = 'B'
|
2023-10-05 06:56:46 +00:00
|
|
|
# Motoren für Aufsätze
|
2023-09-27 19:00:25 +00:00
|
|
|
FRONT_MOTOR_RIGHT_PORT = "E"
|
|
|
|
self.frontMotorRight: Motor = Motor(FRONT_MOTOR_RIGHT_PORT)
|
2023-10-05 06:56:46 +00:00
|
|
|
|
|
|
|
elif self.typ==BRICKIES_BOT:
|
|
|
|
# Radantrieb
|
2023-09-27 19:00:25 +00:00
|
|
|
LEFT_MOTOR_PORT = 'E'
|
|
|
|
RIGHT_MOTOR_PORT = 'F'
|
2023-10-05 06:56:46 +00:00
|
|
|
# Motoren für Aufsätze
|
2023-09-27 19:00:25 +00:00
|
|
|
FRONT_MOTOR_RIGHT_PORT = "B"
|
|
|
|
FRONT_MOTOR_LEFT_PORT = "A"
|
|
|
|
self.frontMotorRight: Motor = Motor(FRONT_MOTOR_RIGHT_PORT)
|
|
|
|
self.frontMotorLeft: Motor = Motor(FRONT_MOTOR_LEFT_PORT)
|
|
|
|
self.bothFrontMotors: MotorPair = MotorPair(FRONT_MOTOR_LEFT_PORT, FRONT_MOTOR_RIGHT_PORT)
|
2023-10-05 06:56:46 +00:00
|
|
|
# Radius der Antriebsräder
|
|
|
|
self.rad_radius = 2.1
|
2023-09-27 19:00:25 +00:00
|
|
|
# Abstand zwischen Rädern (Mitte) und Vorderseite des Roboters
|
|
|
|
self.abstand_rad_front = 5.55
|
2023-10-05 06:56:46 +00:00
|
|
|
|
|
|
|
elif self.typ==BRICKIES_BOT_2:
|
|
|
|
# Radantrieb
|
|
|
|
LEFT_MOTOR_PORT = 'E'
|
|
|
|
RIGHT_MOTOR_PORT = 'F'
|
|
|
|
# Radius der Antriebsräder
|
|
|
|
self.rad_radius = 2.9
|
|
|
|
# Abstand zwischen Rädern (Mitte) und Vorderseite des Roboters
|
|
|
|
self.abstand_rad_front = 8.5
|
|
|
|
|
|
|
|
## Allgemein ##
|
|
|
|
self.movementMotors: MotorPair = MotorPair(LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT)
|
|
|
|
# Radumfang neu berechnen und Motor konfigurieren
|
|
|
|
rad_umfang = 2 * math.pi * self.rad_radius
|
|
|
|
self.movementMotors.set_motor_rotation(rad_umfang)
|
2023-09-27 19:00:25 +00:00
|
|
|
self.leftMotor: Motor = Motor(LEFT_MOTOR_PORT)
|
2023-10-05 06:56:46 +00:00
|
|
|
self.rightMotor: Motor = Motor(RIGHT_MOTOR_PORT)
|
2023-09-27 19:00:25 +00:00
|
|
|
#self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
|
|
|
|
#self.frontMotorLeft: Motor = Motor("C")
|
|
|
|
self.motionSensor: MotionSensor = MotionSensor()
|
2023-02-14 16:43:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def show(self, image: str):
|
|
|
|
'''
|
|
|
|
Zeige Bild auf LED Matrix des Spikes
|
|
|
|
image: Bildname wie zB 'HAPPY'
|
|
|
|
'''
|
|
|
|
self.hub.light_matrix.show_image(image)
|
|
|
|
|
|
|
|
|
2023-02-15 08:07:56 +00:00
|
|
|
def driveForward_for_sec(self, seconds: float):
|
2023-02-14 16:43:41 +00:00
|
|
|
# Fahre die übergebene Anzahl seconds gerade aus
|
|
|
|
self.movementMotors.start()
|
|
|
|
wait_for_seconds(seconds)
|
|
|
|
self.movementMotors.stop()
|
|
|
|
|
2023-10-05 06:56:46 +00:00
|
|
|
|
2023-02-14 16:43:41 +00:00
|
|
|
def getColorIntensity(self):
|
|
|
|
# Ermittele Farbintensität über den Farbsensor
|
2023-09-27 19:00:25 +00:00
|
|
|
(red, green, blue, colorIntensity) = self .colorSensor.get_rgb_intensity()
|
2023-02-14 16:43:41 +00:00
|
|
|
return colorIntensity
|
|
|
|
|
|
|
|
|
2023-10-05 06:56:46 +00:00
|
|
|
def drehe(self, grad=90, with_reset=True):
|
2023-10-25 17:27:02 +00:00
|
|
|
if grad == 0 or grad == 360 :
|
|
|
|
print("nichts zu tun")
|
|
|
|
return
|
2023-10-05 06:56:46 +00:00
|
|
|
if with_reset:
|
|
|
|
self.motionSensor.reset_yaw_angle()
|
2023-10-25 17:27:02 +00:00
|
|
|
#steering = 100 if grad > 0 else -100
|
|
|
|
toleranz = 0
|
|
|
|
aktuell = self.motionSensor.get_yaw_angle()
|
|
|
|
ziel = grad
|
|
|
|
steering = 100 if ziel > aktuell else -100
|
2023-10-05 06:56:46 +00:00
|
|
|
self.movementMotors.start(steering=steering, speed=10)
|
2023-10-25 17:27:02 +00:00
|
|
|
differenz = ziel - aktuell
|
|
|
|
print ("Start Ziel: {}, Aktuell: {}".format(ziel, aktuell))
|
|
|
|
while abs(differenz) > toleranz :
|
|
|
|
aktuell = self.motionSensor.get_yaw_angle()
|
|
|
|
differenz = ziel - aktuell
|
2023-10-05 06:56:46 +00:00
|
|
|
pass
|
|
|
|
self.movementMotors.stop()
|
2023-10-25 17:27:02 +00:00
|
|
|
print ("Final Ziel: {}, Aktuell: {}".format(ziel, aktuell))
|
2023-10-05 06:56:46 +00:00
|
|
|
|
|
|
|
|
2023-09-27 19:00:25 +00:00
|
|
|
def fahre_gerade(self, cm):
|
|
|
|
if self.typ == "brickies":
|
|
|
|
cm = -cm
|
|
|
|
self.motionSensor.reset_yaw_angle()
|
|
|
|
self.movementMotors.start_tank(10, 10)
|
|
|
|
self.movementMotors.set_default_speed(10)
|
|
|
|
self.movementMotors.move(cm)
|
|
|
|
versatz = self.motionSensor.get_yaw_angle()
|
|
|
|
self.drehe(grad=-versatz)
|
|
|
|
|
|
|
|
|
|
|
|
def fahre_gerade_geregelt(self, cm):
|
|
|
|
if self.typ == "brickies":
|
|
|
|
cm = -cm
|
|
|
|
self.motionSensor.reset_yaw_angle()
|
|
|
|
self.movementMotors.start_tank(10, 10)
|
|
|
|
self.movementMotors.set_default_speed(10)
|
|
|
|
|
|
|
|
sum_cm = 0
|
|
|
|
while sum_cm < cm:
|
|
|
|
self.movementMotors.move(1)
|
|
|
|
versatz = self.motionSensor.get_yaw_angle()
|
|
|
|
self.drehe(grad=-versatz)
|
|
|
|
self.motionSensor.reset_yaw_angle()
|
|
|
|
sum_cm = sum_cm + 1
|
|
|
|
|
|
|
|
self.movementMotors.move(cm - sum_cm)
|
|
|
|
|
|
|
|
def fahre_mit_drehung(self, strecke1, grad, strecke2):
|
|
|
|
self.fahre_gerade_geregelt(strecke1 + self.abstand_rad_front)
|
|
|
|
self.drehe(grad)
|
|
|
|
self.fahre_gerade_geregelt(strecke2 - self.abstand_rad_front)
|
2023-10-25 17:27:02 +00:00
|
|
|
|
|
|
|
def fahre_gerade_aus(self, cm,speed):
|
|
|
|
|
|
|
|
self.motionSensor.reset_yaw_angle()
|
|
|
|
|
|
|
|
self.movementMotors.move_tank(amount=cm,left_speed=speed, right_speed=speed)
|
|
|
|
drehung = self.motionSensor.get_yaw_angle()
|
|
|
|
print(drehung)
|
|
|
|
if drehung > 0:
|
|
|
|
richtung = -1
|
|
|
|
else:
|
|
|
|
richtung = 1
|
|
|
|
while abs(drehung) > 2:
|
|
|
|
self.movementMotors.move(amount=richtung * 0.1, steering=100)
|
|
|
|
drehung = self.motionSensor.get_yaw_angle()
|
|
|
|
print(drehung)
|
2023-02-14 16:43:41 +00:00
|
|
|
|
2023-10-25 17:27:02 +00:00
|
|
|
|
2023-02-15 07:58:29 +00:00
|
|
|
print("successfully loaded the IQ Lego teams code :)")
|
2023-02-14 16:43:41 +00:00
|
|
|
|
|
|
|
|
2023-10-25 17:27:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|