forked from mavlink/MAVSDK-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeofence.py
executable file
·55 lines (39 loc) · 1.56 KB
/
geofence.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
#!/usr/bin/env python3
import asyncio
from mavsdk import System
from mavsdk.geofence import Point, Polygon
"""
This example shows how to use the geofence plugin.
Note: The behavior when your vehicle hits the geofence is NOT configured in this example.
"""
async def run():
# Connect to the Simulation
drone = System()
await drone.connect(system_address="udp://:14540")
# Wait for the drone to connect
print("Waiting for drone...")
async for state in drone.core.connection_state():
if state.is_connected:
print(f"Drone discovered with UUID: {state.uuid}")
break
# Fetch the home location coordinates, in order to set a boundary around the home location
print("Fetching home location coordinates...")
async for terrain_info in drone.telemetry.home():
latitude = terrain_info.latitude_deg
longitude = terrain_info.longitude_deg
break
await asyncio.sleep(1)
# Define your geofence boundary
p1 = Point(latitude - 0.0001, longitude - 0.0001)
p2 = Point(latitude + 0.0001, longitude - 0.0001)
p3 = Point(latitude + 0.0001, longitude + 0.0001)
p4 = Point(latitude - 0.0001, longitude + 0.0001)
# Create a polygon object using your points
polygon = Polygon([p1, p2, p3, p4], Polygon.FenceType.INCLUSION)
# Upload the geofence to your vehicle
print("Uploading geofence...")
await drone.geofence.upload_geofence([polygon])
print("Geofence uploaded!")
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run())