From e2a5e290fedd9f61caf9177e2627cc2468979f40 Mon Sep 17 00:00:00 2001 From: FARHANG <46557204+farhangnaderi@users.noreply.github.com> Date: Fri, 10 Mar 2023 00:24:07 +0200 Subject: [PATCH 1/4] Add files via upload --- examples/upload_params.py | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/upload_params.py diff --git a/examples/upload_params.py b/examples/upload_params.py new file mode 100644 index 00000000..def41e86 --- /dev/null +++ b/examples/upload_params.py @@ -0,0 +1,75 @@ +import asyncio +import time +import argparse +from mavsdk import System +from tqdm import tqdm + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "param_file", help="Param file to be uploaded with .params format") + parser.add_argument( + "connection_type", help="The connection type, either 'udp' or 'serial' python3 upload_params.py serial /dev/tty.usbmodem01:115200 or python3 upload_params.py udp :14540") + parser.add_argument("port", help="The port for the connection") + + args = parser.parse_args() + system_address = f"{args.connection_type}://{args.port}" + param_file_name = f"{args.param_file}" + + with open(param_file_name) as file: + for line in file: + if not line.startswith("#"): + columns = line.strip().split("\t") + vehicle_id = columns[0] + component_id = columns[1] + name = columns[2] + value = columns[3] + type = columns[4] + + async def set_params(): + drone = System() + await drone.connect(system_address=system_address) + print("Connected to the Vehicle") + param_plugin = drone.param + params = await param_plugin.get_all_params() + float_params = params.float_params + int_params = params.int_params + custom_params = params.custom_params + int_param_names = [p.name for p in int_params] + float_param_names = [p.name for p in float_params] + custom_param_names = [p.name for p in custom_params] + + while True: + async for is_in_air in drone.telemetry.in_air(): + if is_in_air: + print("The Vehicle not landed!") + time.sleep(4) + else: + break + + with open(param_file_name, "r") as param_file: + print("Uploading Parameters! Please do not arm the vehicle!") + for line in tqdm(param_file, unit='lines'): + if not line.startswith("#"): + columns = line.strip().split("\t") + vehicle_id = columns[0] + component_id = columns[1] + name = columns[2] + value = columns[3] + type = columns[4] + if name in int_param_names: + await drone.param.set_param_int(name, int(value)) + elif name in float_param_names: + await drone.param.set_param_float(name, float(value)) + elif name in custom_param_names: + await drone.param.set_param_custom(name, value) + + print("Params uploaded!") + + loop = asyncio.get_event_loop() + loop.run_until_complete(set_params()) + + +if __name__ == "__main__": + main() \ No newline at end of file From 524e6cc8a911941bfd6317a2e66a9cced51896a4 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 2 Mar 2023 10:47:34 +1300 Subject: [PATCH 2/4] upload_params: add shebang, make executable Signed-off-by: Julian Oes --- examples/upload_params.py | 2 ++ 1 file changed, 2 insertions(+) mode change 100644 => 100755 examples/upload_params.py diff --git a/examples/upload_params.py b/examples/upload_params.py old mode 100644 new mode 100755 index def41e86..07d33369 --- a/examples/upload_params.py +++ b/examples/upload_params.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + import asyncio import time import argparse From dc86909d8dc155bc5ceb0fa526ee92330ac25d42 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 2 Mar 2023 10:49:28 +1300 Subject: [PATCH 3/4] upload_params: couple of taste changes I changed a few things according to how I'd do it: - Don't use time.sleep in async. - Use early return or continue to avoid nesting too deep. - Use the usual MAVSDK connection string instead of type and port. - Use new asyncio syntax (no more getting the event loop). - Avoid opening file twice. Signed-off-by: Julian Oes --- examples/upload_params.py | 94 +++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 53 deletions(-) diff --git a/examples/upload_params.py b/examples/upload_params.py index 07d33369..8360ef06 100755 --- a/examples/upload_params.py +++ b/examples/upload_params.py @@ -1,7 +1,6 @@ #!/usr/bin/env python import asyncio -import time import argparse from mavsdk import System from tqdm import tqdm @@ -10,67 +9,56 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument( - "param_file", help="Param file to be uploaded with .params format") + "connection", + help="Connection string (e.g. udp://:14540)") parser.add_argument( - "connection_type", help="The connection type, either 'udp' or 'serial' python3 upload_params.py serial /dev/tty.usbmodem01:115200 or python3 upload_params.py udp :14540") - parser.add_argument("port", help="The port for the connection") + "param_file", help="Param file to be uploaded with .params format") args = parser.parse_args() - system_address = f"{args.connection_type}://{args.port}" - param_file_name = f"{args.param_file}" - with open(param_file_name) as file: - for line in file: - if not line.startswith("#"): - columns = line.strip().split("\t") - vehicle_id = columns[0] - component_id = columns[1] - name = columns[2] - value = columns[3] - type = columns[4] + asyncio.run(set_params()) + - async def set_params(): - drone = System() - await drone.connect(system_address=system_address) - print("Connected to the Vehicle") - param_plugin = drone.param - params = await param_plugin.get_all_params() - float_params = params.float_params - int_params = params.int_params - custom_params = params.custom_params - int_param_names = [p.name for p in int_params] - float_param_names = [p.name for p in float_params] - custom_param_names = [p.name for p in custom_params] +async def set_params(): + drone = System() + await drone.connect(system_address=args.connection) + print("Connected to the Vehicle") + param_plugin = drone.param + params = await param_plugin.get_all_params() + float_params = params.float_params + int_params = params.int_params + custom_params = params.custom_params + int_param_names = [p.name for p in int_params] + float_param_names = [p.name for p in float_params] + custom_param_names = [p.name for p in custom_params] - while True: - async for is_in_air in drone.telemetry.in_air(): - if is_in_air: - print("The Vehicle not landed!") - time.sleep(4) - else: - break + while True: + async for is_in_air in drone.telemetry.in_air(): + if is_in_air: + print("Waiting until vehicle is landed...") + else: + break - with open(param_file_name, "r") as param_file: - print("Uploading Parameters! Please do not arm the vehicle!") - for line in tqdm(param_file, unit='lines'): - if not line.startswith("#"): - columns = line.strip().split("\t") - vehicle_id = columns[0] - component_id = columns[1] - name = columns[2] - value = columns[3] - type = columns[4] - if name in int_param_names: - await drone.param.set_param_int(name, int(value)) - elif name in float_param_names: - await drone.param.set_param_float(name, float(value)) - elif name in custom_param_names: - await drone.param.set_param_custom(name, value) + with open(args.param_file, "r") as param_file: + print("Uploading Parameters... Please do not arm the vehicle!") + for line in tqdm(param_file, unit='lines'): + if line.startswith("#"): + continue - print("Params uploaded!") + columns = line.strip().split("\t") + vehicle_id = columns[0] + component_id = columns[1] + name = columns[2] + value = columns[3] + type = columns[4] + if name in int_param_names: + await drone.param.set_param_int(name, int(value)) + elif name in float_param_names: + await drone.param.set_param_float(name, float(value)) + elif name in custom_param_names: + await drone.param.set_param_custom(name, value) - loop = asyncio.get_event_loop() - loop.run_until_complete(set_params()) + print("Params uploaded!") if __name__ == "__main__": From 31b12f6b0d63a8f7fcf4f10a14e266b0f0cdc0a7 Mon Sep 17 00:00:00 2001 From: FARHANG <46557204+farhangnaderi@users.noreply.github.com> Date: Fri, 10 Mar 2023 11:48:35 +0200 Subject: [PATCH 4/4] Update upload_params.py --- examples/upload_params.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/upload_params.py b/examples/upload_params.py index 8360ef06..aa8c9503 100755 --- a/examples/upload_params.py +++ b/examples/upload_params.py @@ -16,10 +16,10 @@ def main(): args = parser.parse_args() - asyncio.run(set_params()) + asyncio.run(set_params(args)) -async def set_params(): +async def set_params(args): drone = System() await drone.connect(system_address=args.connection) print("Connected to the Vehicle") @@ -32,12 +32,11 @@ async def set_params(): float_param_names = [p.name for p in float_params] custom_param_names = [p.name for p in custom_params] - while True: - async for is_in_air in drone.telemetry.in_air(): - if is_in_air: - print("Waiting until vehicle is landed...") - else: - break + async for is_in_air in drone.telemetry.in_air(): + if is_in_air: + print("Waiting until vehicle is landed...") + else: + break with open(args.param_file, "r") as param_file: print("Uploading Parameters... Please do not arm the vehicle!") @@ -62,4 +61,4 @@ async def set_params(): if __name__ == "__main__": - main() \ No newline at end of file + main()