forked from NGCP/VTOL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_scan_autonomy.py
More file actions
303 lines (242 loc) · 11.8 KB
/
quick_scan_autonomy.py
File metadata and controls
303 lines (242 loc) · 11.8 KB
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
import json
import math
import time
from conversions import *
from threading import Thread
from pymavlink import mavutil
from dronekit import connect, Command, VehicleMode, LocationGlobalRelative
from detailed_search import detailed_search
import msgpack
# first import gives access to global variables in "autonomy" namespace
# second import is for functions
import autonomy
from autonomy import comm_simulation, acknowledge, bad_msg, takeoff, land, update_thread, \
change_status, setup_xbee, start_auto_mission, setup_vehicle
search_area = None # search area object, populated by callback on start
comm_sim_on = False
# Represents a search area for quick scan aerial vehicles
class SearchArea:
def __init__(self, tl, tr, bl, br):
# tl-br are tuples containing the coordinates of rectangle corners
self.tl = tl
self.tr = tr
self.bl = bl
self.br = br
def __str__(self):
return "SearchArea(" + \
str(self.tl) + ", " + \
str(self.tr) + ", " + \
str(self.bl) + ", " + \
str(self.br) + ")"
# Callback function for messages from GCS, parses JSON message and sets globals
def xbee_callback(compressed_message, autonomyToCV):
global search_area
global comm_sim_on
if (comm_sim_on):
message = compressed_message
else:
message = msgpack.unpackb(compressed_message)
address = message.remote_device.get_64bit_addr()
msg = json.loads(message.data.decode("utf8"))
print("Received data from %s: %s" % (address, msg))
try:
msg_type = msg["type"]
if msg_type == "addMission":
area = msg["missionInfo"]["searchArea"]
search_area = SearchArea(area["topLeft"], area["topRight"],
area["bottomLeft"], area["bottomRight"])
autonomy.start_mission = True
acknowledge(address, msg["id"], autonomyToCV)
elif msg_type == "pause":
autonomy.pause_mission = True
acknowledge(address, msg["id"], autonomyToCV)
elif msg_type == "resume":
autonomy.pause_mission = False
acknowledge(address, msg["id"], autonomyToCV)
elif msg_type == "stop":
autonomy.stop_mission = True
acknowledge(address, msg["id"], autonomyToCV)
elif msg_type == "ack":
autonomy.ack_id = msg["ackid"]
else:
bad_msg(address, "Unknown message type: \'" + msg_type + "\'", autonomyToCV)
# KeyError if message was missing an expected key
except KeyError as e:
bad_msg(address, "Missing \'" + e.args[0] + "\' key", autonomyToCV)
# Generate NED and LLA waypoints in spiral pattern
def generate_waypoints(configs, search_area):
print("Begin generating waypoints")
waypointsNED = []
waypointsLLA = []
# start at the bottom left
start = search_area.tl
# end at the top right
end = search_area.br
# pre-defined in configs file
altitude = configs["altitude"]
lat = start[0]
lon = start[1]
startX, startY, startZ = geodetic2ecef(lat, lon, altitude)
start_n, start_e, d = ecef2ned(startX, startY, startZ, lat, lon, altitude)
dX = (search_area.tl[0] - search_area.bl[0]) * 111111.0
dY = (search_area.tl[1] - search_area.bl[1]) * 111111.0
height = (((search_area.tl[0] - search_area.tr[0]) * 111111.0) ** 2 +
((search_area.tl[1] - search_area.tr[1]) * 111111.0) **2) ** .5
endX, endY, endZ = geodetic2ecef(end[0], end[1], altitude)
end_n, end_e, end_d = ecef2ned(endX, endY, endZ, lat, lon, altitude)
print(height)
# fovH = math.radians(62.2) # raspicam horizontal FOV
# boxH = 2 * altitude / math.tan(fovH / 2) # height of bounding box
# overlap = (0.5 * boxH) / (2 * math.pi) # distance between zags with 50% overlap
overlap = 5.5 # 3 meters
temp_n = start_n
temp_e = start_e
i = 0
while i * 2 * overlap < height:
# convert NED to LLA
newLat, newLon, newAlt = ned2geodetic(temp_n, temp_e, d, lat, lon, altitude)
waypointsNED.append([temp_n, temp_e, d])
waypointsLLA.append(LocationGlobalRelative(newLat, newLon, newAlt))
newLat, newLon, newAlt = ned2geodetic(temp_n + dY, temp_e + dX, d, lat, lon, altitude)
waypointsNED.append([temp_n + dY, temp_e + dX, d])
waypointsLLA.append(LocationGlobalRelative(newLat, newLon, altitude))
temp_n += overlap * dX / (dY**2 + dX**2)**.5
temp_e -= overlap * dY / (dY**2 + dX**2)**.5
newLat, newLon, newAlt = ned2geodetic(temp_n + dY, temp_e + dX, d, lat, lon, altitude)
waypointsNED.append([temp_n + dY, temp_e + dX, d])
waypointsLLA.append(LocationGlobalRelative(newLat, newLon, altitude))
newLat, newLon, newAlt = ned2geodetic(temp_n, temp_e, d, lat, lon, altitude)
waypointsNED.append([temp_n, temp_e, d])
waypointsLLA.append(LocationGlobalRelative(newLat, newLon, altitude))
temp_n += overlap * dX / (dY**2 + dX**2)**.5
temp_e -= overlap * dY / (dY**2 + dX**2)**.5
i += 1
return (waypointsNED, waypointsLLA)
def quick_scan_adds_mission(configs, vehicle, lla_waypoint_list):
"""
Adds a takeoff command and four waypoint commands to the current mission.
The waypoints are positioned to form a square of side length 2*aSize around the specified LocationGlobal (aLocation).
The function assumes vehicle.commands matches the vehicle mission state
(you must have called download at least once in the session and after clearing the mission)
"""
# Declare shorthands
altitude = configs["altitude"]
cmds = vehicle.commands
print(" Clear any existing commands")
cmds.clear()
print(" Define/add new commands.")
# Due to a bug presumed to be the fault of DroneKit, the first command is ignored. Thus we have two takeoff commands
if (configs["vehicle_type"] == "VTOL"):
# Separate MAVlink message for a VTOL takeoff. This takes off to altitude and transitions
cmds.add(Command( 0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_VTOL_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 0, altitude))
cmds.add(Command( 0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_VTOL_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 0, altitude))
elif (configs["vehicle_type"] == "Quadcopter"):
cmds.add(Command( 0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 0, altitude))
cmds.add(Command( 0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 0, altitude))
# Set the target speed of vehicle
cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_DO_CHANGE_SPEED, 0, 0,
0, configs["air_speed"], -1, 0, 0, 0, 0))
# Add waypoints to the auto mission
if (configs["vehicle_type"] == "VTOL"):
for point in lla_waypoint_list:
# Planes need a waypoint tolerance
cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0,
0, configs["waypoint_tolerance"], 0, 0, point.lat, point.lon, point.alt))
# Adds dummy end point - this endpoint is the same as the last waypoint and lets us know we have reached destination.
cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0,
0, configs["waypoint_tolerance"], 0, 0, lla_waypoint_list[-1].lat, lla_waypoint_list[-1].lon, lla_waypoint_list[-1].alt))
elif (configs["vehicle_type"] == "Quadcopter"):
for point in lla_waypoint_list:
cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0,
0, 0, 0, 0, point.lat, point.lon, point.alt))
# Adds dummy end point - this endpoint is the same as the last waypoint and lets us know we have reached destination.
cmds.add(Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0,
0, 0, 0, 0, lla_waypoint_list[-1].lat, lla_waypoint_list[-1].lon, lla_waypoint_list[-1].alt))
print("Upload new commands to vehicle")
cmds.upload()
# Main autonomous flight thread
# :param configs: dict from configs file
def quick_scan_autonomy(configs, autonomyToCV, gcs_timestamp, connection_timestamp):
print("\n######################## STARTING QUICK SCAN AUTONOMY ########################")
autonomy.configs = configs
# If comms is simulated, start comm simulation thread
if configs["quick_scan_specific"]["comms_simulated"]["toggled_on"]:
global comm_sim_on
comm_sim_on = True
comm_sim = Thread(target=comm_simulation, args=(configs["quick_scan_specific"]["comms_simulated"]["comm_sim_file"], xbee_callback, autonomyToCV))
comm_sim.start()
# Otherwise, set up XBee device and add callback
else:
comm_sim = None
autonomy.xbee = setup_xbee()
#store xbee to autonomyToCV
autonomyToCV.xbeeMutex.acquire()
autonomyToCV.xbee = autonomy.xbee
autonomyToCV.xbeeMutex.release()
autonomy.gcs_timestamp = gcs_timestamp
autonomy.connection_timestamp = connection_timestamp
autonomyToCV.xbeeMutex.acquire()
autonomy.xbee.add_data_received_callback(xbee_callback)
autonomyToCV.xbeeMutex.release()
# Generate waypoints after start_mission = True
while not autonomy.start_mission:
pass
# Generate waypoints
waypoints = generate_waypoints(configs, search_area)
# Connect to vehicle
vehicle = setup_vehicle(configs)
autonomyToCV.vehicleMutex.acquire()
autonomyToCV.vehicle = vehicle
autonomyToCV.vehicleMutex.release()
# Starts the update thread
update = Thread(target=update_thread, args=(vehicle, configs["mission_control_MAC"], autonomyToCV))
update.start()
# Send mission to vehicle
quick_scan_adds_mission(configs, vehicle, waypoints[1])
# Start the mission
start_auto_mission(configs, vehicle)
# Change vehicle status to running
change_status("running")
# Fly about spiral pattern
set_autonomytocv_start(autonomyToCV, True)
while vehicle.commands.next != vehicle.commands.count:
print(vehicle.location.global_relative_frame)
time.sleep(1)
# Holds the copter in place if receives pause
if autonomy.pause_mission:
if (configs["vehicle_type"] == "VTOL"):
vehicle.mode = VehicleMode("QHOVER")
elif (configs["vehicle_type"] == "Quadcopter"):
vehicle.mode = VehicleMode("ALT_HOLD")
# Lands the vehicle if receives stop mission
elif autonomy.stop_mission:
set_autonomytocv_stop(autonomyToCV, True)
land(configs, vehicle)
return
set_autonomytocv_stop(autonomyToCV, True)
# Switch to detailed search if role switching is enabled
if configs["quick_scan_specific"]["role_switching"]:
autonomy.mission_completed = True
update.join()
detailed_search(vehicle)
else:
land(configs, vehicle)
# Ready for a new mission
autonomy.mission_completed = True
# Wait for update thread to end
update.join()
# Wait for comm simulation thread to end
if comm_sim:
comm_sim.join()
else:
autonomyToCV.xbeeMutex.acquire()
autonomy.xbee.close()
autonomyToCV.xbeeMutex.release()
def set_autonomytocv_stop(autonomyToCV, stop):
autonomyToCV.stopMutex.acquire()
autonomyToCV.stop = stop
autonomyToCV.stopMutex.release()
def set_autonomytocv_start(autonomyToCV, start):
autonomyToCV.startMutex.acquire()
autonomyToCV.start = start
autonomyToCV.startMutex.release()