class TestMemoryMapping(unittest.TestCase):
    def setUp(self):
        cflib.crtp.init_drivers(enable_debug_driver=False)
        self.test_rig_support = RigSupport()

    def test_memory_mapping_with_one_cf(self):
        # Fixture
        uri = self.test_rig_support.all_uris[0]
        self.test_rig_support.restart_devices([uri])
        cf = Crazyflie(rw_cache='./cache')

        # Test and Assert
        with SyncCrazyflie(uri, cf=cf) as scf:
            self.assert_memory_mapping(scf)

    def test_memory_mapping_with_all_cfs(self):
        # Fixture
        uris = self.test_rig_support.all_uris
        self.test_rig_support.restart_devices(uris)
        factory = CachedCfFactory(rw_cache='./cache')

        # Test and Assert
        with Swarm(uris, factory=factory) as swarm:
            swarm.parallel_safe(self.assert_memory_mapping)

    def assert_memory_mapping(self, scf):
        mems = scf.cf.mem.get_mems(MemoryElement.TYPE_MEMORY_TESTER)
        count = len(mems)
        self.assertEqual(1, count, 'unexpected number of memories found')

        self.verify_reading_memory_data(mems)
        self.verify_writing_memory_data(mems, scf)

    def verify_writing_memory_data(self, mems, scf):
        self.wrote_data = False
        scf.cf.param.set_value('memTst.resetW', '1')
        time.sleep(0.1)
        mems[0].write_data(5, 1000, self._data_written)
        while not self.wrote_data:
            time.sleep(1)
        log_conf = LogConfig(name='memtester', period_in_ms=100)
        log_conf.add_variable('memTst.errCntW', 'uint32_t')
        with SyncLogger(scf, log_conf) as logger:
            for log_entry in logger:
                errorCount = log_entry[1]['memTst.errCntW']
                self.assertEqual(0, errorCount)
                break

    def verify_reading_memory_data(self, mems):
        self.got_data = False
        mems[0].read_data(5, 1000, self._data_read)
        while not self.got_data:
            time.sleep(1)

    def _data_read(self, mem):
        self.got_data = True

    def _data_written(self, mem, address):
        self.wrote_data = True
Ejemplo n.º 2
0
class TestLogging(unittest.TestCase):
    def setUp(self):
        cflib.crtp.init_drivers()
        self.test_rig_support = RigSupport()

    def test_that_requested_logging_is_received_properly_from_one_cf(self):
        # Fixture
        uri = self.test_rig_support.all_uris[0]
        self.test_rig_support.restart_devices([uri])
        cf = Crazyflie(rw_cache='./cache')

        # Test and Assert
        with SyncCrazyflie(uri, cf=cf) as scf:
            self.assert_add_logging_and_get_non_zero_value(scf)

    def test_that_requested_logging_is_received_properly_from_all_cfs(self):
        # Fixture
        uris = self.test_rig_support.all_uris
        self.test_rig_support.restart_devices(uris)
        factory = CachedCfFactory(rw_cache='./cache')

        # Test and Assert
        with Swarm(uris, factory=factory) as swarm:
            swarm.parallel_safe(self.assert_add_logging_and_get_non_zero_value)

    def assert_add_logging_and_get_non_zero_value(self, scf):
        log_name = 'stabilizer.roll'
        expected = 0.0

        lg_conf = LogConfig(name='SysTest', period_in_ms=10)
        lg_conf.add_variable(log_name, 'float')

        with SyncLogger(scf, lg_conf) as logger:
            for log_entry in logger:
                actual = log_entry[1][log_name]
                break

        self.assertNotAlmostEqual(expected, actual, places=4)
Ejemplo n.º 3
0
 def setUp(self):
     cflib.crtp.init_drivers()
     self.test_rig_support = RigSupport()
