-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
168 lines (134 loc) · 4.06 KB
/
main.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
import math
from tangent import *
from geotransform import *
safety_distance = 10
waypoint_file = ("./mission.txt")
obstacle_file = ("./obstacle")
waypoint = []
obstacle = []
waypoint_final = []
waypoint_final_ = []
def toM(feet):
return (0.3048*feet)
def read_waypoint(waypoint_file):
waypoint_ = []
wp = []
with open(waypoint_file) as wf:
line = wf.readline()
waypoint_.append(line.split(" "))
while line:
line = wf.readline()
waypoint_.append(line.split(" "))
wp = waypoint_[3:]
for hi in range(len(wp)):
wp[hi][0] = wp[hi][0].split('\t')
wp = wp[:-1]
for hello in range(len(wp)):
longi = wp[hello][0][8]
lati = wp[hello][0][9]
waypoint.append([longi,lati])
def read_obstacle(obstacle_file):
obstacle_ = []
ob = []
with open(obstacle_file) as of:
line = of.readline()
obstacle_.append(line.split(" "))
while line:
line = of.readline()
obstacle_.append(line.split(" "))
ob = obstacle_[2:]
for hi in range(len(ob)):
ob[hi][0] = ob[hi][0].split('\t')
ob = ob[:-1]
for hello in range(len(ob)):
longi = ob[hello][0][8]
lati = ob[hello][0][9]
rad = ob[hello][0][10]
obstacle.append([longi,lati,rad])
read_waypoint(waypoint_file)
print ("initial", waypoint)
read_obstacle(obstacle_file)
#print ("obstacle = ",obstacle)
########################################################################
def convert(arr):
for m in range(len(arr)):
for n in range(len(arr[0])):
arr[m][n] = float(arr[m][n])
convert(waypoint)
convert(obstacle)
########################################################################
for i in range(len(waypoint)):
cwp = waypoint[i]
clong = cwp[0]
clat = cwp[1]
cx,cy = translatell2xy(clat,clong)
waypoint[i] = [cx,cy]
#print ("old : ",waypoint,"\n")
for j in range(len(obstacle)):
cobs = obstacle[j]
clong = cobs[0]
clat = cobs[1]
cx, cy = translatell2xy(clat, clong)
crad = toM(cobs[2])
obstacle[j] = [cx,cy,crad]
########################################################################
# obstacle avoidance
def distance(p1,p2,co):
x1 = p1[0]
y1 = p1[1]
x2 = p2[0]
y2 = p2[1]
x0 = co[0]
y0 = co[1]
num = abs((y0 - y1)*(x2 - x1) - (x0 - x1)*(y2 - y1))
den = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
return (num/den)
def main():
#print (waypoint)
for i in range(len(waypoint)):
if (i == 0):
waypoint_final.append(waypoint[i])
#print ("sabse pehla daala")
else:
safe = True
for j in range(len(obstacle)):
d = distance(waypoint[i-1],waypoint[i],obstacle[j][:-1])
r = obstacle[j][2]
if (d < r + safety_distance):
safe = False
#print ("thukka")
break
if (safe):
waypoint_final.append(waypoint[i])
#print ("safe tha")
else:
for l in range(len(obstacle)):
d = distance(waypoint[i-1],waypoint[i],obstacle[l][:-1])
r = obstacle[l][2]
if (d < r + safety_distance):
##################################################
# finding the point to be added to waypoint file
# using intersection of two tangents from both the
# waypoints.
a, b = find_point(waypoint[i-1],waypoint[i],r + safety_distance,[obstacle[l][0], obstacle[l][1]])
##################################################
intersection = [a,b]
waypoint_final.append(intersection)
#print ("naya daala")
waypoint_final.append(waypoint[i])
#print (waypoint_final)
for k in range(len(waypoint_final)):
cx = waypoint_final[i][0]
cy = waypoint_final[i][1]
lat, lng = translatexy2ll(cx,cy)
waypoint_final_.append([lng,lat])
print ("final",waypoint_final_)
######################################################################
final_wp_file = "./new_waypoint.txt"
file = open(final_wp_file,"w")
file.write('QGC' + '\t' + 'WPL' + '\t' + '110' + '\n')
for p in range(len(waypoint_final_)):
file.write(str(p) + '\t' + str(0) + '\t' + str(0) + '\t' + str(16) + '\t' + str(0) + '\t' + str(0) + '\t' + str(0) + '\t' + str(0) + '\t' + str(waypoint_final_[p][0]) + '\t' + str(waypoint_final_[p][1]) + '\t' + str(550) + '\t' + str(1) + "\n")
file.close()
##########################################################################
main()