2022年 冬休みプログラミングイベント開催のお知らせ(Pythonでドローンプログラミング)

Pythonでドローンプログラミング


Pythonでドローンを制御するプログラムを作ります。操作画面の各ボタンをクリックすることでドローンが離陸したり、前進したりします。



使用するドローン(Tello)






ドローン操作プログラムの画面イメージ


画面上のボタンをクリックしてドローンを操作します。

Pythonで記述するコード
import socket
import tkinter

# Tello側のローカルIPアドレス(デフォルト)、宛先ポート番号(コマンドモード用)
TELLO_IP = "192.168.10.1"
TELLO_PORT = 8889
TELLO_ADDRESS = (TELLO_IP, TELLO_PORT)

# UDP通信ソケットの作成(アドレスファミリ:AF_INET(IPv4)、ソケットタイプ:SOCK_DGRAM(UDP))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# TELLOがPCと通信する際に使用するIPアドレスとポート番号を設定
sock.bind(("0.0.0.0", TELLO_PORT))

# 最初にcommandコマンドを送信
try:
    sock.sendto("command".encode(encoding="utf-8"), TELLO_ADDRESS)
except:
    pass

# 離陸
def takeoff():
    表示["text"]="離陸"
    try:
        sock.sendto("takeoff".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 着陸
def land():
    表示["text"]="着陸"
    try:
        sock.sendto("land".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 上昇(50cm)
def up():
    表示["text"]="上昇(50cm)"
    try:
        sock.sendto("up 50".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 下降(50cm)
def down():
    表示["text"]="下降(50cm)"
    try:
        sock.sendto("down 50".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 前に進む(50cm)
def forward():
    表示["text"]="前進(50cm)"
    try:
        sock.sendto("forward 50".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 後に進む(50cm)
def back():
    表示["text"]="後退(50cm)"
    try:
        sock.sendto("back 50".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 右に進む(50cm)
def right():
    表示["text"]="右移動(50cm)"
    try:
        sock.sendto("right 50".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 左に進む(50cm)
def left():
    表示["text"]="左移動(50cm)"
    try:
        sock.sendto("left 50".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 右回りに回転(90度)
def cw():
    表示["text"]="右旋回(90度)"
    try:
        sock.sendto("cw 90".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass
        
# 左回りに回転(90度)
def ccw():
    表示["text"]="左旋回(90度)"
    try:
        sock.sendto("ccw 90".encode(encoding="utf-8"), TELLO_ADDRESS)
    except:
        pass

# 操作画面作成
ウィンドウ = tkinter.Tk()
ウィンドウ.geometry("800x600")
ウィンドウ.title("Tello操作画面")
ウィンドウ.resizable(False,False)
キャンバス=tkinter.Canvas(ウィンドウ,width=800,heigh=600)
キャンバス.place(x=0,y=0)

#画像ファイルアップロード
背景 = tkinter.PhotoImage(file=r"image\背景.png")
右移動 = tkinter.PhotoImage(file=r"image\右移動.png")
左移動 = tkinter.PhotoImage(file=r"image\左移動.png")
右回りに回転 = tkinter.PhotoImage(file=r"image\右回りに回転.png")
左回りに回転 = tkinter.PhotoImage(file=r"image\左回りに回転.png")
下降 = tkinter.PhotoImage(file=r"image\下降.png")
上昇 = tkinter.PhotoImage(file=r"image\上昇.png")
前進 = tkinter.PhotoImage(file=r"image\前進.png")
後退 = tkinter.PhotoImage(file=r"image\後退.png")
離陸 = tkinter.PhotoImage(file=r"image\離陸.png")
着陸 = tkinter.PhotoImage(file=r"image\着陸.png")

#キャンバス作成
キャンバス.create_image(400,300,image=背景)

# 離陸ボタン
takeoff = tkinter.Button(ウィンドウ,image = 離陸,command = takeoff
                         ,width = 174, height = 47)
takeoff.place(x=310, y=547)

# 着陸ボタン
land = tkinter.Button(ウィンドウ,image = 着陸,command = land
                      ,width = 172, height = 44)
land.place(x=310, y=0)

# 上昇(50cm)ボタン
up = tkinter.Button(ウィンドウ,image = 上昇, command = up
                    ,width = 40, height = 40)
up.place(x=95, y=225)

# 下降(50cm)ボタン
down = tkinter.Button(ウィンドウ,image = 下降, command = down
                      ,width = 40, height = 40)
down.place(x=95, y=332)

# 前に進む(50cm)ボタン
forward = tkinter.Button(ウィンドウ,image = 前進, command = forward
                         ,width = 40, height = 40)
forward.place(x=660, y=225)

# 後に進む(50cm)ボタン
back = tkinter.Button(ウィンドウ,image = 後退, command = back
                      ,width = 40, height = 40)
back.place(x=660, y=330)

# 右に進む(50cm)ボタン
right = tkinter.Button(ウィンドウ,image = 右移動, command = right
                       ,width = 40, height = 40)
right.place(x=718, y=280)

# 左に進む(20cm)ボタン
left = tkinter.Button(ウィンドウ,image = 左移動, command = left
                      ,width = 40, height = 40)
left.place(x=605, y=280)

# 右回りに回転(90度)ボタン
cw = tkinter.Button(ウィンドウ,image = 右回りに回転, command = cw
                    ,width = 40, height = 40)
cw.place(x=150, y=280)

# 左回りに回転(90度)ボタン
ccw = tkinter.Button(ウィンドウ,image = 左回りに回転, command = ccw
                     ,width = 40, height = 40)
ccw.place(x=40, y=280)

#表示用ラベル
表示 = tkinter.Label(ウィンドウ,text="----",bg="black",fg="white",font=24)
表示.place(x=10,y=10)

#メインループ
ウィンドウ.mainloop()


イベント開催日時


2022/12/28(水)
■ 10:30 ~ 12:00
■ 15:00 ~ 16:30

2023/1/9(月)
■ 13:00 ~ 14:30

イベント開催場所


テクノロ守口校
〒570-0026
大阪府守口市松月町2-19 松月ビル1階
(京阪本線・守口市駅 東改札口 から徒歩約5分)

Google Map

参加条件


tkinterモジュール学習済であること
※イベントではSockモジュールの使い方、ソケット通信の仕組み、IPアドレス、ポート番号、APIについて詳しく説明するため、tkinterモジュールに関する説明は行いません。

参加費


スクール生:無料
外部参加:不可 
※他のスクール等でPythonを学習しtkinterモジュールの使い方が分かる場合、参加について検討します。以下のようなプログラムを作成できるスキルがあれば、問題ありません。



持ち物


筆記用具

申込方法


以下のページからお申込みください。

冬休みイベント申込_Pythonでドローンプログラミング_2022年



注意事項


飛行中のドローンを絶対に手で触らないでください。プロペラに指が当たるとケガをします。Telloは15秒間、命令を送らない状態が続くと自動的に着陸するようにできています。