|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
3 | 3 | import asyncio
|
4 |
| -from mavsdk import Point, Polygon, System |
| 4 | +from mavsdk import System |
| 5 | +from mavsdk.geofence import Point, Polygon |
5 | 6 |
|
| 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 | +""" |
6 | 13 |
|
7 | 14 | async def run():
|
8 | 15 |
|
| 16 | + # Connect to the Simulation |
9 | 17 | drone = System()
|
10 | 18 | await drone.connect(system_address="udp://:14540")
|
11 | 19 |
|
| 20 | + # Wait for the drone to connect |
12 | 21 | print("Waiting for drone...")
|
13 | 22 | async for state in drone.core.connection_state():
|
14 | 23 | if state.is_connected:
|
15 | 24 | print(f"Drone discovered with UUID: {state.uuid}")
|
16 | 25 | break
|
17 | 26 |
|
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) |
24 | 41 |
|
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) |
26 | 44 |
|
| 45 | + # Upload the geofence to your vehicle |
27 | 46 | print("Uploading geofence...")
|
28 | 47 | await drone.geofence.upload_geofence([polygon])
|
29 | 48 |
|
|
0 commit comments