ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
5

How to setParam xmlrpc value struct with C++ ?

asked 2017-01-19 05:35:35 -0600

updated 2017-01-19 08:00:11 -0600

I have getParam() working. The initial data is loaded from a configuration yaml from my launch file.

I am trying to figure out how to write a C++ structure out using setParam() to update the same data.

What do I create in C++ in order to call setParam() and store the data in the parameter server?

Here is an example of the yaml:

drive_config:
  mode: mecanum
  radius: 0.062
  rpm: 60.0
  scale: 1.0
  track: 0.2
  servos:
    - {servo: 1, position: 1}
    - {servo: 2, position: 2}
    - {servo: 3, position: 3}
    - {servo: 4, position: 4}
edit retag flag offensive close merge delete

Comments

1

(I'll edit this again once I figure out how to include formatted code)

select the text, press ctrl+k or click the Preformatted Text button (the one with 101010 on it).

gvdhoorn gravatar image gvdhoorn  ( 2017-01-19 06:48:30 -0600 )edit

re: (I'll edit this again once I figure out how to include formatted code)

I was on a mobile device. The 101010 button did not do it correctly. Thanks for fixing my question.

suforeman gravatar image suforeman  ( 2017-01-19 08:00:02 -0600 )edit

is there any easy to set the .xml file to rosserver??? Must I re-write the contents of my .yaml in c++ code to build the xmlrpc as above??? xmlrpc can not load .xml or .yaml directly?

yueweiliang gravatar image yueweiliang  ( 2019-06-22 08:12:16 -0600 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2018-09-28 13:48:58 -0600

tahsinkose gravatar image

I have encountered the exact same problem today and the solution was not a straightforward one. Unfortunately XmlRpc API does not have a dedicated method for the custom structs. Therefore, you need to write your own function that validly constructs an XML from your struct.

Following code constructs a valid portion of your yaml file which affects the parameter server in the exact same manner as yaml file would.

const string init_value = "<value>";
const string end_value = "</value>";
const string init_struct = "<struct>";
const string end_struct = "</struct>";

There are also <data>, <array> and <member> tags. They should be defined as above. Then for only the mode part:

XmlRpc::XmlRpcValue drive_config;
string xml_body;
xml_body.append(init_value);
xml_body.append(init_struct);
xml_body.append(init_member);
xml_body.append("<name>mode</name>");
xml_body.append(init_value);
xml_body.append("mecanum");
xml_body.append(end_value);
xml_body.append(end_member);
xml_body.append(end_struct);
xml_body.append(end_value);

int offset = 0;
int* offset_ptr = &offset;
drive_config.fromXml(xml_body,offset_ptr);
nh.setParam("/drive_config",drive_config);

Above lines will upload a parameter to ROS Parameter Server in the form of:

drive_config:
    mode: mecanum

Above code can be optimized, automated and shortened with struct tag retrievals, type resolvers and recursive calls. But for the current task, naïve implementation would suffice.

I recommend first printing the raw XML format of a known parameter uploaded through YAML file. Then, it will be easier to construct arrays of arrays, arrays of dicts, dicts of arrays and etc.

edit flag offensive delete link more
1

answered 2019-04-24 01:42:21 -0600

mihota gravatar image

I found a way:

XmlRpc::XmlRpcValue a;
a.setSize(2);
a[0] = 0;
a[1] = 1;
ros::param::set("param_name", a);

Just keep nesting this and you can set any parameter structure you want.

edit flag offensive delete link more

Comments

this works for modifying the retrieved data and can then be used to set a param on the server as was the question!

harshal gravatar image harshal  ( 2021-01-23 18:31:12 -0600 )edit

Thank you so much. You can even do

a["name"] = 3;

and skip the setSize. Awesome.

luckyyou gravatar image luckyyou  ( 2021-02-04 11:16:25 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-01-19 05:35:35 -0600

Seen: 1,519 times

Last updated: Jun 22 '19