Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering incoming messages based on target_id #585

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d2d4cd1
Support filtering incoming messages based on target_id
peterbarker Feb 24, 2016
3ed9331
Add a MAVSystem abstraction in mavlink.py
peterbarker Feb 29, 2016
5a21d1c
multivehicle example
peterbarker Mar 11, 2016
0ec0e69
examples: augment channel_overrides example
peterbarker Apr 8, 2016
b11c41b
test_mavlink: remove infinite loop
peterbarker Apr 8, 2016
1bef5d0
Fix pipe; it no longer fixes targets
peterbarker Apr 9, 2016
251bd92
create_attribute: pass kwargs through for __init__
peterbarker Apr 6, 2016
b47306c
Add a temporary README.md file
peterbarker Mar 11, 2016
45eed81
examples: add avoidance example
peterbarker May 16, 2016
2aebdeb
neaten avoidance.py
peterbarker Jun 21, 2016
a824ecf
avoidance.py fix
peterbarker Jun 21, 2016
c4e95b7
avoidance: add many-flights example
peterbarker Jun 21, 2016
243be0a
avoidance: flesh out tests
peterbarker Jun 22, 2016
a1dbfed
avoidance: rename to AVD
peterbarker Jun 28, 2016
b8e836d
avoidance-many: factor
peterbarker Jun 28, 2016
059c8f6
more work on manytests
peterbarker Jul 5, 2016
017f4ed
more manytests fixes
peterbarker Jul 6, 2016
5df4e3e
Vehicle: waypoint_count_send method
peterbarker Jul 6, 2016
d2e39d9
examples: add rc override flight example
peterbarker Jul 6, 2016
3f7d0dc
avoidance: add plane tests
peterbarker Jul 13, 2016
ca23052
avoidance: make it clear where hub messages are coming from
peterbarker Aug 5, 2016
6dacb02
mavlink: check we are alive as we enter the main loop
peterbarker Aug 5, 2016
5e495dc
avoidance: add missing git clone instruction
peterbarker Aug 5, 2016
2d57a3e
avoidance: update readme with corrected instructions
peterbarker Aug 9, 2016
9b9c8a9
hack
peterbarker Aug 15, 2016
70f396a
mavlink.py: adjust for new version of pymavlink
peterbarker Oct 18, 2016
dac0703
Update instructions for new developments and new pymavlink repo
peterbarker Oct 18, 2016
f189c67
avoidance: correct docs
peterbarker Dec 16, 2016
eef45b0
multivehicle: correct docs
peterbarker Dec 16, 2016
9e014a4
avoidance readme fix (multivehicle)
guglie Nov 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
124 changes: 60 additions & 64 deletions dronekit/__init__.py
Expand Up @@ -752,7 +752,7 @@ def _send(self):
overrides = [0] * 8
for k, v in self.iteritems():
overrides[int(k) - 1] = v
self._vehicle._master.mav.rc_channels_override_send(0, 0, *overrides)
self._vehicle._master.send_rc_overrides(overrides)


class Channels(dict):
Expand Down Expand Up @@ -975,6 +975,8 @@ def global_relative_frame(self):
return LocationGlobalRelative(self._lat, self._lon, self._relative_alt)


from dronekit.mavlink import MAVSystem

class Vehicle(HasObservers):
"""
The main vehicle API.
Expand Down Expand Up @@ -1013,11 +1015,12 @@ class Vehicle(HasObservers):
to the project!
"""

def __init__(self, handler):
def __init__(self, xhandler, target_system=None, target_component=1):
super(Vehicle, self).__init__()

self._handler = handler
self._master = handler.master
self._handler = MAVSystem(xhandler, target_system=target_system, target_component=target_component)
self._master = self._handler
self._close_mav_on_disconnect = True

# Cache all updated attributes for wait_ready.
# By default, we presume all "commands" are loaded.
Expand All @@ -1033,7 +1036,7 @@ def listener(_, name, value):
# Attaches message listeners.
self._message_listeners = dict()

@handler.forward_message
@self._handler.forward_message
def listener(_, msg):
self.notify_message_listeners(msg.get_type(), msg)

