Skip to content

Commit

Permalink
fix some typing definitions and make mypy happy again
Browse files Browse the repository at this point in the history
  • Loading branch information
M4GNV5 committed Jan 23, 2021
1 parent 584ef7d commit b1ad6b5
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions djitellopy/tello.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import threading
import cv2 # type: ignore
from threading import Thread
from typing import Optional
from typing import Optional, Union, Type, Dict

from .enforce_types import enforce_types

Expand Down Expand Up @@ -60,6 +60,8 @@ class Tello:
'tof', 'h', 'bat', 'time'
)
FLOAT_STATE_FIELDS = ('baro', 'agx', 'agy', 'agz')

state_field_converters: Dict[str, Union[Type[int], Type[float]]]
state_field_converters = {key : int for key in INT_STATE_FIELDS}
state_field_converters.update({key : float for key in FLOAT_STATE_FIELDS})

Expand Down Expand Up @@ -157,7 +159,7 @@ def udp_state_receiver():
break

@staticmethod
def parse_state(state: str) -> dict:
def parse_state(state: str) -> Dict[str, Union[int, float, str]]:
"""Parse a state line to a dictionary
Internal method, you normally wouldn't call this yourself.
"""
Expand All @@ -174,16 +176,17 @@ def parse_state(state: str) -> dict:
continue

key = split[0]
value = split[1]
value: Union[int, float, str] = split[1]

if key in Tello.state_field_converters:
num_type = Tello.state_field_converters[key]
try:
value = num_type(value)
except Exception as e:
except ValueError as e:
Tello.LOGGER.debug('Error parsing state value for {}: {} to {}'
.format(key, value, num_type))
Tello.LOGGER.error(e)
continue

state_dict[key] = value

Expand Down Expand Up @@ -410,7 +413,7 @@ def send_command_with_return(self, command: str, timeout: int = RESPONSE_TIMEOUT
diff = time.time() - self.last_received_command_timestamp
if diff < self.TIME_BTW_COMMANDS:
self.LOGGER.debug('Waiting {} seconds to execute command: {}...'.format(diff, command))
self.sleep(diff)
time.sleep(diff)

self.LOGGER.info("Send command: '{}'".format(command))
timestamp = time.time()
Expand All @@ -424,7 +427,7 @@ def send_command_with_return(self, command: str, timeout: int = RESPONSE_TIMEOUT
message = "Aborting command '{}'. Did not receive a response after {} seconds".format(command, timeout)
self.LOGGER.warning(message)
return message
self.sleep(0.1) # Sleep during send command
time.sleep(0.1) # Sleep during send command

self.last_received_command_timestamp = time.time()

Expand Down Expand Up @@ -517,16 +520,11 @@ def connect(self, wait_for_state=True):
t = i / REPS # in seconds
Tello.LOGGER.debug("'.connect()' received first state packet after {} seconds".format(t))
break
self.sleep(1 / REPS)
time.sleep(1 / REPS)

if not self.get_current_state():
raise Exception('Did not receive a state packet from the Tello')

def sleep(time_sleep=5):
"""Sleep.
"""
time.sleep(time_sleep)

def takeoff(self):
"""Automatic takeoff.
"""
Expand Down Expand Up @@ -776,7 +774,7 @@ def send_rc_control(self, left_right_velocity: int, forward_backward_velocity: i
up_down_velocity: -100~100 (up/down)
yaw_velocity: -100~100 (yaw)
"""
def clamp100(x: int): int:

This comment has been minimized.

Copy link
@Nikolaj-K

Nikolaj-K Jan 23, 2021

Contributor

Yeah I just saw this too.
In sleep, there was only self missing.

This comment has been minimized.

Copy link
@Nikolaj-K

Nikolaj-K Jan 23, 2021

Contributor

That said the 5 was random and I didn't like that either.

Do you know if direct comments are more reliable than the full 90° rotation calls (do the latter maybe depend more on IMU?). I ask because I had had a lot of IMU message issues when i first tried it.

This comment has been minimized.

Copy link
@M4GNV5

M4GNV5 Jan 23, 2021

Author Collaborator

Yeah, but i dont really think having a public sleep method in a Tello related class is good style.

"direct comments"? You mean rc commands instead of rotation commands? Im not sure if you can accurately rotate the tello with rc commands, thus rc is good if you are e.g. tracking something in the video feed and cw/ccw is good when you want to acurately rotate by 90 degrees.

This comment has been minimized.

Copy link
@Nikolaj-K

Nikolaj-K Jan 23, 2021

Contributor

kk, yeah I meant commands.

Btw. fyi I got to the code base in the first place 10 days ago from
https://youtu.be/LmEcyQnfpDA

This comment has been minimized.

Copy link
@M4GNV5

M4GNV5 Jan 23, 2021

Author Collaborator

haha, i guess that explains the high visitor counts to this repo.
Sadly he is using the very old version from pypi which lacks type checks, swarm functionality and even has some bugs.

def clamp100(x: int) -> int:
return max(-100, min(100, x))

if time.time() - self.last_rc_control_timestamp > self.TIME_BTW_RC_CONTROL_COMMANDS:
Expand Down

0 comments on commit b1ad6b5

Please sign in to comment.