前回の残り半分も理解はしましたが、、、
スレッドを立てているのは、コマンドのtelloからの
反応を、printしているだけの感じで
並列処理するほど? 疑問です。
import socket #ソケットはつなげるので必要
import threading #並列処理をしよう
import time #時間は大事
from stats import Stats #何をいましているか状況をプリントする
class Tello:
def __init__(self): #呼び出されたときに最初に実行される関数(初期化関数)
self.local_ip = '' #ネット全般を扱うよ(0,0,0,0)と同じ公共放送です
self.local_port = 8889 #受信では8889ポート使います。受信は8889番線で
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # socket for sending cmd
#ソケットプロトコル AF_INET インターネットIpv4 SOCK_DGRAMはUDP接続
#telloはかんたんに接続する複雑なことはできませんて話
self.socket.bind((self.local_ip, self.local_port))
#ソケットはできてもPC(サーバ側に繋げないと何してるかわからないというわけでバインドする
#tello側は常時待機状態なので必要ないようです
# thread for receiving cmd ack
self.receive_thread = threading.Thread(target=self._receive_thread)
#あとの_receive_threadを並列処理するので名前を**receive_thread 実際はtelloとつけます
self.receive_thread.daemon = True #ドラちゃんに働いてもらいます
self.receive_thread.start() #スレッド開始です
self.tello_ip = '192.168.10.1' #telloと通信するアドレス
self.tello_port = 8889 #ポートは受信用telloから見ると報告を送信するポート
self.tello_adderss = (self.tello_ip, self.tello_port) #合体させてaddressお名前つけ
self.log = []
self.MAX_TIME_OUT = 15.0 #timeアウトは15秒 timeアウト処理は下に出てきます。
def send_command(self, command): #"""3つ使うとコメントアウトできるのね
"""
Send a command to the ip address. Will be blocked until
the last command receives an 'OK'.
IPアドレスにコマンドを送信します。「OK]コマンドを最後に受け取るまではブロックされます。
受信したよ次のコマンドOKだよ受け取るまでは送信しないということさ。
If the command fails (either b/c time out or error),
will try to resend the command
コマンドが失敗した場合は再送信を試みる
:param command: (str) the command to send command変数はコマンドを送るため
:param ip: (str) the ip of Tello ip変数はtelloのipあどれす
:return: The latest command response return変数は最新のコマンドの応答
"""
self.log.append(Stats(command, len(self.log))) #コマンドの応答をlogする
self.socket.sendto(command.encode('utf-8'), self.tello_adderss)
#ようやくutf-8のコードで命令を送ります
print 'sending command: %s to %s' % (command, self.tello_ip)
#この命令を送ったよと表示
start = time.time() #スタート時間を取得
while not self.log[-1].got_response(): #got_responseはstatsで定義されてます反応です
#反応がないときはfalseで−1を返します それまでは
#実行しますということ
now = time.time() #今の時間をnowに入れて
diff = now - start # スタートしてから何秒立ったというわけ?
if diff > self.MAX_TIME_OUT: # タイムアウト15秒過ぎたら処理をするよ
print 'Max timeout exceeded... command %s' % command
# TODO: is timeout considered failure or next command still get executed
# now, next one got executed
return #終わってしまうわけね
print 'Done!!! sent command: %s to %s' % (command, self.tello_ip)
#ここまでくれば終わり
def _receive_thread(self): #並列処理の本体ですが
"""Listen to responses from the Tello.
Runs as a thread, sets self.response to whatever the Tello last returned.
"""
while True:
try:
self.response, ip = self.socket.recvfrom(1024) #telloからip とポート番号を受信
print('from %s: %s' % (ip, self.response))
self.log[-1].add_response(self.response)
except socket.error, exc:
print "Caught exception socket.error : %s" % exc
def on_close(self):
pass
# for ip in self.tello_ip_list:
# self.socket.sendto('land'.encode('utf-8'), (ip, 8889))
# self.socket.close()
def get_log(self):
return self.log