From 2f1f87203cb3fd625c561af1e9efc98a37105286 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Tue, 3 Nov 2015 13:18:46 -0800 Subject: [PATCH 1/2] Adds wait_ready to connect, and Vehicle defaults. --- dronekit/__init__.py | 11 ++++++++++- dronekit/lib/__init__.py | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index f7935af49..e21cf178e 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -171,7 +171,7 @@ def close(self): time.sleep(0.1) self.master.close() -def connect(ip, await_params=False, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200): +def connect(ip, await_params=False, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200): handler = MAVHandler(mavutil.mavlink_connection(ip, baud=baud)) vehicle = vehicle_class(handler) @@ -181,6 +181,15 @@ def listener(self, name, m): status_printer(re.sub(r'(^|\n)', '>>> ', m.text.rstrip())) vehicle.initialize(rate=rate) + + # Legacy / deprecated. if await_params: vehicle.wait_ready('parameters', 'gps_0') + + if wait_ready: + if wait_ready == True: + vehicle.wait_ready(True) + else: + vehicle.wait_ready(*wait_ready) + return vehicle diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 54bbae7d5..4bcb56cae 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -540,6 +540,9 @@ def __init__(self, handler): # By default, we presume all "commands" are loaded. self._ready_attrs = set(['commands']) + # Default parameters when calling wait_ready() or wait_ready(True). + self._default_ready_attrs = ['parameters', 'gps_0', 'armed', 'mode', 'attitude'] + @self.attribute_listener('*') def listener(_, name): self._ready_attrs.add(name) @@ -1149,6 +1152,10 @@ def wait_ready(self, *types, **kwargs): timeout = kwargs.get('timeout', 30) raise_exception = kwargs.get('raise_exception', True) + # Vehicle defaults for wait_ready(True) or wait_ready() + if types == [True] or types == []: + types = self._default_ready_attrs + if not all(isinstance(item, basestring) for item in types): raise APIException('wait_ready expects one or more string arguments.') From 52be5ca8465dafc3e17779836d3f83ca7d343660 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Tue, 3 Nov 2015 13:22:29 -0800 Subject: [PATCH 2/2] Replaces await_params with wait_ready. --- README.md | 2 +- docs/examples/create_attribute.rst | 2 +- docs/guide/auto_mode.rst | 10 +++++----- docs/guide/getting_started.rst | 4 ++-- docs/guide/migrating.rst | 4 ++-- docs/guide/taking_off.rst | 2 +- dronekit/__init__.py | 6 +----- dronekit/lib/__init__.py | 14 +++++++------- examples/channel_overrides/channel_overrides.py | 2 +- examples/create_attribute/create_attribute.py | 2 +- examples/drone_delivery/drone_delivery.py | 2 +- examples/flight_replay/flight_replay.py | 2 +- examples/follow_me/follow_me.py | 2 +- examples/gcs/microgcs.py | 2 +- .../guided_set_speed_yaw/guided_set_speed_yaw.py | 2 +- examples/mission_basic/mission_basic.py | 2 +- .../mission_import_export/mission_import_export.py | 2 +- examples/perf/perf_test.py | 2 +- examples/simple_goto/simple_goto.py | 2 +- examples/vehicle_state/vehicle_state.py | 2 +- tests/sitl/test_110.py | 2 +- tests/sitl/test_115.py | 2 +- tests/sitl/test_12.py | 2 +- tests/sitl/test_goto.py | 2 +- tests/sitl/test_localposition.py | 2 +- tests/sitl/test_simpledemo.py | 4 ++-- tests/sitl/test_waypoints.py | 4 ++-- 27 files changed, 41 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index f04ee20af..e27f6856e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ from dronekit import connect from dronekit.lib import VehicleMode # Connect to UDP endpoint. -vehicle = connect('127.0.0.1:14550', await_params=True) +vehicle = connect('127.0.0.1:14550', wait_ready=True) # Use returned Vehicle object to query device state - e.g. to get the mode: print " Mode: %s" % vehicle.mode.name ``` diff --git a/docs/examples/create_attribute.rst b/docs/examples/create_attribute.rst index 1c35d2408..86bb01e5f 100644 --- a/docs/examples/create_attribute.rst +++ b/docs/examples/create_attribute.rst @@ -124,7 +124,7 @@ All values in the new attribute should be set to ``None`` so that it is obvious .. code:: python # Connect to the Vehicle passed in as args.connect - vehicle = connect(args.connect, await_params=True) + vehicle = connect(args.connect, wait_ready=True) #Create an Vehicle.raw_imu object and set all values to None. vehicle.raw_imu=RawIMU(None,None,None,None,None,None,None,None,None,None) diff --git a/docs/guide/auto_mode.rst b/docs/guide/auto_mode.rst index 3f20ef245..ed00f0c07 100644 --- a/docs/guide/auto_mode.rst +++ b/docs/guide/auto_mode.rst @@ -71,7 +71,7 @@ use :py:func:`wait_ready() ` to block y .. code:: python # Connect to the Vehicle (in this case a simulated vehicle at 127.0.0.1:14550) - vehicle = connect('127.0.0.1:14550', await_params=True) + vehicle = connect('127.0.0.1:14550', wait_ready=True) # Download the vehicle waypoints (commands). Wait until download is complete. cmds = vehicle.commands @@ -96,7 +96,7 @@ To clear a mission you call :py:func:`clear() ` is fully populated with values from the vehicle. Vehicle *attributes* are populated in parallel but are not guaranteed to have values when ``connect()`` completes (an attribute will have value ``None`` if a corresponding MAVLink message has not been received - for example, diff --git a/docs/guide/migrating.rst b/docs/guide/migrating.rst index 14dbfd621..2a2329c7d 100644 --- a/docs/guide/migrating.rst +++ b/docs/guide/migrating.rst @@ -59,10 +59,10 @@ values as were passed to *MAVProxy* when setting up a connection in DKPY 1.x (in from dronekit import connect # Connect to the Vehicle (in this case a UDP endpoint) - vehicle = connect('127.0.0.1:14550', await_params=True) + vehicle = connect('127.0.0.1:14550', wait_ready=True) -The ``await_params=True`` parameter ensures that ``connect()`` won't return until +The ``wait_ready=True`` parameter ensures that ``connect()`` won't return until :py:attr:`Vehicle.parameters ` has been populated. This also allows *MAVLink* messages to arrive from the connected vehicle and populate other ``Vehicle`` attributes. diff --git a/docs/guide/taking_off.rst b/docs/guide/taking_off.rst index 369444125..5f58072f3 100644 --- a/docs/guide/taking_off.rst +++ b/docs/guide/taking_off.rst @@ -25,7 +25,7 @@ The code below shows a function to arm a Copter, take off, and fly to a specifie .. code-block:: python # Connect to the Vehicle (in this case a simulator running the same computer) - vehicle = connect('127.0.0.1:14550', await_params=True) + vehicle = connect('127.0.0.1:14550', wait_ready=True) def arm_and_takeoff(aTargetAltitude): """ diff --git a/dronekit/__init__.py b/dronekit/__init__.py index e21cf178e..c6822c266 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -171,7 +171,7 @@ def close(self): time.sleep(0.1) self.master.close() -def connect(ip, await_params=False, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200): +def connect(ip, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200): handler = MAVHandler(mavutil.mavlink_connection(ip, baud=baud)) vehicle = vehicle_class(handler) @@ -182,10 +182,6 @@ def listener(self, name, m): vehicle.initialize(rate=rate) - # Legacy / deprecated. - if await_params: - vehicle.wait_ready('parameters', 'gps_0') - if wait_ready: if wait_ready == True: vehicle.wait_ready(True) diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 4bcb56cae..89812e409 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -17,7 +17,7 @@ from dronekit import connect # Connect to the Vehicle using "connection string" (in this case an address on network) - vehicle = connect('127.0.0.1:14550', await_params=True) + vehicle = connect('127.0.0.1:14550', wait_ready=True) :py:class:`Vehicle ` provides access to vehicle *state* through python attributes (e.g. :py:attr:`Vehicle.mode `) @@ -39,13 +39,13 @@ .. todo:: Update this when have confirmed how to register for parameter notifications. -.. py:function:: connect(ip, await_params=False, status_printer=errprinter, vehicle_class=Vehicle, rate=4) +.. py:function:: connect(ip, wait_ready=False, status_printer=errprinter, vehicle_class=Vehicle, rate=4) Returns a :py:class:`Vehicle` object connected to the address specified by string parameter ``ip``. Connection string parameters for different targets are listed in the :ref:`getting started guide `. :param String ip: Connection string for target address - e.g. 127.0.0.1:14550. - :param Bool await_params: Wait until all :py:func:`Vehicle.parameters` have downloaded before the method returns (default is false) + :param Bool wait_ready: Wait until all :py:func:`Vehicle.parameters` have downloaded before the method returns (default is false) :param status_printer: NA :param Vehicle vehicle_class: NA :param int rate: NA @@ -1012,7 +1012,7 @@ def home_location(self): .. code-block:: python #Connect to a vehicle object (for example, on com14) - vehicle = connect('com14', await_params=True) + vehicle = connect('com14', wait_ready=True) # Download the vehicle waypoints (commands). Wait until download is complete. cmds = vehicle.commands @@ -1116,7 +1116,7 @@ def message_factory(self): """ return self._master.mav - def initialize(self, await_params=False, rate=None): + def initialize(self, wait_ready=False, rate=None): self._handler.start() # Wait for first heartbeat. @@ -1153,7 +1153,7 @@ def wait_ready(self, *types, **kwargs): raise_exception = kwargs.get('raise_exception', True) # Vehicle defaults for wait_ready(True) or wait_ready() - if types == [True] or types == []: + if list(types) == [True] or list(types) == []: types = self._default_ready_attrs if not all(isinstance(item, basestring) for item in types): @@ -1305,7 +1305,7 @@ class CommandSequence(object): :emphasize-lines: 5-10 #Connect to a vehicle object (for example, on com14) - vehicle = connect('com14', await_params=True) + vehicle = connect('com14', wait_ready=True) # Download the vehicle waypoints (commands). Wait until download is complete. cmds = vehicle.commands diff --git a/examples/channel_overrides/channel_overrides.py b/examples/channel_overrides/channel_overrides.py index 89066ce0b..57f9908ce 100644 --- a/examples/channel_overrides/channel_overrides.py +++ b/examples/channel_overrides/channel_overrides.py @@ -28,7 +28,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) #Override channels print "\nOverriding RC channels for roll and yaw" diff --git a/examples/create_attribute/create_attribute.py b/examples/create_attribute/create_attribute.py index 0bcdd10a2..b629b0d82 100644 --- a/examples/create_attribute/create_attribute.py +++ b/examples/create_attribute/create_attribute.py @@ -26,7 +26,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) diff --git a/examples/drone_delivery/drone_delivery.py b/examples/drone_delivery/drone_delivery.py index 85a1d21bd..e617e99d7 100644 --- a/examples/drone_delivery/drone_delivery.py +++ b/examples/drone_delivery/drone_delivery.py @@ -47,7 +47,7 @@ class Drone(object): def __init__(self, home_coords, server_enabled=True): # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect - self.vehicle = connect(args.connect, await_params=True) + self.vehicle = connect(args.connect, wait_ready=True) print "connected ..." self.gps_lock = False self.altitude = 30.0 diff --git a/examples/flight_replay/flight_replay.py b/examples/flight_replay/flight_replay.py index d77d2f503..6cda9042c 100644 --- a/examples/flight_replay/flight_replay.py +++ b/examples/flight_replay/flight_replay.py @@ -28,7 +28,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) diff --git a/examples/follow_me/follow_me.py b/examples/follow_me/follow_me.py index 2db35cdd7..4f50801db 100644 --- a/examples/follow_me/follow_me.py +++ b/examples/follow_me/follow_me.py @@ -27,7 +27,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) diff --git a/examples/gcs/microgcs.py b/examples/gcs/microgcs.py index 720589416..6e264d00c 100644 --- a/examples/gcs/microgcs.py +++ b/examples/gcs/microgcs.py @@ -20,7 +20,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) def setMode(mode): # Now change the vehicle into auto mode diff --git a/examples/guided_set_speed_yaw/guided_set_speed_yaw.py b/examples/guided_set_speed_yaw/guided_set_speed_yaw.py index e5458fd64..fecc8942d 100644 --- a/examples/guided_set_speed_yaw/guided_set_speed_yaw.py +++ b/examples/guided_set_speed_yaw/guided_set_speed_yaw.py @@ -23,7 +23,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) def arm_and_takeoff(aTargetAltitude): diff --git a/examples/mission_basic/mission_basic.py b/examples/mission_basic/mission_basic.py index 6ef7d5363..6e7e6ba16 100644 --- a/examples/mission_basic/mission_basic.py +++ b/examples/mission_basic/mission_basic.py @@ -21,7 +21,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) def get_location_metres(original_location, dNorth, dEast): diff --git a/examples/mission_import_export/mission_import_export.py b/examples/mission_import_export/mission_import_export.py index 82dd96108..2514f5358 100644 --- a/examples/mission_import_export/mission_import_export.py +++ b/examples/mission_import_export/mission_import_export.py @@ -23,7 +23,7 @@ #Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) def readmission(aFileName): diff --git a/examples/perf/perf_test.py b/examples/perf/perf_test.py index 773ced7a8..a4ea8efd1 100644 --- a/examples/perf/perf_test.py +++ b/examples/perf/perf_test.py @@ -110,7 +110,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) #global vehicle diff --git a/examples/simple_goto/simple_goto.py b/examples/simple_goto/simple_goto.py index 10caa358c..c69a73ee5 100644 --- a/examples/simple_goto/simple_goto.py +++ b/examples/simple_goto/simple_goto.py @@ -23,7 +23,7 @@ # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) def arm_and_takeoff(aTargetAltitude): diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index 249b33d02..de7dcb703 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -21,7 +21,7 @@ # Connect to the Vehicle print "\nConnecting to vehicle on: %s" % args.connect -vehicle = connect(args.connect, await_params=True) +vehicle = connect(args.connect, wait_ready=True) if vehicle.mode.name == "INITIALISING": print "Waiting for vehicle to initialise" diff --git a/tests/sitl/test_110.py b/tests/sitl/test_110.py index e759815ba..cf28bde23 100644 --- a/tests/sitl/test_110.py +++ b/tests/sitl/test_110.py @@ -7,7 +7,7 @@ @with_sitl def test_110(connpath): - v = connect(connpath, await_params=True) + v = connect(connpath, wait_ready=True) # NOTE these are *very inappropriate settings* # to make on a real vehicle. They are leveraged diff --git a/tests/sitl/test_115.py b/tests/sitl/test_115.py index ed5b0b1f9..7334475d6 100644 --- a/tests/sitl/test_115.py +++ b/tests/sitl/test_115.py @@ -7,7 +7,7 @@ @with_sitl def test_115(connpath): - v = connect(connpath, await_params=True) + v = connect(connpath, wait_ready=True) # Dummy callback def mavlink_callback(*args): diff --git a/tests/sitl/test_12.py b/tests/sitl/test_12.py index 71d25e5fd..a3e8b8b7f 100644 --- a/tests/sitl/test_12.py +++ b/tests/sitl/test_12.py @@ -10,7 +10,7 @@ def current_milli_time(): @with_sitl def test_timeout(connpath): - v = connect(connpath, await_params=True) + v = connect(connpath, wait_ready=True) value = v.parameters['THR_MIN'] assert_equals(type(value), float) diff --git a/tests/sitl/test_goto.py b/tests/sitl/test_goto.py index 31de61d57..89cbbff19 100644 --- a/tests/sitl/test_goto.py +++ b/tests/sitl/test_goto.py @@ -14,7 +14,7 @@ @with_sitl def test_goto(connpath): - vehicle = connect(connpath, await_params=True) + vehicle = connect(connpath, wait_ready=True) # NOTE these are *very inappropriate settings* # to make on a real vehicle. They are leveraged diff --git a/tests/sitl/test_localposition.py b/tests/sitl/test_localposition.py index e3e116a67..013ba44a1 100644 --- a/tests/sitl/test_localposition.py +++ b/tests/sitl/test_localposition.py @@ -5,7 +5,7 @@ @with_sitl def test_timeout(connpath): - vehicle = connect(connpath, await_params=True) + vehicle = connect(connpath, wait_ready=True) # NOTE these are *very inappropriate settings* # to make on a real vehicle. They are leveraged diff --git a/tests/sitl/test_simpledemo.py b/tests/sitl/test_simpledemo.py index e3abe755d..04539b12e 100644 --- a/tests/sitl/test_simpledemo.py +++ b/tests/sitl/test_simpledemo.py @@ -13,7 +13,7 @@ # This test runs first! @with_sitl def test_parameter(connpath): - v = connect(connpath, await_params=True) + v = connect(connpath, wait_ready=True) # Perform a simple parameter check assert_equals(type(v.parameters['THR_MIN']), float) @@ -21,7 +21,7 @@ def test_parameter(connpath): # This test runs second. Add as many tests as you like @with_sitl def test_mode(connpath): - v = connect(connpath, await_params=True) + v = connect(connpath, wait_ready=True) # Ensure Mode is an instance of VehicleMode assert isinstance(v.mode, VehicleMode) diff --git a/tests/sitl/test_waypoints.py b/tests/sitl/test_waypoints.py index 21818ff1b..a8d968199 100644 --- a/tests/sitl/test_waypoints.py +++ b/tests/sitl/test_waypoints.py @@ -16,7 +16,7 @@ def test_empty_clear(connpath): @with_sitl def test_set_home(connpath): - vehicle = connect(connpath, await_params=True) + vehicle = connect(connpath, wait_ready=True) # Wait for home position to be real and not 0, 0, 0 # once we request it via cmds.download() @@ -38,7 +38,7 @@ def test_set_home(connpath): @with_sitl def test_parameter(connpath): - vehicle = connect(connpath, await_params=True) + vehicle = connect(connpath, wait_ready=True) # Home should be None at first. assert_equals(vehicle.home_location, None)