-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsons.py
executable file
·213 lines (157 loc) · 6.17 KB
/
sons.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
#!/usr/bin/env python
import rospy
from math import acos
from math import sqrt
from math import pi
from geometry_msgs.msg import PoseStamped
son_location = [[], []]
def pose_callback1(msg1):
sp1 = [msg1.pose.position.x, msg1.pose.position.y, msg1.pose.position.z]
son_location[0] = sp1
def pose_callback2(msg2):
sp2 = [msg2.pose.position.x, msg2.pose.position.y, msg2.pose.position.z]
son_location[1] = sp2
"""******************************************************************************************"""
"""get son information"""
def getSonPoseSubscriber(numson):
# forming a son subscriber topic list
sonSubTopic = []
# uav1 is mom.
for i in range(numson):
topic = 'uav' + str(i+2) +'/mavros/local_position/pose'
sonSubTopic.append(topic)
# forming a son subscriber list
sonPose = []
sonsub1 = rospy.Subscriber(sonSubTopic[0], PoseStamped, pose_callback1)
sonsub2 = rospy.Subscriber(sonSubTopic[1], PoseStamped, pose_callback2)
def getSonVector(momLoc, sonLoc, numberOfSons):
sonVectorFromMom = []
for i in range(len(sonLoc)):
sonVec = []
# x1-x2, y1-y2, z1-z2
for j in range(len(sonLoc[i])):
sonVec.append(momLoc[j] - sonLoc[i][j])
sonVectorFromMom.append(sonVec)
return sonVectorFromMom
def getSonPosePublisher(numberofSons):
sonPubTopic = []
for i in range(numberofSons):
topic = 'uav' + str(i+2) +'/mavros/setpoint_position/local'
sonPubTopic.append(topic)
sonPublisher = []
for i in range(numberofSons):
sonpub = rospy.Publisher(sonPubTopic[i], PoseStamped, queue_size=10)
sonPublisher.append(sonpub)
return sonPublisher
"""def getSonVelPublisher(numberofSons):
sonVelTopic = []
for i in range(numberofSons):
topic = 'uav' + str(i+1) + '/cmd_vel'
sonVelTopic.append(topic)
sonVelPub = []
for i in range(numberofSons):
sonvpub = rospy.Publisher(sonVelTopic[i], Twist, queue_size=10)
sonVelPub.append(sonvpub)
return sonVelPub"""
"""***********************************************************************************************************"""
"""calculate angle and distance"""
def norm(vector):
l = 0
for i in range(len(vector)):
l += vector[i] ** 2
return sqrt(l)
def dot_product(v,w):
dp = 0
for i in range(len(v)):
dp += v[i] * w[i]
return dp
def getSonDistanceFromMom(sonVector):
distancelist = []
for i in range(len(sonVector)):
distancelist.append(getDistance(sonVector[i]))
return distancelist
def getDistance(sonvector):
squared = 0
for i in range(len(sonvector)):
squared += (sonvector[i]) ** 2
distance = sqrt(squared)
return distance
def getSonAngleBetween(sonVector):
angle = []
rad = []
for i in range(len(sonVector)):
for j in range(len(sonVector)):
denominator = norm(sonVector[i])*norm(sonVector[j])
if i == 0 and (i != j) and (denominator != 0):
cosx = dot_product(sonVector[i],sonVector[j]) / denominator
rad.append(acos(cosx))
if (i > 0) and (i > j) and (i < (len(sonVector) -1)) and (denominator != 0):
cosx = dot_product(sonVector[i],sonVector[j]) / denominator
rad.append(acos(cosx))
for m in range(len(rad)):
angle.append(rad[m] * 180 / pi)
return angle
"""***********************************************************************************************************"""
def updateSonLocation(numberOfSons, momLocation):
rate = rospy.Rate(50)
# sons subscribers
getSonPoseSubscriber(numberOfSons)
# form vector from son to mom
sonVector = getSonVector(momLocation, son_location, numberOfSons)
# sons pose publishers list
sonPosePublisher = getSonPosePublisher(numberOfSons)
"""# sons velocity publishers list
sonVelPublisher = getSonVelPublisher(numberofSons)"""
# son distance
sv = []
if len(son_location) != 0 and len(son_location[0]) != 0:
for i in range(3):
sv.append(son_location[0][i] - son_location[1][i])
sonDistance = getDistance(sv)
# make triangle shape
# sonDistance = [distance of 12, 13, 14 ... 23, 24 ... 34, 35 ...]
# sonAngle = [angle of 12, 13, 14 ... 23, 24 ... 34, 35 ... ]
sonDistanceFromMom = getSonDistanceFromMom(sonVector)
sonAngle = getSonAngleBetween(sonVector)
sonPose = []
for i in range(numberOfSons):
# sonPose = [[pose of son_1], [pose of son_2]]
sonPose.append(PoseStamped())
if len(son_location[0]) != 0:
# adjust location if z_mom - z_son
for i in range(len(son_location)):
# to adjust z location
if momLocation[2] != son_location[i][2] :
sonPose[i].pose.position.z = momLocation[2]
# to adjust y location
if abs(son_location[i][1] - momLocation[1]) != 1:
sonPose[i].pose.position.y = momLocation[1] - 1
# to adjust x location
if abs(son_location[i][0] - momLocation[0]) != 0.5:
if i == 0:
sonPose[i].pose.position.x = momLocation[0] - 0.5
elif i == 1:
sonPose[i].pose.position.x = momLocation[0] + 0.5
if sonDistance != 1:
if i == 0:
sonPose[i].pose.position.x = sonPose[i].pose.position.x - 0.1
sonPose[i].pose.position.y = sonPose[i].pose.position.y - 0.1
elif i == 1:
sonPose[i].pose.position.x = sonPose[i].pose.position.x + 0.1
sonPose[i].pose.position.y = sonPose[i].pose.position.y + 0.1
# to initialize change
else:
sonPose[i].pose.position.x = momLocation[0]
sonPose[i].pose.position.y = momLocation[1]
for i in range(len(sonPosePublisher)):
sonPose[i].header.stamp = rospy.Time.now()
sonPosePublisher[i].publish(sonPose[i])
### check the syntax of subscriber and publiser.
### use virtual structure or circle????
# find the centroid of a circle of some radius
# then assign each vehicle to the location where
# its location will be in a constant distance from centroid
# or set the formation as triangle first.
# let the mom be one of the vertices
# sons have to be in the same distance from mom
# perpendicular distance of baseline to mom will be set at constant value