forked from DaemonLab/Drone-Swarm-InterIIT-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3_toge.py
514 lines (348 loc) · 16 KB
/
task3_toge.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from multiprocessing import Pipe
from pypluto.drone import pluto
import numpy as np
import time
import threading
# poseDrone2 = None
# poseDrone1 = None
# POSE1DICT = {
# '0' : poseDrone1,
# '8' : poseDrone2
# }
# POSE2DICT = {
# '0' : poseDrone1,
# '8' : poseDrone2
# }
# --------------------------------------------
# xTarget, yTarget, heightTarget = 646, 348, 0.8 # for hover at point
# target_array2=[]
# target_array2.append(target_array[0])
# x_target, y_target, height_target = target_array[0][0],target_array[0][1], 0.8 #pixel, pixel , height(m)
YAW_TARGET = 1.5708
# --------
#pid gains
KPx, KPy, KPz, KPyaw = 0.8, 0.4, 380 , 50
KIx, KIy, KIz, KIyaw = 0.02, 0.01, 0, 0
KDx, KDy, KDz, KDyaw = 18, 25, 10, 0
def pid(pose, target, Err, ErrI):
"""
PID Control Loop
"""
x_error, y_error, z_error, yaw_error = Err
x_errorI, y_errorI, z_errorI, yaw_errorI = ErrI
x_target, y_target, height_target = target
x_error_old = x_error
y_error_old = y_error
z_error_old = z_error
yaw_error_old = yaw_error
x, y, z, yaw = pose
x_error = x_target-x
y_error = y_target-y
z_error = height_target-z
yaw_error = YAW_TARGET-yaw
x_errorI += x_error
y_errorI += y_error
z_errorI += z_error
yaw_errorI += yaw_error
# compute derivative (variation) of errors (D)
x_errorD = x_error-x_error_old
y_errorD = y_error-y_error_old
z_errorD = z_error-z_error_old
yaw_errorD = yaw_error-yaw_error_old
# compute commands
xCommand = KPx*x_error + KIx*x_errorI + KDx*x_errorD
yCommand = KPy*y_error + KIy*y_errorI + KDy*y_errorD
zCommand = KPz*z_error + KIz*z_errorI + KDz*z_errorD
yaw_command = int( KPyaw*yaw_error + KIyaw*yaw_errorI + KDyaw*yaw_errorD)
pitch_command = int( np.cos(yaw)*xCommand + np.sin(yaw)*yCommand)
roll_command = int( -np.sin(yaw)*xCommand + np.cos(yaw)*yCommand )
throttle_command = int(zCommand)
Err = [x_error, y_error, z_error, yaw_error]
ErrI = [x_errorI, y_errorI, z_errorI, yaw_errorI]
return roll_command, pitch_command, throttle_command, yaw_command, Err, ErrI
class MissionPlanner():
def __init__(self,conn) :
#Target coords
#For Recieving Pose
self.conn = conn
self.target_array = [
[ [375, 162], #0
[538, 160],
[652, 158],
[788, 152],
[882, 175] #4
],
[
[882, 175],
[920, 240],
[922, 332],
[886, 402] #
],
[
[886, 402],
[776, 422],
[632, 428],
[486, 432],
[402, 412]#
],
[
[402, 412],
[365, 355],
[364, 248],
[375, 162]#
]
]
self.Drone1=pluto(DroneIP='10.42.0.74')
self.Drone1.connect()
self.Drone1.disarm()
self.Drone2=pluto(DroneIP='10.42.0.61')
self.Drone2.connect()
self.Drone2.disarm()
self.compliDict = {
'0' : '8',
'8' : '0'
}
self.START_OUTER_IDX = {
'0' : 0,
'8' : 3 # as this corresponds to 2 subarray, and is given to IDX
}
self.DRONEOBJDICT = {
'0' : self.Drone1 ,
'8' : self.Drone2
}
self.Drone1ID = '8'
self.Drone2ID = '0'
self.moveDrone1 = True
self.moveDrone2 = False
self.MOVEDICT = {
'0' : self.moveDrone1,
'8' : self.moveDrone2,
}
self.poseDrone1 = None
self.poseDrone2 = None
self.POSEDICT = {
'0' : self.poseDrone1,
'8' : self.poseDrone2
}
self.Task_not_done = True
self.Rectangle1_done = False
self.Rectangle2_done = False
self.RECT_DONE_DICT = {
'0' : self.Rectangle1_done ,
'8' : self.Rectangle2_done
}
self.target_idx1 = 0
self.target_idx2 = 4
self.TARGET_IDX_DICT = {
'0' : self.target_idx1 ,
'1' : self.target_idx2
}
#currently global ,
#for deciding drone's final yaw orientation
self.YAW_TARGET = 1.5708 # we can change the yaw for each here YAW_TARGET1 & YAW_TARGET2
def recv_pose(self):
'''changes the var of poses'''
print("\nListerning to pose in a sec")
time.sleep(0.5)
#we need to make task not done as False somewhere
while ( self.Task_not_done ):
if (self.conn.poll()):
pose_dict = self.conn.recv()
if not (pose_dict ):
pass
else:
self.POSEDICT[self.Drone1ID] = pose_dict[self.Drone1ID]
self.POSEDICT[self.Drone2ID] = pose_dict[self.Drone2ID]
else:
self.POSEDICT[self.Drone1ID] = None
self.POSEDICT[self.Drone2ID] = None
if( self.Rectangle1_done and self.Rectangle2_done ):
self.Task_not_done = False
print("\n-|-$--Both Rectangles Completed, closing recv_pose--|-")
def move( self, IDX , DroneID , Err , ErrI):
'''
temp target : list of 4-5 targets,whose endpt is nxt corner
DroneID : '0' or '8' '''
roll_log = []
pitch_log = []
#Local vars
temp_targets = self.target_array[IDX]
Droneobj = self.DRONEOBJDICT[DroneID]
pose = self.POSEDICT[DroneID]
#locals
xTarget, yTarget, heightTarget = temp_targets[0][0] , temp_targets[0][1], 0.8
#
tReachCount=0
temp_target_idx = 0
start = time.time()
while temp_target_idx != len(temp_targets) : #loop till idx just greater than last ele
#to change target within temp array ( one corner to other)
xTarget, yTarget, heightTarget = temp_targets[0][0] , temp_targets[0][1], 0.8
pose = self.POSEDICT[DroneID]
try:
if not pose : # pose is None
now_time = time.time()
time_detected = now_time - start
roll_command, pitch_command, throttle_command, yaw_command = 0, 0, 50, 0
if time_detected> 8 :
print(f"\nAruco not detected ,landing : {DroneID}\n")
Droneobj.land()
return 'not det'
elif (not pose)==False: #Pose not None
start = time.time()
#Target Vicinity counter block
if np.sqrt((xTarget-pose[0])**2+(yTarget-pose[1])**2)<25:
# print(f"Pose: {pose}")
tReachCount += 1
Droneobj.reset_speed() #why??? to avoid overshooting after targets
# corner target reached , then idx+1
if ( IDX < (self.START_OUTER_IDX[DroneID]-1)%4 and temp_target_idx == len(temp_targets)-1 and tReachCount >= 5):
print(f"\nReached Target Corner {temp_target_idx}, ;{DroneID}\n")
temp_target_idx +=1
break
#On the way targets reached , then idx+1 and reset tReachCounter
elif (temp_target_idx < len(temp_targets)-1 and tReachCount>= 5 ):
print(f"\nOn the way Target Reached. : {DroneID} , Target : {IDX,temp_target_idx}\n")
temp_target_idx +=1
tReachCount=0
#Last target in last array , just land & break
if IDX == (self.START_OUTER_IDX[DroneID]-1)%4 and temp_target_idx == len(temp_targets)-1 and tReachCount >=10 :
print(f"\nTask Completed\nLanding , {DroneID}")
Droneobj.land()
time.sleep(2)
return "stop"
roll_command, pitch_command, throttle_command, yaw_command, Err, ErrI = pid(pose, xTarget, yTarget, heightTarget, Err, ErrI)
#Clipping commands
if (roll_command>100):
roll_command=100
elif (roll_command<-100):
roll_command=-100
if (pitch_command>70):
pitch_command=70
elif (pitch_command<-70):
pitch_command=-70
# Set all speeds at once
Droneobj.set_all_speed(roll_command, pitch_command, throttle_command, yaw_command)
time.sleep(0.04)
'''this sleep adjusts the running of this files while loop,
so that the rate of receiving from marker files is almost matched
to that of this file sending commands to drone using api '''
# if not roll_command==0:
print(f"\n(sending) move , rcmd: {roll_command}, pcmd: {pitch_command}, tcmd:{throttle_command}, yawcmd:{yaw_command}")
except KeyboardInterrupt:
Droneobj.disarm()
print(f"Disarming Drone {DroneID}")
time.time(2)
return 'interupt'
def hover (self, target_coords,DroneID):
#locals
xTarget , yTarget ,heightTarget = target_coords[0], target_coords[1], 0.8
Droneobj = self.DRONEOBJDICT[DroneID]
pose = self.POSEDICT[DroneID]
move_state = self.MOVEDICT[DroneID]
start = time.time()
while (not move_state):
pose = self.POSEDICT[DroneID]
move_state = self.MOVEDICT[DroneID]
try:
if not pose : # pose is None
now_time = time.time()
timeout_limit = now_time - start
roll_command, pitch_command, throttle_command, yaw_command = 0, 0, 50, 0
if timeout_limit > 8 :
print("Aruco not detected ,landing")
Droneobj.land()
return "Stop"
elif (not pose)==False: #Pose not None
start = time.time()
roll_command, pitch_command, throttle_command, yaw_command, Err, ErrI = pid(pose, xTarget, yTarget, heightTarget, Err, ErrI)
print(f"\nHovering {DroneID} at {xTarget,yTarget}")
# Clipping commands
if (roll_command>100):
roll_command=100
elif (roll_command<-100):
roll_command=-100
if (pitch_command>70):
pitch_command=70
elif (pitch_command<-70):
pitch_command=-70
# Set all speeds at once
Droneobj.set_all_speed(roll_command, pitch_command, throttle_command, yaw_command)
time.sleep(0.04)
'''this sleep adjusts the running of this files while loop,
so that the rate of receiving from marker files is almost matched
to that of this file sending commands to drone using api '''
# if roll_command !=0:
print(f"\nHover : rcmd: {roll_command}, pcmd: {pitch_command}, tcmd:{throttle_command}, yawcmd:{yaw_command}")
except KeyboardInterrupt:
Droneobj.disarm()
print(f"Disarming Drone {DroneID}")
return "interupt"
def dronePlan(self,DroneID):
print(f"\n-----Starting Drone {DroneID} plan--------")
#local scope
x_error, y_error, z_error, yaw_error = 0, 0, 0, 0
x_errorI, y_errorI, z_errorI, yaw_errorI = 0, 0, 0, 0
x_errorD, y_errorD, z_errorD, yaw_errorD = 0, 0, 0, 0
x_error_old, y_error_old, z_error_old, yaw_error_old = 0, 0, 0, 0
Err = [x_error, y_error, z_error, yaw_error]
ErrI = [x_errorI, y_errorI, z_errorI, yaw_errorI]
path = [[0,0,0,0]]
DroneObj= self.DRONEOBJDICT[DroneID]
move_state = self.MOVEDICT[DroneID]
# # Drone1.connect()
#Drone1.trim(-8,20,0,0) # iit
DroneObj.trim(23, 5,0,0)# ??????????????
DroneObj.disarm()
DroneObj.arm()
DroneObj.throttle_speed(300,3)
# DroneObj.takeoff()
print(f"Takeoff: {DroneID}")
# default values when not detected
roll_command, pitch_command, throttle_command, yaw_command = 0, 0, 50, 0
start = time.time()
Rectangle_done = self.RECT_DONE_DICT[DroneID]
IDX = self.START_OUTER_IDX[DroneID] # goes from 0 to 4
'''need to tell when drone ids rect is done'''
while not Rectangle_done :
move_state = self.MOVEDICT[DroneID]
if move_state : #true at very first for drone1 and False for drone2
resp = self.move(IDX= IDX, DroneID=DroneID,Err=Err,ErrI=ErrI )
#now current Drone has reached its temp corner pt , so make itself hover and other move
self.MOVEDICT[DroneID] = False
self.MOVEDICT[self.compliDict[DroneID]] = True
#change current drones outer idx to nxt, (and pass 1st element of this new sub array to hover)
#last
if( IDX == (self.START_OUTER_IDX[DroneID]-1) %4 ):
Rectangle_done = True
DroneObj.land()
print(f"Rectangle{DroneID} Done")
time.sleep(2)
#shift to next set of temp array
if(resp == 'stop' or 'not det'):
print(f"\n\nresp (move): {resp}")
break #reached final 15th target
else: #last element of subarray
resp = self.hover( self.target_array[IDX][ len(self.target_array[IDX])-1 ] , DroneID=DroneID)
#nxt IDX to get nxt subarray to move (above in nxt loop)
IDX += 1
self.MOVEDICT[DroneID] = True
self.MOVEDICT[self.compliDict[DroneID]] = False
if (resp == "stop" or "not det" or 'interupt'):
print(f"\nresp (hover) : {resp}")
break # to break plan in case of Aruco not det
Rectangle_done = self.RECT_DONE_DICT[DroneID]
def PID_main(conn):
MP = MissionPlanner(conn=conn)
t0 = threading.Thread( target= MP.recv_pose )
t1 = threading.Thread( target= MP.dronePlan, args=(MP.Drone1ID))
t2 = threading.Thread( target= MP.dronePlan, args=(MP.Drone2ID))
print("\nListerning to pose in a sec")
time.sleep(1)
t0.start()
time.sleep(2)
t1.start()
t2.start()
t0.join()
t1.join()
t2.join()