Skip to content

Commit

Permalink
Add graceful halt for Tello driver
Browse files Browse the repository at this point in the history
* graceful terminate connections for video and command communication
  • Loading branch information
mike1808 authored and deadprogram committed Mar 17, 2020
1 parent 6bf5b3a commit e80a701
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions platforms/dji/tello/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ type Driver struct {
throttle int
bouncing bool
gobot.Eventer
doneCh chan struct{}
}

// NewDriver creates a driver for the Tello drone. Pass in the UDP port to use for the responses
Expand All @@ -201,6 +202,7 @@ func NewDriver(port string) *Driver {
respPort: port,
videoPort: "11111",
Eventer: gobot.NewEventer(),
doneCh: make(chan struct{}, 1),
}

d.AddEvent(ConnectedEvent)
Expand Down Expand Up @@ -284,10 +286,16 @@ func (d *Driver) Start() error {
d.processVideo()
})

cmdLoop:
for {
err := d.handleResponse(cmdConn)
if err != nil {
fmt.Println("response parse error:", err)
select {
case <-d.doneCh:
break cmdLoop
default:
err := d.handleResponse(cmdConn)
if err != nil {
fmt.Println("response parse error:", err)
}
}
}
}()
Expand All @@ -313,9 +321,9 @@ func (d *Driver) Start() error {
func (d *Driver) Halt() (err error) {
// send a landing command when we disconnect, and give it 500ms to be received before we shutdown
d.Land()
d.doneCh <- struct{}{}
time.Sleep(500 * time.Millisecond)

// TODO: cleanly shutdown the goroutines that are handling the UDP connections before closing
d.cmdConn.Close()
d.videoConn.Close()
return
Expand Down Expand Up @@ -937,15 +945,21 @@ func (d *Driver) processVideo() error {
}

go func() {
videoConnLoop:
for {
buf := make([]byte, 2048)
n, _, err := d.videoConn.ReadFromUDP(buf)
if err != nil {
fmt.Println("Error: ", err)
continue
select {
case <-d.doneCh:
break videoConnLoop
default:
buf := make([]byte, 2048)
n, _, err := d.videoConn.ReadFromUDP(buf)
if err != nil {
fmt.Println("Error: ", err)
continue
}

d.Publish(d.Event(VideoFrameEvent), buf[2:n])
}

d.Publish(d.Event(VideoFrameEvent), buf[2:n])
}
}()

Expand Down

0 comments on commit e80a701

Please sign in to comment.