From 3b6ad13994d4f0f48f3e90bccff57b711c2e9281 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Thu, 8 Oct 2015 16:40:07 -0400 Subject: [PATCH 1/4] Supports reading and sending waypoints. --- dronekit/__init__.py | 57 ++++++++++++++++++++++++- dronekit/module/api.py | 6 +-- examples/vehicle_state/vehicle_state.py | 17 -------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index b55ea395c..d650e9eb3 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -5,7 +5,7 @@ import os import platform import re -from pymavlink import mavutil +from pymavlink import mavutil, mavwp from Queue import Empty from pkgutil import extend_path @@ -128,6 +128,26 @@ def __init__(self, master, status_printer=None): self.status_printer = status_printer + # waypoints + self.wploader = mavwp.MAVWPLoader() + self.wp_loaded = True + + def fetch(self): + """ + Fetch waypoints. + """ + self.wp_loaded = False + print('FETCHING WAYPOINTs') + self.master.waypoint_request_list_send() + + def send_all_waypoints(self): + """ + Send waypoints to master. + """ + self.master.waypoint_clear_all_send() + if self.wploader.count() > 0: + self.master.waypoint_count_send(self.wploader.count()) + def fix_targets(self, message): pass # """Set correct target IDs for our vehicle""" @@ -278,6 +298,7 @@ def mavlink_thread(): # Downtime time.sleep(0.05) + # Parameter watchdog. # Check the time duration for last "new" params exceeds watchdog. if params.start: if None not in params.mav_param_set: @@ -294,6 +315,8 @@ def mavlink_thread(): duration = repeat_duration last_new_param = time.time() + # TODO: Waypoint watching / re-requesting + # Send 1 heartbeat per second if time.time() - last_heartbeat_sent > 1: send_heartbeat(self.master) @@ -351,6 +374,7 @@ def mavlink_thread(): if not msg: break + # Parmater: receive values if msg.get_type() == 'PARAM_VALUE': # If we discover a new param count, assume we # are receiving a new param set. @@ -374,7 +398,36 @@ def mavlink_thread(): import traceback traceback.print_exc() - elif msg.get_type() == 'HEARTBEAT': + # Waypoint receive + if not self.wp_loaded: + if msg.get_type() in ['WAYPOINT_COUNT','MISSION_COUNT']: + self.wploader.clear() + self.wploader.expected_count = msg.count + self.master.waypoint_request_send(0) + if msg.get_type() in ['WAYPOINT', 'MISSION_ITEM']: + if msg.seq > self.wploader.count(): + # Unexpected waypoint + pass + elif msg.seq < self.wploader.count(): + # Waypoint duplicate + pass + else: + self.wploader.add(msg) + + if msg.seq + 1 < self.wploader.expected_count: + self.master.waypoint_request_send(msg.seq + 1) + else: + self.wp_loaded = True + if msg.get_type() in ["WAYPOINT_CURRENT", "MISSION_CURRENT"]: + self.last_waypoint = msg.seq + + # Waypoint send to master + # TODO + if msg.get_type() in ["WAYPOINT_REQUEST", "MISSION_REQUEST"]: + pass + + # Heartbeat: armed + mode update + if msg.get_type() == 'HEARTBEAT': self.status.armed = (msg.base_mode & mavutil.mavlink.MAV_MODE_FLAG_SAFETY_ARMED) != 0 self.status.flightmode = {v: k for k, v in self.master.mode_mapping().items()}[msg.custom_mode] last_heartbeat_received = time.time() diff --git a/dronekit/module/api.py b/dronekit/module/api.py index 17962aca5..0f91dd18a 100644 --- a/dronekit/module/api.py +++ b/dronekit/module/api.py @@ -60,8 +60,8 @@ def download(self): def wait_valid(self): '''Block the calling thread until waypoints have been downloaded''' # FIXME this is a super crufty spin-wait, also we should give the user the option of specifying a timeout - while (self.__wp.wp_op is not None) and not self.__module.api.exit: - time.sleep(0.200) + while not self.__module.wp_loaded: + time.sleep(0.1) def takeoff(self, alt=None): if alt is not None: @@ -138,7 +138,7 @@ def __init__(self, module): def flush(self): if self.wpts_dirty: - self.__module.module('wp').send_all_waypoints() + self.__module.send_all_waypoints() self.wpts_dirty = False # diff --git a/examples/vehicle_state/vehicle_state.py b/examples/vehicle_state/vehicle_state.py index 7f8a9cb41..128f422b8 100644 --- a/examples/vehicle_state/vehicle_state.py +++ b/examples/vehicle_state/vehicle_state.py @@ -103,23 +103,6 @@ def mavrx_debug_handler(message): print "Remove the MAVLink callback handler (stop getting messages)" vehicle.unset_mavlink_callback() - - -# Overriding an RC channel -# NOTE: CHANNEL OVERRIDES may be useful for simulating user input and when implementing certain types of joystick control. -#DO NOT use unless there is no other choice (there almost always is!) -print "\nOverriding RC channels for roll and yaw" -vehicle.channel_override = { "1" : 900, "4" : 1000 } -vehicle.flush() -print " Current overrides are:", vehicle.channel_override -print " Channel default values:", vehicle.channel_readback # All channel values before override - -# Cancel override by setting channels to 0 -print " Cancelling override" -vehicle.channel_override = { "1" : 0, "4" : 0 } -vehicle.flush() - - ## Reset variables to sensible values. print "\nReset vehicle attributes/parameters and exit" vehicle.mode = VehicleMode("STABILIZE") From 981480d3d52c79cde205650298d4bd59067071d4 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Thu, 8 Oct 2015 16:40:30 -0400 Subject: [PATCH 2/4] Removes some read-only, deprecated properties. --- dronekit/module/api.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/dronekit/module/api.py b/dronekit/module/api.py index 0f91dd18a..7e901a7ca 100644 --- a/dronekit/module/api.py +++ b/dronekit/module/api.py @@ -35,14 +35,10 @@ def __setitem__(self, name, value): def wait_valid(self): '''Block the calling thread until parameters have been downloaded''' # FIXME this is a super crufty spin-wait, also we should give the user the option of specifying a timeout - pstate = self.__param.pstate + pstate = self.__module.pstate while (pstate.mav_param_count == 0 or len(pstate.mav_param_set) != pstate.mav_param_count) and not self.__module.api.exit: time.sleep(0.200) - @property - def __param(self): - return self.__module.module('param') - class MPCommandSequence(CommandSequence): """ See CommandSequence baseclass for documentation. @@ -54,7 +50,7 @@ def __init__(self, module): def download(self): '''Download all waypoints from the vehicle''' self.wait_valid() - self.__wp.fetch() + self.__module.fetch() # BIG FIXME - wait for full wpt download before allowing any of the accessors to work def wait_valid(self): @@ -90,23 +86,19 @@ def goto(self, l): def clear(self): '''Clears the command list''' self.wait_valid() - self.__wp.wploader.clear() + self.__module.wploader.clear() self.__module.vehicle.wpts_dirty = True def add(self, cmd): '''Add a new command at the end of the command list''' self.wait_valid() self.__module.fix_targets(cmd) - self.__wp.wploader.add(cmd, comment = 'Added by DroneAPI') + self.__module.wploader.add(cmd, comment = 'Added by DroneAPI') self.__module.vehicle.wpts_dirty = True - @property - def __wp(self): - return self.__module.module('wp') - @property def count(self): - return self.__wp.wploader.count() + return self.__module.wploader.count() @property def next(self): @@ -122,10 +114,10 @@ def next(self, index): self.__module.master.waypoint_set_current_send(index) def __getitem__(self, index): - return self.__wp.wploader.wp(index) + return self.__module.wploader.wp(index) def __setitem__(self, index, value): - self.__wp.wploader.set(value, index) + self.__module.wploader.set(value, index) self.__module.vehicle.wpts_dirty = True class MPVehicle(Vehicle): @@ -236,10 +228,6 @@ def channel_override(self, newch): def channel_readback(self): return self.__module.rc_readback - @property - def __rc(self): - return self.__module.module('rc') - @property def commands(self): """ From d3a36fa6fcb96a8a1c97165c41877f2d3d706b15 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Thu, 8 Oct 2015 16:50:44 -0400 Subject: [PATCH 3/4] Removes stray print. --- dronekit/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index d650e9eb3..8f760c08b 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -137,7 +137,6 @@ def fetch(self): Fetch waypoints. """ self.wp_loaded = False - print('FETCHING WAYPOINTs') self.master.waypoint_request_list_send() def send_all_waypoints(self): From b990e13e79ac8834c8e03eb77957a046292ff753 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Thu, 8 Oct 2015 17:22:48 -0400 Subject: [PATCH 4/4] Removes synthetic clock. --- dronekit/tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dronekit/tools/__init__.py b/dronekit/tools/__init__.py index 14f3f48ab..cad05cc3e 100644 --- a/dronekit/tools/__init__.py +++ b/dronekit/tools/__init__.py @@ -3,7 +3,7 @@ from dronekit.sitl import SITL sitl = SITL('copter', '3.3-rc5') -sitl_args = ['-I0', '-S', '--model', 'quad', '--home=-35.363261,149.165230,584,353'] +sitl_args = ['-I0', '--model', 'quad', '--home=-35.363261,149.165230,584,353'] if 'SITL_SPEEDUP' in os.environ: sitl_args += ['--speedup', str(os.environ['SITL_SPEEDUP'])]