diff --git a/examples/camera.py b/examples/camera.py index 9f45db79..9c29d8f0 100755 --- a/examples/camera.py +++ b/examples/camera.py @@ -20,13 +20,23 @@ 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}") @@ -34,12 +44,12 @@ async def run(): 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: @@ -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__": diff --git a/examples/camera_params.py b/examples/camera_params.py index 70ebc900..9403b5f0 100755 --- a/examples/camera_params.py +++ b/examples/camera_params.py @@ -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: @@ -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}") @@ -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}") @@ -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(): diff --git a/examples/gimbal.py b/examples/gimbal.py index ac098690..4dfdd015 100755 --- a/examples/gimbal.py +++ b/examples/gimbal.py @@ -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(): @@ -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(): diff --git a/examples/px4_ev_automation.py b/examples/px4_ev_automation.py index 421eb8da..9f2aa818 100755 --- a/examples/px4_ev_automation.py +++ b/examples/px4_ev_automation.py @@ -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()) diff --git a/examples/px4_ev_automation_keyboard.py b/examples/px4_ev_automation_keyboard.py index f9c59eb9..d17211b2 100755 --- a/examples/px4_ev_automation_keyboard.py +++ b/examples/px4_ev_automation_keyboard.py @@ -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()) diff --git a/examples/shell.py b/examples/shell.py index 76111088..eeacdc55 100755 --- a/examples/shell.py +++ b/examples/shell.py @@ -4,6 +4,7 @@ import sys from mavsdk_grpc import System +from mavsdk_grpc.shell import Device send_tasks = set() @@ -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(): @@ -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__":