-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibrate_sonar.py
More file actions
173 lines (146 loc) · 6.04 KB
/
calibrate_sonar.py
File metadata and controls
173 lines (146 loc) · 6.04 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
''' Calibration and support functions to interact with via python
Assumes arduino is coded to constantly spit out sonar readings'''
import serial
import pdb
# ------------------------------------------------------------------------------
# Main Class
# ------------------------------------------------------------------------------
class SonarController():
'''Controller class for an Arduino supporting a Sonar sensor.
Attributes: port, high, low, max
Methods: data, calibrate, reCal
'''
def __init__(self, minPower = 1, maxPower = 4):
self.low = 200
self.high = 1500
self.noHand = 2400
self.calThresh = 10
self.prevData = 1.2
self.minPower = minPower
self.maxPower = maxPower
self.rangeRatio = (maxPower-minPower)/(self.high-self.low)
self.port = "com22"
self.arduinoConnect()
def data(self):
try:
#print('A', end="\n")
raw = self.arduinoSerialData.readline()
#print(raw)
# raw = self.arduinoSerialData.readline()
#print('B', end="\n")
data = raw.decode().strip('\r\n')
#print('C', end="\n")
if '-' in data or data is None:
# print('None', end="\n")
return None
data = int(data.split('.')[0])
#print('D', end="\n")
if data > (self.high + self.calThresh) or data < (self.low - self.calThresh):
# print('NoneTwo', end="\n")
return None
scaleData = (data-self.low)*self.rangeRatio + self.minPower
#print('E', end="\n")
#print('Data', scaleData, end="\n")
self.prevData = scaleData
return scaleData
except:
pass
def rawData(self):
if (self.arduinoSerialData.inWaiting()>0):
raw = self.arduinoSerialData.readline()
else:
print('No Reading Flag---')
return self.prevData
# raw = self.arduinoSerialData.readline()
data = raw.decode().strip('\r\n')
if data is None:
return None
data = int(data.split('.')[0])
self.prevData = data
return data
def arduinoConnect(self):
try:
self.arduinoSerialData = serial.Serial(self.port, 9600, timeout=.05) # Initialize arduino for sonar
except:
print("Arduino not found. Reset commencing.")
self.reset()
return
def reset(self):
if hasattr(self, 'arduinoSerialData'):
print('yep')
self.arduinoSerialData.close()
self.port = str(input("Arduino Connection Port (i.e. com22 or /dev/ttyACM0):"))
self.arduinoConnect()
print("\nBegin calibration...")
self.calibrate()
def reCal(self, calCount):
calCount += 1
#Variable instructions
if calCount <= 2:
print("Please ensure your hand is directly over the sensor so it still sees you.")
if calCount >= 1 and calCount <= 4:
print("Consider keeping your range smaller and lowering your upper limit.")
if calCount >= 3:
print("Calibration is struggling. Consider repositioning the sensors.")
print(" Try to ensure straight lines of hand movement, and reduce\
possible interferences within a wide code around the sensor.")
print("Please retry calibration.\n")
return calCount
#--------------------------- Calibration Function -----------------------------
def calibrate(self, calCount = 0):
#resets low, high, noHand, rangeRatio
#----------Min----------
print("Please hold your hand at your comfortable lower limit")
input(" ** Press Enter to take the reading.")
self.low = self.rawData()
print("Reading taken")
#----------Max----------
print("Please hold your hand at your comfortable upper limit")
input(" ** Press Enter to take the reading.")
self.high = self.rawData()
print("Reading taken")
#----------None----------
print("Please remove both of your hands from near the sensor")
input(" ** Press Enter to take the reading.")
self.noHand = self.rawData()
print("Reading taken")
#----------Check and Re-Cal----------
if self.low is None or self.high is None or self.noHand is None:
print("\nSomething didn't read right. Please try again.")
calCount = self.reCal(calCount)
self.calibrate(calCount)
return
if (abs(self.noHand - self.high) < self.calThresh):
print("\nWe couldn't tell the difference between no hand and your upper height.")
calCount = self.reCal(calCount)
self.calibrate(calCount)
return
if (abs(self.low - self.high) == 0):
print("\nWe couldn't tell the difference between your lower and your upper height.")
calCount = self.reCal(calCount)
self.calibrate(calCount)
return
#----------Print----------
print("Lower, Upper, No Hand")
print(self.low, self.high, self.noHand)
self.rangeRatio = ((self.maxPower-self.minPower)/(self.high-self.low))
print("Calibration complete.")
return
# ------------------------------------------------------------------------------
# Testing: Random Output
# ------------------------------------------------------------------------------
import random
class randomController(SonarController):
def __init__(self, minPower = 1, maxPower = 4):
self.low = 200
self.high = 300
self.noHand = 1000
self.minPower = minPower
self.maxPower = maxPower
self.rangeRatio = ((self.maxPower-self.minPower)/(self.high-self.low))
def data(self):
raw = random.randint(300,700)
if raw > self.high:
return None
scaleData = (raw-self.low)*self.rangeRatio + self.minPower
return scaleData