Ejemplo n.º 1
0
    def test_get_param(self):
        from rosmaster.paramserver import ParamDictionary
        param_server = ParamDictionary(None)

        val = random.randint(0, 10000)

        full_dict = {}
        
        # very similar to has param sequence
        self.failIf(param_server.has_param('/new_param'))
        self.failIf(param_server.has_param('/new_param/'))        
        self.assertGetParamFail(param_server, '/new_param')
        param_server.set_param('/new_param', val)
        full_dict['new_param'] = val
        self.assertEquals(val, param_server.get_param('/new_param'))
        self.assertEquals(val, param_server.get_param('/new_param/'))
        # - test homonym
        self.assertEquals(val, param_server.get_param('/new_param//'))
        
        # test full get
        self.assertEquals(full_dict, param_server.get_param('/'))
        
        # test with param in sub-namespace
        val = random.randint(0, 10000)        
        self.failIf(param_server.has_param('/sub/sub2/new_param2'))
        self.assertGetParamFail(param_server, '/sub/sub2/new_param2')
        param_server.set_param('/sub/sub2/new_param2', val)
        full_dict['sub'] = {'sub2': { 'new_param2': val }}
        self.assertEquals(val, param_server.get_param('/sub/sub2/new_param2'))
        # - test homonym
        self.assertEquals(val, param_server.get_param('/sub///sub2/new_param2/'))
        
        # test full get
        self.assertEquals(full_dict, param_server.get_param('/'))

        # test that parameter server namespace-get (#587)
        val1 = random.randint(0, 10000)
        val2 = random.randint(0, 10000)
        val3 = random.randint(0, 10000)
        
        for k in ['/gains/P', '/gains/I', '/gains/D', '/gains']:
            self.assertGetParamFail(param_server, k)
            self.failIf(param_server.has_param(k))

        param_server.set_param('/gains/P', val1)
        param_server.set_param('/gains/I', val2)
        param_server.set_param('/gains/D', val3)        

        pid = {'P': val1, 'I': val2, 'D': val3}
        full_dict['gains'] = pid
        self.assertEquals(pid,
                          param_server.get_param('/gains'))
        self.assertEquals(pid,
                          param_server.get_param('/gains/'))
        self.assertEquals(full_dict,
                          param_server.get_param('/'))

        self.failIf(param_server.has_param('/ns/gains/P'))
        self.failIf(param_server.has_param('/ns/gains/I'))
        self.failIf(param_server.has_param('/ns/gains/D'))
        self.failIf(param_server.has_param('/ns/gains'))
        
        param_server.set_param('/ns/gains/P', val1)
        param_server.set_param('/ns/gains/I', val2)
        param_server.set_param('/ns/gains/D', val3)
        full_dict['ns'] = {'gains': pid}
        
        self.assertEquals(pid,
                          param_server.get_param('/ns/gains'))
        self.assertEquals({'gains': pid},
                          param_server.get_param('/ns/'))
        self.assertEquals({'gains': pid},
                          param_server.get_param('/ns'))
        self.assertEquals(full_dict,
                          param_server.get_param('/'))
