srfinder

Quick tool for measuring distance over time using a RPi
git clone git://git.noxz.tech/srfinder
Log | Files | Refs | README | LICENSE

srfinder.py (3199B)


      1 #!/usr/bin/python3
      2 
      3 import os
      4 import sys
      5 import signal
      6 import getopt
      7 import time
      8 import RPi.GPIO as GPIO
      9 
     10 TRIG = 16
     11 ECHO = 18
     12 
     13 '''
     14 To setup the hardware use a raspberry pi and either a HY-SRF05 or a HC-SR04.
     15 Connect the SR-module to the raspberry pi in this way:
     16 
     17 -----------------------------------------Edge of pi (furthest from you)--------------------------------------
     18 
     19        VCC       GND                      TRG  ECH
     20         |         |                        |    |
     21    +----v---------v------------------------v----v-----------------------------------------------------------+
     22    |    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    |
     23    |    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    x    |
     24    +--------------------------------------------------------------------------------------------------------+
     25 
     26 -----------------------------------------Front of Pi (closest to you)----------------------------------------
     27 
     28 IMPORTANT: You must also connect resistors between ECH <-> GND [470 ohm] and ECH <-> cable [330 ohm] like so:
     29 
     30    ECH                    GND
     31     |                      |
     32     *--[330]---*---[470]---*
     33                |           |
     34              GPIO24       GND
     35 
     36 '''
     37 
     38 def exit_gracefully(sig, frame):
     39 	GPIO.cleanup()
     40 	sys.exit(0)
     41 
     42 def setup():
     43 	GPIO.setmode(GPIO.BOARD)
     44 	GPIO.setup(TRIG,GPIO.OUT)
     45 	GPIO.output(TRIG,0)
     46 
     47 	GPIO.setup(ECHO,GPIO.IN)
     48 
     49 	time.sleep(0.1)
     50 
     51 def start(freq = 25.0):
     52 	timeout = (1.0 / freq)
     53 	initial = time.time()
     54 
     55 	signal.signal(signal.SIGINT, exit_gracefully)
     56 	while True:
     57 
     58 		GPIO.output(TRIG,1)
     59 		time.sleep(0.0001)
     60 		GPIO.output(TRIG,0)
     61 
     62 		while GPIO.input(ECHO) == 0:
     63 			pass
     64 		start = time.time()
     65 
     66 		while GPIO.input(ECHO) == 1:
     67 			pass
     68 		stop = time.time()
     69 
     70 		print('{:f};{:f}'.format((stop - initial), (stop - start) * (34029 / 2)), file=sys.stdout)
     71 		time.sleep(timeout)
     72 
     73 def usage(code = 0):
     74 	filename = os.path.basename(__file__)
     75 	print(filename + ' -f <frequency>', file=sys.stderr)
     76 	print('\nReads distance at a rate based on (-f) frequency and prints <time>;<distance> to stdout.', file=sys.stderr)
     77 	print('The operation of measurement is ended using SIGINT or Ctrl+C.', file=sys.stderr)
     78 	print('\nThis tool is intended for a raspberry pi using a HY-SRF05 or a HC-SR04.', file=sys.stderr)
     79 	print('Default pins used are GPIO23 (trig) and GPIO24 (echo).', file=sys.stderr)
     80 	print('\nUsage:\n  ' + filename + ' [options...]', file=sys.stderr)
     81 	print('  ' + filename + ' -f 30 > output.csv', file=sys.stderr)
     82 	print('\nOptions:', file=sys.stderr)
     83 	print('  -f, --frequency            determines read frequency. Default is 25hz', file=sys.stderr)
     84 	print('\nReport bugs to <chris@noxz.tech>', file=sys.stderr)
     85 	print('Source code: https://git.noxz.tech/srfinder', file=sys.stderr)
     86 	sys.exit(code)
     87 
     88 def main(argv):
     89 	freq = 25.0
     90 	try:
     91 		opts, args = getopt.getopt(argv,"hf:",["frequency="])
     92 	except getopt.GetoptError:
     93 		usage(2)
     94 	for opt, arg in opts:
     95 		if opt == '-h':
     96 			usage(0)
     97 		elif opt in ("-f", "--frequency"):
     98 			freq = float(arg)
     99 	if (freq > 0.0 and freq < 100.0):
    100 		setup()
    101 		start(freq)
    102 
    103 if __name__ == "__main__":
    104 	main(sys.argv[1:])