diff --git a/dronekit/__init__.py b/dronekit/__init__.py index b55ea395c..8f760c08b 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,25 @@ 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 + 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 +297,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 +314,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 +373,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 +397,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..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,14 +50,14 @@ 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): '''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: @@ -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): @@ -138,7 +130,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 # @@ -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): """ 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'])] 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")