forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_minidrone.go
320 lines (264 loc) · 7.67 KB
/
ble_minidrone.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package ble
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/hybridgroup/gobot"
)
var _ gobot.Driver = (*BLEMinidroneDriver)(nil)
type BLEMinidroneDriver struct {
name string
connection gobot.Connection
stepsfa0a uint16
stepsfa0b uint16
flying bool
Pcmd Pcmd
gobot.Eventer
}
const (
// service IDs
DroneCommandService = "9a66fa000800919111e4012d1540cb8e"
DroneNotificationService = "9a66fb000800919111e4012d1540cb8e"
// characteristic IDs
PcmdCharacteristic = "9a66fa0a0800919111e4012d1540cb8e"
CommandCharacteristic = "9a66fa0b0800919111e4012d1540cb8e"
FlightStatusCharacteristic = "9a66fb0e0800919111e4012d1540cb8e"
BatteryCharacteristic = "9a66fb0f0800919111e4012d1540cb8e"
// Battery event
Battery = "battery"
// flight status event
Status = "status"
// flying event
Flying = "flying"
// landed event
Landed = "landed"
)
type Pcmd struct {
Flag int
Roll int
Pitch int
Yaw int
Gaz int
Psi float32
}
func validatePitch(val int) int {
if val > 100 {
return 100
} else if val < 0 {
return 0
}
return val
}
// NewBLEMinidroneDriver creates a BLEMinidroneDriver by name
func NewBLEMinidroneDriver(a *BLEClientAdaptor, name string) *BLEMinidroneDriver {
n := &BLEMinidroneDriver{
name: name,
connection: a,
Pcmd: Pcmd{
Flag: 0,
Roll: 0,
Pitch: 0,
Yaw: 0,
Gaz: 0,
Psi: 0,
},
Eventer: gobot.NewEventer(),
}
n.AddEvent(Battery)
n.AddEvent(Status)
n.AddEvent(Flying)
n.AddEvent(Landed)
return n
}
func (b *BLEMinidroneDriver) Connection() gobot.Connection { return b.connection }
func (b *BLEMinidroneDriver) Name() string { return b.name }
// adaptor returns BLE adaptor
func (b *BLEMinidroneDriver) adaptor() *BLEClientAdaptor {
return b.Connection().(*BLEClientAdaptor)
}
// Start tells driver to get ready to do work
func (b *BLEMinidroneDriver) Start() (errs []error) {
b.Init()
b.FlatTrim()
b.StartPcmd()
b.FlatTrim()
return
}
// Halt stops minidrone driver (void)
func (b *BLEMinidroneDriver) Halt() (errs []error) {
b.Land()
<-time.After(500 * time.Millisecond)
return
}
func (b *BLEMinidroneDriver) Init() (err error) {
b.GenerateAllStates()
// subscribe to battery notifications
b.adaptor().Subscribe(DroneNotificationService, BatteryCharacteristic, func(data []byte, e error) {
gobot.Publish(b.Event(Battery), data[len(data)-1])
})
// subscribe to flying status notifications
b.adaptor().Subscribe(DroneNotificationService, FlightStatusCharacteristic, func(data []byte, e error) {
if len(data) < 7 || data[2] != 2 {
fmt.Println(data)
return
}
gobot.Publish(b.Event(Status), data[6])
if (data[6] == 1 || data[6] == 2) && !b.flying {
b.flying = true
gobot.Publish(b.Event(Flying), true)
} else if (data[6] == 0) && b.flying {
b.flying = false
gobot.Publish(b.Event(Landed), true)
}
})
return
}
func (b *BLEMinidroneDriver) GenerateAllStates() (err error) {
b.stepsfa0b++
buf := []byte{0x04, byte(b.stepsfa0b), 0x00, 0x04, 0x01, 0x00, 0x32, 0x30, 0x31, 0x34, 0x2D, 0x31, 0x30, 0x2D, 0x32, 0x38, 0x00}
err = b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, buf)
if err != nil {
fmt.Println("GenerateAllStates error:", err)
return err
}
return
}
func (b *BLEMinidroneDriver) TakeOff() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x01, 0x00}
err = b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, buf)
if err != nil {
fmt.Println("takeoff error:", err)
return err
}
return
}
func (b *BLEMinidroneDriver) Land() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b), 0x02, 0x00, 0x03, 0x00}
err = b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, buf)
return err
}
func (b *BLEMinidroneDriver) FlatTrim() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x00, 0x00}
err = b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, buf)
return err
}
func (b *BLEMinidroneDriver) StartPcmd() {
go func() {
// wait a little bit so that there is enough time to get some ACKs
<-time.After(500 * time.Millisecond)
for {
err := b.adaptor().WriteCharacteristic(DroneCommandService, PcmdCharacteristic, b.generatePcmd().Bytes())
if err != nil {
fmt.Println("pcmd write error:", err)
}
<-time.After(50 * time.Millisecond)
}
}()
}
func (b *BLEMinidroneDriver) Up(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Gaz = validatePitch(val)
return nil
}
func (b *BLEMinidroneDriver) Down(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Gaz = validatePitch(val) * -1
return nil
}
func (b *BLEMinidroneDriver) Forward(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Pitch = validatePitch(val)
return nil
}
func (b *BLEMinidroneDriver) Backward(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Pitch = validatePitch(val) * -1
return nil
}
func (b *BLEMinidroneDriver) Right(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Roll = validatePitch(val)
return nil
}
func (b *BLEMinidroneDriver) Left(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Roll = validatePitch(val) * -1
return nil
}
func (b *BLEMinidroneDriver) Clockwise(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Yaw = validatePitch(val)
return nil
}
func (b *BLEMinidroneDriver) CounterClockwise(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Yaw = validatePitch(val) * -1
return nil
}
func (b *BLEMinidroneDriver) Stop() error {
b.Pcmd = Pcmd{
Flag: 0,
Roll: 0,
Pitch: 0,
Yaw: 0,
Gaz: 0,
Psi: 0,
}
return nil
}
// StartRecording not supported
func (b *BLEMinidroneDriver) StartRecording() error {
return nil
}
// StopRecording not supported
func (b *BLEMinidroneDriver) StopRecording() error {
return nil
}
// HullProtection not supported
func (b *BLEMinidroneDriver) HullProtection(protect bool) error {
return nil
}
// Outdoor not supported
func (b *BLEMinidroneDriver) Outdoor(outdoor bool) error {
return nil
}
func (b *BLEMinidroneDriver) FrontFlip() (err error) {
return b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, b.generateAnimation(0).Bytes())
}
func (b *BLEMinidroneDriver) BackFlip() (err error) {
return b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, b.generateAnimation(1).Bytes())
}
func (b *BLEMinidroneDriver) RightFlip() (err error) {
return b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, b.generateAnimation(2).Bytes())
}
func (b *BLEMinidroneDriver) LeftFlip() (err error) {
return b.adaptor().WriteCharacteristic(DroneCommandService, CommandCharacteristic, b.generateAnimation(3).Bytes())
}
func (b *BLEMinidroneDriver) generateAnimation(direction int8) *bytes.Buffer {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x04, 0x00, 0x00, byte(direction), 0x00, 0x00, 0x00}
return bytes.NewBuffer(buf)
}
func (b *BLEMinidroneDriver) generatePcmd() *bytes.Buffer {
b.stepsfa0a++
cmd := &bytes.Buffer{}
binary.Write(cmd, binary.LittleEndian, int8(2))
binary.Write(cmd, binary.LittleEndian, int8(b.stepsfa0a))
binary.Write(cmd, binary.LittleEndian, int8(2))
binary.Write(cmd, binary.LittleEndian, int8(0))
binary.Write(cmd, binary.LittleEndian, int8(2))
binary.Write(cmd, binary.LittleEndian, int8(0))
binary.Write(cmd, binary.LittleEndian, int8(b.Pcmd.Flag))
binary.Write(cmd, binary.LittleEndian, int8(b.Pcmd.Roll))
binary.Write(cmd, binary.LittleEndian, int8(b.Pcmd.Pitch))
binary.Write(cmd, binary.LittleEndian, int8(b.Pcmd.Yaw))
binary.Write(cmd, binary.LittleEndian, int8(b.Pcmd.Gaz))
binary.Write(cmd, binary.LittleEndian, float32(b.Pcmd.Psi))
binary.Write(cmd, binary.LittleEndian, int16(0))
binary.Write(cmd, binary.LittleEndian, int16(0))
return cmd
}