Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions examples/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,36 @@ async def run():
print("-- Connected to drone!")
break

print("Waiting for a camera...")
async for camera_list in drone.camera.camera_list():
if camera_list.cameras:
camera = camera_list.cameras[0]
break
print(
f"-- Found camera {camera.vendor_name} {camera.model_name} "
f"(component ID {camera.component_id})"
)

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_storage_task = asyncio.ensure_future(print_storage(drone))
running_tasks = [print_mode_task, print_storage_task]

print("Setting mode to 'PHOTO'")
try:
await drone.camera.set_mode(Mode.PHOTO)
await drone.camera.set_mode(camera.component_id, 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()
await drone.camera.take_photo(camera.component_id)
except CameraError as error:
print(f"Couldn't take photo: {error._result.result}")

# Shut down the running coroutines (here 'print_mode()' and
# 'print_status()')
# 'print_storage()')
for task in running_tasks:
task.cancel()
try:
Expand All @@ -50,13 +60,13 @@ async def run():


async def print_mode(drone):
async for mode in drone.camera.mode():
print(f"Camera mode: {mode}")
async for update in drone.camera.mode():
print(f"Camera {update.component_id} mode: {update.mode}")


async def print_status(drone):
async for status in drone.camera.status():
print(status)
async def print_storage(drone):
async for update in drone.camera.storage():
print(f"Camera {update.component_id} storage: {update.storage}")


if __name__ == "__main__":
Expand Down
39 changes: 25 additions & 14 deletions examples/camera_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ async def run():
drone = System()
await drone.connect(system_address="udpin://0.0.0.0:14540")

print("Waiting for a camera...")
async for camera_list in drone.camera.camera_list():
if camera_list.cameras:
camera = camera_list.cameras[0]
break
component_id = camera.component_id
print(f"-- Found camera {camera.vendor_name} {camera.model_name} (component ID {component_id})")

_tasks = [
asyncio.create_task(observe_current_settings(drone)),
asyncio.create_task(observe_camera_mode(drone)),
asyncio.create_task(observe_possible_setting_options(drone)),
asyncio.create_task(observe_current_settings(drone, component_id)),
asyncio.create_task(observe_camera_mode(drone, component_id)),
asyncio.create_task(observe_possible_setting_options(drone, component_id)),
]

while True:
Expand Down Expand Up @@ -55,7 +63,7 @@ async def run():
print(f"Setting camera mode to {chosen_mode}!")

try:
await drone.camera.set_mode(chosen_mode)
await drone.camera.set_mode(component_id, chosen_mode)
print(" --> Succeeded")
except CameraError as error:
print(f" --> Failed with code: {error._result.result_str}")
Expand Down Expand Up @@ -110,7 +118,7 @@ async def run():
continue

try:
await drone.camera.set_setting(setting)
await drone.camera.set_setting(component_id, setting)
print(" --> Succeeded")
except CameraError as error:
print(f" --> Failed with code: {error._result.result_str}")
Expand All @@ -119,22 +127,25 @@ async def run():
continue


async def observe_camera_mode(drone):
async def observe_camera_mode(drone, component_id):
global camera_mode
async for mode in drone.camera.mode():
camera_mode = mode
async for update in drone.camera.mode():
if update.component_id == component_id:
camera_mode = update.mode


async def observe_current_settings(drone):
async def observe_current_settings(drone, component_id):
global current_settings
async for settings in drone.camera.current_settings():
current_settings = settings
async for update in drone.camera.current_settings():
if update.component_id == component_id:
current_settings = update.current_settings


async def observe_possible_setting_options(drone):
async def observe_possible_setting_options(drone, component_id):
global possible_setting_options
async for settings in drone.camera.possible_setting_options():
possible_setting_options = settings
async for update in drone.camera.possible_setting_options():
if update.component_id == component_id:
possible_setting_options = update.setting_options


def print_current_settings():
Expand Down
10 changes: 6 additions & 4 deletions examples/gimbal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
logging.basicConfig(level=logging.INFO)


async def get_gimbals(drone, timeout=10):
async def get_gimbals(drone, wait_s=10):
gimbals_found = [] # List to store all gimbals found

async def fetch_gimbals():
Expand All @@ -24,14 +24,16 @@ async def fetch_gimbals():
f"Device ID={gimbal.gimbal_device_id}"
)
gimbals_found.append(gimbal)
return
# The subscription never ends, so stop at the first list with gimbals
if gimbals_found:
return

try:
# Apply timeout to async loop
await asyncio.wait_for(fetch_gimbals(), timeout=timeout)
return gimbals_found
await asyncio.wait_for(fetch_gimbals(), timeout=wait_s)
except asyncio.TimeoutError:
print("Timeout: No gimbals found.")
return gimbals_found


async def run():
Expand Down
2 changes: 0 additions & 2 deletions examples/px4_ev_automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ async def main():
# Wait for the tasks to complete
await asyncio.gather(*running_tasks)

await drone.close()


if __name__ == "__main__":
asyncio.run(main())
2 changes: 0 additions & 2 deletions examples/px4_ev_automation_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ async def main():
else:
print("Invalid mode: 'ev', 'gps', or 'multi-fusion'.")

await drone.telemetry.close()


if __name__ == "__main__":
asyncio.run(main())
7 changes: 6 additions & 1 deletion examples/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

from mavsdk_grpc import System
from mavsdk_grpc.shell import Device

send_tasks = set()

Expand All @@ -23,6 +24,10 @@ async def run():
asyncio.get_event_loop().add_reader(sys.stdin, got_stdin_data, drone)
print("nsh> ", end="", flush=True)

# Keep the program running indefinitely
exit_event = asyncio.Event()
await exit_event.wait()


async def observe_shell(drone):
async for output in drone.shell.receive():
Expand All @@ -34,7 +39,7 @@ def got_stdin_data(drone):


async def send(drone, command):
await drone.shell.send(command)
await drone.shell.send(command, Device.SHELL)


if __name__ == "__main__":
Expand Down
Loading