Skip to content

Commit cd39c9d

Browse files
committed
Updated the Geofence Example
1 parent 98a4258 commit cd39c9d

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

examples/geofence.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
#!/usr/bin/env python3
22

33
import asyncio
4-
from mavsdk import Point, Polygon, System
4+
from mavsdk import System
5+
from mavsdk.geofence import Point, Polygon
56

7+
"""
8+
This example shows how to use the geofence plugin.
9+
10+
Note: The behavior when your vehicle hits the geofence is NOT configured in this example.
11+
12+
"""
613

714
async def run():
815

16+
# Connect to the Simulation
917
drone = System()
1018
await drone.connect(system_address="udp://:14540")
1119

20+
# Wait for the drone to connect
1221
print("Waiting for drone...")
1322
async for state in drone.core.connection_state():
1423
if state.is_connected:
1524
print(f"Drone discovered with UUID: {state.uuid}")
1625
break
1726

18-
lat = 47.3977508
19-
lon = 8.5456074
20-
p1 = Point(lat - 0.0001, lon - 0.0001)
21-
p2 = Point(lat + 0.0001, lon - 0.0001)
22-
p3 = Point(lat + 0.0001, lon + 0.0001)
23-
p4 = Point(lat - 0.0001, lon + 0.0001)
27+
# Fetch the home location coordinates, in order to set a boundary around the home location
28+
print("Fetching home location coordinates...")
29+
async for terrain_info in drone.telemetry.home():
30+
latitude = terrain_info.latitude_deg
31+
longitude = terrain_info.longitude_deg
32+
break
33+
34+
await asyncio.sleep(1)
35+
36+
# Define your geofence boundary
37+
p1 = Point(latitude - 0.0001, longitude - 0.0001)
38+
p2 = Point(latitude + 0.0001, longitude - 0.0001)
39+
p3 = Point(latitude + 0.0001, longitude + 0.0001)
40+
p4 = Point(latitude - 0.0001, longitude + 0.0001)
2441

25-
polygon = Polygon([p1, p2, p3, p4], Polygon.Type.INCLUSION)
42+
# Create a polygon object using your points
43+
polygon = Polygon([p1, p2, p3, p4], Polygon.FenceType.INCLUSION)
2644

45+
# Upload the geofence to your vehicle
2746
print("Uploading geofence...")
2847
await drone.geofence.upload_geofence([polygon])
2948

0 commit comments

Comments
 (0)