diff --git a/docs/examples/simple_goto.rst b/docs/examples/simple_goto.rst index 1b72ce8b3..496df27de 100644 --- a/docs/examples/simple_goto.rst +++ b/docs/examples/simple_goto.rst @@ -70,12 +70,19 @@ On the command prompt you should see (something like): >>> APM:Copter V3.4-dev (e0810c2e) >>> Frame: QUAD Basic pre-arm checks - Waiting for GPS...: None - ... - Waiting for GPS...: None + Waiting for vehicle to initialise... + Waiting for vehicle to initialise... + Waiting for vehicle to initialise... + Waiting for vehicle to initialise... + Waiting for vehicle to initialise... Arming motors Waiting for arming... + Waiting for arming... + Waiting for arming... >>> ARMING MOTORS + >>> GROUND START + Waiting for arming... + Waiting for arming... >>> Initialising APM... Taking off! Altitude: 0.0 @@ -93,14 +100,16 @@ On the command prompt you should see (something like): .. tip:: - If you get stuck in ``Waiting for arming...`` it is very likely that the vehicle did not pass all pre-arm checks. - On a real device you can view the controller LEDs to determine possible causes. On the Simulator console you - can disable the checks if needed: + The code waits on :py:func:`Vehicle.is_armable `, so you shouldn't get stuck in ``Waiting for arming...``. + If you do, then: + + * On a real device you can view the controller LEDs to determine possible causes. + * On the Simulator console you can disable the checks if needed: - .. code-block:: bash + .. code-block:: bash - STABILIZE>param load ../Tools/autotest/copter_params.parm - STABILIZE>param set ARMING_CHECK 0 + STABILIZE>param load ../Tools/autotest/copter_params.parm + STABILIZE>param set ARMING_CHECK 0 How does it work? @@ -111,7 +120,8 @@ The code has three distinct sections: arming and takeoff, flight to two location Takeoff ------- -To launch *Copter* you need to set the mode to ``GUIDED``, arm the vehicle, and then call +To launch *Copter* you need to first check that the vehicle :py:func:`Vehicle.is_armable `. +Then set the mode to ``GUIDED``, arm the vehicle, and call :py:func:`Vehicle.commands.takeoff() `. The takeoff code in this example is explained in the guide topic :ref:`taking-off`. diff --git a/docs/examples/vehicle_state.rst b/docs/examples/vehicle_state.rst index 9ec84250d..16d3c2750 100644 --- a/docs/examples/vehicle_state.rst +++ b/docs/examples/vehicle_state.rst @@ -67,6 +67,7 @@ On the command prompt you should see (something like): Rangefinder: Rangefinder: distance=None, voltage=None Rangefinder distance: None Rangefinder voltage: None + Is Armable?: True System status: 3 Heading: 341 Mode: STABILIZE diff --git a/docs/guide/migrating.rst b/docs/guide/migrating.rst index f294357de..6c8b1c60b 100644 --- a/docs/guide/migrating.rst +++ b/docs/guide/migrating.rst @@ -226,8 +226,11 @@ New attributes In addition to the :ref:`home_location `, a few more attributes have been added, including: -:py:func:`Vehicle.system_status ` and -:py:func:`Vehicle.heading `. +:py:func:`Vehicle.system_status `, +:py:func:`Vehicle.heading `, +:py:func:`Vehicle.mount_status `, +:py:func:`Vehicle.ekf_ok `, +:py:func:`Vehicle.is_armable `. Debugging diff --git a/docs/guide/taking_off.rst b/docs/guide/taking_off.rst index 5f58072f3..242fca390 100644 --- a/docs/guide/taking_off.rst +++ b/docs/guide/taking_off.rst @@ -19,6 +19,9 @@ and then call :py:func:`Vehicle.commands.takeoff() `_ waypoint in your mission (you can run a mission by switching to ``AUTO`` mode after you're in the air). + + By contrast, Plane apps take off using the ``MAV_CMD_NAV_TAKEOFF`` command in a mission. Plane should first arm and then change to + ``AUTO`` mode to start the mission. The code below shows a function to arm a Copter, take off, and fly to a specified altitude. This is taken from :ref:`example_simple_goto`. @@ -33,12 +36,9 @@ The code below shows a function to arm a Copter, take off, and fly to a specifie """ print "Basic pre-arm checks" - # Don't let the user try to fly autopilot is booting - if vehicle.mode.name == "INITIALISING": - print "Waiting for vehicle to initialise" - time.sleep(1) - while vehicle.gps_0.fix_type < 2: - print "Waiting for GPS...:", vehicle.gps_0.fix_type + # Don't let the user try to arm until autopilot is ready + while not vehicle.is_armable: + print " Waiting for vehicle to initialise..." time.sleep(1) print "Arming motors" @@ -72,16 +72,32 @@ The function first performs some pre-arm checks. until the vehicle has passed a series of pre-arm checks to ensure that it is safe to fly. DroneKit-Python can't check every possible symptom that might prevent arming, but we can confirm that the -vehicle has booted and has a GPS lock: +vehicle has booted, EKF is ready, and it has a GPS lock. These checks are encapsulated in the +:py:func:`Vehicle.is_armable ` attribute: .. code-block:: python - if v.mode.name == "INITIALISING": - print "Waiting for vehicle to initialise" - time.sleep(1) - while vehicle.gps_0.fix_type < 2: - print "Waiting for GPS...:", vehicle.gps_0.fix_type - time.sleep(1) + print "Basic pre-arm checks" + # Don't let the user try to arm until autopilot is ready + while not vehicle.is_armable: + print " Waiting for vehicle to initialise..." + time.sleep(1) + +.. note:: + + If you need more status information you can perform the following sorts of checks: + + .. code-block:: python + + if v.mode.name == "INITIALISING": + print "Waiting for vehicle to initialise" + time.sleep(1) + while vehicle.gps_0.fix_type < 2: + print "Waiting for GPS...:", vehicle.gps_0.fix_type + time.sleep(1) + + You should always do a final check on :py:func:`Vehicle.is_armable `! + Once the vehicle is ready we set the mode to ``GUIDED`` and arm it. We then wait until arming is confirmed before sending the :py:func:`takeoff ` command. diff --git a/docs/guide/vehicle_state_and_parameters.rst b/docs/guide/vehicle_state_and_parameters.rst index 9cd91579f..61f478a79 100644 --- a/docs/guide/vehicle_state_and_parameters.rst +++ b/docs/guide/vehicle_state_and_parameters.rst @@ -33,6 +33,7 @@ Vehicle state information is exposed through vehicle *attributes*. DroneKit-Pyth :py:attr:`Vehicle.home_location `, :py:func:`Vehicle.system_status `, :py:func:`Vehicle.heading `, +:py:func:`Vehicle.is_armable `, :py:attr:`Vehicle.armed `, :py:attr:`Vehicle.mode `. @@ -72,6 +73,7 @@ regularly updated from MAVLink messages sent by the vehicle). print "Rangefinder distance: %s" % vehicle.rangefinder.distance print "Rangefinder voltage: %s" % vehicle.rangefinder.voltage print "Heading: %s" % vehicle.heading + print "Is Armable?: %s" % vehicle.is_armable print "System status: %s" % vehicle.system_status print "Mode: %s" % vehicle.mode.name # settable print "Armed: %s" % vehicle.armed # settable diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 2ebc5e3c6..c9a7e6c38 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -1055,6 +1055,19 @@ def armed(self, value): else: self._master.arducopter_disarm() + @property + def is_armable(self): + """ + Returns ``True`` if the vehicle is ready to arm, false otherwise. + + This attribute wraps a number of pre-arm checks, ensuring that the vehicle has booted, + has a good GPS fix, and that the EKF pre-arm is complete. + """ + # check that mode is not INITIALSING + # check that we have a GPS fix + # check that EKF pre-arm is complete + return self.mode != 'INITIALISING' and self.gps_0.fix_type > 1 and self._ekf_predposhorizabs + @property def system_status(self): """ diff --git a/dronekit/test/sitl/test_goto.py b/dronekit/test/sitl/test_goto.py index 0439ecf05..fb3468a78 100644 --- a/dronekit/test/sitl/test_goto.py +++ b/dronekit/test/sitl/test_goto.py @@ -27,43 +27,33 @@ def arm_and_takeoff(aTargetAltitude): Arms vehicle and fly to aTargetAltitude. """ - # print "Basic pre-arm checks" # Don't let the user try to fly autopilot is booting - if vehicle.mode.name == "INITIALISING": - # print "Waiting for vehicle to initialise" - time.sleep(1) - while vehicle.gps_0.fix_type < 2: - # print "Waiting for GPS...:", vehicle.gps_0.fix_type + i = 60 + while not vehicle.is_armable and i > 0: time.sleep(1) - - # print "Arming motors" - # Copter should arm in GUIDED mode - vehicle.mode = VehicleMode("GUIDED") + i = i - 1 + assert_equals(vehicle.is_armable, True) + # Copter should arm in GUIDED mode + vehicle.mode = VehicleMode("GUIDED") i = 60 while vehicle.mode.name != 'GUIDED' and i > 0: # print " Waiting for guided %s seconds..." % (i,) time.sleep(1) i = i - 1 + assert_equals(vehicle.mode.name, 'GUIDED') - # EKF warmup. Await that EKF's predicted horizontal position (absolute) estimate is good - while not vehicle._ekf_predposhorizabs: - time.sleep(1) - + # Arm copter. vehicle.armed = True - i = 60 while not vehicle.armed and vehicle.mode.name == 'GUIDED' and i > 0: # print " Waiting for arming %s seconds..." % (i,) time.sleep(1) i = i - 1 + assert_equals(vehicle.armed, True) - # Failure will result in arming but immediately landing - assert vehicle.armed - assert_equals(vehicle.mode.name, 'GUIDED') - - # print "Taking off!" - vehicle.commands.takeoff(aTargetAltitude) # Take off to target altitude + # Take off to target altitude + vehicle.commands.takeoff(aTargetAltitude) # Wait until the vehicle reaches a safe height before # processing the goto (otherwise the command after diff --git a/dronekit/test/sitl/test_localposition.py b/dronekit/test/sitl/test_localposition.py index 844677484..ebf433f23 100644 --- a/dronekit/test/sitl/test_localposition.py +++ b/dronekit/test/sitl/test_localposition.py @@ -18,43 +18,33 @@ def arm_and_takeoff(aTargetAltitude): Arms vehicle and fly to aTargetAltitude. """ - # print "Basic pre-arm checks" # Don't let the user try to fly autopilot is booting - if vehicle.mode.name == "INITIALISING": - # print "Waiting for vehicle to initialise" - time.sleep(1) - while vehicle.gps_0.fix_type < 2: - # print "Waiting for GPS...:", vehicle.gps_0.fix_type + i = 60 + while not vehicle.is_armable and i > 0: time.sleep(1) - - # print "Arming motors" - # Copter should arm in GUIDED mode - vehicle.mode = VehicleMode("GUIDED") + i = i - 1 + assert_equals(vehicle.is_armable, True) + # Copter should arm in GUIDED mode + vehicle.mode = VehicleMode("GUIDED") i = 60 while vehicle.mode.name != 'GUIDED' and i > 0: # print " Waiting for guided %s seconds..." % (i,) time.sleep(1) i = i - 1 + assert_equals(vehicle.mode.name, 'GUIDED') - # EKF warmup. Await that EKF's predicted horizontal position (absolute) estimate is good - while not vehicle._ekf_predposhorizabs: - time.sleep(1) - + # Arm copter. vehicle.armed = True - i = 60 while not vehicle.armed and vehicle.mode.name == 'GUIDED' and i > 0: # print " Waiting for arming %s seconds..." % (i,) time.sleep(1) i = i - 1 + assert_equals(vehicle.armed, True) - # Failure will result in arming but immediately landing - assert vehicle.armed - assert_equals(vehicle.mode.name, 'GUIDED') - - # print "Taking off!" - vehicle.commands.takeoff(aTargetAltitude) # Take off to target altitude + # Take off to target altitude + vehicle.commands.takeoff(aTargetAltitude) # Wait until the vehicle reaches a safe height before # processing the goto (otherwise the command after diff --git a/examples/simple_goto/simple_goto.py b/examples/simple_goto/simple_goto.py index c69a73ee5..a0d5c8985 100644 --- a/examples/simple_goto/simple_goto.py +++ b/examples/simple_goto/simple_goto.py @@ -32,12 +32,9 @@ def arm_and_takeoff(aTargetAltitude): """ print "Basic pre-arm checks" - # Don't let the user try to fly autopilot is booting - if vehicle.mode.name == "INITIALISING": - print "Waiting for vehicle to initialise" - time.sleep(1) - while vehicle.gps_0.fix_type < 2: - print "Waiting for GPS...:", vehicle.gps_0.fix_type + # Don't let the user try to arm until autopilot is ready + while not vehicle.is_armable: + print " Waiting for vehicle to initialise..." time.sleep(1) diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index ba479b8a9..e90fbb71e 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -47,6 +47,7 @@ print " Rangefinder distance: %s" % vehicle.rangefinder.distance print " Rangefinder voltage: %s" % vehicle.rangefinder.voltage print " Heading: %s" % vehicle.heading +print " Is Armable?: %s" % vehicle.is_armable print " System status: %s" % vehicle.system_status print " Mode: %s" % vehicle.mode.name # settable print " Armed: %s" % vehicle.armed # settable