From 3dca3ff86a945f77909b6b2461633152e7bbb31a Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Mon, 9 Nov 2015 16:32:32 -0800 Subject: [PATCH 1/2] Implements SystemStatus property. --- dronekit/__init__.py | 1 + dronekit/lib/__init__.py | 55 ++++++++++++++++++++++++-------- dronekit/test/sitl/test_state.py | 14 ++++++++ 3 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 dronekit/test/sitl/test_state.py diff --git a/dronekit/__init__.py b/dronekit/__init__.py index 14710f283..0cb6de4cb 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -22,6 +22,7 @@ Command = dronekit.lib.Command CommandSequence = dronekit.lib.CommandSequence VehicleMode = dronekit.lib.VehicleMode +SystemStatus = dronekit.lib.SystemStatus LocationGlobal = dronekit.lib.LocationGlobal LocationLocal = dronekit.lib.LocationLocal CloudClient = dronekit.lib.CloudClient diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index c9a7e6c38..df0c81327 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -266,6 +266,26 @@ def __eq__(self, other): def __ne__(self, other): return self.name != other +class SystemStatus(object): + """ + This object is used to get and set the current "system status". + + .. py:attribute:: state + + The system state, as a ``string``. + """ + def __init__(self, state): + self.state = state + + def __str__(self): + return "SystemStatus:%s" % self.state + + def __eq__(self, other): + return self.state == other + + def __ne__(self, other): + return self.state != other + class HasObservers(object): def __init__(self): # A mapping from attr_name to a list of observers @@ -744,8 +764,9 @@ def listener(self, name, m): self._armed = (m.base_mode & mavutil.mavlink.MAV_MODE_FLAG_SAFETY_ARMED) != 0 self.notify_attribute_listeners('armed', self.armed) self._flightmode = {v: k for k, v in self._master.mode_mapping().items()}[m.custom_mode] - self._system_status = m.system_status self.notify_attribute_listeners('mode', self.mode) + self._system_status = m.system_status + self.notify_attribute_listeners('system_status', self.system_status) # Waypoints. @@ -1071,22 +1092,28 @@ def is_armable(self): @property def system_status(self): """ - System status flag according to the MAVLink - `MAV_STATE `_ enum. - - States include: + System status. Is a ``SystemStatus`` object with a ``state`` property of: - * ``MAV_STATE_UNINIT`` (0): Uninitialized system, state is unknown. - * ``MAV_STATE_BOOT`` (1): System is booting up. - * ``MAV_STATE_CALIBRATING`` (2): System is calibrating and not flight-ready. - * ``MAV_STATE_STANDBY`` (3): System is grounded and on standby. It can be launched any time. - * ``MAV_STATE_ACTIVE`` (4): System is active and might be already airborne. Motors are engaged. - * ``MAV_STATE_CRITICAL`` (5): System is in a non-normal flight mode. It can however still navigate. - * ``MAV_STATE_EMERGENCY`` (6): System is in a non-normal flight mode. It lost control over parts + * ``UNINIT``: Uninitialized system, state is unknown. + * ``BOOT``: System is booting up. + * ``CALIBRATING``: System is calibrating and not flight-ready. + * ``STANDBY``: System is grounded and on standby. It can be launched any time. + * ``ACTIVE``: System is active and might be already airborne. Motors are engaged. + * ``CRITICAL``: System is in a non-normal flight mode. It can however still navigate. + * ``EMERGENCY``: System is in a non-normal flight mode. It lost control over parts or over the whole airframe. It is in mayday and going down. - * ``MAV_STATE_POWEROFF`` (7): System just initialized its power-down sequence, will shut down now. + * ``POWEROFF``: System just initialized its power-down sequence, will shut down now. """ - return self._system_status + return { + 0: SystemStatus('UNINIT'), + 1: SystemStatus('BOOT'), + 2: SystemStatus('CALIBRATING'), + 3: SystemStatus('STANDBY'), + 4: SystemStatus('ACTIVE'), + 5: SystemStatus('CRITICAL'), + 6: SystemStatus('EMERGENCY'), + 7: SystemStatus('POWEROFF'), + }.get(self._system_status, None) @property def heading(self): diff --git a/dronekit/test/sitl/test_state.py b/dronekit/test/sitl/test_state.py new file mode 100644 index 000000000..e14f8ea16 --- /dev/null +++ b/dronekit/test/sitl/test_state.py @@ -0,0 +1,14 @@ +import time +import sys +import os +import socket +from dronekit import connect, VehicleMode, SystemStatus +from dronekit.test import with_sitl +from nose.tools import assert_equals + +@with_sitl +def test_state(connpath): + vehicle = connect(connpath, wait_ready=['system_status']) + + assert_equals(type(vehicle.system_status), SystemStatus) + assert_equals(type(vehicle.system_status.state), str) From 7c4cccd65bb7bdedcc4a0968f551c00662ecd234 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Tue, 10 Nov 2015 21:42:55 +1100 Subject: [PATCH 2/2] Update example to print vehicle.system_status.state --- docs/examples/vehicle_state.rst | 2 +- docs/guide/vehicle_state_and_parameters.rst | 2 +- examples/vehicle_state/vehicle_state.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/vehicle_state.rst b/docs/examples/vehicle_state.rst index 16d3c2750..20afac94e 100644 --- a/docs/examples/vehicle_state.rst +++ b/docs/examples/vehicle_state.rst @@ -68,7 +68,7 @@ On the command prompt you should see (something like): Rangefinder distance: None Rangefinder voltage: None Is Armable?: True - System status: 3 + System status: STANDBY Heading: 341 Mode: STABILIZE Armed: False diff --git a/docs/guide/vehicle_state_and_parameters.rst b/docs/guide/vehicle_state_and_parameters.rst index 61f478a79..8e9bad3e8 100644 --- a/docs/guide/vehicle_state_and_parameters.rst +++ b/docs/guide/vehicle_state_and_parameters.rst @@ -74,7 +74,7 @@ regularly updated from MAVLink messages sent by the vehicle). 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 "System status: %s" % vehicle.system_status.state print "Mode: %s" % vehicle.mode.name # settable print "Armed: %s" % vehicle.armed # settable diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index e90fbb71e..58329df50 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -48,7 +48,7 @@ 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 " System status: %s" % vehicle.system_status.state print " Mode: %s" % vehicle.mode.name # settable print " Armed: %s" % vehicle.armed # settable