Skip to content

Commit

Permalink
Dji Tello Halt does not terminate all the related goroutines and may …
Browse files Browse the repository at this point in the history
…wait forever when it is called multiple times

Fix the issue.
  • Loading branch information
st-user committed May 25, 2021
1 parent 1ffc138 commit 8c5ac6f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
37 changes: 31 additions & 6 deletions platforms/dji/tello/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"strconv"
"sync"
"sync/atomic"
"time"

"gobot.io/x/gobot"
Expand Down Expand Up @@ -191,7 +192,8 @@ type Driver struct {
throttle int
bouncing bool
gobot.Eventer
doneCh chan struct{}
doneCh chan struct{}
doneChReaderCount int32
}

// NewDriver creates a driver for the Tello drone. Pass in the UDP port to use for the responses
Expand Down Expand Up @@ -280,7 +282,10 @@ func (d *Driver) Start() error {
d.cmdConn = cmdConn

// handle responses
d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)

d.On(d.Event(ConnectedEvent), func(interface{}) {
d.SendDateTime()
d.processVideo()
Expand All @@ -304,13 +309,22 @@ func (d *Driver) Start() error {
d.SendCommand(d.connectionString())

// send stick commands
d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)

stickCmdLoop:
for {
err := d.SendStickCommand()
if err != nil {
fmt.Println("stick command error:", err)
select {
case <-d.doneCh:
break stickCmdLoop
default:
err := d.SendStickCommand()
if err != nil {
fmt.Println("stick command error:", err)
}
time.Sleep(20 * time.Millisecond)
}
time.Sleep(20 * time.Millisecond)
}
}()

Expand All @@ -321,7 +335,11 @@ 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{}{}
readerCount := atomic.LoadInt32(&d.doneChReaderCount)
for i := 0; i < int(readerCount); i++ {
d.doneCh <- struct{}{}
}

time.Sleep(500 * time.Millisecond)

d.cmdConn.Close()
Expand Down Expand Up @@ -946,7 +964,10 @@ func (d *Driver) processVideo() error {
return err
}

d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)

videoConnLoop:
for {
select {
Expand Down Expand Up @@ -989,6 +1010,10 @@ func (d *Driver) connectionString() string {
return res
}

func (d *Driver) addDoneChReaderCount(delta int32) {
atomic.AddInt32(&d.doneChReaderCount, delta)
}

func (f *FlightData) AirSpeed() float64 {
return math.Sqrt(
math.Pow(float64(f.NorthSpeed), 2) +
Expand Down
14 changes: 14 additions & 0 deletions platforms/dji/tello/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,38 @@ func TestHaltShouldTerminateAllTheRelatedGoroutines(t *testing.T) {

var wg sync.WaitGroup
wg.Add(3)

d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)

<-d.doneCh
wg.Done()
fmt.Println("Done routine 1.")
}()

d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)

<-d.doneCh
wg.Done()
fmt.Println("Done routine 2.")
}()

d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)

<-d.doneCh
wg.Done()
fmt.Println("Done routine 3.")
}()

d.Halt()
wg.Wait()

gobottest.Assert(t, d.doneChReaderCount, int32(0))
}

func TestHaltNotWaitForeverWhenCalledMultipleTimes(t *testing.T) {
Expand Down

0 comments on commit 8c5ac6f

Please sign in to comment.