Ejemplo n.º 2
0
    def test_set_param(self):
        from rosmaster.paramserver import ParamDictionary
        param_server = ParamDictionary(None)
        caller_id = '/node'
        val = random.randint(0, 10000)

        # verify error behavior with root
        try:
            param_server.set_param('/', 1)
            self.fail("ParamDictionary allowed root to be set to non-dictionary")
        except: pass

        # very similar to has param sequence
        self.failIf(param_server.has_param('/new_param'))
        param_server.set_param('/new_param', val)
        self.assertEquals(val, param_server.get_param('/new_param'))
        self.assertEquals(val, param_server.get_param('/new_param/'))
        self.assert_(param_server.has_param('/new_param'))

        # test with param in sub-namespace
        val = random.randint(0, 10000)        
        self.failIf(param_server.has_param('/sub/sub2/new_param2'))
        param_server.set_param('/sub/sub2/new_param2', val)
        self.assertEquals(val, param_server.get_param('/sub/sub2/new_param2'))

        # test with param type mutation
        vals = ['a', {'a': 'b'}, 1, 1., 'foo', {'c': 'd'}, 4, {'a': {'b': 'c'}}, 3]
        for v in vals:
            param_server.set_param('/multi/multi_param', v)
            self.assertEquals(v, param_server.get_param('/multi/multi_param'))

        # - set value within subtree that mutates higher level value
        param_server.set_param('/multi2/multi_param', 1)
        self.assertEquals(1, param_server.get_param('/multi2/multi_param'))

        param_server.set_param('/multi2/multi_param/a', 2)
        self.assertEquals(2, param_server.get_param('/multi2/multi_param/a'))
        self.assertEquals({'a': 2}, param_server.get_param('/multi2/multi_param/'))        
        param_server.set_param('/multi2/multi_param/a/b', 3)
        self.assertEquals(3, param_server.get_param('/multi2/multi_param/a/b'))
        self.assertEquals({'b': 3}, param_server.get_param('/multi2/multi_param/a/'))
        self.assertEquals({'a': {'b': 3}}, param_server.get_param('/multi2/multi_param/'))        

        
        # test that parameter server namespace-set (#587)
        self.failIf(param_server.has_param('/gains/P'))
        self.failIf(param_server.has_param('/gains/I'))
        self.failIf(param_server.has_param('/gains/D'))                        
        self.failIf(param_server.has_param('/gains'))

        pid = {'P': random.randint(0, 10000), 'I': random.randint(0, 10000), 'D': random.randint(0, 10000)}
        param_server.set_param('/gains', pid)
        self.assertEquals(pid,  param_server.get_param('/gains'))
        self.assertEquals(pid['P'], param_server.get_param('/gains/P'))
        self.assertEquals(pid['I'], param_server.get_param('/gains/I'))
        self.assertEquals(pid['D'], param_server.get_param('/gains/D'))

        subns = {'gains1': pid, 'gains2': pid}
        param_server.set_param('/ns', subns)
        self.assertEquals(pid['P'], param_server.get_param('/ns/gains1/P'))
        self.assertEquals(pid['I'], param_server.get_param('/ns/gains1/I'))
        self.assertEquals(pid['D'], param_server.get_param('/ns/gains1/D'))
        self.assertEquals(pid, param_server.get_param('/ns/gains1'))
        self.assertEquals(pid, param_server.get_param('/ns/gains2'))
        self.assertEquals(subns, param_server.get_param('/ns/'))

        # test empty dictionary set
        param_server.set_param('/ns', {})
        # - param should still exist
        self.assert_(param_server.has_param('/ns/'))
        # - value should remain dictionary
        self.assertEquals({}, param_server.get_param('/ns/'))
        # - value2 below /ns/ should be erased
        self.failIf(param_server.has_param('/ns/gains1'))
        self.failIf(param_server.has_param('/ns/gains1/P'))
        
        # verify that root can be set and that it erases all values
        param_server.set_param('/', {})
        self.failIf(param_server.has_param('/new_param'))
        param_server.set_param('/', {'foo': 1, 'bar': 2, 'baz': {'a': 'a'}})
        self.assertEquals(1, param_server.get_param('/foo'))
        self.assertEquals(1, param_server.get_param('/foo/'))        
        self.assertEquals(2, param_server.get_param('/bar'))
        self.assertEquals(2, param_server.get_param('/bar/'))
        self.assertEquals('a', param_server.get_param('/baz/a'))
        self.assertEquals('a', param_server.get_param('/baz/a/'))
