From 26d82b48bee7ded2b70ce3115d2d2e8db95cd314 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Wed, 28 Oct 2015 15:10:46 -0700 Subject: [PATCH 1/2] Exports Commands. Implements Commands.__len__ --- dronekit/__init__.py | 2 ++ dronekit/lib/__init__.py | 8 ++++++++ tests/sitl/test_waypoints.py | 30 +++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index ae62a04e3..424574f3d 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -20,6 +20,8 @@ # Public re-exports Vehicle = dronekit.lib.Vehicle +Command = dronekit.lib.Command +CommandSequence = dronekit.lib.CommandSequence VehicleMode = dronekit.lib.VehicleMode LocationGlobal = dronekit.lib.LocationGlobal LocationLocal = dronekit.lib.LocationLocal diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index bb83cda93..779893823 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -1087,6 +1087,14 @@ def next(self, index): """ self.__module.master.waypoint_set_current_send(index) + def __len__(self): + ''' + Return number of waypoints. + + :return: The number of waypoints in the sequence. + ''' + return self.__module.wploader.count() + def __getitem__(self, index): return self.__module.wploader.wp(index) diff --git a/tests/sitl/test_waypoints.py b/tests/sitl/test_waypoints.py index 8e9dd788c..17d3b4328 100644 --- a/tests/sitl/test_waypoints.py +++ b/tests/sitl/test_waypoints.py @@ -1,10 +1,9 @@ import time import math -from dronekit import connect, VehicleMode, LocationGlobal +from dronekit import connect, VehicleMode, LocationGlobal, Command from dronekit.tools import with_sitl from nose.tools import assert_not_equals -# This test runs first! @with_sitl def test_parameter(connpath): # Connect to the Vehicle @@ -13,6 +12,31 @@ def test_parameter(connpath): cmds = vehicle.commands cmds.download() cmds.wait_valid() + cmds.clear() + vehicle.flush() + + print 'hi' + + # home + cmds.download() + cmds.wait_valid() - vehicle.commands.clear() + for command in [ + Command(0, 0, 0, 0, 16, 1, 1, 0.0, 0.0, 0.0, 0.0, -35.3605, 149.172363, 747.0), + Command(0, 0, 0, 3, 22, 0, 1, 0.0, 0.0, 0.0, 0.0, -35.359831, 149.166334, 100.0), + Command(0, 0, 0, 3, 16, 0, 1, 0.0, 0.0, 0.0, 0.0, -35.363489, 149.167213, 100.0), + Command(0, 0, 0, 3, 16, 0, 1, 0.0, 0.0, 0.0, 0.0, -35.355491, 149.169595, 100.0), + Command(0, 0, 0, 3, 16, 0, 1, 0.0, 0.0, 0.0, 0.0, -35.355071, 149.175839, 100.0), + Command(0, 0, 0, 3, 113, 0, 1, 0.0, 0.0, 0.0, 0.0, -35.362666, 149.178715, 22222.0), + Command(0, 0, 0, 3, 115, 0, 1, 2.0, 22.0, 1.0, 3.0, 0.0, 0.0, 0.0), + Command(0, 0, 0, 3, 16, 0, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), + ]: + cmds.add(command) vehicle.flush() + + cmds.download() + cmds.wait_valid() + for cmd in cmds[1:]: #skip first item as it is home waypoint. + print(cmd) + + print(len(cmds)) From ea8ff0dba970cc14b26ede992781dbd3cd6354c2 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Wed, 28 Oct 2015 15:36:52 -0700 Subject: [PATCH 2/2] Implements waypoint uploading. --- dronekit/__init__.py | 14 +++++++++++--- tests/sitl/test_waypoints.py | 18 +++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index 424574f3d..73a6ba128 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -70,6 +70,7 @@ def __init__(self, master, vehicle_class=Vehicle): # waypoints self.wploader = mavwp.MAVWPLoader() self.wp_loaded = True + self.wp_uploaded = None # Attaches message listeners. self.message_listeners = dict() @@ -268,7 +269,11 @@ def send_all_waypoints(self): """ self.master.waypoint_clear_all_send() if self.wploader.count() > 0: + self.wp_uploaded = [False]*self.wploader.count() self.master.waypoint_count_send(self.wploader.count()) + while False in self.wp_uploaded: + time.sleep(0.1) + self.wp_uploaded = None def fix_targets(self, message): pass @@ -511,9 +516,12 @@ def mavlink_thread(): self.last_waypoint = msg.seq # Waypoint send to master - # TODO - if msg.get_type() in ["WAYPOINT_REQUEST", "MISSION_REQUEST"]: - pass + if self.wp_uploaded != None and msg.get_type() in ["WAYPOINT_REQUEST", "MISSION_REQUEST"]: + wp = self.wploader.wp(msg.seq) + wp.target_system = self.target_system + wp.target_component = self.target_component + self.master.mav.send(wp) + self.wp_uploaded[msg.seq] = True # Heartbeat: armed + mode update if msg.get_type() == 'HEARTBEAT': diff --git a/tests/sitl/test_waypoints.py b/tests/sitl/test_waypoints.py index 17d3b4328..1a745e560 100644 --- a/tests/sitl/test_waypoints.py +++ b/tests/sitl/test_waypoints.py @@ -2,25 +2,27 @@ import math from dronekit import connect, VehicleMode, LocationGlobal, Command from dronekit.tools import with_sitl -from nose.tools import assert_not_equals +from nose.tools import assert_not_equals, assert_equals @with_sitl def test_parameter(connpath): # Connect to the Vehicle vehicle = connect(connpath, await_params=True) + # Initial cmds = vehicle.commands cmds.download() cmds.wait_valid() + assert_equals(len(cmds), 1) + + # After clearing cmds.clear() vehicle.flush() - - print 'hi' - - # home cmds.download() cmds.wait_valid() + assert_equals(len(cmds), 1) + # Upload for command in [ Command(0, 0, 0, 0, 16, 1, 1, 0.0, 0.0, 0.0, 0.0, -35.3605, 149.172363, 747.0), Command(0, 0, 0, 3, 22, 0, 1, 0.0, 0.0, 0.0, 0.0, -35.359831, 149.166334, 100.0), @@ -34,9 +36,7 @@ def test_parameter(connpath): cmds.add(command) vehicle.flush() + # After upload cmds.download() cmds.wait_valid() - for cmd in cmds[1:]: #skip first item as it is home waypoint. - print(cmd) - - print(len(cmds)) + assert_equals(len(cmds), 9)