-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPARTSearchingAlgorithm.py
More file actions
276 lines (226 loc) · 7.12 KB
/
PARTSearchingAlgorithm.py
File metadata and controls
276 lines (226 loc) · 7.12 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# CREATE A SEARCHING ALGORITHM FOR PLANE
import numpy as np
import math
from matplotlib import pyplot as plt
from matplotlib import animation
# ---------------------------------------------------------
# plot points
# ---------------------------------------------------------
# import points
searchGridPoints = [{
"latitude": 38.1444444444444,
"longitude": -76.4280916666667
},
{
"latitude": 38.1459444444444,
"longitude": -76.4237944444445
},
{
"latitude": 38.1439305555556,
"longitude": -76.4227444444444
},
{
"latitude": 38.1417138888889,
"longitude": -76.4253805555556
},
{
"latitude": 38.1412111111111,
"longitude": -76.4322361111111
},
{
"latitude": 38.1431055555556,
"longitude": -76.4335972222222
},
{
"latitude": 38.1441805555556,
"longitude": -76.4320111111111
},
{
"latitude": 38.1452611111111,
"longitude": -76.4289194444444
},
{
"latitude": 38.1444444444444,
"longitude": -76.4280916666667
}
]
# create figure and axis
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')
# axis boundaries on grid
minX = 90
maxX = -90
minY = 180
maxY = -180
for pt in searchGridPoints :
xvalue = pt["latitude"]
yvalue = pt["longitude"]
xdata.append(xvalue)
ydata.append(yvalue)
# find maximum and minimum values
if (xvalue > maxX):
maxX = xvalue
if (xvalue < minX):
minX = xvalue
if (yvalue > maxY):
maxY = yvalue
if (yvalue < minY):
minY = yvalue
lengthx = maxX - minX
lengthy = maxY - minY
# initialize background frame
def init():
ax.set_xlim(minX - lengthx * 0.2, maxX + lengthx * 0.2)
ax.set_ylim(minY - lengthy * 0.2, maxY + lengthy * 0.2)
ax.plot(xdata, ydata, 'b')
ax.set_aspect('equal')
ax.set_xlabel("latitude")
ax.set_ylabel("longitude")
createPts()
return ln,
# update function
def update(frame):
global prevPlanePts, prevCamPts, prevConnectorPts, planeAngle
planeAngle %= 2 * math.pi
if (planeAngle < 0):
planeAngle += 2 * math.pi
# commands, implement algorithm here
moveToPts(pts)
# remove previous points for visual clarity
if (frame > 0):
prevPlanePts.remove()
prevCamPts.remove()
#prevConnectorPts.remove()
prevPlanePts, = ax.plot(planePosX, planePosY, marker = (3, 0, 30+planeAngle*180/math.pi), markersize = 12, color='green') # plot plane
ax.plot(cameraPosX, cameraPosY, 'yo') # plot searched points
#prevConnectorPts, = ax.plot([planePosX, cameraPosX], [planePosY, cameraPosY], color = 'black', linewidth = 3) # connect plane and camera
prevCamPts, = ax.plot(cameraPosX, cameraPosY, 'mo') # plot camera current position
#ax.plot(minX + frame * lengthx / 10, minY + frame * lengthy / 10, 'go')
ln.set_data(xdata, ydata)
return ln,
# CONSTANTS - ENTER VALUES FOR THESE DEPENDING ON PROPERTIES OF PLANE/CAMERA
planeRadius = 0.002
cameraRadius = 0.002
distToCamera = 0.0008
planeSpeed = 0.0001
# variables for properties of the plane and the camera
planePosX = (maxX + minX) / 2
planePosY = (maxY + minY) / 2
planeAngle = 0
planeAngleSpeed = math.pi / 40
cameraPosX = planePosX + distToCamera * math.cos(planeAngle)
cameraPosY = planePosY + distToCamera * math.sin(planeAngle)
curPt = 0
pts = []
def moveForward():
global planePosX
global planePosY
planePosX += planeSpeed * math.cos(planeAngle)
planePosY += planeSpeed * math.sin(planeAngle)
setCamera()
def turn(turnby):
global planeAngle
planeAngle += turnby
setCamera()
def setCamera():
global cameraPosX
global cameraPosY
cameraPosX = planePosX + distToCamera * math.cos(planeAngle)
cameraPosY = planePosY + distToCamera * math.sin(planeAngle)
def turnTo(x, y):
global planeAngle, planePosX, planePosY
if (math.fabs(x - planePosX) <= 0.01):
if (y - planePosY < 0):
difX = 0.001
else:
difX = -0.001
else:
difX = x - planePosX
if (math.fabs(y - planePosY) <= 0.01):
if (x - planePosX < 0):
difY = 0.001
else:
difY = -0.001
else:
difY = y - planePosY
targetAngle = math.atan((difY) / (difX))
targetAngle += (difX < 0) * math.pi
'''
difX = x - planePosX
difY = y - planePosY
if (math.fabs(difX) <= 0.01):
if (difY > 0):
targetAngle = math.pi / 2
else:
targetAngle = math.pi * 3 / 2
elif (math.fabs(difY) <= 0.01):
if (difX > 0):
targetAngle = 0
else:
targetAngle = math.pi
else:
targetAngle = math.atan((y - planePosY) / (difX))
targetAngle += (difX < 0) * math.pi
'''
if (planeAngle > targetAngle):
turn(-planeAngleSpeed)
elif (planeAngle <= targetAngle):
turn(planeAngleSpeed)
setCamera()
def moveTo(x, y):
if (math.sqrt((planePosX - x) ** 2 + (planePosY - y) ** 2) > planeSpeed * 5):
moveForward()
return True
#=--------------
if (math.fabs(x - planePosX) <= 0.01):
if (y - planePosY < 0):
difX = 0.001
else:
difX = -0.001
else:
difX = x - planePosX
if (math.fabs(y - planePosY) <= 0.01):
if (x - planePosX < 0):
difY = 0.001
else:
difY = -0.001
else:
difY = y - planePosY
targetAngle = math.atan((difY) / (difX))
targetAngle += (difX < 0) * math.pi
if (math.fabs(targetAngle - planeAngle) > planeAngleSpeed * 2 and math.sqrt((planePosX - x) ** 2 + (planePosY - y) ** 2) > planeSpeed * 5):
turnTo(x, y)
setCamera()
return False
if (math.sqrt((planePosX - x) ** 2 + (planePosY - y) ** 2) > planeSpeed * 5):
moveForward()
setCamera()
return False
return True
def moveToPts(pts):
global curPt
if (curPt < len(pts) and moveTo(pts[curPt][0], pts[curPt][1])) :
curPt += 1
setCamera()
def createPts():
global pts
pts = []
for pt in searchGridPoints:
pts.append([pt["latitude"], pt["longitude"]])
#pts = [[minX, minY], [minX, maxY], [maxX, maxY], [maxX, minY]]
return pts
# animate
ani = animation.FuncAnimation(fig, update, frames=np.linspace(0, 4*np.pi, 100), init_func = init, blit=True)
plt.show()
writergif = animation.PillowWriter(fps=30)
ani.save('PARTSearchAlgorithmVersion2.gif',writer=writergif)
# ---------------------------------------------------------
# create animation
# ---------------------------------------------------------
# ---------------------------------------------------------
# draw circle
# ---------------------------------------------------------
# ---------------------------------------------------------
# determine whether points have been searched or not while flying
# ---------------------------------------------------------