Ejemplo n.º 3
0
    def test_subscribe_param_tree(self):
        from rosmaster.registrations import RegistrationManager
        from rosmaster.paramserver import ParamDictionary

        # setup node and subscriber data
        reg_manager = RegistrationManager(ThreadPoolMock())
        param_server = ParamDictionary(reg_manager)

        # Test Parameter Tree Subscriptions

        # simple case - subscribe and set whole tree
        gains = {'p': 'P', 'i': 'I', 'd' : 'D'}
        self.assertEquals({}, param_server.subscribe_param('/gains', ('ptnode', 'http://ptnode:1')))
        param_server.set_param('/gains', gains.copy(), notify_task=self.notify_task)
        self.assertEquals([([('ptnode', 'http://ptnode:1')], '/gains/', gains), ], self.last_update)
        # - test with trailing slash
        param_server.set_param('/gains/', gains.copy(), notify_task=self.notify_task)
        self.assertEquals([([('ptnode', 'http://ptnode:1')], '/gains/', gains), ], self.last_update)

        # change params within tree
        param_server.set_param('/gains/p', 'P2', notify_task=self.notify_task)
        self.assertEquals([([('ptnode', 'http://ptnode:1')], '/gains/p/', 'P2'), ], self.last_update)
        param_server.set_param('/gains/i', 'I2', notify_task=self.notify_task)
        self.assertEquals([([('ptnode', 'http://ptnode:1')], '/gains/i/', 'I2'), ], self.last_update)

        # test overlapping subscriptions
        self.assertEquals('P2', param_server.subscribe_param('/gains/p', ('ptnode2', 'http://ptnode2:2')))
        param_server.set_param('/gains', gains.copy(), notify_task=self.notify_task)
        self.assertEquals([([('ptnode', 'http://ptnode:1')], '/gains/', gains), \
                           ([('ptnode2', 'http://ptnode2:2')], '/gains/p/', 'P'), \
                           ], self.last_update)
        # - retest with trailing slash on subscribe
        self.last_update = None
        self.assertEquals('P', param_server.subscribe_param('/gains/p/', ('ptnode2', 'http://ptnode2:2')))
        param_server.set_param('/gains', gains.copy(), notify_task=self.notify_task)
        self.assertEquals([([('ptnode', 'http://ptnode:1')], '/gains/', gains), \
                           ([('ptnode2', 'http://ptnode2:2')], '/gains/p/', 'P'), \
                           ], self.last_update)
        # test with overlapping (change to sub param)
        param_server.set_param('/gains/p', 'P3', notify_task=self.notify_task)
        # - this is a bit overtuned as a more optimal ps could use one update
        ptnode2 = ([('ptnode2', 'http://ptnode2:2')], '/gains/p/', 'P3')
        ptnode = ([('ptnode', 'http://ptnode:1')], '/gains/p/', 'P3')
        self.assertTrue(len(self.last_update) == 2)
        self.assertTrue(ptnode2 in self.last_update)
        self.assertTrue(ptnode in self.last_update)

        # virtual deletion: subscribe to subparam, parameter tree reset
        self.last_update = None
        param_server.set_param('/gains2', gains.copy(), notify_task=self.notify_task)
        self.assertEquals('P', param_server.subscribe_param('/gains2/p/', ('ptnode3', 'http://ptnode3:3')))
        # - erase the sub parameters
        param_server.set_param('/gains2', {}, notify_task=self.notify_task)        
        self.assertEquals([([('ptnode3', 'http://ptnode3:3')], '/gains2/p/', {}), ], self.last_update)        

        #Final test: test subscription to entire tree
        self.last_update = None
        param_server.delete_param('/gains')
        param_server.delete_param('/gains2')        
        self.assertEquals({}, param_server.get_param('/'))
        self.assertEquals({}, param_server.subscribe_param('/', ('allnode', 'http://allnode:1')))
        param_server.set_param('/one', 1, notify_task=self.notify_task)
        self.assertEquals([([('allnode', 'http://allnode:1')], '/one/', 1), ], self.last_update)
        param_server.set_param('/two', 2, notify_task=self.notify_task)
        self.assertEquals([([('allnode', 'http://allnode:1')], '/two/', 2), ], self.last_update)
        param_server.set_param('/foo/bar', 'bar', notify_task=self.notify_task)
        self.assertEquals([([('allnode', 'http://allnode:1')], '/foo/bar/', 'bar'), ], self.last_update)