-
Notifications
You must be signed in to change notification settings - Fork 219
/
camera.py
executable file
·60 lines (44 loc) · 1.54 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import asyncio
from mavsdk.camera import (CameraError, Mode)
from mavsdk import System
async def run():
drone = System()
await drone.connect(system_address="udp://:14540")
print("Waiting for drone to connect...")
async for state in drone.core.connection_state():
if state.is_connected:
print(f"-- Connected to drone!")
break
print_mode_task = asyncio.ensure_future(print_mode(drone))
print_status_task = asyncio.ensure_future(print_status(drone))
running_tasks = [print_mode_task, print_status_task]
print("Setting mode to 'PHOTO'")
try:
await drone.camera.set_mode(Mode.PHOTO)
except CameraError as error:
print(f"Setting mode failed with error code: {error._result.result}")
await asyncio.sleep(2)
print("Taking a photo")
try:
await drone.camera.take_photo()
except CameraError as error:
print(f"Couldn't take photo: {error._result.result}")
# Shut down the running coroutines (here 'print_mode()' and
# 'print_status()')
for task in running_tasks:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
await asyncio.get_event_loop().shutdown_asyncgens()
async def print_mode(drone):
async for mode in drone.camera.mode():
print(f"Camera mode: {mode}")
async def print_status(drone):
async for status in drone.camera.status():
print(status)
if __name__ == "__main__":
# Run the asyncio loop
asyncio.run(run())