示例#1
0
    def putIntoParamServer(self, data):

        try:
            self.succeed(get_param_server().setParam(self.get_caller_id(),
                                                     "hyq", data))

        except socket.error:
            raise ROSParamIOException("Unable to communicate with master!")
        print "set parameter [%s] to [%s]" % ('hyq', data)
        eval(help(get_param_server().setParam()))
        pass
示例#2
0
文件: rosparam.py 项目: daju1-ros/ros
def _set_param(param, value, verbose=False):
    """
    Set param on the Parameter Server. Unlike L{set_param()}, this
    takes in a Python value to set instead of YAML.
    
    @param param: parameter name
    @type  param: str
    @param value XmlRpcLegalValue: value to upload
    @type  value: XmlRpcLegalValue
    """
    if type(value) == dict:
        # #1098 changing dictionary behavior to be an update, rather
        # than replace behavior.
        for k, v in value.iteritems():
            # dictionary keys must be non-unicode strings
            if isinstance(k, str):
                _set_param(ns_join(param, k), v, verbose=verbose)
            else:
                raise ROSParamException("YAML dictionaries must have string keys. Invalid dictionary is:\n%s"%value)
    else:
        if type(value) == long:
            if value > sys.maxint:
                raise ROSParamException("Overflow: Parameter Server integers must be 32-bit signed integers:\n\t-%s <= value <= %s"%(sys.maxint-1, sys.maxint))
            
        try:
            _succeed(get_param_server().setParam(_get_caller_id(), param, value))
        except socket.error:
            raise ROSParamIOException("Unable to communicate with master!")
        if verbose:
            print "set parameter [%s] to [%s]"%(param, value)
示例#3
0
def set_param_raw(param, value, verbose=False):
    """
    Set param on the Parameter Server. Unlike L{set_param()}, this
    takes in a Python value to set instead of YAML.
    
    @param param: parameter name
    @type  param: str
    @param value XmlRpcLegalValue: value to upload
    @type  value: XmlRpcLegalValue
    """
    if type(value) == dict:
        # #1098 changing dictionary behavior to be an update, rather
        # than replace behavior.
        for k, v in value.iteritems():
            # dictionary keys must be non-unicode strings
            if isinstance(k, str):
                set_param_raw(ns_join(param, k), v, verbose=verbose)
            else:
                raise ROSParamException("YAML dictionaries must have string keys. Invalid dictionary is:\n%s" % value)
    else:
        if type(value) == long:
            if value > sys.maxint:
                raise ROSParamException(
                    "Overflow: Parameter Server integers must be 32-bit signed integers:\n\t-%s <= value <= %s"
                    % (sys.maxint - 1, sys.maxint)
                )

        try:
            _succeed(get_param_server().setParam(_get_caller_id(), param, value))
        except socket.error:
            raise ROSParamIOException("Unable to communicate with master!")
        if verbose:
            print "set parameter [%s] to [%s]" % (param, value)
