-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmove.py
90 lines (62 loc) · 2.51 KB
/
move.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
import airsim,time,threading
import numpy as np
# connect to the AirSim simulator
global client
client = airsim.MultirotorClient()
client.confirmConnection()
client.reset()
for count in range(0,2):
(client.enableApiControl(True,vehicle_name='Drone' + str(count + 1)))
(client.armDisarm(True,vehicle_name='Drone' + str(count + 1)))
global V, Vt, eps
Vt=1
V=2
eps=0.1
#################################################################### TAKE-OFF
def myjoint_TAKEOFF(goalx, goaly, goalz, Vel,vehicle_name):
dist=(abs(goalz) - abs(client.simGetGroundTruthKinematics(vehicle_name).position.z_val))
time.sleep(abs(dist)/Vel)
#################################################################### NAVIGATE
def myjoint(goalx,goaly,goalz,Vel,vehicle_name):
global lk
lk.acquire()
pos = client.simGetGroundTruthKinematics(vehicle_name)
lk.release()
dist = ((np.sqrt(np.power((goalx -pos.position.x_val),2) + np.power((goaly - pos.position.y_val),2)+ np.power((goalz - pos.position.z_val),2))))
print ("waiting ",int(abs(dist)/Vel), vehicle_name)
time.sleep(int(abs(dist)/Vel))
lk.acquire()
pos = client.simGetGroundTruthKinematics(vehicle_name)
lk.release()
while ((np.sqrt(np.power((goalx -pos.position.x_val),2) + np.power((goaly - pos.position.y_val),2)+ np.power((goalz - pos.position.z_val),2))))>eps:
lk.acquire()
pos = client.simGetGroundTruthKinematics(vehicle_name)
lk.release()
lk=threading.Lock()
def move_drone(n):
global lk
name=["Drone1", "Drone2"]
positions=[[[15,0,-4],[15,5,-4],[0,5,-4],[0,0,-4]],
[[12,0,-3],[12,5,-3],[0,5,-3],[0,0,-3]]]
for x,y,z in positions[n]:
lk.acquire()
client.moveToPositionAsync(x,y,z, V, vehicle_name=name[n])
lk.release()
print("goint to", x,y,z,name[n])
myjoint(x,y,z,V,vehicle_name=name[n])
print("reached to", x,y,z,name[n])
client.moveToPositionAsync(0,0,-2, Vt, vehicle_name="Drone1")
while (abs(-2) - abs(client.simGetGroundTruthKinematics("Drone1").position.z_val))>eps:
myjoint_TAKEOFF(0,0,-2,Vt,"Drone1")
client.moveToPositionAsync(0,0,-2, Vt, vehicle_name="Drone2")
while (abs(-2) - abs(client.simGetGroundTruthKinematics("Drone2").position.z_val))>eps:
myjoint_TAKEOFF(0,0,-2,Vt,"Drone2")
#move_drone
global d
d=[]
for i in [0,1]:
d.append(threading.Thread(target=move_drone, args=[i]))
d[i].start()
for i in [0,1]:
d[i].join()
print("Done!")