diff --git a/dronekit/__init__.py b/dronekit/__init__.py index ae62a04e3..73a6ba128 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 @@ -68,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() @@ -266,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 @@ -509,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/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..1a745e560 100644 --- a/tests/sitl/test_waypoints.py +++ b/tests/sitl/test_waypoints.py @@ -1,18 +1,42 @@ 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 +from nose.tools import assert_not_equals, assert_equals -# This test runs first! @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) - vehicle.commands.clear() + # After clearing + cmds.clear() vehicle.flush() + 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), + 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() + + # After upload + cmds.download() + cmds.wait_valid() + assert_equals(len(cmds), 9)