-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
301 lines (252 loc) · 9 KB
/
commands.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
from re import L, M
from pymongo import MongoClient
# pprint library is used to make the output look more pretty
from pprint import pprint
import time
from pymavlink import mavutil
from pymavlink import mavwp
import cv2
# This method opens a connection and returns an object corresponding to that connection. baud rate defaults to 57600.
# @param port the port to connect to (using "COM4")
# @return an object that corresponds to the connection between the computer and the pixhawk.
sequenceCount = None
def connect(port):
return mavutil.mavlink_connection(port, baud=57600)
# waits for a heartbeat message from the connection before continuing.
# @param m the connection
def playLiveVideo():
video = cv2.VideoCapture("rtsp://104.236.89.5:8554/pi")
while True:
_, frame = video.read()
cv2.imshow("RTSP", frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
video.release()
cv2.destroyAllWindows()
def wait_heartbeat(m):
print("Waiting for APM heartbeat")
m.wait_heartbeat()
print("Heartbeat from APM (system %u component %u)" %
(m.target_system, m.target_system))
# Requests a message from the pixhawk according to its message id
# @param m the connection
# @param id the message id requested (can be found on common.xml)
def request_message(m, id):
try:
m.mav.command_long_send(
m.target_system,
m.target_component,
mavutil.mavlink.MAV_CMD_REQUEST_MESSAGE,
0,
id, 0, 0, 0, 0, 0, 0)
return m.recv_match(type='SYS_STATUS', blocking=True)
except Exception as e:
print(e)
# generates essential data by requesting mavlink messages and returns the data as a python dictionary
# @param m the connection
# @return a dictionary of essential telemetry to be used by a GUI
def display_data(m):
while True:
wait_heartbeat(m)
getCoords(m)
request_message(m, 24)
request_message(m, 74)
request_message(m, 141)
request_message(m, 1)
try:
altitude = m.messages['ALTITUDE'].altitude_monotonic
except:
altitude = "no data given"
try:
airspeed = m.messages['VFR_HUD'].airspeed
except:
airspeed = "no data given"
try:
groundspeed = m.messages['VFR_HUD'].groundspeed
except:
groundspeed = "no data given"
try:
heading = m.messages['VFR_HUD'].heading
except:
heading = "no data given"
try:
gpslat = m.messages['GPS_RAW_INT'].lat
gpslat /= 10000000.0
except:
gpslat = "no data given"
try:
gpslong = m.messages['GPS_RAW_INT'].lon
gpslong /= 10000000.0
except:
gpslong = "no data given"
try:
volt = m.messages['SYS_STATUS'].voltage_battery
volt /= 1000.0
except:
volt = "no data given"
try:
mode = m.messages['HEARTBEAT'].base_mode
modeString = " "
if mode > 128:
modeString = "armed"
else:
modeString = "unarmed"
except:
modeString = "no data given"
exportedData = {
"altitude": str(altitude),
"airspeed": str(airspeed),
"groundspeed": str(groundspeed),
"heading": str(heading),
"latitude": str(gpslat),
"longitude": str(gpslong),
"voltage": str(volt),
"flightMode": str(modeString)
}
return exportedData
# arms the drone using a command_long command
# @param m the connection
def arm(m):
m.mav.command_long_send(
m.target_system,
m.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
0,
1, 21196, 0, 0, 0, 0, 0)
# disarms the drone using a command_long command
# @param m the connection
def disarm(m):
m.mav.command_long_send(
m.target_system,
m.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
0,
0, 0, 0, 0, 0, 0, 0)
# reboots the autopilot using a command_long command
# param m the connection
def reboot(m):
m.mav.command_long_send(
m.target_system,
m.target_component,
mavutil.mavlink.MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN,
0,
1, 0, 0, 0, 0, 0, 0)
# sets a waypoint on the drone given the latitude and longitude
# param m the connection
# param lat the latitude
# param long the longitude
def takeoff(m, lat, long, altitude):
change_mode(m, 'MISSION')
def waypoint(m, lat, long, altitude):
set_mission(m)
m.mav.command_long_send(
m.target_system,
m.target_component,
mavutil.mavlink.MAV_CMD_NAV_WAYPOINT,
0,
40, 2, 0, 'NaN', lat, long, altitude)
def change_mode(m, mode):
m.set_mode(mode)
msg = m.recv_match(type=['COMMAND_ACK'], blocking=True)
print(msg)
# while True:
# ack_msg = m.recv_match(type='COMMAND_ACK', blocking=True)
# ack_msg = ack_msg.to_dict()
# if ack_msg['command'] != mavutil.mavlink.MAVLINK_MSG_ID_SET_MODE:
# continue
# print(mavutil.mavlink.enums['MAV_RESULT']
# [ack_msg['result']].description)
# break
def set_mission(m):
m.mav.command_long_send(
m.target_system, m.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,
157, 4, 4, 0, 0, 0, 0)
def set_home(m, home_location, altitude):
print('--- ', m.target_system, ',', m.target_component)
m.mav.command_long_send(
m.target_system, m.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_HOME,
1, # set position
0, # param1
0, # param2
0, # param3
0, # param4
home_location[0], # lat
home_location[1], # lon
altitude)
def upload_mission(m, lat, longit, altitude):
home_location = (41.790328, -88.106085)
# start a UDP connection , port #: 14550: ON HOLD
# create wploader object
wp = mavwp.MAVWPLoader()
# create and add home waypoint
# homewaypointItem = mavutil.mavlink.MAVLink_mission_item_int_message(m.target_system,
# m.target_component, 0, 0 , 16, 0, 1,
# 0, 2, 0, 0, 417953585, -881664969, 222.2)
# wp.add(homewaypointItem)
# create and add takeoff mission item
# takeoffItem = mavutil.mavlink.MAVLink_mission_item_int_message(m.target_system, m.target_component, 0, 0, 22,0, 1, 0, 0, 0, 0, 417953585, -881664969, 5) # may need to reset origin if this doesn't work
# wp.add(takeoffItem)
# create and add loiter mission item (maybe do later?)
# create and add waypoint mission item
latitude = lat * 10000000
longitude = longit * 10000000
waypointItem = mavutil.mavlink.MAVLink_mission_item_int_message(
m.target_system, m.target_component, 0, 0, 16, 0, 1, 5, 2, 0, 0, int(latitude), int(longitude), altitude)
wp.add(waypointItem)
set_home(m, home_location, 220)
msg = m.recv_match(type=['COMMAND_ACK'], blocking=True)
print(msg)
print('Set home location: {0} {1}'.format(
home_location[0], home_location[1]))
time.sleep(1)
# clear all mission items from pixhawk via clear_all_send
m.waypoint_clear_all_send()
wait_heartbeat(m)
m.waypoint_count_send(wp.count())
msg = m.recv_match(type=['MISSION_ACK'], blocking=True)
print(msg)
for i in range(wp.count()):
# if not receiving a message, change to Mission_Request and then change back
msg = m.recv_match(type=['MISSION_REQUEST_INT'],
blocking=True, timeout=250)
print(msg)
m.mav.send(wp.wp(msg.seq))
msg = m.recv_match(type=['MISSION_ACK'], blocking=True)
sequenceCount = wp.count()
print(msg)
def checkCurrentMissionSequence(m):
msg = m.recv_match(type=['MISSION_ITEM_REACHED'], blocking=True)
return msg.seq
def landDrone(m):
wait_heartbeat(m)
change_mode(m, 'LAND')
def beginDelivery(m):
if type(sequenceCount) == None:
return
else:
wait_heartbeat(m)
change_mode(m, 'TAKEOFF')
time.sleep(3)
change_mode(m, 'MISSION')
currentMissionSeq = checkCurrentMissionSequence(m)
while currentMissionSeq < sequenceCount:
currentMissionSeq = checkCurrentMissionSequence(m)
landDrone(m)
sequenceCount = None # resets the sequence count after mission
def getCoords(m):
client = MongoClient(
"mongodb+srv://affinity:[email protected]/test?authSource=admin&replicaSet=atlas-4ltrda-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true")
db = client["Drones"]
collection = db["MapData"]
pprint(collection.find_one()["Update"])
if (collection.find_one()["Update"] == 1):
myquery = {}
newvalues = {"$set": {"Update": 0}}
collection.update_one(myquery, newvalues)
x = collection.find_one()["Latitude"]
y = collection.find_one()["Longitude"]
upload_mission(m, x, y, 5)
# beginDelivery(m)