Skip to content

Commit 8c60c2f

Browse files
starting trying to do a more stripped down node for the video
1 parent d015484 commit 8c60c2f

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

Tello_Video/tello.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def _h264_decode(self, packet_data):
194194
for framedata in frames:
195195
(frame, w, h, ls) = framedata
196196
if frame is not None:
197-
# print 'frame size %i bytes, w %i, h %i, linesize %i' % (len(frame), w, h, ls)
197+
print 'frame size %i bytes, w %i, h %i, linesize %i' % (len(frame), w, h, ls)
198+
# pdb.set_trace()
198199

199200
frame = np.fromstring(frame, dtype=np.ubyte, count=len(frame), sep='')
200201
frame = (frame.reshape((h, ls / 3, 3)))

Tello_Video/test.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import socket
2+
test = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
3+
video = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4+
5+
address = ("192.168.10.1", 8889)
6+
7+
8+
test.sendto(b"command", address)
9+
test.sendto(b"streamon", address)
10+
11+
while True:
12+
print(test.sendto(b"battery?", address))
13+
14+

Tello_Video/video_node.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import socket
2+
import rospy
3+
import numpy as np
4+
import libh264decoder
5+
from sensor_msgs.msg import Image
6+
import pdb
7+
import threading
8+
9+
10+
class Video_Feed:
11+
def __init__(self):
12+
13+
self.message_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
14+
self.video_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
15+
16+
self.message_address = ("192.168.10.1", 8899)
17+
self.video_port = 11111
18+
self.decoder = libh264decoder.H264Decoder()
19+
20+
# "" represents INADDR_ANY and is used to bind to all interfaces
21+
rospy.init_node("raw_video", anonymous=True)
22+
self.publisher = rospy.Publisher("raw_video", Image, queue_size=1)
23+
24+
self.message_socket.sendto(b"command", self.message_address)
25+
print("sent: command")
26+
self.message_socket.sendto(b"streamon", self.message_address)
27+
print("sent: streamon")
28+
29+
self.video_socket.bind(("", self.video_port))
30+
31+
recieve_thread = threading.Thread(target=self.recieve)
32+
recieve_thread.daemon = True
33+
recieve_thread.start()
34+
35+
# pdb.set_trace()
36+
37+
def recieve(self):
38+
packet_data = ""
39+
while True:
40+
try:
41+
res_string, ip = self.video_socket.recvfrom(2048)
42+
pdb.set_trace()
43+
packet_data += res_string
44+
if len(res_string) != 1460:
45+
for frame in self._h264_decode(packet_data):
46+
# pdb.set_trace()
47+
rospy.loginfo("published frame")
48+
self.publisher.publish(frame)
49+
50+
packet_data = ""
51+
except socket.error as exc:
52+
print("Cautch exception TypeError {}".format(exc))
53+
54+
def _h264_decode(self, packet_data):
55+
pdb.set_trace()
56+
"""
57+
decode raw h264 format data from Tello
58+
59+
:param packet_data: raw h264 data array
60+
61+
:return: a list of decoded frame
62+
"""
63+
res_frame_list = []
64+
frames = self.decoder.decode(packet_data)
65+
for framedata in frames:
66+
(frame, w, h, ls) = framedata
67+
if frame is not None:
68+
print('frame size %i bytes, w %i, h %i, linesize %i' % (len(frame), w, h, ls))
69+
70+
frame = np.fromstring(frame, dtype=np.ubyte, count=len(frame), sep='')
71+
frame = (frame.reshape((h, ls / 3, 3)))
72+
frame = frame[:, :w, :]
73+
res_frame_list.append(frame)
74+
75+
return res_frame_list
76+
77+
78+
def main():
79+
""" Initialises and cleansup after ros node"""
80+
feed = Video_Feed()
81+
try:
82+
feed.recieve()
83+
rospy.spin()
84+
except KeyboardInterrupt:
85+
rospy.signal_shutdown("down we go")
86+
87+
88+
if __name__ == '__main__':
89+
main()

0 commit comments

Comments
 (0)