Ejemplo n.º 4
0
class TestConnection(unittest.TestCase):
    def setUp(self):
        cflib.crtp.init_drivers()
        self.test_rig_support = RigSupport()

    def test_that_connection_time_scales_with_more_devices_without_cache(self):
        # Fixture
        self.test_rig_support.restart_devices(self.test_rig_support.all_uris)

        EXPECTED_CONNECTION_TIME = 5

        for nr_of_devices in range(1, len(self.test_rig_support.all_uris)):
            # Test
            uris = self.test_rig_support.all_uris[:nr_of_devices]

            start_time = time.time()
            with Swarm(uris):
                connected_time = time.time()

            actual = connected_time - start_time
            max_expected = EXPECTED_CONNECTION_TIME * nr_of_devices
            print('Connection time for', nr_of_devices, ':', actual,
                  ', per device:', actual / nr_of_devices)

            # Assert
            self.assertLess(actual, max_expected)

    def test_that_connection_time_scales_with_more_devices_with_cache(self):
        # Fixture
        self.test_rig_support.restart_devices(self.test_rig_support.all_uris)

        # Fill caches first by connecting to all devices
        factory = CachedCfFactory(rw_cache='./cache')
        with Swarm(self.test_rig_support.all_uris, factory=factory):
            pass

        EXPECTED_CONNECTION_TIME = 1.5

        for nr_of_devices in range(1, len(self.test_rig_support.all_uris)):
            # Test
            uris = self.test_rig_support.all_uris[:nr_of_devices]

            start_time = time.time()
            with Swarm(uris, factory=factory):
                connected_time = time.time()

            actual = connected_time - start_time
            max_expected = EXPECTED_CONNECTION_TIME * nr_of_devices
            print('Connection time for', nr_of_devices, ':', actual,
                  ', per device:', actual / nr_of_devices)

            # Assert
            self.assertLess(actual, max_expected)

    def test_that_all_devices_are_restarted(self):
        # Fixture
        uris = self.test_rig_support.all_uris

        # Test
        # Assert
        self.test_rig_support.restart_devices(uris)

    def test_that_all_devices_are_restarted_multiple_times(self):
        # Fixture
        uris = self.test_rig_support.all_uris

        # Test
        # Assert
        for i in range(10):
            self.test_rig_support.restart_devices(uris)

    def test_that_the_same_cf_object_can_be_connected_multiple_times(self):
        # Fixture
        self.test_rig_support.restart_devices(self.test_rig_support.all_uris)
        cf = Crazyflie(rw_cache='./cache')

        # Test
        for uri in self.test_rig_support.all_uris:
            with SyncCrazyflie(uri, cf=cf):
                pass
    def setUp(self):
        cflib.crtp.init_drivers(enable_debug_driver=False)
        self.test_rig_support = RigSupport()

        self.links = []
class TestResponseTime(unittest.TestCase):
    ECHO = 0

    def setUp(self):
        cflib.crtp.init_drivers(enable_debug_driver=False)
        self.test_rig_support = RigSupport()

        self.links = []

    def tearDown(self):
        for link in self.links:
            link.close()
        self.links = []

    def test_response_time_to_one_cf(self):
        # Fixture
        uri = self.test_rig_support.all_uris[0]
        self.test_rig_support.restart_devices([uri])
        link = self.connect_link(uri)
        seq_nr = 47
        expected_max_response_time = 0.01

        # Test
        time_send_echo = time.time()
        self.request_echo_with_seq_nr(link, seq_nr)
        response_timestamps = self.assert_wait_for_all_seq_nrs([link], seq_nr)
        response_time = response_timestamps[uri] - time_send_echo

        # Assert
        self.assertLess(response_time, expected_max_response_time)

    def test_response_time_to_all_cfs(self):
        # Fixture
        uris = self.test_rig_support.all_uris
        self.test_rig_support.restart_devices(uris)

        for uri in uris:
            self.connect_link(uri)

        seq_nr = 47
        expected_max_response_time = 0.1
        expected_mean_response_time = 0.05

        # Test
        time_send_echo = time.time()
        for link in self.links:
            self.request_echo_with_seq_nr(link, seq_nr)

        response_timestamps = self.assert_wait_for_all_seq_nrs(
            self.links, seq_nr)

        # Assert
        response_times = {}
        for uri, response_time in response_timestamps.items():
            response_times[uri] = response_time - time_send_echo

        times = response_times.values()
        max_time = max(times)
        mean_time = float(sum(times)) / len(times)

        # print(max_time, mean_time, times)
        self.assertLess(max_time, expected_max_response_time)
        self.assertLess(mean_time, expected_mean_response_time)

    def request_echo_with_seq_nr(self, link, seq_nr):
        pk = CRTPPacket()
        pk.set_header(CRTPPort.LINKCTRL, self.ECHO)
        pk.data = (seq_nr, )
        link.send_packet(pk)

    def assert_wait_for_all_seq_nrs(self, links, seq_nr, timeout=1):
        NO_BLOCKING = -1

        time_end = time.time() + timeout
        response_timestamps = {}
        while time.time() < time_end:
            for link in links:
                if link.uri not in response_timestamps:
                    response = link.receive_packet(time=NO_BLOCKING)
                    if self._is_response_correct_seq_nr(response, seq_nr):
                        response_timestamps[link.uri] = time.time()

            if len(response_timestamps) == len(self.links):
                return response_timestamps

            time.sleep(0.001)

        self.fail('Time out while waiting for seq nrs.')

    def _is_response_correct_seq_nr(self, response, seq_nr):
        if response is not None:
            if response._get_channel() == self.ECHO and \
                    response._get_port() == CRTPPort.LINKCTRL:
                received_seq = response._get_data_t()[0]
                if received_seq == seq_nr:
                    return True

        return False

    def connect_link(self, uri):
        link = cflib.crtp.get_link_driver(uri, self._link_quality_cb,
                                          self._link_error_cb)
        self.assertIsNotNone(link)
        self.links.append(link)

        return link

    def _link_quality_cb(self, percentage):
        pass

    def _link_error_cb(self, errmsg):
        self.fail(errmsg)