Skip to content

Commit

Permalink
tello: correct packet construction now working
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Apr 24, 2018
1 parent d113da1 commit b0ff165
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 72 deletions.
69 changes: 58 additions & 11 deletions platforms/dji/tello/crc.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,66 @@
package tello

import (
"github.com/go-daq/crc8"
"github.com/howeyc/crc16"
)
var crc8table = []byte{
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35,
}

// CalculateCRC8 calculates the starting CRC8 byte for packet.
func CalculateCRC8(bytes []byte) byte {
return crc8.Checksum(bytes, crc8.MakeTable(0x77))
func CalculateCRC8(pkt []byte) byte {
i := 0
l := len(pkt)
seed := byte(0x77)
for l > 0 {
seed = crc8table[(seed^pkt[i])&0xff]
i = i + 1
l = l - 1
}

return seed
}

var crc16table = []uint16{
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78,
}

// CalculateCRC16 calculates the ending CRC16 bytes for packet.
func CalculateCRC16(pkt []byte) (low, high byte) {
i := crc16.Checksum(pkt, crc16.MakeTable(0x3692))
low = ((byte)(i & 0xFF))
high = ((byte)(i >> 8 & 0xFF))
return
func CalculateCRC16(pkt []byte) (i uint16) {
i = 0
l := len(pkt)
seed := uint16(0x3692)
for l > 0 {
seed = crc16table[(seed^uint16(pkt[i]))&0xff] ^ (seed >> 8)
i = i + 1
l = l - 1
}
return seed
}
140 changes: 79 additions & 61 deletions platforms/dji/tello/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ type Driver struct {
videoConn *net.UDPConn // UDP connection for drone video
respPort string
cmdMutex sync.Mutex
seq int16
rx, ry, lx, ly, throttle float32
gobot.Eventer
}
Expand Down Expand Up @@ -335,8 +336,8 @@ func (d *Driver) processVideo() error {
}

go func() {
buf := make([]byte, 2048)
for {
buf := make([]byte, 2048)
n, _, err := d.videoConn.ReadFromUDP(buf)
d.Publish(d.Event(VideoFrameEvent), buf[2:n])

Expand All @@ -358,22 +359,34 @@ func (d *Driver) Halt() (err error) {

// TakeOff tells drones to liftoff and start flying.
func (d *Driver) TakeOff() (err error) {
takeOffPacket := []byte{messageStart, 0x58, 0x00, 0x7c, 0x68, takeoffCommand, 0x00, 0xe4, 0x01, 0xc2, 0x16}
_, err = d.reqConn.Write(takeOffPacket)
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.reqConn.Write(buf.Bytes())
return
}

// Land tells drone to come in for landing.
func (d *Driver) Land() (err error) {
landPacket := []byte{messageStart, 0x60, 0x00, 0x27, 0x68, landCommand, 0x00, 0xe5, 0x01, 0x00, 0xba, 0xc7}
_, err = d.reqConn.Write(landPacket)
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.reqConn.Write(buf.Bytes())
return
}

// StartVideo tells Tello to send start info (SPS/PPS) for video stream.
func (d *Driver) StartVideo() (err error) {
pkt := []byte{messageStart, 0x58, 0x00, 0x7c, 0x60, videoStartCommand, 0x00, 0x00, 0x00, 0x6c, 0x95}
_, err = d.reqConn.Write(pkt)
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.reqConn.Write(buf.Bytes())
return
}

Expand All @@ -382,37 +395,37 @@ func (d *Driver) SetExposure(level int) (err error) {
if level < 0 || level > 2 {
return errors.New("Invalid exposure level")
}
pkt := []byte{messageStart, 0x60, 0x00, 0x27, 0x48, exposureCommand, 0x00, 0xe6, 0x01, byte(level), 0x00, 0x00}

// sets ending crc bytes for packet
l := len(pkt)
pkt[(l - 2)], pkt[(l - 1)] = CalculateCRC16(pkt)
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.reqConn.Write(pkt)
_, err = d.reqConn.Write(buf.Bytes())
return
}

// SetVideoEncoderRate sets the drone video encoder rate.
func (d *Driver) SetVideoEncoderRate(rate VideoBitRate) (err error) {
pkt := []byte{messageStart, 0x60, 0x00, 0x27, 0x68, videoEncoderRateCommand, 0x00, 0xe6, 0x01, byte(rate), 0x00, 0x00}

// sets ending crc bytes for packet
l := len(pkt)
pkt[(l - 2)], pkt[(l - 1)] = CalculateCRC16(pkt)
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.reqConn.Write(pkt)
_, err = d.reqConn.Write(buf.Bytes())
return
}

// Rate queries the current video bit rate.
func (d *Driver) Rate() (err error) {
pkt := []byte{messageStart, 0x58, 0x00, 0x7c, 0x48, videoRateQuery, 0x00, 0xe6, 0x01, 0x6c, 0x95}
buf, _ := d.createPacket(videoRateQuery, 0x48, 0)
d.seq++
binary.Write(buf, binary.LittleEndian, d.seq)
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))

// sets ending crc bytes for packet
l := len(pkt)
pkt[(l - 2)], pkt[(l - 1)] = CalculateCRC16(pkt)

_, err = d.reqConn.Write(pkt)
_, err = d.reqConn.Write(buf.Bytes())
return
}

Expand Down Expand Up @@ -491,13 +504,13 @@ func (d *Driver) CounterClockwise(val int) error {

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

// sets ending crc bytes for packet
l := len(pkt)
pkt[(l - 2)], pkt[(l - 1)] = CalculateCRC16(pkt)
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.reqConn.Write(pkt)
_, err = d.reqConn.Write(buf.Bytes())
return
}

Expand Down Expand Up @@ -586,7 +599,8 @@ func (d *Driver) SendStickCommand() (err error) {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()

pkt := []byte{messageStart, 0xb0, 0x00, 0x7f, 0x60, stickCommand, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x16, 0x01, 0x0e, 0x00, 0x25, 0x54}
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)
Expand All @@ -604,25 +618,25 @@ func (d *Driver) SendStickCommand() (err error) {
axis5 := int16(660.0*d.throttle + 1024.0)

packedAxis := int64(axis1)&0x7FF | int64(axis2&0x7FF)<<11 | 0x7FF&int64(axis3)<<22 | 0x7FF&int64(axis4)<<33 | int64(axis5)<<44
pkt[9] = byte(0xFF & packedAxis)
pkt[10] = byte(packedAxis >> 8 & 0xFF)
pkt[11] = byte(packedAxis >> 16 & 0xFF)
pkt[12] = byte(packedAxis >> 24 & 0xFF)
pkt[13] = byte(packedAxis >> 32 & 0xFF)
pkt[14] = byte(packedAxis >> 40 & 0xFF)
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()
pkt[15] = byte(now.Hour())
pkt[16] = byte(now.Minute())
pkt[17] = byte(now.Second())
pkt[18] = byte(now.UnixNano() / int64(time.Millisecond) & 0xff)
pkt[19] = byte(now.UnixNano() / int64(time.Millisecond) >> 8)
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))

// sets ending crc for packet
l := len(pkt)
pkt[(l - 2)], pkt[(l - 1)] = CalculateCRC16(pkt)
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))

