Aktuelle Version aus Fahre mit Regler #4
2 changed files with 43 additions and 18 deletions
54
iqrobot.py
54
iqrobot.py
|
@ -6,6 +6,9 @@ from spike import PrimeHub, Motor, MotorPair, ColorSensor, MotionSensor
|
|||
from spike.control import wait_for_seconds
|
||||
|
||||
HELLO = "HELLO IQ"
|
||||
BRICKIES_BOT = "brickies"
|
||||
BRICKIES_BOT_2 = "brickies_2"
|
||||
BACKSTEIN_BOT = "backstein"
|
||||
|
||||
'''
|
||||
Wir nutzen "Duck typing", dh wir schreiben hinter jede Variabel mit ':' die Klasse, zB `leftMotor: Motor`
|
||||
|
@ -13,34 +16,50 @@ damit man dann später auch wieder Code Completion hat bei Nutzung der Variablen
|
|||
'''
|
||||
class IQRobot:
|
||||
|
||||
def __init__(self, hub: PrimeHub, leftMotorPort: str, rightMotorPort: str, colorSensorPort: str):
|
||||
def __init__(self, hub: PrimeHub, typ: str):
|
||||
|
||||
self.hub: PrimeHub = hub
|
||||
self.typ=typ
|
||||
if self.typ=="backstein":
|
||||
|
||||
if self.typ==BACKSTEIN_BOT:
|
||||
# Radantrieb
|
||||
LEFT_MOTOR_PORT = 'F'
|
||||
RIGHT_MOTOR_PORT = 'B'
|
||||
# Motoren für Aufsätze
|
||||
FRONT_MOTOR_RIGHT_PORT = "E"
|
||||
self.frontMotorRight: Motor = Motor(FRONT_MOTOR_RIGHT_PORT)
|
||||
self.movementMotors: MotorPair = MotorPair(LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT)
|
||||
elif self.typ=="brickies":
|
||||
|
||||
elif self.typ==BRICKIES_BOT:
|
||||
# Radantrieb
|
||||
LEFT_MOTOR_PORT = 'E'
|
||||
RIGHT_MOTOR_PORT = 'F'
|
||||
# Motoren für Aufsätze
|
||||
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)
|
||||
self.movementMotors: MotorPair = MotorPair(LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT)
|
||||
# Radumfang neu berechnen und Motor konfigurieren
|
||||
rad_radius = 2.1
|
||||
rad_umfang = 2 * math.pi * rad_radius
|
||||
self.movementMotors.set_motor_rotation(rad_umfang)
|
||||
# Radius der Antriebsräder
|
||||
self.rad_radius = 2.1
|
||||
# Abstand zwischen Rädern (Mitte) und Vorderseite des Roboters
|
||||
self.abstand_rad_front = 5.55
|
||||
|
||||
|
||||
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)
|
||||
self.leftMotor: Motor = Motor(LEFT_MOTOR_PORT)
|
||||
self.rightMotor: Motor = Motor(RIGHT_MOTOR_PORT)
|
||||
|
||||
self.rightMotor: Motor = Motor(RIGHT_MOTOR_PORT)
|
||||
#self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
|
||||
#self.frontMotorLeft: Motor = Motor("C")
|
||||
self.motionSensor: MotionSensor = MotionSensor()
|
||||
|
@ -60,12 +79,23 @@ class IQRobot:
|
|||
wait_for_seconds(seconds)
|
||||
self.movementMotors.stop()
|
||||
|
||||
|
||||
def getColorIntensity(self):
|
||||
# Ermittele Farbintensität über den Farbsensor
|
||||
(red, green, blue, colorIntensity) = self .colorSensor.get_rgb_intensity()
|
||||
return colorIntensity
|
||||
|
||||
|
||||
def drehe(self, grad=90, with_reset=True):
|
||||
if with_reset:
|
||||
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 fahre_gerade(self, cm):
|
||||
if self.typ == "brickies":
|
||||
cm = -cm
|
||||
|
|
7
main.py
7
main.py
|
@ -70,16 +70,11 @@ und auch `hub` als Instanz von PrimeHub
|
|||
dh auch an die Funktionen im importierten Code übergeben werde
|
||||
'''
|
||||
|
||||
# Definiere an welchen Ports die Sensoren angeschlossen sind
|
||||
COLOR_SENSOR_PORT = 'E'
|
||||
LEFT_MOTOR_PORT = 'A'
|
||||
RIGHT_MOTOR_PORT = 'B'
|
||||
|
||||
# Initialisieren des Hubs, der Aktoren und Sensoren
|
||||
hub = PrimeHub()
|
||||
|
||||
# Initialisiere Robot Klasse mit unseren Funktionen
|
||||
iqRobot: iq.IQRobot = iq.IQRobot(hub, LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT, COLOR_SENSOR_PORT)
|
||||
iqRobot: iq.IQRobot = iq.IQRobot(hub, typ=iq.BRICKIES_BOT)
|
||||
|
||||
# Führe Funktionen aus unser Robot Klasse aus:
|
||||
iqRobot.show('HAPPY')
|
||||
|
|
Loading…
Reference in a new issue