-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
driver.go
1128 lines (955 loc) · 28.4 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"
"sync/atomic"
"time"
"gobot.io/x/gobot/v2"
)
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.
//
// The meaning of some fields is not documented. If you learned more, please, contribute.
// See https://github.com/hybridgroup/gobot/issues/798.
type FlightData struct {
BatteryLow bool
BatteryLower bool
BatteryPercentage int8 // How much battery left [in %].
CameraState int8
DroneBatteryLeft int16
DroneFlyTimeLeft int16
DroneHover bool // If the drone is in the air and not moving.
EmOpen bool
Flying bool // If the drone is currently in the air.
OnGround bool // If the drone is currently on the ground.
EastSpeed int16 // Movement speed towards East [in cm/s]. Negative if moving west.
ElectricalMachineryState int16
FactoryMode bool
FlyMode int8
FlyTime int16 // How long since take off [in s/10].
FrontIn bool
FrontLSC bool
FrontOut bool
GravityState bool
VerticalSpeed int16 // Movement speed up [in cm/s].
Height int16 // The height [in decimeters].
ImuCalibrationState int8 // The IMU calibration step (when doing IMU calibration).
NorthSpeed int16 // Movement speed towards North [in cm/s]. Negative if moving South.
ThrowFlyTimer int8
// Warnings:
DownVisualState bool // If the ground is visible by the down camera.
BatteryState bool // If there is an issue with battery.
ImuState bool // If drone needs IMU (Inertial Measurement Unit) calibration.
OutageRecording bool // If there is an issue with video recording.
PowerState bool // If there is an issue with power supply.
PressureState bool // If there is an issue with air pressure.
TemperatureHigh bool // If drone is overheating.
WindState bool // If the wind is too strong.
}
// 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{}
doneChReaderCount int32
}
// 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 {
return NewDriverWithIP("192.168.10.1", port)
}
// 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(),
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
}
// 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
d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)
err := d.On(d.Event(ConnectedEvent), func(interface{}) {
if err := d.SendDateTime(); err != nil {
panic(err)
}
if err := d.processVideo(); err != nil {
panic(err)
}
})
if err != nil {
panic(err)
}
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
if err := d.SendCommand(d.connectionString()); err != nil {
return err
}
// send stick commands
d.addDoneChReaderCount(1)
go func() {
defer d.addDoneChReaderCount(-1)
stickCmdLoop:
for {
select {
case <-d.doneCh:
break stickCmdLoop
default:
if err := d.SendStickCommand(); err != nil {
fmt.Println("stick command error:", err)
}
time.Sleep(20 * time.Millisecond)
}
}
}()
return nil
}
// Halt stops the driver.
func (d *Driver) Halt() error {
// send a landing command when we disconnect, and give it 500ms to be received before we shutdown
if d.cmdConn != nil {
if err := d.Land(); err != nil {
return err
}
}
time.Sleep(500 * time.Millisecond)
if d.cmdConn != nil {
d.cmdConn.Close()
}
if d.videoConn != nil {
d.videoConn.Close()
}
readerCount := atomic.LoadInt32(&d.doneChReaderCount)
for i := 0; i < int(readerCount); i++ {
d.doneCh <- struct{}{}
}
return nil
}
// TakeOff tells drones to liftoff and start flying.
func (d *Driver) TakeOff() error {
buf, _ := d.createPacket(takeoffCommand, 0x68, 0)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// Throw & Go support
func (d *Driver) ThrowTakeOff() error {
buf, _ := d.createPacket(throwtakeoffCommand, 0x48, 0)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// Land tells drone to come in for landing.
func (d *Driver) Land() error {
buf, _ := d.createPacket(landCommand, 0x68, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(0x00)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// StopLanding tells drone to stop landing.
func (d *Driver) StopLanding() error {
buf, _ := d.createPacket(landCommand, 0x68, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(0x01)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// PalmLand tells drone to come in for a hand landing.
func (d *Driver) PalmLand() error {
buf, _ := d.createPacket(palmLandCommand, 0x68, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(0x00)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// StartVideo tells Tello to send start info (SPS/PPS) for video stream.
func (d *Driver) StartVideo() error {
buf, _ := d.createPacket(videoStartCommand, 0x60, 0)
// seq = 0
if err := binary.Write(buf, binary.LittleEndian, int16(0x00)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// SetExposure sets the drone camera exposure level. Valid levels are 0, 1, and 2.
func (d *Driver) SetExposure(level int) error {
if level < 0 || level > 2 {
return errors.New("Invalid exposure level")
}
buf, _ := d.createPacket(exposureCommand, 0x48, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(level)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// SetVideoEncoderRate sets the drone video encoder rate.
func (d *Driver) SetVideoEncoderRate(rate VideoBitRate) error {
buf, _ := d.createPacket(videoEncoderRateCommand, 0x68, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(rate)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// 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() error {
buf, _ := d.createPacket(videoRateQuery, 0x48, 0)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// bound is a naive implementation that returns the smaller of x or y.
func bound(x, y float32) float32 { //nolint:unparam // keep y as parameter
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).
//
//nolint:nonamedreturns // sufficient here
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() error {
buf, _ := d.createPacket(bounceCommand, 0x68, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if d.bouncing {
if err := binary.Write(buf, binary.LittleEndian, byte(0x31)); err != nil {
return err
}
} else {
if err := binary.Write(buf, binary.LittleEndian, byte(0x30)); err != nil {
return err
}
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
d.bouncing = !d.bouncing
return err
}
// Flip tells drone to flip
func (d *Driver) Flip(direction FlipType) error {
buf, _ := d.createPacket(flipCommand, 0x70, 1)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(direction)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// FrontFlip tells the drone to perform a front flip.
func (d *Driver) FrontFlip() error {
return d.Flip(FlipFront)
}
// BackFlip tells the drone to perform a back flip.
func (d *Driver) BackFlip() error {
return d.Flip(FlipBack)
}
// RightFlip tells the drone to perform a flip to the right.
func (d *Driver) RightFlip() error {
return d.Flip(FlipRight)
}
// LeftFlip tells the drone to perform a flip to the left.
func (d *Driver) LeftFlip() error {
return d.Flip(FlipLeft)
}
// ParseFlightData from drone
func (d *Driver) ParseFlightData(b []byte) (*FlightData, 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 fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.Height); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.NorthSpeed); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.EastSpeed); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.VerticalSpeed); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.FlyTime); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &data); err != nil {
return fd, err
}
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
if err := binary.Read(buf, binary.LittleEndian, &fd.ImuCalibrationState); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.BatteryPercentage); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.DroneFlyTimeLeft); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.DroneBatteryLeft); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &data); err != nil {
return fd, err
}
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
if err := binary.Read(buf, binary.LittleEndian, &fd.FlyMode); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.ThrowFlyTimer); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &fd.CameraState); err != nil {
return fd, err
}
if err := binary.Read(buf, binary.LittleEndian, &data); err != nil {
return fd, err
}
fd.ElectricalMachineryState = int16(data & 0xff)
if err := binary.Read(buf, binary.LittleEndian, &data); err != nil {
return fd, err
}
fd.FrontIn = (data >> 0 & 0x1) == 1
fd.FrontOut = (data >> 1 & 0x1) == 1
fd.FrontLSC = (data >> 2 & 0x1) == 1
if err := binary.Read(buf, binary.LittleEndian, &data); err != nil {
return fd, err
}
fd.TemperatureHigh = (data >> 0 & 0x1) == 1
return fd, nil
}
// SendStickCommand sends the joystick command packet to the drone.
func (d *Driver) SendStickCommand() error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
buf, _ := d.createPacket(stickCommand, 0x60, 11)
// seq = 0
if err := binary.Write(buf, binary.LittleEndian, int16(0x00)); err != nil {
return err
}
// 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) //nolint:gosec // TODO: fix later
packedAxis := int64(axis1)&0x7FF | int64(axis2&0x7FF)<<11 | 0x7FF&int64(axis3)<<22 | 0x7FF&int64(axis4)<<33 |
int64(axis5)<<44
if err := binary.Write(buf, binary.LittleEndian, byte(0xFF&packedAxis)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(packedAxis>>8&0xFF)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(packedAxis>>16&0xFF)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(packedAxis>>24&0xFF)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(packedAxis>>32&0xFF)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(packedAxis>>40&0xFF)); err != nil {
return err
}
now := time.Now()
if err := binary.Write(buf, binary.LittleEndian, byte(now.Hour())); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(now.Minute())); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(now.Second())); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(now.UnixNano()/int64(time.Millisecond)&0xff)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, byte(now.UnixNano()/int64(time.Millisecond)>>8)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// SendDateTime sends the current date/time to the drone.
func (d *Driver) SendDateTime() error {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()
buf, _ := d.createPacket(timeCommand, 0x50, 11)
d.seq++
if err := binary.Write(buf, binary.LittleEndian, d.seq); err != nil {
return err
}
now := time.Now()
if err := binary.Write(buf, binary.LittleEndian, byte(0x00)); err != nil {
return err
}
//nolint:gosec // TODO: fix later
if err := binary.Write(buf, binary.LittleEndian, int16(now.Hour())); err != nil {
return err
}
//nolint:gosec // TODO: fix later
if err := binary.Write(buf, binary.LittleEndian, int16(now.Minute())); err != nil {
return err
}
//nolint:gosec // TODO: fix later
if err := binary.Write(buf, binary.LittleEndian, int16(now.Second())); err != nil {
return err
}
//nolint:gosec // TODO: fix later
if err := binary.Write(buf, binary.LittleEndian, int16(now.UnixNano()/int64(time.Millisecond)&0xff)); err != nil {
return err
}
//nolint:gosec // TODO: fix later
if err := binary.Write(buf, binary.LittleEndian, int16(now.UnixNano()/int64(time.Millisecond)>>8)); err != nil {
return err
}
if err := binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes())); err != nil {
return err
}
_, err := d.cmdConn.Write(buf.Bytes())
return err
}
// SendCommand is used to send a text command such as the initial connection request to the drone.
func (d *Driver) SendCommand(cmd string) error {
_, err := d.cmdConn.Write([]byte(cmd))
return err
}
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