示例#4
0
    def test_rosparam_set(self):
        from ros import rosparam
        cmd = 'rosparam'

        ps = get_param_server()
        with fakestdout() as b:
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/test1", "1"])
            self.assertEquals(1, ps.getParam('/', '/rosparam_set/test1')[2])
        with fakestdout() as b:            
            # -- verbose
            rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/test1", "1"])
            self.assertEquals(1, ps.getParam('/', '/rosparam_set/test1')[2])
        with fakestdout() as b:            
            rosparam.yamlmain([cmd, 'set', "rosparam_set/test1", "2"])
            self.assertEquals(2, ps.getParam('/', '/rosparam_set/test1')[2])
            
        with fakestdout() as b:
            # - floats
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "1.0"])
            self.assertEquals(1., ps.getParam('/', '/rosparam_set/test2')[2])
        with fakestdout() as b:
            # - floats
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "2.0"])
            self.assertEquals(2., ps.getParam('/', '/rosparam_set/test2')[2])
        with fakestdout() as b:            
            # - booleans
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/testbool", "true"])
            self.assertEquals(True, ps.getParam('/', '/rosparam_set/testbool')[2])
        with fakestdout() as b:            
            # - strings
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/teststr", "hi"])
            self.assertEquals("hi", ps.getParam('/', '/rosparam_set/teststr')[2])
        with fakestdout() as b: 
            # - list
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/testlist", "[1, 2, 3]"])
            self.assertEquals([1, 2, 3], ps.getParam('/', '/rosparam_set/testlist')[2])
        with fakestdout() as b: 
            # - dictionary
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/testdict", "{a: b, c: d}"])
            self.assertEquals('b', ps.getParam('/', '/rosparam_set/testdict/a')[2])
            self.assertEquals('d', ps.getParam('/', '/rosparam_set/testdict/c')[2])
        with fakestdout() as b:             
            #   - empty dictionary should be a noop
            rosparam.yamlmain([cmd, 'set', "set/testdict", "{}"])
            self.assertEquals('b', ps.getParam('/', '/rosparam_set/testdict/a')[2])
            self.assertEquals('d', ps.getParam('/', '/rosparam_set/testdict/c')[2])
        with fakestdout() as b:                         
            #   - this should be an update
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/testdict", "{e: f, g: h}"])
            self.assertEquals('b', ps.getParam('/', '/rosparam_set/testdict/a')[2])
            self.assertEquals('d', ps.getParam('/', '/rosparam_set/testdict/c')[2])
            self.assertEquals('f', ps.getParam('/', '/rosparam_set/testdict/e')[2])
            self.assertEquals('h', ps.getParam('/', '/rosparam_set/testdict/g')[2])
        with fakestdout() as b:                                     
            # -- verbose
            rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/testdictverbose", "{e: f, g: h}"])
            self.assertEquals('f', ps.getParam('/', '/rosparam_set/testdictverbose/e')[2])
            self.assertEquals('h', ps.getParam('/', '/rosparam_set/testdictverbose/g')[2])
示例#5
0
    def test_rosparam_delete(self):
        from ros import rosparam
        cmd = 'rosparam'
        ps = get_param_server()

        try:
            rosparam.yamlmain([cmd, 'delete'])
        except SystemExit, e:
            self.assertNotEquals(0, e.code)
示例#6
0
    def test_rosparam_delete(self):
        from ros import rosparam
        cmd = 'rosparam'
        ps = get_param_server()

        try:
            rosparam.yamlmain([cmd, 'delete'])
        except SystemExit, e:
            self.assertNotEquals(0, e.code)
示例#7
0
文件: rosparam.py 项目: daju1-ros/ros
def get_param(param):
    """
    Download a parameter from Parameter Server

    @param param: parameter name to retrieve from parameter
        server. If param is a parameter namespace, entire parameter
        subtree will be downloaded.
    @type  param: str
    """
    try:
        return _succeed(get_param_server().getParam(_get_caller_id(), param))
    except socket.error:
        raise ROSParamIOException("Unable to communicate with master!")
示例#8
0
    def test_rosparam_dump(self):
        from ros import rosparam
        import roslib.packages
        f = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'), 'test', 'test.yaml')
        f_out = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'), 'test', 'test_dump.yaml')
        
        cmd = 'rosparam'
        ps = get_param_server()

        try:
            rosparam.yamlmain([cmd, 'dump'])
        except SystemExit, e:
            self.assertNotEquals(0, e.code)
示例#9
0
文件: rosparam.py 项目: daju1-ros/ros
def delete_param(param, verbose=False):
    """
    Delete a parameter from the Parameter Server
    @param param: parameter name
    @type  param: str
    @param verbose: print verbose output for debugging
    @type  verbose: bool
    """
    try:
        if param == GLOBALNS:
            # not allowed to delete the root of the tree as it must always
            # have a value. the equivalent command is setting the root to an
            # empty dictionary
            _succeed(get_param_server().setParam(_get_caller_id(), GLOBALNS, {})) 
            if verbose:
                print "deleted ENTIRE parameter server"
        else:
            _succeed(get_param_server().deleteParam(_get_caller_id(), param))
            if verbose:
                print "deleted parameter [%s]"%param
    except socket.error:
        raise ROSParamIOException("Unable to communicate with master!")
