Skip to content

Commit d96b309

Browse files
Merge pull request #246 from TheSharad/master
Examples: Added FollowMe example and updated goto.py
2 parents 7c33f2f + 067c0cd commit d96b309

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

examples/follow_me_example.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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())

examples/goto.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ async def run():
2020
print("Global position estimate ok")
2121
break
2222

23+
print("Fetching amsl altitude at home location....")
24+
async for terrain_info in drone.telemetry.home():
25+
absolute_altitude = terrain_info.absolute_altitude_m
26+
break
27+
2328
print("-- Arming")
2429
await drone.action.arm()
2530

2631
print("-- Taking off")
2732
await drone.action.takeoff()
2833

2934
await asyncio.sleep(1)
30-
31-
await drone.action.goto_location(47.399386, 8.535245, 500, float('nan'))
35+
flying_alt = absolute_altitude + 20.0 #To fly drone 20m above the ground plane
36+
#goto_location() takes Absolute MSL altitude
37+
await drone.action.goto_location(47.399386, 8.535245, flying_alt, 0)
3238

3339
if __name__ == "__main__":
3440
loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)