Schiefe Ebene mit Laserlichtschranken
Verfasst: Donnerstag 7. Dezember 2017, 09:22
Hallo,
ich bin neu hier und ein Anfänger in Python. In meiner Projektarbeit muss ich eine schiefe Ebene bauen, die mit Laserlichtschranken versehen ist. Die Ebene ist bereits gebaut. Wenn die Kugel/Quader nach unter rollt möchte ich bei jeder Lichtschranke die Zeit ermitteln und auf einem Display ausgeben. Das Display funktioniert bereits über den I2C-Bus. Das ganze wird über den Raspberry-Pi gesteuert und in Python programmiert. Jedoch hackt es noch ein wenig mit der Programmierung. Ich lade das Programm einfach mal mit hoch.
Mein Problem ist: Wenn ich das Programm starte, wird mir die Zeit 1 ausgegeben. Jedoch wird mir nicht immer die Zeit 2/3 ausgegeben.
Mfg Simon
#Projektarbeit_Schiefe_Ebene_2017
#!/usr/bin/python
import time
import RPi.GPIO as GPIO
import lcddriver
lcd = lcddriver.lcd()
lcd.lcd_clear()
GPIO_SENSOR1_PIN = 22
GPIO_SENSOR2_PIN = 23
GPIO_SENSOR3_PIN = 24
GPIO_SENSOR4_PIN = 25
DISTANCE_1 = 10.0 # (in cm) Anpassen, falls notwendig
DISTANCE_2 = 40.0 # (in cm) Anpassen, falls notwendig
DISTANCE_3 = 90.0 # (in cm) Anpassen, falls notwendig
TIMEOUT_1 = 5 # sek.
TIMEOUT_2 = 10 # sek.
TIMEOUT_3 = 15 # sek.
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Programm starten und Sensoren initialisieren:
if __name__ == '__main__':
GPIO.setup(GPIO_SENSOR1_PIN, GPIO.IN)
GPIO.setup(GPIO_SENSOR2_PIN, GPIO.IN)
GPIO.setup(GPIO_SENSOR3_PIN, GPIO.IN)
GPIO.setup(GPIO_SENSOR4_PIN, GPIO.IN)
start_time1, end_time1 = 0, 0
end_time2 = 0, 0
# Sensor 1 und 2:
while GPIO.input(GPIO_SENSOR1_PIN) == GPIO.LOW:
time.sleep(0.1)
start_time1 = time.time()
end_time1 = 0
while GPIO.input(GPIO_SENSOR2_PIN) == GPIO.LOW and time.time()-start_time1 < TIMEOUT_1:
time.sleep(0.1)
else:
if time.time()-start_time1 >= TIMEOUT_1:
print "Timeout_1"
lcd.lcd_display_string("Timeout_1", 1)
exit()
else:
end_time1 = time.time()
speed_1 = DISTANCE_1 / (end_time1 - start_time1)
print "1. Geschwindigkeit: %.2f cm/s" % speed_1
print "1. Zeit: %.2f s" % (end_time1 - start_time1)
lcd.lcd_display_string("v: %.2f cm/s" % speed_1, 1)
lcd.lcd_display_string("t: %.2f s" % (end_time1 - start_time1), 2)
# Sensor 2 und 3:
while GPIO.input(GPIO_SENSOR2_PIN) == GPIO.LOW:
time.sleep(0.1)
start_time1 = time.time()
end_time2 = 0
while GPIO.input(GPIO_SENSOR3_PIN) == GPIO.LOW and time.time()-start_time1 < TIMEOUT_2:
time.sleep(0.1)
else:
if time.time()-start_time1 >= TIMEOUT_2:
print "Timeout_2"
lcd.lcd_clear()
lcd.lcd_display_string("Timeout_2", 1)
exit()
else:
end_time2 = time.time()
speed_2 = DISTANCE_2 / (end_time2 - start_time1)
print "2. Geschwindigkeit: %.2f cm/s" % speed_2
print "2. Zeit: %.2f s" % (end_time2 - start_time1)
lcd.lcd_clear()
lcd.lcd_display_string("v: %.2f cm/s" % speed_2, 1)
lcd.lcd_display_string("t: %.2f s" % (end_time2 - start_time1), 2)
# Sensor 3 und 4:
while GPIO.input(GPIO_SENSOR3_PIN) == GPIO.LOW:
time.sleep(0.1)
start_time1 = time.time()
end_time3 = 0
while GPIO.input(GPIO_SENSOR4_PIN) == GPIO.LOW and time.time()-start_time1 < TIMEOUT_3:
time.sleep(0.1)
else:
if time.time()-start_time1 >= TIMEOUT_3:
print "Timeout_3"
lcd.lcd_clear()
lcd.lcd_display_string("Timeout_3", 1)
exit()
else:
end_time3 = time.time()
speed_3 = DISTANCE_3 / (end_time3 - start_time1)
print "3. Geschwindigkeit: %.2f cm/s" % speed_3
print "3. Zeit: %.2f s" % (end_time3 - start_time1)
lcd.lcd_clear()
lcd.lcd_display_string("v: %.2f cm/s" % speed_3, 1)
lcd.lcd_display_string("t: %.2f s" % (end_time3 - start_time1), 2)
ich bin neu hier und ein Anfänger in Python. In meiner Projektarbeit muss ich eine schiefe Ebene bauen, die mit Laserlichtschranken versehen ist. Die Ebene ist bereits gebaut. Wenn die Kugel/Quader nach unter rollt möchte ich bei jeder Lichtschranke die Zeit ermitteln und auf einem Display ausgeben. Das Display funktioniert bereits über den I2C-Bus. Das ganze wird über den Raspberry-Pi gesteuert und in Python programmiert. Jedoch hackt es noch ein wenig mit der Programmierung. Ich lade das Programm einfach mal mit hoch.
Mein Problem ist: Wenn ich das Programm starte, wird mir die Zeit 1 ausgegeben. Jedoch wird mir nicht immer die Zeit 2/3 ausgegeben.
Mfg Simon
#Projektarbeit_Schiefe_Ebene_2017
#!/usr/bin/python
import time
import RPi.GPIO as GPIO
import lcddriver
lcd = lcddriver.lcd()
lcd.lcd_clear()
GPIO_SENSOR1_PIN = 22
GPIO_SENSOR2_PIN = 23
GPIO_SENSOR3_PIN = 24
GPIO_SENSOR4_PIN = 25
DISTANCE_1 = 10.0 # (in cm) Anpassen, falls notwendig
DISTANCE_2 = 40.0 # (in cm) Anpassen, falls notwendig
DISTANCE_3 = 90.0 # (in cm) Anpassen, falls notwendig
TIMEOUT_1 = 5 # sek.
TIMEOUT_2 = 10 # sek.
TIMEOUT_3 = 15 # sek.
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Programm starten und Sensoren initialisieren:
if __name__ == '__main__':
GPIO.setup(GPIO_SENSOR1_PIN, GPIO.IN)
GPIO.setup(GPIO_SENSOR2_PIN, GPIO.IN)
GPIO.setup(GPIO_SENSOR3_PIN, GPIO.IN)
GPIO.setup(GPIO_SENSOR4_PIN, GPIO.IN)
start_time1, end_time1 = 0, 0
end_time2 = 0, 0
# Sensor 1 und 2:
while GPIO.input(GPIO_SENSOR1_PIN) == GPIO.LOW:
time.sleep(0.1)
start_time1 = time.time()
end_time1 = 0
while GPIO.input(GPIO_SENSOR2_PIN) == GPIO.LOW and time.time()-start_time1 < TIMEOUT_1:
time.sleep(0.1)
else:
if time.time()-start_time1 >= TIMEOUT_1:
print "Timeout_1"
lcd.lcd_display_string("Timeout_1", 1)
exit()
else:
end_time1 = time.time()
speed_1 = DISTANCE_1 / (end_time1 - start_time1)
print "1. Geschwindigkeit: %.2f cm/s" % speed_1
print "1. Zeit: %.2f s" % (end_time1 - start_time1)
lcd.lcd_display_string("v: %.2f cm/s" % speed_1, 1)
lcd.lcd_display_string("t: %.2f s" % (end_time1 - start_time1), 2)
# Sensor 2 und 3:
while GPIO.input(GPIO_SENSOR2_PIN) == GPIO.LOW:
time.sleep(0.1)
start_time1 = time.time()
end_time2 = 0
while GPIO.input(GPIO_SENSOR3_PIN) == GPIO.LOW and time.time()-start_time1 < TIMEOUT_2:
time.sleep(0.1)
else:
if time.time()-start_time1 >= TIMEOUT_2:
print "Timeout_2"
lcd.lcd_clear()
lcd.lcd_display_string("Timeout_2", 1)
exit()
else:
end_time2 = time.time()
speed_2 = DISTANCE_2 / (end_time2 - start_time1)
print "2. Geschwindigkeit: %.2f cm/s" % speed_2
print "2. Zeit: %.2f s" % (end_time2 - start_time1)
lcd.lcd_clear()
lcd.lcd_display_string("v: %.2f cm/s" % speed_2, 1)
lcd.lcd_display_string("t: %.2f s" % (end_time2 - start_time1), 2)
# Sensor 3 und 4:
while GPIO.input(GPIO_SENSOR3_PIN) == GPIO.LOW:
time.sleep(0.1)
start_time1 = time.time()
end_time3 = 0
while GPIO.input(GPIO_SENSOR4_PIN) == GPIO.LOW and time.time()-start_time1 < TIMEOUT_3:
time.sleep(0.1)
else:
if time.time()-start_time1 >= TIMEOUT_3:
print "Timeout_3"
lcd.lcd_clear()
lcd.lcd_display_string("Timeout_3", 1)
exit()
else:
end_time3 = time.time()
speed_3 = DISTANCE_3 / (end_time3 - start_time1)
print "3. Geschwindigkeit: %.2f cm/s" % speed_3
print "3. Zeit: %.2f s" % (end_time3 - start_time1)
lcd.lcd_clear()
lcd.lcd_display_string("v: %.2f cm/s" % speed_3, 1)
lcd.lcd_display_string("t: %.2f s" % (end_time3 - start_time1), 2)