-
Notifications
You must be signed in to change notification settings - Fork 0
/
minidrone.go
588 lines (482 loc) · 13.1 KB
/
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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package minidrone
import (
"encoding/binary"
"errors"
"fmt"
"math"
"sync"
"time"
"tinygo.org/x/bluetooth"
)
type Minidrone struct {
device *bluetooth.Device
commandService *bluetooth.DeviceService
commandCharacteristic *bluetooth.DeviceCharacteristic
pcmdCharacteristic *bluetooth.DeviceCharacteristic
notificationService *bluetooth.DeviceService
flightStatusCharacteristic *bluetooth.DeviceCharacteristic
buf []byte
stepsfa0a uint16
stepsfa0b uint16
pcmdMutex sync.Mutex
Flying bool
Pcmd Pcmd
pcmddata []byte
shutdown chan bool
pilotingStateHandler func(state, substate int)
}
var (
// BLE services
droneCommandServiceUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfa, 0x00, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
droneNotificationServiceUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfb, 0x00, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
// send characteristics
pcmdCharacteristicUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfa, 0x0a, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
commandCharacteristicUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfa, 0x0b, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
priorityCharacteristicUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfa, 0x0c, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
// receive characteristics
flightStatusCharacteristicUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfb, 0x0e, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
batteryCharacteristicUUID = bluetooth.NewUUID([16]byte{0x9a, 0x66, 0xfb, 0x0f, 0x08, 0x00, 0x91, 0x91, 0x11, 0xe4, 0x01, 0x2d, 0x15, 0x40, 0xcb, 0x8e})
)
const (
// piloting states
PilotingStateFlatTrimChanged = 0
PilotingStateFlyingStateChanged = 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"
// LightFixed mode for LightControl
LightFixed = 0
// LightBlinked mode for LightControl
LightBlinked = 1
// LightOscillated mode for LightControl
LightOscillated = 3
// ClawOpen mode for ClawControl
ClawOpen = 0
// ClawClosed mode for ClawControl
ClawClosed = 1
)
// Pcmd is the Parrot Command structure for flight control
type Pcmd struct {
Flag int
Roll int
Pitch int
Yaw int
Gaz int
Psi float32
}
func NewMinidrone(dev *bluetooth.Device) *Minidrone {
n := &Minidrone{
device: dev,
Pcmd: Pcmd{
Flag: 0,
Roll: 0,
Pitch: 0,
Yaw: 0,
Gaz: 0,
Psi: 0,
},
pcmddata: make([]byte, 19),
shutdown: make(chan bool),
buf: make([]byte, 255),
}
return n
}
func (m *Minidrone) PilotingStateChange(handler func(state, substate int)) {
m.pilotingStateHandler = handler
}
func (m *Minidrone) Start() (err error) {
if debug {
println("drone: Start")
}
srvcs, err := m.device.DiscoverServices([]bluetooth.UUID{
droneCommandServiceUUID,
droneNotificationServiceUUID,
})
switch {
case err != nil:
return err
case len(srvcs) == 0:
return errors.New("could not find drone services")
}
m.commandService = &srvcs[0]
m.notificationService = &srvcs[1]
if debug {
println("found drone command service", m.commandService.UUID().String())
println("found drone notify service", m.notificationService.UUID().String())
}
chars, err := m.commandService.DiscoverCharacteristics([]bluetooth.UUID{
commandCharacteristicUUID,
pcmdCharacteristicUUID,
})
switch {
case err != nil:
return err
case len(chars) == 0:
return errors.New("could not find drone command characteristics")
}
if debug {
println("found drone command characteristics", chars[0].UUID().String(), chars[1].UUID().String())
}
m.commandCharacteristic = &chars[0]
m.pcmdCharacteristic = &chars[1]
chars, err = m.notificationService.DiscoverCharacteristics([]bluetooth.UUID{
flightStatusCharacteristicUUID,
})
switch {
case err != nil:
return err
case len(chars) == 0:
return errors.New("could not find drone notify characteristics")
}
if debug {
println("found drone notify characteristics", chars[0].UUID().String())
}
m.flightStatusCharacteristic = &chars[0]
err = m.Init()
if err != nil {
if debug {
println("init error", err.Error())
}
return err
}
if debug {
println("drone init complete")
}
m.FlatTrim()
m.StartPcmd()
m.FlatTrim()
return
}
// Halt stops minidrone driver (void)
func (m *Minidrone) Halt() (err error) {
m.Land()
m.shutdown <- true
time.Sleep(500 * time.Millisecond)
return
}
// Init initializes the BLE insterfaces used by the Minidrone
func (m *Minidrone) Init() (err error) {
if debug {
println("init")
}
err = m.GenerateAllStates()
if err != nil {
println(err.Error())
return
}
if debug {
println("enabling pcmd notifications")
}
// if you do not enable these notifications, then you cannot send commands to the drone.
err = m.flightStatusCharacteristic.EnableNotifications(func(buf []byte) {
m.processFlightStatus(buf)
})
// TODO: subscribe to battery notifications
return
}
func (m *Minidrone) Disconnect() {
m.device.Disconnect()
}
// GenerateAllStates sets up all the default states aka settings on the drone
func (m *Minidrone) GenerateAllStates() (err error) {
m.stepsfa0b++
buf := []byte{0x04, byte(m.stepsfa0b) & 0xff, 0x00, 0x04, 0x01, 0x00, 0x32, 0x30, 0x31, 0x34, 0x2D, 0x31, 0x30, 0x2D, 0x32, 0x38, 0x00}
_, err = m.commandCharacteristic.WriteWithoutResponse(buf)
return err
}
// TakeOff tells the Minidrone to takeoff
func (m *Minidrone) TakeOff() (err error) {
m.stepsfa0b++
buf := []byte{0x02, byte(m.stepsfa0b) & 0xff, 0x02, 0x00, 0x01, 0x00}
_, err = m.commandCharacteristic.WriteWithoutResponse(buf)
return err
}
// Land tells the Minidrone to land
func (m *Minidrone) Land() (err error) {
m.stepsfa0b++
buf := []byte{0x02, byte(m.stepsfa0b) & 0xff, 0x02, 0x00, 0x03, 0x00}
_, err = m.commandCharacteristic.WriteWithoutResponse(buf)
return err
}
// FlatTrim calibrates the Minidrone to use its current position as being level
func (m *Minidrone) FlatTrim() (err error) {
m.stepsfa0b++
buf := []byte{0x02, byte(m.stepsfa0b) & 0xff, 0x02, 0x00, 0x00, 0x00}
_, err = m.commandCharacteristic.WriteWithoutResponse(buf)
return err
}
// Emergency sets the Minidrone into emergency mode
func (m *Minidrone) Emergency() (err error) {
m.stepsfa0b++
buf := []byte{0x02, byte(m.stepsfa0b) & 0xff, 0x02, 0x00, 0x04, 0x00}
_, err = m.commandCharacteristic.WriteWithoutResponse(buf)
return err
}
// StartPcmd starts the continuous Pcmd communication with the Minidrone
func (m *Minidrone) StartPcmd() {
go func() {
// wait a little bit so that there is enough time to get some ACKs
time.Sleep(500 * time.Millisecond)
for {
select {
case <-m.shutdown:
return
default:
}
m.generatePcmd()
_, err := m.pcmdCharacteristic.WriteWithoutResponse(m.pcmddata)
if err != nil {
fmt.Println("pcmd write error:", err)
}
time.Sleep(50 * time.Millisecond)
}
}()
}
// Up tells the drone to ascend. Pass in an int from 0-100.
func (m *Minidrone) Up(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Gaz = validatePitch(val)
return nil
}
// Down tells the drone to descend. Pass in an int from 0-100.
func (m *Minidrone) Down(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Gaz = validatePitch(val) * -1
return nil
}
// Forward tells the drone to go forward. Pass in an int from 0-100.
func (m *Minidrone) Forward(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Pitch = validatePitch(val)
return nil
}
// Backward tells drone to go in reverse. Pass in an int from 0-100.
func (m *Minidrone) Backward(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Pitch = validatePitch(val) * -1
return nil
}
// Right tells drone to go right. Pass in an int from 0-100.
func (m *Minidrone) Right(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Roll = validatePitch(val)
return nil
}
// Left tells drone to go left. Pass in an int from 0-100.
func (m *Minidrone) Left(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Roll = validatePitch(val) * -1
return nil
}
// Clockwise tells drone to rotate in a clockwise direction. Pass in an int from 0-100.
func (m *Minidrone) Clockwise(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Yaw = validatePitch(val)
return nil
}
// CounterClockwise tells drone to rotate in a counter-clockwise direction.
// Pass in an int from 0-100.
func (m *Minidrone) CounterClockwise(val int) error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd.Flag = 1
m.Pcmd.Yaw = validatePitch(val) * -1
return nil
}
// Hover tells the drone to stop moving in any direction and simply hover in place
func (m *Minidrone) Hover() error {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.Pcmd = Pcmd{
Flag: 0,
Roll: 0,
Pitch: 0,
Yaw: 0,
Gaz: 0,
Psi: 0,
}
return nil
}
// FrontFlip tells the drone to perform a front flip
func (m *Minidrone) FrontFlip() error {
_, err := m.commandCharacteristic.WriteWithoutResponse(m.generateAnimation(0))
return err
}
// BackFlip tells the drone to perform a backflip
func (m *Minidrone) BackFlip() error {
_, err := m.commandCharacteristic.WriteWithoutResponse(m.generateAnimation(1))
return err
}
// RightFlip tells the drone to perform a flip to the right
func (m *Minidrone) RightFlip() error {
_, err := m.commandCharacteristic.WriteWithoutResponse(m.generateAnimation(2))
return err
}
// LeftFlip tells the drone to perform a flip to the left
func (m *Minidrone) LeftFlip() error {
_, err := m.commandCharacteristic.WriteWithoutResponse(m.generateAnimation(3))
return err
}
func (m *Minidrone) generateAnimation(anim int) []byte {
m.stepsfa0b++
return []byte{0x02, byte(m.stepsfa0b) & 0xff, 0x02, 0x04, 0x00, 0x00, byte(anim), 0x00, 0x00, 0x00}
}
func FlyingState(state int) string {
switch state {
case FlyingStateLanded:
return Landed
case FlyingStateTakeoff:
return Takeoff
case FlyingStateHovering:
return Hovering
case FlyingStateFlying:
return Flying
case FlyingStateLanding:
return Landing
case FlyingStateEmergency:
return Emergency
case FlyingStateRolling:
return Rolling
}
return "unknown"
}
func (m *Minidrone) generatePcmd() {
m.pcmdMutex.Lock()
defer m.pcmdMutex.Unlock()
m.stepsfa0a++
m.pcmddata[0] = 0x02
m.pcmddata[1] = byte(m.stepsfa0a)
m.pcmddata[2] = 0x02
m.pcmddata[3] = 0x00
m.pcmddata[4] = 0x02
m.pcmddata[5] = 0x00
m.pcmddata[6] = byte(m.Pcmd.Flag)
m.pcmddata[7] = byte(m.Pcmd.Roll)
m.pcmddata[8] = byte(m.Pcmd.Pitch)
m.pcmddata[9] = byte(m.Pcmd.Yaw)
m.pcmddata[10] = byte(m.Pcmd.Gaz)
binary.LittleEndian.PutUint32(m.buf[11:], math.Float32bits(m.Pcmd.Psi))
m.pcmddata[15] = 0x00
m.pcmddata[16] = 0x00
m.pcmddata[17] = 0x00
m.pcmddata[18] = 0x00
return
}
func (m *Minidrone) processFlightStatus(data []byte) {
if len(data) < 5 {
// ignore, just a sync
return
}
switch data[4] {
case PilotingStateFlatTrimChanged:
if debug {
println("flatTrimChanged")
}
if m.pilotingStateHandler != nil {
m.pilotingStateHandler(int(data[4]), 0)
}
case PilotingStateFlyingStateChanged:
switch data[6] {
case FlyingStateLanded:
if m.Flying {
m.Flying = false
if debug {
println("flyingStateLanded")
}
}
case FlyingStateTakeoff:
if debug {
println("flyingStateTakeoff")
}
case FlyingStateHovering:
if !m.Flying {
m.Flying = true
if debug {
println("flyingStateHovering")
}
}
case FlyingStateFlying:
if !m.Flying {
m.Flying = true
if debug {
println("flyingStateFlying")
}
}
case FlyingStateLanding:
if debug {
println("flyingStateLanding")
}
case FlyingStateEmergency:
if debug {
println("flyingStateEmergency")
}
case FlyingStateRolling:
if debug {
println("flyingStateRolling")
}
}
if m.pilotingStateHandler != nil {
m.pilotingStateHandler(int(data[4]), int(data[6]))
}
}
}
// ValidatePitch helps validate pitch values such as those created by
// a joystick to values between 0-100 that are required as
// params to Parrot Minidrone PCMDs
func ValidatePitch(data float64, offset float64) int {
value := math.Abs(data) / offset
if value >= 0.1 {
if value <= 1.0 {
return int((float64(int(value*100)) / 100) * 100)
}
return 100
}
return 0
}
func validatePitch(val int) int {
if val > 100 {
return 100
} else if val < 0 {
return 0
}
return val
}