Advertisement
Deddieslab

IR_lightgate2

Sep 13th, 2014
9,877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. from time import sleep
  3. from datetime import datetime
  4. from decimal import *
  5.  
  6. #-----------------------------------------------------------#
  7. #                    General Parameters                     #
  8. #-----------------------------------------------------------#
  9.  
  10. #display no warnings (on command line)
  11. GPIO.setwarnings(False)
  12. GPIO.setmode(GPIO.BOARD)
  13.  
  14. GPIO.setup(13, GPIO.IN)   # GPIO2, BPW85 phototransistor
  15. GPIO.setup(15, GPIO.IN)   # GPIO3, BPW85 phototransistor
  16.  
  17. global lg1, lg2, dt1, dt2, distance
  18. lg1 = 0
  19. lg2 = 0
  20.  
  21. distance = 30000 #um, distance between light gates
  22.  
  23. #-----------------------------------------------------------#
  24. #function LIGHTGATE: function is executed when input pins   #
  25. #                    are edge triggered                     #
  26. #-----------------------------------------------------------#
  27. def lightgate(channel):
  28.  
  29.   #Get current date/time stamp
  30.   dt = datetime.now()
  31.   print "Edge triggered on channel ", channel, " at ", dt
  32.   print " "
  33.  
  34.   global lg1, lg2, dt1, dt2
  35.  
  36.   #Lightgate 1
  37.   if channel==13:               #detect if lightgate 1 is triggered
  38.  
  39.     lg1 = 1
  40.     dt1 = dt                    #remember date/time as dt1
  41.  
  42.     if lg2==1:                  #check if lightgate 2 was triggered already
  43.      
  44.       ddt = dt1-dt2             #calculate time difference between lightgate 1 and 2
  45.       s = "Goodbye birdey!"     #string mentions that the bird just left the birdhouse    
  46.  
  47.   #Lightgate 2
  48.   if channel==15: #detect if lightgate 2 is triggered
  49.    
  50.     lg2 = 1
  51.     dt2 = dt            #remember date/time as dt2
  52.  
  53.     if lg1==1:          #check if lightgate 1 was triggered already
  54.  
  55.       ddt = dt2 - dt1       #calculate time difference between lightgate 2 and 1
  56.       s =  "Hello birdey!"  #string mentions that the bird just entered the birdhouse
  57.  
  58.   if lg1*lg2 == 1:      #if both lightgates were triggered, reset the status
  59.  
  60.     lg1 = 0
  61.     lg2 = 0
  62.  
  63.     print s
  64.     print " "
  65.     print "dt2      : ", dt2
  66.     print "dt1      : ", dt1
  67.     print "time diff: ", ddt
  68.     print " "    
  69.  
  70.     #calculate time difference between lightgates in microseconds (us)
  71.     deltat = 1000000*ddt.seconds+ddt.microseconds #us
  72.    
  73.     #determine decimal precision
  74.     getcontext().prec = 2  
  75.  
  76.     print "distance : ", Decimal(distance), "um"
  77.     print "deltaT   : ", Decimal(deltat), "us"
  78.     print " "
  79.  
  80.     #calculate speed of bird leaving/entering the birdhouse
  81.     print "speed    : ", Decimal(distance) / Decimal(deltat), "m/s"
  82.     print " "
  83.  
  84. #-----------------------------------------------------------#
  85. #                     GPIO edge trigger                     #
  86. #-----------------------------------------------------------#
  87.    
  88. #Check for falling edge of input pins. Call function LIGHTGATE if triggered  
  89. GPIO.add_event_detect(15,GPIO.FALLING,callback=lightgate,bouncetime=200)
  90. GPIO.add_event_detect(13,GPIO.FALLING,callback=lightgate,bouncetime=200)
  91.  
  92. #-----------------------------------------------------------#
  93. #                 main loop... sleeping...                  #
  94. #-----------------------------------------------------------#
  95. try:
  96.  
  97.   while True:
  98.     sleep(1)    #Sleep as long as no edge is triggered
  99.  
  100. #-----------------------------------------------------------#
  101. #               Watch for keyboard interupt                 #
  102. #-----------------------------------------------------------#
  103.    
  104. except KeyboardInterrupt:  #CTRL+C
  105.   print "KEYBOARD INTERRUPT (CTRL+C)!"
  106.  
  107. finally:
  108.   print "Cleaning up..."
  109.   GPIO.cleanup()
  110.   print "Done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement