-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
229 lines (195 loc) · 8.99 KB
/
utils.py
File metadata and controls
229 lines (195 loc) · 8.99 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
from __future__ import print_function
from dronekit import connect, VehicleMode, LocationGlobal, LocationGlobalRelative, Command
from pymavlink import mavutil # Needed for command message definitions
import time
import math
import cv2
import numpy as np
from realPos import *
def connectCopter(connection_string):
print('Connecting to vehicle on: %s' % connection_string)
vehicle = connect(connection_string, wait_ready=True)
return vehicle
def arm_and_takeoff(vehicle,mode,aTargetAltitude):
"""
Arms vehicle and fly to aTargetAltitude.
"""
# Clear the automode missions
cmds = vehicle.commands
cmds.clear()
print("Basic pre-arm checks")
# Don't let the user try to arm until autopilot is ready
while not vehicle.is_armable:
print(" Waiting for vehicle to initialise...")
time.sleep(1)
print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode(mode)
vehicle.armed = True
while not vehicle.armed:
print(" Waiting for arming...")
time.sleep(1)
print("Taking off!")
vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude
# Wait until the vehicle reaches a safe height before processing the goto (otherwise the command
# after Vehicle.simple_takeoff will execute immediately).
while True:
print(" Altitude: ", vehicle.location.global_relative_frame.alt)
if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95: #Trigger just below target alt.
print("Reached target altitude")
break
time.sleep(1)
def get_distance_metres(aLocation1, aLocation2):
dlat = aLocation2.lat - aLocation1.lat
dlong = aLocation2.lon - aLocation1.lon
return math.sqrt((dlat*dlat) + (dlong*dlong)) * 1.113195e5
def get_location_metres(original_location, dNorth, dEast):
earth_radius = 6378137.0 #Radius of "spherical" earth
#Coordinate offsets in radians
dLat = dNorth/earth_radius
dLon = dEast/(earth_radius*math.cos(math.pi*original_location.lat/180))
#New position in decimal degrees
newlat = original_location.lat + (dLat * 180/math.pi)
newlon = original_location.lon + (dLon * 180/math.pi)
if type(original_location) is LocationGlobal:
targetlocation=LocationGlobal(newlat, newlon,original_location.alt)
elif type(original_location) is LocationGlobalRelative:
targetlocation=LocationGlobalRelative(newlat, newlon,original_location.alt)
else:
raise Exception("Invalid Location object passed")
return targetlocation
def fixPosition(vehicle, info):
if info == (0,0,0,0,0,0):
return
(sizeX,sizeY )= process(info,altitude = vehicle.location.global_relative_frame.alt)
hipotenuse = math.sqrt(sizeX**2+sizeY**2)
if hipotenuse > 0.3:
currentLocation = vehicle.location.global_relative_frame
headingAngle = vehicle.heading
(xg,yg,wg,hg,centerY,centerX) = info
objX = xg + wg//2
objY = yg + hg//2
lengthX = abs(centerX-objX)
lengthY = abs(centerY-objY)
radianOfObj = math.atan2(lengthY, lengthX)
angleOfObj = math.degrees(radianOfObj)
dNorth , dEast = 0,0
if centerX < objX and centerY > objY:
angleOfObj = 90 - angleOfObj
elif centerX < objX and centerY <= objY:
angleOfObj = 90 + angleOfObj
elif centerX >= objX and centerY < objY:
angleOfObj = 270 - angleOfObj
elif centerX > objX and centerY >= objY:
angleOfObj = 270 + angleOfObj
NorthToTargetAngle = angleOfObj + headingAngle
NorthToTargetAngle %= 360
if NorthToTargetAngle > 0 and NorthToTargetAngle <= 90:
dNorth = 1
dEast = 1
NorthToTargetAngle = 90 - NorthToTargetAngle
elif NorthToTargetAngle > 90 and NorthToTargetAngle <= 180:
dNorth = -1
dEast = 1
NorthToTargetAngle = NorthToTargetAngle - 90
elif NorthToTargetAngle > 180 and NorthToTargetAngle <= 270:
dNorth = -1
dEast = -1
NorthToTargetAngle = 270 - NorthToTargetAngle
elif NorthToTargetAngle > 270 and NorthToTargetAngle <= 360:
dNorth = 1
dEast = -1
NorthToTargetAngle = NorthToTargetAngle - 270
dNorth *= hipotenuse*math.sin(math.radians(NorthToTargetAngle))
dEast *= hipotenuse*math.cos(math.radians(NorthToTargetAngle))
targetLocation = get_location_metres(currentLocation, dNorth, dEast)
targetDistance = get_distance_metres(currentLocation, targetLocation)
vehicle.simple_goto(targetLocation)
while vehicle.mode.name=="GUIDED": #Stop action if we are no longer in guided mode.
#print "DEBUG: mode: %s" % vehicle.mode.name
remainingDistance=get_distance_metres(vehicle.location.global_relative_frame, targetLocation)
print("Distance to target: ", remainingDistance)
if remainingDistance<=targetDistance*0.05 or remainingDistance <= 0.3: #Just below target, in case of undershoot.
print("Reached target")
break;
time.sleep(2)
def get_relative_dNorth_dEast(vehicle, info,trueSize):
if info == (0,0,0,0,0,0):
return None
(sizeX,sizeY )= process(info,altitude = vehicle.location.global_relative_frame.alt)
currentLocation = vehicle.location.global_relative_frame
headingAngle = vehicle.heading
(xg,yg,wg,hg,centerY,centerX) = info
area51 = abs(calcSizePixel(centerX * 2, centerY * 2, vehicle.location.global_relative_frame.alt, trueSize))
print((wg)*(hg), "pixel2 detected.")
if (wg)*(hg) >= area51*0.95*0.95:
objX = xg + wg//2
objY = yg + hg//2
lengthX = abs(centerX-objX)
lengthY = abs(centerY-objY)
radianOfObj = math.atan2(lengthY, lengthX)
angleOfObj = math.degrees(radianOfObj)
hipotenuse = math.sqrt(sizeX**2+sizeY**2)
dNorth , dEast = 0,0
if centerX < objX and centerY > objY:
angleOfObj = 90 - angleOfObj
elif centerX < objX and centerY <= objY:
angleOfObj = 90 + angleOfObj
elif centerX >= objX and centerY < objY:
angleOfObj = 270 - angleOfObj
elif centerX > objX and centerY >= objY:
angleOfObj = 270 + angleOfObj
NorthToTargetAngle = angleOfObj + headingAngle
NorthToTargetAngle %= 360
if NorthToTargetAngle > 0 and NorthToTargetAngle <= 90:
dNorth = 1
dEast = 1
NorthToTargetAngle = 90 - NorthToTargetAngle
elif NorthToTargetAngle > 90 and NorthToTargetAngle <= 180:
dNorth = -1
dEast = 1
NorthToTargetAngle = NorthToTargetAngle - 90
elif NorthToTargetAngle > 180 and NorthToTargetAngle <= 270:
dNorth = -1
dEast = -1
NorthToTargetAngle = 270 - NorthToTargetAngle
elif NorthToTargetAngle > 270 and NorthToTargetAngle <= 360:
dNorth = 1
dEast = -1
NorthToTargetAngle = NorthToTargetAngle - 270
dNorth *= hipotenuse*math.sin(math.radians(NorthToTargetAngle))
dEast *= hipotenuse*math.cos(math.radians(NorthToTargetAngle))
targetLocation = get_location_metres(currentLocation, dNorth, dEast)
return targetLocation
else:
return None
def readmission(aFileName):
print("\nReading mission from file: %s" % aFileName)
missionlist=[]
with open(aFileName) as f:
for i, line in enumerate(f):
if i==0:
if not line.startswith('QGC WPL 110'):
raise Exception('File is not supported WP version')
else:
linearray=line.split('\t')
ln_index=int(linearray[0])
ln_currentwp=int(linearray[1])
ln_frame=int(linearray[2])
ln_command=int(linearray[3])
ln_param1=float(linearray[4])
ln_param2=float(linearray[5])
ln_param3=float(linearray[6])
ln_param4=float(linearray[7])
ln_param5=float(linearray[8])
ln_param6=float(linearray[9])
ln_param7=float(linearray[10])
ln_autocontinue=int(linearray[11].strip())
cmd = Command( 0, 0, 0, ln_frame, ln_command, ln_currentwp, ln_autocontinue, ln_param1, ln_param2, ln_param3, ln_param4, ln_param5, ln_param6, ln_param7)
print(0, 0, 0, ln_frame, ln_command, ln_currentwp, ln_autocontinue, ln_param1, ln_param2, ln_param3, ln_param4, ln_param5, ln_param6, ln_param7)
gpsData = [ln_param5, ln_param6, ln_param7]
missionlist.append(gpsData)
return missionlist
def set_ground_speed(vehicle, speed):
print("Setting ground speed to " + str(speed))
vehicle.groundspeed = speed