Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions docs/guide/copter/guided_mode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ The command can be interrupted by a later movement command. When moving the vehi

.. tip::

You can also control the velocity using the `SET_POSITION_TARGET_GLOBAL_INT <https://pixhawk.ethz.ch/mavlink/#SET_POSITION_TARGET_GLOBAL_INT>`_ MAVLink command in almost exactly the same way (there is no real benefit in sending one command over the other). For more information on this option see :ref:`example_guided_mode_send_global_velocity` in the example code.
You can also control the velocity using the
`SET_POSITION_TARGET_GLOBAL_INT <https://pixhawk.ethz.ch/mavlink/#SET_POSITION_TARGET_GLOBAL_INT>`_
MAVLink command in almost exactly the same way (there is no real benefit in sending one command over the other).
For more information on this option see :ref:`example_guided_mode_send_global_velocity` in the example code.



Expand Down Expand Up @@ -166,15 +169,27 @@ This section explains how to send MAVLink commands, what commands can be sent, a
Sending messages/commands
-------------------------

MAVLink commands are sent by first using :py:func:`message_factory <dronekit.lib.Vehicle.message_factory>` to encode the message and then calling :py:func:`send_mavlink <dronekit.lib.Vehicle.send_mavlink>` to send them.
MAVLink commands are sent by first using :py:func:`message_factory() <dronekit.lib.Vehicle.message_factory>`
to encode the message and then calling :py:func:`send_mavlink() <dronekit.lib.Vehicle.send_mavlink>` to send them.

``message_factory()`` uses a factory method for the encoding. The name of this method will always be the lower case version of the message/command name with ``_encode`` appended. For example, to encode a `SET_POSITION_TARGET_LOCAL_NED <https://pixhawk.ethz.ch/mavlink/#SET_POSITION_TARGET_LOCAL_NED>`_ message we call ``message_factory.set_position_target_local_ned_encode()`` with values for all the message fields as arguments:
.. note::

Vehicles support a subset of the messages defined in the MAVLink standard. For more information
about the supported sets see wiki topics:
`Copter Commands in Guided Mode <http://dev.ardupilot.com/wiki/copter-commands-in-guided-mode/>`_
and `Plane Commands in Guided Mode <http://dev.ardupilot.com/wiki/plane-commands-in-guided-mode/>`_.

``message_factory()`` uses a factory method for the encoding. The name of this method will always be the
lower case version of the message/command name with ``_encode`` appended. For example, to encode a
`SET_POSITION_TARGET_LOCAL_NED <https://pixhawk.ethz.ch/mavlink/#SET_POSITION_TARGET_LOCAL_NED>`_
message we call ``message_factory.set_position_target_local_ned_encode()`` with values for all the
message fields as arguments:

.. code-block:: python

