Skip to content

Commit 47ed28d

Browse files
Merge pull request #409 from mavlink/add-mavshell-example
Add mavshell example
2 parents 1f70fec + fadc789 commit 47ed28d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

examples/shell.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
3+
import asyncio
4+
import sys
5+
6+
from mavsdk import System
7+
8+
9+
async def run():
10+
drone = System()
11+
await drone.connect(system_address="udp://:14540")
12+
13+
asyncio.ensure_future(observe_shell(drone))
14+
15+
print("Waiting for drone to connect...")
16+
async for state in drone.core.connection_state():
17+
if state.is_connected:
18+
print(f"Drone discovered!")
19+
break
20+
21+
asyncio.get_event_loop().add_reader(sys.stdin, got_stdin_data, drone)
22+
print("nsh> ", end='', flush=True)
23+
24+
async def observe_shell(drone):
25+
async for output in drone.shell.receive():
26+
print(f"\n{output} ", end='', flush=True)
27+
28+
def got_stdin_data(drone):
29+
asyncio.ensure_future(send(drone, sys.stdin.readline()))
30+
31+
async def send(drone, command):
32+
await drone.shell.send(command)
33+
34+
35+
if __name__ == "__main__":
36+
asyncio.ensure_future(run())
37+
38+
try:
39+
asyncio.get_event_loop().run_forever()
40+
except KeyboardInterrupt:
41+
pass

0 commit comments

Comments
 (0)