forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bebop_ps3.go
149 lines (122 loc) · 2.99 KB
/
bebop_ps3.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// +build example
//
// Do not build by default.
package main
import (
"sync/atomic"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/joystick"
"gobot.io/x/gobot/platforms/parrot/bebop"
)
type pair struct {
x float64
y float64
}
var leftX, leftY, rightX, rightY atomic.Value
const offset = 32767.0
func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
bebopAdaptor := bebop.NewAdaptor()
drone := bebop.NewDriver(bebopAdaptor)
work := func() {
leftX.Store(float64(0.0))
leftY.Store(float64(0.0))
rightX.Store(float64(0.0))
rightY.Store(float64(0.0))
recording := false
stick.On(joystick.CirclePress, func(data interface{}) {
if recording {
drone.StopRecording()
} else {
drone.StartRecording()
}
recording = !recording
})
stick.On(joystick.SquarePress, func(data interface{}) {
drone.HullProtection(true)
drone.TakeOff()
})
stick.On(joystick.TrianglePress, func(data interface{}) {
drone.Stop()
})
stick.On(joystick.XPress, func(data interface{}) {
drone.Land()
})
stick.On(joystick.LeftX, func(data interface{}) {
val := float64(data.(int16))
leftX.Store(val)
})
stick.On(joystick.LeftY, func(data interface{}) {
val := float64(data.(int16))
leftY.Store(val)
})
stick.On(joystick.RightX, func(data interface{}) {
val := float64(data.(int16))
rightX.Store(val)
})
stick.On(joystick.RightY, func(data interface{}) {
val := float64(data.(int16))
rightY.Store(val)
})
gobot.Every(10*time.Millisecond, func() {
leftStick := getLeftStick()
switch {
case leftStick.y < -10:
drone.Forward(bebop.ValidatePitch(leftStick.y, offset))
case leftStick.y > 10:
drone.Backward(bebop.ValidatePitch(leftStick.y, offset))
default:
drone.Forward(0)
}
switch {
case leftStick.x > 10:
drone.Right(bebop.ValidatePitch(leftStick.x, offset))
case leftStick.x < -10:
drone.Left(bebop.ValidatePitch(leftStick.x, offset))
default:
drone.Right(0)
}
})
gobot.Every(10*time.Millisecond, func() {
rightStick := getRightStick()
switch {
case rightStick.y < -10:
drone.Up(bebop.ValidatePitch(rightStick.y, offset))
case rightStick.y > 10:
drone.Down(bebop.ValidatePitch(rightStick.y, offset))
default:
drone.Up(0)
}
switch {
case rightStick.x > 20:
drone.Clockwise(bebop.ValidatePitch(rightStick.x, offset))
case rightStick.x < -20:
drone.CounterClockwise(bebop.ValidatePitch(rightStick.x, offset))
default:
drone.Clockwise(0)
}
})
}
robot := gobot.NewRobot("bebop",
[]gobot.Connection{joystickAdaptor, bebopAdaptor},
[]gobot.Device{stick, drone},
work,
)
robot.Start()
}
func getLeftStick() pair {
s := pair{x: 0, y: 0}
s.x = leftX.Load().(float64)
s.y = leftY.Load().(float64)
return s
}
func getRightStick() pair {
s := pair{x: 0, y: 0}
s.x = rightX.Load().(float64)
s.y = rightY.Load().(float64)
return s
}