Example #1
0
 def __init__(self):
     threading.Thread.__init__(self)
     self._general_state = None
     self._calibration_state = None
     self._udpclient = UdpClient()
     self._exit_flag = threading.Event()
     self.daemon = True
 def test_get_raw_data(self):
     us = UDPServer()
     uc = UdpClient(protocol_description=fake_json,
                    port=UDP_PORT,
                    drone_ip="127.0.0.1")
     data = uc._get_raw_data()
     us.stop_thread()
     self.assertEqual(data, struct.pack("<BBb", 2, 2, 2))
 def test_get_data_dict(self):
     us = UDPServer()
     uc = UdpClient(protocol_description=fake_json,
                    port=UDP_PORT,
                    drone_ip="127.0.0.1")
     data = uc.get_data_dict()
     us.stop_thread()
     self.assertEqual(data, {'u1-2-v': 2, 'u1-2-t': 2, 'i1-2': 2})
 def test_get_data_t3(self):
     us = UDPServer()
     uc = UdpClient(protocol_description=fake_json,
                    port=UDP_PORT,
                    drone_ip="127.0.0.1")
     data = uc.get_data(packet_type=3)
     us.stop_thread()
     self.assertEqual(data, (2, 3, 3))
Example #5
0
 def __init__(self, ip: str = "192.168.1.101", udp_timeout: float = 3):
     threading.Thread.__init__(self)
     self._ip = ip
     self._udp_timeout = udp_timeout
     self._general_state = None
     self._general_state_received = threading.Event()
     self._calibration_state = None
     self._calibration_state_received = threading.Event()
     self._udp_client = UdpClient(drone_ip=self._ip)
     self._exit_flag = threading.Event()
     self.daemon = True
Example #6
0
    def _wait_for_udp_communication(timeout: float, ip: str = "192.168.1.101"):
        """Simple helper for waiting for drone to come online

        Raises ConnectionError if no connection is established in the specified timeout.
        """
        temp_udp_client = UdpClient(drone_ip=ip)
        temp_udp_client._sock.settimeout(timeout)
        try:
            temp_udp_client.get_data_dict()
        except socket.timeout as e:
            raise ConnectionError(
                "Could not establish connection with drone") from e
Example #7
0
class _DroneStateWatcher(threading.Thread):
    """Subscribes to UDP messages from the drone and stores the latest data"""
    def __init__(self, ip: str = "192.168.1.101", udp_timeout: float = 3):
        threading.Thread.__init__(self)
        self._ip = ip
        self._udp_timeout = udp_timeout
        self._general_state = None
        self._general_state_received = threading.Event()
        self._calibration_state = None
        self._calibration_state_received = threading.Event()
        self._udp_client = UdpClient(drone_ip=self._ip)
        self._exit_flag = threading.Event()
        self.daemon = True

    @property
    def general_state(self) -> dict:
        if not self._general_state_received.wait(timeout=self._udp_timeout):
            raise TimeoutError("No state message received from drone")
        return self._general_state

    @property
    def calibration_state(self) -> dict:
        if not self._calibration_state_received.wait(
                timeout=self._udp_timeout):
            raise TimeoutError("No state message received from drone")
        return self._calibration_state

    def run(self):
        while not self._exit_flag.is_set():
            data_packet = self._udp_client.get_data_dict()
            if data_packet["command_type"] == 1:
                self._general_state = data_packet
                self._general_state_received.set()
            elif data_packet["command_type"] == 2:
                self._calibration_state = data_packet
                self._calibration_state_received.set()

    def stop(self):
        self._exit_flag.set()
Example #8
0
class _PioneerStateWatcher(threading.Thread):
    """Subscribes to UDP messages from the drone and stores the latest data
    """

    def __init__(self):
        threading.Thread.__init__(self)
        self._general_state = None
        self._calibration_state = None
        self._udpclient = UdpClient()
        self._exit_flag = threading.Event()
        self.daemon = True

    @property
    def general_state(self) -> dict:
        start = time.time()
        while self._general_state is None:
            if time.time() - start > 3:
                raise TimeoutError("No state message received from drone")
        return self._general_state

    @property
    def calibration_state(self) -> dict:
        start = time.time()
        while self._calibration_state is None:
            if time.time() - start > 3:
                raise TimeoutError("No state message received from drone")
        return self._calibration_state

    def run(self):
        while not self._exit_flag.is_set():
            data_packet = self._udpclient.get_data_dict()
            if data_packet["command_type"] == 1:
                self._general_state = data_packet
            elif data_packet["command_type"] == 2:
                self._calibration_state = data_packet

    def stop(self):
        self._exit_flag.set()