示例#10
0
文件: rosparam.py 项目: daju1-ros/ros
def list_params(ns):
    """
    Get list of parameters in ns
    @param ns: namespace to match
    @type  ns: str
    """
    try:
        ns = make_global_ns(ns)
        names = _succeed(get_param_server().getParamNames(_get_caller_id()))
        names.sort()
        return [n for n in names if n.startswith(ns)]
    except socket.error:
        raise ROSParamIOException("Unable to communicate with master!")
示例#11
0
def get_param(param):
    """
    Download a parameter from Parameter Server

    @param param: parameter name to retrieve from parameter
        server. If param is a parameter namespace, entire parameter
        subtree will be downloaded.
    @type  param: str
    """
    try:
        return _succeed(get_param_server().getParam(_get_caller_id(), param))
    except socket.error:
        raise ROSParamIOException("Unable to communicate with master!")
示例#12
0
def list_params(ns):
    """
    Get list of parameters in ns
    @param ns: namespace to match
    @type  ns: str
    """
    try:
        ns = make_global_ns(ns)
        names = _succeed(get_param_server().getParamNames(_get_caller_id()))
        names.sort()
        return [n for n in names if n.startswith(ns)]
    except socket.error:
        raise ROSParamIOException("Unable to communicate with master!")
示例#13
0
def delete_param(param, verbose=False):
    """
    Delete a parameter from the Parameter Server
    @param param: parameter name
    @type  param: str
    @param verbose: print verbose output for debugging
    @type  verbose: bool
    """
    try:
        if param == GLOBALNS:
            # not allowed to delete the root of the tree as it must always
            # have a value. the equivalent command is setting the root to an
            # empty dictionary
            _succeed(get_param_server().setParam(_get_caller_id(), GLOBALNS, {}))
            if verbose:
                print "deleted ENTIRE parameter server"
        else:
            _succeed(get_param_server().deleteParam(_get_caller_id(), param))
            if verbose:
                print "deleted parameter [%s]" % param
    except socket.error:
        raise ROSParamIOException("Unable to communicate with master!")
示例#14
0
    def test_rosparam_dump(self):
        from ros import rosparam
        import roslib.packages
        f = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'), 'test',
                         'test.yaml')
        f_out = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'),
                             'test', 'test_dump.yaml')

        cmd = 'rosparam'
        ps = get_param_server()

        try:
            rosparam.yamlmain([cmd, 'dump'])
        except SystemExit, e:
            self.assertNotEquals(0, e.code)