Expand Down Expand Up @@ -1255,8 +1258,7 @@ def listener(self, name, msg):
def listener(self, name, msg):
if self._wp_uploaded != None:
wp = self._wploader.wp(msg.seq)
handler.fix_targets(wp)
self._master.mav.send(wp)
self._master.send_wp(wp)
self._wp_uploaded[msg.seq] = True

# TODO: Waypoint loop listeners
Expand All @@ -1275,7 +1277,7 @@ def listener(self, name, msg):
self._params_duration = start_duration
self._parameters = Parameters(self)

@handler.forward_loop
@xhandler.forward_loop
def listener(_):
# Check the time duration for last "new" params exceeds watchdog.
if not self._params_start:
Expand Down Expand Up @@ -1322,51 +1324,34 @@ def listener(self, name, msg):
import traceback
traceback.print_exc()

# Heartbeats.

self._heartbeat_started = False
self._heartbeat_lastsent = 0
self._heartbeat_lastreceived = 0
self._heartbeat_timeout = False

self._heartbeat_warning = 5
self._heartbeat_error = 30
self._heartbeat_system = None
self._heartbeat_lastreceived = 0
self._heartbeat_timeout = False

@handler.forward_loop
@xhandler.forward_loop
def listener(_):
# Send 1 heartbeat per second
if monotonic.monotonic() - self._heartbeat_lastsent > 1:
self._master.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS,
mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
self._heartbeat_lastsent = monotonic.monotonic()

# Timeouts.
if self._heartbeat_started:
if self._heartbeat_error and self._heartbeat_error > 0 and monotonic.monotonic(
) - self._heartbeat_lastreceived > self._heartbeat_error:
raise APIException('No heartbeat in %s seconds, aborting.' %
self._heartbeat_error)
elif monotonic.monotonic() - self._heartbeat_lastreceived > self._heartbeat_warning:
if self._heartbeat_timeout == False:
errprinter('>>> Link timeout, no heartbeat in last %s seconds' %
self._heartbeat_warning)
self._heartbeat_timeout = True
delta = monotonic.monotonic() - self._heartbeat_lastreceived
if self._heartbeat_error and self._heartbeat_error > 0 and delta > self._heartbeat_error:
raise APIException('No heartbeat in %s seconds, aborting.' %
self._heartbeat_error)
elif delta > self._heartbeat_warning:
if self._heartbeat_timeout == False:
errprinter('>>> Link timeout, no heartbeat in last %s seconds' %
self._heartbeat_warning)
self._heartbeat_timeout = True

@self.on_message(['HEARTBEAT'])
def listener(self, name, msg):
# ignore groundstations
if msg.type == mavutil.mavlink.MAV_TYPE_GCS:
return
self._heartbeat_system = msg.get_srcSystem()
self._heartbeat_lastreceived = monotonic.monotonic()
# print("heartbeat from (%d)" % (msg.get_srcSystem()))
if self._heartbeat_timeout:
errprinter('>>> ...link restored.')
self._heartbeat_timeout = False
self._heartbeat_timeout = False

self._last_heartbeat = None

