|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +#This example shows how to use the follow me plugin |
| 4 | + |
| 5 | +import asyncio |
| 6 | +from mavsdk import System |
| 7 | +from mavsdk.follow_me import (Config, FollowMeError, TargetLocation) |
| 8 | + |
| 9 | +default_height = 8.0 #in Meters |
| 10 | +follow_distance = 2.0 #in Meters, this is the distance that the drone will remain away from Target while following it |
| 11 | +#Direction relative to the Target |
| 12 | +#Options are NONE, FRONT, FRONT_LEFT, FRONT_RIGHT, BEHIND |
| 13 | +direction = Config.FollowDirection.BEHIND |
| 14 | +responsiveness = 0.02 |
| 15 | + |
| 16 | +#This list contains fake location coordinates (These coordinates are obtained from mission.py example) |
| 17 | +fake_location = [[47.398039859999997,8.5455725400000002],[47.398036222362471,8.5450146439425509],[47.397825620791885,8.5450092830163271]] |
| 18 | + |
| 19 | +async def fly_drone(): |
| 20 | + drone = System() |
| 21 | + await drone.connect(system_address="udp://:14540") |
| 22 | + |
| 23 | + #This waits till a mavlink based drone is connected |
| 24 | + async for state in drone.core.connection_state(): |
| 25 | + if state.is_connected: |
| 26 | + print(f"-- Connected to drone with UUID: {state.uuid}") |
| 27 | + break |
| 28 | + |
| 29 | + #Checking if Global Position Estimate is ok |
| 30 | + async for global_lock in drone.telemetry.health(): |
| 31 | + if global_lock.is_global_position_ok: |
| 32 | + print("-- Global position state is good enough for flying.") |
| 33 | + break |
| 34 | + |
| 35 | + #Arming the drone |
| 36 | + print ("-- Arming") |
| 37 | + await drone.action.arm() |
| 38 | + |
| 39 | + #Follow me Mode requires some configuration to be done before starting the mode |
| 40 | + conf = Config(default_height, follow_distance, direction, responsiveness) |
| 41 | + await drone.follow_me.set_config(conf) |
| 42 | + |
| 43 | + print ("-- Taking Off") |
| 44 | + await drone.action.takeoff() |
| 45 | + await asyncio.sleep(8) |
| 46 | + print ("-- Starting Follow Me Mode") |
| 47 | + await drone.follow_me.start() |
| 48 | + await asyncio.sleep(8) |
| 49 | + |
| 50 | + #This for loop provides fake coordinates from the fake_location list for the follow me mode to work |
| 51 | + #In a simulator it won't make much sense though |
| 52 | + for latitude,longitude in fake_location: |
| 53 | + target = TargetLocation(latitude, longitude, 0, 0, 0, 0) |
| 54 | + print ("-- Following Target") |
| 55 | + await drone.follow_me.set_target_location(target) |
| 56 | + await asyncio.sleep(2) |
| 57 | + |
| 58 | + #Stopping the follow me mode |
| 59 | + print ("-- Stopping Follow Me Mode") |
| 60 | + await drone.follow_me.stop() |
| 61 | + await asyncio.sleep(5) |
| 62 | + |
| 63 | + print ("-- Landing") |
| 64 | + await drone.action.land() |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + loop = asyncio.get_event_loop() |
| 68 | + loop.run_until_complete(fly_drone()) |
0 commit comments