-
Notifications
You must be signed in to change notification settings - Fork 4
/
handlers.go
129 lines (106 loc) · 2.58 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"os/exec"
"sync/atomic"
"time"
"github.com/bep/debounce"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/dji/tello"
"gobot.io/x/gobot/platforms/keyboard"
)
func handleKeyboardInput(drone *tello.Driver) func(interface{}) {
return func(data interface{}) {
if !connected || landing {
return
}
key := data.(keyboard.KeyEvent).Key
// Case 1: Pressed key is a steering command
for _, k := range controlCommands {
if k == key {
cc := atomic.LoadInt32(¤tControl)
if cc == -1 {
steeringDebounce = debounce.New(debounceTimeout)
}
atomic.StoreInt32(¤tControl, int32(key))
steeringDebounce(func() {
atomic.StoreInt32(¤tControl, -1)
})
return
}
}
// Case 2: Pressed key is any other commmand or none
if key == keyboard.Spacebar {
// Space
if !flying {
fmt.Println("Taking off.")
if !dry {
if err := drone.TakeOff(); err != nil {
fmt.Println(err)
}
}
} else {
fmt.Println("Landing.")
if !dry {
if err := drone.Land(); err != nil {
fmt.Println(err)
}
}
}
} else {
fmt.Println("Unknown command...")
}
}
}
func handleFlightData(drone *tello.Driver) func(interface{}) {
return func(data interface{}) {
flightData := data.(*tello.FlightData)
atomic.AddUint64(&dataCounter, 1)
if atomic.LoadUint64(&dataCounter)%10 == 0 {
fmt.Printf("Ground Speed: %.2f, Battery: %d %%, Height: %.2f m\n", flightData.GroundSpeed(), flightData.BatteryPercentage, float32(flightData.Height)/10.0)
}
flying = flightData.Flying
if landing && flightData.Height <= 0 {
fmt.Println("Drone has landed.")
landing = false
}
}
}
func handleConnected(drone *tello.Driver) func(interface{}) {
return func(data interface{}) {
fmt.Println("Drone connected.")
connected = true
drone.SetVideoEncoderRate(2)
gobot.Every(100*time.Millisecond, func() {
drone.StartVideo()
})
gobot.Every(time.Duration((1000.0/tickRate))*time.Millisecond, func() {
if connected {
tick(drone)
}
})
}
}
func handleLanding(drone *tello.Driver) func(interface{}) {
return func(data interface{}) {
fmt.Println("Drone is landing ...")
}
}
func handleVideo(drone *tello.Driver) func(interface{}) {
mplayer := exec.Command("mplayer", "-fps", "20", "-")
videoIn, err := mplayer.StdinPipe()
if err != nil {
fmt.Println(err)
return nil
}
if err := mplayer.Start(); err != nil {
fmt.Println(err)
return nil
}
return func(data interface{}) {
pkt := data.([]byte)
if _, err := videoIn.Write(pkt); err != nil {
fmt.Println(err)
}
}
}