-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathultrasonicRanger.py
154 lines (117 loc) · 4.09 KB
/
ultrasonicRanger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
# Modified by SwtichDoc Labs July 2017
"""
* ultrasonic.py
* A library for ultrasonic sensor at RP
*
* Copyright (c) 2012 seeed technology inc.
* Website : www.seeed.cc
* Author : seeed fellow
* Create Time:
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
"""
import RPi.GPIO as GPIO
import time
import config
import state
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
def getAndPrint():
print "Grove Ultrasonic get level and print"
# test 10 times
for i in range(10):
print "%5.3fcm" % measurementInCM()
# Reset GPIO settings
#GPIO.cleanup()
def measurementInCM():
# setup the GPIO_SIG as output
GPIO.setup(config.UltrasonicLevel, GPIO.OUT)
GPIO.output(config.UltrasonicLevel, GPIO.LOW)
time.sleep(0.2)
GPIO.output(config.UltrasonicLevel, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(config.UltrasonicLevel, GPIO.LOW)
# initialize times for abort check
start = -1
stop = -1
# setup GPIO_SIG as input
GPIO.setup(config.UltrasonicLevel, GPIO.IN)
measurementStartTime = time.time()
# get duration from Ultrasonic SIG pin
while GPIO.input(config.UltrasonicLevel) == 0:
start = time.time()
if (measurementStartTime+2<start):
# abort
return -1
if (start == -1):
# abort
return -1
measurementStartTime = time.time()
while GPIO.input(config.UltrasonicLevel) == 1:
stop = time.time()
if (measurementStartTime+2<stop):
# abort
return -1
if (stop == -1):
# abort
return -1
pulseLength = measurementPulse(start, stop)
return pulseLength
def measurementPulse(start, stop):
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300
# That was the distance there and back so halve the value
distance = distance / 2
return distance
def readCalibrationNumbers():
f = open("TankCalibration","r")
line = f.read()
line = line.strip()
empty, full = line.split(",")
state.Tank_Empty_Level = float(empty)
state.Tank_Full_Level = float(full)
f.close()
def returnPercentFull():
measurement = measurementInCM()
#check for abort
if (measurement < 0.0):
return -1.00
state.Tank_Level = measurement
full = ((state.Tank_Empty_Level - state.Tank_Level)/(state.Tank_Empty_Level - state.Tank_Full_Level))*100.0
if (config.DEBUG):
print "returnPercentFull"
print "state.Tank_Level:", state.Tank_Level
print "state.Tank_Empty_Level:", state.Tank_Empty_Level
print "state.Tank_Full_Level:", state.Tank_Full_Level
print "Old Tank_Percentage_Full= %f" % state.Tank_Percentage_Full
print "New Tank_Percentage_Full= %f" % full
if (full < 0.0):
full = 0.0
if (full > 100.0):
full = 100.0
if (config.DEBUG):
print "Adjusted Tank_Percentage_Full= %f" % full
return full