-
Notifications
You must be signed in to change notification settings - Fork 4
/
tello.go
120 lines (101 loc) · 2.29 KB
/
tello.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
package main
import (
"flag"
"fmt"
"sync/atomic"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/dji/tello"
"gobot.io/x/gobot/platforms/keyboard"
)
var (
dry bool
connected bool
flying bool
landing bool
previousControl int32 = -1
currentControl int32 = -1
tickCounter uint64
dataCounter uint64
steeringDebounce func(func())
)
func tick(drone *tello.Driver) {
cc, pc := atomic.LoadInt32(¤tControl), atomic.LoadInt32(&previousControl)
if !connected || landing || cc == pc {
return
}
if cc == keyboard.A {
fmt.Println("Going left.")
if !dry {
drone.Left(intensity)
}
} else if cc == keyboard.D {
fmt.Println("Going right.")
if !dry {
drone.Right(intensity)
}
} else if cc == keyboard.W {
fmt.Println("Going up.")
if !dry {
drone.Up(intensity)
}
} else if cc == keyboard.S {
fmt.Println("Going down.")
if !dry {
drone.Down(intensity)
}
} else if cc == keyboard.ArrowUp {
fmt.Println("Going forward.")
if !dry {
drone.Forward(intensity)
}
} else if cc == keyboard.ArrowDown {
fmt.Println("Going backward.")
if !dry {
drone.Backward(intensity)
}
} else if cc == keyboard.ArrowLeft {
fmt.Println("Rotating counter-clockwise.")
if !dry {
drone.CounterClockwise(intensity)
}
} else if cc == keyboard.ArrowRight {
fmt.Println("Rotating clockwise.")
if !dry {
drone.Clockwise(intensity)
}
} else {
fmt.Println("Resetting steering.")
resetSteering(drone)
}
atomic.StoreInt32(&previousControl, cc)
atomic.AddUint64(&tickCounter, 1)
}
func init() {
var dryFlag = flag.Bool("dry", false, "Perform a dry run (don't send any actual control commands to Tello")
flag.Parse()
dry = *dryFlag
if dry {
fmt.Println("Running in dry mode.")
}
}
func main() {
// Init Gobot drivers
keys := keyboard.NewDriver()
drone := tello.NewDriver("8890")
work := func() {
// Handle keyboard inputs
keys.On(keyboard.Key, handleKeyboardInput(drone))
// Handle drone events
drone.On(tello.FlightDataEvent, handleFlightData(drone))
drone.On(tello.ConnectedEvent, handleConnected(drone))
drone.On(tello.LandingEvent, handleLanding(drone))
drone.On(tello.VideoFrameEvent, handleVideo(drone))
}
robot := gobot.NewRobot(
"tello",
[]gobot.Connection{},
[]gobot.Device{keys, drone},
work,
)
robot.Start()
}