-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (63 loc) · 2.56 KB
/
main.py
File metadata and controls
76 lines (63 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from tello import TelloController
import sys
from datetime import datetime
import time
def process_command_line(command, tello_controller):
"""コマンド1行分の処理"""
if command != '' and command != '\n':
# コマンドの後の空白や空行を削除
command = command.rstrip()
if command.find('delay') != -1:
# 待機コマンドの場合
delay_time_sec = command.partition('delay')[2]
sec = float(delay_time_sec)
print(f"{sec}[秒]待機")
time.sleep(sec)
else:
# 待機以外のコマンドの場合、TELLOにコマンドを送信する
tello_controller.send_command(command)
def logging(tello_controller):
"""ログ記録&表示処理"""
# 記録されたログを取得
log = tello_controller.get_log()
# ログ出力ファイル名を生成(log/<時刻>.txt)
log_file = 'log/' + str(datetime.now()) + '.txt'
# ログファイルを書き込みモードでオープン
try:
with open(log_file, 'w') as out_file:
for stat in log:
# statからログ情報を1つ抜き出して表示する
stat.print_stats()
# ログファイルに書き出す
out_file.write(stat.return_stats())
except IOError:
# エラー表示
print(f"予期せぬエラーが発生しました")
def send_tello_commands(file_name):
"""TELLOにファイルから読み出したコマンドを1つずつ送信する"""
# TELLOを制御するためのクラスの生成
tello_controller = TelloController()
# コマンドの実行
try:
# コマンドファイルを開く
with open(file_name, 'r') as in_file:
# 全ての行数分取り出す
commands = in_file.readlines()
for command in commands:
# 1行分を取り出してコマンド解析し送信する
process_command_line(command, tello_controller)
except FileNotFoundError:
# エラー表示
print(f"コマンドファイルが見つかりませんでした {file_name}")
except IOError:
# エラー表示
print(f"予期せぬエラーが発生しました {file_name}")
# ログ記録&表示
logging(tello_controller)
# メイン処理
if len(sys.argv) == 2:
# 引数からコマンドが書かれたファイル名を取得する
send_tello_commands(sys.argv[1])
else:
# エラー表示
print(f"引数が間違っています")