forked from NGCPSlo/Code2022-23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drone_client.py
255 lines (217 loc) · 10 KB
/
drone_client.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
import math
import time
import collections
try:
from collections import abc
collections.MutableMapping = abc.MutableMapping
except:
pass
from dronekit import connect, VehicleMode, LocationGlobal, LocationGlobalRelative, Command, mavutil
class DroneClient:
def __init__(self, connectionStr: str = "/dev/ttyTHS0", baud: int = None) -> None:
self.connectionStr = connectionStr
self.baud = baud
self.vehicle = None
def connect(self):
'''
Connects to vehicle and blocks until it is ready. Waits until vehicle is ready.
'''
print("Connecting to vehicle on: %s" % (self.connectionStr,))
while True:
try:
self.vehicle = connect(self.connectionStr, wait_ready=True, baud=self.baud, timeout = 120, heartbeat_timeout=120)
break
except:
pass
def armVehicle(self):
"""
Ensures vehicle is ready to arm and arms it. Blocks until vehicle is armed.
"""
if self.vehicle == None:
self.connect()
# Sets the drone mode to guided, blocks until complete
print("Arming the vehicle")
self.vehicle.mode = VehicleMode("GUIDED")
while self.vehicle.mode.name != "GUIDED":
time.sleep(1)
# Set vehicle to armed and blocks until it is
self.vehicle.armed = True
while not self.vehicle.armed:
print(" Waiting for arming...")
time.sleep(1)
def takeoff(self, heightInM):
print("Taking off the vehicle")
self.vehicle.mode = VehicleMode("GUIDED")
self.vehicle.simple_takeoff(alt=heightInM)
# Delay until takeoff height is reached
while self.vehicle.mode.name == "GUIDED":
print(" Altitude: ", self.vehicle.location.global_relative_frame.alt)
# Within 5% of target altitude or within 1m
if self.vehicle.location.global_relative_frame.alt >= heightInM*0.95 or\
abs(self.vehicle.location.global_relative_frame.alt-heightInM) < 1:
print("Reached target altitude")
break
time.sleep(1)
# * get_location_metres - Get LocationGlobal (decimal degrees) at distance (m) North & East of a given LocationGlobal.
def get_location_metres(self, original_location, dNorth, dEast):
"""
Returns a LocationGlobal object containing the latitude/longitude `dNorth` and `dEast` metres from the
specified `original_location`. The returned LocationGlobal has the same `alt` value
as `original_location`.
The function is useful when you want to move the vehicle around specifying locations relative to
the current vehicle position.
The algorithm is relatively accurate over small distances (10m within 1km) except close to the poles.
For more information see:
http://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters
"""
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
# * get_distance_metres - Get the distance between two LocationGlobal objects in metres
def get_distance_metres(self, aLocation1, aLocation2):
"""
Returns the ground distance in metres between two LocationGlobal objects.
This method is an approximation, and will not be accurate over large distances and close to the
earth's poles. It comes from the ArduPilot test code:
https://github.com/diydrones/ardupilot/blob/master/Tools/autotest/common.py
"""
dlat = aLocation2.lat - aLocation1.lat
dlong = aLocation2.lon - aLocation1.lon
return math.sqrt((dlat*dlat) + (dlong*dlong)) * 1.113195e5
# * get_bearing - Get the bearing in degrees to a LocationGlobal
def get_bearing(self, aLocation1, aLocation2):
"""
Returns the bearing between the two LocationGlobal objects passed as parameters.
This method is an approximation, and may not be accurate over large distances and close to the
earth's poles. It comes from the ArduPilot test code:
https://github.com/diydrones/ardupilot/blob/master/Tools/autotest/common.py
"""
off_x = aLocation2.lon - aLocation1.lon
off_y = aLocation2.lat - aLocation1.lat
bearing = 90.00 + math.atan2(-off_y, off_x) * 57.2957795
if bearing < 0:
bearing += 360.00
return bearing
def getCoords(self):
return self.vehicle.location.global_relative_frame
def flyToMeters(self, dNorth, dEast):
"""
Takes in Cartesian coordinates in meters as a target to fly to.
Then flies the vehicle to the target location, stopping once the target has been reached.
Function blocks until target is reached.
"""
currentLocation = self.vehicle.location.global_relative_frame
targetLocation = self.get_location_metres(
currentLocation, dNorth, dEast)
targetDistance = self.get_distance_metres(
currentLocation, targetLocation)
print(targetLocation)
self.vehicle.mode = VehicleMode("GUIDED")
self.vehicle.simple_goto(targetLocation)
# Stop action if we are no longer in guided mode.
while self.vehicle.mode.name == "GUIDED":
remainingDistance = self.get_distance_metres(
self.vehicle.location.global_frame, targetLocation)
print("Distance to target: ", remainingDistance)
# Just below target, in case of undershoot.
if remainingDistance <= max(targetDistance*0.01, 1):
print("Reached target")
break
time.sleep(1)
def printVehicleState(self):
# Prints some vehicle attributes (state)
vehicle = self.vehicle
print("Get some vehicle attribute values:")
print(" GPS: %s" % vehicle.gps_0)
print(" Battery: %s" % vehicle.battery)
print(" Last Heartbeat: %s" % vehicle.last_heartbeat)
print(" Is Armable?: %s" % vehicle.is_armable)
print(" System status: %s" % vehicle.system_status.state)
print(" Mode: %s" % vehicle.mode.name) # settable
print(" Location: %s" % vehicle.location.global_frame)
def goHome(self):
self.vehicle.mode("RTL")
def flyToCords(self, lat: float, lon: float, alt:float = None, groundspeed = None, airspeed = None):
'''
Sets vehicle target destination to a set of coordinates and blocks until destination reached
'''
self.vehicle.mode = VehicleMode("GUIDED")
startLocation = self.vehicle.location.global_relative_frame
if alt == None:
alt = startLocation.alt
targetLocation = LocationGlobalRelative(lat = float(lat), lon =float(lon), alt = float(alt))
print(targetLocation)
self.vehicle.mode = VehicleMode("GUIDED")
self.vehicle.simple_goto(targetLocation, airspeed, groundspeed)
targetDistance = self.get_distance_metres(
startLocation, targetLocation)
# Stop action if we are no longer in guided mode.
while self.vehicle.mode.name == "GUIDED":
remainingDistance = self.get_distance_metres(
self.vehicle.location.global_frame, targetLocation)
print("Distance to target: ", remainingDistance)
# Block until 1% of target distance is reached or within 1m
if remainingDistance <= max(targetDistance*0.01, 1):
print("Reached target")
break
time.sleep(1)
def setGeoFence(self, coordsList: list[tuple[int, int]]):
# MAV_CMD_DO_FENCE_ENABLE
# MAV_CMD_NAV_FENCE_POLYGON_VERTEX_INCLUSION
print("Setting geofence")
self.vehicle.parameters["FENCE_ENABLE"] = 0 # disable
self.vehicle.parameters["FENCE_TYPE"] = 2 # polygon
self.vehicle.parameters["FENCE_ACTION"] = 0 # fence action report
self.vehicle.parameters["FENCE_TOTAL"] = len(coordsList) # enable
for index, coord in enumerate(coordsList):
lat = coord[0]
lng = coord[1]
print(lat,lng)
msg = self.vehicle.message_factory.command_long_encode(
0,
0,
mavutil.mavlink.MAV_CMD_NAV_FENCE_POLYGON_VERTEX_INCLUSION, 0,
len(coordsList),
1,
0,
0,
lat,
lng,
0)
print("Uploading geofence vertex")
self.vehicle.send_mavlink(msg)
self.vehicle.parameters["FENCE_ENABLE"] = 1 # enable
print("Complete")
# Example usage w/ simulation
if __name__ == '__main__':
# testGeoFence = [
# (30, -47),
# (30, -48),
# (30, -47.5)
# ]
drone = DroneClient(connectionStr="udp:localhost:14551")
drone.connect()
# drone.setGeoFence(testGeoFence)
drone.armVehicle()
drone.takeoff(20)
fence_points = int(drone.vehicle.parameters["FENCE_TOTAL"])
print(fence_points)
for i in range(fence_points):
msg = drone.vehicle.message_factory.fence_fetch_point_encode(
0, 0, # Target System, Target component
i, # idx
)
drone.vehicle.send_mavlink(msg)