DistanceSensor hinzugefügt + getDIstance Methode hinzugefügt

This commit is contained in:
vilenne 2023-03-15 19:03:33 +01:00
parent ee76b97d83
commit 377195855a
2 changed files with 41 additions and 24 deletions

View file

@ -1,6 +1,6 @@
# LEGO type:standard slot:6 autostart
from spike import PrimeHub, Motor, MotorPair, ColorSensor
from spike import PrimeHub, Motor, MotorPair, ColorSensor, DistanceSensor
from spike.control import wait_for_seconds
HELLO = "HELLO IQ"
@ -12,14 +12,22 @@ 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, stickMotorPort: str, gabelMotorPort: str):
def __init__(self, hub: PrimeHub, leftMotorPort: str, rightMotorPort: str, colorSensorPort: str, stickMotorPort: str, gabelMotorPort: str, distanceSensorPort: str):
self.hub: PrimeHub = hub
self.leftMotor: Motor = Motor(leftMotorPort)
self.rightMotor: Motor = Motor(rightMotorPort)
self.movementMotors: MotorPair = MotorPair(leftMotorPort, rightMotorPort)
self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
self.stickMotor: Motor = Motor(stickMotorPort)
self.gabelMotor: Motor = Motor(gabelMotorPort)
if (leftMotorPort != ''):
self.leftMotor: Motor = Motor(leftMotorPort)
if (rightMotorPort != ''):
self.rightMotor: Motor = Motor(rightMotorPort)
if (leftMotorPort != '' and rightMotorPort != ''):
self.movementMotors: MotorPair = MotorPair(leftMotorPort, rightMotorPort)
if (colorSensorPort != ''):
self.colorSensor: ColorSensor = ColorSensor(colorSensorPort)
if (stickMotorPort != ''):
self.stickMotor: Motor = Motor(stickMotorPort)
if (gabelMotorPort != ''):
self.gabelMotor: Motor = Motor(gabelMotorPort)
if (distanceSensorPort != ''):
self.distanceSensor: DistanceSensor = DistanceSensor(distanceSensorPort)
def show(self, image: str):
'''
@ -71,5 +79,12 @@ class IQRobot:
#Bewege sanft und langsam die Schleuderstange am hinteren Rumpf des Geräts
self.gabelMotor.run_for_degrees(degrees)
print("Loading library IQRobot in version " + VERSION)
def getDistance (self) :
cmDistance = self.distanceSensor.get_distance_cm(short_range=False)
return cmDistance
def getShortDistance (self) :
cmDistance = self.distanceSensor.get_distance_cm(short_range=True)
return cmDistance
print("Loading library IQRobot in version " + VERSION)

30
main.py
View file

@ -56,21 +56,22 @@ import iqrobot as iq
print(iq.HELLO)
# Definiere an welchen Ports die Sensoren angeschlossen sind
COLOR_SENSOR_PORT = 'E'
LEFT_MOTOR_PORT = 'A'
RIGHT_MOTOR_PORT = 'B'
STICK_MOTOR_PORT = 'C'
GABEL_MOTOR_PORT = 'D'
COLOR_SENSOR_PORT = ''
LEFT_MOTOR_PORT = ''
RIGHT_MOTOR_PORT = ''
STICK_MOTOR_PORT = ''
GABEL_MOTOR_PORT = ''
DISTANCE_SENSOR_PORT = 'F'
# 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, STICK_MOTOR_PORT, GABEL_MOTOR_PORT)
iqRobot: iq.IQRobot = iq.IQRobot(hub, LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT, COLOR_SENSOR_PORT, STICK_MOTOR_PORT, GABEL_MOTOR_PORT, DISTANCE_SENSOR_PORT)
'''
while(True):
print('DIE FARBE IST ', iqRobot.colorSensor.get_color())
while(True):iqRobot.moveGabel(50)or())
if (iqRobot.colorSensor.get_color() == "cyan"):
iqRobot.driveForward_for_sec(1)
print('Blau du Affe')
@ -81,11 +82,12 @@ while(True):
print('Keine Farbe!')
'''
iqRobot.moveGabel(50)
while (True):
print('Kurz:' + str(iqRobot.getShortDistance()))
wait_for_seconds(1)
'''
print('Lang:' + str(iqRobot.getDistance()))
wait_for_seconds(5)
'''