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