msg = vehicle.message_factory.set_position_target_local_ned_encode(
0, # time_boot_ms (not used)
0, 0, # target system, target component
0, 0, # target_system, target_component
mavutil.mavlink.MAV_FRAME_BODY_NED, # frame
0b0000111111000111, # type_mask (only speeds enabled)
0, 0, 0, # x, y, z positions
Expand All @@ -184,16 +199,23 @@ MAVLink commands are sent by first using :py:func:`message_factory <dronekit.lib
# send command to vehicle
vehicle.send_mavlink(msg)

There is no need to specify the system id, component id or sequence number of messages (if defined in the message type) as the API will set these appropriately when the message is sent.
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() <dronekit.lib.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.

.. _guided_mode_how_to_send_commands_command_long:

In Copter, the `COMMAND_LONG message <https://pixhawk.ethz.ch/mavlink/#COMMAND_LONG>`_ can be used send/package *a number* of different `supported MAV_CMD commands <http://dev.ardupilot.com/wiki/copter-commands-in-guided-mode/>`_. The factory function is again the lower case message name with suffix ``_encode`` (``message_factory.command_long_encode``). The message parameters include the actual command to be sent (in the code fragment below ``MAV_CMD_CONDITION_YAW``) and its fields.
In Copter, the `COMMAND_LONG message <https://pixhawk.ethz.ch/mavlink/#COMMAND_LONG>`_ can be used send/package
*a number* of different `supported MAV_CMD commands <http://dev.ardupilot.com/wiki/copter-commands-in-guided-mode/>`_.
The factory function is again the lower case message name with suffix ``_encode`` (``message_factory.command_long_encode``).
The message parameters include the actual command to be sent (in the code fragment below ``MAV_CMD_CONDITION_YAW``) and its fields.

.. code-block:: python

msg = vehicle.message_factory.command_long_encode(
0, 0, # target system, target component
0, 0, # target_system, target_component
mavutil.mavlink.MAV_CMD_CONDITION_YAW, #command
0, #confirmation
heading, # param 1, yaw in degrees
Expand Down
37 changes: 26 additions & 11 deletions dronekit/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
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 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() <dronekit.lib.Vehicle.send_mavlink>`).


:returns: A connected vehicle of the type defined in ``vehicle_class`` (a superclass of :py:class:`Vehicle`).
Expand Down Expand Up @@ -1539,11 +1543,12 @@ def send_mavlink(self, message):
"""
This method is used to send raw MAVLink "custom messages" to the vehicle.

The function can send arbitrary messages/commands to a vehicle at any time and in any vehicle mode. It is particularly useful for
controlling vehicles outside of missions (for example, in GUIDED mode).
The function can send arbitrary messages/commands to the connected vehicle at any time and in any vehicle mode.
It is particularly useful for controlling vehicles outside of missions (for example, in GUIDED mode).

The :py:func:`message_factory <dronekit.lib.Vehicle.message_factory>` is used to create messages in the appropriate format.
Callers do not need to populate sysId/componentId/crc in the packet as the method will take care of that before sending.

For more information see the guide topic: :ref:`guided_mode_how_to_send_commands`.

:param message: A ``MAVLink_message`` instance, created using :py:func:`message_factory <dronekit.lib.Vehicle.message_factory>`.
There is need to specify the system id, component id or sequence number of messages as the API will set these appropriately.
Expand All @@ -1555,8 +1560,15 @@ def message_factory(self):
"""
Returns an object that can be used to create 'raw' MAVLink messages that are appropriate for this vehicle.
The message can then be sent using :py:func:`send_mavlink(message) <dronekit.lib.Vehicle.send_mavlink>`.

.. note::

Vehicles support a subset of the messages defined in the MAVLink standard. For more information
about the supported sets see wiki topics:
`Copter Commands in Guided Mode <http://dev.ardupilot.com/wiki/copter-commands-in-guided-mode/>`_
and `Plane Commands in Guided Mode <http://dev.ardupilot.com/wiki/plane-commands-in-guided-mode/>`_.

These message types are defined in the central MAVLink github repository. For example, a Pixhawk understands
All message types are defined in the central MAVLink github repository. For example, a Pixhawk understands
the following messages (from `pixhawk.xml <https://github.com/mavlink/mavlink/blob/master/message_definitions/v1.0/pixhawk.xml>`_):

.. code:: xml
Expand All @@ -1574,12 +1586,13 @@ def message_factory(self):
msg = vehicle.message_factory.image_trigger_control_encode(True)
vehicle.send_mavlink(msg)

There is no need to specify the system id, component id or sequence number of messages (if defined in the message type) as the
API will set these appropriately when the message is sent.

.. todo:: When I have a custom message guide topic. Link from here to it.

.. todo:: Check if the standard MAV_CMD messages can be sent this way too, and if so add link.
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() <dronekit.lib.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.

For more information see the guide topic: :ref:`guided_mode_how_to_send_commands`.
"""
return self._master.mav

Expand Down Expand Up @@ -1876,7 +1889,9 @@ class Command(mavutil.mavlink.MAVLink_mission_item_message):
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.
Set this to zero (broadcast) when communicating with a companion computer.
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() <dronekit.lib.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
(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).
Expand Down