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

How to implement complex parameters in cpp?

asked 2016-12-31 05:29:58 -0500

I have written a package to support simple PWM servo motors. To support some features, the node must be initialized with settings for each servo. Here is an example:

/config_servos "servos: [{servo: 1, center: 333, range: 100, direction: -1}, {servo: 2, center: 336, range: 108, direction: 1}]"

This initialized two servos - each having a center, range, and direction parameter. There can be many servos.

Currently a user of the node, must make a service call to perform the configuration. I'd like to make it possible for this setup to be done via the launch file or some similar external file rather than require it be coded.

Is there an example or best practice for how to initialize objects?

Here is a link to the draft documentation which shows all of the current configuration services: http://bradanlane.gitlab.io/ros-i2cpw...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-12-31 06:34:00 -0500

NEngelhard gravatar image

updated 2016-12-31 11:09:32 -0500

You could load a yaml-file with your parameters: rosparam load

asd.yaml:

base_joint_motor: 
  center: 333
  range: 100
  direction: -1
wrist_joint_motor:
  center: 336
  range: 108
  direction: 1

or if you have a list of similar motors:

servos:
  - id: 1
    center: 333
    range: 100
    direction: -1
  - id: 2
    center: 336
    range: 108
    direction: 1

You can load this file in a launch file or on the command line (with added namespace).

// Added @gvdhoorn's proposal with lists

edit flag offensive delete link more

Comments

It looks like the yaml file example shows "simple" data. My code has a few simple parameters such as "PWM frequency". Where I struggle is with "objects" - e.g. The user may need to configure several servos and each servo has several values. Is there a standard method for initializing objects?

suforeman gravatar image suforeman  ( 2016-12-31 06:47:08 -0500 )edit

"It looks like the yaml file example shows "simple" data." It shows exactly the data you provided. Could you tell a bit more about the structure of your parameters? And please include all information into your question. You asked for A, got a solution for A and now complain that it does not solve B.

NEngelhard gravatar image NEngelhard  ( 2016-12-31 06:50:27 -0500 )edit

Sorry, was not trying to complain. I had started my comment response before you had finished updating your comment with example.

Numeric postfixing got quite messy when I had "objects inside objects". I would need to restructure my service calls. I may choose that route if that is best.

suforeman gravatar image suforeman  ( 2016-12-31 06:58:02 -0500 )edit
2

@NEngelhard chose to use suffixes, but there is no requirement for that: rosparam supports yaml lists and dictionaries, and those don't require suffixes. ROS parameters in yaml lists do often include numeric IDs (but as separate fields), as those make it easy to identify specific entries.

gvdhoorn gravatar image gvdhoorn  ( 2016-12-31 07:11:14 -0500 )edit

An example list would be:

servos:
  - id: 1
    center: 333
    range: 100
    direction: -1
  - id: 2
    center: 336
    range: 108
    direction: 1
gvdhoorn gravatar image gvdhoorn  ( 2016-12-31 07:12:25 -0500 )edit

Ah! I never though of moving the index to be an ID. I hadn't found that example. Thanks for the added suggestion @gvdhoorn. I'll map out my current initialization services and try to get this working.

suforeman gravatar image suforeman  ( 2016-12-31 07:22:52 -0500 )edit

Followup: I found a starting point which combines my scenario and the above suggestions. The result looks something like this:

/servos          : '1' : {center: 331, direction: -1, range: 101}
/servos/1        : {center: 331, direction: -1, range: 101}
/servos/1/center : 331
suforeman gravatar image suforeman  ( 2016-12-31 09:11:05 -0500 )edit

the above is based off of this question/answer - http://answers.ros.org/question/19827...

suforeman gravatar image suforeman  ( 2016-12-31 09:31:49 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2016-12-31 05:29:58 -0500

Seen: 263 times

Last updated: Dec 31 '16