Skip to content

Controlling the Drone With the API

Dave Walker edited this page Jan 24, 2020 · 1 revision

Creating a Connection to the Drone

To connect to a Tello drone at the published address and on the published port:

using TelloCommander.CommandDictionaries;
using TelloCommander.Commander;
using TelloCommander.Connections;

var dictionary = CommandDictionary.ReadStandardDictionary(version);
var commander = new DroneCommander(new TelloConnection(), dictionary);
commander.Connect();

"version" is a string containing the API version number e.g. "1.3.0.0".

Creating a Connection to the Simulator

To connect to a local copy of the simulator:

using TelloCommander.CommandDictionaries;
using TelloCommander.Commander;
using TelloCommander.Connections;

var dictionary = CommandDictionary.ReadStandardDictionary(version);
ITelloConnection connection = new TelloConnection(
                                    IPAddress.Loopback.ToString(),
                                    TelloConnection.DefaultTelloPort,
                                    ConnectionType.Simulator);
                            
var commander = new DroneCommander(connection, dictionary);
commander.Connect();

"version" is a string containing the API version number e.g. "1.3.0.0".

Issuing Commands

To issue a command via a commander instance connected to a drone or the simulator:

try
{
    commander.RunCommand(command);
}
catch (Exception ex)
{
}

Where "command" is a string containing the command and it's arguments e.g. "up 50".

Note that the commander will throw exceptions if it encounters an error condition so it is recommended to run commands within a try/catch block so that exceptions can be reported and the drone landed and connection closed down cleanly, if necessary.

Closing the Current Connection

To close the current connection:

commander.Disconnect();

Please make sure to land the drone before closing the connection.

Accessing Command History

The last response received by the commander instance can be accessed using the "LastResponse" property e.g.

Console.WriteLine(commander.LastResponse);

The command/response and error history can be accessed using the "History" property e.g.

foreach (string entry in commander.History)
{
    Console.WriteLine(entry);
}

Processing Read Command Responses

The Tello API provides a number of commands for reading the status of the drone. Many of the responses are returned as delimited strings or strings containing numbers with units. To make these values easier to use in applications built using TelloCommander, a ResponseParser class is provided in the TelloCommander.Response namespace. This exposes methods to convert the responses from the drone into usable values, as follows:

Method Example Command Example Response Method Returns
ParseToNumber height? 6dm decimal
ParseToRange temp? 60-61C A tuple : (decimal minimum, decimal maximum)
ParseToDictionary attitude? pitch:9;roll:-36;yaw:-51; Dictionary<string, string>
ParseToAcceleration acceleration? agx:-65.00;agy:31.00;agz:-994.00; An instance of the Acceleration class, wth X, Y and Z properties
ParseToAttitude attitude? pitch:9;roll:-36;yaw:-51; An instance of the Attitude class, with Pitch, Roll and Yaw properties
ParseToTemperature temp? 60-61C An instance of the Temperature class, with Minimum and Maximum properties

For example, assuming a connected DroneCommander instance, commander, the following code snippet would retrieve the current attitude and write it to the console:

commander.RunCommand("attitude?");
Attitude a = ResponseParser.ParseToAttitude(commander.LastResponse);
Console.WriteLine($"Attitude : Pitch {a.Pitch}, roll {a.Roll}, yaw {a.Yaw}");

Exceptions

The following custom exceptions may be thrown by the API when running a command:

Type Meaning
CommandNotValidForConnectionTypeException A command has been issued that is not valid for the current connection type
InvalidArgumentCountException Too many or too few arguments supplied with a command
InvalidArgumentException A numeric value is not a valid number or is out of the valid range
InvalidCommandException Command is not in the current dictionary
InvalidDictionaryXmlException Requested dictionary does not conform to the schema specified in the command dictionary XSD
InvalidValueRangeException The dictionary contains a value range in which the minimum and maximum are the wrong way round
OptionalArgumentPositionException The dictionary contains an argument list where mandatory and optional arguments are intermingled
TooLowToMoveDownException A "down" command has been issued with a value that is not valid for the current drone height
ValueOutOfRangeException A value is out of the minimum to maximum range for a command argument