LoginSignup
8
5

More than 3 years have passed since last update.

【Swift】TelloとUDP通信①- Telloにメッセージを送信する -

Last updated at Posted at 2019-10-06

概要

Ryze Tech社のドローン「Tello」は、Tello SDKでプログラミングを行い、ドローンを操作することができます。

Tello SDKでは、Tello(IP:192.168.10.1 PORT:8889)とUDP通信を行いTelloを操作しています。

今回は、Tello SDKを参考に、Swiftでのドローンのプログラミングを行います。

Telloに接続する

SwiftはNWConnectionを使用することででUDP通信を行えるため、
Networkをimportし、NWConnectionを使用できるようにします。

import Network

まず、NWConnectionを使用し、UDPでTello(IP:192.168.10.1 PORT:8889)との接続を開始します。

//Telloへの接続の初期化
let connection = NWConnection(host: "192.168.10.1", port: 8889, using: NWParameters.udp)
//Telloへの接続開始
connection.start(queue: .global())

接続開始後にTelloとの接続状態の更新を受け取ります。接続が確立された場合、stateがreadyになります。

connection.stateUpdateHandler = { state in

     switch state {
          case .setup:
               ()
          case .waiting(let error):
               print(error)
          case .preparing:
               ()
          case .ready:
               ()
          case .failed(let error):
               print(error)
          case .cancelled:
               ()
     }

}

コマンドをTelloに送信する

Telloとの接続状態がreadyになった時、Telloにcommandを送ることでTelloを操作することが可能になります。

//コマンドをdataに変換
let text = "command"
let message = text.data(using: .utf8)!

//Telloに送信
connection.send(content: message, contentContext: .defaultMessage, isComplete: true, completion: .contentProcessed({ error in if let error = error { print(error) } }))

以下のコマンドを使用することで、Telloの離陸、着陸の操作ができます。

コマンド 操作
command SDKモード開始
takeoff 離陸
land 着陸

参考

NWConnection
Tello SDK
[WWDC18] iOS 12で登場するNetwork Frameworkでネットワークプログラミングが容易になりそうです #WWDC18
PythonによるTello操作(基本、及びクラウドからのMQTTによる操作まで)

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5