The Ultraschallsensor is finished and all addresses are added.
This commit is contained in:
@@ -19,227 +19,268 @@ bus = smbus.SMBus(1)
|
|||||||
#################################################################################
|
#################################################################################
|
||||||
|
|
||||||
class Ultrasonic():
|
class Ultrasonic():
|
||||||
'''This class is responsible for handling i2c requests to an ultrasonic sensor'''
|
'''This class is responsible for handling i2c requests to an ultrasonic sensor'''
|
||||||
|
|
||||||
def __init__(self,address):
|
def __init__(self,address):
|
||||||
self.address = address
|
self.address = address
|
||||||
|
|
||||||
# Aufgabe 2
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# Diese Methode soll ein Datenbyte an den Ultraschallsensor senden um eine Messung zu starten
|
# Diese Methode soll ein Datenbyte an den Ultraschallsensor senden um eine Messung zu starten
|
||||||
def write(self,value):
|
def write(self,value):
|
||||||
return 0
|
try:
|
||||||
|
bus.write_byte_data(self.address, 0x00, 0x51)
|
||||||
|
except:
|
||||||
|
print "ERROR: Ultrasonic sensor start failed!"
|
||||||
|
return 0
|
||||||
|
|
||||||
# Aufgabe 2
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# Diese Methode soll den Lichtwert auslesen und zurueckgeben.
|
# Diese Methode soll den Lichtwert auslesen und zurueckgeben.
|
||||||
def get_brightness(self):
|
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
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# Diese Methode soll die Entfernung auslesen und zurueckgeben.
|
# Diese Methode soll die Entfernung auslesen und zurueckgeben.
|
||||||
def get_distance(self):
|
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):
|
def get_address(self):
|
||||||
return self.address
|
return self.address
|
||||||
|
|
||||||
class UltrasonicThread(threading.Thread):
|
class UltrasonicThread(threading.Thread):
|
||||||
''' Thread-class for holding ultrasonic data '''
|
''' Thread-class for holding ultrasonic data '''
|
||||||
|
|
||||||
# distance to obstacle in cm
|
stop = False
|
||||||
distance = 0
|
|
||||||
|
|
||||||
# brightness value
|
# distance to obstacle in cm
|
||||||
brightness = 0
|
distance = 0
|
||||||
|
|
||||||
# Aufgabe 4
|
# brightness value
|
||||||
#
|
brightness = 0
|
||||||
# Hier muss der Thread initialisiert werden.
|
|
||||||
def __init__(self, address):
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Schreiben Sie die Messwerte in die lokalen Variablen
|
# Hier muss der Thread initialisiert werden.
|
||||||
def run(self):
|
def __init__(self, address):
|
||||||
while not self.stopped:
|
try:
|
||||||
continue
|
threading.Thread.__init__(self)
|
||||||
|
time.sleep(0.1)
|
||||||
def stop(self):
|
self.ultrasonic = Ultrasonic(address)
|
||||||
self.stopped = True
|
self.setDaemon(True)
|
||||||
|
self.start()
|
||||||
|
except:
|
||||||
|
print "ERROR: Ultrasonic sensor thread init failed!"
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# Aufgabe 4
|
||||||
|
#
|
||||||
|
# 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):
|
||||||
|
self.stopped = True
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
# Compass
|
# Compass
|
||||||
#################################################################################
|
#################################################################################
|
||||||
|
|
||||||
class Compass(object):
|
class Compass(object):
|
||||||
'''This class is responsible for handling i2c requests to a compass sensor'''
|
'''This class is responsible for handling i2c requests to a compass sensor'''
|
||||||
|
|
||||||
def __init__(self,address):
|
def __init__(self,address):
|
||||||
self.address = address
|
self.address = address
|
||||||
|
|
||||||
# Aufgabe 2
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# Diese Methode soll den Kompasswert auslesen und zurueckgeben.
|
# Diese Methode soll den Kompasswert auslesen und zurueckgeben.
|
||||||
def get_bearing(self):
|
def get_bearing(self):
|
||||||
b = 0
|
b = 0
|
||||||
try:
|
try:
|
||||||
bear1 = bus.read_byte_data(address, 2)
|
bear1 = bus.read_byte_data(address, 2)
|
||||||
bear2 = bus.read_byte_data(address, 3)
|
bear2 = bus.read_byte_data(address, 3)
|
||||||
bear = (bear1 << 8) + bear2
|
bear = (bear1 << 8) + bear2
|
||||||
b = bear/10.0
|
b = bear/10.0
|
||||||
except:
|
except:
|
||||||
print "Bearing konnte nicht ausgelesen werden!"
|
print "Bearing konnte nicht ausgelesen werden!"
|
||||||
return b
|
return b
|
||||||
|
|
||||||
class CompassThread(threading.Thread):
|
class CompassThread(threading.Thread):
|
||||||
''' Thread-class for holding compass data '''
|
''' Thread-class for holding compass data '''
|
||||||
|
|
||||||
# Compass bearing value
|
# Compass bearing value
|
||||||
bearing = 0
|
bearing = 0
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Hier muss der Thread initialisiert werden.
|
# Hier muss der Thread initialisiert werden.
|
||||||
def __init__(self, address):
|
def __init__(self, address):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Diese Methode soll den Kompasswert aktuell halten.
|
# Diese Methode soll den Kompasswert aktuell halten.
|
||||||
def run(self):
|
def run(self):
|
||||||
while not self.stopped:
|
while not self.stopped:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.stopped = True
|
self.stopped = True
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
# Infrared
|
# Infrared
|
||||||
#################################################################################
|
#################################################################################
|
||||||
|
|
||||||
class Infrared(object):
|
class Infrared(object):
|
||||||
'''This class is responsible for handling i2c requests to an infrared sensor'''
|
'''This class is responsible for handling i2c requests to an infrared sensor'''
|
||||||
|
|
||||||
def __init__(self,address):
|
def __init__(self,address):
|
||||||
self.address = address
|
self.address = address
|
||||||
|
|
||||||
# Aufgabe 2
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# In dieser Methode soll der gemessene Spannungswert des Infrarotsensors ausgelesen werden.
|
# In dieser Methode soll der gemessene Spannungswert des Infrarotsensors ausgelesen werden.
|
||||||
def get_voltage(self):
|
def get_voltage(self):
|
||||||
voltage = 0
|
voltage = 0
|
||||||
try:
|
try:
|
||||||
voltage = bus.read_byte(self.address)
|
voltage = bus.read_byte(self.address)
|
||||||
except:
|
except:
|
||||||
print "Spannung konnte nicht ausgelesen werden!"
|
print "Spannung konnte nicht ausgelesen werden!"
|
||||||
return voltage
|
return voltage
|
||||||
|
|
||||||
# Aufgabe 3
|
# Aufgabe 3
|
||||||
#
|
#
|
||||||
# Der Spannungswert soll in einen Distanzwert umgerechnet werden.
|
# Der Spannungswert soll in einen Distanzwert umgerechnet werden.
|
||||||
def get_distance(self):
|
def get_distance(self):
|
||||||
# v=(readChannel(0)/1023.0)*3.3
|
# v=(readChannel(0)/1023.0)*3.3
|
||||||
v = self.get_voltage()
|
v = self.get_voltage()
|
||||||
# interpolation von https://tutorials-raspberrypi.de/wp-content/uploads/gp2y0a02yk-600x455.png
|
# interpolation von https://tutorials-raspberrypi.de/wp-content/uploads/gp2y0a02yk-600x455.png
|
||||||
dist = 16.2537 * v**4 - 129.893 * v**3 + 382.268 * v**2 - 512.611 * v + 301.439
|
dist = 16.2537 * v**4 - 129.893 * v**3 + 382.268 * v**2 - 512.611 * v + 301.439
|
||||||
return dist
|
return dist
|
||||||
|
|
||||||
|
|
||||||
class InfraredThread(threading.Thread):
|
class InfraredThread(threading.Thread):
|
||||||
''' Thread-class for holding Infrared data '''
|
''' Thread-class for holding Infrared data '''
|
||||||
|
|
||||||
# distance to an obstacle in cm
|
# distance to an obstacle in cm
|
||||||
distance = 0
|
distance = 0
|
||||||
|
|
||||||
# length of parking space in cm
|
# length of parking space in cm
|
||||||
parking_space_length = 0
|
parking_space_length = 0
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Hier muss der Thread initialisiert werden.
|
# Hier muss der Thread initialisiert werden.
|
||||||
def __init__(self, address, encoder=None):
|
def __init__(self, address, encoder=None):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while not self.stopped:
|
while not self.stopped:
|
||||||
read_infrared_value()
|
read_infrared_value()
|
||||||
calculate_parking_space_length()
|
calculate_parking_space_length()
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Diese Methode soll den Infrarotwert aktuell halten
|
# Diese Methode soll den Infrarotwert aktuell halten
|
||||||
def read_infrared_value(self):
|
def read_infrared_value(self):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# Aufgabe 5
|
# Aufgabe 5
|
||||||
#
|
#
|
||||||
# Hier soll die Berechnung der Laenge der Parkluecke definiert werden
|
# Hier soll die Berechnung der Laenge der Parkluecke definiert werden
|
||||||
def calculate_parking_space_length(self):
|
def calculate_parking_space_length(self):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.stopped = True
|
self.stopped = True
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
# Encoder
|
# Encoder
|
||||||
#################################################################################
|
#################################################################################
|
||||||
|
|
||||||
class Encoder(object):
|
class Encoder(object):
|
||||||
''' This class is responsible for handling encoder data '''
|
''' This class is responsible for handling encoder data '''
|
||||||
|
|
||||||
# Aufgabe 2
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# Wieviel cm betraegt ein einzelner Encoder-Schritt?
|
# 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
|
# number of encoder steps
|
||||||
count = 0
|
counter = 0
|
||||||
|
|
||||||
def __init__(self, pin):
|
def __init__(self, pin):
|
||||||
self.pin = pin
|
self.pin = pin
|
||||||
|
GPIO.add_event_detect(encoder_pin, GPIO.BOTH, callback=count, bouncetime=1)
|
||||||
|
|
||||||
# Aufgabe 2
|
# Aufgabe 2
|
||||||
#
|
#
|
||||||
# Jeder Flankenwechsel muss zur Berechnung der Entfernung gezaehlt werden.
|
# Jeder Flankenwechsel muss zur Berechnung der Entfernung gezaehlt werden.
|
||||||
# Definieren Sie alle dazu noetigen Methoden.
|
# Definieren Sie alle dazu noetigen Methoden.
|
||||||
|
|
||||||
# Aufgabe 2
|
def count(channel):
|
||||||
#
|
global counter
|
||||||
# Diese Methode soll die gesamte zurueckgelegte Distanz zurueckgeben.
|
counter = counter + 1
|
||||||
def get_travelled_dist(self):
|
print "Number of impulses" + str(counter)
|
||||||
return 0
|
|
||||||
|
# Aufgabe 2
|
||||||
|
#
|
||||||
|
# Diese Methode soll die gesamte zurueckgelegte Distanz zurueckgeben.
|
||||||
|
def get_travelled_dist(self):
|
||||||
|
global counter
|
||||||
|
global STEP_LENGTH
|
||||||
|
dist = counter * STEP_LENGTH
|
||||||
|
return dist
|
||||||
|
|
||||||
class EncoderThread(threading.Thread):
|
class EncoderThread(threading.Thread):
|
||||||
''' Thread-class for holding speed and distance data of all encoders'''
|
''' Thread-class for holding speed and distance data of all encoders'''
|
||||||
|
|
||||||
# current speed.
|
# current speed.
|
||||||
speed = 0
|
speed = 0
|
||||||
|
|
||||||
# currently traversed distance.
|
# currently traversed distance.
|
||||||
distance = 0
|
distance = 0
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Hier muss der Thread initialisiert werden.
|
# Hier muss der Thread initialisiert werden.
|
||||||
def __init__(self, encoder):
|
def __init__(self, encoder):
|
||||||
return 0
|
try:
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
GPIO.setup(encoder_pin, GPIO.IN)
|
||||||
|
except:
|
||||||
|
print "ERROR: Encoder GPIO init failed!"
|
||||||
|
return 0
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while not self.stopped:
|
while not self.stopped:
|
||||||
get_values()
|
get_values()
|
||||||
|
|
||||||
# Aufgabe 4
|
# Aufgabe 4
|
||||||
#
|
#
|
||||||
# Diese Methode soll die aktuelle Geschwindigkeit sowie die zurueckgelegte Distanz aktuell halten.
|
# Diese Methode soll die aktuelle Geschwindigkeit sowie die zurueckgelegte Distanz aktuell halten.
|
||||||
def get_values(self):
|
def get_values(self):
|
||||||
return 0
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.stopped = True
|
self.stopped = True
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
# Main Thread
|
# Main Thread
|
||||||
@@ -247,23 +288,23 @@ class EncoderThread(threading.Thread):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
# The GPIO pin to which the encoder is connected
|
# The GPIO pin to which the encoder is connected
|
||||||
encoder_pin = 23
|
encoder_pin = 23
|
||||||
|
|
||||||
# Aufgabe 1
|
# Aufgabe 1
|
||||||
#
|
#
|
||||||
# Tragen Sie die i2c Adressen der Sensoren hier ein
|
# Tragen Sie die i2c Adressen der Sensoren hier ein
|
||||||
|
|
||||||
# The i2c addresses of front and rear ultrasound sensors
|
# The i2c addresses of front and rear ultrasound sensors
|
||||||
ultrasonic_front_i2c_address = 0x00;
|
ultrasonic_front_i2c_address = 0x70;
|
||||||
ultrasonic_rear_i2c_address = 0x00;
|
ultrasonic_rear_i2c_address = 0x71;
|
||||||
|
|
||||||
# The i2c address of the compass sensor
|
# The i2c address of the compass sensor
|
||||||
compass_i2c_address = 0x00
|
compass_i2c_address = 0x60
|
||||||
|
|
||||||
# The i2c address of the infrared sensor
|
# The i2c address of the infrared sensor
|
||||||
infrared_i2c_address = 0x00
|
infrared_i2c_address = 0x4f
|
||||||
|
|
||||||
# Aufgabe 6
|
# Aufgabe 6
|
||||||
#
|
#
|
||||||
# Hier sollen saemtlichen Messwerte periodisch auf der Konsole ausgegeben werden.
|
# Hier sollen saemtlichen Messwerte periodisch auf der Konsole ausgegeben werden.
|
||||||
|
|||||||
Reference in New Issue
Block a user