1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】TelloとUDP通信②- Telloからメッセージを受信する -

1
Posted at

概要

前回、Telloに対しUDP通信でコマンドを送信しましたが、今回はコマンドに対しての応答について説明していきます。
各コマンドに対し、Telloから応答メッセージが送信されるため、端末側で受信処理を行うことでメッセージを取得できます。

受信処理

Telloにコマンドを送信した時、Telloから応答メッセージが送信されます。
NWConnection の receive を使用することで、受信処理を行えます。

let connection = NWConnection(host: "192.168.10.1", port: 8889, using: .udp)

//送信処理
connection.send(content: message, contentContext: .defaultMessage, isComplete: true, completion: .contentProcessed( error in ))

//受信処理
connection.receive(minimumIncompleteLength: 0, maximumLength: Int(Int32.max)) { (data: Data?, contentContext: NWConnection.ContentContext?, aBool: Bool, error: NWError?) in
         //処理内容           
}
```

取得したdataは文字に変換することで内容を確認できます。

```
if let data = data, let message = String(data: data, encoding: .utf8) {
         //応答メッセージ
         print("message: \(message)")
}
```


# 応答メッセージ

Tello SDKのコマンドでは、離陸、着陸等の操作に対しての応答と、バッテリー等のステータス情報に対しての応答があります。

操作コマンドは以下になります。応答としてはokかerrorを送信します。

| コマンド|          説明|
|:-----------|:---------------------|
| command |        SDKモード開始 |
| takeoff |        離陸 |
| land |        着地 |
| streamon |        ビデオストリーム開始 |
| streamoff |        ビデオストリーム停止 |
| emergency |        緊急停止 |
| up x |        上にxcm進む<br />x:20-500 |
| down x |        下にxcm進む<br />x:20-500 |
| left x |        左にxcm進む<br />x:20-500 |
| right x |        右にxcm進む<br />x:20-500 |
| forward x |        前にxcm進む<br />x:20-500 |
| back x |        後ろにxcm進む<br />x:20-500 |
| cw x |        時計回りにx度回転<br /> x: 1-3600|
| ccw x |        反時計回りにx度回転<br />x: 1-3600 |
| flip x |        xにひっくり返す<br /> X:l (左) r (右) f (前) b (後ろ)|
| go x y z speed |        x y zにspeed(cm/s)で進む<br />x: 20-500 y: 20-500 z: 20-500 speed: 10-100 |
| curve x1 y1 z1 x2 y2 z2 speed  |        現在の座標と2つの与えられた座標で定義された曲線をspeed(cm/s)で飛行<br />x1, x2: 20-500<br />y1, y2: 20-500<br />z1, z2: 20-500<br />speed: 10-60|


Telloのステータス情報は以下のコマンドで確認できます。

| コマンド | 説明 |応答|
|:-----------|:------------|:------------|
| speed?  |        速度|x:1-100 (cm/s)|
| battery? |        バッテリー |x:0-100|
| time? |        飛行時間 |x (s)|
| height? |        高さ |x:0-3000 (cm)|
| temp? |        温度 |x:0-90 (℃)|
| attitude? |        IMU姿勢データ |pitch roll yaw|
| baro? |       気圧 |x (m)|
| acceleration? |        IMU角加速度データ |x y z (0.001g)|
| tof? |        TOFから距離値 |x:30-1000 (cm)|
| wifi? |        Wi-Fi SNR | SNR |

# 動画情報の取得

Telloのカメラからの情報は stremon のコマンドで動画情報を受信できるようになります。
取得する場合、PORT:8889からは取得できないため、IP:0.0.0.0 PORT:11111 のUDPサーバを作り受信させる必要があります。

# 参考
[receive](https://developer.apple.com/documentation/network/nwconnection/2998572-receive)
[Tello SDK](https://terra-1-g.djicdn.com/2d4dce68897a46b19fc717f3576b7c6a/Tello%20编程相关/For%20Tello/Tello%20SDK%20Documentation%20EN_1.3_1122.pdf)
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?