Skip to content

Commit

Permalink
reduce min time between commands
Browse files Browse the repository at this point in the history
in the official tello python code they send the next command immediately after
receiving a response to the last one.

With a framerate of 120 in the pygame example i experienced no noticable input
lag when controlling the drone.
This commit also includes some other tidy ups to the pygame example.
  • Loading branch information
M4GNV5 committed May 22, 2020
1 parent 7ec6752 commit 5a44e54
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions djitellopy/tello.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Tello:
"""
# Send and receive commands, client socket
RESPONSE_TIMEOUT = 7 # in seconds
TIME_BTW_COMMANDS = 1 # in seconds
TIME_BTW_RC_CONTROL_COMMANDS = 0.5 # in seconds
TIME_BTW_COMMANDS = 0.1 # in seconds
TIME_BTW_RC_CONTROL_COMMANDS = 0.001 # in seconds
RETRY_COUNT = 3 # number of retries after a failed command
TELLO_IP = '192.168.10.1' # Tello IP address

Expand Down
43 changes: 21 additions & 22 deletions examples/manual-control-pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# Speed of the drone
S = 60
# Frames per second of the pygame window display
FPS = 25
# A low number also results in input lag, as input information is processed once per frame.
FPS = 120


class FrontEnd(object):
Expand All @@ -27,7 +28,7 @@ def __init__(self):

# Creat pygame window
pygame.display.set_caption("Tello video stream")
self.screen = pygame.display.set_mode([640, 480])
self.screen = pygame.display.set_mode([960, 720])

# Init Tello object that interacts with the Tello drone
self.tello = Tello()
Expand All @@ -42,26 +43,16 @@ def __init__(self):
self.send_rc_control = False

# create update timer
pygame.time.set_timer(pygame.USEREVENT + 1, 50)
pygame.time.set_timer(pygame.USEREVENT + 1, 1000 // FPS)

def run(self):

if not self.tello.connect():
print("Tello not connected")
return

if not self.tello.set_speed(self.speed):
print("Not set speed to lowest possible")
return
self.tello.connect()
self.tello.set_speed(self.speed)

# In case streaming is on. This happens when we quit this program without the escape key.
if not self.tello.streamoff():
print("Could not stop video stream")
return

if not self.tello.streamon():
print("Could not start video stream")
return
self.tello.streamoff()
self.tello.streamon()

frame_read = self.tello.get_frame_read()

Expand All @@ -85,9 +76,15 @@ def run(self):
break

self.screen.fill([0, 0, 0])
frame = cv2.cvtColor(frame_read.frame, cv2.COLOR_BGR2RGB)

frame = frame_read.frame
text = "Battery: {}%".format(self.tello.get_battery())
cv2.putText(frame, text, (5, 720 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = np.rot90(frame)
frame = np.flipud(frame)

frame = pygame.surfarray.make_surface(frame)
self.screen.blit(frame, (0, 0))
pygame.display.update()
Expand Down Expand Up @@ -133,15 +130,17 @@ def keyup(self, key):
elif key == pygame.K_a or key == pygame.K_d: # set zero yaw velocity
self.yaw_velocity = 0
elif key == pygame.K_t: # takeoff
self.send_rc_control = self.tello.takeoff()
self.tello.takeoff()
self.send_rc_control = True
elif key == pygame.K_l: # land
self.send_rc_control = not self.tello.land()
not self.tello.land()
self.send_rc_control = False

def update(self):
""" Update routine. Send velocities to Tello."""
if self.send_rc_control:
self.tello.send_rc_control(self.left_right_velocity, self.for_back_velocity, self.up_down_velocity,
self.yaw_velocity)
self.tello.send_rc_control(self.left_right_velocity, self.for_back_velocity,
self.up_down_velocity, self.yaw_velocity)


def main():
Expand Down

0 comments on commit 5a44e54

Please sign in to comment.