-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_planning.py
114 lines (95 loc) · 3.5 KB
/
path_planning.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
# Implement "A path planning algorithm for a crop monitoring fixed-wing unmanned aerial system"
# https://link.springer.com/article/10.1007/s11432-023-4087-4
import numpy as np
# Required Input: Map vertex Matrix - 40 by 140m rectangle - "the matrix containing column-wise stacked polygon vertices"
# Final output: two sets of Waypoints - Wn and Wf - Wn is coordinates of straight line path primitives closert to UAS, Wf is the opposite
def main(V=np.array([[0, 0, 140, 140, 0], [0, 40, 40, 0, 0]])):
M = V.shape[1] - 1 # this is the number of edges, which is less than the full loop
# Output: d*, θ*, Vf
print("Algorithm 1: Sweep Direction Optimization")
# Algorithm 1
# d, θ and Vf are labeled as OUTput for now
dStar = 0
thetaStar = 0
Vf = 0
iStar = 0
# matlab code index 2 is actually index 1
for i in range(1, M):
theta = np.arctan2((V[1, i + 1] - V[1, i]), (V[0, i + 1] - V[0, i]))
rotationMatrix = np.array(
[
[np.cos(theta), np.sin(theta)],
[-np.sin(theta), np.cos(theta)],
]
)
Ṽ = rotationMatrix @ V
d = np.amax(Ṽ[1]) - Ṽ[1, i]
if i == 1 or d < dStar:
dStar = d
thetaStar = theta
iStar = i # ?
Vs = np.array([])
if iStar == M:
Vs = np.hstack([V[:, M - 1 :], V[:, : M - 1]]) # possible place for error (M-1)
else:
Vs = np.hstack([V[:, iStar - 1 :], V[:, : iStar - 1]])
rotationMatrix2 = np.array(
[
[np.cos(thetaStar), np.sin(thetaStar)],
[-np.sin(thetaStar), np.cos(thetaStar)],
]
)
VStar = rotationMatrix2 @ Vs
print("VStar", VStar)
print()
print("Algorithm 2: Waypoint Generation")
# Algorithm #2
# Input Ly, dStarP, VStar, Ns, M - all fake values for the next few lines
altitude = 100 #! Ideally the altitude is provided by the aircraft, we assume constant altitude
Ly = 833 # 640 pixels (idek what units are being used)
Ns = 10 #! need to calculate
dStarP = (dStar - Ly) / (Ns - 1)
# I think Yn, Xn, Yf, Xf are two single dimension arrays
yn = np.zeros(Ns)
yf = np.zeros(Ns)
xn = np.zeros(Ns)
xf = np.zeros(Ns)
# Output Wn, Wf
# Line 1-2
yn[0] = Ly / 2
yf[0] = Ly / 2
# Line 3
for j in range(1, Ns):
yn[j] = yn[j - 1] - dStarP
yf[j] = yf[j - 1] - dStarP
# Line 7
for i in range(Ns):
for j in range(
M - 1, 0, -1
): # might be troublesome, check if it should be M-1 or M and 0 or 2 and -1 (from j = M to 2)
if VStar[1, j] >= yn[i]:
k = (VStar[0, j] - VStar[0, j + 1]) / (VStar[1, j] - VStar[1, j + 1])
xn[i] = k * (yn[i] - VStar[1, j]) + VStar[0, j]
# Line 16
for i in range(Ns):
for j in range(1, M):
if VStar[1, j] >= yf[i]:
k = (VStar[0, j] - VStar[0, j - 1]) / (VStar[1, j] - VStar[1, j - 1])
xf[i] = k * (yf[i] - VStar[1, j]) + VStar[0, j]
# Line 25: Wn
Wn = np.vstack((xn, yn))
# Line 26: Wf
Wf = np.vstack((xf, yf))
f = open("waypoints.txt", "w")
f.write(str(Wn) + "\n" + str(Wf) + "\n")
f.close()
return Wn, Wf
if __name__ == "__main__":
V = np.array(
input(
"Enter the map vertex matrix (default: [[0, 0, 140, 140, 0], [0, 40, 40, 0, 0]]): "
)
or [[0, 0, 140, 140, 0], [0, 40, 40, 0, 0]]
)
Wn, Wf = main(V)
print("Waypoints generated: ", Wn, Wf)