forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
1001 lines (834 loc) · 24.7 KB
/
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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tello
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"net"
"strconv"
"sync"
"time"
"gobot.io/x/gobot"
)
const (
// BounceEvent event
BounceEvent = "bounce"
// ConnectedEvent event
ConnectedEvent = "connected"
// FlightDataEvent event
FlightDataEvent = "flightdata"
// TakeoffEvent event
TakeoffEvent = "takeoff"
// LandingEvent event
LandingEvent = "landing"
// PalmLandingEvent event
PalmLandingEvent = "palm-landing"
// FlipEvent event
FlipEvent = "flip"
// TimeEvent event
TimeEvent = "time"
// LogEvent event
LogEvent = "log"
// WifiDataEvent event
WifiDataEvent = "wifidata"
// LightStrengthEvent event
LightStrengthEvent = "lightstrength"
// SetExposureEvent event
SetExposureEvent = "setexposure"
// VideoFrameEvent event
VideoFrameEvent = "videoframe"
// SetVideoEncoderRateEvent event
SetVideoEncoderRateEvent = "setvideoencoder"
)
// the 16-bit messages and commands stored in bytes 6 & 5 of the packet
const (
messageStart = 0x00cc // 204
wifiMessage = 0x001a // 26
videoRateQuery = 0x0028 // 40
lightMessage = 0x0035 // 53
flightMessage = 0x0056 // 86
logMessage = 0x1050 // 4176
videoEncoderRateCommand = 0x0020 // 32
videoStartCommand = 0x0025 // 37
exposureCommand = 0x0034 // 52
timeCommand = 0x0046 // 70
stickCommand = 0x0050 // 80
takeoffCommand = 0x0054 // 84
landCommand = 0x0055 // 85
flipCommand = 0x005c // 92
throwtakeoffCommand = 0x005d // 93
palmLandCommand = 0x005e // 94
bounceCommand = 0x1053 // 4179
)
// FlipType is used for the various flips supported by the Tello.
type FlipType int
const (
// FlipFront flips forward.
FlipFront FlipType = 0
// FlipLeft flips left.
FlipLeft FlipType = 1
// FlipBack flips backwards.
FlipBack FlipType = 2
// FlipRight flips to the right.
FlipRight FlipType = 3
// FlipForwardLeft flips forwards and to the left.
FlipForwardLeft FlipType = 4
// FlipBackLeft flips backwards and to the left.
FlipBackLeft FlipType = 5
// FlipBackRight flips backwards and to the right.
FlipBackRight FlipType = 6
// FlipForwardRight flips forewards and to the right.
FlipForwardRight FlipType = 7
)
// VideoBitRate is used to set the bit rate for the streaming video returned by the Tello.
type VideoBitRate int
const (
// VideoBitRateAuto sets the bitrate for streaming video to auto-adjust.
VideoBitRateAuto VideoBitRate = 0
// VideoBitRate1M sets the bitrate for streaming video to 1 Mb/s.
VideoBitRate1M VideoBitRate = 1
// VideoBitRate15M sets the bitrate for streaming video to 1.5 Mb/s
VideoBitRate15M VideoBitRate = 2
// VideoBitRate2M sets the bitrate for streaming video to 2 Mb/s.
VideoBitRate2M VideoBitRate = 3
// VideoBitRate3M sets the bitrate for streaming video to 3 Mb/s.
VideoBitRate3M VideoBitRate = 4
// VideoBitRate4M sets the bitrate for streaming video to 4 Mb/s.
VideoBitRate4M VideoBitRate = 5
)
// FlightData packet returned by the Tello
type FlightData struct {
BatteryLow bool
BatteryLower bool
BatteryPercentage int8
BatteryState bool
CameraState int8
DownVisualState bool
DroneBatteryLeft int16
DroneFlyTimeLeft int16
DroneHover bool
EmOpen bool
Flying bool
OnGround bool
EastSpeed int16
ElectricalMachineryState int16
FactoryMode bool
FlyMode int8
FlyTime int16
FrontIn bool
FrontLSC bool
FrontOut bool
GravityState bool
VerticalSpeed int16
Height int16
ImuCalibrationState int8
ImuState bool
LightStrength int8
NorthSpeed int16
OutageRecording bool
PowerState bool
PressureState bool
SmartVideoExitMode int16
TemperatureHigh bool
ThrowFlyTimer int8
WindState bool
}
// WifiData packet returned by the Tello
type WifiData struct {
Disturb int8
Strength int8
}
// Driver represents the DJI Tello drone
type Driver struct {
name string
reqAddr string
cmdConn io.WriteCloser // UDP connection to send/receive drone commands
videoConn *net.UDPConn // UDP connection for drone video
respPort string
videoPort string
cmdMutex sync.Mutex
seq int16
rx, ry, lx, ly float32
throttle int
bouncing bool
gobot.Eventer
doneCh chan struct{}
}
// NewDriver creates a driver for the Tello drone. Pass in the UDP port to use for the responses
// from the drone.
func NewDriver(port string) *Driver {
d := &Driver{name: gobot.DefaultName("Tello"),
reqAddr: "192.168.10.1:8889",
respPort: port,
videoPort: "11111",
Eventer: gobot.NewEventer(),
doneCh: make(chan struct{}, 1),
}
d.AddEvent(ConnectedEvent)
d.AddEvent(FlightDataEvent)
d.AddEvent(TakeoffEvent)
d.AddEvent(LandingEvent)
d.AddEvent(PalmLandingEvent)
d.AddEvent(BounceEvent)
d.AddEvent(FlipEvent)
d.AddEvent(TimeEvent)
d.AddEvent(LogEvent)
d.AddEvent(WifiDataEvent)
d.AddEvent(LightStrengthEvent)
d.AddEvent(SetExposureEvent)
d.AddEvent(VideoFrameEvent)
d.AddEvent(SetVideoEncoderRateEvent)
return d
}
// NewDriverWithIP creates a driver for the Tello EDU drone. Pass in the ip address and UDP port to use for the responses
// from the drone.
func NewDriverWithIP(ip string, port string) *Driver {
d := &Driver{name: gobot.DefaultName("Tello"),
reqAddr: ip + ":8889",
respPort: port,
videoPort: "11111",
Eventer: gobot.NewEventer(),
}
d.AddEvent(ConnectedEvent)
d.AddEvent(FlightDataEvent)
d.AddEvent(TakeoffEvent)
d.AddEvent(LandingEvent)
d.AddEvent(PalmLandingEvent)
d.AddEvent(BounceEvent)
d.AddEvent(FlipEvent)
d.AddEvent(TimeEvent)
d.AddEvent(LogEvent)
d.AddEvent(WifiDataEvent)
d.AddEvent(LightStrengthEvent)
d.AddEvent(SetExposureEvent)
d.AddEvent(VideoFrameEvent)
d.AddEvent(SetVideoEncoderRateEvent)
return d
}
// Name returns the name of the device.
func (d *Driver) Name() string { return d.name }
// SetName sets the name of the device.
func (d *Driver) SetName(n string) { d.name = n }
// Connection returns the Connection of the device.
func (d *Driver) Connection() gobot.Connection { return nil }
// Start starts the driver.
func (d *Driver) Start() error {
reqAddr, err := net.ResolveUDPAddr("udp", d.reqAddr)
if err != nil {
fmt.Println(err)
return err
}
respPort, err := net.ResolveUDPAddr("udp", ":"+d.respPort)
if err != nil {
fmt.Println(err)
return err
}
cmdConn, err := net.DialUDP("udp", respPort, reqAddr)
if err != nil {
fmt.Println(err)
return err
}
d.cmdConn = cmdConn
// handle responses
go func() {
d.On(d.Event(ConnectedEvent), func(interface{}) {
d.SendDateTime()
d.processVideo()
})
cmdLoop:
for {
select {
case <-d.doneCh:
break cmdLoop
default:
err := d.handleResponse(cmdConn)
if err != nil {
fmt.Println("response parse error:", err)
}
}
}
}()
// starts notifications coming from drone to video port normally 11111
d.SendCommand(d.connectionString())
// send stick commands
go func() {
for {
err := d.SendStickCommand()
if err != nil {
fmt.Println("stick command error:", err)
}
time.Sleep(20 * time.Millisecond)
}
}()
return nil
}
// Halt stops the driver.
func (d *Driver) Halt() (err error) {
// send a landing command when we disconnect, and give it 500ms to be received before we shutdown
d.Land()
d.doneCh <- struct{}{}
time.Sleep(500 * time.Millisecond)
d.cmdConn.Close()
d.videoConn.Close()
return
}
// TakeOff tells drones to liftoff and start flying.
func (d *Driver) TakeOff() (err error) {
buf, _ := d.createPacket(takeoffCommand, 0x68, 0)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// Throw & Go support
func (d *Driver) ThrowTakeOff() (err error) {
buf, _ := d.createPacket(throwtakeoffCommand, 0x48, 0)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// Land tells drone to come in for landing.
func (d *Driver) Land() (err error) {
buf, _ := d.createPacket(landCommand, 0x68, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, byte(0x00))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// StopLanding tells drone to stop landing.
func (d *Driver) StopLanding() (err error) {
buf, _ := d.createPacket(landCommand, 0x68, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, byte(0x01))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// PalmLand tells drone to come in for a hand landing.
func (d *Driver) PalmLand() (err error) {
buf, _ := d.createPacket(palmLandCommand, 0x68, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, byte(0x00))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// StartVideo tells Tello to send start info (SPS/PPS) for video stream.
func (d *Driver) StartVideo() (err error) {
buf, _ := d.createPacket(videoStartCommand, 0x60, 0)
binary.Write(buf, binary.LittleEndian, int16(0x00)) // seq = 0
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// SetExposure sets the drone camera exposure level. Valid levels are 0, 1, and 2.
func (d *Driver) SetExposure(level int) (err error) {
if level < 0 || level > 2 {
return errors.New("Invalid exposure level")
}
buf, _ := d.createPacket(exposureCommand, 0x48, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, byte(level))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// SetVideoEncoderRate sets the drone video encoder rate.
func (d *Driver) SetVideoEncoderRate(rate VideoBitRate) (err error) {
buf, _ := d.createPacket(videoEncoderRateCommand, 0x68, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, byte(rate))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// SetFastMode sets the drone throttle to 1.
func (d *Driver) SetFastMode() error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.throttle = 1
return nil
}
// SetSlowMode sets the drone throttle to 0.
func (d *Driver) SetSlowMode() error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.throttle = 0
return nil
}
// Rate queries the current video bit rate.
func (d *Driver) Rate() (err error) {
buf, _ := d.createPacket(videoRateQuery, 0x48, 0)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// bound is a naive implementation that returns the smaller of x or y.
func bound(x, y float32) float32 {
if x < -y {
return -y
}
if x > y {
return y
}
return x
}
// Vector returns the current motion vector.
// Values are from 0 to 1.
// x, y, z denote forward, side and vertical translation,
// and psi yaw (rotation around the z-axis).
func (d *Driver) Vector() (x, y, z, psi float32) {
return d.ry, d.rx, d.ly, d.lx
}
// AddVector adds to the current motion vector.
// Pass values from 0 to 1.
// See Vector() for the frame of reference.
func (d *Driver) AddVector(x, y, z, psi float32) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ry = bound(d.ry+x, 1)
d.rx = bound(d.rx+y, 1)
d.ly = bound(d.ly+z, 1)
d.lx = bound(d.lx+psi, 1)
return nil
}
// SetVector sets the current motion vector.
// Pass values from 0 to 1.
// See Vector() for the frame of reference.
func (d *Driver) SetVector(x, y, z, psi float32) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ry = x
d.rx = y
d.ly = z
d.lx = psi
return nil
}
// SetX sets the x component of the current motion vector
// Pass values from 0 to 1.
// See Vector() for the frame of reference.
func (d *Driver) SetX(x float32) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ry = x
return nil
}
// SetY sets the y component of the current motion vector
// Pass values from 0 to 1.
// See Vector() for the frame of reference.
func (d *Driver) SetY(y float32) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.rx = y
return nil
}
// SetZ sets the z component of the current motion vector
// Pass values from 0 to 1.
// See Vector() for the frame of reference.
func (d *Driver) SetZ(z float32) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ly = z
return nil
}
// SetPsi sets the psi component (yaw) of the current motion vector
// Pass values from 0 to 1.
// See Vector() for the frame of reference.
func (d *Driver) SetPsi(psi float32) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.lx = psi
return nil
}
// Up tells the drone to ascend. Pass in an int from 0-100.
func (d *Driver) Up(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ly = float32(val) / 100.0
return nil
}
// Down tells the drone to descend. Pass in an int from 0-100.
func (d *Driver) Down(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ly = float32(val) / 100.0 * -1
return nil
}
// Forward tells the drone to go forward. Pass in an int from 0-100.
func (d *Driver) Forward(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ry = float32(val) / 100.0
return nil
}
// Backward tells drone to go in reverse. Pass in an int from 0-100.
func (d *Driver) Backward(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.ry = float32(val) / 100.0 * -1
return nil
}
// Right tells drone to go right. Pass in an int from 0-100.
func (d *Driver) Right(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.rx = float32(val) / 100.0
return nil
}
// Left tells drone to go left. Pass in an int from 0-100.
func (d *Driver) Left(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.rx = float32(val) / 100.0 * -1
return nil
}
// Clockwise tells drone to rotate in a clockwise direction. Pass in an int from 0-100.
func (d *Driver) Clockwise(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.lx = float32(val) / 100.0
return nil
}
// CounterClockwise tells drone to rotate in a counter-clockwise direction.
// Pass in an int from 0-100.
func (d *Driver) CounterClockwise(val int) error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.lx = float32(val) / 100.0 * -1
return nil
}
// Hover tells the drone to stop moving on the X, Y, and Z axes and stay in place
func (d *Driver) Hover() {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.rx = float32(0)
d.ry = float32(0)
d.lx = float32(0)
d.ly = float32(0)
}
// CeaseRotation stops any rotational motion
func (d *Driver) CeaseRotation() {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
d.lx = float32(0)
}
// Bounce tells drone to start/stop performing the bouncing action
func (d *Driver) Bounce() (err error) {
buf, _ := d.createPacket(bounceCommand, 0x68, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
if d.bouncing {
binary.Write(buf, binary.LittleEndian, byte(0x31))
} else {
binary.Write(buf, binary.LittleEndian, byte(0x30))
}
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
d.bouncing = !d.bouncing
return
}
// Flip tells drone to flip
func (d *Driver) Flip(direction FlipType) (err error) {
buf, _ := d.createPacket(flipCommand, 0x70, 1)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, byte(direction))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// FrontFlip tells the drone to perform a front flip.
func (d *Driver) FrontFlip() (err error) {
return d.Flip(FlipFront)
}
// BackFlip tells the drone to perform a back flip.
func (d *Driver) BackFlip() (err error) {
return d.Flip(FlipBack)
}
// RightFlip tells the drone to perform a flip to the right.
func (d *Driver) RightFlip() (err error) {
return d.Flip(FlipRight)
}
// LeftFlip tells the drone to perform a flip to the left.
func (d *Driver) LeftFlip() (err error) {
return d.Flip(FlipLeft)
}
// ParseFlightData from drone
func (d *Driver) ParseFlightData(b []byte) (fd *FlightData, err error) {
buf := bytes.NewReader(b)
fd = &FlightData{}
var data byte
if buf.Len() < 24 {
err = errors.New("Invalid buffer length for flight data packet")
fmt.Println(err)
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.Height)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.NorthSpeed)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.EastSpeed)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.VerticalSpeed)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.FlyTime)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &data)
if err != nil {
return
}
fd.ImuState = (data >> 0 & 0x1) == 1
fd.PressureState = (data >> 1 & 0x1) == 1
fd.DownVisualState = (data >> 2 & 0x1) == 1
fd.PowerState = (data >> 3 & 0x1) == 1
fd.BatteryState = (data >> 4 & 0x1) == 1
fd.GravityState = (data >> 5 & 0x1) == 1
fd.WindState = (data >> 7 & 0x1) == 1
err = binary.Read(buf, binary.LittleEndian, &fd.ImuCalibrationState)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.BatteryPercentage)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.DroneFlyTimeLeft)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.DroneBatteryLeft)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &data)
if err != nil {
return
}
fd.Flying = (data >> 0 & 0x1) == 1
fd.OnGround = (data >> 1 & 0x1) == 1
fd.EmOpen = (data >> 2 & 0x1) == 1
fd.DroneHover = (data >> 3 & 0x1) == 1
fd.OutageRecording = (data >> 4 & 0x1) == 1
fd.BatteryLow = (data >> 5 & 0x1) == 1
fd.BatteryLower = (data >> 6 & 0x1) == 1
fd.FactoryMode = (data >> 7 & 0x1) == 1
err = binary.Read(buf, binary.LittleEndian, &fd.FlyMode)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.ThrowFlyTimer)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &fd.CameraState)
if err != nil {
return
}
err = binary.Read(buf, binary.LittleEndian, &data)
if err != nil {
return
}
fd.ElectricalMachineryState = int16(data & 0xff)
err = binary.Read(buf, binary.LittleEndian, &data)
if err != nil {
return
}
fd.FrontIn = (data >> 0 & 0x1) == 1
fd.FrontOut = (data >> 1 & 0x1) == 1
fd.FrontLSC = (data >> 2 & 0x1) == 1
err = binary.Read(buf, binary.LittleEndian, &data)
if err != nil {
return
}
fd.TemperatureHigh = (data >> 0 & 0x1) == 1
return
}
// SendStickCommand sends the joystick command packet to the drone.
func (d *Driver) SendStickCommand() (err error) {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
buf, _ := d.createPacket(stickCommand, 0x60, 11)
binary.Write(buf, binary.LittleEndian, int16(0x00)) // seq = 0
// RightX center=1024 left =364 right =-364
axis1 := int16(660.0*d.rx + 1024.0)
// RightY down =364 up =-364
axis2 := int16(660.0*d.ry + 1024.0)
// LeftY down =364 up =-364
axis3 := int16(660.0*d.ly + 1024.0)
// LeftX left =364 right =-364
axis4 := int16(660.0*d.lx + 1024.0)
// speed control
axis5 := int16(d.throttle)
packedAxis := int64(axis1)&0x7FF | int64(axis2&0x7FF)<<11 | 0x7FF&int64(axis3)<<22 | 0x7FF&int64(axis4)<<33 | int64(axis5)<<44
binary.Write(buf, binary.LittleEndian, byte(0xFF&packedAxis))
binary.Write(buf, binary.LittleEndian, byte(packedAxis>>8&0xFF))
binary.Write(buf, binary.LittleEndian, byte(packedAxis>>16&0xFF))
binary.Write(buf, binary.LittleEndian, byte(packedAxis>>24&0xFF))
binary.Write(buf, binary.LittleEndian, byte(packedAxis>>32&0xFF))
binary.Write(buf, binary.LittleEndian, byte(packedAxis>>40&0xFF))
now := time.Now()
binary.Write(buf, binary.LittleEndian, byte(now.Hour()))
binary.Write(buf, binary.LittleEndian, byte(now.Minute()))
binary.Write(buf, binary.LittleEndian, byte(now.Second()))
binary.Write(buf, binary.LittleEndian, byte(now.UnixNano()/int64(time.Millisecond)&0xff))
binary.Write(buf, binary.LittleEndian, byte(now.UnixNano()/int64(time.Millisecond)>>8))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// SendDateTime sends the current date/time to the drone.
func (d *Driver) SendDateTime() (err error) {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
buf, _ := d.createPacket(timeCommand, 0x50, 11)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
now := time.Now()
binary.Write(buf, binary.LittleEndian, byte(0x00))
binary.Write(buf, binary.LittleEndian, int16(now.Hour()))
binary.Write(buf, binary.LittleEndian, int16(now.Minute()))
binary.Write(buf, binary.LittleEndian, int16(now.Second()))
binary.Write(buf, binary.LittleEndian, int16(now.UnixNano()/int64(time.Millisecond)&0xff))
binary.Write(buf, binary.LittleEndian, int16(now.UnixNano()/int64(time.Millisecond)>>8))
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))
_, err = d.cmdConn.Write(buf.Bytes())
return
}
// SendCommand is used to send a text command such as the initial connection request to the drone.
func (d *Driver) SendCommand(cmd string) (err error) {
_, err = d.cmdConn.Write([]byte(cmd))
return
}
func (d *Driver) handleResponse(r io.Reader) error {
var buf [2048]byte
var msgType uint16
n, err := r.Read(buf[0:])
if err != nil {
return err
}
// parse binary packet
if buf[0] == messageStart {
msgType = (uint16(buf[6]) << 8) | uint16(buf[5])
switch msgType {
case wifiMessage:
buf := bytes.NewReader(buf[9:10])
wd := &WifiData{}
binary.Read(buf, binary.LittleEndian, &wd.Strength)
binary.Read(buf, binary.LittleEndian, &wd.Disturb)
d.Publish(d.Event(WifiDataEvent), wd)
case lightMessage:
buf := bytes.NewReader(buf[9:9])
var ld int8
binary.Read(buf, binary.LittleEndian, &ld)
d.Publish(d.Event(LightStrengthEvent), ld)
case logMessage:
d.Publish(d.Event(LogEvent), buf[9:])
case timeCommand:
d.Publish(d.Event(TimeEvent), buf[7:8])
case bounceCommand:
d.Publish(d.Event(BounceEvent), buf[7:8])
case takeoffCommand:
d.Publish(d.Event(TakeoffEvent), buf[7:8])
case landCommand:
d.Publish(d.Event(LandingEvent), buf[7:8])
case palmLandCommand:
d.Publish(d.Event(PalmLandingEvent), buf[7:8])
case flipCommand:
d.Publish(d.Event(FlipEvent), buf[7:8])
case flightMessage:
fd, _ := d.ParseFlightData(buf[9:])
d.Publish(d.Event(FlightDataEvent), fd)
case exposureCommand:
d.Publish(d.Event(SetExposureEvent), buf[7:8])
case videoEncoderRateCommand:
d.Publish(d.Event(SetVideoEncoderRateEvent), buf[7:8])
default:
fmt.Printf("Unknown message: %+v\n", buf[0:n])
}
return nil
}
// parse text packet
if buf[0] == 0x63 && buf[1] == 0x6f && buf[2] == 0x6e {
d.Publish(d.Event(ConnectedEvent), nil)
}
return nil
}
func (d *Driver) processVideo() error {
videoPort, err := net.ResolveUDPAddr("udp", ":11111")
if err != nil {
return err
}
d.videoConn, err = net.ListenUDP("udp", videoPort)
if err != nil {
return err
}
go func() {
videoConnLoop:
for {
select {
case <-d.doneCh:
break videoConnLoop
default:
buf := make([]byte, 2048)
n, _, err := d.videoConn.ReadFromUDP(buf)
if err != nil {
fmt.Println("Error: ", err)
continue
}
d.Publish(d.Event(VideoFrameEvent), buf[2:n])
}
}
}()
return nil
}
func (d *Driver) createPacket(cmd int16, pktType byte, len int16) (buf *bytes.Buffer, err error) {
l := len + 11
buf = &bytes.Buffer{}
binary.Write(buf, binary.LittleEndian, byte(messageStart))
binary.Write(buf, binary.LittleEndian, l<<3)
binary.Write(buf, binary.LittleEndian, CalculateCRC8(buf.Bytes()[0:3]))
binary.Write(buf, binary.LittleEndian, pktType)
binary.Write(buf, binary.LittleEndian, cmd)
return buf, nil
}
func (d *Driver) connectionString() string {
x, _ := strconv.Atoi(d.videoPort)
b := [2]byte{}
binary.LittleEndian.PutUint16(b[:], uint16(x))
res := fmt.Sprintf("conn_req:%s", b)
return res
}
func (f *FlightData) AirSpeed() float64 {
return math.Sqrt(
math.Pow(float64(f.NorthSpeed), 2) +
math.Pow(float64(f.EastSpeed), 2) +
math.Pow(float64(f.VerticalSpeed), 2))
}
func (f *FlightData) GroundSpeed() float64 {
return math.Sqrt(
math.Pow(float64(f.NorthSpeed), 2) +
math.Pow(float64(f.EastSpeed), 2))