daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Aug 19th, 2015 221 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Messung Entfernung
  2.  
  3. import time
  4. import RPi.GPIO as GPIO
  5. import os
  6. import sys
  7.  
  8.  
  9. # define GPIO pins
  10. GPIOTrigger1 = 18
  11. GPIOEcho1    = 24
  12. GPIOTrigger2 = 25
  13. GPIOEcho2    = 23
  14.  
  15. # function to measure the distance1
  16. def MeasureDistance1():
  17.   # set trigger to high
  18.   GPIO.output(GPIOTrigger1, True)
  19.  
  20.   # set trigger after 10µs to low
  21.   time.sleep(0.00001)
  22.   GPIO.output(GPIOTrigger1, False)
  23.  
  24.   # store initial start time
  25.   StartTime1 = time.time()
  26.  
  27.   # store start time
  28.   while GPIO.input(GPIOEcho1) == 0:
  29.     StartTime1 = time.time()
  30.  
  31.   # store stop time
  32.   while GPIO.input(GPIOEcho1) == 1:
  33.     StopTime1 = time.time()
  34.  
  35.   # calculate distance
  36.   TimeElapsed1 = StopTime1 - StartTime1
  37.   Distance1 = TimeElapsed1 * 331.5 / 2
  38.  
  39.   return Distance1
  40.  
  41.  
  42. # function to measure the distance2
  43. def MeasureDistance2():
  44.   # set trigger to high
  45.   GPIO.output(GPIOTrigger2, True)
  46.  
  47.   # set trigger after 10µs to low
  48.   time.sleep(0.00001)
  49.   GPIO.output(GPIOTrigger2, False)
  50.  
  51.   # store initial start time
  52.   StartTime2 = time.time()
  53.  
  54.   # store start time
  55.   while GPIO.input(GPIOEcho2) == 0:
  56.     StartTime2 = time.time()
  57.  
  58.   # store stop time
  59.   while GPIO.input(GPIOEcho2) == 1:
  60.     StopTime2 = time.time()
  61.  
  62.   # calculate distance
  63.   TimeElapsed2 = StopTime2 - StartTime2
  64.   Distance2 = TimeElapsed2 * 331.5 / 2
  65.  
  66.   return Distance2
  67.  
  68.  
  69. # main function
  70. def main():
  71.  try:
  72.   while True:    
  73.    Start = True
  74.    while os.path.exists("/home/pi/RailRob/Messwerte/Start.txt"):
  75.     Start = False
  76.  
  77.     while os.path.exists("/home/pi/RailRob/Messwerte/MessDaten.qwe"):
  78.       file_out = open("/home/pi/RailRob/Messwerte/MessDaten.qwe","w") # w=ueberschreiben, a=anhaengen der Daten
  79.        Zeitstempel = time.strftime("%Y_%m_%d_%H_%M_%S")
  80.        #print("2")
  81.        Distance1 = MeasureDistance1()
  82.        print("1 = %.4f m" % Distance1)
  83.        file_out.write(Zeitstempel+" Obenrechts %.4f\n" % Distance1)
  84.        #print("3")
  85.        Distance2 = MeasureDistance2()
  86.        print("2 = %.4f m" % Distance2)
  87.        file_out.write(Zeitstempel+" Obenlinks %.4f\n" % Distance2)
  88.        #time.sleep(.1)
  89.        #print("")
  90.        file_out.close()
  91.        os.rename("/home/pi/RailRob/Messwerte/MessDaten.qwe","/home/pi/RailRob/Messwerte/MessDaten.txt")
  92.        time.sleep(.5)
  93.  
  94.     else:
  95.      while Start == False and not os.path.exists("/home/pi/RailRob/Messwerte/Start.txt"):
  96.       GPIO.cleanup()
  97.       print("Messung beendet")
  98.       sys.exit()
  99.       Start=True
  100.        
  101.  except KeyboardInterrupt:
  102.     print("Measurement stopped by user")
  103.     GPIO.cleanup()
  104.  
  105.  
  106.  
  107. if __name__ == '__main__':
  108.  
  109.   # use GPIO pin numbering convention
  110.   GPIO.setmode(GPIO.BCM)
  111.  
  112.   # set up GPIO pins
  113.   GPIO.setup(GPIOTrigger1, GPIO.OUT)
  114.   GPIO.setup(GPIOEcho1, GPIO.IN)
  115.  
  116.   GPIO.setup(GPIOTrigger2, GPIO.OUT)
  117.   GPIO.setup(GPIOEcho2, GPIO.IN)
  118.  
  119.   # set trigger to false
  120.   GPIO.output(GPIOTrigger1, False)
  121.   GPIO.output(GPIOTrigger2, False)
  122.  
  123.   # call main function
  124.   main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top