diff --git a/examples/follow_me_example.py b/examples/follow_me_example.py new file mode 100644 index 00000000..1053c32c --- /dev/null +++ b/examples/follow_me_example.py @@ -0,0 +1,68 @@ +#! /usr/bin/env python3 + +#This example shows how to use the follow me plugin + +import asyncio +from mavsdk import System +from mavsdk.follow_me import (Config, FollowMeError, TargetLocation) + +default_height = 8.0 #in Meters +follow_distance = 2.0 #in Meters, this is the distance that the drone will remain away from Target while following it +#Direction relative to the Target +#Options are NONE, FRONT, FRONT_LEFT, FRONT_RIGHT, BEHIND +direction = Config.FollowDirection.BEHIND +responsiveness = 0.02 + +#This list contains fake location coordinates (These coordinates are obtained from mission.py example) +fake_location = [[47.398039859999997,8.5455725400000002],[47.398036222362471,8.5450146439425509],[47.397825620791885,8.5450092830163271]] + +async def fly_drone(): + drone = System() + await drone.connect(system_address="udp://:14540") + + #This waits till a mavlink based drone is connected + async for state in drone.core.connection_state(): + if state.is_connected: + print(f"-- Connected to drone with UUID: {state.uuid}") + break + + #Checking if Global Position Estimate is ok + async for global_lock in drone.telemetry.health(): + if global_lock.is_global_position_ok: + print("-- Global position state is good enough for flying.") + break + + #Arming the drone + print ("-- Arming") + await drone.action.arm() + + #Follow me Mode requires some configuration to be done before starting the mode + conf = Config(default_height, follow_distance, direction, responsiveness) + await drone.follow_me.set_config(conf) + + print ("-- Taking Off") + await drone.action.takeoff() + await asyncio.sleep(8) + print ("-- Starting Follow Me Mode") + await drone.follow_me.start() + await asyncio.sleep(8) + + #This for loop provides fake coordinates from the fake_location list for the follow me mode to work + #In a simulator it won't make much sense though + for latitude,longitude in fake_location: + target = TargetLocation(latitude, longitude, 0, 0, 0, 0) + print ("-- Following Target") + await drone.follow_me.set_target_location(target) + await asyncio.sleep(2) + + #Stopping the follow me mode + print ("-- Stopping Follow Me Mode") + await drone.follow_me.stop() + await asyncio.sleep(5) + + print ("-- Landing") + await drone.action.land() + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(fly_drone()) diff --git a/examples/goto.py b/examples/goto.py index 8a034dfc..f4f76c58 100755 --- a/examples/goto.py +++ b/examples/goto.py @@ -20,6 +20,11 @@ async def run(): print("Global position estimate ok") break + print("Fetching amsl altitude at home location....") + async for terrain_info in drone.telemetry.home(): + absolute_altitude = terrain_info.absolute_altitude_m + break + print("-- Arming") await drone.action.arm() @@ -27,8 +32,9 @@ async def run(): await drone.action.takeoff() await asyncio.sleep(1) - - await drone.action.goto_location(47.399386, 8.535245, 500, float('nan')) + flying_alt = absolute_altitude + 20.0 #To fly drone 20m above the ground plane + #goto_location() takes Absolute MSL altitude + await drone.action.goto_location(47.399386, 8.535245, flying_alt, 0) if __name__ == "__main__": loop = asyncio.get_event_loop()