-
Notifications
You must be signed in to change notification settings - Fork 0
/
arcade_joystickController_bubulle_demo.py
325 lines (244 loc) · 10.3 KB
/
arcade_joystickController_bubulle_demo.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
#!/usr/bin/env python
import logging
from pynput import keyboard
import numpy as np
from pycrazyswarm import *
import sys
import signal
import rospy
from sensor_msgs.msg import Joy
from std_msgs.msg import String, Float64MultiArray
Z = 0.3
sleepRate = 30
import uav_trajectory
import dvic_demo #ALL TRAJECTORIES...
def signal_handler(signal, frame):
sys.exit(0)
class KeyboardDrone:
def __init__(self, cfs):
self.ids = (5, 6)
self.cfs = cfs
self.velocity = [0, 0, 0, 0, 0, 0] #CF API DOES NOT TAKE FLOATS // CURRENTLY FOR SIX DRONES
self.direction = np.array([0, 0, 0, 0, 0, 0]) #ENABLE DIRECTIONS // CURRENTLY FOR TWO DRONES
self.arm = True
self.base_velocity = 0.3
self.ang_velocity = 120
self.takeoff_height = 0.5
self.sleeptime = 0.5
self.control_from_joystick = False
self.heli = uav_trajectory.Trajectory()
self.heli.loadcsv("helicoidale.csv")
self.traj0 = uav_trajectory.Trajectory()
self.traj0.loadcsv("figure8.csv")
self.rdev18_traj = uav_trajectory.Trajectory()
self.rdev18_traj.loadcsv("demo_shapes/rdev_18deg.csv")
self.ldev18_traj = uav_trajectory.Trajectory()
self.ldev18_traj.loadcsv("demo_shapes/ldev_18deg.csv")
self.joystick = Joy()
self.joystick.axes = [-0.0, -0.0, -0.0, -0.0, 0.0, -0.0]
self.joystick.buttons = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print ('Press B0 to take off!')
for cf in self.cfs:
if cf.id == 1:
self.cf1 = cf
print("cf1 found")
#self.cf1.takeoff(targetHeight=self.takeoff_height, duration=3.0)
if cf.id == 2:
self.cf2 = cf
print("cf2 found")
#self.cf2.takeoff(targetHeight=self.takeoff_height, duration=3.0)
if cf.id == 3:
self.cf3 = cf
print("cf3 found")
#self.cf3.takeoff(targetHeight=self.takeoff_height, duration=3.0)
def read_joystick(self, key):
# self.joystick.axes = key.axes
# self.joystick.buttons = key.buttons
if key != None:
self.joystick = key
# axes: [-0.0, -0.0, -0.0, -0.0, -1.0, -0.0]
# buttons: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
def on_press(self, key):
#print(key)
print('connected to joy')
# A subscriber to the topic '/joy'. self.on_press is called
# when a message of type Joy is received.
"""MSG HEADER INDICATES THE DRONE ID"""
# print("hi")
# if key.data[0] == '1':
# #print ('update v1.')
# velocity_cf1 = float(key.data[:1])
# self.velocity[0] = self.base_velocity * velocity_cf1
# if key.data[0] == '2':
# #print ('update v2.')
# velocity_cf2 = float(key.data[:1])
# self.velocity[1] = self.base_velocity * velocity_cf2
# if key.data[0] == '3':
# #print ('update v3')
# velocity_cf3 = float(key.data[:1])
# self.velocity[2] *= self.base_velocity * velocity_cf3
"""SEPARATE SPEED COEFFICIENT UPDATES THE VELOCITY: SECURITYSPEED[6]*SELF.BASE_VELOCITY = SELF.VELOCITY[6]"""
msg = rospy.wait_for_message('/security_speed', Float64MultiArray)
print("i'm ok")
update_drone1 = msg.data[self.ids[0]-1]
#print("update {:.3f}".format(update)) #FOR DEBUGGING.
self.velocity[self.ids[0]-1] = update_drone1 * self.base_velocity
update_drone2 = msg.data[self.ids[1]-1]
#print("update {:.3f}".format(update)) #FOR DEBUGGING.
self.velocity[self.ids[1]-1] = update_drone2 * self.base_velocity
## MAKING A SELF.DIRECTION STRUCTURE: indices [0, 1, 2] refers to /j0 JOYSTICK, ordered as [+up/-down, +left/-right, {empty because plane for now}]
if key.axes[0]== 1:
print('LEFT')
self.direction[0] = 1
if key.axes[0]== -1:
print('RIGHT')
self.direction[0] = -1
if key.axes[0]== 0:
#print('LR FIXED')
self.direction[0] = 0
if key.axes[1]== 1:
print('UP')
self.direction[1] = 1
if key.axes[1]== -1:
print('DOWN')
self.direction[1] = -1
if key.axes[1]== 0:
#print('UD FIXED')
self.direction[1] = 0
if key.buttons[0]== 1:
print('B0: TAKEOFF') #take_off
if self.arm == True:
for cf in self.cfs:
if cf.id == self.ids[0]:
print("Takeoff.")
self.direction[0] = 0
self.direction[1] = 0
cf.takeoff(targetHeight=self.takeoff_height, duration=3.0)
if key.buttons[1]== 1:
print('B1: LAND')
if self.arm == True:
for cf in self.cfs:
if cf.id == self.ids[0]:
print("Land.")
self.direction[0] = 0
self.direction[1] = 0
cf.land(0.05, duration = 3.0)
if key.buttons[2]== 1:
print('B2: JOYSTICK MODE ACTIVATED')
self.control_from_joystick = True
if key.buttons[3]== 1:
print('B3: HELI, ONE DRONE')
self.control_from_joystick=False
self.trajectory = "1 heli"
# for cf in allcfs.crazyflies:
# if cf.id == self.ids[0]:
# cf.uploadTrajectory(0, 0, self.heli)
# TRIALS = 1
# TIMESCALE = 1
# dvic_demo.fly(self.ids[0], allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
if key.buttons[4]==1:
print('B4: RIGHT DEV, DRONE 1')
TRIALS = 1
TIMESCALE = 0.75
#for i in range(TRIALS):
# for cf in allcfs.crazyflies:
# cf.uploadTrajectory(0, 0, self.rdev18_traj)
# dvic_demo.fly(self.ids[0], allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
if key.buttons[5]== 1:
print('B5: LEFT DEV, DRONE 1')
TRIALS = 1
TIMESCALE = 0.75
#for i in range(TRIALS):
# for cf in allcfs.crazyflies:
# cf.uploadTrajectory(0, 0, self.ldev18_traj)
# dvic_demo.fly(self.ids[0], allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
## MAKING A SELF.DIRECTION STRUCTURE: indices [3, 4, 5] refers to /j0 JOYSTICK, ordered as [+up/-down, +left/-right, {empty because plane for now}]
if key.axes[2]== 1:
print('LEFT')
self.direction[2] = 1
if key.axes[2]== -1:
print('RIGHT')
self.direction[2] = -1
if key.axes[2]== 0:
#print('LR FIXED')
self.direction[2] = 0
if key.axes[3]== 1:
print('UP')
self.direction[3] = 1
if key.axes[3]== -1:
print('DOWN')
self.direction[3] = -1
if key.axes[3]== 0:
#print('UD FIXED')
self.direction[3] = 0
if key.buttons[6]== 1:
print('B6: TAKEOFF') #take_off
#self.velocity_streaming = False
if self.arm == True:
for cf in self.cfs:
if cf.id == self.ids[1]:
print("Takeoff.")
self.direction[3] = 0
self.direction[2] = 0
cf.takeoff(targetHeight=self.takeoff_height, duration=3.0)
if key.buttons[7]== 1:
print('B7: LAND')
#self.velocity_streaming = False
if self.arm == True:
for cf in self.cfs:
if cf.id == self.ids[1]:
print("Land.")
self.direction[3] = 0
self.direction[2] = 0
cf.land(0.05, duration = 3.0)
if key.buttons[8]== 1:
print('B8: FIGURE OF 8, BOTH')
self.control_from_joystick = False
# for cf in allcfs.crazyflies:
# cf.uploadTrajectory(0, 0, self.traj0)
# TRIALS = 1
# TIMESCALE = 1
# dvic_demo.fly_two_drones(allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
if key.buttons[9] == 1:
print('B9: HELI, DRONE 2')
self.control_from_joystick=False
# for cf in allcfs.crazyflies:
# if cf.id == self.ids[1]:
# cf.uploadTrajectory(0, 0, self.heli)
# TRIALS = 1
# TIMESCALE = 1
# dvic_demo.fly(self.ids[1], allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
if key.buttons[10]==1:
print('B10: RIGHT DEV, DRONE 2')
TRIALS = 1
TIMESCALE = 0.75
#for i in range(TRIALS):
# for cf in allcfs.crazyflies:
# cf.uploadTrajectory(0, 0, self.rdev18_traj)
# dvic_demo.fly(self.ids[1], allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
if key.buttons[11]== 1:
print('B11')
TRIALS = 1
TIMESCALE = 0.75
#for i in range(TRIALS):
# for cf in allcfs.crazyflies:
# cf.uploadTrajectory(0, 0, self.ldev18_traj)
# dvic_demo.fly(self.ids[1], allcfs, swarm, timeHelper, TRIALS, TIMESCALE)
if self.arm == True: #and self.velocity_streaming == True:
for cf in self.cfs:
if cf.id == self.ids[0]:
cf.cmdVelocityWorld(np.array([self.velocity[self.ids[0]-1]*self.direction[0], self.velocity[self.ids[0]-1]*self.direction[1], 0]), yawRate=0)
if cf.id == self.ids[1]:
cf.cmdVelocityWorld(np.array([self.velocity[self.ids[1]-1]*self.direction[2], self.velocity[self.ids[1]-1]*self.direction[3], 0]), yawRate=0)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == '__main__':
#rospy.init_node('joystick_to_drone')
print("allo")
swarm = Crazyswarm()
timeHelper = swarm.timeHelper
allcfs = swarm.allcfs
print("connect to drones")
drone = KeyboardDrone(allcfs.crazyflies)
print('will connect to joy ?')
joy_sub = rospy.Subscriber('/joy', Joy, drone.on_press)
rospy.spin()