_, err = d.reqConn.Write(buf.Bytes())

_, err = d.reqConn.Write(pkt)
return
}

Expand All @@ -631,19 +645,9 @@ func (d *Driver) SendDateTime() (err error) {
d.cmdMutex.Lock()
defer d.cmdMutex.Unlock()

l := 22
pkt := make([]byte, l)
buf := bytes.NewBuffer(pkt)

binary.Write(buf, binary.LittleEndian, byte(messageStart))
binary.Write(buf, binary.LittleEndian, byte(l<<3))
binary.Write(buf, binary.LittleEndian, byte(0x00))
binary.Write(buf, binary.LittleEndian, byte(CalculateCRC8(pkt[0:2])))
binary.Write(buf, binary.LittleEndian, byte(0x50)) // packet type 0x50
binary.Write(buf, binary.LittleEndian, timeCommand)
binary.Write(buf, binary.LittleEndian, byte(0x00))
binary.Write(buf, binary.LittleEndian, byte(0x12)) // seq
binary.Write(buf, binary.LittleEndian, byte(0x00))
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))
Expand All @@ -654,9 +658,9 @@ func (d *Driver) SendDateTime() (err error) {
binary.Write(buf, binary.LittleEndian, int16(now.UnixNano()/int64(time.Millisecond)>>8))

// sets ending crc for packet
pkt[(l - 2)], pkt[(l - 1)] = CalculateCRC16(pkt)
binary.Write(buf, binary.LittleEndian, CalculateCRC16(buf.Bytes()))

_, err = d.reqConn.Write(pkt)
_, err = d.reqConn.Write(buf.Bytes())
return
}

Expand All @@ -666,10 +670,24 @@ func (d *Driver) SendCommand(cmd string) (err error) {
return
}

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 validatePitch(val int) int {
if val > 100 {
return 100
} else if val < 0 {
}
if val < 0 {
return 0
}

Expand Down

0 comments on commit b0ff165

Please sign in to comment.