示例#15
0
def _get_param(param):
    return succeed(get_param_server().getParam(_get_caller_id(), param))
    def test_rosparam(self):
        ps = get_param_server()

        # network is initialized
        cmd = 'rosparam'
        names = ['/chatter', 'foo/chatter']

        # list
        params = ['/string', '/int', '/float',
                  '/g1/string', '/g1/int', '/g1/float',
                  '/g2/string', '/g2/int', '/g2/float',
                  ]
        # - we aren't matching against the core services as those can make the test suites brittle
        output = Popen([cmd, 'list'], stdout=PIPE).communicate()[0]
        l = set(output.split())
        for t in params:
            self.assert_(t in l)

        # get
        # - strings
        output = Popen([cmd, 'get', "string"], stdout=PIPE).communicate()[0]
        self.assertEquals('foo-value', output.strip())
        # -- pretty
        output = Popen([cmd, 'get', '-p', "string"], stdout=PIPE).communicate()[0]
        self.assertEquals('foo-value', output.strip())
        output = Popen([cmd, 'get', "/string"], stdout=PIPE).communicate()[0]
        self.assertEquals('foo-value', output.strip())
        output = Popen([cmd, 'get', "g1/string"], stdout=PIPE).communicate()[0]
        self.assertEquals('g1-foo-value', output.strip())
        output = Popen([cmd, 'get', "/g1/string"], stdout=PIPE).communicate()[0]
        self.assertEquals('g1-foo-value', output.strip())
        output = Popen([cmd, 'get', "/g2/string"], stdout=PIPE).communicate()[0]
        self.assertEquals('g2-foo-value', output.strip())
        # - ints
        output = Popen([cmd, 'get', "int"], stdout=PIPE).communicate()[0]
        self.assertEquals('1', output.strip())
        # -- pretty
        output = Popen([cmd, 'get', '-p', "int"], stdout=PIPE).communicate()[0]
        self.assertEquals('1', output.strip())
        output = Popen([cmd, 'get', "/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('1', output.strip())
        output = Popen([cmd, 'get', "g1/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('10', output.strip())
        output = Popen([cmd, 'get', "/g1/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('10', output.strip())
        output = Popen([cmd, 'get', "/g2/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('20', output.strip())
        # - floats
        output = Popen([cmd, 'get', "float"], stdout=PIPE).communicate()[0]
        self.assertEquals('1.0', output.strip())
        # -- pretty
        output = Popen([cmd, 'get', '-p', "float"], stdout=PIPE).communicate()[0]
        self.assertEquals('1.0', output.strip())
        output = Popen([cmd, 'get', "/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('1.0', output.strip())
        output = Popen([cmd, 'get', "g1/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('10.0', output.strip())
        output = Popen([cmd, 'get', "/g1/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('10.0', output.strip())
        output = Popen([cmd, 'get', "/g2/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('20.0', output.strip())
        # - dictionary
        output = Popen([cmd, 'get', "g1"], stdout=PIPE).communicate()[0]
        import yaml
        d = yaml.load(output)
        self.assertEquals(d['float'], 10.0)
        self.assertEquals(d['int'], 10.0)
        self.assertEquals(d['string'], "g1-foo-value")
        self.assertEquals(set(['float', 'int', 'string']), set(d.keys()))

        # -- don't bother parsing pretty output of dictionary, but check for no errors
        check_call([cmd, 'get', '-p', "g1"])
        # --- with verbose
        check_call([cmd, 'get', '-pv', "g1"])
        
        # set
        # - integers
        Popen([cmd, 'set', "/set/test1", "1"], stdout=PIPE).communicate()[0]
        self.assertEquals(1, ps.getParam('/', '/set/test1')[2])
        # -- verbose
        Popen([cmd, 'set', '-v', "/set/test1", "1"], stdout=PIPE).communicate()[0]
        self.assertEquals(1, ps.getParam('/', '/set/test1')[2])
        Popen([cmd, 'set', "set/test1", "2"], stdout=PIPE).communicate()[0]
        self.assertEquals(2, ps.getParam('/', '/set/test1')[2])
        # - floats
        Popen([cmd, 'set', "/set/test2", "1.0"], stdout=PIPE).communicate()[0]
        self.assertEquals(1, ps.getParam('/', '/set/test2')[2])
        Popen([cmd, 'set', "set/test2", "2.0"], stdout=PIPE).communicate()[0]
        self.assertEquals(2, ps.getParam('/', '/set/test2')[2])
        # - booleans
        Popen([cmd, 'set', "/set/testbool", "true"], stdout=PIPE).communicate()[0]
        self.assertEquals(True, ps.getParam('/', '/set/testbool')[2])
        Popen([cmd, 'set', "set/testbool", "false"], stdout=PIPE).communicate()[0]
        self.assertEquals(False, ps.getParam('/', '/set/testbool')[2])
        # - strings
        #   TODO: test more interesting encodings, like multi-line
        Popen([cmd, 'set', "/set/teststr", "hi"], stdout=PIPE).communicate()[0]
        self.assertEquals("hi", ps.getParam('/', '/set/teststr')[2])
        Popen([cmd, 'set', "set/teststr", "hello world"], stdout=PIPE).communicate()[0]
        self.assertEquals("hello world", ps.getParam('/', '/set/teststr')[2])
        Popen([cmd, 'set', "set/teststr", "'true'"], stdout=PIPE).communicate()[0]
        self.assertEquals("true", ps.getParam('/', '/set/teststr')[2])
        # - list
        Popen([cmd, 'set', "set/testlist", "[]"], stdout=PIPE).communicate()[0]
        self.assertEquals([], ps.getParam('/', '/set/testlist')[2])
        Popen([cmd, 'set', "/set/testlist", "[1, 2, 3]"], stdout=PIPE).communicate()[0]
        self.assertEquals([1, 2, 3], ps.getParam('/', '/set/testlist')[2])
        # - dictionary
        Popen([cmd, 'set', "/set/testdict", "{a: b, c: d}"], stdout=PIPE).communicate()[0]
        self.assertEquals('b', ps.getParam('/', '/set/testdict/a')[2])
        self.assertEquals('d', ps.getParam('/', '/set/testdict/c')[2])
        #   - empty dictionary should be a noop
        Popen([cmd, 'set', "set/testdict", "{}"], stdout=PIPE).communicate()[0]
        self.assertEquals('b', ps.getParam('/', '/set/testdict/a')[2])
        self.assertEquals('d', ps.getParam('/', '/set/testdict/c')[2])
        #   - this should be an update
        Popen([cmd, 'set', "/set/testdict", "{e: f, g: h}"], stdout=PIPE).communicate()[0]
        self.assertEquals('b', ps.getParam('/', '/set/testdict/a')[2])
        self.assertEquals('d', ps.getParam('/', '/set/testdict/c')[2])
        self.assertEquals('f', ps.getParam('/', '/set/testdict/e')[2])
        self.assertEquals('h', ps.getParam('/', '/set/testdict/g')[2])
        # -- verbose
        check_call([cmd, 'set', '-v', "/set/testdictverbose", "{e: f, g: h}"])
        
        # delete
        ps.setParam('/', '/delete/me', True)
        self.assert_(ps.hasParam('/', '/delete/me')[2])
        Popen([cmd, 'delete', "/delete/me"], stdout=PIPE).communicate()[0]
        self.failIf(ps.hasParam('/', '/delete/me')[2])
示例#17
0
        f = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'), 'test', 'test.yaml')
        f_ns = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'), 'test', 'test_ns.yaml')
        
        cmd = 'rosparam'
        try:
            rosparam.yamlmain([cmd, 'load'])
            self.fail("command-line arg should have failed")
        except SystemExit, e:
            self.assertNotEquals(0, e.code)
        try:
            rosparam.yamlmain([cmd, 'load', 'fake-file.yaml'])
            self.fail("command-line arg should have failed")
        except SystemExit, e:
            self.assertNotEquals(0, e.code)

        ps = get_param_server()

        # load into top-level
        rosparam.yamlmain([cmd, 'load', f])
        self.assertEquals('bar', ps.getParam('/', '/foo')[2])
        # - make sure it did an overlay, not erase
        self.assertEquals('foo-value', ps.getParam('/', '/string')[2])
        
        rosparam.yamlmain([cmd, 'load', '-v', f])
        self.assertEquals('bar', ps.getParam('/', '/foo')[2])
        
        # load into namespace
        rosparam.yamlmain([cmd, 'load', f, '/rosparam_load/test'])
        self.assertEquals('bar', ps.getParam('/', '/rosparam_load/test/foo')[2])
        rosparam.yamlmain([cmd, 'load', '-v', f, '/rosparam_load/test'])
        self.assertEquals('bar', ps.getParam('/', '/rosparam_load/test/foo')[2])
示例#18
0
    def test_rosparam_set(self):
        from ros import rosparam
        cmd = 'rosparam'

        ps = get_param_server()
        with fakestdout() as b:
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/test1", "1"])
            self.assertEquals(1, ps.getParam('/', '/rosparam_set/test1')[2])
        with fakestdout() as b:
            # -- verbose
            rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/test1", "1"])
            self.assertEquals(1, ps.getParam('/', '/rosparam_set/test1')[2])
        with fakestdout() as b:
            rosparam.yamlmain([cmd, 'set', "rosparam_set/test1", "2"])
            self.assertEquals(2, ps.getParam('/', '/rosparam_set/test1')[2])

        with fakestdout() as b:
            # - floats
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "1.0"])
            self.assertEquals(1., ps.getParam('/', '/rosparam_set/test2')[2])
        with fakestdout() as b:
            # - floats
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "2.0"])
            self.assertEquals(2., ps.getParam('/', '/rosparam_set/test2')[2])
        with fakestdout() as b:
            # - booleans
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/testbool", "true"])
            self.assertEquals(True,
                              ps.getParam('/', '/rosparam_set/testbool')[2])
        with fakestdout() as b:
            # - strings
            rosparam.yamlmain([cmd, 'set', "/rosparam_set/teststr", "hi"])
            self.assertEquals("hi",
                              ps.getParam('/', '/rosparam_set/teststr')[2])
        with fakestdout() as b:
            # - list
            rosparam.yamlmain(
                [cmd, 'set', "/rosparam_set/testlist", "[1, 2, 3]"])
            self.assertEquals([1, 2, 3],
                              ps.getParam('/', '/rosparam_set/testlist')[2])
        with fakestdout() as b:
            # - dictionary
            rosparam.yamlmain(
                [cmd, 'set', "/rosparam_set/testdict", "{a: b, c: d}"])
            self.assertEquals('b',
                              ps.getParam('/', '/rosparam_set/testdict/a')[2])
            self.assertEquals('d',
                              ps.getParam('/', '/rosparam_set/testdict/c')[2])
        with fakestdout() as b:
            #   - empty dictionary should be a noop
            rosparam.yamlmain([cmd, 'set', "set/testdict", "{}"])
            self.assertEquals('b',
                              ps.getParam('/', '/rosparam_set/testdict/a')[2])
            self.assertEquals('d',
                              ps.getParam('/', '/rosparam_set/testdict/c')[2])
        with fakestdout() as b:
            #   - this should be an update
            rosparam.yamlmain(
                [cmd, 'set', "/rosparam_set/testdict", "{e: f, g: h}"])
            self.assertEquals('b',
                              ps.getParam('/', '/rosparam_set/testdict/a')[2])
            self.assertEquals('d',
                              ps.getParam('/', '/rosparam_set/testdict/c')[2])
            self.assertEquals('f',
                              ps.getParam('/', '/rosparam_set/testdict/e')[2])
            self.assertEquals('h',
                              ps.getParam('/', '/rosparam_set/testdict/g')[2])
        with fakestdout() as b:
            # -- verbose
            rosparam.yamlmain([
                cmd, 'set', '-v', "/rosparam_set/testdictverbose",
                "{e: f, g: h}"
            ])
            self.assertEquals(
                'f',
                ps.getParam('/', '/rosparam_set/testdictverbose/e')[2])
            self.assertEquals(
                'h',
                ps.getParam('/', '/rosparam_set/testdictverbose/g')[2])
示例#19
0
        f_ns = os.path.join(roslib.packages.get_pkg_dir('test_rosparam'),
                            'test', 'test_ns.yaml')

        cmd = 'rosparam'
        try:
            rosparam.yamlmain([cmd, 'load'])
            self.fail("command-line arg should have failed")
        except SystemExit, e:
            self.assertNotEquals(0, e.code)
        try:
            rosparam.yamlmain([cmd, 'load', 'fake-file.yaml'])
            self.fail("command-line arg should have failed")
        except SystemExit, e:
            self.assertNotEquals(0, e.code)

        ps = get_param_server()

        # load into top-level
        rosparam.yamlmain([cmd, 'load', f])
        self.assertEquals('bar', ps.getParam('/', '/foo')[2])
        # - make sure it did an overlay, not erase
        self.assertEquals('foo-value', ps.getParam('/', '/string')[2])

        rosparam.yamlmain([cmd, 'load', '-v', f])
        self.assertEquals('bar', ps.getParam('/', '/foo')[2])

        # load into namespace
        rosparam.yamlmain([cmd, 'load', f, '/rosparam_load/test'])
        self.assertEquals('bar',
                          ps.getParam('/', '/rosparam_load/test/foo')[2])
        rosparam.yamlmain([cmd, 'load', '-v', f, '/rosparam_load/test'])
    def test_rosparam(self):
        ps = get_param_server()

        # network is initialized
        cmd = 'rosparam'
        names = ['/chatter', 'foo/chatter']

        # list
        params = [
            '/string',
            '/int',
            '/float',
            '/g1/string',
            '/g1/int',
            '/g1/float',
            '/g2/string',
            '/g2/int',
            '/g2/float',
        ]
        # - we aren't matching against the core services as those can make the test suites brittle
        output = Popen([cmd, 'list'], stdout=PIPE).communicate()[0]
        l = set(output.split())
        for t in params:
            self.assert_(t in l)

        # get
        # - strings
        output = Popen([cmd, 'get', "string"], stdout=PIPE).communicate()[0]
        self.assertEquals('foo-value', output.strip())
        # -- pretty
        output = Popen([cmd, 'get', '-p', "string"],
                       stdout=PIPE).communicate()[0]
        self.assertEquals('foo-value', output.strip())
        output = Popen([cmd, 'get', "/string"], stdout=PIPE).communicate()[0]
        self.assertEquals('foo-value', output.strip())
        output = Popen([cmd, 'get', "g1/string"], stdout=PIPE).communicate()[0]
        self.assertEquals('g1-foo-value', output.strip())
        output = Popen([cmd, 'get', "/g1/string"],
                       stdout=PIPE).communicate()[0]
        self.assertEquals('g1-foo-value', output.strip())
        output = Popen([cmd, 'get', "/g2/string"],
                       stdout=PIPE).communicate()[0]
        self.assertEquals('g2-foo-value', output.strip())
        # - ints
        output = Popen([cmd, 'get', "int"], stdout=PIPE).communicate()[0]
        self.assertEquals('1', output.strip())
        # -- pretty
        output = Popen([cmd, 'get', '-p', "int"], stdout=PIPE).communicate()[0]
        self.assertEquals('1', output.strip())
        output = Popen([cmd, 'get', "/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('1', output.strip())
        output = Popen([cmd, 'get', "g1/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('10', output.strip())
        output = Popen([cmd, 'get', "/g1/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('10', output.strip())
        output = Popen([cmd, 'get', "/g2/int"], stdout=PIPE).communicate()[0]
        self.assertEquals('20', output.strip())
        # - floats
        output = Popen([cmd, 'get', "float"], stdout=PIPE).communicate()[0]
        self.assertEquals('1.0', output.strip())
        # -- pretty
        output = Popen([cmd, 'get', '-p', "float"],
                       stdout=PIPE).communicate()[0]
        self.assertEquals('1.0', output.strip())
        output = Popen([cmd, 'get', "/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('1.0', output.strip())
        output = Popen([cmd, 'get', "g1/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('10.0', output.strip())
        output = Popen([cmd, 'get', "/g1/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('10.0', output.strip())
        output = Popen([cmd, 'get', "/g2/float"], stdout=PIPE).communicate()[0]
        self.assertEquals('20.0', output.strip())
        # - dictionary
        output = Popen([cmd, 'get', "g1"], stdout=PIPE).communicate()[0]
        import yaml
        d = yaml.load(output)
        self.assertEquals(d['float'], 10.0)
        self.assertEquals(d['int'], 10.0)
        self.assertEquals(d['string'], "g1-foo-value")
        self.assertEquals(set(['float', 'int', 'string']), set(d.keys()))

        # -- don't bother parsing pretty output of dictionary, but check for no errors
        check_call([cmd, 'get', '-p', "g1"])
        # --- with verbose
        check_call([cmd, 'get', '-pv', "g1"])

        # set
        # - integers
        Popen([cmd, 'set', "/set/test1", "1"], stdout=PIPE).communicate()[0]
        self.assertEquals(1, ps.getParam('/', '/set/test1')[2])
        # -- verbose
        Popen([cmd, 'set', '-v', "/set/test1", "1"],
              stdout=PIPE).communicate()[0]
        self.assertEquals(1, ps.getParam('/', '/set/test1')[2])
        Popen([cmd, 'set', "set/test1", "2"], stdout=PIPE).communicate()[0]
        self.assertEquals(2, ps.getParam('/', '/set/test1')[2])
        # - floats
        Popen([cmd, 'set', "/set/test2", "1.0"], stdout=PIPE).communicate()[0]
        self.assertEquals(1, ps.getParam('/', '/set/test2')[2])
        Popen([cmd, 'set', "set/test2", "2.0"], stdout=PIPE).communicate()[0]
        self.assertEquals(2, ps.getParam('/', '/set/test2')[2])
        # - booleans
        Popen([cmd, 'set', "/set/testbool", "true"],
              stdout=PIPE).communicate()[0]
        self.assertEquals(True, ps.getParam('/', '/set/testbool')[2])
        Popen([cmd, 'set', "set/testbool", "false"],
              stdout=PIPE).communicate()[0]
        self.assertEquals(False, ps.getParam('/', '/set/testbool')[2])
        # - strings
        #   TODO: test more interesting encodings, like multi-line
        Popen([cmd, 'set', "/set/teststr", "hi"], stdout=PIPE).communicate()[0]
        self.assertEquals("hi", ps.getParam('/', '/set/teststr')[2])
        Popen([cmd, 'set', "set/teststr", "hello world"],
              stdout=PIPE).communicate()[0]
        self.assertEquals("hello world", ps.getParam('/', '/set/teststr')[2])
        Popen([cmd, 'set', "set/teststr", "'true'"],
              stdout=PIPE).communicate()[0]
        self.assertEquals("true", ps.getParam('/', '/set/teststr')[2])
        # - list
        Popen([cmd, 'set', "set/testlist", "[]"], stdout=PIPE).communicate()[0]
        self.assertEquals([], ps.getParam('/', '/set/testlist')[2])
        Popen([cmd, 'set', "/set/testlist", "[1, 2, 3]"],
              stdout=PIPE).communicate()[0]
        self.assertEquals([1, 2, 3], ps.getParam('/', '/set/testlist')[2])
        # - dictionary
        Popen([cmd, 'set', "/set/testdict", "{a: b, c: d}"],
              stdout=PIPE).communicate()[0]
        self.assertEquals('b', ps.getParam('/', '/set/testdict/a')[2])
        self.assertEquals('d', ps.getParam('/', '/set/testdict/c')[2])
        #   - empty dictionary should be a noop
        Popen([cmd, 'set', "set/testdict", "{}"], stdout=PIPE).communicate()[0]
        self.assertEquals('b', ps.getParam('/', '/set/testdict/a')[2])
        self.assertEquals('d', ps.getParam('/', '/set/testdict/c')[2])
        #   - this should be an update
        Popen([cmd, 'set', "/set/testdict", "{e: f, g: h}"],
              stdout=PIPE).communicate()[0]
        self.assertEquals('b', ps.getParam('/', '/set/testdict/a')[2])
        self.assertEquals('d', ps.getParam('/', '/set/testdict/c')[2])
        self.assertEquals('f', ps.getParam('/', '/set/testdict/e')[2])
        self.assertEquals('h', ps.getParam('/', '/set/testdict/g')[2])
        # -- verbose
        check_call([cmd, 'set', '-v', "/set/testdictverbose", "{e: f, g: h}"])

        # delete
        ps.setParam('/', '/delete/me', True)
        self.assert_(ps.hasParam('/', '/delete/me')[2])
        Popen([cmd, 'delete', "/delete/me"], stdout=PIPE).communicate()[0]
        self.failIf(ps.hasParam('/', '/delete/me')[2])
示例#21
0
def _set_param(param, value):
    succeed(get_param_server().setParam(_get_caller_id(), param, value))
示例#22
0
#!/usr/bin/env python
import roslib.scriptutil as s
s.get_param_server().setParam('/', 'robot_description',
                              open('pr2_arm.xml').read())