gobot.io/x/gobot/v2@v2.1.0/examples/tello_video.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8  You must have MPlayer (https://mplayerhq.hu) installed in order to run this code. it will connect to the Tello
     9  and then open a window using MPlayer showing the streaming video.
    10  
    11  How to run
    12  
    13  	go run examples/tello_video.go
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os/exec"
    21  	"time"
    22  
    23  	"gobot.io/x/gobot/v2"
    24  	"gobot.io/x/gobot/v2/platforms/dji/tello"
    25  )
    26  
    27  func main() {
    28  	drone := tello.NewDriver("8890")
    29  
    30  	mplayer := exec.Command("mplayer", "-fps", "60", "-")
    31  	mplayerIn, _ := mplayer.StdinPipe()
    32  	if err := mplayer.Start(); err != nil {
    33  		fmt.Println(err)
    34  		return
    35  	}
    36  
    37  	work := func() {
    38  		drone.On(tello.ConnectedEvent, func(data interface{}) {
    39  			fmt.Println("Connected")
    40  			drone.StartVideo()
    41  			drone.SetVideoEncoderRate(tello.VideoBitRateAuto)
    42  			gobot.Every(100*time.Millisecond, func() {
    43  				drone.StartVideo()
    44  			})
    45  		})
    46  
    47  		drone.On(tello.VideoFrameEvent, func(data interface{}) {
    48  			pkt := data.([]byte)
    49  			if _, err := mplayerIn.Write(pkt); err != nil {
    50  				fmt.Println(err)
    51  			}
    52  		})
    53  	}
    54  
    55  	robot := gobot.NewRobot("tello",
    56  		[]gobot.Connection{},
    57  		[]gobot.Device{drone},
    58  		work,
    59  	)
    60  
    61  	robot.Start()
    62  }