From d1919d5eda68f4e7977fae2e98483de24e8f9e45 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Fri, 6 Nov 2015 17:33:54 -0800 Subject: [PATCH 1/3] Prevents division error in battery instantiation. --- dronekit/lib/__init__.py | 2 ++ dronekit/test/sitl/test_298.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 dronekit/test/sitl/test_298.py diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 34b7f1268..a21da8c92 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -1166,6 +1166,8 @@ def location(self): @property def battery(self): + if not (self._voltage and self._current and self._level): + return None return Battery(self._voltage, self._current, self._level) @property diff --git a/dronekit/test/sitl/test_298.py b/dronekit/test/sitl/test_298.py new file mode 100644 index 000000000..5a1434a3a --- /dev/null +++ b/dronekit/test/sitl/test_298.py @@ -0,0 +1,23 @@ +import time +import sys +import os +import socket +from dronekit import connect, VehicleMode +from dronekit.test import with_sitl +from nose.tools import assert_equals + +@with_sitl +def test_battery_none(connpath): + vehicle = connect(connpath) + + # Ensure we can get (possibly unpopulated) battery object without throwing error. + b1 = vehicle.battery + + vehicle.wait_ready('battery') + + # Ensure we can get battery object without throwing error. + b2 = vehicle.battery + + assert b1 == b2 or b1 == None + + vehicle.close() From 10e76ce81be071d72d79f7554d15f5173189c8ad Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Wed, 11 Nov 2015 13:39:09 -0800 Subject: [PATCH 2/3] Fixes battery test, fixes battery bug! --- dronekit/__init__.py | 5 +++-- dronekit/lib/__init__.py | 4 ++-- .../test/sitl/{test_298.py => test_earlyattrs.py} | 13 ++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) rename dronekit/test/sitl/{test_298.py => test_earlyattrs.py} (63%) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index f2b168b81..edc20d7e2 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -177,7 +177,7 @@ def close(self): time.sleep(0.1) self.master.close() -def connect(ip, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200, heartbeat_timeout=30): +def connect(ip, _initialize=True, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200, heartbeat_timeout=30): handler = MAVHandler(mavutil.mavlink_connection(ip, baud=baud)) vehicle = vehicle_class(handler) @@ -186,7 +186,8 @@ def connect(ip, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicl def listener(self, name, m): status_printer(re.sub(r'(^|\n)', '>>> ', m.text.rstrip())) - vehicle.initialize(rate=rate, heartbeat_timeout=heartbeat_timeout) + if _initialize: + vehicle.initialize(rate=rate, heartbeat_timeout=heartbeat_timeout) if wait_ready: if wait_ready == True: diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index a21da8c92..0de03a721 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -1166,7 +1166,7 @@ def location(self): @property def battery(self): - if not (self._voltage and self._current and self._level): + if self._voltage == None or self._current == None or self._level == None: return None return Battery(self._voltage, self._current, self._level) @@ -1426,7 +1426,7 @@ def message_factory(self): """ return self._master.mav - def initialize(self, wait_ready=False, rate=None, heartbeat_timeout=30): + def initialize(self, rate=4, heartbeat_timeout=30): self._handler.start() # Start heartbeat polling. diff --git a/dronekit/test/sitl/test_298.py b/dronekit/test/sitl/test_earlyattrs.py similarity index 63% rename from dronekit/test/sitl/test_298.py rename to dronekit/test/sitl/test_earlyattrs.py index 5a1434a3a..febd08fbe 100644 --- a/dronekit/test/sitl/test_298.py +++ b/dronekit/test/sitl/test_earlyattrs.py @@ -4,20 +4,19 @@ import socket from dronekit import connect, VehicleMode from dronekit.test import with_sitl -from nose.tools import assert_equals +from nose.tools import assert_equals, assert_not_equals @with_sitl def test_battery_none(connpath): - vehicle = connect(connpath) + vehicle = connect(connpath, _initialize=False) # Ensure we can get (possibly unpopulated) battery object without throwing error. - b1 = vehicle.battery + assert_equals(vehicle.battery, None) - vehicle.wait_ready('battery') + vehicle.initialize() # Ensure we can get battery object without throwing error. - b2 = vehicle.battery - - assert b1 == b2 or b1 == None + vehicle.wait_ready('battery') + assert_not_equals(vehicle.battery, None) vehicle.close() From 041a7097b053164e39334514471c1d0986fbbb7e Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Wed, 11 Nov 2015 13:39:48 -0800 Subject: [PATCH 3/3] Removes excess print statements from test_channels. --- dronekit/test/sitl/test_channels.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dronekit/test/sitl/test_channels.py b/dronekit/test/sitl/test_channels.py index 04dc4e04f..0df00ac6a 100644 --- a/dronekit/test/sitl/test_channels.py +++ b/dronekit/test/sitl/test_channels.py @@ -65,13 +65,13 @@ def test_timeout(connpath): #test try: - print "Ch9: %s" % vehicle.channels['9'] + vehicle.channels['9'] assert False, "Can read over end of channels" except: pass try: - print "Ch0: %s" % vehicle.channels['0'] + vehicle.channels['0'] assert False, "Can read over start of channels" except: pass @@ -141,7 +141,6 @@ def test_timeout(connpath): result = {'success': False} vehicle.channels.overrides = {} def channels_callback(vehicle, name, channels): - print(channels['3']) if channels['3'] == 55: result['success'] = True vehicle.add_attribute_listener('channels', channels_callback)