From 5142a90f37e66ff413492e9581db96d05d03a948 Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Tue, 17 Nov 2015 18:01:13 -0800 Subject: [PATCH 1/4] Implements target_system lookup. Implements better target semantics. --- dronekit/__init__.py | 4 ++-- dronekit/lib/__init__.py | 5 +++++ dronekit/mavlink.py | 26 ++++++++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/dronekit/__init__.py b/dronekit/__init__.py index 516de230a..2d3b550e0 100644 --- a/dronekit/__init__.py +++ b/dronekit/__init__.py @@ -25,8 +25,8 @@ LocationLocal = dronekit.lib.LocationLocal CloudClient = dronekit.lib.CloudClient -def connect(ip, _initialize=True, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200, heartbeat_timeout=30, target_system=0): - handler = MAVConnection(ip, baud=baud, target_system=target_system) +def connect(ip, _initialize=True, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200, heartbeat_timeout=30, source_system=255): + handler = MAVConnection(ip, baud=baud, source_system=source_system) vehicle = vehicle_class(handler) if status_printer: diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index a0eb71f9e..1a304dd16 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -1102,6 +1102,7 @@ def listener(self, name, msg): self._heartbeat_warning = 5 self._heartbeat_error = 30 + self._heartbeat_system = None @handler.forward_loop def listener(_): @@ -1121,6 +1122,7 @@ def listener(_): @self.on_message(['HEARTBEAT']) def listener(self, name, msg): + self._heartbeat_system = msg.get_srcSystem() self._heartbeat_lastreceived = time.time() if self._heartbeat_timeout: errprinter('>>> ...link restored.') @@ -1614,6 +1616,9 @@ def initialize(self, rate=4, heartbeat_timeout=30): if not self._handler._alive: raise APIException('Timeout in initializing connection.') + # Register target_system now. + self._handler.target_system = self._heartbeat_system + # Wait until board has booted. while True: if self._flightmode not in [None, 'INITIALISING', 'MAV']: diff --git a/dronekit/mavlink.py b/dronekit/mavlink.py index 78e5b3ceb..cb63fe368 100644 --- a/dronekit/mavlink.py +++ b/dronekit/mavlink.py @@ -5,6 +5,7 @@ import os import platform import re +import copy import dronekit.lib from dronekit.lib import APIException from dronekit.util import errprinter @@ -34,8 +35,9 @@ def read(self): class MAVConnection(object): def __init__(self, ip, baud=115200, - target_system=0): - self.master = mavutil.mavlink_connection(ip, baud=baud) + target_system=0, + source_system=255): + self.master = mavutil.mavlink_connection(ip, baud=baud, source_system=source_system) # TODO get rid of "master" object as exposed, # keep it private, expose something smaller for dronekit @@ -45,6 +47,13 @@ def __init__(self, ip, srcSystem=self.master.source_system, use_native=False) + # Monkey-patch MAVLink object for fix_targets. + sendfn = self.master.mav.send + def newsendfn(mavmsg): + self.fix_targets(mavmsg) + return sendfn(mavmsg) + self.master.mav.send = newsendfn + # Targets self.target_system = target_system @@ -78,7 +87,6 @@ def mavlink_thread(): while True: try: msg = self.out_queue.get_nowait() - self.fix_targets(msg) self.master.write(msg) except socket.error as error: # If connection reset (closed), stop polling. @@ -178,12 +186,18 @@ def close(self): self.master.close() def pipe(self, target): + target.target_system = self.target_system + + # vehicle -> self -> target @self.forward_message - def callback(self, msg): - target.out_queue.put(msg.pack(self.master.mav)) + def callback(_, msg): + target.out_queue.put(msg.pack(target.master.mav)) + # target -> self -> vehicle @target.forward_message - def callback(self, msg): + def callback(_, msg): + msg = copy.copy(msg) + target.fix_targets(msg) self.out_queue.put(msg.pack(self.master.mav)) return target From 476f44ec2869f93e05a27e22e16861cf9cde1b57 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Wed, 18 Nov 2015 17:34:52 +1100 Subject: [PATCH 2/4] Tidies up connect() and related docs for heartbeat and source_system --- docs/guide/copter/guided_mode.rst | 11 +++--- dronekit/lib/__init__.py | 60 ++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/docs/guide/copter/guided_mode.rst b/docs/guide/copter/guided_mode.rst index 9940b2978..dedb5dcfa 100644 --- a/docs/guide/copter/guided_mode.rst +++ b/docs/guide/copter/guided_mode.rst @@ -199,11 +199,12 @@ message fields as arguments: # send command to vehicle vehicle.send_mavlink(msg) -There is no need to specify the ``target_system`` id in messages (just set to zero) as DroneKit will automatically -update messages with the vehicle's internal ``target_system`` (see :py:func:`connect() `) before sending. -The ``target_component`` should be set to 0 (broadcast) unless the message is to specific component. -CRC fields and sequence numbers (if defined in the message type) are automatically set by DroneKit and can also -be ignored/set to zero. +If a message includes ``target_system`` id you can set it to zero (DroneKit will automatically +update the value with the correct ID for the connected vehicle). Similarly CRC fields and sequence numbers +(if defined in the message type) can be set to zero as they are automatically updated by DroneKit. +The ``target_component`` is not updated by DroneKit, but should be set to 0 (broadcast) unless the message is +really intended for a specific component. + .. _guided_mode_how_to_send_commands_command_long: diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 1a304dd16..32cc8cf96 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -30,14 +30,25 @@ ---- -.. py:function:: connect(ip, wait_ready=False, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200) +.. py:function:: connect(ip, wait_ready=None, status_printer=errprinter, vehicle_class=Vehicle, rate=4, baud=115200, heartbeat_timeout=30, source_system=255) 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 `. + + The method is usually called with ``wait_ready=True`` to ensure that vehicle parameters and (most) attributes are + available when ``connect()`` returns. + + .. code:: python + + from dronekit import connect + + # Connect to the Vehicle using "connection string" (in this case an address on network) + vehicle = connect('127.0.0.1:14550', wait_ready=True) - :param String ip: Connection string for target address - e.g. 127.0.0.1:14550. + :param String ip: :ref:`Connection string ` for target address - e.g. 127.0.0.1:14550. + :param Bool/Array wait_ready: If ``True`` wait until all default attributes have downloaded before - the method returns (default is ``False``). + the method returns (default is ``None``). The default attributes to wait on are: :py:attr:`parameters`, :py:attr:`gps_0`, :py:attr:`armed`, :py:attr:`mode`, and :py:attr:`attitude`. @@ -45,16 +56,26 @@ For more information see :py:func:`Vehicle.wait_ready `. - :param status_printer: Method of signature ``def status_printer(txt)`` that prints STATUS_TEXT messages from the Vehicle and other diagnostic information. + :param status_printer: Method of signature ``def status_printer(txt)`` that prints + STATUS_TEXT messages from the Vehicle and other diagnostic information. By default the status information is printed to the command prompt in which the script is running. :param Vehicle vehicle_class: The class that will be instantiated by the ``connect()`` method. This can be any sub-class of ``Vehicle`` (and defaults to ``Vehicle``). - :param int rate: Data sream refresh rate. The default is 4Hz (4 updates per second). + :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 target_system: The MAVLink target_system for the connected vehicle. The default value is 0 (MAVLink broadcast id). - This setting overrides any value for the target system obtained from the vehicle, - and is used in place of any value set by the user in :py:class:`Commands` or when sending messages - (see :py:func:`Vehicle.send_mavlink() `). + :param int heartbeat_timeout: Connection timeout value in seconds (default is 30s). + If a link is not set up 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). + + .. note:: + + The returned :py:class:`Vehicle` object is acting as like a ground control station from the + perspective of the connected "real" vehicle. It will process/receive messages from the real vehicle + if they are addressed to this ``source_system`` id. Messages sent to the real vehicle are + automatically updated to use the vehicle's ``target_system`` id. + + It is *good practice* to assign a unique id for every system on the MAVLink network. + It is possible to configure the autopilot to only respond to guided-mode commands from a specified GCS ID. :returns: A connected vehicle of the type defined in ``vehicle_class`` (a superclass of :py:class:`Vehicle`). @@ -199,10 +220,10 @@ class GPSInfo(object): If there is no GPS lock the parameters are set to ``None``. - :param IntType eph: GPS horizontal dilution of position (HDOP). - :param IntType epv: GPS horizontal dilution of position (VDOP). - :param IntType fix_type: 0-1: no fix, 2: 2D fix, 3: 3D fix - :param IntType satellites_visible: Number of satellites visible. + :param Int eph: GPS horizontal dilution of position (HDOP). + :param Int epv: GPS horizontal dilution of position (VDOP). + :param Int fix_type: 0-1: no fix, 2: 2D fix, 3: 3D fix + :param Int satellites_visible: Number of satellites visible. .. todo:: FIXME: GPSInfo class - possibly normalize eph/epv? report fix type as string? """ @@ -1588,8 +1609,9 @@ def message_factory(self): msg = vehicle.message_factory.image_trigger_control_encode(True) vehicle.send_mavlink(msg) - There is no need to specify the ``target_system`` id in messages (just set to zero) as DroneKit will automatically - update messages with the vehicle's internal ``target_system`` (see :py:func:`connect() `) before sending. + Some message types include "addressing information". If present, there is no need to specify the ``target_system`` + id (just set to zero) as DroneKit will automatically update messages with the correct ID for the connected + vehicle before sending. The ``target_component`` should be set to 0 (broadcast) unless the message is to specific component. CRC fields and sequence numbers (if defined in the message type) are automatically set by DroneKit and can also be ignored/set to zero. @@ -1893,11 +1915,9 @@ class Command(mavutil.mavlink.MAVLink_mission_item_message): cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0, 0, 0, 0, 0,-34.364114, 149.166022, 30) - :param target_system: The id number of the message's target system (drone, GSC) within the MAVLink network. - This can be set to any value (nominally 0) because it will be overridden by the - vehicle's internal ``target_system`` (as set :py:func:`connect() `). By - default this is the broadcast id. - :param target_component: The id of a component the message should be routed to within the target system + :param target_system: This can be set to any value + (DroneKit changes the value to the MAVLink ID of the connected vehicle before the command is sent). + :param target_component: The component id if the message is intended for a particular component within the target system (for example, the camera). Set to zero (broadcast) in most cases. :param seq: The sequence number within the mission (the autopilot will reject messages sent out of sequence). This should be set to zero as the API will automatically set the correct value when uploading a mission. From 46857126c685c1034fd60a300013b4f716f7c450 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Wed, 18 Nov 2015 20:16:28 +1100 Subject: [PATCH 3/4] Fix the connection timeout information --- dronekit/lib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index 32cc8cf96..f2193ff3b 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -64,7 +64,7 @@ :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 link is not set up within this time, an exception will be raised. + If a heartbeat is not detected up 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). .. note:: From 67782a8c5d064569f77864f79416af11330adf22 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Wed, 18 Nov 2015 20:19:56 +1100 Subject: [PATCH 4/4] Fix the connection timeout typo --- dronekit/lib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dronekit/lib/__init__.py b/dronekit/lib/__init__.py index f2193ff3b..7a85a0df1 100644 --- a/dronekit/lib/__init__.py +++ b/dronekit/lib/__init__.py @@ -64,7 +64,7 @@ :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 up within this time an exception will be raised. + 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). .. note::