The Ultraschallsensor is finished and all addresses are added.
This commit is contained in:
@@ -28,19 +28,33 @@ class Ultrasonic():
|
||||
#
|
||||
# Diese Methode soll ein Datenbyte an den Ultraschallsensor senden um eine Messung zu starten
|
||||
def write(self,value):
|
||||
try:
|
||||
bus.write_byte_data(self.address, 0x00, 0x51)
|
||||
except:
|
||||
print "ERROR: Ultrasonic sensor start failed!"
|
||||
return 0
|
||||
|
||||
# Aufgabe 2
|
||||
#
|
||||
# Diese Methode soll den Lichtwert auslesen und zurueckgeben.
|
||||
def get_brightness(self):
|
||||
return 0
|
||||
try:
|
||||
light_v = bus.read_byte_data(self.address, 0x01)
|
||||
except:
|
||||
print "ERROR: Ultrasonic sensor get brightness failed!"
|
||||
return light_v
|
||||
|
||||
# Aufgabe 2
|
||||
#
|
||||
# Diese Methode soll die Entfernung auslesen und zurueckgeben.
|
||||
def get_distance(self):
|
||||
return 0
|
||||
try:
|
||||
range_High_Byte = bus.read_byte_data(self.address, 0x02)
|
||||
range_Low_Byte = bus.read_byte_data(self.address, 0x03)
|
||||
dist = (range_High_Byte << 8) + range_Low_Byte
|
||||
except:
|
||||
print "ERROR: Ultrasonic sensor get distance failed!"
|
||||
return dist
|
||||
|
||||
def get_address(self):
|
||||
return self.address
|
||||
@@ -48,6 +62,8 @@ class Ultrasonic():
|
||||
class UltrasonicThread(threading.Thread):
|
||||
''' Thread-class for holding ultrasonic data '''
|
||||
|
||||
stop = False
|
||||
|
||||
# distance to obstacle in cm
|
||||
distance = 0
|
||||
|
||||
@@ -58,6 +74,14 @@ class UltrasonicThread(threading.Thread):
|
||||
#
|
||||
# Hier muss der Thread initialisiert werden.
|
||||
def __init__(self, address):
|
||||
try:
|
||||
threading.Thread.__init__(self)
|
||||
time.sleep(0.1)
|
||||
self.ultrasonic = Ultrasonic(address)
|
||||
self.setDaemon(True)
|
||||
self.start()
|
||||
except:
|
||||
print "ERROR: Ultrasonic sensor thread init failed!"
|
||||
return 0
|
||||
|
||||
# Aufgabe 4
|
||||
@@ -65,6 +89,8 @@ class UltrasonicThread(threading.Thread):
|
||||
# Schreiben Sie die Messwerte in die lokalen Variablen
|
||||
def run(self):
|
||||
while not self.stopped:
|
||||
distance = self.ultrasonic.get_distance()
|
||||
brightness = self.ultrasonic.get_brightness()
|
||||
continue
|
||||
|
||||
def stop(self):
|
||||
@@ -193,24 +219,33 @@ class Encoder(object):
|
||||
# Aufgabe 2
|
||||
#
|
||||
# Wieviel cm betraegt ein einzelner Encoder-Schritt?
|
||||
STEP_LENGTH = 0 # in cm
|
||||
STEP_LENGTH = 0 # in cm !!!ONP: to measure the wheel!!!
|
||||
|
||||
# number of encoder steps
|
||||
count = 0
|
||||
counter = 0
|
||||
|
||||
def __init__(self, pin):
|
||||
self.pin = pin
|
||||
GPIO.add_event_detect(encoder_pin, GPIO.BOTH, callback=count, bouncetime=1)
|
||||
|
||||
# Aufgabe 2
|
||||
#
|
||||
# Jeder Flankenwechsel muss zur Berechnung der Entfernung gezaehlt werden.
|
||||
# Definieren Sie alle dazu noetigen Methoden.
|
||||
|
||||
def count(channel):
|
||||
global counter
|
||||
counter = counter + 1
|
||||
print "Number of impulses" + str(counter)
|
||||
|
||||
# Aufgabe 2
|
||||
#
|
||||
# Diese Methode soll die gesamte zurueckgelegte Distanz zurueckgeben.
|
||||
def get_travelled_dist(self):
|
||||
return 0
|
||||
global counter
|
||||
global STEP_LENGTH
|
||||
dist = counter * STEP_LENGTH
|
||||
return dist
|
||||
|
||||
class EncoderThread(threading.Thread):
|
||||
''' Thread-class for holding speed and distance data of all encoders'''
|
||||
@@ -225,6 +260,11 @@ class EncoderThread(threading.Thread):
|
||||
#
|
||||
# Hier muss der Thread initialisiert werden.
|
||||
def __init__(self, encoder):
|
||||
try:
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(encoder_pin, GPIO.IN)
|
||||
except:
|
||||
print "ERROR: Encoder GPIO init failed!"
|
||||
return 0
|
||||
|
||||
def run(self):
|
||||
@@ -235,6 +275,7 @@ class EncoderThread(threading.Thread):
|
||||
#
|
||||
# Diese Methode soll die aktuelle Geschwindigkeit sowie die zurueckgelegte Distanz aktuell halten.
|
||||
def get_values(self):
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
@@ -255,14 +296,14 @@ if __name__ == "__main__":
|
||||
# Tragen Sie die i2c Adressen der Sensoren hier ein
|
||||
|
||||
# The i2c addresses of front and rear ultrasound sensors
|
||||
ultrasonic_front_i2c_address = 0x00;
|
||||
ultrasonic_rear_i2c_address = 0x00;
|
||||
ultrasonic_front_i2c_address = 0x70;
|
||||
ultrasonic_rear_i2c_address = 0x71;
|
||||
|
||||
# The i2c address of the compass sensor
|
||||
compass_i2c_address = 0x00
|
||||
compass_i2c_address = 0x60
|
||||
|
||||
# The i2c address of the infrared sensor
|
||||
infrared_i2c_address = 0x00
|
||||
infrared_i2c_address = 0x4f
|
||||
|
||||
# Aufgabe 6
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user