-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
231 lines (208 loc) · 7.14 KB
/
settings.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
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
"""
Global variable and function is decleared
"""
import numpy
import vg
def get_magnitude(vector: numpy.ndarray) -> numpy.float64:
"return magnitude of matrix"
return numpy.linalg.norm(vector)
def set_magnitude(vector: numpy.ndarray, factor: float) -> numpy.ndarray:
"set magnitude of matrix"
magnitude = numpy.linalg.norm(vector)
if magnitude == 0.0:
magnitude = 0.001
newX = vector[0] * factor / magnitude
newY = vector[1] * factor / magnitude
newZ = vector[2] * factor / magnitude
return numpy.array([newX, newY, newZ])
def get_distance(vector1: numpy.ndarray, vector2: numpy.ndarray):
"scaler distance between two vectors"
return numpy.linalg.norm(vector1 - vector2)
def get_rotation_matrix(degree) -> numpy.ndarray:
"return rotation matrix after taking degree of rotation"
radian = degree * (numpy.pi / 180.0)
return numpy.array(
[
[numpy.cos(radian), -numpy.sin(radian), 0.0],
[numpy.sin(radian), numpy.cos(radian), 0.0],
[0.0, 0.0, 1.0]
]
)
def unit_vector(vector):
""" Returns the unit vector of the vector. """
return vector / numpy.linalg.norm(vector)
def angle_between(vec1, vec2) -> float:
"return angle between two vectors"
vec1 = unit_vector(vec1)
vec2 = unit_vector(vec2)
vec1[2] = 0.0
vec2[2] = 0.0
# inRadian = numpy.arccos(numpy.clip(numpy.dot(vec1, vec2), -1.0, 1.0))
angle = vg.angle(vec1, vec2, units='deg')
return angle
def vec3(x_val = 0.00, y_val = 0.00, z_val = 0.00) -> numpy.ndarray:
"return 3d vector"
return numpy.array([x_val, y_val, z_val])
FORMATION_OFFSET = 0.6
FORMATION_CONTROL_GAIN = 0.15
FORMATION_TRAJECTORY_GAIN = 2.0
# 2D - This is wrong do not use this!
FORMATION_TRIANGLE = numpy.array(
[
[0.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 0.0],
]
) * FORMATION_OFFSET
FORMATION_SQUARE = numpy.array(
[
[0.0, 1.0, numpy.sqrt(2.0), 1.0],
[1.0, 0.0, 1.0, numpy.sqrt(2.0)],
[numpy.sqrt(2.0), 1.0, 0.0, 1.0],
[1.0, numpy.sqrt(2.0), 1.0, 0.0],
]
)* FORMATION_OFFSET
FORMATION_PYRAMID = numpy.array(
[
[0.0, 1.0, 1.0, numpy.sqrt(2.0), numpy.sqrt(1.5)],
[1.0, 0.0, numpy.sqrt(2.0), 1.0, numpy.sqrt(1.5)],
[1.0, numpy.sqrt(2.0), 0.0, 1.0, numpy.sqrt(1.5)],
[numpy.sqrt(2.0), 1.0, 1.0, 0.0, numpy.sqrt(1.5)],
[numpy.sqrt(1.5), numpy.sqrt(1.5), numpy.sqrt(1.5), numpy.sqrt(1.5), 0.0]
]
) * FORMATION_OFFSET
FORMATION_HEXAGON = numpy.array(
[
[0.0, 1.0, numpy.sqrt(3), 2.0, numpy.sqrt(3), 1.0],
[1.0, 0.0, 1.0, numpy.sqrt(3), 2.0, numpy.sqrt(3)],
[numpy.sqrt(3), 1.0, 0.0, 1.0, numpy.sqrt(3), 2.0],
[2.0, numpy.sqrt(3), 1.0, 0.0, 1.0, numpy.sqrt(3)],
[numpy.sqrt(3), 2.0, numpy.sqrt(3), 1.0, 0.0, 1.0],
[1.0, numpy.sqrt(3), 2.0, numpy.sqrt(3), 1.0, 0.0],
]
) * FORMATION_OFFSET
def calculate_matrix(matrix:numpy.ndarray,matrix_length:float):
"Calculation of formation matrix after getting final position of each agents"
twod_list = []
for i in range(matrix_length):
new = []
for j in range(matrix_length):
new.append([0.0,0.0,0.0])
twod_list.append(new)
for i in range(matrix_length):
for j in range(matrix_length):
if i!=j:
twod_list[i][j] = matrix[j] - matrix[i]
return numpy.array(twod_list)
def nine_pyramid()->numpy.ndarray:
"return formation matrix for the nine_pyramid shape with nine agents"
ret_value = numpy.array([
[-0.5,-0.5,0.0],
[0.5,-0.5,0.0],
[0.5,0.5,0.0],
[-0.5,0.5,0.0],
[-0.5,-0.5,1.0],
[0.5,-0.5,1.0],
[0.5,0.5,1.0],
[-0.5,0.5,1.0],
[0.5+0.5*numpy.sqrt(3),0.0,0.5]
])* FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def cube()->numpy.ndarray:
"return formation matrix for the cube shape with 8 agents"
ret_value = numpy.array([
[-0.5,-0.5,0.0],
[0.5,-0.5,0.0],
[0.5,0.5,0.0],
[-0.5,0.5,0.0],
[-0.5,-0.5,1.0],
[0.5,-0.5,1.0],
[0.5,0.5,1.0],
[-0.5,0.5,1.0]
])* FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def triangle_prism():
"return formation matrix for the triangle prism shape with 6 agents"
ret_value = numpy.array([
[-0.5,0.0,0.0],
[0.5,0.0,0.0],
[0,0.5*numpy.sqrt(3),0.0],
[-0.5,0.0,1.0],
[0.5,0.0,1.0],
[0,0.5*numpy.sqrt(3),1.0]
])
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def square_pyramid():
"return formation matrix for the square pyramid shape with 4 agents"
ret_value = numpy.array([
[-0.5,-0.5,0.0],
[0.5,-0.5,0.0],
[0.5,0.5,0.0],
[-0.5,0.5,0.0],
[0.0,0.0,0.5*numpy.sqrt(3)]
])* FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def square():
"return formation matrix for the square(2d) shape with 4 agents"
ret_value = numpy.array([
[-0.5,-0.5,0.0],
[0.5,-0.5,0.0],
[0.5,0.5,0.0],
[-0.5,0.5,0.0],
])* FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def v_shape():
"return formation matrix for the V shape with 5 agents"
ret_value = numpy.array([
[-1.0,0.0,0.0],
[-0.5,1.0,0.0],
[0.0,2.0,0.0],
[0.5,1.0,0.0],
[1.0,0.0,0.0]
])*FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def triangle()->numpy.ndarray:
"return formation matrix for the triangle(2d) shape with 3 agents"
ret_value = numpy.array([
[-0.5,0.0,1.0],
[0.5,0.0,1.0],
[0.0,0.5,1.0]
])*FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def crescent()->numpy.ndarray:
"return formation matrix for the crescent shape with 5 agents"
ret_value = numpy.array([
[-0.5,0.0,0.0],
[-numpy.sqrt(2)/4,numpy.sqrt(2)/4,0.0],
[0.0,0.5,0.0],
[numpy.sqrt(2)/4,numpy.sqrt(2)/4,0.0],
[0.5,0.0,0.0]
])*FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def myself()->numpy.ndarray:
ret_value = numpy.array(
[0.3,0.0,0.5]
)*FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
def pyramid():
"return formation matrix for the square(2d) shape with 4 agents"
ret_value = numpy.array([
[0.5,1.0,0.0],
[0.1,0.5,0.0],
[1.5,0.0,0.0],
[1.0,-0.5,0.0],
[0.5,-1.0,0.0]
])* FORMATION_OFFSET
matrix_length = int(ret_value.size/3)
return calculate_matrix(ret_value,matrix_length=matrix_length)
if __name__=="__main__":
print(triangle())