Skip to content

Commit c0e08e2

Browse files
committed
tello: API improvements by using const values and types for streaming video settings
Signed-off-by: Ron Evans <[email protected]>
1 parent 73069e4 commit c0e08e2

File tree

3 files changed

+82
-23
lines changed

3 files changed

+82
-23
lines changed

examples/tello_opencv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func main() {
6363
drone.On(tello.ConnectedEvent, func(data interface{}) {
6464
fmt.Println("Connected")
6565
drone.StartVideo()
66-
drone.SetExposure(1)
67-
drone.SetVideoEncoderRate(4)
66+
drone.SetVideoEncoderRate(tello.VideoBitRateAuto)
67+
drone.SetExposure(0)
6868

6969
gobot.Every(100*time.Millisecond, func() {
7070
drone.StartVideo()

examples/tello_video.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
drone.On(tello.ConnectedEvent, func(data interface{}) {
3737
fmt.Println("Connected")
3838
drone.StartVideo()
39-
drone.SetVideoEncoderRate(4)
39+
drone.SetVideoEncoderRate(tello.VideoBitRateAuto)
4040
gobot.Every(100*time.Millisecond, func() {
4141
drone.StartVideo()
4242
})

platforms/dji/tello/driver.go

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ const (
4545

4646
// VideoFrameEvent event
4747
VideoFrameEvent = "videoframe"
48+
49+
// SetVideoEncoderRateEvent event
50+
SetVideoEncoderRateEvent = "setvideoencoder"
4851
)
4952

5053
const (
51-
messageStart = 0xcc
52-
wifiMessage = 26
53-
lightMessage = 53
54-
timeMessage = 70
55-
flightMessage = 86
54+
messageStart = 0xcc
55+
wifiMessage = 26
56+
videoRateQuery = 40
57+
lightMessage = 53
58+
timeMessage = 70
59+
flightMessage = 86
5660

5761
logMessage = 0x50
5862

@@ -63,11 +67,58 @@ const (
6367
takeoffCommand = 0x54
6468
landCommand = 0x55
6569
flipCommand = 0x5c
70+
)
71+
72+
// FlipType is used for the various flips supported by the Tello.
73+
type FlipType int
74+
75+
const (
76+
// FlipFront flips forward.
77+
FlipFront FlipType = 0
78+
79+
// FlipLeft flips left.
80+
FlipLeft FlipType = 1
81+
82+
// FlipBack flips backwards.
83+
FlipBack FlipType = 2
84+
85+
// FlipRight flips to the right.
86+
FlipRight FlipType = 3
87+
88+
// FlipForwardLeft flips forwards and to the left.
89+
FlipForwardLeft FlipType = 4
90+
91+
// FlipBackLeft flips backwards and to the left.
92+
FlipBackLeft FlipType = 5
93+
94+
// FlipBackRight flips backwards and to the right.
95+
FlipBackRight FlipType = 6
6696

67-
flipFront = 0
68-
flipLeft = 1
69-
flipBack = 2
70-
flipRight = 3
97+
// FlipForwardRight flips forewards and to the right.
98+
FlipForwardRight FlipType = 7
99+
)
100+
101+
// VideoBitRate is used to set the bit rate for the streaming video returned by the Tello.
102+
type VideoBitRate int
103+
104+
const (
105+
// VideoBitRateAuto sets the bitrate for streaming video to auto-adjust.
106+
VideoBitRateAuto VideoBitRate = 0
107+
108+
// VideoBitRate1M sets the bitrate for streaming video to 1 Mb/s.
109+
VideoBitRate1M VideoBitRate = 1
110+
111+
// VideoBitRate15M sets the bitrate for streaming video to 1.5 Mb/s
112+
VideoBitRate15M VideoBitRate = 2
113+
114+
// VideoBitRate2M sets the bitrate for streaming video to 2 Mb/s.
115+
VideoBitRate2M VideoBitRate = 3
116+
117+
// VideoBitRate3M sets the bitrate for streaming video to 3 Mb/s.
118+
VideoBitRate3M VideoBitRate = 4
119+
120+
// VideoBitRate4M sets the bitrate for streaming video to 4 Mb/s.
121+
VideoBitRate4M VideoBitRate = 5
71122
)
72123

73124
// FlightData packet returned by the Tello
@@ -140,10 +191,16 @@ func NewDriver(port string) *Driver {
140191

141192
d.AddEvent(ConnectedEvent)
142193
d.AddEvent(FlightDataEvent)
194+
d.AddEvent(TakeoffEvent)
195+
d.AddEvent(LandingEvent)
196+
d.AddEvent(FlipEvent)
197+
d.AddEvent(TimeEvent)
198+
d.AddEvent(LogEvent)
143199
d.AddEvent(WifiDataEvent)
144200
d.AddEvent(LightStrengthEvent)
145-
d.AddEvent(VideoFrameEvent)
146201
d.AddEvent(SetExposureEvent)
202+
d.AddEvent(VideoFrameEvent)
203+
d.AddEvent(SetVideoEncoderRateEvent)
147204

148205
return d
149206
}
@@ -250,6 +307,8 @@ func (d *Driver) handleResponse() error {
250307
d.Publish(d.Event(FlightDataEvent), fd)
251308
case exposureCommand:
252309
d.Publish(d.Event(SetExposureEvent), buf[7:8])
310+
case videoEncoderRateCommand:
311+
d.Publish(d.Event(SetVideoEncoderRateEvent), buf[7:8])
253312
default:
254313
fmt.Printf("Unknown message: %+v\n", buf[0:n])
255314
}
@@ -311,7 +370,7 @@ func (d *Driver) Land() (err error) {
311370
return
312371
}
313372

314-
// StartVideo tells to start video stream.
373+
// StartVideo tells Tello to send start info (SPS/PPS) for video stream.
315374
func (d *Driver) StartVideo() (err error) {
316375
pkt := []byte{messageStart, 0x58, 0x00, 0x7c, 0x60, videoStartCommand, 0x00, 0x00, 0x00, 0x6c, 0x95}
317376
_, err = d.reqConn.Write(pkt)
@@ -334,8 +393,8 @@ func (d *Driver) SetExposure(level int) (err error) {
334393
}
335394

336395
// SetVideoEncoderRate sets the drone video encoder rate.
337-
func (d *Driver) SetVideoEncoderRate(rate int) (err error) {
338-
pkt := []byte{messageStart, 0x62, 0x00, 0x27, 0x68, videoEncoderRateCommand, 0x00, 0xe6, 0x01, byte(rate), 0x00, 0x00, 0x00}
396+
func (d *Driver) SetVideoEncoderRate(rate VideoBitRate) (err error) {
397+
pkt := []byte{messageStart, 0x60, 0x00, 0x27, 0x68, videoEncoderRateCommand, 0x00, 0xe6, 0x01, byte(rate), 0x00, 0x00}
339398

340399
// sets ending crc bytes for packet
341400
l := len(pkt)
@@ -345,9 +404,9 @@ func (d *Driver) SetVideoEncoderRate(rate int) (err error) {
345404
return
346405
}
347406

348-
// Rate does some still unknown thing.
407+
// Rate queries the current video bit rate.
349408
func (d *Driver) Rate() (err error) {
350-
pkt := []byte{messageStart, 0x58, 0x00, 0x7c, 0x48, 40, 0x00, 0xe6, 0x01, 0x6c, 0x95}
409+
pkt := []byte{messageStart, 0x58, 0x00, 0x7c, 0x48, videoRateQuery, 0x00, 0xe6, 0x01, 0x6c, 0x95}
351410

352411
// sets ending crc bytes for packet
353412
l := len(pkt)
@@ -431,7 +490,7 @@ func (d *Driver) CounterClockwise(val int) error {
431490
}
432491

433492
// Flip tells drone to flip
434-
func (d *Driver) Flip(direction int) (err error) {
493+
func (d *Driver) Flip(direction FlipType) (err error) {
435494
pkt := []byte{messageStart, 0x60, 0x00, 0x27, 0x70, flipCommand, 0x00, 0xe6, 0x01, byte(direction), 0x00, 0x00}
436495

437496
// sets ending crc bytes for packet
@@ -444,22 +503,22 @@ func (d *Driver) Flip(direction int) (err error) {
444503

445504
// FrontFlip tells the drone to perform a front flip.
446505
func (d *Driver) FrontFlip() (err error) {
447-
return d.Flip(flipFront)
506+
return d.Flip(FlipFront)
448507
}
449508

450509
// BackFlip tells the drone to perform a back flip.
451510
func (d *Driver) BackFlip() (err error) {
452-
return d.Flip(flipBack)
511+
return d.Flip(FlipBack)
453512
}
454513

455514
// RightFlip tells the drone to perform a flip to the right.
456515
func (d *Driver) RightFlip() (err error) {
457-
return d.Flip(flipRight)
516+
return d.Flip(FlipRight)
458517
}
459518

460519
// LeftFlip tells the drone to perform a flip to the left.
461520
func (d *Driver) LeftFlip() (err error) {
462-
return d.Flip(flipLeft)
521+
return d.Flip(FlipLeft)
463522
}
464523

465524
// ParseFlightData from drone

0 commit comments

Comments
 (0)