Linux活用レシピ > ドローンに活用 > ドローンをRaspberryPiとGoで自動飛行させカメラ動画を撮影し録画する方法
このページでは、ドローン「TELLO」をRaspberry PiとGoで自動飛行させカメラ動画を撮影し録画する方法を紹介します。


ドローンをラズパイとGoで自動飛行&撮影(録画)する方法
カメラ動画をライブ表示

自動飛行ができたところで、カメラ動画をライブ表示出来るよう、コードを変更しましょう。

カメラ動画をライブ表示できるようコードを追加

    先ほど作った、自動飛行プログラムに、カメラ動画をライブ表示できるようコード(赤文字)を追加し、実行しましょう。

    まず、Raspbianのメニューから赤枠の部分をクリックしてターミナルを起動します。


    カレントディレクトリに「tellotakeoff-left20-play.go」をviで作成します。
    pi@raspberrypi:~ $ vi tellotakeoff-left20-play.go[ENTER]
    
    Telloが離陸して10秒後に左にスピード「20」で移動し、5秒後に静止その後5秒後に着陸するプログラムを先ほど書きましたが、ここに以下の通り赤文字で示したコードを増やしています。
    package main
    
    import (
        "time"
        "os/exec"
        "fmt"
    
        "gobot.io/x/gobot"
        "gobot.io/x/gobot/platforms/dji/tello"
    )
    
    func main(){
        drone := tello.NewDriver("8888")
    
        work := func() {
                    mplayer := exec.Command("mplayer","-fps", "25", "-")
                    mplayerIn, _ := mplayer.StdinPipe()
                    if err := mplayer.Start(); err != nil {
                            fmt.Println(err)
                            return
                    }
    
                    drone.On(tello.ConnectedEvent, func(data interface{}) {
                            fmt.Println("Connected")
                            drone.StartVideo()
                            drone.SetVideoEncoderRate(4)
                            gobot.Every(100*time.Millisecond, func() {
                                    drone.StartVideo()
                            })
                    })
    
                    drone.On(tello.VideoFrameEvent, func(data interface{}) {
                            pkt := data.([]byte)
                            if _, err := mplayerIn.Write(pkt); err != nil {
                                    fmt.Println(err)
                            }
                    })
                    time.Sleep(10 * time.Second)
    
                    drone.Left(0)
                    drone.Right(0)
                    drone.Forward(0)
                    drone.Backward(0)
                    drone.TakeOff()
                    fmt.Printf("TakeOff\n")
    
                    gobot.After(10*time.Second, func() {
                           drone.Left(20)
                           fmt.Printf("Left 20\n")
    
                           time.Sleep(5 * time.Second)
                           drone.Left(0)
                           fmt.Printf("Left 0\n")
    
                           time.Sleep(5 * time.Second)
                           drone.Land()
                           fmt.Printf("Land\n")
                           return
                    })
    
        }
    
        robot := gobot.NewRobot("tello",
            []gobot.Connection{},
            []gobot.Device{drone},
            work,
        )
    
        robot.Start()
    }
    


    「tellotakeoff-left20-play.go」を実行します。
    pi@raspberrypi:~ $ go run tellotakeoff-left20-play.go[ENTER]
    

    いかがでしょうか、MPlayerが表示されるまで10秒待ってTELLOが離陸、10秒後に左にスピード「20」で移動し、5秒後に静止その後5秒後に着陸する。
    こちら以下の動画はその様子を撮影したものです。ライブ映像は若干遅れて表示されますが、画像はまずまずですね。



続いて飛行中の動画を録画するサンプルプログラムを紹介します。