/
minidrone_driver.go
430 lines (356 loc) · 10.1 KB
/
minidrone_driver.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package minidrone
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
)
// Driver is the Gobot interface to the Parrot Minidrone
type Driver struct {
name string
connection gobot.Connection
stepsfa0a uint16
stepsfa0b uint16
flying bool
Pcmd Pcmd
gobot.Eventer
}
const (
// BLE services
droneCommandService = "9a66fa000800919111e4012d1540cb8e"
droneNotificationService = "9a66fb000800919111e4012d1540cb8e"
// send characteristics
pcmdCharacteristic = "9a66fa0a0800919111e4012d1540cb8e"
commandCharacteristic = "9a66fa0b0800919111e4012d1540cb8e"
priorityCharacteristic = "9a66fa0c0800919111e4012d1540cb8e"
// receive characteristics
flightStatusCharacteristic = "9a66fb0e0800919111e4012d1540cb8e"
batteryCharacteristic = "9a66fb0f0800919111e4012d1540cb8e"
// piloting states
flatTrimChanged = 0
flyingStateChanged = 1
// flying states
flyingStateLanded = 0
flyingStateTakeoff = 1
flyingStateHovering = 2
flyingStateFlying = 3
flyingStateLanding = 4
flyingStateEmergency = 5
flyingStateRolling = 6
// Battery event
Battery = "battery"
// FlightStatus event
FlightStatus = "flightstatus"
// Takeoff event
Takeoff = "takeoff"
// Hovering event
Hovering = "hovering"
// Flying event
Flying = "flying"
// Landing event
Landing = "landing"
// Landed event
Landed = "landed"
// Emergency event
Emergency = "emergency"
// Rolling event
Rolling = "rolling"
// FlatTrimChange event
FlatTrimChange = "flattrimchange"
)
// Pcmd is the Parrot Command structure for flight control
type Pcmd struct {
Flag int
Roll int
Pitch int
Yaw int
Gaz int
Psi float32
}
// NewDriver creates a Parrot Minidrone Driver
func NewDriver(a *ble.ClientAdaptor) *Driver {
n := &Driver{
name: gobot.DefaultName("Minidrone"),
connection: a,
Pcmd: Pcmd{
Flag: 0,
Roll: 0,
Pitch: 0,
Yaw: 0,
Gaz: 0,
Psi: 0,
},
Eventer: gobot.NewEventer(),
}
n.AddEvent(Battery)
n.AddEvent(FlightStatus)
n.AddEvent(Takeoff)
n.AddEvent(Flying)
n.AddEvent(Hovering)
n.AddEvent(Landing)
n.AddEvent(Landed)
n.AddEvent(Emergency)
n.AddEvent(Rolling)
return n
}
// Connection returns the BLE connection
func (b *Driver) Connection() gobot.Connection { return b.connection }
// Name returns the Driver Name
func (b *Driver) Name() string { return b.name }
// SetName sets the Driver Name
func (b *Driver) SetName(n string) { b.name = n }
// adaptor returns BLE adaptor
func (b *Driver) adaptor() *ble.ClientAdaptor {
return b.Connection().(*ble.ClientAdaptor)
}
// Start tells driver to get ready to do work
func (b *Driver) Start() (err error) {
b.Init()
b.FlatTrim()
b.StartPcmd()
b.FlatTrim()
return
}
// Halt stops minidrone driver (void)
func (b *Driver) Halt() (err error) {
b.Land()
time.Sleep(500 * time.Millisecond)
return
}
// Init initializes the BLE insterfaces used by the Minidrone
func (b *Driver) Init() (err error) {
b.GenerateAllStates()
// subscribe to battery notifications
b.adaptor().Subscribe(batteryCharacteristic, func(data []byte, e error) {
b.Publish(b.Event(Battery), data[len(data)-1])
})
// subscribe to flying status notifications
b.adaptor().Subscribe(flightStatusCharacteristic, func(data []byte, e error) {
if len(data) < 5 {
// ignore, just a sync
return
}
b.Publish(FlightStatus, data[4])
if data[4] == flatTrimChanged {
b.Publish(FlatTrimChange, true)
}
if data[4] == flyingStateChanged {
switch data[6] {
case flyingStateLanded:
if b.flying {
b.flying = false
b.Publish(Landed, true)
}
case flyingStateTakeoff:
b.Publish(Takeoff, true)
case flyingStateHovering:
if !b.flying {
b.flying = true
b.Publish(Hovering, true)
}
case flyingStateFlying:
if !b.flying {
b.flying = true
b.Publish(Flying, true)
}
case flyingStateLanding:
b.Publish(Landing, true)
case flyingStateEmergency:
b.Publish(Emergency, true)
case flyingStateRolling:
b.Publish(Rolling, true)
}
}
})
return
}
// GenerateAllStates sets up all the default states aka settings on the drone
func (b *Driver) 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(commandCharacteristic, buf)
if err != nil {
fmt.Println("GenerateAllStates error:", err)
return err
}
return
}
// TakeOff tells the Minidrone to takeoff
func (b *Driver) TakeOff() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x01, 0x00}
err = b.adaptor().WriteCharacteristic(commandCharacteristic, buf)
if err != nil {
fmt.Println("takeoff error:", err)
return err
}
return
}
// Land tells the Minidrone to land
func (b *Driver) Land() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x03, 0x00}
err = b.adaptor().WriteCharacteristic(commandCharacteristic, buf)
return err
}
// FlatTrim calibrates the Minidrone to use its current position as being level
func (b *Driver) FlatTrim() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x00, 0x00}
err = b.adaptor().WriteCharacteristic(commandCharacteristic, buf)
return err
}
// Emergency sets the Minidrone into emergency mode
func (b *Driver) Emergency() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x04, 0x00}
err = b.adaptor().WriteCharacteristic(priorityCharacteristic, buf)
return err
}
// TakePicture tells the Minidrone to take a picture
func (b *Driver) TakePicture() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x06, 0x01, 0x00}
err = b.adaptor().WriteCharacteristic(commandCharacteristic, buf)
return err
}
// StartPcmd starts the continuous Pcmd communication with the Minidrone
func (b *Driver) StartPcmd() {
go func() {
// wait a little bit so that there is enough time to get some ACKs
time.Sleep(500 * time.Millisecond)
for {
err := b.adaptor().WriteCharacteristic(pcmdCharacteristic, b.generatePcmd().Bytes())
if err != nil {
fmt.Println("pcmd write error:", err)
}
time.Sleep(50 * time.Millisecond)
}
}()
}
// Up tells the drone to ascend
func (b *Driver) Up(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Gaz = validatePitch(val)
return nil
}
// Down tells the drone to descend
func (b *Driver) Down(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Gaz = validatePitch(val) * -1
return nil
}
// Forward tells the drone to go forward
func (b *Driver) Forward(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Pitch = validatePitch(val)
return nil
}
// Backward tells drone to go in reverse
func (b *Driver) Backward(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Pitch = validatePitch(val) * -1
return nil
}
// Right tells drone to go right
func (b *Driver) Right(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Roll = validatePitch(val)
return nil
}
// Left tells drone to go left
func (b *Driver) Left(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Roll = validatePitch(val) * -1
return nil
}
// Clockwise tells drone to rotate in a clockwise direction
func (b *Driver) Clockwise(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Yaw = validatePitch(val)
return nil
}
// CounterClockwise tells drone to rotate in a counter-clockwise direction
func (b *Driver) CounterClockwise(val int) error {
b.Pcmd.Flag = 1
b.Pcmd.Yaw = validatePitch(val) * -1
return nil
}
// Stop tells the drone to stop moving in any direction and simply hover in place
func (b *Driver) Stop() error {
b.Pcmd = Pcmd{
Flag: 0,
Roll: 0,
Pitch: 0,
Yaw: 0,
Gaz: 0,
Psi: 0,
}
return nil
}
// StartRecording is not supported by the Parrot Minidrone
func (b *Driver) StartRecording() error {
return nil
}
// StopRecording is not supported by the Parrot Minidrone
func (b *Driver) StopRecording() error {
return nil
}
// HullProtection is not supported by the Parrot Minidrone
func (b *Driver) HullProtection(protect bool) error {
return nil
}
// Outdoor mdoe is not supported by the Parrot Minidrone
func (b *Driver) Outdoor(outdoor bool) error {
return nil
}
// FrontFlip tells the drone to perform a front flip
func (b *Driver) FrontFlip() (err error) {
return b.adaptor().WriteCharacteristic(commandCharacteristic, b.generateAnimation(0).Bytes())
}
// BackFlip tells the drone to perform a backflip
func (b *Driver) BackFlip() (err error) {
return b.adaptor().WriteCharacteristic(commandCharacteristic, b.generateAnimation(1).Bytes())
}
// RightFlip tells the drone to perform a flip to the right
func (b *Driver) RightFlip() (err error) {
return b.adaptor().WriteCharacteristic(commandCharacteristic, b.generateAnimation(2).Bytes())
}
// LeftFlip tells the drone to perform a flip to the left
func (b *Driver) LeftFlip() (err error) {
return b.adaptor().WriteCharacteristic(commandCharacteristic, b.generateAnimation(3).Bytes())
}
func (b *Driver) 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 *Driver) 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
}
func validatePitch(val int) int {
if val > 100 {
return 100
} else if val < 0 {
return 0
}
return val
}