srfinder.py
1#!/usr/bin/python3
2
3import os
4import sys
5import signal
6import getopt
7import time
8import RPi.GPIO as GPIO
9
10TRIG = 16
11ECHO = 18
12
13'''
14To setup the hardware use a raspberry pi and either a HY-SRF05 or a HC-SR04.
15Connect 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
28IMPORTANT: 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
38def exit_gracefully(sig, frame):
39 GPIO.cleanup()
40 sys.exit(0)
41
42def 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
51def 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
73def 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://noxz.tech/git/srfinder', file=sys.stderr)
86 sys.exit(code)
87
88def 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
103if __name__ == "__main__":
104 main(sys.argv[1:])