-
Notifications
You must be signed in to change notification settings - Fork 8
/
combined.py
181 lines (143 loc) · 5.03 KB
/
combined.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
from math import radians, cos, sin, asin, sqrt
import numpy as np
import time
from dronekit import connect, VehicleMode
from pymavlink import mavutil
dt=1
T=50000
N=2
pos=[[38.146200,-76.428387],[38.145313,-76.429119],[38.149222,-76.429483],[38.150233,-76.430855]]
class swarm_bot:
def __init__(self,n,pos,s):
self.id=n
self.string=str(s)
self.pos=pos
self.velocity = [0,0]
print("Connecting to Vehicle id:",n)
self.vehicle = connect(self.string)#, wait_ready=True)
print("Connected")
def get_pos(self):
self.pos= [self.vehicle.location.global_frame.lat,self.vehicle.location.global_frame.lon]
return self.pos
def update_vel(self,v):
self.velocity=v
velocity_x=v[0]
velocity_y=v[1]
velocity_z=0
"""
Move vehicle in direction based on specified velocity vectors.
"""
msg = self.vehicle.message_factory.set_position_target_global_int_encode(
0, # time_boot_ms (not used)
0, 0, # target system, target component
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
0b0000111111000111, # type_mask (only speeds enabled)
0, # lat_int - X Position in WGS84 frame in 1e7 * meters
0, # lon_int - Y Position in WGS84 frame in 1e7 * meters
0, # alt - Altitude in meters in AMSL altitude(not WGS84 if absolute or relative)
# altitude above terrain if GLOBAL_TERRAIN_ALT_INT
velocity_x, # X velocity in NED frame in m/s
velocity_y, # Y velocity in NED frame in m/s
velocity_z, # Z velocity in NED frame in m/s
0, 0, 0, # afx, afy, afz acceleration (not supported yet, ignored in GCS_Mavlink)
0, 0) # yaw, yaw_rate (not supported yet, ignored in GCS_Mavlink)
self.vehicle.send_mavlink(msg)
def arm_and_takeoff(self, aTargetAltitude):
print "Basic pre-arm checks",self.id
while not self.vehicle.is_armable:
print " Waiting for vehicle to initialise...",self.id
time.sleep(1)
print "Arming motors",self.id
# Copter should arm in GUIDED mode
self.vehicle.mode = VehicleMode("GUIDED")
self.vehicle.armed = True
while not self.vehicle.armed:
print " Waiting for arming...",self.id
time.sleep(1)
print "Taking off!",self.id
self.vehicle.simple_takeoff(aTargetAltitude)
while True:
print " Altitude: ", self.vehicle.location.global_relative_frame.alt
if self.vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95:
print "Reached target altitude",self.id
break
time.sleep(1)
def land(self):
self.vehicle.mode = VehicleMode("LAND")
def distance(lat1, lat2, lon1, lon2):
# The math module contains a function named
# radians which converts from degrees to radians.
lon1 = radians(lon1)
lon2 = radians(lon2)
lat1 = radians(lat1)
lat2 = radians(lat2)
# Haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * asin(sqrt(a))
# Radius of earth in kilometers. Use 3956 for miles
r = 6371
# calculate the result
return(c * r)
vehicle=list()
print N
for i in range(N):
if i==0:
s='127.0.0.1:14551'
else:
s='127.0.0.1:14561'
temp='v'+str(i+1)
temp=swarm_bot(i+1,pos[i],s)
vehicle.append(temp)
for i in range(N):
vehicle[i].arm_and_takeoff(5)
print vehicle
for j in range(1,T,dt):
for i in range(N):
v = [0,0]
pos1=vehicle[i].get_pos()
print "Vehicle id", i+1
for k in range(N):
pos2=vehicle[k].get_pos()
#print pos2
#d=math.sqrt((pos1[0]-pos2[0])**2 + (pos1[1]-pos2[1])**2)
d = distance(pos1[0],pos2[0],pos1[1],pos2[1])
print d
#w=(d-0.001)*5000/(d-0.0005)
w = 5000
#print "d",d
#print 'w',w
e=[w*(pos2[0]-pos1[0]),w*(pos2[1]-pos1[1])]
v=[v[0]-e[0],v[1]-e[1]]
if d > 0.03:
v=[0,0];
print v
vehicle[i].update_vel(v)
#time.sleep(1)
print vehicle
for j in range(1,T,dt):
for i in range(N):
v = [0,0]
pos1=vehicle[i].get_pos()
print "Vehicle id", i+1
for k in range(N):
if i==k:
continue
pos2=vehicle[k].get_pos()
d = distance(pos1[0],pos2[0],pos1[1],pos2[1])
print d
#w=(d-0.001)*5000/(d-0.0005)
w=5000
e=[w*(pos2[0]-pos1[0]),w*(pos2[1]-pos1[1])]
v=[v[0]+e[0],v[1]+e[1]]
if d<0.01:
v=[0,0]
print v
vehicle[i].update_vel(v)
#time.sleep(1)
while True:
print "LAND1"
vehicle[0].land()
print "LAND2"
vehicle[1].land()