@handler.forward_loop
@xhandler.forward_loop
def listener(_):
if self._heartbeat_lastreceived:
self._last_heartbeat = monotonic.monotonic() - self._heartbeat_lastreceived
Expand Down Expand Up @@ -2045,25 +2030,17 @@ def simple_goto(self, location, airspeed=None, groundspeed=None):
:param groundspeed: Target groundspeed in m/s (optional).
'''
if isinstance(location, LocationGlobalRelative):
frame = mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT
alt = location.alt
elif isinstance(location, LocationGlobal):
# This should be the proper code:
# frame = mavutil.mavlink.MAV_FRAME_GLOBAL
# However, APM discards information about the relative frame
# and treats any alt value as relative. So we compensate here.
frame = mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT
# currently the vehicle's goto only handles relative positions
if not self.home_location:
self.commands.download()
self.commands.wait_ready()
alt = location.alt - self.home_location.alt
else:
raise APIException('Expecting location to be LocationGlobal or LocationGlobalRelative.')

self._master.mav.mission_item_send(0, 0, 0, frame,
mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 2, 0, 0,
0, 0, 0, location.lat, location.lon,
alt)
self._handler.simple_goto(location.lat, location.lon, alt, relative=True)

if airspeed != None:
self.airspeed = airspeed
Expand Down Expand Up @@ -2139,15 +2116,13 @@ def initialize(self, rate=4, heartbeat_timeout=30):

# Poll for first heartbeat.
# If heartbeat times out, this will interrupt.
while self._handler._alive:
while self._handler.alive():
time.sleep(.1)
if self._heartbeat_lastreceived != start:
break
if not self._handler._alive:
raise APIException('Timeout in initializing connection.')

# Register target_system now.
self._handler.target_system = self._heartbeat_system
if not self._handler.alive():
raise APIException('Timeout in initializing connection.')

# Wait until board has booted.
while True:
Expand All @@ -2157,8 +2132,7 @@ def initialize(self, rate=4, heartbeat_timeout=30):

# Initialize data stream.
if rate != None:
self._master.mav.request_data_stream_send(0, 0, mavutil.mavlink.MAV_DATA_STREAM_ALL,
rate, 1)
self._master.request_data_stream(rate)

self.add_message_listener('HEARTBEAT', self.send_capabilties_request)

Expand Down Expand Up @@ -2237,6 +2211,18 @@ def wait_ready(self, *types, **kwargs):

return True

def waypoint_count_send(self,count):
self._master.waypoint_count_send(count)

def set_close_mav_on_disconnect(self, value):
self._close_mav_on_disconnect = value

def close_mav_on_disconnect(self):
return self._close_mav_on_disconnect

def disconnect(self):
if self.close_mav_on_disconnect():
self._handler.conn.close()

class Gimbal(object):
"""
Expand Down Expand Up @@ -2747,7 +2733,7 @@ def upload(self):
self._vehicle._master.waypoint_clear_all_send()
if self._vehicle._wploader.count() > 0:
self._vehicle._wp_uploaded = [False] * self._vehicle._wploader.count()
self._vehicle._master.waypoint_count_send(self._vehicle._wploader.count())
self._vehicle.waypoint_count_send(self._vehicle._wploader.count())
while False in self._vehicle._wp_uploaded:
time.sleep(0.1)
self._vehicle._wp_uploaded = None
Expand Down Expand Up @@ -2812,7 +2798,10 @@ def connect(ip,
baud=115200,
heartbeat_timeout=30,
source_system=255,
use_native=False):
target_system=1,
target_component=1,
use_native=False,
handler=None):
"""
Returns a :py:class:`Vehicle` object connected to the address specified by string parameter ``ip``.
Connection string parameters (``ip``) for different targets are listed in the :ref:`getting started guide <get_started_connecting>`.
Expand Down Expand Up @@ -2845,9 +2834,10 @@ def connect(ip,
This can be any sub-class of ``Vehicle`` (and defaults to ``Vehicle``).
:param int rate: Data stream refresh rate. The default is 4Hz (4 updates per second).
:param int baud: The baud rate for the connection. The default is 115200.
:param int heartbeat_timeout: Connection timeout value in seconds (default is 30s).
If a heartbeat is not detected within this time an exception will be raised.
:param int source_system: The MAVLink ID of the :py:class:`Vehicle` object returned by this method (by default 255).
:param int heartbeat_timeout: Connection timeout value in seconds (default is 30s).
If a heartbeat is not detected within this time an exception will be raised.
:param int source_system: The MAVLink ID of the :py:class:`Vehicle` object returned by this method (by default 255). When commands are sent to the vehicle, this is used as the source ID.
:param int target_system: The MAVLink ID of the vehicle this Vehicle object represents. When commands are sent to the vehicle, this SHOULD be used as the target id. Incoming messages from the connection are filtered according to this value, with "0" indicating no filtering and None indicating total filtering. Broadcast messages (those with a source system of 0) are only filtered if target_id is None
:param bool use_native: Use precompiled MAVLink parser.

.. note::
Expand All @@ -2867,8 +2857,14 @@ def connect(ip,
if not vehicle_class:
vehicle_class = Vehicle

handler = MAVConnection(ip, baud=baud, source_system=source_system, use_native=use_native)
vehicle = vehicle_class(handler)
if isinstance(ip,mavlink.MAVConnection):
handler = ip
else:
handler = MAVConnection(ip, baud=baud, source_system=source_system, use_native=use_native)

vehicle = vehicle_class(handler, target_system=target_system, target_component=target_component)

vehicle.set_close_mav_on_disconnect(not isinstance(ip,mavlink.MAVConnection))

if status_printer:

Expand Down